CommanDelete: Fixed pattern match and added dry-run mode

Bu işleme şunda yer alıyor:
Albert S. 2019-04-09 21:38:15 +02:00
ebeveyn 53db42e038
işleme 03320ad6eb

Dosyayı Görüntüle

@ -9,8 +9,12 @@
int CommandDelete::handle(QStringList arguments) int CommandDelete::handle(QStringList arguments)
{ {
QCommandLineParser parser; QCommandLineParser parser;
parser.addOptions({{{"v", "verbose"}, "Print path of the files while deleting them"}, parser.addOptions(
{"pattern", "Only delete files from index matching the pattern, e. g. */.git/*", "pattern"}, {{{"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"}}); {"deleted", "Delete all files from the index that don't exist anymore"}});
parser.addHelpOption(); parser.addHelpOption();
@ -19,6 +23,7 @@ int CommandDelete::handle(QStringList arguments)
parser.process(arguments); parser.process(arguments);
bool removeNonExistant = parser.isSet("deleted"); bool removeNonExistant = parser.isSet("deleted");
bool verbose = parser.isSet("verbose"); bool verbose = parser.isSet("verbose");
bool dryRun = parser.isSet("dry-run");
QString pattern = parser.value("pattern"); QString pattern = parser.value("pattern");
// TODO: port this to QRegularExpression once >5.12 gets more widespread because of this bug // TODO: port this to QRegularExpression once >5.12 gets more widespread because of this bug
@ -34,7 +39,7 @@ int CommandDelete::handle(QStringList arguments)
if(removeNonExistant) if(removeNonExistant)
{ {
// TODO: try to translate pattern to SQL WHERE // TODO: try to translate pattern to SQL WHERE statement
QSqlQuery pathsQuery("SELECT path FROM file", db); QSqlQuery pathsQuery("SELECT path FROM file", db);
if(!pathsQuery.exec()) if(!pathsQuery.exec())
{ {
@ -45,10 +50,17 @@ int CommandDelete::handle(QStringList arguments)
while(pathsQuery.next()) while(pathsQuery.next())
{ {
QString path = pathsQuery.value(0).toString(); QString path = pathsQuery.value(0).toString();
if(usePattern && regexPattern.exactMatch(path)) bool removeFile = true;
if(usePattern)
{
removeFile = regexPattern.exactMatch(path);
}
if(removeFile)
{ {
QFile file(path); QFile file(path);
if(!file.exists()) if(!file.exists())
{
if(!dryRun)
{ {
QSqlQuery query("DELETE FROM file WHERE path = ?", db); QSqlQuery query("DELETE FROM file WHERE path = ?", db);
query.addBindValue(path); query.addBindValue(path);
@ -62,6 +74,11 @@ int CommandDelete::handle(QStringList arguments)
qInfo() << "Deleted " << path; qInfo() << "Deleted " << path;
} }
} }
else
{
qInfo() << "Would delete " << path;
}
}
} }
} }
} }
@ -72,6 +89,8 @@ int CommandDelete::handle(QStringList arguments)
QFileInfo fileInfo(file); QFileInfo fileInfo(file);
QString absPath = fileInfo.absoluteFilePath(); QString absPath = fileInfo.absoluteFilePath();
if(fileExistsInDatabase(db, absPath)) if(fileExistsInDatabase(db, absPath))
{
if(!dryRun)
{ {
QSqlQuery deletionQuery("DELETE FROM file WHERE path = ?", db); QSqlQuery deletionQuery("DELETE FROM file WHERE path = ?", db);
deletionQuery.addBindValue(absPath); deletionQuery.addBindValue(absPath);
@ -87,6 +106,7 @@ int CommandDelete::handle(QStringList arguments)
qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError(); qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError();
} }
} }
}
else else
{ {
qInfo() << "No such file in database:" << absPath; qInfo() << "No such file in database:" << absPath;