Git: Reuse handleResponse() inside vcsExecAbortable()

Remove ConflictHandler::attachToCommand().
Rename handleResponse() into handleConflictResponse() and
move it out of ConflictHandler class.

Change-Id: I0a64bd19f288d9377a93cdb5eea2c44b65bf6fdb
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Jarek Kobus
2022-12-09 16:45:43 +01:00
parent 305dc46902
commit db85862a8c

View File

@@ -721,59 +721,38 @@ public:
} }
}; };
class ConflictHandler static void handleConflictResponse(const VcsBase::CommandResult &result,
const FilePath &workingDirectory,
const QString &abortCommand = {})
{ {
public: const bool success = result.result() == ProcessResult::FinishedWithSuccess;
static void attachToCommand(VcsCommand *command, const FilePath &workingDirectory, const QString stdOutData = success ? QString() : result.cleanedStdOut();
const QString &abortCommand = {}) { const QString stdErrData = success ? QString() : result.cleanedStdErr();
command->addFlags(RunFlags::ExpectRepoChanges); static const QRegularExpression patchFailedRE("Patch failed at ([^\\n]*)");
QObject::connect(command, &VcsCommand::done, command, [=] { static const QRegularExpression conflictedFilesRE("Merge conflict in ([^\\n]*)");
finalize(workingDirectory, abortCommand, static const QRegularExpression couldNotApplyRE("[Cc]ould not (?:apply|revert) ([^\\n]*)");
command->cleanedStdOut(), command->cleanedStdErr()); QString commit;
}); QStringList files;
const QRegularExpressionMatch outMatch = patchFailedRE.match(stdOutData);
if (outMatch.hasMatch())
commit = outMatch.captured(1);
QRegularExpressionMatchIterator it = conflictedFilesRE.globalMatch(stdOutData);
while (it.hasNext())
files.append(it.next().captured(1));
const QRegularExpressionMatch errMatch = couldNotApplyRE.match(stdErrData);
if (errMatch.hasMatch())
commit = errMatch.captured(1);
// If interactive rebase editor window is closed, plugin is terminated
// but referenced here when the command ends
if (commit.isEmpty() && files.isEmpty()) {
if (m_instance->checkCommandInProgress(workingDirectory) == GitClient::NoCommand)
m_instance->endStashScope(workingDirectory);
} else {
m_instance->handleMergeConflicts(workingDirectory, commit, files, abortCommand);
} }
}
static void handleResponse(const VcsBase::CommandResult &result,
const FilePath &workingDirectory,
const QString &abortCommand = {})
{
const bool success = result.result() == ProcessResult::FinishedWithSuccess;
const QString stdOutData = success ? QString() : result.cleanedStdOut();
const QString stdErrData = success ? QString() : result.cleanedStdErr();
finalize(workingDirectory, abortCommand, stdOutData, stdErrData);
}
private:
static void finalize(const FilePath &workingDirectory, const QString &abortCommand,
const QString &stdOutData, const QString &stdErrData)
{
QString commit;
QStringList files;
static const QRegularExpression patchFailedRE("Patch failed at ([^\\n]*)");
static const QRegularExpression conflictedFilesRE("Merge conflict in ([^\\n]*)");
static const QRegularExpression couldNotApplyRE("[Cc]ould not (?:apply|revert) ([^\\n]*)");
const QRegularExpressionMatch outMatch = patchFailedRE.match(stdOutData);
if (outMatch.hasMatch())
commit = outMatch.captured(1);
QRegularExpressionMatchIterator it = conflictedFilesRE.globalMatch(stdOutData);
while (it.hasNext())
files.append(it.next().captured(1));
const QRegularExpressionMatch errMatch = couldNotApplyRE.match(stdErrData);
if (errMatch.hasMatch())
commit = errMatch.captured(1);
// If interactive rebase editor window is closed, plugin is terminated
// but referenced here when the command ends
if (commit.isEmpty() && files.isEmpty()) {
if (m_instance->checkCommandInProgress(workingDirectory) == GitClient::NoCommand)
m_instance->endStashScope(workingDirectory);
} else {
m_instance->handleMergeConflicts(workingDirectory, commit, files, abortCommand);
}
}
};
class GitProgressParser class GitProgressParser
{ {
@@ -3095,7 +3074,7 @@ bool GitClient::executeAndHandleConflicts(const FilePath &workingDirectory,
| RunFlags::ShowSuccessMessage; | RunFlags::ShowSuccessMessage;
const CommandResult result = vcsSynchronousExec(workingDirectory, arguments, flags); const CommandResult result = vcsSynchronousExec(workingDirectory, arguments, flags);
// Notify about changed files or abort the rebase. // Notify about changed files or abort the rebase.
ConflictHandler::handleResponse(result, workingDirectory, abortCommand); handleConflictResponse(result, workingDirectory, abortCommand);
return result.result() == ProcessResult::FinishedWithSuccess; return result.result() == ProcessResult::FinishedWithSuccess;
} }
@@ -3374,13 +3353,13 @@ void GitClient::vcsExecAbortable(const FilePath &workingDirectory, const QString
// For rebase, Git might request an editor (which means the process keeps running until the // For rebase, Git might request an editor (which means the process keeps running until the
// user closes it), so run without timeout. // user closes it), so run without timeout.
command->addJob({vcsBinary(), arguments}, isRebase ? 0 : vcsTimeoutS()); command->addJob({vcsBinary(), arguments}, isRebase ? 0 : vcsTimeoutS());
ConflictHandler::attachToCommand(command, workingDirectory, abortString); const QObject *actualContext = context ? context : this;
if (handler) { connect(command, &VcsCommand::done, actualContext, [=] {
const QObject *actualContext = context ? context : this; const CommandResult result = CommandResult(*command);
connect(command, &VcsCommand::done, actualContext, [command, handler] { handleConflictResponse(result, workingDirectory, abortString);
handler(CommandResult(*command)); if (handler)
}); handler(result);
} });
if (isRebase) if (isRebase)
command->setProgressParser(GitProgressParser()); command->setProgressParser(GitProgressParser());
command->start(); command->start();
@@ -3440,7 +3419,7 @@ void GitClient::stashPop(const FilePath &workingDirectory, const QString &stash)
if (!stash.isEmpty()) if (!stash.isEmpty())
arguments << stash; arguments << stash;
const auto commandHandler = [workingDirectory](const CommandResult &result) { const auto commandHandler = [workingDirectory](const CommandResult &result) {
ConflictHandler::handleResponse(result, workingDirectory); handleConflictResponse(result, workingDirectory);
}; };
vcsExecWithHandler(workingDirectory, arguments, this, commandHandler, vcsExecWithHandler(workingDirectory, arguments, this, commandHandler,
RunFlags::ShowStdOut | RunFlags::ExpectRepoChanges); RunFlags::ShowStdOut | RunFlags::ExpectRepoChanges);