private static boolean isSameParents(RevCommit prior, RevCommit next) {
   if (prior.getParentCount() != next.getParentCount()) {
     return false;
   } else if (prior.getParentCount() == 0) {
     return true;
   }
   return prior.getParent(0).equals(next.getParent(0));
 }
    @Override
    public ChangeKind load(Key key) throws IOException {
      if (Objects.equal(key.prior, key.next)) {
        return ChangeKind.NO_CODE_CHANGE;
      }

      RevWalk walk = new RevWalk(key.repo);
      try {
        RevCommit prior = walk.parseCommit(key.prior);
        walk.parseBody(prior);
        RevCommit next = walk.parseCommit(key.next);
        walk.parseBody(next);

        if (!next.getFullMessage().equals(prior.getFullMessage())) {
          if (next.getTree() == prior.getTree() && isSameParents(prior, next)) {
            return ChangeKind.NO_CODE_CHANGE;
          } else {
            return ChangeKind.REWORK;
          }
        }

        if (prior.getParentCount() != 1 || next.getParentCount() != 1) {
          // Trivial rebases done by machine only work well on 1 parent.
          return ChangeKind.REWORK;
        }

        if (next.getTree() == prior.getTree() && isSameParents(prior, next)) {
          return ChangeKind.TRIVIAL_REBASE;
        }

        // A trivial rebase can be detected by looking for the next commit
        // having the same tree as would exist when the prior commit is
        // cherry-picked onto the next commit's new first parent.
        ThreeWayMerger merger =
            MergeUtil.newThreeWayMerger(
                key.repo, MergeUtil.createDryRunInserter(), key.strategyName);
        merger.setBase(prior.getParent(0));
        if (merger.merge(next.getParent(0), prior)
            && merger.getResultTreeId().equals(next.getTree())) {
          return ChangeKind.TRIVIAL_REBASE;
        } else {
          return ChangeKind.REWORK;
        }
      } finally {
        key.repo = null;
        walk.release();
      }
    }
  protected boolean hasIgnoreFile(Repository repository, RevCommit revCommit, String relativePath)
      throws Exception {

    if (_ignoreFileName == null) {
      return false;
    }

    try (TreeWalk treeWalk = new TreeWalk(repository)) {
      treeWalk.addTree(revCommit.getTree());

      if (revCommit.getParentCount() > 0) {
        RevCommit parentRevCommit = revCommit.getParent(0);

        treeWalk.addTree(parentRevCommit.getTree());
      }

      treeWalk.setRecursive(true);

      treeWalk.setFilter(
          AndTreeFilter.create(
              PathFilter.create(relativePath + "/" + _ignoreFileName), TreeFilter.ANY_DIFF));

      return treeWalk.next();
    }
  }
示例#4
0
  public CodeReviewCommit createCherryPickFromCommit(
      Repository repo,
      ObjectInserter inserter,
      RevCommit mergeTip,
      RevCommit originalCommit,
      PersonIdent cherryPickCommitterIdent,
      String commitMsg,
      CodeReviewRevWalk rw)
      throws MissingObjectException, IncorrectObjectTypeException, IOException,
          MergeIdenticalTreeException, MergeConflictException {

    final ThreeWayMerger m = newThreeWayMerger(repo, inserter);

    m.setBase(originalCommit.getParent(0));
    if (m.merge(mergeTip, originalCommit)) {
      ObjectId tree = m.getResultTreeId();
      if (tree.equals(mergeTip.getTree())) {
        throw new MergeIdenticalTreeException("identical tree");
      }

      CommitBuilder mergeCommit = new CommitBuilder();
      mergeCommit.setTreeId(tree);
      mergeCommit.setParentId(mergeTip);
      mergeCommit.setAuthor(originalCommit.getAuthorIdent());
      mergeCommit.setCommitter(cherryPickCommitterIdent);
      mergeCommit.setMessage(commitMsg);
      return rw.parseCommit(commit(inserter, mergeCommit));
    } else {
      throw new MergeConflictException("merge conflict");
    }
  }
