CommanDelete: Fixed pattern match and added dry-run mode
This commit is contained in:
parent
61f8a2d8cd
commit
4fc8a7e37b
@ -11,7 +11,8 @@ int CommandDelete::handle(QStringList arguments)
|
|||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
parser.addOptions({
|
parser.addOptions({
|
||||||
{ { "v", "verbose" }, "Print path of the files while deleting them" },
|
{ { "v", "verbose" }, "Print path of the files while deleting them" },
|
||||||
{ "pattern", "Only delete files from index matching the pattern, e. g. */.git/*", "pattern" },
|
{ { "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" }
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -21,6 +22,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");
|
||||||
|
|
||||||
|
|
||||||
@ -41,7 +43,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())
|
||||||
{
|
{
|
||||||
@ -52,10 +54,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);
|
||||||
@ -69,6 +78,12 @@ int CommandDelete::handle(QStringList arguments)
|
|||||||
qInfo() << "Deleted " << path;
|
qInfo() << "Deleted " << path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qInfo() << "Would delete " << path;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -79,6 +94,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);
|
||||||
@ -94,6 +111,8 @@ 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;
|
||||||
|
Loading…
Reference in New Issue
Block a user