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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2012, 2014 IBM Corporation and others. 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.pgm;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import static org.junit.Assert.fail;
  15. import java.io.File;
  16. import org.eclipse.jgit.api.Git;
  17. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  18. import org.eclipse.jgit.lib.Constants;
  19. import org.eclipse.jgit.lib.RefUpdate;
  20. import org.eclipse.jgit.pgm.internal.CLIText;
  21. import org.eclipse.jgit.revwalk.RevCommit;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. public class BranchTest extends CLIRepositoryTestCase {
  25. @Override
  26. @Before
  27. public void setUp() throws Exception {
  28. super.setUp();
  29. try (Git git = new Git(db)) {
  30. git.commit().setMessage("initial commit").call();
  31. }
  32. }
  33. @Test
  34. public void testHelpAfterDelete() throws Exception {
  35. String err = toString(executeUnchecked("git branch -d"));
  36. String help = toString(executeUnchecked("git branch -h"));
  37. String errAndHelp = toString(executeUnchecked("git branch -d -h"));
  38. assertEquals(CLIText.fatalError(CLIText.get().branchNameRequired), err);
  39. assertEquals(toString(err, help), errAndHelp);
  40. }
  41. @Test
  42. public void testList() throws Exception {
  43. assertEquals("* master", toString(execute("git branch")));
  44. assertEquals("* master 6fd41be initial commit",
  45. toString(execute("git branch -v")));
  46. }
  47. @Test
  48. public void testListDetached() throws Exception {
  49. RefUpdate updateRef = db.updateRef(Constants.HEAD, true);
  50. updateRef.setNewObjectId(db.resolve("6fd41be"));
  51. updateRef.update();
  52. assertEquals(
  53. toString("* (no branch) 6fd41be initial commit",
  54. "master 6fd41be initial commit"),
  55. toString(execute("git branch -v")));
  56. }
  57. @Test
  58. public void testListContains() throws Exception {
  59. try (Git git = new Git(db)) {
  60. git.branchCreate().setName("initial").call();
  61. RevCommit second = git.commit().setMessage("second commit")
  62. .call();
  63. assertEquals(toString(" initial", "* master"),
  64. toString(execute("git branch --contains 6fd41be")));
  65. assertEquals("* master",
  66. toString(execute("git branch --contains " + second.name())));
  67. }
  68. }
  69. @Test
  70. public void testExistingBranch() throws Exception {
  71. assertEquals("fatal: A branch named 'master' already exists.",
  72. toString(executeUnchecked("git branch master")));
  73. }
  74. @Test
  75. public void testRenameSingleArg() throws Exception {
  76. try {
  77. toString(execute("git branch -m"));
  78. fail("Must die");
  79. } catch (Die e) {
  80. // expected, requires argument
  81. }
  82. String result = toString(execute("git branch -m slave"));
  83. assertEquals("", result);
  84. result = toString(execute("git branch -a"));
  85. assertEquals("* slave", result);
  86. }
  87. @Test
  88. public void testRenameTwoArgs() throws Exception {
  89. String result = toString(execute("git branch -m master slave"));
  90. assertEquals("", result);
  91. result = toString(execute("git branch -a"));
  92. assertEquals("* slave", result);
  93. }
  94. @Test
  95. public void testCreate() throws Exception {
  96. try {
  97. toString(execute("git branch a b"));
  98. fail("Must die");
  99. } catch (Die e) {
  100. // expected, too many arguments
  101. }
  102. String result = toString(execute("git branch second"));
  103. assertEquals("", result);
  104. result = toString(execute("git branch"));
  105. assertEquals(toString("* master", "second"), result);
  106. result = toString(execute("git branch -v"));
  107. assertEquals(toString("* master 6fd41be initial commit",
  108. "second 6fd41be initial commit"), result);
  109. }
  110. @Test
  111. public void testDelete() throws Exception {
  112. try {
  113. toString(execute("git branch -d"));
  114. fail("Must die");
  115. } catch (Die e) {
  116. // expected, requires argument
  117. }
  118. String result = toString(execute("git branch second"));
  119. assertEquals("", result);
  120. result = toString(execute("git branch -d second"));
  121. assertEquals("", result);
  122. result = toString(execute("git branch"));
  123. assertEquals("* master", result);
  124. }
  125. @Test
  126. public void testDeleteMultiple() throws Exception {
  127. String result = toString(execute("git branch second",
  128. "git branch third", "git branch fourth"));
  129. assertEquals("", result);
  130. result = toString(execute("git branch -d second third fourth"));
  131. assertEquals("", result);
  132. result = toString(execute("git branch"));
  133. assertEquals("* master", result);
  134. }
  135. @Test
  136. public void testDeleteForce() throws Exception {
  137. try {
  138. toString(execute("git branch -D"));
  139. fail("Must die");
  140. } catch (Die e) {
  141. // expected, requires argument
  142. }
  143. String result = toString(execute("git branch second"));
  144. assertEquals("", result);
  145. result = toString(execute("git checkout second"));
  146. assertEquals("Switched to branch 'second'", result);
  147. File a = writeTrashFile("a", "a");
  148. assertTrue(a.exists());
  149. execute("git add a", "git commit -m 'added a'");
  150. result = toString(execute("git checkout master"));
  151. assertEquals("Switched to branch 'master'", result);
  152. result = toString(execute("git branch"));
  153. assertEquals(toString("* master", "second"), result);
  154. try {
  155. toString(execute("git branch -d second"));
  156. fail("Must die");
  157. } catch (Die e) {
  158. // expected, the current HEAD is on second and not merged to master
  159. }
  160. result = toString(execute("git branch -D second"));
  161. assertEquals("", result);
  162. result = toString(execute("git branch"));
  163. assertEquals("* master", result);
  164. }
  165. @Test
  166. public void testDeleteForceMultiple() throws Exception {
  167. String result = toString(execute("git branch second",
  168. "git branch third", "git branch fourth"));
  169. assertEquals("", result);
  170. result = toString(execute("git checkout second"));
  171. assertEquals("Switched to branch 'second'", result);
  172. File a = writeTrashFile("a", "a");
  173. assertTrue(a.exists());
  174. execute("git add a", "git commit -m 'added a'");
  175. result = toString(execute("git checkout master"));
  176. assertEquals("Switched to branch 'master'", result);
  177. result = toString(execute("git branch"));
  178. assertEquals(toString("fourth", "* master", "second", "third"), result);
  179. try {
  180. toString(execute("git branch -d second third fourth"));
  181. fail("Must die");
  182. } catch (Die e) {
  183. // expected, the current HEAD is on second and not merged to master
  184. }
  185. result = toString(execute("git branch"));
  186. assertEquals(toString("fourth", "* master", "second", "third"), result);
  187. result = toString(execute("git branch -D second third fourth"));
  188. assertEquals("", result);
  189. result = toString(execute("git branch"));
  190. assertEquals("* master", result);
  191. }
  192. @Test
  193. public void testCreateFromOldCommit() throws Exception {
  194. File a = writeTrashFile("a", "a");
  195. assertTrue(a.exists());
  196. execute("git add a", "git commit -m 'added a'");
  197. File b = writeTrashFile("b", "b");
  198. assertTrue(b.exists());
  199. execute("git add b", "git commit -m 'added b'");
  200. String result = toString(execute("git log -n 1 --reverse"));
  201. String firstCommitId = result.substring("commit ".length(),
  202. result.indexOf('\n'));
  203. result = toString(execute("git branch -f second " + firstCommitId));
  204. assertEquals("", result);
  205. result = toString(execute("git branch"));
  206. assertEquals(toString("* master", "second"), result);
  207. result = toString(execute("git checkout second"));
  208. assertEquals("Switched to branch 'second'", result);
  209. assertFalse(b.exists());
  210. }
  211. }