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

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