PushHandler: Use cleanedStdErr()

Instead of connecting to stdErrText() signal.

Change-Id: Ib9fb94ca3fb0b5cbf4871e0ea3b06a047c31d272
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Jarek Kobus
2022-09-19 09:53:34 +02:00
parent 85c5edcb6b
commit 012de6b4c3

View File

@@ -3263,37 +3263,18 @@ public:
nullptr, true, VcsCommand::ShowSuccessMessage); nullptr, true, VcsCommand::ShowSuccessMessage);
// Make command a parent of this in order to delete this when command is deleted // Make command a parent of this in order to delete this when command is deleted
setParent(command); setParent(command);
connect(command, &VcsCommand::finished, this, [=](bool success) {
connect(command, &VcsCommand::stdErrText, this, [this](const QString &text) { QString pushFallbackCommand;
if (text.contains("non-fast-forward")) { const PushFailure pushFailure = handleError(command->cleanedStdErr(),
m_pushFailure = NonFastForward; &pushFallbackCommand);
} else if (text.contains("has no upstream branch")) {
m_pushFailure = NoRemoteBranch;
const QStringList lines = text.split('\n', Qt::SkipEmptyParts);
for (const QString &line : lines) {
/* Extract the suggested command from the git output which
* should be similar to the following:
*
* git push --set-upstream origin add_set_upstream_dialog
*/
const QString trimmedLine = line.trimmed();
if (trimmedLine.startsWith("git push")) {
m_pushFallbackCommand = trimmedLine;
break;
}
}
}
});
connect(command, &VcsCommand::finished, this, [this, workingDir, pushArgs](bool success) {
if (success) { if (success) {
GitPlugin::updateCurrentBranch(); GitPlugin::updateCurrentBranch();
return; return;
} }
if (m_pushFailure == Unknown || !m_gitClient) if (pushFailure == Unknown || !m_gitClient)
return; return;
if (m_pushFailure == NonFastForward) { if (pushFailure == NonFastForward) {
const QColor warnColor = Utils::creatorTheme()->color(Theme::TextColorError); const QColor warnColor = Utils::creatorTheme()->color(Theme::TextColorError);
if (QMessageBox::question( if (QMessageBox::question(
Core::ICore::dialogParent(), tr("Force Push"), Core::ICore::dialogParent(), tr("Force Push"),
@@ -3326,7 +3307,7 @@ public:
return; return;
} }
const QStringList fallbackCommandParts = const QStringList fallbackCommandParts =
m_pushFallbackCommand.split(' ', Qt::SkipEmptyParts); pushFallbackCommand.split(' ', Qt::SkipEmptyParts);
VcsCommand *rePushCommand = m_gitClient->vcsExec(workingDir, VcsCommand *rePushCommand = m_gitClient->vcsExec(workingDir,
fallbackCommandParts.mid(1), nullptr, true, VcsCommand::ShowSuccessMessage); fallbackCommandParts.mid(1), nullptr, true, VcsCommand::ShowSuccessMessage);
connect(rePushCommand, &VcsCommand::finished, this, [workingDir](bool success) { connect(rePushCommand, &VcsCommand::finished, this, [workingDir](bool success) {
@@ -3336,9 +3317,32 @@ public:
}); });
} }
private: private:
enum PushFailure { Unknown, NonFastForward, NoRemoteBranch } m_pushFailure = Unknown; enum PushFailure { Unknown, NonFastForward, NoRemoteBranch };
PushFailure handleError(const QString &text, QString *pushFallbackCommand) const {
if (text.contains("non-fast-forward"))
return NonFastForward;
if (text.contains("has no upstream branch")) {
const QStringList lines = text.split('\n', Qt::SkipEmptyParts);
for (const QString &line : lines) {
/* Extract the suggested command from the git output which
* should be similar to the following:
*
* git push --set-upstream origin add_set_upstream_dialog
*/
const QString trimmedLine = line.trimmed();
if (trimmedLine.startsWith("git push")) {
*pushFallbackCommand = trimmedLine;
break;
}
}
return NoRemoteBranch;
}
return Unknown;
};
QPointer<GitClient> m_gitClient; QPointer<GitClient> m_gitClient;
QString m_pushFallbackCommand;
}; };
void GitClient::push(const FilePath &workingDirectory, const QStringList &pushArgs) void GitClient::push(const FilePath &workingDirectory, const QStringList &pushArgs)