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.

BranchCommandTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  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.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.fail;
  48. import java.util.List;
  49. import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
  50. import org.eclipse.jgit.api.ListBranchCommand.ListMode;
  51. import org.eclipse.jgit.api.errors.CannotDeleteCurrentBranchException;
  52. import org.eclipse.jgit.api.errors.DetachedHeadException;
  53. import org.eclipse.jgit.api.errors.GitAPIException;
  54. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  55. import org.eclipse.jgit.api.errors.JGitInternalException;
  56. import org.eclipse.jgit.api.errors.NotMergedException;
  57. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  58. import org.eclipse.jgit.api.errors.RefNotFoundException;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.Ref;
  61. import org.eclipse.jgit.lib.RefUpdate;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.eclipse.jgit.lib.RepositoryTestCase;
  64. import org.eclipse.jgit.lib.StoredConfig;
  65. import org.eclipse.jgit.revwalk.RevCommit;
  66. import org.eclipse.jgit.transport.FetchResult;
  67. import org.eclipse.jgit.transport.RefSpec;
  68. import org.eclipse.jgit.transport.RemoteConfig;
  69. import org.eclipse.jgit.transport.URIish;
  70. import org.junit.Before;
  71. import org.junit.Test;
  72. public class BranchCommandTest extends RepositoryTestCase {
  73. private Git git;
  74. RevCommit initialCommit;
  75. RevCommit secondCommit;
  76. @Override
  77. @Before
  78. public void setUp() throws Exception {
  79. super.setUp();
  80. git = new Git(db);
  81. // checkout master
  82. git.commit().setMessage("initial commit").call();
  83. // commit something
  84. writeTrashFile("Test.txt", "Hello world");
  85. git.add().addFilepattern("Test.txt").call();
  86. initialCommit = git.commit().setMessage("Initial commit").call();
  87. writeTrashFile("Test.txt", "Some change");
  88. git.add().addFilepattern("Test.txt").call();
  89. secondCommit = git.commit().setMessage("Second commit").call();
  90. // create a master branch
  91. RefUpdate rup = db.updateRef("refs/heads/master");
  92. rup.setNewObjectId(initialCommit.getId());
  93. rup.setForceUpdate(true);
  94. rup.update();
  95. }
  96. private Git setUpRepoWithRemote() throws Exception {
  97. Repository remoteRepository = createWorkRepository();
  98. Git remoteGit = new Git(remoteRepository);
  99. // commit something
  100. writeTrashFile("Test.txt", "Hello world");
  101. remoteGit.add().addFilepattern("Test.txt").call();
  102. initialCommit = remoteGit.commit().setMessage("Initial commit").call();
  103. writeTrashFile("Test.txt", "Some change");
  104. remoteGit.add().addFilepattern("Test.txt").call();
  105. secondCommit = remoteGit.commit().setMessage("Second commit").call();
  106. // create a master branch
  107. RefUpdate rup = remoteRepository.updateRef("refs/heads/master");
  108. rup.setNewObjectId(initialCommit.getId());
  109. rup.forceUpdate();
  110. Repository localRepository = createWorkRepository();
  111. Git localGit = new Git(localRepository);
  112. StoredConfig config = localRepository.getConfig();
  113. RemoteConfig rc = new RemoteConfig(config, "origin");
  114. rc.addURI(new URIish(remoteRepository.getDirectory().getPath()));
  115. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
  116. rc.update(config);
  117. config.save();
  118. FetchResult res = localGit.fetch().setRemote("origin").call();
  119. assertFalse(res.getTrackingRefUpdates().isEmpty());
  120. rup = localRepository.updateRef("refs/heads/master");
  121. rup.setNewObjectId(initialCommit.getId());
  122. rup.forceUpdate();
  123. rup = localRepository.updateRef(Constants.HEAD);
  124. rup.link("refs/heads/master");
  125. rup.setNewObjectId(initialCommit.getId());
  126. rup.update();
  127. return localGit;
  128. }
  129. @Test
  130. public void testCreateAndList() throws Exception {
  131. int localBefore;
  132. int remoteBefore;
  133. int allBefore;
  134. // invalid name not allowed
  135. try {
  136. git.branchCreate().setName("In va lid").call();
  137. fail("Create branch with invalid ref name should fail");
  138. } catch (InvalidRefNameException e) {
  139. // expected
  140. }
  141. // existing name not allowed w/o force
  142. try {
  143. git.branchCreate().setName("master").call();
  144. fail("Create branch with existing ref name should fail");
  145. } catch (RefAlreadyExistsException e) {
  146. // expected
  147. }
  148. localBefore = git.branchList().call().size();
  149. remoteBefore = git.branchList().setListMode(ListMode.REMOTE).call()
  150. .size();
  151. allBefore = git.branchList().setListMode(ListMode.ALL).call().size();
  152. assertEquals(localBefore + remoteBefore, allBefore);
  153. Ref newBranch = createBranch(git, "NewForTestList", false, "master",
  154. null);
  155. assertEquals("refs/heads/NewForTestList", newBranch.getName());
  156. assertEquals(1, git.branchList().call().size() - localBefore);
  157. assertEquals(0, git.branchList().setListMode(ListMode.REMOTE).call()
  158. .size()
  159. - remoteBefore);
  160. assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
  161. .size()
  162. - allBefore);
  163. // we can only create local branches
  164. newBranch = createBranch(git,
  165. "refs/remotes/origin/NewRemoteForTestList", false, "master",
  166. null);
  167. assertEquals("refs/heads/refs/remotes/origin/NewRemoteForTestList",
  168. newBranch.getName());
  169. assertEquals(2, git.branchList().call().size() - localBefore);
  170. assertEquals(0, git.branchList().setListMode(ListMode.REMOTE).call()
  171. .size()
  172. - remoteBefore);
  173. assertEquals(2, git.branchList().setListMode(ListMode.ALL).call()
  174. .size()
  175. - allBefore);
  176. }
  177. @Test
  178. public void testCreateFromCommit() throws Exception {
  179. Ref branch = git.branchCreate().setName("FromInitial").setStartPoint(
  180. initialCommit).call();
  181. assertEquals(initialCommit.getId(), branch.getObjectId());
  182. branch = git.branchCreate().setName("FromInitial2").setStartPoint(
  183. initialCommit.getId().name()).call();
  184. assertEquals(initialCommit.getId(), branch.getObjectId());
  185. try {
  186. git.branchCreate().setName("FromInitial").setStartPoint(
  187. secondCommit).call();
  188. } catch (RefAlreadyExistsException e) {
  189. // expected
  190. }
  191. branch = git.branchCreate().setName("FromInitial").setStartPoint(
  192. secondCommit).setForce(true).call();
  193. assertEquals(secondCommit.getId(), branch.getObjectId());
  194. }
  195. @Test
  196. public void testCreateForce() throws Exception {
  197. // using commits
  198. Ref newBranch = createBranch(git, "NewForce", false, secondCommit
  199. .getId().name(), null);
  200. assertEquals(newBranch.getTarget().getObjectId(), secondCommit.getId());
  201. try {
  202. newBranch = createBranch(git, "NewForce", false, initialCommit
  203. .getId().name(), null);
  204. fail("Should have failed");
  205. } catch (RefAlreadyExistsException e) {
  206. // expected
  207. }
  208. newBranch = createBranch(git, "NewForce", true, initialCommit.getId()
  209. .name(), null);
  210. assertEquals(newBranch.getTarget().getObjectId(), initialCommit.getId());
  211. git.branchDelete().setBranchNames("NewForce").call();
  212. // using names
  213. git.branchCreate().setName("NewForce").setStartPoint("master").call();
  214. assertEquals(newBranch.getTarget().getObjectId(), initialCommit.getId());
  215. try {
  216. git.branchCreate().setName("NewForce").setStartPoint("master")
  217. .call();
  218. fail("Should have failed");
  219. } catch (RefAlreadyExistsException e) {
  220. // expected
  221. }
  222. git.branchCreate().setName("NewForce").setStartPoint("master")
  223. .setForce(true).call();
  224. assertEquals(newBranch.getTarget().getObjectId(), initialCommit.getId());
  225. }
  226. @Test
  227. public void testDelete() throws Exception {
  228. createBranch(git, "ForDelete", false, "master", null);
  229. git.branchDelete().setBranchNames("ForDelete").call();
  230. // now point the branch to a non-merged commit
  231. createBranch(git, "ForDelete", false, secondCommit.getId().name(), null);
  232. try {
  233. git.branchDelete().setBranchNames("ForDelete").call();
  234. fail("Deletion of a non-merged branch without force should have failed");
  235. } catch (NotMergedException e) {
  236. // expected
  237. }
  238. List<String> deleted = git.branchDelete().setBranchNames("ForDelete")
  239. .setForce(true).call();
  240. assertEquals(1, deleted.size());
  241. assertEquals(Constants.R_HEADS + "ForDelete", deleted.get(0));
  242. createBranch(git, "ForDelete", false, "master", null);
  243. try {
  244. createBranch(git, "ForDelete", false, "master", null);
  245. fail("Repeated creation of same branch without force should fail");
  246. } catch (RefAlreadyExistsException e) {
  247. // expected
  248. }
  249. // change starting point
  250. Ref newBranch = createBranch(git, "ForDelete", true, initialCommit
  251. .name(), null);
  252. assertEquals(newBranch.getTarget().getObjectId(), initialCommit.getId());
  253. newBranch = createBranch(git, "ForDelete", true, secondCommit.name(),
  254. null);
  255. assertEquals(newBranch.getTarget().getObjectId(), secondCommit.getId());
  256. git.branchDelete().setBranchNames("ForDelete").setForce(true);
  257. try {
  258. git.branchDelete().setBranchNames("master").call();
  259. fail("Deletion of checked out branch without force should have failed");
  260. } catch (CannotDeleteCurrentBranchException e) {
  261. // expected
  262. }
  263. try {
  264. git.branchDelete().setBranchNames("master").setForce(true).call();
  265. fail("Deletion of checked out branch with force should have failed");
  266. } catch (CannotDeleteCurrentBranchException e) {
  267. // expected
  268. }
  269. }
  270. @Test
  271. public void testPullConfigRemoteBranch() throws Exception {
  272. Git localGit = setUpRepoWithRemote();
  273. Ref remote = localGit.branchList().setListMode(ListMode.REMOTE).call()
  274. .get(0);
  275. assertEquals("refs/remotes/origin/master", remote.getName());
  276. // by default, we should create pull configuration
  277. createBranch(localGit, "newFromRemote", false, remote.getName(), null);
  278. assertEquals("origin", localGit.getRepository().getConfig().getString(
  279. "branch", "newFromRemote", "remote"));
  280. localGit.branchDelete().setBranchNames("newFromRemote").call();
  281. // the pull configuration should be gone after deletion
  282. assertNull(localGit.getRepository().getConfig().getString("branch",
  283. "newFromRemote", "remote"));
  284. createBranch(localGit, "newFromRemote", false, remote.getName(), null);
  285. assertEquals("origin", localGit.getRepository().getConfig().getString(
  286. "branch", "newFromRemote", "remote"));
  287. localGit.branchDelete().setBranchNames("refs/heads/newFromRemote")
  288. .call();
  289. // the pull configuration should be gone after deletion
  290. assertNull(localGit.getRepository().getConfig().getString("branch",
  291. "newFromRemote", "remote"));
  292. // use --no-track
  293. createBranch(localGit, "newFromRemote", false, remote.getName(),
  294. SetupUpstreamMode.NOTRACK);
  295. assertNull(localGit.getRepository().getConfig().getString("branch",
  296. "newFromRemote", "remote"));
  297. localGit.branchDelete().setBranchNames("newFromRemote").call();
  298. }
  299. @Test
  300. public void testPullConfigLocalBranch() throws Exception {
  301. Git localGit = setUpRepoWithRemote();
  302. // by default, we should not create pull configuration
  303. createBranch(localGit, "newFromMaster", false, "master", null);
  304. assertNull(localGit.getRepository().getConfig().getString("branch",
  305. "newFromMaster", "remote"));
  306. localGit.branchDelete().setBranchNames("newFromMaster").call();
  307. // use --track
  308. createBranch(localGit, "newFromMaster", false, "master",
  309. SetupUpstreamMode.TRACK);
  310. assertEquals(".", localGit.getRepository().getConfig().getString(
  311. "branch", "newFromMaster", "remote"));
  312. localGit.branchDelete().setBranchNames("refs/heads/newFromMaster")
  313. .call();
  314. // the pull configuration should be gone after deletion
  315. assertNull(localGit.getRepository().getConfig().getString("branch",
  316. "newFromRemote", "remote"));
  317. }
  318. @Test
  319. public void testPullConfigRenameLocalBranch() throws Exception {
  320. Git localGit = setUpRepoWithRemote();
  321. // by default, we should not create pull configuration
  322. createBranch(localGit, "newFromMaster", false, "master", null);
  323. assertNull(localGit.getRepository().getConfig().getString("branch",
  324. "newFromMaster", "remote"));
  325. localGit.branchDelete().setBranchNames("newFromMaster").call();
  326. // use --track
  327. createBranch(localGit, "newFromMaster", false, "master",
  328. SetupUpstreamMode.TRACK);
  329. assertEquals(".", localGit.getRepository().getConfig().getString(
  330. "branch", "newFromMaster", "remote"));
  331. localGit.branchRename().setOldName("newFromMaster").setNewName(
  332. "renamed").call();
  333. assertNull(".", localGit.getRepository().getConfig().getString(
  334. "branch", "newFromMaster", "remote"));
  335. assertEquals(".", localGit.getRepository().getConfig().getString(
  336. "branch", "renamed", "remote"));
  337. localGit.branchDelete().setBranchNames("renamed").call();
  338. // the pull configuration should be gone after deletion
  339. assertNull(localGit.getRepository().getConfig().getString("branch",
  340. "newFromRemote", "remote"));
  341. }
  342. @Test
  343. public void testRenameLocalBranch() throws Exception {
  344. // null newName not allowed
  345. try {
  346. git.branchRename().call();
  347. } catch (InvalidRefNameException e) {
  348. // expected
  349. }
  350. // invalid newName not allowed
  351. try {
  352. git.branchRename().setNewName("In va lid").call();
  353. } catch (InvalidRefNameException e) {
  354. // expected
  355. }
  356. // not existing name not allowed
  357. try {
  358. git.branchRename().setOldName("notexistingbranch").setNewName(
  359. "newname").call();
  360. } catch (RefNotFoundException e) {
  361. // expected
  362. }
  363. // create some branch
  364. createBranch(git, "existing", false, "master", null);
  365. // a local branch
  366. Ref branch = createBranch(git, "fromMasterForRename", false, "master",
  367. null);
  368. assertEquals(Constants.R_HEADS + "fromMasterForRename", branch
  369. .getName());
  370. Ref renamed = git.branchRename().setOldName("fromMasterForRename")
  371. .setNewName("newName").call();
  372. assertEquals(Constants.R_HEADS + "newName", renamed.getName());
  373. try {
  374. git.branchRename().setOldName(renamed.getName()).setNewName(
  375. "existing").call();
  376. fail("Should have failed");
  377. } catch (RefAlreadyExistsException e) {
  378. // expected
  379. }
  380. try {
  381. git.branchRename().setNewName("In va lid").call();
  382. fail("Rename with invalid ref name should fail");
  383. } catch (InvalidRefNameException e) {
  384. // expected
  385. }
  386. // rename without old name and detached head not allowed
  387. RefUpdate rup = git.getRepository().updateRef(Constants.HEAD, true);
  388. rup.setNewObjectId(initialCommit);
  389. rup.forceUpdate();
  390. try {
  391. git.branchRename().setNewName("detached").call();
  392. } catch (DetachedHeadException e) {
  393. // expected
  394. }
  395. }
  396. @Test
  397. public void testRenameRemoteTrackingBranch() throws Exception {
  398. Git localGit = setUpRepoWithRemote();
  399. Ref remoteBranch = localGit.branchList().setListMode(ListMode.REMOTE)
  400. .call().get(0);
  401. Ref renamed = localGit.branchRename()
  402. .setOldName(remoteBranch.getName()).setNewName("newRemote")
  403. .call();
  404. assertEquals(Constants.R_REMOTES + "newRemote", renamed.getName());
  405. }
  406. @Test
  407. public void testCreationImplicitStart() throws JGitInternalException,
  408. GitAPIException {
  409. git.branchCreate().setName("topic").call();
  410. }
  411. public Ref createBranch(Git actGit, String name, boolean force,
  412. String startPoint, SetupUpstreamMode mode)
  413. throws JGitInternalException, RefAlreadyExistsException,
  414. RefNotFoundException,
  415. InvalidRefNameException {
  416. CreateBranchCommand cmd = actGit.branchCreate();
  417. cmd.setName(name);
  418. cmd.setForce(force);
  419. cmd.setStartPoint(startPoint);
  420. cmd.setUpstreamMode(mode);
  421. return cmd.call();
  422. }
  423. }