Skip to content
Permalink
Browse files
[JENKINS-45771] Pick up consolidated MergeWithGitSCMExtension from gi…
…t plugin
  • Loading branch information
stephenc committed Jul 27, 2017
1 parent 0568648 commit ed08e75e55f227e7d001dfd84a234fd477c7b831
Showing with 12 additions and 73 deletions.
  1. +1 −1 pom.xml
  2. +1 −0 src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMBuilder.java
  3. +10 −72 src/main/java/org/jenkinsci/plugin/gitea/MergeWithGitSCMExtension.java
@@ -60,7 +60,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>3.4.0</version>
<version>3.5.0-20170727.090936-2</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -43,6 +43,7 @@
import java.util.Random;
import java.util.Set;
import jenkins.plugins.git.GitSCMBuilder;
import jenkins.plugins.git.MergeWithGitSCMExtension;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSourceOwner;
@@ -34,92 +34,30 @@
import hudson.plugins.git.extensions.impl.PreBuildMerge;
import hudson.plugins.git.util.MergeRecord;
import java.io.IOException;
import java.io.ObjectStreamException;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.gitclient.CheckoutCommand;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.MergeCommand;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* Similar to {@link PreBuildMerge}, but we cannot use that unmodified: we need to specify the exact base branch
* hash. The hash is specified so that we are not subject to a race condition between the {@code baseHash} we think
* we are merging with and a possibly newer one that was just pushed.
* Retained for data migration.
* @deprecated use {@link jenkins.plugins.git.MergeWithGitSCMExtension}
*/
@Restricted(NoExternalUse.class)
public class MergeWithGitSCMExtension extends GitSCMExtension {
@NonNull
private final String baseName;
@CheckForNull
private final String baseHash;
@Deprecated
@Restricted(DoNotUse.class)
public class MergeWithGitSCMExtension extends jenkins.plugins.git.MergeWithGitSCMExtension {

MergeWithGitSCMExtension(@NonNull String baseName, @CheckForNull String baseHash) {
this.baseName = baseName;
this.baseHash = baseHash;
super(baseName, baseHash);
}

@NonNull
public String getBaseName() {
return baseName;
}

public String getBaseHash() {
return baseHash;
}

@Override
public Revision decorateRevisionToBuild(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener,
Revision marked, Revision rev)
throws IOException, InterruptedException, GitException {
ObjectId baseObjectId;
if (StringUtils.isBlank(baseHash)) {
try {
baseObjectId = git.revParse(Constants.R_REFS + baseName);
} catch (GitException e) {
listener.getLogger().printf("Unable to determine head revision of %s prior to merge with PR%n",
baseName);
throw e;
}
} else {
baseObjectId = ObjectId.fromString(baseHash);
}
listener.getLogger().printf("Merging %s commit %s into PR head commit %s%n",
baseName, baseObjectId.name(), rev.getSha1String()
);
checkout(scm, build, git, listener, rev);
try {
/* could parse out of JenkinsLocationConfiguration.get().getAdminAddress() but seems overkill */
git.setAuthor("Jenkins", "nobody@nowhere");
git.setCommitter("Jenkins", "nobody@nowhere");
MergeCommand cmd = git.merge().setRevisionToMerge(baseObjectId);
for (GitSCMExtension ext : scm.getExtensions()) {
// By default we do a regular merge, allowing it to fast-forward.
ext.decorateMergeCommand(scm, build, git, listener, cmd);
}
cmd.execute();
} catch (GitException x) {
// Try to revert merge conflict markers.
// TODO IGitAPI offers a reset(hard) method yet GitClient does not. Why?
checkout(scm, build, git, listener, rev);
// TODO would be nicer to throw an AbortException with just the message, but this is actually worse
// until git-client 1.19.7+
throw x;
}
build.addAction(
new MergeRecord(baseName, baseObjectId.getName())); // does not seem to be used, but just in case
ObjectId mergeRev = git.revParse(Constants.HEAD);
listener.getLogger().println("Merge succeeded, producing " + mergeRev.name());
return new Revision(mergeRev, rev.getBranches()); // note that this ensures Build.revision != Build.marked
}

private void checkout(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener, Revision rev)
throws InterruptedException, IOException, GitException {
CheckoutCommand checkoutCommand = git.checkout().ref(rev.getSha1String());
for (GitSCMExtension ext : scm.getExtensions()) {
ext.decorateCheckoutCommand(scm, build, git, listener, checkoutCommand);
}
checkoutCommand.execute();
private Object readResolve() throws ObjectStreamException {
return new jenkins.plugins.git.MergeWithGitSCMExtension(getBaseName(), getBaseHash());
}
}

0 comments on commit ed08e75

Please sign in to comment.