Skip to content
Permalink
Browse files

[JENKINS-30486] Added --no-commit option to MergeCommand

  • Loading branch information
praqma-thi committed Sep 16, 2015
1 parent ad3e18d commit 3682d132de29176fa7cc3328ef495118c0f23041
@@ -529,6 +529,7 @@ public MergeCommand merge() {
public String strategy;
public String fastForwardMode;
public boolean squash;
public boolean commit = true;

public MergeCommand setRevisionToMerge(ObjectId rev) {
this.rev = rev;
@@ -555,6 +556,11 @@ public MergeCommand setMessage(String comment) {
return this;
}

public MergeCommand setCommit(boolean commit) {
this.commit = commit;
return this;
}

public void execute() throws GitException, InterruptedException {
ArgumentListBuilder args = new ArgumentListBuilder();
args.add("merge");
@@ -563,6 +569,10 @@ public void execute() throws GitException, InterruptedException {
args.add("--squash");
}

if(!commit){
args.add("--no-commit");
}

if (comment != null && !comment.isEmpty()) {
args.add("-m");
args.add(comment);
@@ -1467,6 +1467,7 @@ public MergeCommand merge() {
MergeStrategy strategy;
FastForwardMode fastForwardMode;
boolean squash;
boolean commit = true;
String comment;

public MergeCommand setRevisionToMerge(ObjectId rev) {
@@ -1514,16 +1515,21 @@ public MergeCommand setMessage(String comment) {
return this;
}

public MergeCommand setCommit(boolean commit) {
this.commit = commit;
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().setMessage(comment).setStrategy(strategy).setFastForward(fastForwardMode).setSquash(squash).include(rev).call();
mergeResult = git.merge().setMessage(comment).setStrategy(strategy).setFastForward(fastForwardMode).setSquash(squash).setCommit(commit).include(rev).call();
else
mergeResult = git.merge().setMessage(comment).setFastForward(fastForwardMode).setSquash(squash).include(rev).call();
mergeResult = git.merge().setMessage(comment).setFastForward(fastForwardMode).setSquash(squash).setCommit(commit).include(rev).call();
if (!mergeResult.getMergeStatus().isSuccessful()) {
git.reset().setMode(HARD).call();
throw new GitException("Failed to merge " + rev);
@@ -70,4 +70,12 @@ public String toString() {
* @return a {@link org.jenkinsci.plugins.gitclient.MergeCommand} object.
*/
MergeCommand setSquash(boolean squash);

/**
* setCommit
*
* @param commit - whether or not to commit the result after a successful merge.
* @return a {@link org.jenkinsci.plugins.gitclient.MergeCommand} object.
*/
MergeCommand setCommit(boolean commit);
}
@@ -2396,6 +2396,48 @@ public void test_merge_no_squash() throws Exception{
assertEquals("Squashless merge failed. Should have merged two commits.", 2, commitCountAfter - commitCountBefore);
}

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

//Create branch1 and commit a file
w.git.branch("branch1");
w.git.checkout("branch1");
w.touch("file1", "content1");
w.git.add("file1");
w.git.commit("commit1");

//Merge branch1 with master, without committing the merge.
//Compare commit counts of before and after the merge, should be zero due to the lack of autocommit.
w.git.checkout("master");
final int commitCountBefore = w.git.revList("HEAD").size();
w.git.merge().setCommit(false).setGitPluginFastForwardMode(MergeCommand.GitPluginFastForwardMode.NO_FF).setRevisionToMerge(w.git.getHeadRev(w.repoPath(), "branch1")).execute();
final int commitCountAfter = w.git.revList("HEAD").size();

assertEquals("No Commit merge failed. Shouldn't have committed any changes.", 0, commitCountAfter - commitCountBefore);
}

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

//Create branch1 and commit a file
w.git.branch("branch1");
w.git.checkout("branch1");
w.touch("file1", "content1");
w.git.add("file1");
w.git.commit("commit1");

//Merge branch1 with master, without committing the merge.
//Compare commit counts of before and after the merge, should be two due to the commit of the file and the commit of the merge.
w.git.checkout("master");
final int commitCountBefore = w.git.revList("HEAD").size();
w.git.merge().setCommit(true).setGitPluginFastForwardMode(MergeCommand.GitPluginFastForwardMode.NO_FF).setRevisionToMerge(w.git.getHeadRev(w.repoPath(), "branch1")).execute();
final int commitCountAfter = w.git.revList("HEAD").size();

assertEquals("Commit merge failed. Should have committed the merge.", 2, commitCountAfter - commitCountBefore);
}

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

0 comments on commit 3682d13

Please sign in to comment.
You can’t perform that action at this time.