PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java

https://bitbucket.org/cofarrell/jgit
Java | 309 lines | 212 code | 43 blank | 54 comment | 2 complexity | 89199e634f4eefea9063f14ea06f76e9 MD5 | raw file
  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 java.io.File;
  46. import java.io.IOException;
  47. import org.eclipse.jgit.api.CheckoutCommand.Stage;
  48. import org.eclipse.jgit.api.errors.JGitInternalException;
  49. import org.eclipse.jgit.dircache.DirCache;
  50. import org.eclipse.jgit.dircache.DirCacheEntry;
  51. import org.eclipse.jgit.errors.NoWorkTreeException;
  52. import org.eclipse.jgit.junit.RepositoryTestCase;
  53. import org.eclipse.jgit.lib.ConfigConstants;
  54. import org.eclipse.jgit.lib.ObjectReader;
  55. import org.eclipse.jgit.lib.RepositoryState;
  56. import org.eclipse.jgit.lib.StoredConfig;
  57. import org.eclipse.jgit.revwalk.RevCommit;
  58. import org.junit.Before;
  59. import org.junit.Test;
  60. /**
  61. * Unit tests of path-based uses of {@link CheckoutCommand}
  62. */
  63. public class PathCheckoutCommandTest extends RepositoryTestCase {
  64. private static final String FILE1 = "f/Test.txt";
  65. private static final String FILE2 = "Test2.txt";
  66. private static final String FILE3 = "Test3.txt";
  67. Git git;
  68. RevCommit initialCommit;
  69. RevCommit secondCommit;
  70. @Override
  71. @Before
  72. public void setUp() throws Exception {
  73. super.setUp();
  74. git = new Git(db);
  75. writeTrashFile(FILE1, "1");
  76. writeTrashFile(FILE2, "a");
  77. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  78. initialCommit = git.commit().setMessage("Initial commit").call();
  79. writeTrashFile(FILE1, "2");
  80. writeTrashFile(FILE2, "b");
  81. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  82. secondCommit = git.commit().setMessage("Second commit").call();
  83. writeTrashFile(FILE1, "3");
  84. writeTrashFile(FILE2, "c");
  85. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  86. git.commit().setMessage("Third commit").call();
  87. }
  88. @Test
  89. public void testUpdateWorkingDirectory() throws Exception {
  90. CheckoutCommand co = git.checkout();
  91. File written = writeTrashFile(FILE1, "");
  92. assertEquals("", read(written));
  93. co.addPath(FILE1).call();
  94. assertEquals("3", read(written));
  95. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  96. }
  97. @Test
  98. public void testCheckoutFirst() throws Exception {
  99. CheckoutCommand co = git.checkout();
  100. File written = writeTrashFile(FILE1, "");
  101. co.setStartPoint(initialCommit).addPath(FILE1).call();
  102. assertEquals("1", read(written));
  103. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  104. }
  105. @Test
  106. public void testCheckoutSecond() throws Exception {
  107. CheckoutCommand co = git.checkout();
  108. File written = writeTrashFile(FILE1, "");
  109. co.setStartPoint("HEAD~1").addPath(FILE1).call();
  110. assertEquals("2", read(written));
  111. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  112. }
  113. @Test
  114. public void testCheckoutMultiple() throws Exception {
  115. CheckoutCommand co = git.checkout();
  116. File test = writeTrashFile(FILE1, "");
  117. File test2 = writeTrashFile(FILE2, "");
  118. co.setStartPoint("HEAD~2").addPath(FILE1).addPath(FILE2).call();
  119. assertEquals("1", read(test));
  120. assertEquals("a", read(test2));
  121. }
  122. @Test
  123. public void testUpdateWorkingDirectoryFromIndex() throws Exception {
  124. CheckoutCommand co = git.checkout();
  125. File written = writeTrashFile(FILE1, "3a");
  126. git.add().addFilepattern(FILE1).call();
  127. written = writeTrashFile(FILE1, "");
  128. assertEquals("", read(written));
  129. co.addPath(FILE1).call();
  130. assertEquals("3a", read(written));
  131. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  132. }
  133. @Test
  134. public void testUpdateWorkingDirectoryFromHeadWithIndexChange()
  135. throws Exception {
  136. CheckoutCommand co = git.checkout();
  137. File written = writeTrashFile(FILE1, "3a");
  138. git.add().addFilepattern(FILE1).call();
  139. written = writeTrashFile(FILE1, "");
  140. assertEquals("", read(written));
  141. co.addPath(FILE1).setStartPoint("HEAD").call();
  142. assertEquals("3", read(written));
  143. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  144. }
  145. @Test
  146. public void testUpdateWorkingDirectoryFromIndex2() throws Exception {
  147. CheckoutCommand co = git.checkout();
  148. fsTick(git.getRepository().getIndexFile());
  149. File written1 = writeTrashFile(FILE1, "3(modified)");
  150. File written2 = writeTrashFile(FILE2, "a(modified)");
  151. fsTick(written2);
  152. // make sure that we get unsmudged entries for FILE1 and FILE2
  153. writeTrashFile(FILE3, "foo");
  154. git.add().addFilepattern(FILE3).call();
  155. fsTick(git.getRepository().getIndexFile());
  156. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  157. fsTick(git.getRepository().getIndexFile());
  158. writeTrashFile(FILE1, "3(modified again)");
  159. writeTrashFile(FILE2, "a(modified again)");
  160. fsTick(written2);
  161. co.addPath(FILE1).setStartPoint(secondCommit).call();
  162. assertEquals("2", read(written1));
  163. assertEquals("a(modified again)", read(written2));
  164. validateIndex(git);
  165. }
  166. public static void validateIndex(Git git) throws NoWorkTreeException,
  167. IOException {
  168. DirCache dc = git.getRepository().lockDirCache();
  169. ObjectReader r = git.getRepository().getObjectDatabase().newReader();
  170. try {
  171. for (int i = 0; i < dc.getEntryCount(); ++i) {
  172. DirCacheEntry entry = dc.getEntry(i);
  173. if (entry.getLength() > 0)
  174. assertEquals(entry.getLength(), r.getObjectSize(
  175. entry.getObjectId(), ObjectReader.OBJ_ANY));
  176. }
  177. } finally {
  178. dc.unlock();
  179. r.release();
  180. }
  181. }
  182. public void testCheckoutMixedNewlines() throws Exception {
  183. // "git config core.autocrlf true"
  184. StoredConfig config = git.getRepository().getConfig();
  185. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  186. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  187. config.save();
  188. // edit <FILE1>
  189. File written = writeTrashFile(FILE1, "4\r\n4");
  190. assertEquals("4\r\n4", read(written));
  191. // "git add <FILE1>"
  192. git.add().addFilepattern(FILE1).call();
  193. // "git commit -m 'CRLF'"
  194. git.commit().setMessage("CRLF").call();
  195. // edit <FILE1>
  196. written = writeTrashFile(FILE1, "4\n4");
  197. assertEquals("4\n4", read(written));
  198. // "git add <FILE1>"
  199. git.add().addFilepattern(FILE1).call();
  200. // "git checkout -- <FILE1>
  201. git.checkout().addPath(FILE1).call();
  202. // "git status" => clean
  203. Status status = git.status().call();
  204. assertEquals(0, status.getAdded().size());
  205. assertEquals(0, status.getChanged().size());
  206. assertEquals(0, status.getConflicting().size());
  207. assertEquals(0, status.getMissing().size());
  208. assertEquals(0, status.getModified().size());
  209. assertEquals(0, status.getRemoved().size());
  210. assertEquals(0, status.getUntracked().size());
  211. }
  212. @Test
  213. public void testCheckoutRepository() throws Exception {
  214. CheckoutCommand co = git.checkout();
  215. File test = writeTrashFile(FILE1, "");
  216. File test2 = writeTrashFile(FILE2, "");
  217. co.setStartPoint("HEAD~2").setAllPaths(true).call();
  218. assertEquals("1", read(test));
  219. assertEquals("a", read(test2));
  220. }
  221. @Test(expected = JGitInternalException.class)
  222. public void testCheckoutOfConflictingFileShouldThrow()
  223. throws Exception {
  224. setupConflictingState();
  225. git.checkout().addPath(FILE1).call();
  226. }
  227. @Test
  228. public void testCheckoutOurs() throws Exception {
  229. setupConflictingState();
  230. git.checkout().setStage(Stage.OURS).addPath(FILE1).call();
  231. assertEquals("3", read(FILE1));
  232. assertStageOneToThree(FILE1);
  233. }
  234. @Test
  235. public void testCheckoutTheirs() throws Exception {
  236. setupConflictingState();
  237. git.checkout().setStage(Stage.THEIRS).addPath(FILE1).call();
  238. assertEquals("Conflicting", read(FILE1));
  239. assertStageOneToThree(FILE1);
  240. }
  241. @Test(expected = IllegalStateException.class)
  242. public void testStageNotPossibleWithBranch() throws Exception {
  243. git.checkout().setStage(Stage.OURS).setStartPoint("master").call();
  244. }
  245. private void setupConflictingState() throws Exception {
  246. git.checkout().setCreateBranch(true).setName("conflict")
  247. .setStartPoint(initialCommit).call();
  248. writeTrashFile(FILE1, "Conflicting");
  249. RevCommit conflict = git.commit().setAll(true)
  250. .setMessage("Conflicting change").call();
  251. git.checkout().setName("master").call();
  252. git.merge().include(conflict).call();
  253. assertEquals(RepositoryState.MERGING, db.getRepositoryState());
  254. assertStageOneToThree(FILE1);
  255. }
  256. private void assertStageOneToThree(String name) throws Exception {
  257. DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
  258. int i = cache.findEntry(name);
  259. DirCacheEntry stage1 = cache.getEntry(i);
  260. DirCacheEntry stage2 = cache.getEntry(i + 1);
  261. DirCacheEntry stage3 = cache.getEntry(i + 2);
  262. assertEquals(DirCacheEntry.STAGE_1, stage1.getStage());
  263. assertEquals(DirCacheEntry.STAGE_2, stage2.getStage());
  264. assertEquals(DirCacheEntry.STAGE_3, stage3.getStage());
  265. }
  266. }