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

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