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.

StashDropCommandTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.assertNotNull;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.util.List;
  51. import org.eclipse.jgit.api.errors.JGitInternalException;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.Ref;
  55. import org.eclipse.jgit.lib.RepositoryTestCase;
  56. import org.eclipse.jgit.revwalk.RevCommit;
  57. import org.eclipse.jgit.storage.file.ReflogEntry;
  58. import org.eclipse.jgit.storage.file.ReflogReader;
  59. import org.junit.Before;
  60. import org.junit.Test;
  61. /**
  62. * Unit tests of {@link StashCreateCommand}
  63. */
  64. public class StashDropCommandTest extends RepositoryTestCase {
  65. private RevCommit head;
  66. private Git git;
  67. private File committedFile;
  68. @Before
  69. public void setUp() throws Exception {
  70. super.setUp();
  71. git = Git.wrap(db);
  72. committedFile = writeTrashFile("file.txt", "content");
  73. git.add().addFilepattern("file.txt").call();
  74. head = git.commit().setMessage("add file").call();
  75. assertNotNull(head);
  76. }
  77. @Test(expected = IllegalArgumentException.class)
  78. public void dropNegativeRef() {
  79. git.stashDrop().setStashRef(-1);
  80. }
  81. @Test
  82. public void dropWithNoStashedCommits() throws Exception {
  83. assertNull(git.stashDrop().call());
  84. }
  85. @Test
  86. public void dropWithInvalidLogIndex() throws Exception {
  87. write(committedFile, "content2");
  88. Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
  89. assertNull(stashRef);
  90. RevCommit stashed = git.stashCreate().call();
  91. assertNotNull(stashed);
  92. stashRef = git.getRepository().getRef(Constants.R_STASH);
  93. assertEquals(stashed, git.getRepository().getRef(Constants.R_STASH)
  94. .getObjectId());
  95. try {
  96. assertNull(git.stashDrop().setStashRef(100).call());
  97. fail("Exception not thrown");
  98. } catch (JGitInternalException e) {
  99. assertNotNull(e.getMessage());
  100. assertTrue(e.getMessage().length() > 0);
  101. }
  102. }
  103. @Test
  104. public void dropSingleStashedCommit() throws Exception {
  105. write(committedFile, "content2");
  106. Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
  107. assertNull(stashRef);
  108. RevCommit stashed = git.stashCreate().call();
  109. assertNotNull(stashed);
  110. stashRef = git.getRepository().getRef(Constants.R_STASH);
  111. assertEquals(stashed, git.getRepository().getRef(Constants.R_STASH)
  112. .getObjectId());
  113. assertNull(git.stashDrop().call());
  114. stashRef = git.getRepository().getRef(Constants.R_STASH);
  115. assertNull(stashRef);
  116. ReflogReader reader = new ReflogReader(git.getRepository(),
  117. Constants.R_STASH);
  118. assertTrue(reader.getReverseEntries().isEmpty());
  119. }
  120. @Test
  121. public void dropAll() throws Exception {
  122. write(committedFile, "content2");
  123. Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
  124. assertNull(stashRef);
  125. RevCommit firstStash = git.stashCreate().call();
  126. assertNotNull(firstStash);
  127. stashRef = git.getRepository().getRef(Constants.R_STASH);
  128. assertNotNull(stashRef);
  129. assertEquals(firstStash, git.getRepository().getRef(Constants.R_STASH)
  130. .getObjectId());
  131. write(committedFile, "content3");
  132. RevCommit secondStash = git.stashCreate().call();
  133. assertNotNull(secondStash);
  134. stashRef = git.getRepository().getRef(Constants.R_STASH);
  135. assertNotNull(stashRef);
  136. assertEquals(secondStash, git.getRepository().getRef(Constants.R_STASH)
  137. .getObjectId());
  138. assertNull(git.stashDrop().setAll(true).call());
  139. assertNull(git.getRepository().getRef(Constants.R_STASH));
  140. ReflogReader reader = new ReflogReader(git.getRepository(),
  141. Constants.R_STASH);
  142. assertTrue(reader.getReverseEntries().isEmpty());
  143. }
  144. @Test
  145. public void dropFirstStashedCommit() throws Exception {
  146. write(committedFile, "content2");
  147. Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
  148. assertNull(stashRef);
  149. RevCommit firstStash = git.stashCreate().call();
  150. assertNotNull(firstStash);
  151. stashRef = git.getRepository().getRef(Constants.R_STASH);
  152. assertNotNull(stashRef);
  153. assertEquals(firstStash, git.getRepository().getRef(Constants.R_STASH)
  154. .getObjectId());
  155. write(committedFile, "content3");
  156. RevCommit secondStash = git.stashCreate().call();
  157. assertNotNull(secondStash);
  158. stashRef = git.getRepository().getRef(Constants.R_STASH);
  159. assertNotNull(stashRef);
  160. assertEquals(secondStash, git.getRepository().getRef(Constants.R_STASH)
  161. .getObjectId());
  162. assertEquals(firstStash, git.stashDrop().call());
  163. stashRef = git.getRepository().getRef(Constants.R_STASH);
  164. assertNotNull(stashRef);
  165. assertEquals(firstStash, stashRef.getObjectId());
  166. ReflogReader reader = new ReflogReader(git.getRepository(),
  167. Constants.R_STASH);
  168. List<ReflogEntry> entries = reader.getReverseEntries();
  169. assertEquals(1, entries.size());
  170. assertEquals(ObjectId.zeroId(), entries.get(0).getOldId());
  171. assertEquals(firstStash, entries.get(0).getNewId());
  172. assertTrue(entries.get(0).getComment().length() > 0);
  173. }
  174. @Test
  175. public void dropMiddleStashCommit() throws Exception {
  176. write(committedFile, "content2");
  177. Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
  178. assertNull(stashRef);
  179. RevCommit firstStash = git.stashCreate().call();
  180. assertNotNull(firstStash);
  181. stashRef = git.getRepository().getRef(Constants.R_STASH);
  182. assertNotNull(stashRef);
  183. assertEquals(firstStash, git.getRepository().getRef(Constants.R_STASH)
  184. .getObjectId());
  185. write(committedFile, "content3");
  186. RevCommit secondStash = git.stashCreate().call();
  187. assertNotNull(secondStash);
  188. stashRef = git.getRepository().getRef(Constants.R_STASH);
  189. assertNotNull(stashRef);
  190. assertEquals(secondStash, git.getRepository().getRef(Constants.R_STASH)
  191. .getObjectId());
  192. write(committedFile, "content4");
  193. RevCommit thirdStash = git.stashCreate().call();
  194. assertNotNull(thirdStash);
  195. stashRef = git.getRepository().getRef(Constants.R_STASH);
  196. assertNotNull(stashRef);
  197. assertEquals(thirdStash, git.getRepository().getRef(Constants.R_STASH)
  198. .getObjectId());
  199. assertEquals(thirdStash, git.stashDrop().setStashRef(1).call());
  200. stashRef = git.getRepository().getRef(Constants.R_STASH);
  201. assertNotNull(stashRef);
  202. assertEquals(thirdStash, stashRef.getObjectId());
  203. ReflogReader reader = new ReflogReader(git.getRepository(),
  204. Constants.R_STASH);
  205. List<ReflogEntry> entries = reader.getReverseEntries();
  206. assertEquals(2, entries.size());
  207. assertEquals(ObjectId.zeroId(), entries.get(1).getOldId());
  208. assertEquals(firstStash, entries.get(1).getNewId());
  209. assertTrue(entries.get(1).getComment().length() > 0);
  210. assertEquals(entries.get(0).getOldId(), entries.get(1).getNewId());
  211. assertEquals(thirdStash, entries.get(0).getNewId());
  212. assertTrue(entries.get(0).getComment().length() > 0);
  213. }
  214. @Test
  215. public void dropBoundaryStashedCommits() throws Exception {
  216. write(committedFile, "content2");
  217. Ref stashRef = git.getRepository().getRef(Constants.R_STASH);
  218. assertNull(stashRef);
  219. RevCommit firstStash = git.stashCreate().call();
  220. assertNotNull(firstStash);
  221. stashRef = git.getRepository().getRef(Constants.R_STASH);
  222. assertNotNull(stashRef);
  223. assertEquals(firstStash, git.getRepository().getRef(Constants.R_STASH)
  224. .getObjectId());
  225. write(committedFile, "content3");
  226. RevCommit secondStash = git.stashCreate().call();
  227. assertNotNull(secondStash);
  228. stashRef = git.getRepository().getRef(Constants.R_STASH);
  229. assertNotNull(stashRef);
  230. assertEquals(secondStash, git.getRepository().getRef(Constants.R_STASH)
  231. .getObjectId());
  232. write(committedFile, "content4");
  233. RevCommit thirdStash = git.stashCreate().call();
  234. assertNotNull(thirdStash);
  235. stashRef = git.getRepository().getRef(Constants.R_STASH);
  236. assertNotNull(stashRef);
  237. assertEquals(thirdStash, git.getRepository().getRef(Constants.R_STASH)
  238. .getObjectId());
  239. write(committedFile, "content5");
  240. RevCommit fourthStash = git.stashCreate().call();
  241. assertNotNull(fourthStash);
  242. stashRef = git.getRepository().getRef(Constants.R_STASH);
  243. assertNotNull(stashRef);
  244. assertEquals(fourthStash, git.getRepository().getRef(Constants.R_STASH)
  245. .getObjectId());
  246. assertEquals(thirdStash, git.stashDrop().call());
  247. stashRef = git.getRepository().getRef(Constants.R_STASH);
  248. assertNotNull(stashRef);
  249. assertEquals(thirdStash, stashRef.getObjectId());
  250. assertEquals(thirdStash, git.stashDrop().setStashRef(2).call());
  251. stashRef = git.getRepository().getRef(Constants.R_STASH);
  252. assertNotNull(stashRef);
  253. assertEquals(thirdStash, stashRef.getObjectId());
  254. ReflogReader reader = new ReflogReader(git.getRepository(),
  255. Constants.R_STASH);
  256. List<ReflogEntry> entries = reader.getReverseEntries();
  257. assertEquals(2, entries.size());
  258. assertEquals(ObjectId.zeroId(), entries.get(1).getOldId());
  259. assertEquals(secondStash, entries.get(1).getNewId());
  260. assertTrue(entries.get(1).getComment().length() > 0);
  261. assertEquals(entries.get(0).getOldId(), entries.get(1).getNewId());
  262. assertEquals(thirdStash, entries.get(0).getNewId());
  263. assertTrue(entries.get(0).getComment().length() > 0);
  264. }
  265. }