PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/jgit-2.0.0.201206130900-r/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java

#
Java | 420 lines | 270 code | 63 blank | 87 comment | 4 complexity | 68889494f902da56da9650049ef5cff3 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. assertEquals(prevHead, db.readOrigHead());
  133. }
  134. @Test
  135. public void testResetToNonexistingHEAD() throws JGitInternalException,
  136. AmbiguousObjectException, IOException, GitAPIException {
  137. // create a file in the working tree of a fresh repo
  138. git = new Git(db);
  139. writeTrashFile("f", "content");
  140. try {
  141. git.reset().setRef(Constants.HEAD).call();
  142. fail("Expected JGitInternalException didn't occur");
  143. } catch (JGitInternalException e) {
  144. // got the expected exception
  145. }
  146. }
  147. @Test
  148. public void testSoftReset() throws JGitInternalException,
  149. AmbiguousObjectException, IOException, GitAPIException {
  150. setupRepository();
  151. ObjectId prevHead = db.resolve(Constants.HEAD);
  152. git.reset().setMode(ResetType.SOFT).setRef(initialCommit.getName())
  153. .call();
  154. // check if HEAD points to initial commit now
  155. ObjectId head = db.resolve(Constants.HEAD);
  156. assertTrue(head.equals(initialCommit));
  157. // check if files still exist
  158. assertTrue(untrackedFile.exists());
  159. assertTrue(indexFile.exists());
  160. // fileInIndex must no longer be in HEAD but has to be in the index
  161. String fileInIndexPath = indexFile.getAbsolutePath();
  162. assertFalse(inHead(fileInIndexPath));
  163. assertTrue(inIndex(indexFile.getName()));
  164. assertReflog(prevHead, head);
  165. assertEquals(prevHead, db.readOrigHead());
  166. }
  167. @Test
  168. public void testMixedReset() throws JGitInternalException,
  169. AmbiguousObjectException, IOException, GitAPIException {
  170. setupRepository();
  171. ObjectId prevHead = db.resolve(Constants.HEAD);
  172. git.reset().setMode(ResetType.MIXED).setRef(initialCommit.getName())
  173. .call();
  174. // check if HEAD points to initial commit now
  175. ObjectId head = db.resolve(Constants.HEAD);
  176. assertTrue(head.equals(initialCommit));
  177. // check if files still exist
  178. assertTrue(untrackedFile.exists());
  179. assertTrue(indexFile.exists());
  180. // fileInIndex must no longer be in HEAD and in the index
  181. String fileInIndexPath = indexFile.getAbsolutePath();
  182. assertFalse(inHead(fileInIndexPath));
  183. assertFalse(inIndex(indexFile.getName()));
  184. assertReflog(prevHead, head);
  185. assertEquals(prevHead, db.readOrigHead());
  186. }
  187. @Test
  188. public void testMixedResetRetainsSizeAndModifiedTime() throws Exception {
  189. git = new Git(db);
  190. writeTrashFile("a.txt", "a").setLastModified(
  191. System.currentTimeMillis() - 60 * 1000);
  192. assertNotNull(git.add().addFilepattern("a.txt").call());
  193. assertNotNull(git.commit().setMessage("a commit").call());
  194. writeTrashFile("b.txt", "b").setLastModified(
  195. System.currentTimeMillis() - 60 * 1000);
  196. assertNotNull(git.add().addFilepattern("b.txt").call());
  197. RevCommit commit2 = git.commit().setMessage("b commit").call();
  198. assertNotNull(commit2);
  199. DirCache cache = db.readDirCache();
  200. DirCacheEntry aEntry = cache.getEntry("a.txt");
  201. assertNotNull(aEntry);
  202. assertTrue(aEntry.getLength() > 0);
  203. assertTrue(aEntry.getLastModified() > 0);
  204. DirCacheEntry bEntry = cache.getEntry("b.txt");
  205. assertNotNull(bEntry);
  206. assertTrue(bEntry.getLength() > 0);
  207. assertTrue(bEntry.getLastModified() > 0);
  208. git.reset().setMode(ResetType.MIXED).setRef(commit2.getName()).call();
  209. cache = db.readDirCache();
  210. DirCacheEntry mixedAEntry = cache.getEntry("a.txt");
  211. assertNotNull(mixedAEntry);
  212. assertEquals(aEntry.getLastModified(), mixedAEntry.getLastModified());
  213. assertEquals(aEntry.getLastModified(), mixedAEntry.getLastModified());
  214. DirCacheEntry mixedBEntry = cache.getEntry("b.txt");
  215. assertNotNull(mixedBEntry);
  216. assertEquals(bEntry.getLastModified(), mixedBEntry.getLastModified());
  217. assertEquals(bEntry.getLastModified(), mixedBEntry.getLastModified());
  218. }
  219. @Test
  220. public void testPathsReset() throws Exception {
  221. setupRepository();
  222. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  223. .getEntry(indexFile.getName());
  224. assertNotNull(preReset);
  225. git.add().addFilepattern(untrackedFile.getName()).call();
  226. // 'a.txt' has already been modified in setupRepository
  227. // 'notAddedToIndex.txt' has been added to repository
  228. git.reset().addPath(indexFile.getName())
  229. .addPath(untrackedFile.getName()).call();
  230. DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS())
  231. .getEntry(indexFile.getName());
  232. assertNotNull(postReset);
  233. Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId());
  234. Assert.assertEquals(prestage.getObjectId(), postReset.getObjectId());
  235. // check that HEAD hasn't moved
  236. ObjectId head = db.resolve(Constants.HEAD);
  237. assertTrue(head.equals(secondCommit));
  238. // check if files still exist
  239. assertTrue(untrackedFile.exists());
  240. assertTrue(indexFile.exists());
  241. assertTrue(inHead(indexFile.getName()));
  242. assertTrue(inIndex(indexFile.getName()));
  243. assertFalse(inIndex(untrackedFile.getName()));
  244. }
  245. @Test
  246. public void testPathsResetOnDirs() throws Exception {
  247. setupRepository();
  248. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  249. .getEntry("dir/b.txt");
  250. assertNotNull(preReset);
  251. git.add().addFilepattern(untrackedFile.getName()).call();
  252. // 'dir/b.txt' has already been modified in setupRepository
  253. git.reset().addPath("dir").call();
  254. DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS())
  255. .getEntry("dir/b.txt");
  256. assertNotNull(postReset);
  257. Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId());
  258. // check that HEAD hasn't moved
  259. ObjectId head = db.resolve(Constants.HEAD);
  260. assertTrue(head.equals(secondCommit));
  261. // check if files still exist
  262. assertTrue(untrackedFile.exists());
  263. assertTrue(inHead("dir/b.txt"));
  264. assertTrue(inIndex("dir/b.txt"));
  265. }
  266. @Test
  267. public void testPathsResetWithRef() throws Exception {
  268. setupRepository();
  269. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  270. .getEntry(indexFile.getName());
  271. assertNotNull(preReset);
  272. git.add().addFilepattern(untrackedFile.getName()).call();
  273. // 'a.txt' has already been modified in setupRepository
  274. // 'notAddedToIndex.txt' has been added to repository
  275. // reset to the inital commit
  276. git.reset().setRef(initialCommit.getName())
  277. .addPath(indexFile.getName())
  278. .addPath(untrackedFile.getName()).call();
  279. // check that HEAD hasn't moved
  280. ObjectId head = db.resolve(Constants.HEAD);
  281. assertTrue(head.equals(secondCommit));
  282. // check if files still exist
  283. assertTrue(untrackedFile.exists());
  284. assertTrue(indexFile.exists());
  285. assertTrue(inHead(indexFile.getName()));
  286. assertFalse(inIndex(indexFile.getName()));
  287. assertFalse(inIndex(untrackedFile.getName()));
  288. }
  289. @Test
  290. public void testHardResetOnTag() throws Exception {
  291. setupRepository();
  292. String tagName = "initialtag";
  293. git.tag().setName(tagName).setObjectId(secondCommit)
  294. .setMessage("message").call();
  295. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  296. .getEntry(indexFile.getName());
  297. assertNotNull(preReset);
  298. git.add().addFilepattern(untrackedFile.getName()).call();
  299. git.reset().setRef(tagName).setMode(HARD).call();
  300. ObjectId head = db.resolve(Constants.HEAD);
  301. assertTrue(head.equals(secondCommit));
  302. }
  303. private void assertReflog(ObjectId prevHead, ObjectId head)
  304. throws IOException {
  305. // Check the reflog for HEAD
  306. String actualHeadMessage = db.getReflogReader(Constants.HEAD)
  307. .getLastEntry().getComment();
  308. String expectedHeadMessage = head.getName() + ": updating HEAD";
  309. assertEquals(expectedHeadMessage, actualHeadMessage);
  310. assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
  311. .getLastEntry().getNewId().getName());
  312. assertEquals(prevHead.getName(), db.getReflogReader(Constants.HEAD)
  313. .getLastEntry().getOldId().getName());
  314. // The reflog for master contains the same as the one for HEAD
  315. String actualMasterMessage = db.getReflogReader("refs/heads/master")
  316. .getLastEntry().getComment();
  317. String expectedMasterMessage = head.getName() + ": updating HEAD"; // yes!
  318. assertEquals(expectedMasterMessage, actualMasterMessage);
  319. assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
  320. .getLastEntry().getNewId().getName());
  321. assertEquals(prevHead.getName(), db
  322. .getReflogReader("refs/heads/master").getLastEntry().getOldId()
  323. .getName());
  324. }
  325. /**
  326. * Checks if a file with the given path exists in the HEAD tree
  327. *
  328. * @param path
  329. * @return true if the file exists
  330. * @throws IOException
  331. */
  332. private boolean inHead(String path) throws IOException {
  333. ObjectId headId = db.resolve(Constants.HEAD);
  334. RevWalk rw = new RevWalk(db);
  335. TreeWalk tw = null;
  336. try {
  337. tw = TreeWalk.forPath(db, path, rw.parseTree(headId));
  338. return tw != null;
  339. } finally {
  340. rw.release();
  341. rw.dispose();
  342. if (tw != null)
  343. tw.release();
  344. }
  345. }
  346. /**
  347. * Checks if a file with the given path exists in the index
  348. *
  349. * @param path
  350. * @return true if the file exists
  351. * @throws IOException
  352. */
  353. private boolean inIndex(String path) throws IOException {
  354. DirCache dc = DirCache.read(db.getIndexFile(), db.getFS());
  355. return dc.getEntry(path) != null;
  356. }
  357. }