Skip to content

Commit

Permalink
Add a force option to push
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhem committed Aug 2, 2014
1 parent 5721ddb commit 79819f5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
42 changes: 37 additions & 5 deletions src/main/java/hudson/plugins/git/GitPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,6 +51,7 @@ public class GitPublisher extends Recorder implements Serializable, MatrixAggreg

private boolean pushMerge;
private boolean pushOnlyIfSuccess;
private boolean forcePush;

private List<TagToPush> tagsToPush;
// Pushes HEAD to these locations
Expand All @@ -61,12 +64,14 @@ public GitPublisher(List<TagToPush> tagsToPush,
List<BranchToPush> branchesToPush,
List<NoteToPush> 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;
}

Expand All @@ -78,6 +83,10 @@ public boolean isPushMerge() {
return pushMerge;
}

public boolean isForcePush() {
return forcePush;
}

public boolean isPushTags() {
if (tagsToPush == null) {
return false;
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
description="${%If pre-build merging is configured, push the result back to the origin}">
<f:checkbox />
</f:entry>
<f:entry field="forcePush"
title="${%Force Push}"
description="${%Add force option to git push}">
<f:checkbox />
</f:entry>
<f:entry field="tagsToPush"
title="${%Tags}"
description="${%Tags to push to remote repositories}">
Expand Down Expand Up @@ -110,4 +115,4 @@
</f:repeatable>
</f:entry>

</j:jelly>
</j:jelly>
6 changes: 3 additions & 3 deletions src/test/java/hudson/plugins/git/GitPublisherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testMatrixBuild() throws Exception {
Collections.singletonList(new TagToPush("origin","foo","message",true, false)),
Collections.<BranchToPush>emptyList(),
Collections.<NoteToPush>emptyList(),
true, true) {
true, true, false) {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
run.incrementAndGet();
Expand Down Expand Up @@ -115,7 +115,7 @@ public void testMergeAndPush() throws Exception {
Collections.<TagToPush>emptyList(),
Collections.singletonList(new BranchToPush("origin", "integration")),
Collections.<NoteToPush>emptyList(),
true, true));
true, true, false));

// create initial commit and then run the build against it:
commit("commitFileBase", johnDoe, "Initial Commit");
Expand Down Expand Up @@ -154,7 +154,7 @@ public void testMergeAndPushWithSkipTagEnabled() throws Exception {
Collections.<TagToPush>emptyList(),
Collections.singletonList(new BranchToPush("origin", "integration")),
Collections.<NoteToPush>emptyList(),
true, true));
true, true, false));

// create initial commit and then run the build against it:
commit("commitFileBase", johnDoe, "Initial Commit");
Expand Down

0 comments on commit 79819f5

Please sign in to comment.