示例#5
0
  public void buildCompositeCommits() throws IOException {
    revWalk = new RevWalk(repository);
    ByteArrayOutputStream diffTexts = new ByteArrayOutputStream();
    DiffFormatter df = new DiffFormatter(diffTexts);
    df.setRepository(repository);
    df.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
    df.setContext(0);
    df.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(SupportedAlgorithm.HISTOGRAM));
    df.setDetectRenames(true);
    for (int idx = 0; idx < _commits.size(); idx++) {
      RevCommit commit = revWalk.parseCommit(_commits.get(idx));
      int p_count = commit.getParentCount();
      if (p_count == 0) {
        throw new RuntimeException("commit with no parent ?!?!");
      }
      RevCommit p = revWalk.parseCommit(commit.getParent(0).getId());
      List<DiffEntry> diffs = df.scan(p.getTree(), commit.getTree());
      for (DiffEntry d : diffs) {
        CompositeDiff cd = new CompositeDiff(d, commit);

        if (ParsingUtils.isSourceFile(d.getOldPath())
            || ParsingUtils.isSourceFile(d.getNewPath())) {
          extractCodeEdits(diffTexts, df, d, cd);
        }
        _diffs.add(cd);
      }
    }
    revWalk.release();
  }
示例#6
0
  public IFileRevision[] getContributors(final IFileRevision ifr) {
    if (!(ifr instanceof CommitFileRevision)) return NO_REVISIONS;

    final CommitFileRevision rev = (CommitFileRevision) ifr;
    final Repository db = walk.getRepository();
    final String p = rev.getGitPath();
    final RevCommit c = rev.getRevCommit();
    final IFileRevision[] r = new IFileRevision[c.getParentCount()];
    for (int i = 0; i < r.length; i++) r[i] = new CommitFileRevision(db, c.getParent(i), p);
    return r;
  }
 @Test
 public void submitWithFastForward() throws Exception {
   RevCommit oldHead = getRemoteHead();
   PushOneCommit.Result change = createChange();
   submit(change.getChangeId());
   RevCommit head = getRemoteHead();
   assertThat(head.getId()).isEqualTo(change.getCommitId());
   assertThat(head.getParent(0)).isEqualTo(oldHead);
   assertSubmitter(change.getChangeId(), 1);
   assertPersonEquals(admin.getIdent(), head.getAuthorIdent());
   assertPersonEquals(admin.getIdent(), head.getCommitterIdent());
 }
示例#8
0
  private void createHeaderArea(Composite parent, FormToolkit toolkit, int span) {
    RevCommit commit = getCommit().getRevCommit();
    Composite top = toolkit.createComposite(parent);
    GridDataFactory.fillDefaults().grab(true, false).span(span, 1).applyTo(top);
    GridLayoutFactory.fillDefaults().numColumns(2).applyTo(top);

    Composite userArea = toolkit.createComposite(top);
    GridLayoutFactory.fillDefaults().spacing(2, 2).numColumns(1).applyTo(userArea);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(userArea);

    PersonIdent author = commit.getAuthorIdent();
    if (author != null) createUserArea(userArea, toolkit, author, true);

    PersonIdent committer = commit.getCommitterIdent();
    if (committer != null && !committer.equals(author))
      createUserArea(userArea, toolkit, committer, false);

    int count = commit.getParentCount();
    if (count > 0) {
      Composite parents = toolkit.createComposite(top);
      GridLayoutFactory.fillDefaults().spacing(2, 2).numColumns(2).applyTo(parents);
      GridDataFactory.fillDefaults().grab(false, false).applyTo(parents);

      for (int i = 0; i < count; i++) {
        final RevCommit parentCommit = commit.getParent(i);
        toolkit
            .createLabel(parents, UIText.CommitEditorPage_LabelParent)
            .setForeground(toolkit.getColors().getColor(IFormColors.TB_TOGGLE));
        final Hyperlink link =
            toolkit.createHyperlink(
                parents, parentCommit.abbreviate(PARENT_LENGTH).name(), SWT.NONE);
        link.addHyperlinkListener(
            new HyperlinkAdapter() {

              public void linkActivated(HyperlinkEvent e) {
                try {
                  CommitEditor.open(
                      new RepositoryCommit(getCommit().getRepository(), parentCommit));
                  if ((e.getStateMask() & SWT.MOD1) != 0) getEditor().close(false);
                } catch (PartInitException e1) {
                  Activator.logError("Error opening commit editor", e1); // $NON-NLS-1$
                }
              }
            });
      }
    }

    createTagsArea(userArea, toolkit, 2);
  }
  @Test
  public void submitMultipleChanges() throws Exception {
    RevCommit initialHead = getRemoteHead();

    testRepo.reset(initialHead);
    PushOneCommit.Result change2 = createChange("Change 2", "b", "b");

    testRepo.reset(initialHead);
    PushOneCommit.Result change3 = createChange("Change 3", "c", "c");

    testRepo.reset(initialHead);
    PushOneCommit.Result change4 = createChange("Change 4", "d", "d");

    // Change 2 stays untouched.
    approve(change2.getChangeId());
    // Change 3 is a fast-forward, no need to merge.
    submit(change3.getChangeId());

    RevCommit tip = getRemoteLog().get(0);
    assertThat(tip.getShortMessage()).isEqualTo(change3.getCommit().getShortMessage());
    assertThat(tip.getParent(0).getId()).isEqualTo(initialHead.getId());
    assertPersonEquals(admin.getIdent(), tip.getAuthorIdent());
    assertPersonEquals(admin.getIdent(), tip.getCommitterIdent());

    // We need to merge change 4.
    submit(change4.getChangeId());

    tip = getRemoteLog().get(0);
    assertThat(tip.getParent(1).getShortMessage()).isEqualTo(change4.getCommit().getShortMessage());
    assertThat(tip.getParent(0).getShortMessage()).isEqualTo(change3.getCommit().getShortMessage());

    assertPersonEquals(admin.getIdent(), tip.getAuthorIdent());
    assertPersonEquals(serverIdent.get(), tip.getCommitterIdent());

    assertNew(change2.getChangeId());
  }
