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

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