diff --git a/src/main/java/hudson/plugins/git/GitPublisher.java b/src/main/java/hudson/plugins/git/GitPublisher.java index 8311631bf2..7c1ac90602 100644 --- a/src/main/java/hudson/plugins/git/GitPublisher.java +++ b/src/main/java/hudson/plugins/git/GitPublisher.java @@ -26,7 +26,9 @@ import hudson.util.FormValidation; import org.apache.commons.lang.StringUtils; import org.eclipse.jgit.transport.RemoteConfig; +import org.eclipse.jgit.transport.URIish; import org.jenkinsci.plugins.gitclient.GitClient; +import org.jenkinsci.plugins.gitclient.PushCommand; import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; @@ -49,6 +51,7 @@ public class GitPublisher extends Recorder implements Serializable, MatrixAggreg private boolean pushMerge; private boolean pushOnlyIfSuccess; + private boolean forcePush; private List tagsToPush; // Pushes HEAD to these locations @@ -61,12 +64,14 @@ public GitPublisher(List tagsToPush, List branchesToPush, List notesToPush, boolean pushOnlyIfSuccess, - boolean pushMerge) { + boolean pushMerge, + boolean forcePush) { this.tagsToPush = tagsToPush; this.branchesToPush = branchesToPush; this.notesToPush = notesToPush; this.pushMerge = pushMerge; this.pushOnlyIfSuccess = pushOnlyIfSuccess; + this.forcePush = forcePush; this.configVersion = 2L; } @@ -78,6 +83,10 @@ public boolean isPushMerge() { return pushMerge; } + public boolean isForcePush() { + return forcePush; + } + public boolean isPushTags() { if (tagsToPush == null) { return false; @@ -200,6 +209,8 @@ public boolean perform(AbstractBuild build, final GitClient git = gitSCM.createClient(listener, environment, build, build.getWorkspace()); + URIish remoteURI; + // If we're pushing the merge back... if (pushMerge) { try { @@ -223,7 +234,12 @@ public boolean perform(AbstractBuild build, RemoteConfig remote = mergeOptions.getMergeRemote(); listener.getLogger().println("Pushing HEAD to branch " + mergeTarget + " of " + remote.getName() + " repository"); - git.push(remote.getName(), "HEAD:" + mergeTarget); + remoteURI = remote.getURIs().get(0); + PushCommand push = git.push().to(remoteURI).ref("HEAD:" + mergeTarget); + if (forcePush) { + push.force(); + } + push.execute(); } else { //listener.getLogger().println("Pushing result " + buildnumber + " to origin repository"); //git.push(null); @@ -273,7 +289,13 @@ else if (!tagExists) { listener.getLogger().println("Pushing tag " + tagName + " to repo " + targetRepo); - git.push(remote.getName(), tagName); + + remoteURI = remote.getURIs().get(0); + PushCommand push = git.push().to(remoteURI).ref(tagName); + if (forcePush) { + push.force(); + } + push.execute(); } catch (GitException e) { e.printStackTrace(listener.error("Failed to push tag " + tagName + " to " + targetRepo)); return false; @@ -300,7 +322,12 @@ else if (!tagExists) { listener.getLogger().println("Pushing HEAD to branch " + branchName + " at repo " + targetRepo); - git.push(remote.getName(), "HEAD:" + branchName); + remoteURI = remote.getURIs().get(0); + PushCommand push = git.push().to(remoteURI).ref("HEAD:" + branchName); + if (forcePush) { + push.force(); + } + push.execute(); } catch (GitException e) { e.printStackTrace(listener.error("Failed to push branch " + branchName + " to " + targetRepo)); return false; @@ -335,7 +362,12 @@ else if (!tagExists) { else git.appendNote( noteMsg, noteNamespace ); - git.push(remote.getName(), "refs/notes/*" ); + remoteURI = remote.getURIs().get(0); + PushCommand push = git.push().to(remoteURI).ref("refs/notes/*"); + if (forcePush) { + push.force(); + } + push.execute(); } catch (GitException e) { e.printStackTrace(listener.error("Failed to add note: \n" + noteMsg + "\n******")); return false; diff --git a/src/main/resources/hudson/plugins/git/GitPublisher/config.jelly b/src/main/resources/hudson/plugins/git/GitPublisher/config.jelly index b82bbde812..146f56f096 100644 --- a/src/main/resources/hudson/plugins/git/GitPublisher/config.jelly +++ b/src/main/resources/hudson/plugins/git/GitPublisher/config.jelly @@ -23,6 +23,11 @@ description="${%If pre-build merging is configured, push the result back to the origin}"> + + + @@ -110,4 +115,4 @@ - \ No newline at end of file + diff --git a/src/test/java/hudson/plugins/git/GitPublisherTest.java b/src/test/java/hudson/plugins/git/GitPublisherTest.java index 717a0566c4..6c164d73fd 100644 --- a/src/test/java/hudson/plugins/git/GitPublisherTest.java +++ b/src/test/java/hudson/plugins/git/GitPublisherTest.java @@ -66,7 +66,7 @@ public void testMatrixBuild() throws Exception { Collections.singletonList(new TagToPush("origin","foo","message",true, false)), Collections.emptyList(), Collections.emptyList(), - true, true) { + true, true, false) { @Override public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { run.incrementAndGet(); @@ -115,7 +115,7 @@ public void testMergeAndPush() throws Exception { Collections.emptyList(), Collections.singletonList(new BranchToPush("origin", "integration")), Collections.emptyList(), - true, true)); + true, true, false)); // create initial commit and then run the build against it: commit("commitFileBase", johnDoe, "Initial Commit"); @@ -154,7 +154,7 @@ public void testMergeAndPushWithSkipTagEnabled() throws Exception { Collections.emptyList(), Collections.singletonList(new BranchToPush("origin", "integration")), Collections.emptyList(), - true, true)); + true, true, false)); // create initial commit and then run the build against it: commit("commitFileBase", johnDoe, "Initial Commit");