Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ent-plugin into master-PR189-praqma-thi-JENKINS-30839
  • Loading branch information
MarkEWaite committed Nov 14, 2015
2 parents 88df612 + d280f64 commit 128fc5e
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -592,6 +592,34 @@ public void execute() throws GitException, InterruptedException {
};
}

/**
* rebase.
*
* @return a {@link org.jenkinsci.plugins.gitclient.RebaseCommand} object.
*/
public RebaseCommand rebase() {
return new RebaseCommand() {
private String upstream;

public RebaseCommand setUpstream(String upstream) {
this.upstream = upstream;
return this;
}

public void execute() throws GitException, InterruptedException {
try {
ArgumentListBuilder args = new ArgumentListBuilder();
args.add("rebase");
args.add(upstream);
launchCommand(args);
} catch (GitException e) {
launchCommand("rebase", "--abort");
throw new GitException("Could not rebase " + upstream, e);
}
}
};
}

/**
* init_.
*
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java
Expand Up @@ -19,7 +19,6 @@

import javax.annotation.CheckForNull;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.List;
Expand Down Expand Up @@ -402,6 +401,13 @@ public interface GitClient {
*/
MergeCommand merge();

/**
* rebase.
*
* @return a {@link org.jenkinsci.plugins.gitclient.RebaseCommand} object.
*/
RebaseCommand rebase();

/**
* init_.
*
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Expand Up @@ -119,6 +119,8 @@
import com.google.common.collect.Lists;

import edu.umd.cs.findbugs.annotations.NonNull;
import org.eclipse.jgit.api.RebaseCommand.Operation;
import org.eclipse.jgit.api.RebaseResult;

/**
* GitClient pure Java implementation using JGit.
Expand Down Expand Up @@ -1570,6 +1572,34 @@ public void execute() throws GitException, InterruptedException {
};
}

public RebaseCommand rebase() {
return new RebaseCommand() {
private String upstream;

public RebaseCommand setUpstream(String upstream) {
this.upstream = upstream;
return this;
}

public void execute() throws GitException, InterruptedException {
Repository repo = null;
try {
repo = getRepository();
Git git = git(repo);
RebaseResult rebaseResult = git.rebase().setUpstream(upstream).call();
if (!rebaseResult.getStatus().isSuccessful()) {
git.rebase().setOperation(Operation.ABORT).call();
throw new GitException("Failed to rebase " + upstream);
}
} catch (GitAPIException e) {
throw new GitException("Failed to rebase " + upstream, e);
} finally {
if (repo != null) repo.close();
}
}
};
}

/** {@inheritDoc} */
public void deleteTag(String tagName) throws GitException {
Repository repo = null;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/RebaseCommand.java
@@ -0,0 +1,15 @@
package org.jenkinsci.plugins.gitclient;

/**
* RebaseCommand interface.
*/
public interface RebaseCommand extends GitCommand {

/**
* setUpstream.
*
* @param upstream a {@link java.lang.String} object.
* @return a {@link org.jenkinsci.plugins.gitclient.RebaseCommand} object.
*/
RebaseCommand setUpstream(String upstream);
}
Expand Up @@ -365,6 +365,15 @@ public MergeCommand merge() {
return command(MergeCommand.class);
}

/**
* rebase.
*
* @return a {@link org.jenkinsci.plugins.gitclient.RebaseCommand} object.
*/
public RebaseCommand rebase() {
return command(RebaseCommand.class);
}

/**
* init_.
*
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Expand Up @@ -2554,6 +2554,71 @@ public void test_merge_refspec() throws Exception {
assertEquals("Wrong invalid default remote", "origin", w.igit().getDefaultRemote("invalid"));
}

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

// First commit to master
w.touch("master_file", "master1");
w.git.add("master_file");
w.git.commit("commit-master1");

// Create a feature branch and make a commit
w.git.branch("feature1");
w.git.checkout("feature1");
w.touch("feature_file", "feature1");
w.git.add("feature_file");
w.git.commit("commit-feature1");

// Second commit to master
w.git.checkout("master");
w.touch("master_file", "master2");
w.git.add("master_file");
w.git.commit("commit-master2");

// Rebase feature commit onto master
w.git.checkout("feature1");
w.git.rebase().setUpstream("master").execute();

assertThat("Should've rebased feature1 onto master", w.git.revList("feature1").contains(w.git.revParse("master")));
assertEquals("HEAD should be on the rebased branch", w.git.revParse("HEAD").name(), w.git.revParse("feature1").name());
assertThat("Rebased file should be present in the worktree",w.git.getWorkTree().child("feature_file").exists());
}

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

// First commit to master
w.touch("file", "master1");
w.git.add("file");
w.git.commit("commit-master1");

// Create a feature branch and make a commit
w.git.branch("feature1");
w.git.checkout("feature1");
w.touch("file", "feature1");
w.git.add("file");
w.git.commit("commit-feature1");

// Second commit to master
w.git.checkout("master");
w.touch("file", "master2");
w.git.add("file");
w.git.commit("commit-master2");

// Rebase feature commit onto master
w.git.checkout("feature1");
try {
w.git.rebase().setUpstream("master").execute();
fail();
}
catch (GitException e) {
assertEquals("HEAD should've been reset to the feature branch.", w.git.revParse("HEAD").name(), w.git.revParse("feature1").name());
// expected
}
}

/**
* Checks that the ChangelogCommand abort() API does not write
* output to the destination. Does not check that the abort() API
Expand Down

0 comments on commit 128fc5e

Please sign in to comment.