]> source.dussan.org Git - jgit.git/commitdiff
Add --squash option to org.eclipse.jgit.pgm.Merge 56/7956/2
authorTomasz Zarna <tzarna@gmail.com>
Sun, 7 Oct 2012 21:38:34 +0000 (23:38 +0200)
committerTomasz Zarna <tzarna@gmail.com>
Sun, 7 Oct 2012 21:38:34 +0000 (23:38 +0200)
Change-Id: Ifd20b6f4731cfa71319145cac7b464aa53db18b8

org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/MergeTest.java
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java

index b33fc0f44addfa56ef1afef1c5b5aaccad1c0a4f..87a2c855334efa5699e103272674cd0324f602b5 100644 (file)
@@ -43,6 +43,7 @@
 package org.eclipse.jgit.pgm;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
 
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.lib.CLIRepositoryTestCase;
@@ -51,11 +52,15 @@ import org.junit.Before;
 import org.junit.Test;
 
 public class MergeTest extends CLIRepositoryTestCase {
+
+       private Git git;
+
        @Override
        @Before
        public void setUp() throws Exception {
                super.setUp();
-               new Git(db).commit().setMessage("initial commit").call();
+               git = new Git(db);
+               git.commit().setMessage("initial commit").call();
        }
 
        @Test
@@ -63,31 +68,56 @@ public class MergeTest extends CLIRepositoryTestCase {
                assertEquals("Already up-to-date.", execute("git merge master")[0]);
        }
 
+       @Test
+       public void testSquashSelf() throws Exception {
+               assertEquals(" (nothing to squash)Already up-to-date.",
+                               execute("git merge master --squash")[0]);
+       }
+
        @Test
        public void testFastForward() throws Exception {
-               new Git(db).commit().setMessage("initial commit").call();
-               new Git(db).branchCreate().setName("side").call();
+               git.branchCreate().setName("side").call();
                writeTrashFile("file", "master");
-               new Git(db).add().addFilepattern("file").call();
-               new Git(db).commit().setMessage("commit").call();
-               new Git(db).checkout().setName("side").call();
+               git.add().addFilepattern("file").call();
+               git.commit().setMessage("commit").call();
+               git.checkout().setName("side").call();
 
                assertEquals("Fast-forward", execute("git merge master")[0]);
        }
 
        @Test
        public void testMerge() throws Exception {
-               new Git(db).commit().setMessage("initial commit").call();
-               new Git(db).branchCreate().setName("side").call();
+               git.branchCreate().setName("side").call();
                writeTrashFile("master", "content");
-               new Git(db).add().addFilepattern("master").call();
-               new Git(db).commit().setMessage("master commit").call();
-               new Git(db).checkout().setName("side").call();
+               git.add().addFilepattern("master").call();
+               git.commit().setMessage("master commit").call();
+               git.checkout().setName("side").call();
                writeTrashFile("side", "content");
-               new Git(db).add().addFilepattern("side").call();
-               new Git(db).commit().setMessage("side commit").call();
+               git.add().addFilepattern("side").call();
+               git.commit().setMessage("side commit").call();
 
                assertEquals("Merge made by the '" + MergeStrategy.RESOLVE.getName()
                                + "' strategy.", execute("git merge master")[0]);
        }
+
+       @Test
+       public void testSquash() throws Exception {
+               git.branchCreate().setName("side").call();
+               writeTrashFile("file1", "content1");
+               git.add().addFilepattern("file1").call();
+               git.commit().setMessage("file1 commit").call();
+               writeTrashFile("file2", "content2");
+               git.add().addFilepattern("file2").call();
+               git.commit().setMessage("file2 commit").call();
+               git.checkout().setName("side").call();
+               writeTrashFile("side", "content");
+               git.add().addFilepattern("side").call();
+               git.commit().setMessage("side commit").call();
+
+               assertArrayEquals(
+                               new String[] { "Squash commit -- not updating HEAD",
+                                               "Automatic merge went well; stopped before committing as requested",
+                                               "" },
+                               execute("git merge master --squash"));
+       }
 }
