CommandDelete: refactor: split handle to several methods
This commit is contained in:
		@@ -6,39 +6,17 @@
 | 
				
			|||||||
#include <QSqlError>
 | 
					#include <QSqlError>
 | 
				
			||||||
#include "commanddelete.h"
 | 
					#include "commanddelete.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int CommandDelete::handle(QStringList arguments)
 | 
					int CommandDelete::removeNonExistent(QSqlDatabase &db, bool verbose, bool dryRun, QString pattern)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	QCommandLineParser parser;
 | 
					 | 
				
			||||||
	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();
 | 
					 | 
				
			||||||
	parser.addPositionalArgument("delete", "Delete paths from the index", "delete [paths...]");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	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
 | 
						// TODO: port this to QRegularExpression once >5.12 gets more widespread because of this bug
 | 
				
			||||||
	// https://bugreports.qt.io/browse/QTBUG-72539?focusedCommentId=439053&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel
 | 
						// https://bugreports.qt.io/browse/QTBUG-72539?focusedCommentId=439053&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// QRegularExpression regexPattern = QRegularExpression::wildcardToRegularExpression(pattern);
 | 
						// QRegularExpression regexPattern = QRegularExpression::wildcardToRegularExpression(pattern
 | 
				
			||||||
 | 
					 | 
				
			||||||
	bool usePattern = !pattern.isEmpty();
 | 
						bool usePattern = !pattern.isEmpty();
 | 
				
			||||||
	QRegExp regexPattern(pattern);
 | 
						QRegExp regexPattern(pattern);
 | 
				
			||||||
	regexPattern.setPatternSyntax(QRegExp::PatternSyntax::WildcardUnix);
 | 
						regexPattern.setPatternSyntax(QRegExp::PatternSyntax::WildcardUnix);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QSqlDatabase db = dbConnection();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if(removeNonExistant)
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
	// TODO: try to translate pattern to SQL WHERE statement
 | 
						// 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())
 | 
				
			||||||
@@ -83,8 +61,10 @@ int CommandDelete::handle(QStringList arguments)
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QStringList files = parser.positionalArguments();
 | 
					int CommandDelete::removePaths(const QStringList &paths, QSqlDatabase &db, bool verbose, bool dryRun)
 | 
				
			||||||
	for(QString &file : files)
 | 
					{
 | 
				
			||||||
 | 
						int result = 0;
 | 
				
			||||||
 | 
						for(const QString &file : paths)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		QFileInfo fileInfo(file);
 | 
							QFileInfo fileInfo(file);
 | 
				
			||||||
		QString absPath = fileInfo.absoluteFilePath();
 | 
							QString absPath = fileInfo.absoluteFilePath();
 | 
				
			||||||
@@ -104,13 +84,54 @@ int CommandDelete::handle(QStringList arguments)
 | 
				
			|||||||
				else
 | 
									else
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError();
 | 
										qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError();
 | 
				
			||||||
 | 
										result = 1;
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			qInfo() << "No such file in database:" << absPath;
 | 
								qInfo() << "No such file in database:" << absPath;
 | 
				
			||||||
 | 
								result = 1;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					int CommandDelete::handle(QStringList arguments)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						QCommandLineParser parser;
 | 
				
			||||||
 | 
						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();
 | 
				
			||||||
 | 
						parser.addPositionalArgument("delete", "Delete paths from the index", "delete [paths...]");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						parser.process(arguments);
 | 
				
			||||||
 | 
						bool verbose = parser.isSet("verbose");
 | 
				
			||||||
 | 
						bool dryRun = parser.isSet("dry-run");
 | 
				
			||||||
 | 
						QString pattern = parser.value("pattern");
 | 
				
			||||||
 | 
						QSqlDatabase db = dbConnection();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if(parser.isSet("deleted"))
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							int result = removeNonExistent(db, verbose, dryRun, pattern);
 | 
				
			||||||
 | 
							if(result != 0)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								return result;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						QStringList files = parser.positionalArguments();
 | 
				
			||||||
 | 
						int result = removePaths(files, db, verbose, dryRun);
 | 
				
			||||||
 | 
						if(result != 0)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return result;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,10 @@ class CommandDelete : public Command
 | 
				
			|||||||
	using Command::Command;
 | 
						using Command::Command;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	int handle(QStringList arguments) override;
 | 
						int handle(QStringList arguments) override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private:
 | 
				
			||||||
 | 
						int removeNonExistent(QSqlDatabase &db, bool verbose, bool dryRun, QString pattern);
 | 
				
			||||||
 | 
						int removePaths(const QStringList &paths, QSqlDatabase &db, bool verbose, bool dryRun);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // COMMANDDELETE_H
 | 
					#endif // COMMANDDELETE_H
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user