2019-04-16 08:51:27 +02:00
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QThreadPool>
|
2019-04-07 20:22:20 +02:00
|
|
|
#include "commandupdate.h"
|
2019-04-16 08:51:27 +02:00
|
|
|
#include "logger.h"
|
2019-04-07 20:22:20 +02:00
|
|
|
|
|
|
|
int CommandUpdate::handle(QStringList arguments)
|
|
|
|
{
|
2019-04-16 08:51:27 +02:00
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.addOptions(
|
|
|
|
{{{"v", "verbose"}, "Print path of the files while updating them"},
|
|
|
|
{{"n", "dry-run"}, "Only print which files would be updated, don't actually update them"},
|
|
|
|
{"pattern", "Only consider to update files in the index matching the pattern, e. g. */.git/*.", "pattern"},
|
|
|
|
{{"d", "delete"}, "If a file does not exist anymore, delete it"},
|
2019-04-30 23:43:31 +02:00
|
|
|
{{"c", "continue"},
|
2021-06-12 14:59:58 +02:00
|
|
|
"Continue adding files, don't exit on first error. If this option is not given, looqs will exit asap, but "
|
|
|
|
"it's possible that a few files will still be processed. "
|
2019-04-30 23:43:31 +02:00
|
|
|
"Set -t 1 to avoid this behavior, but processing will be slower."},
|
2019-04-16 08:51:27 +02:00
|
|
|
{{"a", "all"}, "On error, no files should be updated, even already processed ones"},
|
|
|
|
{{"t", "threads"}, "Number of threads to use.", "threads"}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.addPositionalArgument("update", "Checks files for changes and updates them", "update");
|
|
|
|
|
|
|
|
parser.process(arguments);
|
|
|
|
bool keepGoing = parser.isSet("continue");
|
|
|
|
bool verbose = parser.isSet("verbose");
|
|
|
|
bool deleteMissing = parser.isSet("delete");
|
|
|
|
bool dryRun = parser.isSet("dry-run");
|
|
|
|
QString pattern = parser.value("pattern");
|
|
|
|
|
|
|
|
if(parser.isSet("all"))
|
|
|
|
{
|
2021-06-12 14:59:58 +02:00
|
|
|
throw LooqsGeneralException("To be implemented");
|
2019-04-16 08:51:27 +02:00
|
|
|
}
|
|
|
|
if(parser.isSet("threads"))
|
|
|
|
{
|
|
|
|
QString threadsCount = parser.value("threads");
|
|
|
|
QThreadPool::globalInstance()->setMaxThreadCount(threadsCount.toInt());
|
|
|
|
}
|
|
|
|
FileSaver saver(*this->dbService);
|
|
|
|
QVector<FileData> files;
|
|
|
|
int offset = 0;
|
|
|
|
int limit = 1000;
|
|
|
|
int processedRows = dbService->getFiles(files, pattern, offset, limit);
|
|
|
|
|
|
|
|
while(processedRows > 0)
|
|
|
|
{
|
|
|
|
QVector<QString> filePathsToUpdate;
|
|
|
|
for(FileData &fileData : files)
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo(fileData.absPath);
|
|
|
|
if(fileInfo.exists() && fileInfo.isFile())
|
|
|
|
{
|
|
|
|
if(fileInfo.lastModified().toSecsSinceEpoch() != fileData.mtime)
|
|
|
|
{
|
|
|
|
if(!dryRun)
|
|
|
|
{
|
|
|
|
filePathsToUpdate.append(fileData.absPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-12 22:55:56 +02:00
|
|
|
Logger::info() << "Would update" << fileData.absPath << Qt::endl;
|
2019-04-16 08:51:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(deleteMissing)
|
|
|
|
{
|
|
|
|
if(!dryRun)
|
|
|
|
{
|
|
|
|
if(!this->dbService->deleteFile(fileData.absPath))
|
|
|
|
{
|
2021-06-12 22:55:56 +02:00
|
|
|
Logger::error()
|
|
|
|
<< "Error: Failed to delete" << fileData.absPath << "from databas" << Qt::endl;
|
2019-04-16 08:51:27 +02:00
|
|
|
if(!keepGoing)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(verbose)
|
|
|
|
{
|
2021-06-12 22:55:56 +02:00
|
|
|
Logger::info() << "Deleted" << fileData.absPath << Qt::endl;
|
2019-04-16 08:51:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
2021-06-12 22:55:56 +02:00
|
|
|
Logger::info() << "Would delete" << fileData.absPath << Qt::endl;
|
2019-04-16 08:51:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:46:33 +02:00
|
|
|
int updatedFilesCount = saver.updateFiles(filePathsToUpdate, keepGoing, verbose);
|
|
|
|
int shouldHaveUpdatedCount = filePathsToUpdate.size();
|
|
|
|
if(updatedFilesCount != shouldHaveUpdatedCount)
|
2019-04-16 08:51:27 +02:00
|
|
|
{
|
|
|
|
if(!keepGoing)
|
|
|
|
{
|
2019-04-26 22:46:33 +02:00
|
|
|
Logger::error() << "Failed to update all files selected for updating. Updated" << updatedFilesCount
|
2021-06-12 22:55:56 +02:00
|
|
|
<< "out of" << shouldHaveUpdatedCount << "selected for upating" << Qt::endl;
|
2019-04-16 08:51:27 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offset += limit;
|
2022-04-25 22:22:40 +02:00
|
|
|
files.clear();
|
2019-04-16 08:51:27 +02:00
|
|
|
processedRows = this->dbService->getFiles(files, pattern, offset, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-04-07 20:22:20 +02:00
|
|
|
}
|