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.

CheckoutCommandTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  3. * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import static org.hamcrest.MatcherAssert.assertThat;
  46. import static org.hamcrest.CoreMatchers.is;
  47. import static org.junit.Assert.assertEquals;
  48. import static org.junit.Assert.assertFalse;
  49. import static org.junit.Assert.assertNotNull;
  50. import static org.junit.Assert.assertNull;
  51. import static org.junit.Assert.assertSame;
  52. import static org.junit.Assert.assertTrue;
  53. import static org.junit.Assert.fail;
  54. import java.io.File;
  55. import java.io.FileInputStream;
  56. import java.io.IOException;
  57. import org.eclipse.jgit.api.CheckoutResult.Status;
  58. import org.eclipse.jgit.api.errors.GitAPIException;
  59. import org.eclipse.jgit.api.errors.JGitInternalException;
  60. import org.eclipse.jgit.api.errors.RefNotFoundException;
  61. import org.eclipse.jgit.dircache.DirCache;
  62. import org.eclipse.jgit.dircache.DirCacheEntry;
  63. import org.eclipse.jgit.junit.RepositoryTestCase;
  64. import org.eclipse.jgit.lib.Constants;
  65. import org.eclipse.jgit.lib.Ref;
  66. import org.eclipse.jgit.lib.RefUpdate;
  67. import org.eclipse.jgit.lib.Repository;
  68. import org.eclipse.jgit.lib.StoredConfig;
  69. import org.eclipse.jgit.revwalk.RevCommit;
  70. import org.eclipse.jgit.transport.RefSpec;
  71. import org.eclipse.jgit.transport.RemoteConfig;
  72. import org.eclipse.jgit.transport.URIish;
  73. import org.eclipse.jgit.util.FileUtils;
  74. import org.junit.Before;
  75. import org.junit.Test;
  76. public class CheckoutCommandTest extends RepositoryTestCase {
  77. private Git git;
  78. RevCommit initialCommit;
  79. RevCommit secondCommit;
  80. @Override
  81. @Before
  82. public void setUp() throws Exception {
  83. super.setUp();
  84. git = new Git(db);
  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. // create a master branch and switch to it
  90. git.branchCreate().setName("test").call();
  91. RefUpdate rup = db.updateRef(Constants.HEAD);
  92. rup.link("refs/heads/test");
  93. // commit something on the test branch
  94. writeTrashFile("Test.txt", "Some change");
  95. git.add().addFilepattern("Test.txt").call();
  96. secondCommit = git.commit().setMessage("Second commit").call();
  97. }
  98. @Test
  99. public void testSimpleCheckout() throws Exception {
  100. git.checkout().setName("test").call();
  101. }
  102. @Test
  103. public void testCheckout() throws Exception {
  104. git.checkout().setName("test").call();
  105. assertEquals("[Test.txt, mode:100644, content:Some change]",
  106. indexState(CONTENT));
  107. Ref result = git.checkout().setName("master").call();
  108. assertEquals("[Test.txt, mode:100644, content:Hello world]",
  109. indexState(CONTENT));
  110. assertEquals("refs/heads/master", result.getName());
  111. assertEquals("refs/heads/master", git.getRepository().getFullBranch());
  112. }
  113. @Test
  114. public void testCreateBranchOnCheckout() throws Exception {
  115. git.checkout().setCreateBranch(true).setName("test2").call();
  116. assertNotNull(db.getRef("test2"));
  117. }
  118. @Test
  119. public void testCheckoutToNonExistingBranch() throws GitAPIException {
  120. try {
  121. git.checkout().setName("badbranch").call();
  122. fail("Should have failed");
  123. } catch (RefNotFoundException e) {
  124. // except to hit here
  125. }
  126. }
  127. @Test
  128. public void testCheckoutWithConflict() {
  129. CheckoutCommand co = git.checkout();
  130. try {
  131. writeTrashFile("Test.txt", "Another change");
  132. assertEquals(Status.NOT_TRIED, co.getResult().getStatus());
  133. co.setName("master").call();
  134. fail("Should have failed");
  135. } catch (Exception e) {
  136. assertEquals(Status.CONFLICTS, co.getResult().getStatus());
  137. assertTrue(co.getResult().getConflictList().contains("Test.txt"));
  138. }
  139. }
  140. @Test
  141. public void testCheckoutWithNonDeletedFiles() throws Exception {
  142. File testFile = writeTrashFile("temp", "");
  143. FileInputStream fis = new FileInputStream(testFile);
  144. try {
  145. FileUtils.delete(testFile);
  146. return;
  147. } catch (IOException e) {
  148. // the test makes only sense if deletion of
  149. // a file with open stream fails
  150. } finally {
  151. fis.close();
  152. }
  153. FileUtils.delete(testFile);
  154. CheckoutCommand co = git.checkout();
  155. // delete Test.txt in branch test
  156. testFile = new File(db.getWorkTree(), "Test.txt");
  157. assertTrue(testFile.exists());
  158. FileUtils.delete(testFile);
  159. assertFalse(testFile.exists());
  160. git.add().addFilepattern("Test.txt");
  161. git.commit().setMessage("Delete Test.txt").setAll(true).call();
  162. git.checkout().setName("master").call();
  163. assertTrue(testFile.exists());
  164. // lock the file so it can't be deleted (in Windows, that is)
  165. fis = new FileInputStream(testFile);
  166. try {
  167. assertEquals(Status.NOT_TRIED, co.getResult().getStatus());
  168. co.setName("test").call();
  169. assertTrue(testFile.exists());
  170. assertEquals(Status.NONDELETED, co.getResult().getStatus());
  171. assertTrue(co.getResult().getUndeletedList().contains("Test.txt"));
  172. } finally {
  173. fis.close();
  174. }
  175. }
  176. @Test
  177. public void testCheckoutCommit() throws Exception {
  178. Ref result = git.checkout().setName(initialCommit.name()).call();
  179. assertEquals("[Test.txt, mode:100644, content:Hello world]",
  180. indexState(CONTENT));
  181. assertNull(result);
  182. assertEquals(initialCommit.name(), git.getRepository().getFullBranch());
  183. }
  184. @Test
  185. public void testCheckoutRemoteTrackingWithoutLocalBranch() throws Exception {
  186. // create second repository
  187. Repository db2 = createWorkRepository();
  188. Git git2 = new Git(db2);
  189. // setup the second repository to fetch from the first repository
  190. final StoredConfig config = db2.getConfig();
  191. RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
  192. URIish uri = new URIish(db.getDirectory().toURI().toURL());
  193. remoteConfig.addURI(uri);
  194. remoteConfig.update(config);
  195. config.save();
  196. // fetch from first repository
  197. RefSpec spec = new RefSpec("+refs/heads/*:refs/remotes/origin/*");
  198. git2.fetch().setRemote("origin").setRefSpecs(spec).call();
  199. // checkout remote tracking branch in second repository
  200. // (no local branches exist yet in second repository)
  201. git2.checkout().setName("remotes/origin/test").call();
  202. assertEquals("[Test.txt, mode:100644, content:Some change]",
  203. indexState(db2, CONTENT));
  204. }
  205. @Test
  206. public void testCheckoutOfFileWithInexistentParentDir() throws Exception {
  207. File a = writeTrashFile("dir/a.txt", "A");
  208. writeTrashFile("dir/b.txt", "A");
  209. git.add().addFilepattern("dir/a.txt").addFilepattern("dir/b.txt")
  210. .call();
  211. git.commit().setMessage("Added dir").call();
  212. File dir = new File(db.getWorkTree(), "dir");
  213. FileUtils.delete(dir, FileUtils.RECURSIVE);
  214. git.checkout().addPath("dir/a.txt").call();
  215. assertTrue(a.exists());
  216. }
  217. @Test
  218. public void testCheckoutOfDirectoryShouldBeRecursive() throws Exception {
  219. File a = writeTrashFile("dir/a.txt", "A");
  220. File b = writeTrashFile("dir/sub/b.txt", "B");
  221. git.add().addFilepattern("dir").call();
  222. git.commit().setMessage("Added dir").call();
  223. write(a, "modified");
  224. write(b, "modified");
  225. git.checkout().addPath("dir").call();
  226. assertThat(read(a), is("A"));
  227. assertThat(read(b), is("B"));
  228. }
  229. @Test
  230. public void testCheckoutAllPaths() throws Exception {
  231. File a = writeTrashFile("dir/a.txt", "A");
  232. File b = writeTrashFile("dir/sub/b.txt", "B");
  233. git.add().addFilepattern("dir").call();
  234. git.commit().setMessage("Added dir").call();
  235. write(a, "modified");
  236. write(b, "modified");
  237. git.checkout().setAllPaths(true).call();
  238. assertThat(read(a), is("A"));
  239. assertThat(read(b), is("B"));
  240. }
  241. @Test
  242. public void testCheckoutWithStartPoint() throws Exception {
  243. File a = writeTrashFile("a.txt", "A");
  244. git.add().addFilepattern("a.txt").call();
  245. RevCommit first = git.commit().setMessage("Added a").call();
  246. write(a, "other");
  247. git.commit().setAll(true).setMessage("Other").call();
  248. git.checkout().setCreateBranch(true).setName("a")
  249. .setStartPoint(first.getId().getName()).call();
  250. assertThat(read(a), is("A"));
  251. }
  252. @Test
  253. public void testCheckoutWithStartPointOnlyCertainFiles() throws Exception {
  254. File a = writeTrashFile("a.txt", "A");
  255. File b = writeTrashFile("b.txt", "B");
  256. git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
  257. RevCommit first = git.commit().setMessage("First").call();
  258. write(a, "other");
  259. write(b, "other");
  260. git.commit().setAll(true).setMessage("Other").call();
  261. git.checkout().setCreateBranch(true).setName("a")
  262. .setStartPoint(first.getId().getName()).addPath("a.txt").call();
  263. assertThat(read(a), is("A"));
  264. assertThat(read(b), is("other"));
  265. }
  266. @Test
  267. public void testDetachedHeadOnCheckout() throws JGitInternalException,
  268. IOException, GitAPIException {
  269. CheckoutCommand co = git.checkout();
  270. co.setName("master").call();
  271. String commitId = db.getRef(Constants.MASTER).getObjectId().name();
  272. co = git.checkout();
  273. co.setName(commitId).call();
  274. Ref head = db.getRef(Constants.HEAD);
  275. assertFalse(head.isSymbolic());
  276. assertSame(head, head.getTarget());
  277. }
  278. @Test
  279. public void testUpdateSmudgedEntries() throws Exception {
  280. git.branchCreate().setName("test2").call();
  281. RefUpdate rup = db.updateRef(Constants.HEAD);
  282. rup.link("refs/heads/test2");
  283. File file = new File(db.getWorkTree(), "Test.txt");
  284. long size = file.length();
  285. long mTime = file.lastModified() - 5000L;
  286. assertTrue(file.setLastModified(mTime));
  287. DirCache cache = DirCache.lock(db.getIndexFile(), db.getFS());
  288. DirCacheEntry entry = cache.getEntry("Test.txt");
  289. assertNotNull(entry);
  290. entry.setLength(0);
  291. entry.setLastModified(0);
  292. cache.write();
  293. assertTrue(cache.commit());
  294. cache = DirCache.read(db.getIndexFile(), db.getFS());
  295. entry = cache.getEntry("Test.txt");
  296. assertNotNull(entry);
  297. assertEquals(0, entry.getLength());
  298. assertEquals(0, entry.getLastModified());
  299. db.getIndexFile().setLastModified(
  300. db.getIndexFile().lastModified() - 5000);
  301. assertNotNull(git.checkout().setName("test").call());
  302. cache = DirCache.read(db.getIndexFile(), db.getFS());
  303. entry = cache.getEntry("Test.txt");
  304. assertNotNull(entry);
  305. assertEquals(size, entry.getLength());
  306. assertEquals(mTime, entry.getLastModified());
  307. }
  308. }