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

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