示例#10
0
  /**
   * Returns the diff between the two commits for the specified file or folder formatted as a patch.
   *
   * @param repository
   * @param baseCommit if base commit is unspecified, the patch is generated against the primary
   *     parent of the specified commit.
   * @param commit
   * @param path if path is specified, the patch is generated only for the specified file or folder.
   *     if unspecified, the patch is generated for the entire diff between the two commits.
   * @return patch as a string
   */
  public static String getCommitPatch(
      Repository repository, RevCommit baseCommit, RevCommit commit, String path) {
    String diff = null;
    try {
      final ByteArrayOutputStream os = new ByteArrayOutputStream();
      RawTextComparator cmp = RawTextComparator.DEFAULT;
      PatchFormatter df = new PatchFormatter(os);
      df.setRepository(repository);
      df.setDiffComparator(cmp);
      df.setDetectRenames(true);

      RevTree commitTree = commit.getTree();
      RevTree baseTree;
      if (baseCommit == null) {
        if (commit.getParentCount() > 0) {
          final RevWalk rw = new RevWalk(repository);
          RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
          baseTree = parent.getTree();
        } else {
          // FIXME initial commit. no parent?!
          baseTree = commitTree;
        }
      } else {
        baseTree = baseCommit.getTree();
      }

      List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
      if (path != null && path.length() > 0) {
        for (DiffEntry diffEntry : diffEntries) {
          if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
            df.format(diffEntry);
            break;
          }
        }
      } else {
        df.format(diffEntries);
      }
      diff = df.getPatch(commit);
      df.flush();
    } catch (Throwable t) {
      try {
        error(t, repository, "failed to generate commit diff!");
      } catch (Exception e) {
      }
    }
    return diff;
  }
  public static List<PathChangeModel> getFilesInCommit(Repository repository, RevCommit commit) {
    List<PathChangeModel> list = new ArrayList<PathChangeModel>();
    if (!hasCommits(repository)) {
      return list;
    }
    RevWalk rw = new RevWalk(repository);
    try {
      if (commit == null) {
        ObjectId object = getDefaultBranch(repository);
        commit = rw.parseCommit(object);
      }

      if (commit.getParentCount() == 0) {
        TreeWalk tw = new TreeWalk(repository);
        tw.reset();
        tw.setRecursive(true);
        tw.addTree(commit.getTree());
        while (tw.next()) {
          list.add(
              new PathChangeModel(
                  tw.getPathString(),
                  tw.getPathString(),
                  0,
                  tw.getRawMode(0),
                  commit.getId().getName(),
                  ChangeType.ADD));
        }
        tw.release();
      } else {
        RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
        DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
        df.setRepository(repository);
        df.setDiffComparator(RawTextComparator.DEFAULT);
        df.setDetectRenames(true);
        List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
        for (DiffEntry diff : diffs) {
          if (diff.getChangeType().equals(ChangeType.DELETE)) {
            list.add(
                new PathChangeModel(
                    diff.getOldPath(),
                    diff.getOldPath(),
                    0,
                    diff.getNewMode().getBits(),
                    commit.getId().getName(),
                    diff.getChangeType()));
          } else {
            list.add(
                new PathChangeModel(
                    diff.getNewPath(),
                    diff.getNewPath(),
                    0,
                    diff.getNewMode().getBits(),
                    commit.getId().getName(),
                    diff.getChangeType()));
          }
        }
      }
    } catch (Throwable t) {
      // todo
      Logger.error(t, t.getMessage());
    } finally {
      rw.dispose();
    }
    return list;
  }
