Esempio n. 1
0
  @Override
  public Revision decorateRevisionToBuild(
      GitSCM scm, AbstractBuild<?, ?> build, GitClient git, BuildListener listener, Revision rev)
      throws IOException, InterruptedException {
    String remoteBranchRef =
        GitSCM.getParameterString(options.getRef(), build.getEnvironment(listener));

    // if the branch we are merging is already at the commit being built, the entire merge becomes
    // no-op
    // so there's nothing to do
    if (rev.containsBranchName(remoteBranchRef)) return rev;

    // Only merge if there's a branch to merge that isn't us..
    listener
        .getLogger()
        .println(
            "Merging "
                + rev
                + " onto "
                + remoteBranchRef
                + " using "
                + scm.getUserMergeOptions().getMergeStrategy().toString()
                + " strategy");

    // checkout origin/blah
    ObjectId target = git.revParse(remoteBranchRef);

    String paramLocalBranch = scm.getParamLocalBranch(build);
    git.checkoutBranch(paramLocalBranch, remoteBranchRef);

    try {
      MergeCommand cmd = git.merge().setRevisionToMerge(rev.getSha1());
      for (GitSCMExtension ext : scm.getExtensions())
        ext.decorateMergeCommand(scm, build, git, listener, cmd);
      cmd.execute();
    } catch (GitException ex) {
      // merge conflict. First, avoid leaving any conflict markers in the working tree
      // by checking out some known clean state. We don't really mind what commit this is,
      // since the next build is going to pick its own commit to build, but 'rev' is as good any.
      git.checkoutBranch(paramLocalBranch, rev.getSha1String());

      // record the fact that we've tried building 'rev' and it failed, or else
      // BuildChooser in future builds will pick up this same 'rev' again and we'll see the exact
      // same merge failure
      // all over again.
      scm.getBuildData(build).saveBuild(new Build(rev, build.getNumber(), FAILURE));
      throw new AbortException("Branch not suitable for integration as it does not merge cleanly");
    }

    build.addAction(new MergeRecord(remoteBranchRef, target.getName()));

    return new GitUtils(listener, git).getRevisionForSHA1(git.revParse(HEAD));
  }