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.

RevertCommandTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (C) 2011, Robin Rosenberg
  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.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.util.Iterator;
  52. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  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.junit.RepositoryTestCase;
  58. import org.eclipse.jgit.lib.ConfigConstants;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.FileMode;
  61. import org.eclipse.jgit.lib.ReflogReader;
  62. import org.eclipse.jgit.lib.RepositoryState;
  63. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  64. import org.eclipse.jgit.revwalk.RevCommit;
  65. import org.junit.Test;
  66. /**
  67. * Test revert command
  68. */
  69. public class RevertCommandTest extends RepositoryTestCase {
  70. @Test
  71. public void testRevert() throws IOException, JGitInternalException,
  72. GitAPIException {
  73. Git git = new Git(db);
  74. writeTrashFile("a", "first line\nsec. line\nthird line\n");
  75. git.add().addFilepattern("a").call();
  76. git.commit().setMessage("create a").call();
  77. writeTrashFile("b", "content\n");
  78. git.add().addFilepattern("b").call();
  79. git.commit().setMessage("create b").call();
  80. writeTrashFile("a", "first line\nsec. line\nthird line\nfourth line\n");
  81. git.add().addFilepattern("a").call();
  82. git.commit().setMessage("enlarged a").call();
  83. writeTrashFile("a",
  84. "first line\nsecond line\nthird line\nfourth line\n");
  85. git.add().addFilepattern("a").call();
  86. RevCommit fixingA = git.commit().setMessage("fixed a").call();
  87. writeTrashFile("b", "first line\n");
  88. git.add().addFilepattern("b").call();
  89. git.commit().setMessage("fixed b").call();
  90. git.revert().include(fixingA).call();
  91. assertTrue(new File(db.getWorkTree(), "b").exists());
  92. checkFile(new File(db.getWorkTree(), "a"),
  93. "first line\nsec. line\nthird line\nfourth line\n");
  94. Iterator<RevCommit> history = git.log().call().iterator();
  95. RevCommit revertCommit = history.next();
  96. String expectedMessage = "Revert \"fixed a\"\n\n"
  97. + "This reverts commit " + fixingA.getId().getName() + ".\n";
  98. assertEquals(expectedMessage, revertCommit.getFullMessage());
  99. assertEquals("fixed b", history.next().getFullMessage());
  100. assertEquals("fixed a", history.next().getFullMessage());
  101. assertEquals("enlarged a", history.next().getFullMessage());
  102. assertEquals("create b", history.next().getFullMessage());
  103. assertEquals("create a", history.next().getFullMessage());
  104. assertFalse(history.hasNext());
  105. ReflogReader reader = db.getReflogReader(Constants.HEAD);
  106. assertTrue(reader.getLastEntry().getComment()
  107. .startsWith("revert: Revert \""));
  108. reader = db.getReflogReader(db.getBranch());
  109. assertTrue(reader.getLastEntry().getComment()
  110. .startsWith("revert: Revert \""));
  111. }
  112. @Test
  113. public void testRevertDirtyIndex() throws Exception {
  114. Git git = new Git(db);
  115. RevCommit sideCommit = prepareRevert(git);
  116. // modify and add file a
  117. writeTrashFile("a", "a(modified)");
  118. git.add().addFilepattern("a").call();
  119. // do not commit
  120. doRevertAndCheckResult(git, sideCommit,
  121. MergeFailureReason.DIRTY_INDEX);
  122. }
  123. @Test
  124. public void testRevertDirtyWorktree() throws Exception {
  125. Git git = new Git(db);
  126. RevCommit sideCommit = prepareRevert(git);
  127. // modify file a
  128. writeTrashFile("a", "a(modified)");
  129. // do not add and commit
  130. doRevertAndCheckResult(git, sideCommit,
  131. MergeFailureReason.DIRTY_WORKTREE);
  132. }
  133. @Test
  134. public void testRevertConflictResolution() throws Exception {
  135. Git git = new Git(db);
  136. RevCommit sideCommit = prepareRevert(git);
  137. RevertCommand revert = git.revert();
  138. RevCommit newHead = revert.include(sideCommit.getId()).call();
  139. assertNull(newHead);
  140. MergeResult result = revert.getFailingResult();
  141. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  142. assertTrue(new File(db.getDirectory(), Constants.MERGE_MSG).exists());
  143. assertEquals("Revert \"" + sideCommit.getShortMessage()
  144. + "\"\n\nThis reverts commit " + sideCommit.getId().getName()
  145. + ".\n\nConflicts:\n\ta\n",
  146. db.readMergeCommitMsg());
  147. assertTrue(new File(db.getDirectory(), Constants.REVERT_HEAD)
  148. .exists());
  149. assertEquals(sideCommit.getId(), db.readRevertHead());
  150. assertEquals(RepositoryState.REVERTING, db.getRepositoryState());
  151. // Resolve
  152. writeTrashFile("a", "a");
  153. git.add().addFilepattern("a").call();
  154. assertEquals(RepositoryState.REVERTING_RESOLVED,
  155. db.getRepositoryState());
  156. git.commit().setOnly("a").setMessage("resolve").call();
  157. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  158. }
  159. @Test
  160. public void testRevertkConflictReset() throws Exception {
  161. Git git = new Git(db);
  162. RevCommit sideCommit = prepareRevert(git);
  163. RevertCommand revert = git.revert();
  164. RevCommit newHead = revert.include(sideCommit.getId()).call();
  165. assertNull(newHead);
  166. MergeResult result = revert.getFailingResult();
  167. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  168. assertEquals(RepositoryState.REVERTING, db.getRepositoryState());
  169. assertTrue(new File(db.getDirectory(), Constants.REVERT_HEAD)
  170. .exists());
  171. git.reset().setMode(ResetType.MIXED).setRef("HEAD").call();
  172. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  173. assertFalse(new File(db.getDirectory(), Constants.REVERT_HEAD)
  174. .exists());
  175. }
  176. @Test
  177. public void testRevertOverExecutableChangeOnNonExectuableFileSystem()
  178. throws Exception {
  179. Git git = new Git(db);
  180. File file = writeTrashFile("test.txt", "a");
  181. assertNotNull(git.add().addFilepattern("test.txt").call());
  182. assertNotNull(git.commit().setMessage("commit1").call());
  183. assertNotNull(git.checkout().setCreateBranch(true).setName("a").call());
  184. writeTrashFile("test.txt", "b");
  185. assertNotNull(git.add().addFilepattern("test.txt").call());
  186. RevCommit commit2 = git.commit().setMessage("commit2").call();
  187. assertNotNull(commit2);
  188. assertNotNull(git.checkout().setName(Constants.MASTER).call());
  189. DirCache cache = db.lockDirCache();
  190. cache.getEntry("test.txt").setFileMode(FileMode.EXECUTABLE_FILE);
  191. cache.write();
  192. assertTrue(cache.commit());
  193. cache.unlock();
  194. assertNotNull(git.commit().setMessage("commit3").call());
  195. db.getFS().setExecute(file, false);
  196. git.getRepository()
  197. .getConfig()
  198. .setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  199. ConfigConstants.CONFIG_KEY_FILEMODE, false);
  200. RevertCommand revert = git.revert();
  201. RevCommit newHead = revert.include(commit2).call();
  202. assertNotNull(newHead);
  203. }
  204. @Test
  205. public void testRevertConflictMarkers() throws Exception {
  206. Git git = new Git(db);
  207. RevCommit sideCommit = prepareRevert(git);
  208. RevertCommand revert = git.revert();
  209. RevCommit newHead = revert.include(sideCommit.getId())
  210. .call();
  211. assertNull(newHead);
  212. MergeResult result = revert.getFailingResult();
  213. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  214. String expected = "<<<<<<< master\na(latest)\n=======\na\n>>>>>>> ca96c31 second master\n";
  215. checkFile(new File(db.getWorkTree(), "a"), expected);
  216. }
  217. @Test
  218. public void testRevertOurCommitName() throws Exception {
  219. Git git = new Git(db);
  220. RevCommit sideCommit = prepareRevert(git);
  221. RevertCommand revert = git.revert();
  222. RevCommit newHead = revert.include(sideCommit.getId())
  223. .setOurCommitName("custom name").call();
  224. assertNull(newHead);
  225. MergeResult result = revert.getFailingResult();
  226. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  227. String expected = "<<<<<<< custom name\na(latest)\n=======\na\n>>>>>>> ca96c31 second master\n";
  228. checkFile(new File(db.getWorkTree(), "a"), expected);
  229. }
  230. private RevCommit prepareRevert(final Git git) throws Exception {
  231. // create, add and commit file a
  232. writeTrashFile("a", "a");
  233. git.add().addFilepattern("a").call();
  234. git.commit().setMessage("first master").call();
  235. // First commit
  236. checkoutBranch("refs/heads/master");
  237. // modify, add and commit file a
  238. writeTrashFile("a", "a(previous)");
  239. git.add().addFilepattern("a").call();
  240. RevCommit oldCommit = git.commit().setMessage("second master").call();
  241. // modify, add and commit file a
  242. writeTrashFile("a", "a(latest)");
  243. git.add().addFilepattern("a").call();
  244. git.commit().setMessage("side").call();
  245. return oldCommit;
  246. }
  247. private void doRevertAndCheckResult(final Git git,
  248. final RevCommit sideCommit, final MergeFailureReason reason)
  249. throws Exception {
  250. // get current index state
  251. String indexState = indexState(CONTENT);
  252. // revert
  253. RevertCommand revert = git.revert();
  254. RevCommit resultCommit = revert.include(sideCommit.getId()).call();
  255. assertNull(resultCommit);
  256. MergeResult result = revert.getFailingResult();
  257. assertEquals(MergeStatus.FAILED, result.getMergeStatus());
  258. // staged file a causes DIRTY_INDEX
  259. assertEquals(1, result.getFailingPaths().size());
  260. assertEquals(reason, result.getFailingPaths().get("a"));
  261. assertEquals("a(modified)", read(new File(db.getWorkTree(), "a")));
  262. // index shall be unchanged
  263. assertEquals(indexState, indexState(CONTENT));
  264. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  265. if (reason == null) {
  266. ReflogReader reader = db.getReflogReader(Constants.HEAD);
  267. assertTrue(reader.getLastEntry().getComment()
  268. .startsWith("revert: "));
  269. reader = db.getReflogReader(db.getBranch());
  270. assertTrue(reader.getLastEntry().getComment()
  271. .startsWith("revert: "));
  272. }
  273. }
  274. }