index 5e38c04bb58d58617deb6baaa1f44600daa23686..8bbcf5e84138f9b0d5ce446a3c712cda69358c16 100644 (file)
@@ -66,6 +66,7 @@ listeningOn=Listening on {0}
 mergeConflict=CONFLICT(content): Merge conflict in {0}
 mergeFailed=Automatic merge failed; fix conflicts and then commit the result
 mergeMadeBy=Merge made by the ''{0}'' strategy.
+mergedSquashed=Squash commit -- not updating HEAD\nAutomatic merge went well; stopped before committing as requested
 metaVar_DAG=DAG
 metaVar_KEY=KEY
 metaVar_arg=ARG
@@ -127,6 +128,7 @@ notAnIndexFile={0} is not an index file
 notAnObject={0} is not an object
 notFound=!! NOT FOUND !!
 noteObjectTooLargeToPrint=Note object {0} too large to print
+nothingToSquash=\ (nothing to squash)
 notOnAnyBranch=Not currently on any branch.
 onBranch=On branch {0}
 onBranchToBeBorn=You are on a branch yet to be born
@@ -262,6 +264,7 @@ usage_showRefNamesMatchingCommits=Show ref names matching commits
 usage_showPatch=display patch
 usage_showRefNamesMatchingCommits=Show ref names matching commits
 usage_showNotes=Add this ref to the list of note branches from which notes are displayed
+usage_squash=Squash commits as if a real merge happened, but do not make a commit or move the HEAD.
 usage_srcPrefix=show the source prefix instead of "a/"
 usage_symbolicVersionForTheProject=Symbolic version for the project
 usage_synchronizeIPZillaData=Synchronize IPZilla data
index a5756cec140ec23e0eba31349022a36247238b24..86ec92d382a48ff19fef2b929276c55b44474ccb 100644 (file)
@@ -133,6 +133,7 @@ public class CLIText extends TranslationBundle {
        /***/ public String mergeConflict;
        /***/ public String mergeFailed;
        /***/ public String mergeMadeBy;
+       /***/ public String mergedSquashed;
        /***/ public String metaVar_KEY;
        /***/ public String metaVar_arg;
        /***/ public String metaVar_author;
@@ -189,6 +190,7 @@ public class CLIText extends TranslationBundle {
        /***/ public String notFound;
        /***/ public String notOnAnyBranch;
        /***/ public String noteObjectTooLargeToPrint;
+       /***/ public String nothingToSquash;
        /***/ public String onBranchToBeBorn;
        /***/ public String onBranch;
        /***/ public String onlyOneMetaVarExpectedIn;
index 8c769e607602759780c6117444940c8f38c3f28f..728866d1824dd37f6ffd607685f7b0412aeeb07b 100644 (file)
@@ -60,6 +60,9 @@ class Merge extends TextBuiltin {
        @Option(name = "--strategy", aliases = { "-s" }, usage = "usage_mergeStrategy")
        private String strategyName;
 
+       @Option(name = "--squash", usage = "usage_squash")
+       private boolean squash;
+
        private MergeStrategy mergeStrategy = MergeStrategy.RESOLVE;
 
        @Argument(required = true)
@@ -83,10 +86,12 @@ class Merge extends TextBuiltin {
 
                Git git = new Git(db);
                MergeResult result = git.merge().setStrategy(mergeStrategy)
-                               .include(src).call();
+                               .setSquash(squash).include(src).call();
 
                switch (result.getMergeStatus()) {
                case ALREADY_UP_TO_DATE:
+                       if (squash)
+                               outw.print(CLIText.get().nothingToSquash);
                        outw.println(CLIText.get().alreadyUpToDate);
                        break;
                case FAST_FORWARD:
@@ -117,6 +122,9 @@ class Merge extends TextBuiltin {
                        outw.println(MessageFormat.format(CLIText.get().mergeMadeBy,
                                        mergeStrategy.getName()));
                        break;
+               case MERGED_SQUASHED:
+                       outw.println(CLIText.get().mergedSquashed);
+                       break;
                case NOT_SUPPORTED:
                        outw.println(MessageFormat.format(
                                        CLIText.get().unsupportedOperation, result.toString()));