From 03513ef305f3d4f8414ab7ac2624b6d2ca75b3ee Mon Sep 17 00:00:00 2001 From: Robin Stocker Date: Sun, 2 Sep 2012 23:37:25 +0200 Subject: [historyView] Rebase: Pass a better Ref as upstream JGit change I1333a8dd170bb0077f491962013485efb6f2a926 is about improving the ours/theirs conflict markers during rebase. The way the rebase action worked before, it would always pass the commit ID to rebase, which is not very helpful during a rebase conflict. This change makes it use a Ref if there is one. If there are multiple refs pointing to the selected commit: - and if the branch being rebased tracks another one, this tracked branch should be the one to pick if it's in the list - otherwise pick a remote tracking branch if it's in the list Also adds a "does it work at all?" test for rebase in History view. Bug: 336819 Change-Id: Ib78c3e8965e2f57e2c844a065629511ab1158160 Signed-off-by: Robin Stocker Signed-off-by: Matthias Sohn --- .../src/org/eclipse/egit/ui/UIText.java | 3 ++ .../history/command/RebaseCurrentHandler.java | 52 +++++++++++++++++++--- .../src/org/eclipse/egit/ui/uitext.properties | 1 + 3 files changed, 50 insertions(+), 6 deletions(-) (limited to 'org.eclipse.egit.ui/src') diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java index 04673367a6..3eddf3589b 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/UIText.java @@ -674,6 +674,9 @@ public class UIText extends NLS { /** */ public static String RebaseCurrentRefCommand_RebasingCurrentJobName; + /** */ + public static String RebaseCurrentRefCommand_ErrorGettingCurrentBranchMessage; + /** */ public static String RebaseResultDialog_Aborted; diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/RebaseCurrentHandler.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/RebaseCurrentHandler.java index fd02dd06d8..b70238ec97 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/RebaseCurrentHandler.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/command/RebaseCurrentHandler.java @@ -13,14 +13,19 @@ package org.eclipse.egit.ui.internal.history.command; +import java.io.IOException; + import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.egit.ui.UIText; import org.eclipse.egit.ui.internal.rebase.RebaseHelper; +import org.eclipse.jgit.lib.BranchConfig; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectIdRef; +import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Ref.Storage; -import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revplot.PlotCommit; import org.eclipse.osgi.util.NLS; /** @@ -30,17 +35,52 @@ public class RebaseCurrentHandler extends AbstractHistoryCommandHandler { public Object execute(ExecutionEvent event) throws ExecutionException { - RevCommit commit = (RevCommit) getSelection(getPage()).getFirstElement(); + PlotCommit commit = (PlotCommit) getSelection(getPage()).getFirstElement(); final Repository repository = getRepository(event); if (repository == null) return null; + String currentBranch = getCurrentBranch(repository); + Ref ref = getRef(commit, repository, currentBranch); + String jobname = NLS.bind( - UIText.RebaseCurrentRefCommand_RebasingCurrentJobName, commit); - RebaseHelper.runRebaseJob(repository, jobname, - new ObjectIdRef.Unpeeled(Storage.LOOSE, commit.getName(), - commit)); + UIText.RebaseCurrentRefCommand_RebasingCurrentJobName, currentBranch); + RebaseHelper.runRebaseJob(repository, jobname, ref); return null; } + private Ref getRef(PlotCommit commit, Repository repository, String currentBranch) { + int count = commit.getRefCount(); + if (count == 0) + return new ObjectIdRef.Unpeeled(Storage.LOOSE, commit.getName(), commit); + else if (count == 1) + return commit.getRef(0); + else { + BranchConfig branchConfig = new BranchConfig(repository.getConfig(), currentBranch); + String trackingBranch = branchConfig.getTrackingBranch(); + Ref remoteRef = null; + + for (int i = 0; i < count; i++) { + Ref ref = commit.getRef(i); + if (trackingBranch != null && trackingBranch.equals(ref.getName())) + return ref; + if (ref.getName().startsWith(Constants.R_REMOTES)) + remoteRef = ref; + } + + if (remoteRef != null) + return remoteRef; + else + // We tried to pick a nice ref, just pick the first then + return commit.getRef(0); + } + } + + private String getCurrentBranch(Repository repository) throws ExecutionException { + try { + return repository.getBranch(); + } catch (IOException e) { + throw new ExecutionException(UIText.RebaseCurrentRefCommand_ErrorGettingCurrentBranchMessage, e); + } + } } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties index 3342410d9a..8a2616c4be 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties @@ -281,6 +281,7 @@ CompareWithIndexAction_FileNotInIndex={0} not in index RebaseCurrentRefCommand_RebaseCanceledMessage=The rebase operation was canceled RebaseCurrentRefCommand_RebaseCanceledTitle=Rebase Canceled RebaseCurrentRefCommand_RebasingCurrentJobName=Rebasing Branch {0} +RebaseCurrentRefCommand_ErrorGettingCurrentBranchMessage=Error getting the branch to rebase RebaseResultDialog_Aborted=Rebase was aborted RebaseResultDialog_AbortRebaseRadioText=&Abort rebase RebaseResultDialog_ActionGroupTitle=Action to perform -- cgit v1.2.3