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