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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/*******************************************************************************
* Copyright (c) 2014 Maik Schreiber
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Maik Schreiber - initial implementation
*******************************************************************************/
package org.eclipse.egit.core.test.op;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.egit.core.op.SquashCommitsOperation;
import org.eclipse.egit.core.test.GitTestCase;
import org.eclipse.egit.core.test.TestRepository;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.LogCommand;
import org.eclipse.jgit.api.RebaseCommand.InteractiveHandler;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.RebaseTodoLine;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SquashCommitsOperationTest extends GitTestCase {
private TestRepository testRepository;
private RevCommit commit1;
private RevCommit commit2;
private RevCommit commit3;
@Override
@Before
public void setUp() throws Exception {
super.setUp();
gitDir = new File(project.getProject().getLocationURI().getPath(),
Constants.DOT_GIT);
testRepository = new TestRepository(gitDir);
testRepository.connect(project.getProject());
testRepository.createInitialCommit("initial");
File file = testRepository.createFile(project.getProject(), "file-1");
commit1 = testRepository.addAndCommit(project.getProject(), file,
"commit 1");
testRepository.appendFileContent(file, "file-2");
commit2 = testRepository.addAndCommit(project.getProject(), file,
"commit 2");
testRepository.appendFileContent(file, "file-3");
commit3 = testRepository.addAndCommit(project.getProject(), file,
"commit 3");
}
@Override
@After
public void tearDown() throws Exception {
testRepository.dispose();
super.tearDown();
}
@Test
public void squash() throws Exception {
InteractiveHandler messageHandler = new InteractiveHandler() {
@Override
public void prepareSteps(List<RebaseTodoLine> steps) {
// not used
}
@Override
public String modifyCommitMessage(String commit) {
return "squashed";
}
};
List<RevCommit> commits = Arrays.asList(commit1, commit2, commit3);
SquashCommitsOperation op = new SquashCommitsOperation(
testRepository.getRepository(), commits, messageHandler);
op.execute(new NullProgressMonitor());
assertEquals(2, countCommitsInHead());
LogCommand log;
try (Git git = new Git(testRepository.getRepository())) {
log = git.log();
}
Iterable<RevCommit> logCommits = log.call();
RevCommit latestCommit = logCommits.iterator().next();
assertEquals("squashed", latestCommit.getFullMessage());
}
private int countCommitsInHead() throws GitAPIException {
LogCommand log;
try (Git git = new Git(testRepository.getRepository())) {
log = git.log();
}
Iterable<RevCommit> commits = log.call();
int result = 0;
for (Iterator i = commits.iterator(); i.hasNext();) {
i.next();
result++;
}
return result;
}
}
|