示例#12
0
  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse rsp)
      throws IOException {
    String keyStr = req.getPathInfo();

    // We shouldn't have to do this extra decode pass, but somehow we
    // are now receiving our "^1" suffix as "%5E1", which confuses us
    // downstream. Other times we get our embedded "," as "%2C", which
    // is equally bad. And yet when these happen a "%2F" is left as-is,
    // rather than escaped as "%252F", which makes me feel really really
    // uncomfortable with a blind decode right here.
    //
    keyStr = URLDecoder.decode(keyStr, "UTF-8");

    if (!keyStr.startsWith("/")) {
      rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }
    keyStr = keyStr.substring(1);

    final Patch.Key patchKey;
    final int side;
    {
      final int c = keyStr.lastIndexOf('^');
      if (c == 0) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
      }

      if (c < 0) {
        side = 0;

      } else {
        try {
          side = Integer.parseInt(keyStr.substring(c + 1));
          keyStr = keyStr.substring(0, c);
        } catch (NumberFormatException e) {
          rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
        }
      }

      try {
        patchKey = Patch.Key.parse(keyStr);
      } catch (NumberFormatException e) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
      }
    }

    final Change.Id changeId = patchKey.getParentKey().getParentKey();
    final Project project;
    final PatchSet patchSet;
    try {
      final ReviewDb db = requestDb.get();
      final ChangeControl control = changeControl.validateFor(changeId);

      project = control.getProject();
      patchSet = db.patchSets().get(patchKey.getParentKey());
      if (patchSet == null) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
      }
    } catch (NoSuchChangeException e) {
      rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    } catch (OrmException e) {
      getServletContext().log("Cannot query database", e);
      rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      return;
    }

    final Repository repo;
    try {
      repo = repoManager.openRepository(project.getNameKey());
    } catch (RepositoryNotFoundException e) {
      getServletContext().log("Cannot open repository", e);
      rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      return;
    }

    final ObjectLoader blobLoader;
    final RevCommit fromCommit;
    final String suffix;
    final String path = patchKey.getFileName();
    try {
      final ObjectReader reader = repo.newObjectReader();
      try {
        final RevWalk rw = new RevWalk(reader);
        final RevCommit c;
        final TreeWalk tw;

        c = rw.parseCommit(ObjectId.fromString(patchSet.getRevision().get()));
        if (side == 0) {
          fromCommit = c;
          suffix = "new";

        } else if (1 <= side && side - 1 < c.getParentCount()) {
          fromCommit = rw.parseCommit(c.getParent(side - 1));
          if (c.getParentCount() == 1) {
            suffix = "old";
          } else {
            suffix = "old" + side;
          }

        } else {
          rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
        }

        tw = TreeWalk.forPath(reader, path, fromCommit.getTree());
        if (tw == null) {
          rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
        }

        if (tw.getFileMode(0).getObjectType() == Constants.OBJ_BLOB) {
          blobLoader = reader.open(tw.getObjectId(0), Constants.OBJ_BLOB);

        } else {
          rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
          return;
        }
      } finally {
        reader.release();
      }
    } catch (IOException e) {
      getServletContext().log("Cannot read repository", e);
      rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      return;
    } catch (RuntimeException e) {
      getServletContext().log("Cannot read repository", e);
      rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      return;
    } finally {
      repo.close();
    }

    final byte[] raw = blobLoader.isLarge() ? null : blobLoader.getCachedBytes();
    final long when = fromCommit.getCommitTime() * 1000L;

    rsp.setDateHeader("Last-Modified", when);
    rsp.setDateHeader("Expires", 0L);
    rsp.setHeader("Pragma", "no-cache");
    rsp.setHeader("Cache-Control", "no-cache, must-revalidate");

    OutputStream out;
    ZipOutputStream zo;

    final MimeType contentType = registry.getMimeType(path, raw);
    if (!registry.isSafeInline(contentType)) {
      // The content may not be safe to transmit inline, as a browser might
      // interpret it as HTML or JavaScript hosted by this site. Such code
      // might then run in the site's security domain, and may be able to use
      // the user's cookies to perform unauthorized actions.
      //
      // Usually, wrapping the content into a ZIP file forces the browser to
      // save the content to the local system instead.
      //

      rsp.setContentType(ZIP.toString());
      rsp.setHeader(
          "Content-Disposition",
          "attachment; filename=\"" + safeFileName(path, suffix) + ".zip" + "\"");

      zo = new ZipOutputStream(rsp.getOutputStream());

      final ZipEntry e = new ZipEntry(safeFileName(path, rand(req, suffix)));
      e.setComment(fromCommit.name() + ":" + path);
      e.setSize(blobLoader.getSize());
      e.setTime(when);
      zo.putNextEntry(e);
      out = zo;

    } else {
      rsp.setContentType(contentType.toString());
      rsp.setHeader("Content-Length", "" + blobLoader.getSize());

      out = rsp.getOutputStream();
      zo = null;
    }

    if (raw != null) {
      out.write(raw);
    } else {
      blobLoader.copyTo(out);
    }

    if (zo != null) {
      zo.closeEntry();
    }
    out.close();
  }
