Skip to content

Commit e8c25a1

Browse files
committed
Add --squash option to org.eclipse.jgit.pgm.Merge
Change-Id: Ifd20b6f4731cfa71319145cac7b464aa53db18b8
1 parent a27c1a6 commit e8c25a1

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
package org.eclipse.jgit.pgm;
4444

4545
import static org.junit.Assert.assertEquals;
46+
import static org.junit.Assert.assertArrayEquals;
4647

4748
import org.eclipse.jgit.api.Git;
4849
import org.eclipse.jgit.lib.CLIRepositoryTestCase;
@@ -51,43 +52,72 @@
5152
import org.junit.Test;
5253

5354
public class MergeTest extends CLIRepositoryTestCase {
55+
56+
private Git git;
57+
5458
@Override
5559
@Before
5660
public void setUp() throws Exception {
5761
super.setUp();
58-
new Git(db).commit().setMessage("initial commit").call();
62+
git = new Git(db);
63+
git.commit().setMessage("initial commit").call();
5964
}
6065

6166
@Test
6267
public void testMergeSelf() throws Exception {
6368
assertEquals("Already up-to-date.", execute("git merge master")[0]);
6469
}
6570

71+
@Test
72+
public void testSquashSelf() throws Exception {
73+
assertEquals(" (nothing to squash)Already up-to-date.",
74+
execute("git merge master --squash")[0]);
75+
}
76+
6677
@Test
6778
public void testFastForward() throws Exception {
68-
new Git(db).commit().setMessage("initial commit").call();
69-
new Git(db).branchCreate().setName("side").call();
79+
git.branchCreate().setName("side").call();
7080
writeTrashFile("file", "master");
71-
new Git(db).add().addFilepattern("file").call();
72-
new Git(db).commit().setMessage("commit").call();
73-
new Git(db).checkout().setName("side").call();
81+
git.add().addFilepattern("file").call();
82+
git.commit().setMessage("commit").call();
83+
git.checkout().setName("side").call();
7484

7585
assertEquals("Fast-forward", execute("git merge master")[0]);
7686
}
7787

7888
@Test
7989
public void testMerge() throws Exception {
80-
new Git(db).commit().setMessage("initial commit").call();
81-
new Git(db).branchCreate().setName("side").call();
90+
git.branchCreate().setName("side").call();
8291
writeTrashFile("master", "content");
83-
new Git(db).add().addFilepattern("master").call();
84-
new Git(db).commit().setMessage("master commit").call();
85-
new Git(db).checkout().setName("side").call();
92+
git.add().addFilepattern("master").call();
93+
git.commit().setMessage("master commit").call();
94+
git.checkout().setName("side").call();
8695
writeTrashFile("side", "content");
87-
new Git(db).add().addFilepattern("side").call();
88-
new Git(db).commit().setMessage("side commit").call();
96+
git.add().addFilepattern("side").call();
97+
git.commit().setMessage("side commit").call();
8998

9099
assertEquals("Merge made by the '" + MergeStrategy.RESOLVE.getName()
91100
+ "' strategy.", execute("git merge master")[0]);
92101
}
102+
103+
@Test
104+
public void testSquash() throws Exception {
105+
git.branchCreate().setName("side").call();
106+
writeTrashFile("file1", "content1");
107+
git.add().addFilepattern("file1").call();
108+
git.commit().setMessage("file1 commit").call();
109+
writeTrashFile("file2", "content2");
110+
git.add().addFilepattern("file2").call();
111+
git.commit().setMessage("file2 commit").call();
112+
git.checkout().setName("side").call();
113+
writeTrashFile("side", "content");
114+
git.add().addFilepattern("side").call();
115+
git.commit().setMessage("side commit").call();
116+
117+
assertArrayEquals(
118+
new String[] { "Squash commit -- not updating HEAD",
119+
"Automatic merge went well; stopped before committing as requested",
120+
"" },
121+
execute("git merge master --squash"));
122+
}
93123
}

