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

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