1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.api;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertNull;
48 import static org.junit.Assert.assertTrue;
49 import static org.junit.Assert.fail;
50
51 import java.io.File;
52 import java.util.List;
53
54 import org.eclipse.jgit.api.errors.JGitInternalException;
55 import org.eclipse.jgit.junit.RepositoryTestCase;
56 import org.eclipse.jgit.lib.Constants;
57 import org.eclipse.jgit.lib.ObjectId;
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.junit.Before;
63 import org.junit.Test;
64
65
66
67
68 public class StashDropCommandTest extends RepositoryTestCase {
69
70 private RevCommit head;
71
72 private Git git;
73
74 private File committedFile;
75
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 }
86
87 @Test(expected = IllegalArgumentException.class)
88 public void dropNegativeRef() {
89 git.stashDrop().setStashRef(-1);
90 }
91
92 @Test
93 public void dropWithNoStashedCommits() throws Exception {
94 assertNull(git.stashDrop().call());
95 }
96
97 @Test
98 public void dropWithInvalidLogIndex() throws Exception {
99 write(committedFile, "content2");
100 Ref stashRef = git.getRepository().exactRef(Constants.R_STASH);
101 assertNull(stashRef);
102 RevCommit stashed = git.stashCreate().call();
103 assertNotNull(stashed);
104 stashRef = git.getRepository().exactRef(Constants.R_STASH);
105 assertEquals(stashed,
106 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
107 try {
108 assertNull(git.stashDrop().setStashRef(100).call());
109 fail("Exception not thrown");
110 } catch (JGitInternalException e) {
111 assertNotNull(e.getMessage());
112 assertTrue(e.getMessage().length() > 0);
113 }
114 }
115
116 @Test
117 public void dropSingleStashedCommit() throws Exception {
118 write(committedFile, "content2");
119 Ref stashRef = git.getRepository().exactRef(Constants.R_STASH);
120 assertNull(stashRef);
121 RevCommit stashed = git.stashCreate().call();
122 assertNotNull(stashed);
123 stashRef = git.getRepository().exactRef(Constants.R_STASH);
124 assertEquals(stashed,
125 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
126 assertNull(git.stashDrop().call());
127 stashRef = git.getRepository().exactRef(Constants.R_STASH);
128 assertNull(stashRef);
129
130 ReflogReader reader = git.getRepository().getReflogReader(
131 Constants.R_STASH);
132 assertNull(reader);
133 }
134
135 @Test
136 public void dropAll() throws Exception {
137 write(committedFile, "content2");
138 Ref stashRef = git.getRepository().exactRef(Constants.R_STASH);
139 assertNull(stashRef);
140 RevCommit firstStash = git.stashCreate().call();
141 assertNotNull(firstStash);
142 stashRef = git.getRepository().exactRef(Constants.R_STASH);
143 assertNotNull(stashRef);
144 assertEquals(firstStash,
145 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
146
147 write(committedFile, "content3");
148 RevCommit secondStash = git.stashCreate().call();
149 assertNotNull(secondStash);
150 stashRef = git.getRepository().exactRef(Constants.R_STASH);
151 assertNotNull(stashRef);
152 assertEquals(secondStash,
153 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
154
155 assertNull(git.stashDrop().setAll(true).call());
156 assertNull(git.getRepository().exactRef(Constants.R_STASH));
157
158 ReflogReader reader = git.getRepository().getReflogReader(
159 Constants.R_STASH);
160 assertNull(reader);
161 }
162
163 @Test
164 public void dropFirstStashedCommit() throws Exception {
165 write(committedFile, "content2");
166 Ref stashRef = git.getRepository().exactRef(Constants.R_STASH);
167 assertNull(stashRef);
168 RevCommit firstStash = git.stashCreate().call();
169 assertNotNull(firstStash);
170 stashRef = git.getRepository().exactRef(Constants.R_STASH);
171 assertNotNull(stashRef);
172 assertEquals(firstStash,
173 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
174
175 write(committedFile, "content3");
176 RevCommit secondStash = git.stashCreate().call();
177 assertNotNull(secondStash);
178 stashRef = git.getRepository().exactRef(Constants.R_STASH);
179 assertNotNull(stashRef);
180 assertEquals(secondStash,
181 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
182
183 assertEquals(firstStash, git.stashDrop().call());
184 stashRef = git.getRepository().exactRef(Constants.R_STASH);
185 assertNotNull(stashRef);
186 assertEquals(firstStash, stashRef.getObjectId());
187
188 ReflogReader reader = git.getRepository().getReflogReader(
189 Constants.R_STASH);
190 List<ReflogEntry> entries = reader.getReverseEntries();
191 assertEquals(1, entries.size());
192 assertEquals(ObjectId.zeroId(), entries.get(0).getOldId());
193 assertEquals(firstStash, entries.get(0).getNewId());
194 assertTrue(entries.get(0).getComment().length() > 0);
195 }
196
197 @Test
198 public void dropMiddleStashCommit() throws Exception {
199 write(committedFile, "content2");
200 Ref stashRef = git.getRepository().exactRef(Constants.R_STASH);
201 assertNull(stashRef);
202 RevCommit firstStash = git.stashCreate().call();
203 assertNotNull(firstStash);
204 stashRef = git.getRepository().exactRef(Constants.R_STASH);
205 assertNotNull(stashRef);
206 assertEquals(firstStash,
207 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
208
209 write(committedFile, "content3");
210 RevCommit secondStash = git.stashCreate().call();
211 assertNotNull(secondStash);
212 stashRef = git.getRepository().exactRef(Constants.R_STASH);
213 assertNotNull(stashRef);
214 assertEquals(secondStash,
215 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
216
217 write(committedFile, "content4");
218 RevCommit thirdStash = git.stashCreate().call();
219 assertNotNull(thirdStash);
220 stashRef = git.getRepository().exactRef(Constants.R_STASH);
221 assertNotNull(stashRef);
222 assertEquals(thirdStash,
223 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
224
225 assertEquals(thirdStash, git.stashDrop().setStashRef(1).call());
226 stashRef = git.getRepository().exactRef(Constants.R_STASH);
227 assertNotNull(stashRef);
228 assertEquals(thirdStash, stashRef.getObjectId());
229
230 ReflogReader reader = git.getRepository().getReflogReader(
231 Constants.R_STASH);
232 List<ReflogEntry> entries = reader.getReverseEntries();
233 assertEquals(2, entries.size());
234 assertEquals(ObjectId.zeroId(), entries.get(1).getOldId());
235 assertEquals(firstStash, entries.get(1).getNewId());
236 assertTrue(entries.get(1).getComment().length() > 0);
237 assertEquals(entries.get(0).getOldId(), entries.get(1).getNewId());
238 assertEquals(thirdStash, entries.get(0).getNewId());
239 assertTrue(entries.get(0).getComment().length() > 0);
240 }
241
242 @Test
243 public void dropBoundaryStashedCommits() throws Exception {
244 write(committedFile, "content2");
245 Ref stashRef = git.getRepository().exactRef(Constants.R_STASH);
246 assertNull(stashRef);
247 RevCommit firstStash = git.stashCreate().call();
248 assertNotNull(firstStash);
249 stashRef = git.getRepository().exactRef(Constants.R_STASH);
250 assertNotNull(stashRef);
251 assertEquals(firstStash,
252 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
253
254 write(committedFile, "content3");
255 RevCommit secondStash = git.stashCreate().call();
256 assertNotNull(secondStash);
257 stashRef = git.getRepository().exactRef(Constants.R_STASH);
258 assertNotNull(stashRef);
259 assertEquals(secondStash,
260 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
261
262 write(committedFile, "content4");
263 RevCommit thirdStash = git.stashCreate().call();
264 assertNotNull(thirdStash);
265 stashRef = git.getRepository().exactRef(Constants.R_STASH);
266 assertNotNull(stashRef);
267 assertEquals(thirdStash,
268 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
269
270 write(committedFile, "content5");
271 RevCommit fourthStash = git.stashCreate().call();
272 assertNotNull(fourthStash);
273 stashRef = git.getRepository().exactRef(Constants.R_STASH);
274 assertNotNull(stashRef);
275 assertEquals(fourthStash,
276 git.getRepository().exactRef(Constants.R_STASH).getObjectId());
277
278 assertEquals(thirdStash, git.stashDrop().call());
279 stashRef = git.getRepository().exactRef(Constants.R_STASH);
280 assertNotNull(stashRef);
281 assertEquals(thirdStash, stashRef.getObjectId());
282
283 assertEquals(thirdStash, git.stashDrop().setStashRef(2).call());
284 stashRef = git.getRepository().exactRef(Constants.R_STASH);
285 assertNotNull(stashRef);
286 assertEquals(thirdStash, stashRef.getObjectId());
287
288 ReflogReader reader = git.getRepository().getReflogReader(
289 Constants.R_STASH);
290 List<ReflogEntry> entries = reader.getReverseEntries();
291 assertEquals(2, entries.size());
292 assertEquals(ObjectId.zeroId(), entries.get(1).getOldId());
293 assertEquals(secondStash, entries.get(1).getNewId());
294 assertTrue(entries.get(1).getComment().length() > 0);
295 assertEquals(entries.get(0).getOldId(), entries.get(1).getNewId());
296 assertEquals(thirdStash, entries.get(0).getNewId());
297 assertTrue(entries.get(0).getComment().length() > 0);
298 }
299 }