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

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