From b6608d0d37561af432da9618eec53ff249d7d58c Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Tue, 14 Apr 2015 13:57:16 +0200 Subject: [PATCH] Git: Simplify use of conflict handler a bit Disentangle binding to an existing command from the actual task of the conflict handler. This makes usage more clear. Also rename "command" to "abortCommand" which is also more clear as to what it is used for. Change-Id: I862cfec97fbf512a3121f0a7d58ae12f148879a6 Reviewed-by: Orgad Shaneh --- src/plugins/git/gitclient.cpp | 53 +++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp index e9a8aff6fa0..d74e54c9c70 100644 --- a/src/plugins/git/gitclient.cpp +++ b/src/plugins/git/gitclient.cpp @@ -457,20 +457,30 @@ class ConflictHandler : public QObject { Q_OBJECT public: - ConflictHandler(VcsCommand *parentCommand, - const QString &workingDirectory, - const QString &command = QString()) - : QObject(parentCommand), - m_workingDirectory(workingDirectory), - m_command(command) - { - if (parentCommand) { - parentCommand->addFlags(VcsBasePlugin::ExpectRepoChanges); - connect(parentCommand, &VcsCommand::output, this, &ConflictHandler::readStdOut); - connect(parentCommand, &VcsCommand::errorText, this, &ConflictHandler::readStdErr); - } + static void attachToCommand(VcsCommand *command, const QString &abortCommand = QString()) { + ConflictHandler *handler = new ConflictHandler(command->workingDirectory(), abortCommand); + handler->setParent(command); // delete when command goes out of scope + + command->addFlags(VcsBasePlugin::ExpectRepoChanges); + connect(command, &VcsCommand::output, handler, &ConflictHandler::readStdOut); + connect(command, &VcsCommand::errorText, handler, &ConflictHandler::readStdErr); } + static void handleResponse(const Utils::SynchronousProcessResponse &response, + const QString &workingDirectory, + const QString &abortCommand = QString()) + { + ConflictHandler handler(workingDirectory, abortCommand); + handler.readStdOut(response.stdOut); + handler.readStdErr(response.stdErr); + } + +private: + ConflictHandler(const QString &workingDirectory, const QString &abortCommand) : + m_workingDirectory(workingDirectory), + m_abortCommand(abortCommand) + { } + ~ConflictHandler() { // If interactive rebase editor window is closed, plugin is terminated @@ -481,12 +491,11 @@ public: if (client->checkCommandInProgress(m_workingDirectory) == GitClient::NoCommand) client->endStashScope(m_workingDirectory); } else { - client->handleMergeConflicts(m_workingDirectory, m_commit, m_files, m_command); + client->handleMergeConflicts(m_workingDirectory, m_commit, m_files, m_abortCommand); } } } -public slots: void readStdOut(const QString &data) { static QRegExp patchFailedRE(QLatin1String("Patch failed at ([^\\n]*)")); @@ -494,9 +503,8 @@ public slots: if (patchFailedRE.indexIn(data) != -1) m_commit = patchFailedRE.cap(1); int fileIndex = -1; - while ((fileIndex = conflictedFilesRE.indexIn(data, fileIndex + 1)) != -1) { + while ((fileIndex = conflictedFilesRE.indexIn(data, fileIndex + 1)) != -1) m_files.append(conflictedFilesRE.cap(1)); - } } void readStdErr(const QString &data) @@ -507,7 +515,7 @@ public slots: } private: QString m_workingDirectory; - QString m_command; + QString m_abortCommand; QString m_commit; QStringList m_files; }; @@ -2769,13 +2777,10 @@ bool GitClient::executeAndHandleConflicts(const QString &workingDirectory, | VcsBasePlugin::ExpectRepoChanges | VcsBasePlugin::ShowSuccessMessage; const SynchronousProcessResponse resp = vcsSynchronousExec(workingDirectory, arguments, flags); - ConflictHandler conflictHandler(0, workingDirectory, abortCommand); // Notify about changed files or abort the rebase. const bool ok = resp.result == SynchronousProcessResponse::Finished; - if (!ok) { - conflictHandler.readStdOut(resp.stdOut); - conflictHandler.readStdErr(resp.stdErr); - } + if (!ok) + ConflictHandler::handleResponse(resp, workingDirectory, abortCommand); return ok; } @@ -2974,7 +2979,7 @@ void GitClient::asyncCommand(const QString &workingDirectory, const QStringList QString gitCommand = arguments.first(); VcsOutputWindow::appendCommand(workingDirectory, vcsBinary(), arguments); VcsCommand *command = createCommand(workingDirectory, 0, VcsWindowOutputBind); - new ConflictHandler(command, workingDirectory, gitCommand); + ConflictHandler::attachToCommand(command, gitCommand); if (hasProgress) command->setProgressParser(new GitProgressParser); command->setCookie(workingDirectory); @@ -3043,7 +3048,7 @@ void GitClient::stashPop(const QString &workingDirectory, const QString &stash) arguments << stash; VcsCommand *cmd = executeGit(workingDirectory, arguments, 0, true, VcsBasePlugin::ExpectRepoChanges); - new ConflictHandler(cmd, workingDirectory); + ConflictHandler::attachToCommand(cmd); } bool GitClient::synchronousStashRestore(const QString &workingDirectory,