CommanDelete: Fixed pattern match and added dry-run mode

Tento commit je obsažen v:
Albert S. 2019-04-09 21:38:15 +02:00
rodič 53db42e038
revize 03320ad6eb

Zobrazit soubor

@ -9,8 +9,12 @@
int CommandDelete::handle(QStringList arguments)
{
QCommandLineParser parser;
parser.addOptions({{{"v", "verbose"}, "Print path of the files while deleting them"},
{"pattern", "Only delete files from index matching the pattern, e. g. */.git/*", "pattern"},
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();
@ -19,6 +23,7 @@ int CommandDelete::handle(QStringList arguments)
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
@ -34,7 +39,7 @@ int CommandDelete::handle(QStringList arguments)
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);
if(!pathsQuery.exec())
{
@ -45,10 +50,17 @@ int CommandDelete::handle(QStringList arguments)
while(pathsQuery.next())
{
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);
if(!file.exists())
{
if(!dryRun)
{
QSqlQuery query("DELETE FROM file WHERE path = ?", db);
query.addBindValue(path);
@ -62,6 +74,11 @@ int CommandDelete::handle(QStringList arguments)
qInfo() << "Deleted " << path;
}
}
else
{
qInfo() << "Would delete " << path;
}
}
}
}
}
@ -72,6 +89,8 @@ int CommandDelete::handle(QStringList arguments)
QFileInfo fileInfo(file);
QString absPath = fileInfo.absoluteFilePath();
if(fileExistsInDatabase(db, absPath))
{
if(!dryRun)
{
QSqlQuery deletionQuery("DELETE FROM file WHERE path = ?", db);
deletionQuery.addBindValue(absPath);
@ -87,6 +106,7 @@ int CommandDelete::handle(QStringList arguments)
qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError();
}
}
}
else
{
qInfo() << "No such file in database:" << absPath;