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.

PathCheckoutCommandTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (C) 2011, 2020 Kevin Sawicki <kevin@github.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.nio.file.Path;
  17. import org.eclipse.jgit.api.CheckoutCommand.Stage;
  18. import org.eclipse.jgit.api.errors.JGitInternalException;
  19. import org.eclipse.jgit.dircache.DirCache;
  20. import org.eclipse.jgit.dircache.DirCacheEntry;
  21. import org.eclipse.jgit.errors.NoWorkTreeException;
  22. import org.eclipse.jgit.junit.RepositoryTestCase;
  23. import org.eclipse.jgit.lib.ConfigConstants;
  24. import org.eclipse.jgit.lib.Constants;
  25. import org.eclipse.jgit.lib.ObjectReader;
  26. import org.eclipse.jgit.lib.RepositoryState;
  27. import org.eclipse.jgit.lib.StoredConfig;
  28. import org.eclipse.jgit.revwalk.RevCommit;
  29. import org.eclipse.jgit.util.FS;
  30. import org.eclipse.jgit.util.FileUtils;
  31. import org.junit.Assume;
  32. import org.junit.Before;
  33. import org.junit.Test;
  34. /**
  35. * Unit tests of path-based uses of {@link CheckoutCommand}
  36. */
  37. public class PathCheckoutCommandTest extends RepositoryTestCase {
  38. private static final String FILE1 = "f/Test.txt";
  39. private static final String FILE2 = "Test2.txt";
  40. private static final String FILE3 = "Test3.txt";
  41. private static final String LINK = "link";
  42. Git git;
  43. RevCommit initialCommit;
  44. RevCommit secondCommit;
  45. @Override
  46. @Before
  47. public void setUp() throws Exception {
  48. super.setUp();
  49. git = new Git(db);
  50. writeTrashFile(FILE1, "1");
  51. writeTrashFile(FILE2, "a");
  52. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  53. initialCommit = git.commit().setMessage("Initial commit").call();
  54. writeTrashFile(FILE1, "2");
  55. writeTrashFile(FILE2, "b");
  56. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  57. secondCommit = git.commit().setMessage("Second commit").call();
  58. writeTrashFile(FILE1, "3");
  59. writeTrashFile(FILE2, "c");
  60. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  61. git.commit().setMessage("Third commit").call();
  62. }
  63. @Test
  64. public void testUpdateSymLink() throws Exception {
  65. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  66. Path path = writeLink(LINK, FILE1);
  67. git.add().addFilepattern(LINK).call();
  68. git.commit().setMessage("Added link").call();
  69. assertEquals("3", read(path.toFile()));
  70. writeLink(LINK, FILE2);
  71. assertEquals("c", read(path.toFile()));
  72. CheckoutCommand co = git.checkout();
  73. co.addPath(LINK).call();
  74. assertEquals("3", read(path.toFile()));
  75. }
  76. @Test
  77. public void testUpdateBrokenSymLinkToDirectory() throws Exception {
  78. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  79. Path path = writeLink(LINK, "f");
  80. git.add().addFilepattern(LINK).call();
  81. git.commit().setMessage("Added link").call();
  82. assertEquals("f", FileUtils.readSymLink(path.toFile()));
  83. assertTrue(path.toFile().exists());
  84. writeLink(LINK, "link_to_nowhere");
  85. assertFalse(path.toFile().exists());
  86. assertEquals("link_to_nowhere", FileUtils.readSymLink(path.toFile()));
  87. CheckoutCommand co = git.checkout();
  88. co.addPath(LINK).call();
  89. assertEquals("f", FileUtils.readSymLink(path.toFile()));
  90. }
  91. @Test
  92. public void testUpdateBrokenSymLink() throws Exception {
  93. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  94. Path path = writeLink(LINK, FILE1);
  95. git.add().addFilepattern(LINK).call();
  96. git.commit().setMessage("Added link").call();
  97. assertEquals("3", read(path.toFile()));
  98. assertEquals(FILE1, FileUtils.readSymLink(path.toFile()));
  99. writeLink(LINK, "link_to_nowhere");
  100. assertFalse(path.toFile().exists());
  101. assertEquals("link_to_nowhere", FileUtils.readSymLink(path.toFile()));
  102. CheckoutCommand co = git.checkout();
  103. co.addPath(LINK).call();
  104. assertEquals("3", read(path.toFile()));
  105. }
  106. @Test
  107. public void testUpdateWorkingDirectory() throws Exception {
  108. CheckoutCommand co = git.checkout();
  109. File written = writeTrashFile(FILE1, "");
  110. assertEquals("", read(written));
  111. co.addPath(FILE1).call();
  112. assertEquals("3", read(written));
  113. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  114. }
  115. @Test
  116. public void testCheckoutFirst() throws Exception {
  117. CheckoutCommand co = git.checkout();
  118. File written = writeTrashFile(FILE1, "");
  119. co.setStartPoint(initialCommit).addPath(FILE1).call();
  120. assertEquals("1", read(written));
  121. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  122. }
  123. @Test
  124. public void testCheckoutSecond() throws Exception {
  125. CheckoutCommand co = git.checkout();
  126. File written = writeTrashFile(FILE1, "");
  127. co.setStartPoint("HEAD~1").addPath(FILE1).call();
  128. assertEquals("2", read(written));
  129. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  130. }
  131. @Test
  132. public void testCheckoutMultiple() throws Exception {
  133. CheckoutCommand co = git.checkout();
  134. File test = writeTrashFile(FILE1, "");
  135. File test2 = writeTrashFile(FILE2, "");
  136. co.setStartPoint("HEAD~2").addPath(FILE1).addPath(FILE2).call();
  137. assertEquals("1", read(test));
  138. assertEquals("a", read(test2));
  139. }
  140. @Test
  141. public void testUpdateWorkingDirectoryFromIndex() throws Exception {
  142. CheckoutCommand co = git.checkout();
  143. File written = writeTrashFile(FILE1, "3a");
  144. git.add().addFilepattern(FILE1).call();
  145. written = writeTrashFile(FILE1, "");
  146. assertEquals("", read(written));
  147. co.addPath(FILE1).call();
  148. assertEquals("3a", read(written));
  149. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  150. }
  151. @Test
  152. public void testUpdateWorkingDirectoryFromHeadWithIndexChange()
  153. throws Exception {
  154. CheckoutCommand co = git.checkout();
  155. File written = writeTrashFile(FILE1, "3a");
  156. git.add().addFilepattern(FILE1).call();
  157. written = writeTrashFile(FILE1, "");
  158. assertEquals("", read(written));
  159. co.addPath(FILE1).setStartPoint("HEAD").call();
  160. assertEquals("3", read(written));
  161. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  162. }
  163. @Test
  164. public void testUpdateWorkingDirectoryFromIndex2() throws Exception {
  165. CheckoutCommand co = git.checkout();
  166. fsTick(git.getRepository().getIndexFile());
  167. File written1 = writeTrashFile(FILE1, "3(modified)");
  168. File written2 = writeTrashFile(FILE2, "a(modified)");
  169. fsTick(written2);
  170. // make sure that we get unsmudged entries for FILE1 and FILE2
  171. writeTrashFile(FILE3, "foo");
  172. git.add().addFilepattern(FILE3).call();
  173. fsTick(git.getRepository().getIndexFile());
  174. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  175. fsTick(git.getRepository().getIndexFile());
  176. writeTrashFile(FILE1, "3(modified again)");
  177. writeTrashFile(FILE2, "a(modified again)");
  178. fsTick(written2);
  179. co.addPath(FILE1).setStartPoint(secondCommit).call();
  180. assertEquals("2", read(written1));
  181. assertEquals("a(modified again)", read(written2));
  182. validateIndex(git);
  183. }
  184. public static void validateIndex(Git git) throws NoWorkTreeException,
  185. IOException {
  186. DirCache dc = git.getRepository().lockDirCache();
  187. try (ObjectReader r = git.getRepository().getObjectDatabase()
  188. .newReader()) {
  189. for (int i = 0; i < dc.getEntryCount(); ++i) {
  190. DirCacheEntry entry = dc.getEntry(i);
  191. if (entry.getLength() > 0)
  192. assertEquals(entry.getLength(), r.getObjectSize(
  193. entry.getObjectId(), ObjectReader.OBJ_ANY));
  194. }
  195. } finally {
  196. dc.unlock();
  197. }
  198. }
  199. @Test
  200. public void testCheckoutMixedNewlines() throws Exception {
  201. // "git config core.autocrlf true"
  202. StoredConfig config = git.getRepository().getConfig();
  203. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  204. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  205. config.save();
  206. // edit <FILE1>
  207. File written = writeTrashFile(FILE1, "4\r\n4");
  208. assertEquals("4\r\n4", read(written));
  209. // "git add <FILE1>"
  210. git.add().addFilepattern(FILE1).call();
  211. // "git commit -m 'CRLF'"
  212. git.commit().setMessage("CRLF").call();
  213. // edit <FILE1>
  214. written = writeTrashFile(FILE1, "4\n4");
  215. assertEquals("4\n4", read(written));
  216. // "git add <FILE1>"
  217. git.add().addFilepattern(FILE1).call();
  218. // "git checkout -- <FILE1>
  219. git.checkout().addPath(FILE1).call();
  220. // "git status" => clean
  221. Status status = git.status().call();
  222. assertEquals(0, status.getAdded().size());
  223. assertEquals(0, status.getChanged().size());
  224. assertEquals(0, status.getConflicting().size());
  225. assertEquals(0, status.getMissing().size());
  226. assertEquals(0, status.getModified().size());
  227. assertEquals(0, status.getRemoved().size());
  228. assertEquals(0, status.getUntracked().size());
  229. }
  230. @Test
  231. public void testCheckoutRepository() throws Exception {
  232. CheckoutCommand co = git.checkout();
  233. File test = writeTrashFile(FILE1, "");
  234. File test2 = writeTrashFile(FILE2, "");
  235. co.setStartPoint("HEAD~2").setAllPaths(true).call();
  236. assertEquals("1", read(test));
  237. assertEquals("a", read(test2));
  238. }
  239. @Test(expected = JGitInternalException.class)
  240. public void testCheckoutOfConflictingFileShouldThrow()
  241. throws Exception {
  242. setupConflictingState();
  243. git.checkout().addPath(FILE1).call();
  244. }
  245. @Test
  246. public void testCheckoutOurs() throws Exception {
  247. setupConflictingState();
  248. git.checkout().setStage(Stage.OURS).addPath(FILE1).call();
  249. assertEquals("3", read(FILE1));
  250. assertStageOneToThree(FILE1);
  251. }
  252. @Test
  253. public void testCheckoutTheirs() throws Exception {
  254. setupConflictingState();
  255. git.checkout().setStage(Stage.THEIRS).addPath(FILE1).call();
  256. assertEquals("Conflicting", read(FILE1));
  257. assertStageOneToThree(FILE1);
  258. }
  259. @Test
  260. public void testCheckoutFileWithConflict() throws Exception {
  261. setupConflictingState();
  262. assertEquals('[' + FILE1 + ']',
  263. git.status().call().getConflicting().toString());
  264. git.checkout().setStartPoint(Constants.HEAD).addPath(FILE1).call();
  265. assertEquals("3", read(FILE1));
  266. assertTrue(git.status().call().isClean());
  267. }
  268. @Test
  269. public void testCheckoutOursWhenNoBase() throws Exception {
  270. String file = "added.txt";
  271. git.checkout().setCreateBranch(true).setName("side")
  272. .setStartPoint(initialCommit).call();
  273. writeTrashFile(file, "Added on side");
  274. git.add().addFilepattern(file).call();
  275. RevCommit side = git.commit().setMessage("Commit on side").call();
  276. git.checkout().setName("master").call();
  277. writeTrashFile(file, "Added on master");
  278. git.add().addFilepattern(file).call();
  279. git.commit().setMessage("Commit on master").call();
  280. git.merge().include(side).call();
  281. assertEquals(RepositoryState.MERGING, db.getRepositoryState());
  282. DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
  283. assertEquals("Expected add/add file to not have base stage",
  284. DirCacheEntry.STAGE_2, cache.getEntry(file).getStage());
  285. assertTrue(read(file).startsWith("<<<<<<< HEAD"));
  286. git.checkout().setStage(Stage.OURS).addPath(file).call();
  287. assertEquals("Added on master", read(file));
  288. cache = DirCache.read(db.getIndexFile(), db.getFS());
  289. assertEquals("Expected conflict stages to still exist after checkout",
  290. DirCacheEntry.STAGE_2, cache.getEntry(file).getStage());
  291. }
  292. @Test(expected = IllegalStateException.class)
  293. public void testStageNotPossibleWithBranch() throws Exception {
  294. git.checkout().setStage(Stage.OURS).setStartPoint("master").call();
  295. }
  296. private void setupConflictingState() throws Exception {
  297. git.checkout().setCreateBranch(true).setName("conflict")
  298. .setStartPoint(initialCommit).call();
  299. writeTrashFile(FILE1, "Conflicting");
  300. RevCommit conflict = git.commit().setAll(true)
  301. .setMessage("Conflicting change").call();
  302. git.checkout().setName("master").call();
  303. git.merge().include(conflict).call();
  304. assertEquals(RepositoryState.MERGING, db.getRepositoryState());
  305. assertStageOneToThree(FILE1);
  306. }
  307. private void assertStageOneToThree(String name) throws Exception {
  308. DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
  309. int i = cache.findEntry(name);
  310. DirCacheEntry stage1 = cache.getEntry(i);
  311. DirCacheEntry stage2 = cache.getEntry(i + 1);
  312. DirCacheEntry stage3 = cache.getEntry(i + 2);
  313. assertEquals(DirCacheEntry.STAGE_1, stage1.getStage());
  314. assertEquals(DirCacheEntry.STAGE_2, stage2.getStage());
  315. assertEquals(DirCacheEntry.STAGE_3, stage3.getStage());
  316. }
  317. }