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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.dircache.DirCache;
  48. import org.eclipse.jgit.dircache.DirCacheEntry;
  49. import org.eclipse.jgit.errors.NoWorkTreeException;
  50. import org.eclipse.jgit.lib.ConfigConstants;
  51. import org.eclipse.jgit.lib.ObjectReader;
  52. import org.eclipse.jgit.lib.RepositoryTestCase;
  53. import org.eclipse.jgit.lib.StoredConfig;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.junit.Before;
  56. import org.junit.Test;
  57. /**
  58. * Unit tests of path-based uses of {@link CheckoutCommand}
  59. */
  60. public class PathCheckoutCommandTest extends RepositoryTestCase {
  61. private static final String FILE1 = "f/Test.txt";
  62. private static final String FILE2 = "Test2.txt";
  63. private static final String FILE3 = "Test3.txt";
  64. Git git;
  65. RevCommit initialCommit;
  66. RevCommit secondCommit;
  67. @Override
  68. @Before
  69. public void setUp() throws Exception {
  70. super.setUp();
  71. git = new Git(db);
  72. writeTrashFile(FILE1, "1");
  73. writeTrashFile(FILE2, "a");
  74. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  75. initialCommit = git.commit().setMessage("Initial commit").call();
  76. writeTrashFile(FILE1, "2");
  77. writeTrashFile(FILE2, "b");
  78. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  79. secondCommit = git.commit().setMessage("Second commit").call();
  80. writeTrashFile(FILE1, "3");
  81. writeTrashFile(FILE2, "c");
  82. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  83. git.commit().setMessage("Third commit").call();
  84. }
  85. @Test
  86. public void testUpdateWorkingDirectory() throws Exception {
  87. CheckoutCommand co = git.checkout();
  88. File written = writeTrashFile(FILE1, "");
  89. assertEquals("", read(written));
  90. co.addPath(FILE1).call();
  91. assertEquals("3", read(written));
  92. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  93. }
  94. @Test
  95. public void testCheckoutFirst() throws Exception {
  96. CheckoutCommand co = git.checkout();
  97. File written = writeTrashFile(FILE1, "");
  98. co.setStartPoint(initialCommit).addPath(FILE1).call();
  99. assertEquals("1", read(written));
  100. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  101. }
  102. @Test
  103. public void testCheckoutSecond() throws Exception {
  104. CheckoutCommand co = git.checkout();
  105. File written = writeTrashFile(FILE1, "");
  106. co.setStartPoint("HEAD~1").addPath(FILE1).call();
  107. assertEquals("2", read(written));
  108. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  109. }
  110. @Test
  111. public void testCheckoutMultiple() throws Exception {
  112. CheckoutCommand co = git.checkout();
  113. File test = writeTrashFile(FILE1, "");
  114. File test2 = writeTrashFile(FILE2, "");
  115. co.setStartPoint("HEAD~2").addPath(FILE1).addPath(FILE2).call();
  116. assertEquals("1", read(test));
  117. assertEquals("a", read(test2));
  118. }
  119. @Test
  120. public void testUpdateWorkingDirectoryFromIndex() throws Exception {
  121. CheckoutCommand co = git.checkout();
  122. File written = writeTrashFile(FILE1, "3a");
  123. git.add().addFilepattern(FILE1).call();
  124. written = writeTrashFile(FILE1, "");
  125. assertEquals("", read(written));
  126. co.addPath(FILE1).call();
  127. assertEquals("3a", read(written));
  128. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  129. }
  130. @Test
  131. public void testUpdateWorkingDirectoryFromHeadWithIndexChange()
  132. throws Exception {
  133. CheckoutCommand co = git.checkout();
  134. File written = writeTrashFile(FILE1, "3a");
  135. git.add().addFilepattern(FILE1).call();
  136. written = writeTrashFile(FILE1, "");
  137. assertEquals("", read(written));
  138. co.addPath(FILE1).setStartPoint("HEAD").call();
  139. assertEquals("3", read(written));
  140. assertEquals("c", read(new File(db.getWorkTree(), FILE2)));
  141. }
  142. @Test
  143. public void testUpdateWorkingDirectoryFromIndex2() throws Exception {
  144. CheckoutCommand co = git.checkout();
  145. fsTick(git.getRepository().getIndexFile());
  146. File written1 = writeTrashFile(FILE1, "3(modified)");
  147. File written2 = writeTrashFile(FILE2, "a(modified)");
  148. fsTick(written2);
  149. // make sure that we get unsmudged entries for FILE1 and FILE2
  150. writeTrashFile(FILE3, "foo");
  151. git.add().addFilepattern(FILE3).call();
  152. fsTick(git.getRepository().getIndexFile());
  153. git.add().addFilepattern(FILE1).addFilepattern(FILE2).call();
  154. fsTick(git.getRepository().getIndexFile());
  155. writeTrashFile(FILE1, "3(modified again)");
  156. writeTrashFile(FILE2, "a(modified again)");
  157. fsTick(written2);
  158. co.addPath(FILE1).setStartPoint(secondCommit).call();
  159. assertEquals("2", read(written1));
  160. assertEquals("a(modified again)", read(written2));
  161. validateIndex(git);
  162. }
  163. public static void validateIndex(Git git) throws NoWorkTreeException,
  164. IOException {
  165. DirCache dc = git.getRepository().lockDirCache();
  166. ObjectReader r = git.getRepository().getObjectDatabase().newReader();
  167. try {
  168. for (int i = 0; i < dc.getEntryCount(); ++i) {
  169. DirCacheEntry entry = dc.getEntry(i);
  170. if (entry.getLength() > 0)
  171. assertEquals(entry.getLength(), r.getObjectSize(
  172. entry.getObjectId(), ObjectReader.OBJ_ANY));
  173. }
  174. } finally {
  175. dc.unlock();
  176. r.release();
  177. }
  178. }
  179. public void testCheckoutMixedNewlines() throws Exception {
  180. // "git config core.autocrlf true"
  181. StoredConfig config = git.getRepository().getConfig();
  182. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  183. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  184. config.save();
  185. // edit <FILE1>
  186. File written = writeTrashFile(FILE1, "4\r\n4");
  187. assertEquals("4\r\n4", read(written));
  188. // "git add <FILE1>"
  189. git.add().addFilepattern(FILE1).call();
  190. // "git commit -m 'CRLF'"
  191. git.commit().setMessage("CRLF").call();
  192. // edit <FILE1>
  193. written = writeTrashFile(FILE1, "4\n4");
  194. assertEquals("4\n4", read(written));
  195. // "git add <FILE1>"
  196. git.add().addFilepattern(FILE1).call();
  197. // "git checkout -- <FILE1>
  198. git.checkout().addPath(FILE1).call();
  199. // "git status" => clean
  200. Status status = git.status().call();
  201. assertEquals(0, status.getAdded().size());
  202. assertEquals(0, status.getChanged().size());
  203. assertEquals(0, status.getConflicting().size());
  204. assertEquals(0, status.getMissing().size());
  205. assertEquals(0, status.getModified().size());
  206. assertEquals(0, status.getRemoved().size());
  207. assertEquals(0, status.getUntracked().size());
  208. }
  209. @Test
  210. public void testCheckoutRepository() throws Exception {
  211. CheckoutCommand co = git.checkout();
  212. File test = writeTrashFile(FILE1, "");
  213. File test2 = writeTrashFile(FILE2, "");
  214. co.setStartPoint("HEAD~2").setAllPaths(true).call();
  215. assertEquals("1", read(test));
  216. assertEquals("a", read(test2));
  217. }
  218. }