forked from qt-creator/qt-creator
Git: Simplify ConflictHandler
Don't create QObject instance when handling conflicts. Operate on static functions only. Replace destructor of ConflictHandler with finalize() method. Change-Id: If579edc6213faa50c73a90cedf4b67cf985478ac Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -720,76 +720,58 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ConflictHandler final : public QObject
|
class ConflictHandler
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
|
||||||
public:
|
public:
|
||||||
static void attachToCommand(VcsCommand *command, const FilePath &workingDirectory,
|
static void attachToCommand(VcsCommand *command, const FilePath &workingDirectory,
|
||||||
const QString &abortCommand = {}) {
|
const QString &abortCommand = {}) {
|
||||||
auto handler = new ConflictHandler(workingDirectory, abortCommand);
|
|
||||||
handler->setParent(command); // delete when command goes out of scope
|
|
||||||
|
|
||||||
command->addFlags(RunFlags::ExpectRepoChanges);
|
command->addFlags(RunFlags::ExpectRepoChanges);
|
||||||
connect(command, &VcsCommand::done, handler, [handler, command] {
|
QObject::connect(command, &VcsCommand::done, command, [=] {
|
||||||
handler->readStdOut(command->cleanedStdOut());
|
finalize(workingDirectory, abortCommand,
|
||||||
handler->readStdErr(command->cleanedStdErr());
|
command->cleanedStdOut(), command->cleanedStdErr());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handleResponse(const VcsBase::CommandResult &result,
|
static void handleResponse(const VcsBase::CommandResult &result,
|
||||||
const FilePath &workingDirectory,
|
const FilePath &workingDirectory,
|
||||||
const QString &abortCommand = QString())
|
const QString &abortCommand)
|
||||||
{
|
{
|
||||||
ConflictHandler handler(workingDirectory, abortCommand);
|
const bool success = result.result() == ProcessResult::FinishedWithSuccess;
|
||||||
// No conflicts => do nothing
|
const QString stdOutData = success ? QString() : result.cleanedStdOut();
|
||||||
if (result.result() == ProcessResult::FinishedWithSuccess)
|
const QString stdErrData = success ? QString() : result.cleanedStdErr();
|
||||||
return;
|
finalize(workingDirectory, abortCommand, stdOutData, stdErrData);
|
||||||
handler.readStdOut(result.cleanedStdOut());
|
|
||||||
handler.readStdErr(result.cleanedStdErr());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ConflictHandler(const FilePath &workingDirectory, const QString &abortCommand) :
|
static void finalize(const FilePath &workingDirectory, const QString &abortCommand,
|
||||||
m_workingDirectory(workingDirectory),
|
const QString &stdOutData, const QString &stdErrData)
|
||||||
m_abortCommand(abortCommand)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
~ConflictHandler() final
|
|
||||||
{
|
{
|
||||||
// If interactive rebase editor window is closed, plugin is terminated
|
QString commit;
|
||||||
// but referenced here when the command ends
|
QStringList files;
|
||||||
if (m_commit.isEmpty() && m_files.isEmpty()) {
|
|
||||||
if (m_instance->checkCommandInProgress(m_workingDirectory) == GitClient::NoCommand)
|
|
||||||
m_instance->endStashScope(m_workingDirectory);
|
|
||||||
} else {
|
|
||||||
m_instance->handleMergeConflicts(m_workingDirectory, m_commit, m_files, m_abortCommand);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void readStdOut(const QString &data)
|
|
||||||
{
|
|
||||||
static const QRegularExpression patchFailedRE("Patch failed at ([^\\n]*)");
|
static const QRegularExpression patchFailedRE("Patch failed at ([^\\n]*)");
|
||||||
static const QRegularExpression conflictedFilesRE("Merge conflict in ([^\\n]*)");
|
static const QRegularExpression conflictedFilesRE("Merge conflict in ([^\\n]*)");
|
||||||
const QRegularExpressionMatch match = patchFailedRE.match(data);
|
|
||||||
if (match.hasMatch())
|
|
||||||
m_commit = match.captured(1);
|
|
||||||
QRegularExpressionMatchIterator it = conflictedFilesRE.globalMatch(data);
|
|
||||||
while (it.hasNext())
|
|
||||||
m_files.append(it.next().captured(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
void readStdErr(const QString &data)
|
|
||||||
{
|
|
||||||
static const QRegularExpression couldNotApplyRE("[Cc]ould not (?:apply|revert) ([^\\n]*)");
|
static const QRegularExpression couldNotApplyRE("[Cc]ould not (?:apply|revert) ([^\\n]*)");
|
||||||
const QRegularExpressionMatch match = couldNotApplyRE.match(data);
|
|
||||||
if (match.hasMatch())
|
const QRegularExpressionMatch outMatch = patchFailedRE.match(stdOutData);
|
||||||
m_commit = match.captured(1);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private:
|
|
||||||
FilePath m_workingDirectory;
|
|
||||||
QString m_abortCommand;
|
|
||||||
QString m_commit;
|
|
||||||
QStringList m_files;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class GitProgressParser
|
class GitProgressParser
|
||||||
|
Reference in New Issue
Block a user