You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RevWalkUtilsReachableTest.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2014, Robin Stocker <robin@nibor.org> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.revwalk;
  11. import static java.util.Arrays.asList;
  12. import static org.junit.Assert.assertEquals;
  13. import java.util.Collection;
  14. import java.util.List;
  15. import org.eclipse.jgit.api.Git;
  16. import org.eclipse.jgit.lib.Ref;
  17. import org.eclipse.jgit.lib.RefComparator;
  18. import org.junit.Test;
  19. public class RevWalkUtilsReachableTest extends RevWalkTestCase {
  20. @Test
  21. public void oneCommit() throws Exception {
  22. RevCommit a = commit();
  23. Ref branchA = branch("a", a);
  24. assertContains(a, asList(branchA));
  25. }
  26. @Test
  27. public void twoCommits() throws Exception {
  28. RevCommit a = commit();
  29. RevCommit b = commit(a);
  30. branch("a", a);
  31. Ref branchB = branch("b", b);
  32. assertContains(b, asList(branchB));
  33. }
  34. @Test
  35. public void multipleBranches() throws Exception {
  36. RevCommit a = commit();
  37. RevCommit b = commit(a);
  38. branch("a", a);
  39. Ref branchB = branch("b", b);
  40. Ref branchB2 = branch("b2", b);
  41. assertContains(b, asList(branchB, branchB2));
  42. }
  43. @Test
  44. public void withMerge() throws Exception {
  45. RevCommit a = commit();
  46. RevCommit b = commit();
  47. RevCommit c = commit(a, b);
  48. Ref branchA = branch("a", a);
  49. Ref branchB = branch("b", b);
  50. Ref branchC = branch("c", c);
  51. assertContains(a, asList(branchA, branchC));
  52. assertContains(b, asList(branchB, branchC));
  53. }
  54. @Test
  55. public void withCommitLoadedByDifferentRevWalk() throws Exception {
  56. RevCommit a = commit();
  57. Ref branchA = branch("a", a);
  58. try (RevWalk walk = new RevWalk(db)) {
  59. RevCommit parsedCommit = walk.parseCommit(a.getId());
  60. assertContains(parsedCommit, asList(branchA));
  61. }
  62. }
  63. private Ref branch(String name, RevCommit dst) throws Exception {
  64. return Git.wrap(db).branchCreate().setName(name)
  65. .setStartPoint(dst.name()).call();
  66. }
  67. private void assertContains(RevCommit commit, Collection<Ref> refsThatShouldContainCommit) throws Exception {
  68. Collection<Ref> allRefs = db.getRefDatabase().getRefs();
  69. Collection<Ref> sortedRefs = RefComparator.sort(allRefs);
  70. List<Ref> actual = RevWalkUtils.findBranchesReachableFrom(commit,
  71. rw, sortedRefs);
  72. assertEquals(refsThatShouldContainCommit, actual);
  73. }
  74. }