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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (C) 2012, 2014 IBM Corporation and others.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.pgm;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.File;
  49. import org.eclipse.jgit.api.Git;
  50. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.RefUpdate;
  53. import org.eclipse.jgit.pgm.internal.CLIText;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.junit.Before;
  56. import org.junit.Test;
  57. public class BranchTest extends CLIRepositoryTestCase {
  58. @Override
  59. @Before
  60. public void setUp() throws Exception {
  61. super.setUp();
  62. try (Git git = new Git(db)) {
  63. git.commit().setMessage("initial commit").call();
  64. }
  65. }
  66. @Test
  67. public void testHelpAfterDelete() throws Exception {
  68. String err = toString(executeUnchecked("git branch -d"));
  69. String help = toString(executeUnchecked("git branch -h"));
  70. String errAndHelp = toString(executeUnchecked("git branch -d -h"));
  71. assertEquals(CLIText.fatalError(CLIText.get().branchNameRequired), err);
  72. assertEquals(toString(err, help), errAndHelp);
  73. }
  74. @Test
  75. public void testList() throws Exception {
  76. assertEquals("* master", toString(execute("git branch")));
  77. assertEquals("* master 6fd41be initial commit",
  78. toString(execute("git branch -v")));
  79. }
  80. @Test
  81. public void testListDetached() throws Exception {
  82. RefUpdate updateRef = db.updateRef(Constants.HEAD, true);
  83. updateRef.setNewObjectId(db.resolve("6fd41be"));
  84. updateRef.update();
  85. assertEquals(
  86. toString("* (no branch) 6fd41be initial commit",
  87. "master 6fd41be initial commit"),
  88. toString(execute("git branch -v")));
  89. }
  90. @Test
  91. public void testListContains() throws Exception {
  92. try (Git git = new Git(db)) {
  93. git.branchCreate().setName("initial").call();
  94. RevCommit second = git.commit().setMessage("second commit")
  95. .call();
  96. assertEquals(toString(" initial", "* master"),
  97. toString(execute("git branch --contains 6fd41be")));
  98. assertEquals("* master",
  99. toString(execute("git branch --contains " + second.name())));
  100. }
  101. }
  102. @Test
  103. public void testExistingBranch() throws Exception {
  104. assertEquals("fatal: A branch named 'master' already exists.",
  105. toString(executeUnchecked("git branch master")));
  106. }
  107. @Test
  108. public void testRenameSingleArg() throws Exception {
  109. try {
  110. toString(execute("git branch -m"));
  111. fail("Must die");
  112. } catch (Die e) {
  113. // expected, requires argument
  114. }
  115. String result = toString(execute("git branch -m slave"));
  116. assertEquals("", result);
  117. result = toString(execute("git branch -a"));
  118. assertEquals("* slave", result);
  119. }
  120. @Test
  121. public void testRenameTwoArgs() throws Exception {
  122. String result = toString(execute("git branch -m master slave"));
  123. assertEquals("", result);
  124. result = toString(execute("git branch -a"));
  125. assertEquals("* slave", result);
  126. }
  127. @Test
  128. public void testCreate() throws Exception {
  129. try {
  130. toString(execute("git branch a b"));
  131. fail("Must die");
  132. } catch (Die e) {
  133. // expected, too many arguments
  134. }
  135. String result = toString(execute("git branch second"));
  136. assertEquals("", result);
  137. result = toString(execute("git branch"));
  138. assertEquals(toString("* master", "second"), result);
  139. result = toString(execute("git branch -v"));
  140. assertEquals(toString("* master 6fd41be initial commit",
  141. "second 6fd41be initial commit"), result);
  142. }
  143. @Test
  144. public void testDelete() throws Exception {
  145. try {
  146. toString(execute("git branch -d"));
  147. fail("Must die");
  148. } catch (Die e) {
  149. // expected, requires argument
  150. }
  151. String result = toString(execute("git branch second"));
  152. assertEquals("", result);
  153. result = toString(execute("git branch -d second"));
  154. assertEquals("", result);
  155. result = toString(execute("git branch"));
  156. assertEquals("* master", result);
  157. }
  158. @Test
  159. public void testDeleteMultiple() throws Exception {
  160. String result = toString(execute("git branch second",
  161. "git branch third", "git branch fourth"));
  162. assertEquals("", result);
  163. result = toString(execute("git branch -d second third fourth"));
  164. assertEquals("", result);
  165. result = toString(execute("git branch"));
  166. assertEquals("* master", result);
  167. }
  168. @Test
  169. public void testDeleteForce() throws Exception {
  170. try {
  171. toString(execute("git branch -D"));
  172. fail("Must die");
  173. } catch (Die e) {
  174. // expected, requires argument
  175. }
  176. String result = toString(execute("git branch second"));
  177. assertEquals("", result);
  178. result = toString(execute("git checkout second"));
  179. assertEquals("Switched to branch 'second'", result);
  180. File a = writeTrashFile("a", "a");
  181. assertTrue(a.exists());
  182. execute("git add a", "git commit -m 'added a'");
  183. result = toString(execute("git checkout master"));
  184. assertEquals("Switched to branch 'master'", result);
  185. result = toString(execute("git branch"));
  186. assertEquals(toString("* master", "second"), result);
  187. try {
  188. toString(execute("git branch -d second"));
  189. fail("Must die");
  190. } catch (Die e) {
  191. // expected, the current HEAD is on second and not merged to master
  192. }
  193. result = toString(execute("git branch -D second"));
  194. assertEquals("", result);
  195. result = toString(execute("git branch"));
  196. assertEquals("* master", result);
  197. }
  198. @Test
  199. public void testDeleteForceMultiple() throws Exception {
  200. String result = toString(execute("git branch second",
  201. "git branch third", "git branch fourth"));
  202. assertEquals("", result);
  203. result = toString(execute("git checkout second"));
  204. assertEquals("Switched to branch 'second'", result);
  205. File a = writeTrashFile("a", "a");
  206. assertTrue(a.exists());
  207. execute("git add a", "git commit -m 'added a'");
  208. result = toString(execute("git checkout master"));
  209. assertEquals("Switched to branch 'master'", result);
  210. result = toString(execute("git branch"));
  211. assertEquals(toString("fourth", "* master", "second", "third"), result);
  212. try {
  213. toString(execute("git branch -d second third fourth"));
  214. fail("Must die");
  215. } catch (Die e) {
  216. // expected, the current HEAD is on second and not merged to master
  217. }
  218. result = toString(execute("git branch"));
  219. assertEquals(toString("fourth", "* master", "second", "third"), result);
  220. result = toString(execute("git branch -D second third fourth"));
  221. assertEquals("", result);
  222. result = toString(execute("git branch"));
  223. assertEquals("* master", result);
  224. }
  225. @Test
  226. public void testCreateFromOldCommit() throws Exception {
  227. File a = writeTrashFile("a", "a");
  228. assertTrue(a.exists());
  229. execute("git add a", "git commit -m 'added a'");
  230. File b = writeTrashFile("b", "b");
  231. assertTrue(b.exists());
  232. execute("git add b", "git commit -m 'added b'");
  233. String result = toString(execute("git log -n 1 --reverse"));
  234. String firstCommitId = result.substring("commit ".length(),
  235. result.indexOf('\n'));
  236. result = toString(execute("git branch -f second " + firstCommitId));
  237. assertEquals("", result);
  238. result = toString(execute("git branch"));
  239. assertEquals(toString("* master", "second"), result);
  240. result = toString(execute("git checkout second"));
  241. assertEquals("Switched to branch 'second'", result);
  242. assertFalse(b.exists());
  243. }
  244. }