Added
Link Here
|
1 |
package org.netbeans.libs.git.jgit.commands; |
2 |
|
3 |
/* |
4 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
5 |
* |
6 |
* Copyright 2010 Oracle and/or its affiliates. All rights reserved. |
7 |
* |
8 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
9 |
* Other names may be trademarks of their respective owners. |
10 |
* |
11 |
* The contents of this file are subject to the terms of either the GNU |
12 |
* General Public License Version 2 only ("GPL") or the Common |
13 |
* Development and Distribution License("CDDL") (collectively, the |
14 |
* "License"). You may not use this file except in compliance with the |
15 |
* License. You can obtain a copy of the License at |
16 |
* http://www.netbeans.org/cddl-gplv2.html |
17 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
18 |
* specific language governing permissions and limitations under the |
19 |
* License. When distributing the software, include this License Header |
20 |
* Notice in each file and include the License file at |
21 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
22 |
* particular file as subject to the "Classpath" exception as provided |
23 |
* by Oracle in the GPL Version 2 section of the License file that |
24 |
* accompanied this code. If applicable, add the following below the |
25 |
* License Header, with the fields enclosed by brackets [] replaced by |
26 |
* your own identifying information: |
27 |
* "Portions Copyrighted [year] [name of copyright owner]" |
28 |
* |
29 |
* If you wish your version of this file to be governed by only the CDDL |
30 |
* or only the GPL Version 2, indicate your decision by adding |
31 |
* "[Contributor] elects to include this software in this distribution |
32 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
33 |
* single choice of license, a recipient has the option to distribute |
34 |
* your version of this file under either the CDDL, the GPL Version 2 or |
35 |
* to extend the choice of license to its licensees as provided above. |
36 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
37 |
* Version 2 license, then the option applies only if the new code is |
38 |
* made subject to such option by the copyright holder. |
39 |
* |
40 |
* Contributor(s): |
41 |
* |
42 |
* Portions Copyrighted 2010 Sun Microsystems, Inc. |
43 |
*/ |
44 |
|
45 |
import java.io.File; |
46 |
import java.io.IOException; |
47 |
import org.eclipse.jgit.lib.Constants; |
48 |
import org.eclipse.jgit.lib.ObjectReader; |
49 |
import org.eclipse.jgit.lib.Repository; |
50 |
import org.eclipse.jgit.lib.RepositoryState; |
51 |
import org.eclipse.jgit.revwalk.RevCommit; |
52 |
import org.netbeans.libs.git.GitCherryPickResult; |
53 |
import org.netbeans.libs.git.GitClient; |
54 |
import org.netbeans.libs.git.GitRevisionInfo; |
55 |
import org.netbeans.libs.git.GitStatus.Status; |
56 |
import org.netbeans.libs.git.SearchCriteria; |
57 |
import org.netbeans.libs.git.jgit.AbstractGitTestCase; |
58 |
import org.netbeans.libs.git.jgit.Utils; |
59 |
|
60 |
/** |
61 |
* |
62 |
* @author ondra |
63 |
*/ |
64 |
public class CherryPickTest extends AbstractGitTestCase { |
65 |
|
66 |
private File workDir; |
67 |
private Repository repository; |
68 |
private static final String BRANCH = "b"; |
69 |
|
70 |
public CherryPickTest (String testName) throws IOException { |
71 |
super(testName); |
72 |
} |
73 |
|
74 |
@Override |
75 |
protected void setUp() throws Exception { |
76 |
super.setUp(); |
77 |
workDir = getWorkingDirectory(); |
78 |
repository = getRepository(getLocalGitRepository()); |
79 |
} |
80 |
|
81 |
public void testCherryPickCommit () throws Exception { |
82 |
File f = new File(workDir, "f"); |
83 |
write(f, "init"); |
84 |
add(f); |
85 |
commit(f); |
86 |
|
87 |
GitClient client = getClient(workDir); |
88 |
client.createBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR); |
89 |
client.checkoutRevision(BRANCH, true, NULL_PROGRESS_MONITOR); |
90 |
|
91 |
write(f, "change on branch"); |
92 |
add(f); |
93 |
GitRevisionInfo c = client.commit(new File[] { f }, "on branch", null, null, NULL_PROGRESS_MONITOR); |
94 |
|
95 |
client.checkoutRevision(Constants.MASTER, true, NULL_PROGRESS_MONITOR); |
96 |
GitRevisionInfo initCommit = client.log("HEAD", NULL_PROGRESS_MONITOR); |
97 |
|
98 |
Thread.sleep(1100); |
99 |
|
100 |
GitCherryPickResult res = client.cherryPick(GitClient.CherryPickOperation.BEGIN, new String[] { BRANCH }, NULL_PROGRESS_MONITOR); |
101 |
assertEquals(GitCherryPickResult.CherryPickStatus.OK, res.getCherryPickStatus()); |
102 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getParents()[0]); |
103 |
assertEquals(c.getCommitTime(), res.getCurrentHead().getCommitTime()); |
104 |
assertEquals(1, res.getCherryPickedCommits().length); |
105 |
assertEquals(c.getRevision(), res.getCherryPickedCommits()[0].getRevision()); |
106 |
assertFalse(new File(workDir, ".git/sequencer").exists()); |
107 |
} |
108 |
|
109 |
public void testCherryPickCommits () throws Exception { |
110 |
File f = new File(workDir, "f"); |
111 |
write(f, "init"); |
112 |
add(f); |
113 |
commit(f); |
114 |
|
115 |
File[] roots = new File[] { f }; |
116 |
|
117 |
GitClient client = getClient(workDir); |
118 |
client.createBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR); |
119 |
client.checkoutRevision(BRANCH, true, NULL_PROGRESS_MONITOR); |
120 |
|
121 |
write(f, "change 1 on branch"); |
122 |
add(f); |
123 |
GitRevisionInfo c1 = client.commit(roots, "on branch 1", null, null, NULL_PROGRESS_MONITOR); |
124 |
write(f, "change 2 on branch"); |
125 |
add(f); |
126 |
GitRevisionInfo c2 = client.commit(roots, "on branch 2", null, null, NULL_PROGRESS_MONITOR); |
127 |
|
128 |
client.checkoutRevision(Constants.MASTER, true, NULL_PROGRESS_MONITOR); |
129 |
GitRevisionInfo initCommit = client.log("HEAD", NULL_PROGRESS_MONITOR); |
130 |
|
131 |
Thread.sleep(1100); |
132 |
|
133 |
GitCherryPickResult res = client.cherryPick(GitClient.CherryPickOperation.BEGIN, |
134 |
new String[] { c1.getRevision(), c2.getRevision() }, NULL_PROGRESS_MONITOR); |
135 |
assertEquals(GitCherryPickResult.CherryPickStatus.OK, res.getCherryPickStatus()); |
136 |
SearchCriteria sc = new SearchCriteria(); |
137 |
sc.setRevisionTo("HEAD"); |
138 |
GitRevisionInfo[] logs = client.log(sc, NULL_PROGRESS_MONITOR); |
139 |
assertEquals(3, logs.length); |
140 |
assertEquals(c2.getFullMessage(), logs[0].getFullMessage()); |
141 |
assertEquals(c2.getCommitTime(), logs[0].getCommitTime()); |
142 |
assertEquals(c1.getFullMessage(), logs[1].getFullMessage()); |
143 |
assertEquals(c1.getCommitTime(), logs[1].getCommitTime()); |
144 |
assertEquals(initCommit.getRevision(), logs[2].getRevision()); |
145 |
|
146 |
assertEquals(2, res.getCherryPickedCommits().length); |
147 |
assertEquals(c1.getRevision(), res.getCherryPickedCommits()[0].getRevision()); |
148 |
assertEquals(c2.getRevision(), res.getCherryPickedCommits()[1].getRevision()); |
149 |
assertFalse(new File(workDir, ".git/sequencer").exists()); |
150 |
} |
151 |
|
152 |
public void testCherryPickFailure () throws Exception { |
153 |
File f = new File(workDir, "f"); |
154 |
write(f, "init"); |
155 |
add(f); |
156 |
commit(f); |
157 |
|
158 |
File[] roots = new File[] { f }; |
159 |
|
160 |
GitClient client = getClient(workDir); |
161 |
client.createBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR); |
162 |
client.checkoutRevision(BRANCH, true, NULL_PROGRESS_MONITOR); |
163 |
|
164 |
write(f, "change on branch"); |
165 |
add(f); |
166 |
GitRevisionInfo c = client.commit(roots, "on branch", null, null, NULL_PROGRESS_MONITOR); |
167 |
|
168 |
client.checkoutRevision(Constants.MASTER, true, NULL_PROGRESS_MONITOR); |
169 |
// make modification so cherry-pick cannot start |
170 |
write(f, "change in master"); |
171 |
|
172 |
GitCherryPickResult res = client.cherryPick(GitClient.CherryPickOperation.BEGIN, new String[] { BRANCH }, NULL_PROGRESS_MONITOR); |
173 |
assertEquals(GitCherryPickResult.CherryPickStatus.FAILED, res.getCherryPickStatus()); |
174 |
assertEquals(0, res.getCurrentHead().getParents().length); |
175 |
} |
176 |
|
177 |
public void testCherryPickCommitsConflictAbort () throws Exception { |
178 |
File f = new File(workDir, "f"); |
179 |
write(f, "init"); |
180 |
add(f); |
181 |
commit(f); |
182 |
|
183 |
File[] roots = new File[] { f }; |
184 |
|
185 |
GitClient client = getClient(workDir); |
186 |
client.createBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR); |
187 |
client.checkoutRevision(BRANCH, true, NULL_PROGRESS_MONITOR); |
188 |
|
189 |
write(f, "change 1 on branch"); |
190 |
add(f); |
191 |
GitRevisionInfo c1 = client.commit(roots, "on branch 1", null, null, NULL_PROGRESS_MONITOR); |
192 |
write(f, "change 2 on branch"); |
193 |
add(f); |
194 |
GitRevisionInfo c2 = client.commit(roots, "on branch 2", null, null, NULL_PROGRESS_MONITOR); |
195 |
write(f, "change 3 on branch"); |
196 |
add(f); |
197 |
GitRevisionInfo c3 = client.commit(roots, "on branch 3", null, null, NULL_PROGRESS_MONITOR); |
198 |
|
199 |
client.checkoutRevision(Constants.MASTER, true, NULL_PROGRESS_MONITOR); |
200 |
GitRevisionInfo initCommit = client.log("HEAD", NULL_PROGRESS_MONITOR); |
201 |
GitCherryPickResult res = client.cherryPick(GitClient.CherryPickOperation.BEGIN, |
202 |
new String[] { c1.getRevision(), c3.getRevision() }, NULL_PROGRESS_MONITOR); |
203 |
assertEquals(GitCherryPickResult.CherryPickStatus.CONFLICTING, res.getCherryPickStatus()); |
204 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getParents()[0]); |
205 |
|
206 |
res = client.cherryPick(GitClient.CherryPickOperation.ABORT, null, NULL_PROGRESS_MONITOR); |
207 |
assertEquals(GitCherryPickResult.CherryPickStatus.ABORTED, res.getCherryPickStatus()); |
208 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getRevision()); |
209 |
assertStatus(client.getStatus(roots, NULL_PROGRESS_MONITOR), workDir, f, true, |
210 |
Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false); |
211 |
} |
212 |
|
213 |
public void testCherryPickCommitsConflictQuit () throws Exception { |
214 |
File f = new File(workDir, "f"); |
215 |
write(f, "init"); |
216 |
add(f); |
217 |
commit(f); |
218 |
|
219 |
File[] roots = new File[] { f }; |
220 |
|
221 |
GitClient client = getClient(workDir); |
222 |
client.createBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR); |
223 |
client.checkoutRevision(BRANCH, true, NULL_PROGRESS_MONITOR); |
224 |
|
225 |
write(f, "change 1 on branch"); |
226 |
add(f); |
227 |
GitRevisionInfo c1 = client.commit(roots, "on branch 1\nblablabla", null, null, NULL_PROGRESS_MONITOR); |
228 |
write(f, "change 2 on branch"); |
229 |
add(f); |
230 |
GitRevisionInfo c2 = client.commit(roots, "on branch 2", null, null, NULL_PROGRESS_MONITOR); |
231 |
write(f, "change 3 on branch"); |
232 |
add(f); |
233 |
GitRevisionInfo c3 = client.commit(roots, "on branch 3\nBLABLABLA", null, null, NULL_PROGRESS_MONITOR); |
234 |
|
235 |
client.checkoutRevision(Constants.MASTER, true, NULL_PROGRESS_MONITOR); |
236 |
GitRevisionInfo initCommit = client.log("HEAD", NULL_PROGRESS_MONITOR); |
237 |
GitCherryPickResult res = client.cherryPick(GitClient.CherryPickOperation.BEGIN, |
238 |
new String[] { c1.getRevision(), c3.getRevision() }, NULL_PROGRESS_MONITOR); |
239 |
assertEquals(GitCherryPickResult.CherryPickStatus.CONFLICTING, res.getCherryPickStatus()); |
240 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getParents()[0]); |
241 |
|
242 |
ObjectReader or = repository.newObjectReader(); |
243 |
RevCommit commit = Utils.findCommit(repository, c3.getRevision()); |
244 |
assertEquals("pick " + or.abbreviate(commit).name() + " " + commit.getShortMessage(), |
245 |
read(new File(repository.getDirectory(), "sequencer/todo"))); |
246 |
|
247 |
res = client.cherryPick(GitClient.CherryPickOperation.QUIT, null, NULL_PROGRESS_MONITOR); |
248 |
assertEquals(GitCherryPickResult.CherryPickStatus.CONFLICTING, res.getCherryPickStatus()); |
249 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getParents()[0]); |
250 |
assertStatus(client.getStatus(roots, NULL_PROGRESS_MONITOR), workDir, f, true, |
251 |
Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, true); |
252 |
assertEquals(RepositoryState.CHERRY_PICKING, repository.getRepositoryState()); |
253 |
|
254 |
res = client.cherryPick(GitClient.CherryPickOperation.ABORT, null, NULL_PROGRESS_MONITOR); |
255 |
assertEquals(GitCherryPickResult.CherryPickStatus.ABORTED, res.getCherryPickStatus()); |
256 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getParents()[0]); |
257 |
assertStatus(client.getStatus(roots, NULL_PROGRESS_MONITOR), workDir, f, true, |
258 |
Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false); |
259 |
} |
260 |
|
261 |
public void testCherryPickCommitConflictResolve () throws Exception { |
262 |
File f = new File(workDir, "f"); |
263 |
write(f, "init"); |
264 |
add(f); |
265 |
commit(f); |
266 |
|
267 |
File[] roots = new File[] { f }; |
268 |
|
269 |
GitClient client = getClient(workDir); |
270 |
client.createBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR); |
271 |
client.checkoutRevision(BRANCH, true, NULL_PROGRESS_MONITOR); |
272 |
|
273 |
write(f, "change 1 on branch"); |
274 |
add(f); |
275 |
GitRevisionInfo c1 = client.commit(roots, "on branch 1", null, null, NULL_PROGRESS_MONITOR); |
276 |
write(f, "change 2 on branch"); |
277 |
add(f); |
278 |
GitRevisionInfo c2 = client.commit(roots, "on branch 2", null, null, NULL_PROGRESS_MONITOR); |
279 |
|
280 |
client.checkoutRevision(Constants.MASTER, true, NULL_PROGRESS_MONITOR); |
281 |
GitRevisionInfo initCommit = client.log("HEAD", NULL_PROGRESS_MONITOR); |
282 |
GitCherryPickResult res = client.cherryPick(GitClient.CherryPickOperation.BEGIN, |
283 |
new String[] { c2.getRevision() }, NULL_PROGRESS_MONITOR); |
284 |
assertEquals(GitCherryPickResult.CherryPickStatus.CONFLICTING, res.getCherryPickStatus()); |
285 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getRevision()); |
286 |
assertFalse(new File(repository.getDirectory(), "sequencer").exists()); |
287 |
|
288 |
write(f, "init\nchange 2 on branch"); |
289 |
add(f); |
290 |
|
291 |
// try continue, should interrupt and ask for commit |
292 |
res = client.cherryPick(GitClient.CherryPickOperation.CONTINUE, null, NULL_PROGRESS_MONITOR); |
293 |
assertEquals(GitCherryPickResult.CherryPickStatus.UNCOMMITTED, res.getCherryPickStatus()); |
294 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getRevision()); |
295 |
assertFalse(new File(repository.getDirectory(), "sequencer").exists()); |
296 |
|
297 |
GitRevisionInfo commit = client.commit(new File[0], c2.getFullMessage(), null, null, NULL_PROGRESS_MONITOR); |
298 |
assertEquals(c2.getCommitTime(), commit.getCommitTime()); |
299 |
assertEquals(RepositoryState.SAFE, repository.getRepositoryState()); |
300 |
assertFalse(new File(repository.getDirectory(), "sequencer").exists()); |
301 |
|
302 |
res = client.cherryPick(GitClient.CherryPickOperation.CONTINUE, null, NULL_PROGRESS_MONITOR); |
303 |
assertEquals(GitCherryPickResult.CherryPickStatus.OK, res.getCherryPickStatus()); |
304 |
} |
305 |
|
306 |
public void testCherryPickCommitsConflictResolve () throws Exception { |
307 |
File f = new File(workDir, "f"); |
308 |
write(f, "init"); |
309 |
add(f); |
310 |
commit(f); |
311 |
|
312 |
File[] roots = new File[] { f }; |
313 |
|
314 |
GitClient client = getClient(workDir); |
315 |
client.createBranch(BRANCH, Constants.MASTER, NULL_PROGRESS_MONITOR); |
316 |
client.checkoutRevision(BRANCH, true, NULL_PROGRESS_MONITOR); |
317 |
|
318 |
write(f, "change 1 on branch"); |
319 |
add(f); |
320 |
GitRevisionInfo c1 = client.commit(roots, "on branch 1", null, null, NULL_PROGRESS_MONITOR); |
321 |
write(f, "init\nchange 2 on branch"); |
322 |
add(f); |
323 |
GitRevisionInfo c2 = client.commit(roots, "on branch 2", null, null, NULL_PROGRESS_MONITOR); |
324 |
write(f, "init\nchange 3 on branch"); |
325 |
add(f); |
326 |
GitRevisionInfo c3 = client.commit(roots, "on branch 3", null, null, NULL_PROGRESS_MONITOR); |
327 |
|
328 |
Thread.sleep(1100); |
329 |
|
330 |
client.checkoutRevision(Constants.MASTER, true, NULL_PROGRESS_MONITOR); |
331 |
GitRevisionInfo initCommit = client.log("HEAD", NULL_PROGRESS_MONITOR); |
332 |
GitCherryPickResult res = client.cherryPick(GitClient.CherryPickOperation.BEGIN, |
333 |
new String[] { c2.getRevision(), c3.getRevision() }, NULL_PROGRESS_MONITOR); |
334 |
assertEquals(GitCherryPickResult.CherryPickStatus.CONFLICTING, res.getCherryPickStatus()); |
335 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getRevision()); |
336 |
|
337 |
write(f, "init\nchange 2 on branch"); |
338 |
add(f); |
339 |
|
340 |
// try continue, should interrupt and ask for commit |
341 |
res = client.cherryPick(GitClient.CherryPickOperation.CONTINUE, null, NULL_PROGRESS_MONITOR); |
342 |
assertEquals(GitCherryPickResult.CherryPickStatus.UNCOMMITTED, res.getCherryPickStatus()); |
343 |
assertEquals(initCommit.getRevision(), res.getCurrentHead().getRevision()); |
344 |
assertTrue(new File(repository.getDirectory(), "sequencer").exists()); |
345 |
|
346 |
GitRevisionInfo commit = client.commit(new File[0], c2.getFullMessage(), null, null, NULL_PROGRESS_MONITOR); |
347 |
assertEquals(c2.getCommitTime(), commit.getCommitTime()); |
348 |
assertEquals(RepositoryState.SAFE, repository.getRepositoryState()); |
349 |
assertTrue(new File(repository.getDirectory(), "sequencer").exists()); |
350 |
|
351 |
res = client.cherryPick(GitClient.CherryPickOperation.CONTINUE, null, NULL_PROGRESS_MONITOR); |
352 |
assertEquals(GitCherryPickResult.CherryPickStatus.OK, res.getCherryPickStatus()); |
353 |
assertEquals(commit.getRevision(), res.getCurrentHead().getParents()[0]); |
354 |
assertEquals(c3.getCommitTime(), res.getCurrentHead().getCommitTime()); |
355 |
assertFalse(new File(repository.getDirectory(), "sequencer").exists()); |
356 |
assertEquals(1, res.getCherryPickedCommits().length); |
357 |
assertEquals(c3.getRevision(), res.getCherryPickedCommits()[0].getRevision()); |
358 |
} |
359 |
} |