|
2 | 2 |
|
3 | 3 | import edu.umd.cs.findbugs.annotations.CheckForNull; |
4 | 4 | import edu.umd.cs.findbugs.annotations.NonNull; |
5 | | -import hudson.model.Run; |
6 | | -import hudson.model.TaskListener; |
7 | | -import hudson.plugins.git.GitException; |
8 | | -import hudson.plugins.git.GitSCM; |
9 | | -import hudson.plugins.git.Revision; |
10 | | -import hudson.plugins.git.extensions.GitSCMExtension; |
11 | | -import hudson.plugins.git.extensions.impl.PreBuildMerge; |
12 | | -import hudson.plugins.git.util.MergeRecord; |
13 | | -import java.io.IOException; |
14 | | -import org.apache.commons.lang.StringUtils; |
15 | | -import org.eclipse.jgit.lib.Constants; |
16 | | -import org.eclipse.jgit.lib.ObjectId; |
17 | | -import org.jenkinsci.plugins.gitclient.CheckoutCommand; |
18 | | -import org.jenkinsci.plugins.gitclient.GitClient; |
19 | | -import org.jenkinsci.plugins.gitclient.MergeCommand; |
| 5 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 6 | +import java.io.ObjectStreamException; |
20 | 7 | import org.kohsuke.accmod.Restricted; |
21 | 8 | import org.kohsuke.accmod.restrictions.NoExternalUse; |
22 | 9 |
|
23 | 10 | /** |
24 | | - * Similar to {@link PreBuildMerge}, but we cannot use that unmodified: we need to specify the exact |
25 | | - * base branch hash. The hash is specified so that we are not subject to a race condition between |
26 | | - * the {@code baseHash} we think we are merging with and a possibly newer one that was just pushed. |
| 11 | + * Retained for data migration. |
| 12 | + * |
| 13 | + * @deprecated use {@link jenkins.plugins.git.MergeWithGitSCMExtension} |
27 | 14 | */ |
| 15 | +@Deprecated |
28 | 16 | @Restricted(NoExternalUse.class) |
29 | | -public class MergeWithGitSCMExtension extends GitSCMExtension { |
30 | | - |
31 | | - @NonNull |
32 | | - private final String baseName; |
33 | | - @CheckForNull |
34 | | - private final String baseHash; |
| 17 | +@SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS") |
| 18 | +public class MergeWithGitSCMExtension extends jenkins.plugins.git.MergeWithGitSCMExtension { |
35 | 19 |
|
36 | 20 | MergeWithGitSCMExtension(@NonNull String baseName, @CheckForNull String baseHash) { |
37 | | - this.baseName = baseName; |
38 | | - this.baseHash = baseHash; |
39 | | - } |
40 | | - |
41 | | - @NonNull |
42 | | - public String getBaseName() { |
43 | | - return baseName; |
44 | | - } |
45 | | - |
46 | | - @CheckForNull |
47 | | - public String getBaseHash() { |
48 | | - return baseHash; |
49 | | - } |
50 | | - |
51 | | - @Override |
52 | | - public Revision decorateRevisionToBuild(GitSCM scm, Run<?, ?> build, GitClient git, |
53 | | - TaskListener listener, |
54 | | - Revision marked, Revision rev) |
55 | | - throws IOException, InterruptedException, GitException { |
56 | | - ObjectId baseObjectId; |
57 | | - if (StringUtils.isBlank(baseHash)) { |
58 | | - try { |
59 | | - baseObjectId = git.revParse(Constants.R_REFS + baseName); |
60 | | - } catch (GitException e) { |
61 | | - listener.getLogger() |
62 | | - .printf("Unable to determine head revision of %s prior to merge with MR%n", |
63 | | - baseName); |
64 | | - throw e; |
65 | | - } |
66 | | - } else { |
67 | | - baseObjectId = ObjectId.fromString(baseHash); |
68 | | - } |
69 | | - listener.getLogger().printf("Merging %s commit %s into MR head commit %s%n", |
70 | | - baseName, baseObjectId.name(), rev.getSha1String() |
71 | | - ); |
72 | | - checkout(scm, build, git, listener, rev); |
73 | | - try { |
74 | | - /* could parse out of JenkinsLocationConfiguration.get().getAdminAddress() but seems overkill */ |
75 | | - git.setAuthor("Jenkins", "nobody@nowhere"); |
76 | | - git.setCommitter("Jenkins", "nobody@nowhere"); |
77 | | - MergeCommand cmd = git.merge().setRevisionToMerge(baseObjectId); |
78 | | - for (GitSCMExtension ext : scm.getExtensions()) { |
79 | | - // By default we do a regular merge, allowing it to fast-forward. |
80 | | - ext.decorateMergeCommand(scm, build, git, listener, cmd); |
81 | | - } |
82 | | - cmd.execute(); |
83 | | - } catch (GitException x) { |
84 | | - // Try to revert merge conflict markers. |
85 | | - // TODO IGitAPI offers a reset(hard) method yet GitClient does not. Why? |
86 | | - checkout(scm, build, git, listener, rev); |
87 | | - // TODO would be nicer to throw an AbortException with just the message, but this is actually worse |
88 | | - // until git-client 1.19.7+ |
89 | | - throw x; |
90 | | - } |
91 | | - build.addAction( |
92 | | - new MergeRecord(baseName, |
93 | | - baseObjectId.getName())); // does not seem to be used, but just in case |
94 | | - ObjectId mergeRev = git.revParse(Constants.HEAD); |
95 | | - listener.getLogger().println("Merge succeeded, producing " + mergeRev.name()); |
96 | | - return new Revision(mergeRev, |
97 | | - rev.getBranches()); // note that this ensures Build.revision != Build.marked |
| 21 | + super(baseName, baseHash); |
98 | 22 | } |
99 | 23 |
|
100 | | - private void checkout(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener, |
101 | | - Revision rev) |
102 | | - throws InterruptedException, IOException, GitException { |
103 | | - CheckoutCommand checkoutCommand = git.checkout().ref(rev.getSha1String()); |
104 | | - for (GitSCMExtension ext : scm.getExtensions()) { |
105 | | - ext.decorateCheckoutCommand(scm, build, git, listener, checkoutCommand); |
106 | | - } |
107 | | - checkoutCommand.execute(); |
| 24 | + private Object readResolve() throws ObjectStreamException { |
| 25 | + return new jenkins.plugins.git.MergeWithGitSCMExtension(getBaseName(), getBaseHash()); |
108 | 26 | } |
109 | 27 |
|
110 | 28 | } |
0 commit comments