PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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