Skip to content
Permalink
Browse files

Merge pull request #248 from guilhem/forceOption

[FIXED JENKINS-24082] Add a force option to push command
  • Loading branch information
wannessels committed Sep 17, 2014
2 parents adc9694 + ef6bc18 commit cebb9da131fa6c9e07ec9e6ff71ec89aa58fdd6f
@@ -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 @@

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

private List<TagToPush> tagsToPush;
// Pushes HEAD to these locations
@@ -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;
}

@@ -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;
@@ -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}">
@@ -110,4 +115,4 @@
</f:repeatable>
</f:entry>

</j:jelly>
</j:jelly>
@@ -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;
@@ -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();
@@ -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");
@@ -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
@@ -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");

0 comments on commit cebb9da

Please sign in to comment.
You can’t perform that action at this time.