示例#13
0
  protected String doDiff(Git git, String objectId, String baseObjectId, String path) {
    Repository r = git.getRepository();
    /*
            RevCommit commit = JGitUtils.getCommit(r, objectId);

            ObjectId current;
            if (isNotBlank(objectId)) {
                current = BlobUtils.getId(r, objectId, blobPath);
            } else {
                current = CommitUtils.getHead(r).getId();
            }
            ObjectId previous;
            if (isNotBlank(baseObjectId)) {
                previous = BlobUtils.getId(r, baseObjectId, blobPath);
            } else {
                RevCommit revCommit = CommitUtils.getCommit(r, current);
                RevCommit[] parents = revCommit.getParents();
                if (parents.length == 0) {
                    throw new IllegalArgumentException("No parent commits!");
                } else {
                    previous = parents[0];
                }
            }
            Collection<Edit> changes = BlobUtils.diff(r, previous, current);

            // no idea how to format Collection<Edit> :)

    */

    RevCommit commit;
    if (Strings.isNotBlank(objectId)) {
      commit = CommitUtils.getCommit(r, objectId);
    } else {
      commit = CommitUtils.getHead(r);
    }
    RevCommit baseCommit = null;
    if (Strings.isNotBlank(baseObjectId)) {
      baseCommit = CommitUtils.getCommit(r, baseObjectId);
    }

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    RawTextComparator cmp = RawTextComparator.DEFAULT;
    DiffFormatter formatter = new DiffFormatter(buffer);
    formatter.setRepository(r);
    formatter.setDiffComparator(cmp);
    formatter.setDetectRenames(true);

    RevTree commitTree = commit.getTree();
    RevTree baseTree;
    try {
      if (baseCommit == null) {
        if (commit.getParentCount() > 0) {
          final RevWalk rw = new RevWalk(r);
          RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
          rw.dispose();
          baseTree = parent.getTree();
        } else {
          // FIXME initial commit. no parent?!
          baseTree = commitTree;
        }
      } else {
        baseTree = baseCommit.getTree();
      }

      List<DiffEntry> diffEntries = formatter.scan(baseTree, commitTree);
      if (path != null && path.length() > 0) {
        for (DiffEntry diffEntry : diffEntries) {
          if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
            formatter.format(diffEntry);
            break;
          }
        }
      } else {
        formatter.format(diffEntries);
      }
      formatter.flush();
      return buffer.toString();
    } catch (IOException e) {
      throw new RuntimeIOException(e);
    }
  }
