CommanDelete: Fixed pattern match and added dry-run mode
这个提交包含在:
父节点
53db42e038
当前提交
03320ad6eb
@ -9,9 +9,13 @@
|
|||||||
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"},
|
||||||
{"deleted", "Delete all files from the index that don't exist anymore"}});
|
{{"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.addHelpOption();
|
||||||
parser.addPositionalArgument("delete", "Delete paths from the index", "delete [paths...]");
|
parser.addPositionalArgument("delete", "Delete paths from the index", "delete [paths...]");
|
||||||
@ -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,21 +50,33 @@ 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())
|
||||||
{
|
{
|
||||||
QSqlQuery query("DELETE FROM file WHERE path = ?", db);
|
if(!dryRun)
|
||||||
query.addBindValue(path);
|
|
||||||
if(!query.exec())
|
|
||||||
{
|
{
|
||||||
qDebug() << "Failed to delete " << path << query.lastError();
|
QSqlQuery query("DELETE FROM file WHERE path = ?", db);
|
||||||
return 1;
|
query.addBindValue(path);
|
||||||
|
if(!query.exec())
|
||||||
|
{
|
||||||
|
qDebug() << "Failed to delete " << path << query.lastError();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(verbose)
|
||||||
|
{
|
||||||
|
qInfo() << "Deleted " << path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(verbose)
|
else
|
||||||
{
|
{
|
||||||
qInfo() << "Deleted " << path;
|
qInfo() << "Would delete " << path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -73,18 +90,21 @@ int CommandDelete::handle(QStringList arguments)
|
|||||||
QString absPath = fileInfo.absoluteFilePath();
|
QString absPath = fileInfo.absoluteFilePath();
|
||||||
if(fileExistsInDatabase(db, absPath))
|
if(fileExistsInDatabase(db, absPath))
|
||||||
{
|
{
|
||||||
QSqlQuery deletionQuery("DELETE FROM file WHERE path = ?", db);
|
if(!dryRun)
|
||||||
deletionQuery.addBindValue(absPath);
|
|
||||||
if(deletionQuery.exec())
|
|
||||||
{
|
{
|
||||||
if(verbose)
|
QSqlQuery deletionQuery("DELETE FROM file WHERE path = ?", db);
|
||||||
|
deletionQuery.addBindValue(absPath);
|
||||||
|
if(deletionQuery.exec())
|
||||||
{
|
{
|
||||||
qInfo() << "Deleted" << absPath;
|
if(verbose)
|
||||||
|
{
|
||||||
|
qInfo() << "Deleted" << absPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
正在加载...
x
在新工单中引用
屏蔽一个用户