Skip to content

Commit

Permalink
Merge pull request #248 from guilhem/forceOption
Browse files Browse the repository at this point in the history
[FIXED JENKINS-24082] Add a force option to push command
  • Loading branch information
wannessels committed Sep 17, 2014
2 parents adc9694 + ef6bc18 commit cebb9da
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
42 changes: 37 additions & 5 deletions src/main/java/hudson/plugins/git/GitPublisher.java
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
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>
42 changes: 39 additions & 3 deletions src/test/java/hudson/plugins/git/GitPublisherTest.java
Expand Up @@ -39,7 +39,9 @@
import hudson.scm.NullSCM;
import hudson.tasks.BuildStepDescriptor;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.Issue;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -66,7 +68,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 +117,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 All @@ -132,6 +134,40 @@ public void testMergeAndPush() throws Exception {
assertEquals(sha1, testRepo.git.revParse(Constants.HEAD).name());

}

@Issue("JENKINS-24082")
public void testForcePush() throws Exception {
FreeStyleProject project = setupSimpleProject("master");

GitSCM scm = new GitSCM(
createRemoteRepositories(),
Collections.singletonList(new BranchSpec("*")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null,
Collections.<GitSCMExtension>emptyList());
project.setScm(scm);

project.getPublishersList().add(new GitPublisher(
Collections.<TagToPush>emptyList(),
Collections.singletonList(new BranchToPush("origin", "otherbranch")),
Collections.<NoteToPush>emptyList(),
true, true, true));

commit("commitFile", johnDoe, "Initial Commit");

testRepo.git.branch("otherbranch");
testRepo.git.checkout("otherbranch");
commit("otherCommitFile", johnDoe, "commit lost on force push");

testRepo.git.checkout("master");
commit("commitFile2", johnDoe, "commit to be pushed");

ObjectId expectedCommit = testRepo.git.revParse("master");

build(project, Result.SUCCESS, "commitFile");

assertEquals(expectedCommit, testRepo.git.revParse("otherbranch"));
}

/**
* Fix push to remote when skipTag is enabled
Expand All @@ -154,7 +190,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 cebb9da

Please sign in to comment.