diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgLogCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgLogCommand.java index fa9b2e7b6386..68267124998b 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgLogCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgLogCommand.java @@ -203,8 +203,8 @@ public class HgLogCommand { } @Nullable - private HgCommandResult execute(@NotNull VirtualFile repo, @NotNull String template, int limit, HgFile hgFile, - @Nullable List argsForCmd) { + public HgCommandResult execute(@NotNull VirtualFile repo, @NotNull String template, int limit, HgFile hgFile, + @Nullable List argsForCmd) { List arguments = new LinkedList(); if (myIncludeRemoved) { // There is a bug in mercurial that causes --follow --removed to cause diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgLogProvider.java b/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgLogProvider.java index 0b878919bf9f..fb3f17d34a63 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgLogProvider.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/log/HgLogProvider.java @@ -241,7 +241,7 @@ public class HgLogProvider implements VcsLogProvider { @NotNull @Override public Collection getContainingBranches(@NotNull VirtualFile root, @NotNull Hash commitHash) throws VcsException { - return ContainerUtil.emptyList(); // TODO + return HgHistoryUtil.getDescendingHeadsOfBranches(myProject, root, commitHash); } private static String prepareParameter(String paramName, String value) { diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgHistoryUtil.java b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgHistoryUtil.java index c042945b5321..c7a93a262caf 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgHistoryUtil.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgHistoryUtil.java @@ -33,6 +33,7 @@ import org.zmlx.hg4idea.*; import org.zmlx.hg4idea.action.HgCommandResultNotifier; import org.zmlx.hg4idea.command.HgLogCommand; import org.zmlx.hg4idea.execution.HgCommandException; +import org.zmlx.hg4idea.execution.HgCommandResult; import org.zmlx.hg4idea.log.HgContentRevisionFactory; import org.zmlx.hg4idea.provider.HgCommittedChangeList; @@ -205,4 +206,29 @@ public class HgHistoryUtil { } return hashArgs; } + + @NotNull + public static Collection getDescendingHeadsOfBranches(@NotNull Project project, @NotNull VirtualFile root, @NotNull Hash hash) + throws VcsException { + //hg log -r "descendants(659db54c1b6865c97c4497fa867194bcd759ca76) and head() " --template "{branch} {bookmarks}\n" + Set branchHeads = new HashSet(); + List params = new ArrayList(); + params.add("-r"); + params.add("descendants(" + hash.asString() + ") and head()"); + HgFile hgFile = new HgFile(root, VcsUtil.getFilePath(root.getPath())); + HgLogCommand hgLogCommand = new HgLogCommand(project); + hgLogCommand.setLogFile(false); + String template = HgChangesetUtil.makeTemplate("{branch}", "{bookmarks}"); + HgCommandResult logResult = hgLogCommand.execute(root, template, -1, hgFile, params); + if (logResult == null || logResult.getExitValue() != 0) { + throw new VcsException("Couldn't get commit details: log command execution error."); + } + String output = logResult.getRawOutput(); + String[] changeSets = output.split(HgChangesetUtil.CHANGESET_SEPARATOR); + for (String line : changeSets) { + String[] attributes = line.split(HgChangesetUtil.ITEM_SEPARATOR); + branchHeads.addAll(Arrays.asList(attributes)); + } + return branchHeads; + } } diff --git a/plugins/hg4idea/testSrc/hg4idea/test/history/HgLogHistoryTest.java b/plugins/hg4idea/testSrc/hg4idea/test/history/HgLogHistoryTest.java new file mode 100644 index 000000000000..3c215aefbf9e --- /dev/null +++ b/plugins/hg4idea/testSrc/hg4idea/test/history/HgLogHistoryTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package hg4idea.test.history; + +import com.intellij.openapi.vcs.VcsException; +import com.intellij.openapi.vcs.VcsTestUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.vcs.log.Hash; +import com.intellij.vcs.log.impl.HashImpl; +import hg4idea.test.HgPlatformTest; +import org.jetbrains.annotations.NotNull; +import org.zmlx.hg4idea.util.HgHistoryUtil; + +import java.util.Arrays; +import java.util.Collection; + +import static com.intellij.openapi.vcs.Executor.cd; +import static com.intellij.openapi.vcs.Executor.echo; +import static com.intellij.openapi.vcs.Executor.touch; +import static hg4idea.test.HgExecutor.hg; + +/** + * @author Nadya Zabrodina + */ +public class HgLogHistoryTest extends HgPlatformTest { + + @Override + public void setUp() throws Exception { + super.setUp(); + createBookmarksAndBranches(myRepository); + } + + public void testContainedInBranchesInLogInfos() throws VcsException { + Hash testHashForFirstCommit = HashImpl.build("0"); + Collection branches = HgHistoryUtil.getDescendingHeadsOfBranches(myProject, myRepository, testHashForFirstCommit); + //B_Bookmark should not be listed - it is inactive and not head// + VcsTestUtil.assertEqualCollections(branches, Arrays.asList("default", "branchA", "branchB", "A_BookMark", "C_BookMark")); + } + + private static void createBookmarksAndBranches(@NotNull VirtualFile repositoryRoot) { + cd(repositoryRoot); + hg("bookmark A_BookMark"); + hg("tag tag1"); + String aFile = "A.txt"; + touch(aFile, "base"); + hg("add " + aFile); + hg("commit -m 'create file'"); + hg("branch branchA"); + hg("tag tag2"); + echo(aFile, " modify with a"); + hg("commit -m 'create branchA'"); + hg("bookmark B_BookMark --inactive"); + echo(aFile, " modify with AA"); + hg("commit -m 'modify branchA'"); + hg("up default"); + hg("branch branchB"); + hg("tag -l localTag"); + echo(aFile, " modify with b"); + hg("commit -m 'modify file in branchB'"); + hg("bookmark C_BookMark"); + hg("up branchA"); + } +}