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.

ResetCommandTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.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.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.io.PrintWriter;
  52. import org.eclipse.jgit.api.ResetCommand.ResetType;
  53. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  54. import org.eclipse.jgit.api.errors.JGitInternalException;
  55. import org.eclipse.jgit.api.errors.NoFilepatternException;
  56. import org.eclipse.jgit.api.errors.NoHeadException;
  57. import org.eclipse.jgit.api.errors.NoMessageException;
  58. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  59. import org.eclipse.jgit.dircache.DirCache;
  60. import org.eclipse.jgit.dircache.DirCacheEntry;
  61. import org.eclipse.jgit.errors.AmbiguousObjectException;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.RepositoryTestCase;
  65. import org.eclipse.jgit.revwalk.RevCommit;
  66. import org.eclipse.jgit.revwalk.RevWalk;
  67. import org.eclipse.jgit.treewalk.TreeWalk;
  68. import org.eclipse.jgit.util.FileUtils;
  69. import org.junit.Assert;
  70. import org.junit.Test;
  71. public class ResetCommandTest extends RepositoryTestCase {
  72. private Git git;
  73. private RevCommit initialCommit;
  74. private RevCommit secondCommit;
  75. private File indexFile;
  76. private File untrackedFile;
  77. private DirCacheEntry prestage;
  78. public void setupRepository() throws IOException, NoFilepatternException,
  79. NoHeadException, NoMessageException, ConcurrentRefUpdateException,
  80. JGitInternalException, WrongRepositoryStateException {
  81. // create initial commit
  82. git = new Git(db);
  83. initialCommit = git.commit().setMessage("initial commit").call();
  84. // create nested file
  85. File dir = new File(db.getWorkTree(), "dir");
  86. FileUtils.mkdir(dir);
  87. File nestedFile = new File(dir, "b.txt");
  88. FileUtils.createNewFile(nestedFile);
  89. PrintWriter nesterFileWriter = new PrintWriter(nestedFile);
  90. nesterFileWriter.print("content");
  91. nesterFileWriter.flush();
  92. // create file
  93. indexFile = new File(db.getWorkTree(), "a.txt");
  94. FileUtils.createNewFile(indexFile);
  95. PrintWriter writer = new PrintWriter(indexFile);
  96. writer.print("content");
  97. writer.flush();
  98. // add file and commit it
  99. git.add().addFilepattern("dir").addFilepattern("a.txt").call();
  100. secondCommit = git.commit().setMessage("adding a.txt and dir/b.txt")
  101. .call();
  102. prestage = DirCache.read(db.getIndexFile(), db.getFS()).getEntry(
  103. indexFile.getName());
  104. // modify file and add to index
  105. writer.print("new content");
  106. writer.close();
  107. nesterFileWriter.print("new content");
  108. nesterFileWriter.close();
  109. git.add().addFilepattern("a.txt").addFilepattern("dir").call();
  110. // create a file not added to the index
  111. untrackedFile = new File(db.getWorkTree(),
  112. "notAddedToIndex.txt");
  113. FileUtils.createNewFile(untrackedFile);
  114. PrintWriter writer2 = new PrintWriter(untrackedFile);
  115. writer2.print("content");
  116. writer2.close();
  117. }
  118. @Test
  119. public void testHardReset() throws JGitInternalException,
  120. AmbiguousObjectException, IOException, NoFilepatternException,
  121. NoHeadException, NoMessageException, ConcurrentRefUpdateException,
  122. WrongRepositoryStateException {
  123. setupRepository();
  124. ObjectId prevHead = db.resolve(Constants.HEAD);
  125. git.reset().setMode(ResetType.HARD).setRef(initialCommit.getName())
  126. .call();
  127. // check if HEAD points to initial commit now
  128. ObjectId head = db.resolve(Constants.HEAD);
  129. assertTrue(head.equals(initialCommit));
  130. // check if files were removed
  131. assertFalse(indexFile.exists());
  132. assertTrue(untrackedFile.exists());
  133. // fileInIndex must no longer be in HEAD and in the index
  134. String fileInIndexPath = indexFile.getAbsolutePath();
  135. assertFalse(inHead(fileInIndexPath));
  136. assertFalse(inIndex(indexFile.getName()));
  137. assertReflog(prevHead, head);
  138. }
  139. @Test
  140. public void testResetToNonexistingHEAD() throws JGitInternalException,
  141. AmbiguousObjectException, IOException {
  142. // create a file in the working tree of a fresh repo
  143. git = new Git(db);
  144. writeTrashFile("f", "content");
  145. try {
  146. git.reset().setRef(Constants.HEAD).call();
  147. fail("Expected JGitInternalException didn't occur");
  148. } catch (JGitInternalException e) {
  149. // got the expected exception
  150. }
  151. }
  152. @Test
  153. public void testSoftReset() throws JGitInternalException,
  154. AmbiguousObjectException, IOException, NoFilepatternException,
  155. NoHeadException, NoMessageException, ConcurrentRefUpdateException,
  156. WrongRepositoryStateException {
  157. setupRepository();
  158. ObjectId prevHead = db.resolve(Constants.HEAD);
  159. git.reset().setMode(ResetType.SOFT).setRef(initialCommit.getName())
  160. .call();
  161. // check if HEAD points to initial commit now
  162. ObjectId head = db.resolve(Constants.HEAD);
  163. assertTrue(head.equals(initialCommit));
  164. // check if files still exist
  165. assertTrue(untrackedFile.exists());
  166. assertTrue(indexFile.exists());
  167. // fileInIndex must no longer be in HEAD but has to be in the index
  168. String fileInIndexPath = indexFile.getAbsolutePath();
  169. assertFalse(inHead(fileInIndexPath));
  170. assertTrue(inIndex(indexFile.getName()));
  171. assertReflog(prevHead, head);
  172. }
  173. @Test
  174. public void testMixedReset() throws JGitInternalException,
  175. AmbiguousObjectException, IOException, NoFilepatternException,
  176. NoHeadException, NoMessageException, ConcurrentRefUpdateException,
  177. WrongRepositoryStateException {
  178. setupRepository();
  179. ObjectId prevHead = db.resolve(Constants.HEAD);
  180. git.reset().setMode(ResetType.MIXED).setRef(initialCommit.getName())
  181. .call();
  182. // check if HEAD points to initial commit now
  183. ObjectId head = db.resolve(Constants.HEAD);
  184. assertTrue(head.equals(initialCommit));
  185. // check if files still exist
  186. assertTrue(untrackedFile.exists());
  187. assertTrue(indexFile.exists());
  188. // fileInIndex must no longer be in HEAD and in the index
  189. String fileInIndexPath = indexFile.getAbsolutePath();
  190. assertFalse(inHead(fileInIndexPath));
  191. assertFalse(inIndex(indexFile.getName()));
  192. assertReflog(prevHead, head);
  193. }
  194. @Test
  195. public void testPathsReset() throws Exception {
  196. setupRepository();
  197. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  198. .getEntry(indexFile.getName());
  199. assertNotNull(preReset);
  200. git.add().addFilepattern(untrackedFile.getName()).call();
  201. // 'a.txt' has already been modified in setupRepository
  202. // 'notAddedToIndex.txt' has been added to repository
  203. git.reset().addPath(indexFile.getName())
  204. .addPath(untrackedFile.getName()).call();
  205. DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS())
  206. .getEntry(indexFile.getName());
  207. assertNotNull(postReset);
  208. Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId());
  209. Assert.assertEquals(prestage.getObjectId(), postReset.getObjectId());
  210. // check that HEAD hasn't moved
  211. ObjectId head = db.resolve(Constants.HEAD);
  212. assertTrue(head.equals(secondCommit));
  213. // check if files still exist
  214. assertTrue(untrackedFile.exists());
  215. assertTrue(indexFile.exists());
  216. assertTrue(inHead(indexFile.getName()));
  217. assertTrue(inIndex(indexFile.getName()));
  218. assertFalse(inIndex(untrackedFile.getName()));
  219. }
  220. @Test
  221. public void testPathsResetOnDirs() throws Exception {
  222. setupRepository();
  223. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  224. .getEntry("dir/b.txt");
  225. assertNotNull(preReset);
  226. git.add().addFilepattern(untrackedFile.getName()).call();
  227. // 'dir/b.txt' has already been modified in setupRepository
  228. git.reset().addPath("dir").call();
  229. DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS())
  230. .getEntry("dir/b.txt");
  231. assertNotNull(postReset);
  232. Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId());
  233. // check that HEAD hasn't moved
  234. ObjectId head = db.resolve(Constants.HEAD);
  235. assertTrue(head.equals(secondCommit));
  236. // check if files still exist
  237. assertTrue(untrackedFile.exists());
  238. assertTrue(inHead("dir/b.txt"));
  239. assertTrue(inIndex("dir/b.txt"));
  240. }
  241. @Test
  242. public void testPathsResetWithRef() throws Exception {
  243. setupRepository();
  244. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  245. .getEntry(indexFile.getName());
  246. assertNotNull(preReset);
  247. git.add().addFilepattern(untrackedFile.getName()).call();
  248. // 'a.txt' has already been modified in setupRepository
  249. // 'notAddedToIndex.txt' has been added to repository
  250. // reset to the inital commit
  251. git.reset().setRef(initialCommit.getName())
  252. .addPath(indexFile.getName())
  253. .addPath(untrackedFile.getName()).call();
  254. // check that HEAD hasn't moved
  255. ObjectId head = db.resolve(Constants.HEAD);
  256. assertTrue(head.equals(secondCommit));
  257. // check if files still exist
  258. assertTrue(untrackedFile.exists());
  259. assertTrue(indexFile.exists());
  260. assertTrue(inHead(indexFile.getName()));
  261. assertFalse(inIndex(indexFile.getName()));
  262. assertFalse(inIndex(untrackedFile.getName()));
  263. }
  264. private void assertReflog(ObjectId prevHead, ObjectId head)
  265. throws IOException {
  266. // Check the reflog for HEAD
  267. String actualHeadMessage = db.getReflogReader(Constants.HEAD)
  268. .getLastEntry().getComment();
  269. String expectedHeadMessage = head.getName() + ": updating HEAD";
  270. assertEquals(expectedHeadMessage, actualHeadMessage);
  271. assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
  272. .getLastEntry().getNewId().getName());
  273. assertEquals(prevHead.getName(), db.getReflogReader(Constants.HEAD)
  274. .getLastEntry().getOldId().getName());
  275. // The reflog for master contains the same as the one for HEAD
  276. String actualMasterMessage = db.getReflogReader("refs/heads/master")
  277. .getLastEntry().getComment();
  278. String expectedMasterMessage = head.getName() + ": updating HEAD"; // yes!
  279. assertEquals(expectedMasterMessage, actualMasterMessage);
  280. assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
  281. .getLastEntry().getNewId().getName());
  282. assertEquals(prevHead.getName(), db
  283. .getReflogReader("refs/heads/master").getLastEntry().getOldId()
  284. .getName());
  285. }
  286. /**
  287. * Checks if a file with the given path exists in the HEAD tree
  288. *
  289. * @param path
  290. * @return true if the file exists
  291. * @throws IOException
  292. */
  293. private boolean inHead(String path) throws IOException {
  294. ObjectId headId = db.resolve(Constants.HEAD);
  295. RevWalk rw = new RevWalk(db);
  296. TreeWalk tw = null;
  297. try {
  298. tw = TreeWalk.forPath(db, path, rw.parseTree(headId));
  299. return tw != null;
  300. } finally {
  301. rw.release();
  302. rw.dispose();
  303. if (tw != null)
  304. tw.release();
  305. }
  306. }
  307. /**
  308. * Checks if a file with the given path exists in the index
  309. *
  310. * @param path
  311. * @return true if the file exists
  312. * @throws IOException
  313. */
  314. private boolean inIndex(String path) throws IOException {
  315. DirCache dc = DirCache.read(db.getIndexFile(), db.getFS());
  316. return dc.getEntry(path) != null;
  317. }
  318. }