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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertNull;
  49. import static org.junit.Assert.assertSame;
  50. import static org.junit.Assert.assertTrue;
  51. import static org.junit.Assert.fail;
  52. import java.io.File;
  53. import java.io.FileInputStream;
  54. import java.io.IOException;
  55. import org.eclipse.jgit.api.CheckoutResult.Status;
  56. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  57. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  58. import org.eclipse.jgit.api.errors.JGitInternalException;
  59. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  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.lib.Constants;
  64. import org.eclipse.jgit.lib.Ref;
  65. import org.eclipse.jgit.lib.RefUpdate;
  66. import org.eclipse.jgit.lib.Repository;
  67. import org.eclipse.jgit.lib.RepositoryTestCase;
  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 JGitInternalException,
  120. RefAlreadyExistsException, InvalidRefNameException,
  121. CheckoutConflictException {
  122. try {
  123. git.checkout().setName("badbranch").call();
  124. fail("Should have failed");
  125. } catch (RefNotFoundException e) {
  126. // except to hit here
  127. }
  128. }
  129. @Test
  130. public void testCheckoutWithConflict() {
  131. CheckoutCommand co = git.checkout();
  132. try {
  133. writeTrashFile("Test.txt", "Another change");
  134. assertEquals(Status.NOT_TRIED, co.getResult().getStatus());
  135. co.setName("master").call();
  136. fail("Should have failed");
  137. } catch (Exception e) {
  138. assertEquals(Status.CONFLICTS, co.getResult().getStatus());
  139. assertTrue(co.getResult().getConflictList().contains("Test.txt"));
  140. }
  141. }
  142. @Test
  143. public void testCheckoutWithNonDeletedFiles() throws Exception {
  144. File testFile = writeTrashFile("temp", "");
  145. FileInputStream fis = new FileInputStream(testFile);
  146. try {
  147. FileUtils.delete(testFile);
  148. return;
  149. } catch (IOException e) {
  150. // the test makes only sense if deletion of
  151. // a file with open stream fails
  152. }
  153. fis.close();
  154. FileUtils.delete(testFile);
  155. CheckoutCommand co = git.checkout();
  156. // delete Test.txt in branch test
  157. testFile = new File(db.getWorkTree(), "Test.txt");
  158. assertTrue(testFile.exists());
  159. FileUtils.delete(testFile);
  160. assertFalse(testFile.exists());
  161. git.add().addFilepattern("Test.txt");
  162. git.commit().setMessage("Delete Test.txt").setAll(true).call();
  163. git.checkout().setName("master").call();
  164. assertTrue(testFile.exists());
  165. // lock the file so it can't be deleted (in Windows, that is)
  166. fis = new FileInputStream(testFile);
  167. try {
  168. assertEquals(Status.NOT_TRIED, co.getResult().getStatus());
  169. co.setName("test").call();
  170. assertTrue(testFile.exists());
  171. assertEquals(Status.NONDELETED, co.getResult().getStatus());
  172. assertTrue(co.getResult().getUndeletedList().contains("Test.txt"));
  173. } finally {
  174. fis.close();
  175. }
  176. }
  177. @Test
  178. public void testCheckoutCommit() throws Exception {
  179. Ref result = git.checkout().setName(initialCommit.name()).call();
  180. assertEquals("[Test.txt, mode:100644, content:Hello world]",
  181. indexState(CONTENT));
  182. assertNull(result);
  183. assertEquals(initialCommit.name(), git.getRepository().getFullBranch());
  184. }
  185. @Test
  186. public void testCheckoutRemoteTrackingWithoutLocalBranch() throws Exception {
  187. // create second repository
  188. Repository db2 = createWorkRepository();
  189. Git git2 = new Git(db2);
  190. // setup the second repository to fetch from the first repository
  191. final StoredConfig config = db2.getConfig();
  192. RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
  193. URIish uri = new URIish(db.getDirectory().toURI().toURL());
  194. remoteConfig.addURI(uri);
  195. remoteConfig.update(config);
  196. config.save();
  197. // fetch from first repository
  198. RefSpec spec = new RefSpec("+refs/heads/*:refs/remotes/origin/*");
  199. git2.fetch().setRemote("origin").setRefSpecs(spec).call();
  200. // checkout remote tracking branch in second repository
  201. // (no local branches exist yet in second repository)
  202. git2.checkout().setName("remotes/origin/test").call();
  203. assertEquals("[Test.txt, mode:100644, content:Some change]",
  204. indexState(db2, CONTENT));
  205. }
  206. @Test
  207. public void testDetachedHeadOnCheckout() throws JGitInternalException,
  208. RefAlreadyExistsException, RefNotFoundException,
  209. InvalidRefNameException, IOException, CheckoutConflictException {
  210. CheckoutCommand co = git.checkout();
  211. co.setName("master").call();
  212. String commitId = db.getRef(Constants.MASTER).getObjectId().name();
  213. co = git.checkout();
  214. co.setName(commitId).call();
  215. Ref head = db.getRef(Constants.HEAD);
  216. assertFalse(head.isSymbolic());
  217. assertSame(head, head.getTarget());
  218. }
  219. @Test
  220. public void testUpdateSmudgedEntries() throws Exception {
  221. git.branchCreate().setName("test2").call();
  222. RefUpdate rup = db.updateRef(Constants.HEAD);
  223. rup.link("refs/heads/test2");
  224. File file = new File(db.getWorkTree(), "Test.txt");
  225. long size = file.length();
  226. long mTime = file.lastModified() - 5000L;
  227. assertTrue(file.setLastModified(mTime));
  228. DirCache cache = DirCache.lock(db.getIndexFile(), db.getFS());
  229. DirCacheEntry entry = cache.getEntry("Test.txt");
  230. assertNotNull(entry);
  231. entry.setLength(0);
  232. entry.setLastModified(0);
  233. cache.write();
  234. assertTrue(cache.commit());
  235. cache = DirCache.read(db.getIndexFile(), db.getFS());
  236. entry = cache.getEntry("Test.txt");
  237. assertNotNull(entry);
  238. assertEquals(0, entry.getLength());
  239. assertEquals(0, entry.getLastModified());
  240. db.getIndexFile().setLastModified(
  241. db.getIndexFile().lastModified() - 5000);
  242. assertNotNull(git.checkout().setName("test").call());
  243. cache = DirCache.read(db.getIndexFile(), db.getFS());
  244. entry = cache.getEntry("Test.txt");
  245. assertNotNull(entry);
  246. assertEquals(size, entry.getLength());
  247. assertEquals(mTime, entry.getLastModified());
  248. }
  249. }