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.

StashCreateCommandTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (C) 2012, GitHub Inc.
  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.List;
  52. import org.eclipse.jgit.api.errors.UnmergedPathsException;
  53. import org.eclipse.jgit.diff.DiffEntry;
  54. import org.eclipse.jgit.junit.RepositoryTestCase;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.lib.PersonIdent;
  58. import org.eclipse.jgit.lib.Ref;
  59. import org.eclipse.jgit.lib.ReflogEntry;
  60. import org.eclipse.jgit.lib.ReflogReader;
  61. import org.eclipse.jgit.revwalk.RevCommit;
  62. import org.eclipse.jgit.revwalk.RevWalk;
  63. import org.eclipse.jgit.treewalk.TreeWalk;
  64. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  65. import org.eclipse.jgit.util.FileUtils;
  66. import org.junit.Before;
  67. import org.junit.Test;
  68. /**
  69. * Unit tests of {@link StashCreateCommand}
  70. */
  71. public class StashCreateCommandTest extends RepositoryTestCase {
  72. private RevCommit head;
  73. private Git git;
  74. private File committedFile;
  75. @Before
  76. public void setUp() throws Exception {
  77. super.setUp();
  78. git = Git.wrap(db);
  79. committedFile = writeTrashFile("file.txt", "content");
  80. git.add().addFilepattern("file.txt").call();
  81. head = git.commit().setMessage("add file").call();
  82. assertNotNull(head);
  83. writeTrashFile("untracked.txt", "content");
  84. }
  85. /**
  86. * Core validation to be performed on all stashed commits
  87. *
  88. * @param commit
  89. * @throws IOException
  90. */
  91. private void validateStashedCommit(final RevCommit commit)
  92. throws IOException {
  93. assertNotNull(commit);
  94. Ref stashRef = db.getRef(Constants.R_STASH);
  95. assertNotNull(stashRef);
  96. assertEquals(commit, stashRef.getObjectId());
  97. assertNotNull(commit.getAuthorIdent());
  98. assertEquals(commit.getAuthorIdent(), commit.getCommitterIdent());
  99. assertEquals(2, commit.getParentCount());
  100. // Load parents
  101. RevWalk walk = new RevWalk(db);
  102. try {
  103. for (RevCommit parent : commit.getParents())
  104. walk.parseBody(parent);
  105. } finally {
  106. walk.release();
  107. }
  108. assertEquals(1, commit.getParent(1).getParentCount());
  109. assertEquals(head, commit.getParent(1).getParent(0));
  110. assertFalse("Head tree matches stashed commit tree", commit.getTree()
  111. .equals(head.getTree()));
  112. assertEquals(head, commit.getParent(0));
  113. assertFalse(commit.getFullMessage().equals(
  114. commit.getParent(1).getFullMessage()));
  115. }
  116. private TreeWalk createTreeWalk() {
  117. TreeWalk walk = new TreeWalk(db);
  118. walk.setRecursive(true);
  119. walk.setFilter(TreeFilter.ANY_DIFF);
  120. return walk;
  121. }
  122. private List<DiffEntry> diffWorkingAgainstHead(final RevCommit commit)
  123. throws IOException {
  124. TreeWalk walk = createTreeWalk();
  125. try {
  126. walk.addTree(commit.getParent(0).getTree());
  127. walk.addTree(commit.getTree());
  128. return DiffEntry.scan(walk);
  129. } finally {
  130. walk.release();
  131. }
  132. }
  133. private List<DiffEntry> diffIndexAgainstHead(final RevCommit commit)
  134. throws IOException {
  135. TreeWalk walk = createTreeWalk();
  136. try {
  137. walk.addTree(commit.getParent(0).getTree());
  138. walk.addTree(commit.getParent(1).getTree());
  139. return DiffEntry.scan(walk);
  140. } finally {
  141. walk.release();
  142. }
  143. }
  144. private List<DiffEntry> diffIndexAgainstWorking(final RevCommit commit)
  145. throws IOException {
  146. TreeWalk walk = createTreeWalk();
  147. try {
  148. walk.addTree(commit.getParent(1).getTree());
  149. walk.addTree(commit.getTree());
  150. return DiffEntry.scan(walk);
  151. } finally {
  152. walk.release();
  153. }
  154. }
  155. @Test
  156. public void noLocalChanges() throws Exception {
  157. assertNull(git.stashCreate().call());
  158. }
  159. @Test
  160. public void workingDirectoryDelete() throws Exception {
  161. deleteTrashFile("file.txt");
  162. RevCommit stashed = git.stashCreate().call();
  163. assertNotNull(stashed);
  164. assertEquals("content", read(committedFile));
  165. validateStashedCommit(stashed);
  166. assertEquals(head.getTree(), stashed.getParent(1).getTree());
  167. List<DiffEntry> diffs = diffWorkingAgainstHead(stashed);
  168. assertEquals(1, diffs.size());
  169. assertEquals(DiffEntry.ChangeType.DELETE, diffs.get(0).getChangeType());
  170. assertEquals("file.txt", diffs.get(0).getOldPath());
  171. }
  172. @Test
  173. public void indexAdd() throws Exception {
  174. File addedFile = writeTrashFile("file2.txt", "content2");
  175. git.add().addFilepattern("file2.txt").call();
  176. RevCommit stashed = Git.wrap(db).stashCreate().call();
  177. assertNotNull(stashed);
  178. assertFalse(addedFile.exists());
  179. validateStashedCommit(stashed);
  180. assertEquals(stashed.getTree(), stashed.getParent(1).getTree());
  181. List<DiffEntry> diffs = diffWorkingAgainstHead(stashed);
  182. assertEquals(1, diffs.size());
  183. assertEquals(DiffEntry.ChangeType.ADD, diffs.get(0).getChangeType());
  184. assertEquals("file2.txt", diffs.get(0).getNewPath());
  185. }
  186. @Test
  187. public void newFileInIndexThenModifiedInWorkTree() throws Exception {
  188. writeTrashFile("file", "content");
  189. git.add().addFilepattern("file").call();
  190. writeTrashFile("file", "content2");
  191. RevCommit stashedWorkTree = Git.wrap(db).stashCreate().call();
  192. validateStashedCommit(stashedWorkTree);
  193. RevWalk walk = new RevWalk(db);
  194. RevCommit stashedIndex = stashedWorkTree.getParent(1);
  195. walk.parseBody(stashedIndex);
  196. walk.parseBody(stashedIndex.getTree());
  197. walk.parseBody(stashedIndex.getParent(0));
  198. List<DiffEntry> workTreeStashAgainstWorkTree = diffWorkingAgainstHead(stashedWorkTree);
  199. assertEquals(1, workTreeStashAgainstWorkTree.size());
  200. List<DiffEntry> workIndexAgainstWorkTree = diffIndexAgainstHead(stashedWorkTree);
  201. assertEquals(1, workIndexAgainstWorkTree.size());
  202. List<DiffEntry> indexStashAgainstWorkTree = diffIndexAgainstWorking(stashedWorkTree);
  203. assertEquals(1, indexStashAgainstWorkTree.size());
  204. }
  205. @Test
  206. public void indexDelete() throws Exception {
  207. git.rm().addFilepattern("file.txt").call();
  208. RevCommit stashed = Git.wrap(db).stashCreate().call();
  209. assertNotNull(stashed);
  210. assertEquals("content", read(committedFile));
  211. validateStashedCommit(stashed);
  212. assertEquals(stashed.getTree(), stashed.getParent(1).getTree());
  213. List<DiffEntry> diffs = diffWorkingAgainstHead(stashed);
  214. assertEquals(1, diffs.size());
  215. assertEquals(DiffEntry.ChangeType.DELETE, diffs.get(0).getChangeType());
  216. assertEquals("file.txt", diffs.get(0).getOldPath());
  217. }
  218. @Test
  219. public void workingDirectoryModify() throws Exception {
  220. writeTrashFile("file.txt", "content2");
  221. RevCommit stashed = Git.wrap(db).stashCreate().call();
  222. assertNotNull(stashed);
  223. assertEquals("content", read(committedFile));
  224. validateStashedCommit(stashed);
  225. assertEquals(head.getTree(), stashed.getParent(1).getTree());
  226. List<DiffEntry> diffs = diffWorkingAgainstHead(stashed);
  227. assertEquals(1, diffs.size());
  228. assertEquals(DiffEntry.ChangeType.MODIFY, diffs.get(0).getChangeType());
  229. assertEquals("file.txt", diffs.get(0).getNewPath());
  230. }
  231. @Test
  232. public void workingDirectoryModifyInSubfolder() throws Exception {
  233. String path = "d1/d2/f.txt";
  234. File subfolderFile = writeTrashFile(path, "content");
  235. git.add().addFilepattern(path).call();
  236. head = git.commit().setMessage("add file").call();
  237. writeTrashFile(path, "content2");
  238. RevCommit stashed = Git.wrap(db).stashCreate().call();
  239. assertNotNull(stashed);
  240. assertEquals("content", read(subfolderFile));
  241. validateStashedCommit(stashed);
  242. assertEquals(head.getTree(), stashed.getParent(1).getTree());
  243. List<DiffEntry> diffs = diffWorkingAgainstHead(stashed);
  244. assertEquals(1, diffs.size());
  245. assertEquals(DiffEntry.ChangeType.MODIFY, diffs.get(0).getChangeType());
  246. assertEquals(path, diffs.get(0).getNewPath());
  247. }
  248. @Test
  249. public void workingDirectoryModifyIndexChanged() throws Exception {
  250. writeTrashFile("file.txt", "content2");
  251. git.add().addFilepattern("file.txt").call();
  252. writeTrashFile("file.txt", "content3");
  253. RevCommit stashed = Git.wrap(db).stashCreate().call();
  254. assertNotNull(stashed);
  255. assertEquals("content", read(committedFile));
  256. validateStashedCommit(stashed);
  257. assertFalse(stashed.getTree().equals(stashed.getParent(1).getTree()));
  258. List<DiffEntry> workingDiffs = diffWorkingAgainstHead(stashed);
  259. assertEquals(1, workingDiffs.size());
  260. assertEquals(DiffEntry.ChangeType.MODIFY, workingDiffs.get(0)
  261. .getChangeType());
  262. assertEquals("file.txt", workingDiffs.get(0).getNewPath());
  263. List<DiffEntry> indexDiffs = diffIndexAgainstHead(stashed);
  264. assertEquals(1, indexDiffs.size());
  265. assertEquals(DiffEntry.ChangeType.MODIFY, indexDiffs.get(0)
  266. .getChangeType());
  267. assertEquals("file.txt", indexDiffs.get(0).getNewPath());
  268. assertEquals(workingDiffs.get(0).getOldId(), indexDiffs.get(0)
  269. .getOldId());
  270. assertFalse(workingDiffs.get(0).getNewId()
  271. .equals(indexDiffs.get(0).getNewId()));
  272. }
  273. @Test
  274. public void workingDirectoryCleanIndexModify() throws Exception {
  275. writeTrashFile("file.txt", "content2");
  276. git.add().addFilepattern("file.txt").call();
  277. writeTrashFile("file.txt", "content");
  278. RevCommit stashed = Git.wrap(db).stashCreate().call();
  279. assertNotNull(stashed);
  280. assertEquals("content", read(committedFile));
  281. validateStashedCommit(stashed);
  282. assertEquals(stashed.getParent(1).getTree(), stashed.getTree());
  283. List<DiffEntry> workingDiffs = diffWorkingAgainstHead(stashed);
  284. assertEquals(1, workingDiffs.size());
  285. assertEquals(DiffEntry.ChangeType.MODIFY, workingDiffs.get(0)
  286. .getChangeType());
  287. assertEquals("file.txt", workingDiffs.get(0).getNewPath());
  288. List<DiffEntry> indexDiffs = diffIndexAgainstHead(stashed);
  289. assertEquals(1, indexDiffs.size());
  290. assertEquals(DiffEntry.ChangeType.MODIFY, indexDiffs.get(0)
  291. .getChangeType());
  292. assertEquals("file.txt", indexDiffs.get(0).getNewPath());
  293. assertEquals(workingDiffs.get(0).getOldId(), indexDiffs.get(0)
  294. .getOldId());
  295. assertTrue(workingDiffs.get(0).getNewId()
  296. .equals(indexDiffs.get(0).getNewId()));
  297. }
  298. @Test
  299. public void workingDirectoryDeleteIndexAdd() throws Exception {
  300. String path = "file2.txt";
  301. File added = writeTrashFile(path, "content2");
  302. assertTrue(added.exists());
  303. git.add().addFilepattern(path).call();
  304. FileUtils.delete(added);
  305. assertFalse(added.exists());
  306. RevCommit stashed = Git.wrap(db).stashCreate().call();
  307. assertNotNull(stashed);
  308. assertFalse(added.exists());
  309. validateStashedCommit(stashed);
  310. assertEquals(stashed.getParent(1).getTree(), stashed.getTree());
  311. List<DiffEntry> workingDiffs = diffWorkingAgainstHead(stashed);
  312. assertEquals(1, workingDiffs.size());
  313. assertEquals(DiffEntry.ChangeType.ADD, workingDiffs.get(0)
  314. .getChangeType());
  315. assertEquals(path, workingDiffs.get(0).getNewPath());
  316. List<DiffEntry> indexDiffs = diffIndexAgainstHead(stashed);
  317. assertEquals(1, indexDiffs.size());
  318. assertEquals(DiffEntry.ChangeType.ADD, indexDiffs.get(0)
  319. .getChangeType());
  320. assertEquals(path, indexDiffs.get(0).getNewPath());
  321. assertEquals(workingDiffs.get(0).getOldId(), indexDiffs.get(0)
  322. .getOldId());
  323. assertTrue(workingDiffs.get(0).getNewId()
  324. .equals(indexDiffs.get(0).getNewId()));
  325. }
  326. @Test
  327. public void workingDirectoryDeleteIndexEdit() throws Exception {
  328. File edited = writeTrashFile("file.txt", "content2");
  329. git.add().addFilepattern("file.txt").call();
  330. FileUtils.delete(edited);
  331. assertFalse(edited.exists());
  332. RevCommit stashed = Git.wrap(db).stashCreate().call();
  333. assertNotNull(stashed);
  334. assertEquals("content", read(committedFile));
  335. validateStashedCommit(stashed);
  336. assertFalse(stashed.getTree().equals(stashed.getParent(1).getTree()));
  337. List<DiffEntry> workingDiffs = diffWorkingAgainstHead(stashed);
  338. assertEquals(1, workingDiffs.size());
  339. assertEquals(DiffEntry.ChangeType.DELETE, workingDiffs.get(0)
  340. .getChangeType());
  341. assertEquals("file.txt", workingDiffs.get(0).getOldPath());
  342. List<DiffEntry> indexDiffs = diffIndexAgainstHead(stashed);
  343. assertEquals(1, indexDiffs.size());
  344. assertEquals(DiffEntry.ChangeType.MODIFY, indexDiffs.get(0)
  345. .getChangeType());
  346. assertEquals("file.txt", indexDiffs.get(0).getNewPath());
  347. assertEquals(workingDiffs.get(0).getOldId(), indexDiffs.get(0)
  348. .getOldId());
  349. assertFalse(workingDiffs.get(0).getNewId()
  350. .equals(indexDiffs.get(0).getNewId()));
  351. }
  352. @Test
  353. public void multipleEdits() throws Exception {
  354. git.rm().addFilepattern("file.txt").call();
  355. File addedFile = writeTrashFile("file2.txt", "content2");
  356. git.add().addFilepattern("file2.txt").call();
  357. RevCommit stashed = Git.wrap(db).stashCreate().call();
  358. assertNotNull(stashed);
  359. assertFalse(addedFile.exists());
  360. validateStashedCommit(stashed);
  361. assertEquals(stashed.getTree(), stashed.getParent(1).getTree());
  362. List<DiffEntry> diffs = diffWorkingAgainstHead(stashed);
  363. assertEquals(2, diffs.size());
  364. assertEquals(DiffEntry.ChangeType.DELETE, diffs.get(0).getChangeType());
  365. assertEquals("file.txt", diffs.get(0).getOldPath());
  366. assertEquals(DiffEntry.ChangeType.ADD, diffs.get(1).getChangeType());
  367. assertEquals("file2.txt", diffs.get(1).getNewPath());
  368. }
  369. @Test
  370. public void refLogIncludesCommitMessage() throws Exception {
  371. PersonIdent who = new PersonIdent("user", "user@email.com");
  372. deleteTrashFile("file.txt");
  373. RevCommit stashed = git.stashCreate().setPerson(who).call();
  374. assertNotNull(stashed);
  375. assertEquals("content", read(committedFile));
  376. validateStashedCommit(stashed);
  377. ReflogReader reader = git.getRepository().getReflogReader(
  378. Constants.R_STASH);
  379. ReflogEntry entry = reader.getLastEntry();
  380. assertNotNull(entry);
  381. assertEquals(ObjectId.zeroId(), entry.getOldId());
  382. assertEquals(stashed, entry.getNewId());
  383. assertEquals(who, entry.getWho());
  384. assertEquals(stashed.getFullMessage(), entry.getComment());
  385. }
  386. @Test(expected = UnmergedPathsException.class)
  387. public void unmergedPathsShouldCauseException() throws Exception {
  388. commitFile("file.txt", "master", "base");
  389. RevCommit side = commitFile("file.txt", "side", "side");
  390. commitFile("file.txt", "master", "master");
  391. git.merge().include(side).call();
  392. git.stashCreate().call();
  393. }
  394. }