From 6da823d7f36bc5dc4fa160901074a9ebf468a0a1 Mon Sep 17 00:00:00 2001 From: Albert S Date: Tue, 9 Apr 2019 22:03:04 +0200 Subject: [PATCH] CommandDelete: refactor: split handle to several methods --- cli/commanddelete.cpp | 139 ++++++++++++++++++++++++------------------ cli/commanddelete.h | 3 + 2 files changed, 82 insertions(+), 60 deletions(-) diff --git a/cli/commanddelete.cpp b/cli/commanddelete.cpp index 5e52f1f..351d613 100644 --- a/cli/commanddelete.cpp +++ b/cli/commanddelete.cpp @@ -6,90 +6,68 @@ #include #include "commanddelete.h" -int CommandDelete::handle(QStringList arguments) + +int CommandDelete::removeNonExistent(QSqlDatabase &db, bool verbose, bool dryRun, QString pattern) { - QCommandLineParser parser; - parser.addOptions({ - { { "v", "verbose" }, "Print path of the files while deleting them" }, - { { "n", "dry-run"}, "Only print which files would be deleted from the database, don't delete them"}, - { "pattern", "Only delete files from index matching the pattern, e. g. */.git/*. Only applies to --deleted or standalone.", "pattern" }, - { "deleted", "Delete all files from the index that don't exist anymore" } - }); - - parser.addHelpOption(); - parser.addPositionalArgument("delete", "Delete paths from the index", "delete [paths...]"); - - parser.process(arguments); - bool removeNonExistant = parser.isSet("deleted"); - bool verbose = parser.isSet("verbose"); - bool dryRun = parser.isSet("dry-run"); - QString pattern = parser.value("pattern"); - - //TODO: port this to QRegularExpression once >5.12 gets more widespread because of this bug //https://bugreports.qt.io/browse/QTBUG-72539?focusedCommentId=439053&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel - // QRegularExpression regexPattern = QRegularExpression::wildcardToRegularExpression(pattern); - + // QRegularExpression regexPattern = QRegularExpression::wildcardToRegularExpression(pattern bool usePattern = ! pattern.isEmpty(); QRegExp regexPattern(pattern); regexPattern.setPatternSyntax(QRegExp::PatternSyntax::WildcardUnix); - - QSqlDatabase db = dbConnection(); - - - if(removeNonExistant) + //TODO: try to translate pattern to SQL WHERE statement + QSqlQuery pathsQuery("SELECT path FROM file", db); + if(!pathsQuery.exec()) { - //TODO: try to translate pattern to SQL WHERE statement - QSqlQuery pathsQuery("SELECT path FROM file", db); - if(!pathsQuery.exec()) + qDebug() << "Failed to query current paths"; + return 1; + } + + while(pathsQuery.next()) + { + QString path = pathsQuery.value(0).toString(); + bool removeFile = true; + if(usePattern) { - qDebug() << "Failed to query current paths"; - return 1; + removeFile = regexPattern.exactMatch(path); } - - while(pathsQuery.next()) + if(removeFile) { - QString path = pathsQuery.value(0).toString(); - bool removeFile = true; - if(usePattern) + QFile file(path); + if(!file.exists()) { - removeFile = regexPattern.exactMatch(path); - } - if(removeFile) - { - QFile file(path); - if(!file.exists()) + if(!dryRun) { - if(!dryRun) + QSqlQuery query("DELETE FROM file WHERE path = ?", db); + query.addBindValue(path); + if(!query.exec()) { - QSqlQuery query("DELETE FROM file WHERE path = ?", db); - query.addBindValue(path); - if(!query.exec()) - { - qDebug() << "Failed to delete " << path << query.lastError(); - return 1; - } - if(verbose) - { - qInfo() << "Deleted " << path; - } + qDebug() << "Failed to delete " << path << query.lastError(); + return 1; } - else + if(verbose) { - qInfo() << "Would delete " << path; + qInfo() << "Deleted " << path; } - } + else + { + qInfo() << "Would delete " << path; + } + } } } +} - QStringList files = parser.positionalArguments(); - for(QString &file : files) +int CommandDelete::removePaths(const QStringList &paths, QSqlDatabase &db, bool verbose, bool dryRun) +{ + int result = 0; + for(const QString &file : paths) { QFileInfo fileInfo(file); QString absPath = fileInfo.absoluteFilePath(); @@ -109,6 +87,7 @@ int CommandDelete::handle(QStringList arguments) else { qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError(); + result = 1; } } @@ -116,9 +95,49 @@ int CommandDelete::handle(QStringList arguments) else { qInfo() << "No such file in database:" << absPath; + result = 1; } - } + return result; +} +int CommandDelete::handle(QStringList arguments) +{ + QCommandLineParser parser; + parser.addOptions({ + { { "v", "verbose" }, "Print path of the files while deleting them" }, + { { "n", "dry-run"}, "Only print which files would be deleted from the database, don't delete them"}, + { "pattern", "Only delete files from index matching the pattern, e. g. */.git/*. Only applies to --deleted or standalone.", "pattern" }, + { "deleted", "Delete all files from the index that don't exist anymore" } + }); + + parser.addHelpOption(); + parser.addPositionalArgument("delete", "Delete paths from the index", "delete [paths...]"); + + parser.process(arguments); + bool verbose = parser.isSet("verbose"); + bool dryRun = parser.isSet("dry-run"); + QString pattern = parser.value("pattern"); + QSqlDatabase db = dbConnection(); + + + if(parser.isSet("deleted")) + { + + int result = removeNonExistent(db, verbose, dryRun, pattern); + if(result != 0) + { + return result; + } + } + + QStringList files = parser.positionalArguments(); + int result = removePaths(files,db, verbose, dryRun); + if(result != 0) + { + return result; + } + + return 0; } diff --git a/cli/commanddelete.h b/cli/commanddelete.h index 61eadde..c1b3070 100644 --- a/cli/commanddelete.h +++ b/cli/commanddelete.h @@ -8,6 +8,9 @@ public: using Command::Command; int handle(QStringList arguments) override; +private: + int removeNonExistent(QSqlDatabase &db, bool verbose, bool dryRun, QString pattern); + int removePaths(const QStringList &paths, QSqlDatabase &db, bool verbose, bool dryRun); }; #endif // COMMANDDELETE_H