CommandDelete: Rework deletion logic

Allow deleting files from index which still exist
on the fileystem without passing their path to "qss delete".

Thus: "qss delete --deleted" deletes all files which
don't exist anymore.

Also, fix some bugs in offset calculation.
This commit is contained in:
Albert S. 2020-08-30 18:12:42 +02:00
parent 1455d8ac1b
commit a6f73d724a
2 changed files with 94 additions and 76 deletions

View File

@ -7,8 +7,9 @@
#include "commanddelete.h" #include "commanddelete.h"
#include "logger.h" #include "logger.h"
int CommandDelete::removeNonExistent(bool verbose, bool dryRun, QString pattern) int CommandDelete::remove(QString pattern, bool onlyDeleted, bool verbose, bool dryRun)
{ {
int deleted = 0;
int offset = 0; int offset = 0;
int limit = 1000; int limit = 1000;
QVector<FileData> files; QVector<FileData> files;
@ -17,8 +18,14 @@ int CommandDelete::removeNonExistent(bool verbose, bool dryRun, QString pattern)
{ {
for(FileData &file : files) for(FileData &file : files)
{ {
QFileInfo fileInfo(file.absPath); if(onlyDeleted && QFileInfo::exists(file.absPath))
if(!fileInfo.exists()) {
if(verbose)
{
Logger::info() << "Skipping " << file.absPath << " as the file exists on the file system" << endl;
}
}
else
{ {
if(!dryRun) if(!dryRun)
{ {
@ -28,11 +35,11 @@ int CommandDelete::removeNonExistent(bool verbose, bool dryRun, QString pattern)
{ {
Logger::info() << "Deleted" << file.absPath << endl; Logger::info() << "Deleted" << file.absPath << endl;
} }
++deleted;
} }
else else
{ {
Logger::error()<< "Failed to delete:" << file.absPath << ", exiting." << endl Logger::error()<< "Failed to delete:" << file.absPath << ", exiting." << endl;
;
return 1; return 1;
} }
} }
@ -42,12 +49,21 @@ int CommandDelete::removeNonExistent(bool verbose, bool dryRun, QString pattern)
} }
} }
} }
if(dryRun)
{
offset += limit; offset += limit;
}
else
{
offset = offset + limit - deleted;
}
files.clear();
processedRows = this->dbService->getFiles(files, pattern, offset, limit); processedRows = this->dbService->getFiles(files, pattern, offset, limit);
deleted = 0;
} }
return 0; return 0;
} }
int CommandDelete::removePaths(const QStringList &paths, bool verbose, bool dryRun) int CommandDelete::removePaths(const QStringList &paths, bool verbose, bool dryRun)
{ {
@ -90,8 +106,8 @@ int CommandDelete::handle(QStringList arguments)
parser.addOptions({ parser.addOptions({
{ { "v", "verbose" }, "Print path of the files while deleting them" }, { { "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"}, { { "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" }, { "pattern", "Only delete files from index matching the pattern, e. g. */.git/*. Can be used to restrict --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. Can be restricted by --pattern." }
}); });
parser.addHelpOption(); parser.addHelpOption();
@ -100,17 +116,21 @@ int CommandDelete::handle(QStringList arguments)
parser.process(arguments); parser.process(arguments);
bool verbose = parser.isSet("verbose"); bool verbose = parser.isSet("verbose");
bool dryRun = parser.isSet("dry-run"); bool dryRun = parser.isSet("dry-run");
bool deleted = parser.isSet("deleted");
QString pattern = parser.value("pattern"); QString pattern = parser.value("pattern");
if(parser.isSet("deleted"))
{
int result = removeNonExistent(verbose, dryRun, pattern);
if(deleted || ! pattern.isEmpty())
{
int result = this->remove(pattern, deleted, verbose, dryRun);
if(result != 0) if(result != 0)
{ {
Logger::error() << "Removal operation did not succeed" << endl;
return result; return result;
} }
} }
QStringList files = parser.positionalArguments(); QStringList files = parser.positionalArguments();
if(files.length() > 0) if(files.length() > 0)
{ {
@ -123,5 +143,3 @@ int CommandDelete::handle(QStringList arguments)
return 0; return 0;
} }

View File

@ -9,7 +9,7 @@ public:
int handle(QStringList arguments) override; int handle(QStringList arguments) override;
private: private:
int removeNonExistent(bool verbose, bool dryRun, QString pattern); int remove(QString pattern, bool onlyDeleted, bool verbose, bool dryRun);
int removePaths(const QStringList &paths, bool verbose, bool dryRun); int removePaths(const QStringList &paths, bool verbose, bool dryRun);
}; };