Skip to content

Commit

Permalink
Merge pull request #169 from Praqma/JENKINS-28833
Browse files Browse the repository at this point in the history
Implemented Squash option on MergeCommand. JENKINS-28833
  • Loading branch information
MarkEWaite committed Jun 19, 2015
2 parents 31e556b + 95d61c3 commit f4d2340
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -507,6 +507,7 @@ public MergeCommand merge() {
public ObjectId rev;
public String strategy;
public String fastForwardMode;
public boolean squash;

public MergeCommand setRevisionToMerge(ObjectId rev) {
this.rev = rev;
Expand All @@ -523,10 +524,19 @@ public MergeCommand setGitPluginFastForwardMode(MergeCommand.GitPluginFastForwar
return this;
}

public MergeCommand setSquash(boolean squash) {
this.squash = squash;
return this;
}

public void execute() throws GitException, InterruptedException {
ArgumentListBuilder args = new ArgumentListBuilder();
args.add("merge");
try {
if(squash) {
args.add("--squash");
}

if (strategy != null && !strategy.isEmpty() && !strategy.equals(MergeCommand.Strategy.DEFAULT.toString())) {
args.add("-s");
args.add(strategy);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Expand Up @@ -1454,6 +1454,7 @@ public MergeCommand merge() {
ObjectId rev;
MergeStrategy strategy;
FastForwardMode fastForwardMode;
boolean squash;

public MergeCommand setRevisionToMerge(ObjectId rev) {
this.rev = rev;
Expand Down Expand Up @@ -1490,16 +1491,21 @@ public MergeCommand setGitPluginFastForwardMode(MergeCommand.GitPluginFastForwar
return this;
}

public MergeCommand setSquash(boolean squash){
this.squash = squash;
return this;
}

public void execute() throws GitException, InterruptedException {
Repository repo = null;
try {
repo = getRepository();
Git git = git(repo);
MergeResult mergeResult;
if (strategy != null)
mergeResult = git.merge().setStrategy(strategy).setFastForward(fastForwardMode).include(rev).call();
mergeResult = git.merge().setStrategy(strategy).setFastForward(fastForwardMode).setSquash(squash).include(rev).call();
else
mergeResult = git.merge().setFastForward(fastForwardMode).include(rev).call();
mergeResult = git.merge().setFastForward(fastForwardMode).setSquash(squash).include(rev).call();
if (!mergeResult.getMergeStatus().isSuccessful()) {
git.reset().setMode(HARD).call();
throw new GitException("Failed to merge " + rev);
Expand Down
Expand Up @@ -54,4 +54,12 @@ public String toString() {
return "--"+name().toLowerCase().replace("_","-");
}
}

/**
* setSquash
*
* @param squash - whether to squash commits or not
* @return a {@link org.jenkinsci.plugins.gitclient.MergeCommand} object.
*/
MergeCommand setSquash(boolean squash);
}
54 changes: 54 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Expand Up @@ -2302,6 +2302,60 @@ public void test_merge_fast_forward_mode_no_ff() throws Exception {
assertEquals("Merge commit failed. branch2 should be a parent of HEAD but it isn't.",revList.get(0).name(), branch2.name());
}

public void test_merge_squash() throws Exception{
w.init();
w.commitEmpty("init");
w.git.branch("branch1");

//First commit to branch1
w.git.checkout("branch1");
w.touch("file1", "content1");
w.git.add("file1");
w.git.commit("commit1");

//Second commit to branch1
w.touch("file2", "content2");
w.git.add("file2");
w.git.commit("commit2");

//Merge branch1 with master, squashing both commits
w.git.checkout("master");
w.git.merge().setSquash(true).setRevisionToMerge(w.git.getHeadRev(w.repoPath(), "branch1")).execute();

//Compare commit counts of before and after commiting the merge, should be one due to the squashing of commits.
final int commitCountBefore = w.git.revList("HEAD").size();
w.git.commit("commitMerge");
final int commitCountAfter = w.git.revList("HEAD").size();

assertEquals("Squash merge failed. Should have merged only one commit.", 1, commitCountAfter - commitCountBefore);
}

public void test_merge_no_squash() throws Exception{
w.init();
w.commitEmpty("init");

//First commit to branch1
w.git.branch("branch1");
w.git.checkout("branch1");
w.touch("file1", "content1");
w.git.add("file1");
w.git.commit("commit1");

//Second commit to branch1
w.touch("file2", "content2");
w.git.add("file2");
w.git.commit("commit2");

//Merge branch1 with master, without squashing commits.
//Compare commit counts of before and after commiting the merge, should be one due to the squashing of commits.
w.git.checkout("master");
final int commitCountBefore = w.git.revList("HEAD").size();
w.git.merge().setSquash(false).setRevisionToMerge(w.git.getHeadRev(w.repoPath(), "branch1")).execute();
final int commitCountAfter = w.git.revList("HEAD").size();

assertEquals("Squashless merge failed. Should have merged two commits.", 2, commitCountAfter - commitCountBefore);
}

@Deprecated
public void test_merge_refspec() throws Exception {
w.init();
Expand Down

0 comments on commit f4d2340

Please sign in to comment.