From e836cd082c49b56ca337a168ab555905ebaf6cb8 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Mon, 6 May 2013 22:40:41 +0300 Subject: [PATCH] Git: Prefer remote tracking branch in GerritPushDialog In case it contains the latest remote commit Change-Id: Id9e4cc40b7d1aa9daac3a9a2cf9ba41f589da7eb Reviewed-by: Petar Perisin Reviewed-by: Tobias Hunger --- src/plugins/git/gerrit/gerritpushdialog.cpp | 18 ++++++++++++++---- src/plugins/git/gitclient.cpp | 17 +++++++++++++++++ src/plugins/git/gitclient.h | 2 ++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/plugins/git/gerrit/gerritpushdialog.cpp b/src/plugins/git/gerrit/gerritpushdialog.cpp index 94965e86cbd..823ea98a6c1 100644 --- a/src/plugins/git/gerrit/gerritpushdialog.cpp +++ b/src/plugins/git/gerrit/gerritpushdialog.cpp @@ -77,13 +77,23 @@ GerritPushDialog::GerritPushDialog(const QString &workingDir, const QString &rev QString head = QLatin1String("/HEAD"); QStringList refs = output.split(QLatin1Char('\n')); + QString remoteTrackingBranch = gitClient->synchronousTrackingBranch(m_workingDir); + QString remoteBranch; foreach (const QString &reference, refs) { - if (reference.contains(head) || reference.isEmpty()) + const QString ref = reference.trimmed(); + if (ref.contains(head) || ref.isEmpty()) continue; - m_suggestedRemoteName = reference.left(reference.indexOf(QLatin1Char('/'))).trimmed(); - m_suggestedRemoteBranch = reference.mid(reference.indexOf(QLatin1Char('/')) + 1).trimmed(); - break; + remoteBranch = ref; + + // Prefer remote tracking branch if it exists and contains the latest remote commit + if (ref == remoteTrackingBranch) + break; + } + + if (!remoteBranch.isEmpty()) { + m_suggestedRemoteName = remoteBranch.left(remoteBranch.indexOf(QLatin1Char('/'))); + m_suggestedRemoteBranch = remoteBranch.mid(remoteBranch.indexOf(QLatin1Char('/')) + 1); } output.clear(); diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp index ec12c79d97d..ae066f4c8df 100644 --- a/src/plugins/git/gitclient.cpp +++ b/src/plugins/git/gitclient.cpp @@ -2425,6 +2425,23 @@ void GitClient::synchronousAbortCommand(const QString &workingDir, const QString outwin->appendError(commandOutputFromLocal8Bit(stdErr)); } +QString GitClient::synchronousTrackingBranch(const QString &workingDirectory, const QString &branch) +{ + QString remote; + QString localBranch = branch.isEmpty() ? synchronousCurrentLocalBranch(workingDirectory) : branch; + if (localBranch.isEmpty()) + return QString(); + localBranch.prepend(QLatin1String("branch.")); + remote = readConfigValue(workingDirectory, localBranch + QLatin1String(".remote")); + if (remote.isEmpty()) + return QString(); + const QString rBranch = readConfigValue(workingDirectory, localBranch + QLatin1String(".merge")) + .replace(QLatin1String("refs/heads/"), QString()); + if (rBranch.isEmpty()) + return QString(); + return remote + QLatin1Char('/') + rBranch; +} + void GitClient::handleMergeConflicts(const QString &workingDir, const QString &commit, const QString &abortCommand) { QString message = commit.isEmpty() ? tr("Conflicts detected") diff --git a/src/plugins/git/gitclient.h b/src/plugins/git/gitclient.h index 10ef91b9f5c..b02ed7cffb3 100644 --- a/src/plugins/git/gitclient.h +++ b/src/plugins/git/gitclient.h @@ -240,6 +240,8 @@ public: void interactiveRebase(const QString &workingDirectory, const QString &commit, StashGuard &stashGuard, bool fixup); void synchronousAbortCommand(const QString &workingDir, const QString &abortCommand); + QString synchronousTrackingBranch(const QString &workingDirectory, + const QString &branch = QString()); // git svn support (asynchronous). void synchronousSubversionFetch(const QString &workingDirectory);