Skip to content

Commit

Permalink
Merge pull request #185 from praqma-thi/JENKINS-30486
Browse files Browse the repository at this point in the history
[JENKINS-30486] Added --no-commit option to MergeCommand
  • Loading branch information
MarkEWaite committed Sep 30, 2015
2 parents e7b0fd6 + 3682d13 commit e39f751
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Expand Up @@ -1467,6 +1467,7 @@ public MergeCommand merge() {
MergeStrategy strategy;
FastForwardMode fastForwardMode;
boolean squash;
boolean commit = true;
String comment;

public MergeCommand setRevisionToMerge(ObjectId rev) {
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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);
}
42 changes: 42 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Expand Up @@ -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");
Expand Down

0 comments on commit e39f751

Please sign in to comment.