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

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