org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ listeningOn=Listening on {0}
6666
mergeConflict=CONFLICT(content): Merge conflict in {0}
6767
mergeFailed=Automatic merge failed; fix conflicts and then commit the result
6868
mergeMadeBy=Merge made by the ''{0}'' strategy.
69+
mergedSquashed=Squash commit -- not updating HEAD\nAutomatic merge went well; stopped before committing as requested
6970
metaVar_DAG=DAG
7071
metaVar_KEY=KEY
7172
metaVar_arg=ARG
@@ -127,6 +128,7 @@ notAnIndexFile={0} is not an index file
127128
notAnObject={0} is not an object
128129
notFound=!! NOT FOUND !!
129130
noteObjectTooLargeToPrint=Note object {0} too large to print
131+
nothingToSquash=\ (nothing to squash)
130132
notOnAnyBranch=Not currently on any branch.
131133
onBranch=On branch {0}
132134
onBranchToBeBorn=You are on a branch yet to be born
@@ -262,6 +264,7 @@ usage_showRefNamesMatchingCommits=Show ref names matching commits
262264
usage_showPatch=display patch
263265
usage_showRefNamesMatchingCommits=Show ref names matching commits
264266
usage_showNotes=Add this ref to the list of note branches from which notes are displayed
267+
usage_squash=Squash commits as if a real merge happened, but do not make a commit or move the HEAD.
265268
usage_srcPrefix=show the source prefix instead of "a/"
266269
usage_symbolicVersionForTheProject=Symbolic version for the project
267270
usage_synchronizeIPZillaData=Synchronize IPZilla data

org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public static String formatLine(String line) {
133133
/***/ public String mergeConflict;
134134
/***/ public String mergeFailed;
135135
/***/ public String mergeMadeBy;
136+
/***/ public String mergedSquashed;
136137
/***/ public String metaVar_KEY;
137138
/***/ public String metaVar_arg;
138139
/***/ public String metaVar_author;
@@ -189,6 +190,7 @@ public static String formatLine(String line) {
189190
/***/ public String notFound;
190191
/***/ public String notOnAnyBranch;
191192
/***/ public String noteObjectTooLargeToPrint;
193+
/***/ public String nothingToSquash;
192194
/***/ public String onBranchToBeBorn;
193195
/***/ public String onBranch;
194196
/***/ public String onlyOneMetaVarExpectedIn;

org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class Merge extends TextBuiltin {
6060
@Option(name = "--strategy", aliases = { "-s" }, usage = "usage_mergeStrategy")
6161
private String strategyName;
6262

63+
@Option(name = "--squash", usage = "usage_squash")
64+
private boolean squash;
65+
6366
private MergeStrategy mergeStrategy = MergeStrategy.RESOLVE;
6467

6568
@Argument(required = true)
@@ -83,10 +86,12 @@ protected void run() throws Exception {
8386

8487
Git git = new Git(db);
8588
MergeResult result = git.merge().setStrategy(mergeStrategy)
86-
.include(src).call();
89+
.setSquash(squash).include(src).call();
8790

8891
switch (result.getMergeStatus()) {
8992
case ALREADY_UP_TO_DATE:
93+
if (squash)
94+
outw.print(CLIText.get().nothingToSquash);
9095
outw.println(CLIText.get().alreadyUpToDate);
9196
break;
9297
case FAST_FORWARD:
@@ -117,6 +122,9 @@ protected void run() throws Exception {
117122
outw.println(MessageFormat.format(CLIText.get().mergeMadeBy,
118123
mergeStrategy.getName()));
119124
break;
125+
case MERGED_SQUASHED:
126+
outw.println(CLIText.get().mergedSquashed);
127+
break;
120128
case NOT_SUPPORTED:
121129
outw.println(MessageFormat.format(
122130
CLIText.get().unsupportedOperation, result.toString()));

0 commit comments

Comments
 (0)