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 18KB

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