diff --git a/plugins/git4idea/src/git4idea/GitBranch.java b/plugins/git4idea/src/git4idea/GitBranch.java index 673067891a1f..671ed1eaff1c 100644 --- a/plugins/git4idea/src/git4idea/GitBranch.java +++ b/plugins/git4idea/src/git4idea/GitBranch.java @@ -287,4 +287,27 @@ public class GitBranch extends GitReference { } return new GitBranch(branch, false, remoteFlag); } + + /** + * Get a merge base between the current branch and specified branch. + * + * @param project the current project + * @param root the vcs root + * @param branch the branch + * @return the common commit or null if the there is no common commit + * @throws VcsException the exception + */ + @Nullable + public GitRevisionNumber getMergeBase(@NotNull Project project, @NotNull VirtualFile root, @NotNull GitBranch branch) + throws VcsException { + GitSimpleHandler h = new GitSimpleHandler(project, root, GitHandler.MERGE_BASE); + h.addParameters(this.getFullName(), branch.getFullName()); + String output = h.run().trim(); + if (output.length() == 0) { + return null; + } + else { + return GitRevisionNumber.resolve(project, root, output); + } + } } diff --git a/plugins/git4idea/src/git4idea/changes/GitOutgoingChangesProvider.java b/plugins/git4idea/src/git4idea/changes/GitOutgoingChangesProvider.java index 99a95e2e4046..373b12019257 100644 --- a/plugins/git4idea/src/git4idea/changes/GitOutgoingChangesProvider.java +++ b/plugins/git4idea/src/git4idea/changes/GitOutgoingChangesProvider.java @@ -10,6 +10,7 @@ import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.Consumer; import git4idea.GitBranchesSearcher; +import git4idea.GitRevisionNumber; import git4idea.GitUtil; import git4idea.commands.GitSimpleHandler; @@ -24,18 +25,22 @@ public class GitOutgoingChangesProvider implements VcsOutgoingChangesProvider> getOutgoingChanges(final VirtualFile vcsRoot, final boolean findRemote) throws VcsException { + public Pair> getOutgoingChanges(final VirtualFile vcsRoot, final boolean findRemote) + throws VcsException { LOG.debug("getOutgoingChanges root: " + vcsRoot.getPath()); final GitBranchesSearcher searcher = new GitBranchesSearcher(myProject, vcsRoot, findRemote); if (searcher.getLocal() == null || searcher.getRemote() == null) { return new Pair>(null, Collections.emptyList()); } - + final GitRevisionNumber base = searcher.getLocal().getMergeBase(myProject, vcsRoot, searcher.getRemote()); + if (base == null) { + return new Pair>(null, Collections.emptyList()); + } final List lists = GitUtil.getLocalCommittedChanges(myProject, vcsRoot, new Consumer() { public void consume(final GitSimpleHandler handler) { - handler.addParameters(searcher.getRemote().getFullName() + "..HEAD"); + handler.addParameters(base.asString() + "..HEAD"); } }); - return new Pair>(null, lists); + return new Pair>(base, lists); } } diff --git a/plugins/git4idea/src/git4idea/commands/GitHandler.java b/plugins/git4idea/src/git4idea/commands/GitHandler.java index aea355d7170a..a1a87e2f7e04 100644 --- a/plugins/git4idea/src/git4idea/commands/GitHandler.java +++ b/plugins/git4idea/src/git4idea/commands/GitHandler.java @@ -166,6 +166,10 @@ public abstract class GitHandler { * The constant for git command {@value} */ @NonNls public static final String MERGE = "merge"; + /** + * The constant for git command {@value} + */ + @NonNls public static final String MERGE_BASE = "merge-base"; /** * The constant for git command {@value} */