示例#14
0
文件: GitUtils.java 项目: snjeza/core
  public static CherryPickResult cherryPickNoMerge(final Git git, Ref src)
      throws GitAPIException, CantMergeCommitWithZeroParentsException {
    // Does the same as the original git-cherryPick
    // except commiting after running merger
    Repository repo = git.getRepository();

    RevCommit newHead = null;
    List<Ref> cherryPickedRefs = new LinkedList<Ref>();

    RevWalk revWalk = new RevWalk(repo);
    try {
      // get the head commit
      Ref headRef = repo.getRef(Constants.HEAD);
      if (headRef == null)
        throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
      RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId());

      newHead = headCommit;

      // get the commit to be cherry-picked
      // handle annotated tags
      ObjectId srcObjectId = src.getPeeledObjectId();
      if (srcObjectId == null) srcObjectId = src.getObjectId();
      RevCommit srcCommit = revWalk.parseCommit(srcObjectId);

      // get the parent of the commit to cherry-pick
      if (srcCommit.getParentCount() == 0)
        throw new CantMergeCommitWithZeroParentsException(
            "Commit with zero parents cannot be merged");

      if (srcCommit.getParentCount() > 1)
        throw new MultipleParentsNotAllowedException(
            MessageFormat.format(
                JGitText.get().canOnlyCherryPickCommitsWithOneParent,
                srcCommit.name(),
                Integer.valueOf(srcCommit.getParentCount())));

      RevCommit srcParent = srcCommit.getParent(0);
      revWalk.parseHeaders(srcParent);

      ResolveMerger merger = (ResolveMerger) MergeStrategy.RESOLVE.newMerger(repo);
      merger.setWorkingTreeIterator(new FileTreeIterator(repo));
      merger.setBase(srcParent.getTree());
      if (merger.merge(headCommit, srcCommit)) {
        DirCacheCheckout dco =
            new DirCacheCheckout(
                repo, headCommit.getTree(), repo.lockDirCache(), merger.getResultTreeId());
        dco.setFailOnConflict(true);
        dco.checkout();

        cherryPickedRefs.add(src);
      } else {
        if (merger.failed()) return new CherryPickResult(merger.getFailingPaths());

        // there are merge conflicts
        String message =
            new MergeMessageFormatter()
                .formatWithConflicts(srcCommit.getFullMessage(), merger.getUnmergedPaths());

        repo.writeCherryPickHead(srcCommit.getId());
        repo.writeMergeCommitMsg(message);

        return CherryPickResult.CONFLICT;
      }
    } catch (IOException e) {
      throw new JGitInternalException(
          MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e),
          e);
    } finally {
      revWalk.release();
    }

    return new CherryPickResult(newHead, cherryPickedRefs);
  }
示例#15
0
  /**
   * Returns the diff between two commits for the specified file.
   *
   * @param repository
   * @param baseCommit if base commit is null the diff is to the primary parent of the commit.
   * @param commit
   * @param path if the path is specified, the diff is restricted to that file or folder. if
   *     unspecified, the diff is for the entire commit.
   * @param outputType
   * @return the diff
   */
  public static DiffOutput getDiff(
      Repository repository,
      RevCommit baseCommit,
      RevCommit commit,
      String path,
      DiffOutputType outputType) {
    DiffStat stat = null;
    String diff = null;
    try {
      final ByteArrayOutputStream os = new ByteArrayOutputStream();
      RawTextComparator cmp = RawTextComparator.DEFAULT;
      DiffFormatter df;
      switch (outputType) {
        case HTML:
          df = new GitBlitDiffFormatter(os, commit.getName());
          break;
        case PLAIN:
        default:
          df = new DiffFormatter(os);
          break;
      }
      df.setRepository(repository);
      df.setDiffComparator(cmp);
      df.setDetectRenames(true);

      RevTree commitTree = commit.getTree();
      RevTree baseTree;
      if (baseCommit == null) {
        if (commit.getParentCount() > 0) {
          final RevWalk rw = new RevWalk(repository);
          RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
          rw.dispose();
          baseTree = parent.getTree();
        } else {
          // FIXME initial commit. no parent?!
          baseTree = commitTree;
        }
      } else {
        baseTree = baseCommit.getTree();
      }

      List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
      if (path != null && path.length() > 0) {
        for (DiffEntry diffEntry : diffEntries) {
          if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
            df.format(diffEntry);
            break;
          }
        }
      } else {
        df.format(diffEntries);
      }
      if (df instanceof GitBlitDiffFormatter) {
        // workaround for complex private methods in DiffFormatter
        diff = ((GitBlitDiffFormatter) df).getHtml();
        stat = ((GitBlitDiffFormatter) df).getDiffStat();
      } else {
        diff = os.toString();
      }
      df.flush();
    } catch (Throwable t) {
      try {
        error(t, repository, "failed to generate commit diff!");
      } catch (Exception e) {
      }
    }

    return new DiffOutput(outputType, diff, stat);
  }