org.eclipse.jgit.treewalk.EmptyTreeIterator Java Examples

The following examples show how to use org.eclipse.jgit.treewalk.EmptyTreeIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UIGit.java    From hop with Apache License 2.0 6 votes vote down vote up
private AbstractTreeIterator getTreeIterator( String commitId ) throws Exception {
  if ( commitId == null ) {
    return new EmptyTreeIterator();
  }
  if ( commitId.equals( WORKINGTREE ) ) {
    return new FileTreeIterator( git.getRepository() );
  } else if ( commitId.equals( INDEX ) ) {
    return new DirCacheIterator( git.getRepository().readDirCache() );
  } else {
    ObjectId id = git.getRepository().resolve( commitId );
    if ( id == null ) { // commitId does not exist
      return new EmptyTreeIterator();
    } else {
      CanonicalTreeParser treeIterator = new CanonicalTreeParser();
      try ( RevWalk rw = new RevWalk( git.getRepository() ) ) {
        RevTree tree = rw.parseTree( id );
        try ( ObjectReader reader = git.getRepository().newObjectReader() ) {
          treeIterator.reset( reader, tree.getId() );
        }
      }
      return treeIterator;
    }
  }
}
 
Example #2
Source File: ThreeWayMerger.java    From onedev with MIT License 5 votes vote down vote up
/**
 * Create an iterator to walk the merge base.
 *
 * @return an iterator over the caller-specified merge base, or the natural
 *         merge base of the two input commits.
 * @throws java.io.IOException
 */
protected AbstractTreeIterator mergeBase() throws IOException {
	if (baseTree != null) {
		return openTree(baseTree);
	}
	RevCommit baseCommit = (baseCommitId != null) ? walk
			.parseCommit(baseCommitId) : getBaseCommit(sourceCommits[0],
			sourceCommits[1]);
	if (baseCommit == null) {
		baseCommitId = null;
		return new EmptyTreeIterator();
	}
	baseCommitId = baseCommit.toObjectId();
	return openTree(baseCommit.getTree());
}
 
Example #3
Source File: ExportCommitCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void run() throws GitException {
    Repository repository = getRepository();
    String workTreePath = repository.getWorkTree().getAbsolutePath();
    RevCommit commit = Utils.findCommit(repository, revisionStr);
    if (commit.getParentCount() > 1) {
        throw new GitException("Unable to export a merge commit");
    }
    try (DiffFormatter formatter = new DiffFormatter(out)) {
        out.write(Constants.encode(formatCommitInfo(commit)));
        formatter.setRepository(repository);
        List<DiffEntry> diffEntries;
        if (commit.getParentCount() > 0) {
            formatter.setDetectRenames(true);
            diffEntries = formatter.scan(commit.getParent(0), commit);
        } else {
            TreeWalk walk = new TreeWalk(repository);
            walk.reset();
            walk.setRecursive(true);
            walk.addTree(new EmptyTreeIterator());
            walk.addTree(commit.getTree());
            walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, PathFilter.ANY_DIFF));
            diffEntries = DiffEntry.scan(walk);
        }
        for (DiffEntry ent : diffEntries) {
            if (monitor.isCanceled()) {
                break;
            }
            listener.notifyFile(new File(workTreePath + File.separator + ent.getNewPath()), ent.getNewPath());
            formatter.format(ent);
        }
        formatter.flush();
    } catch (IOException ex) {
        throw new GitException(ex);
    }
}
 
Example #4
Source File: RecursiveMerger.java    From onedev with MIT License 4 votes vote down vote up
/**
 * Get a single base commit for two given commits. If the two source commits
 * have more than one base commit recursively merge the base commits
 * together until a virtual common base commit has been found.
 *
 * @param a
 *            the first commit to be merged
 * @param b
 *            the second commit to be merged
 * @param callDepth
 *            the callDepth when this method is called recursively
 * @return the merge base of two commits. If a criss-cross merge required a
 *         synthetic merge base this commit is visible only the merger's
 *         RevWalk and will not be in the repository.
 * @throws java.io.IOException
 * @throws IncorrectObjectTypeException
 *             one of the input objects is not a commit.
 * @throws NoMergeBaseException
 *             too many merge bases are found or the computation of a common
 *             merge base failed (e.g. because of a conflict).
 */
protected RevCommit getBaseCommit(RevCommit a, RevCommit b, int callDepth)
		throws IOException {
	ArrayList<RevCommit> baseCommits = new ArrayList<>();
	walk.reset();
	walk.setRevFilter(RevFilter.MERGE_BASE);
	walk.markStart(a);
	walk.markStart(b);
	RevCommit c;
	while ((c = walk.next()) != null)
		baseCommits.add(c);

	if (baseCommits.isEmpty())
		return null;
	if (baseCommits.size() == 1)
		return baseCommits.get(0);
	if (baseCommits.size() >= MAX_BASES)
		throw new NoMergeBaseException(NoMergeBaseException.MergeBaseFailureReason.TOO_MANY_MERGE_BASES, MessageFormat.format(
				JGitText.get().mergeRecursiveTooManyMergeBasesFor,
				Integer.valueOf(MAX_BASES), a.name(), b.name(),
						Integer.valueOf(baseCommits.size())));

	// We know we have more than one base commit. We have to do merges now
	// to determine a single base commit. We don't want to spoil the current
	// dircache and working tree with the results of this intermediate
	// merges. Therefore set the dircache to a new in-memory dircache and
	// disable that we update the working-tree. We set this back to the
	// original values once a single base commit is created.
	RevCommit currentBase = baseCommits.get(0);
	DirCache oldDircache = dircache;
	boolean oldIncore = inCore;
	WorkingTreeIterator oldWTreeIt = workingTreeIterator;
	workingTreeIterator = null;
	try {
		dircache = DirCache.read(reader, currentBase.getTree());
		inCore = true;

		List<RevCommit> parents = new ArrayList<>();
		parents.add(currentBase);
		for (int commitIdx = 1; commitIdx < baseCommits.size(); commitIdx++) {
			RevCommit nextBase = baseCommits.get(commitIdx);
			if (commitIdx >= MAX_BASES)
				throw new NoMergeBaseException(
						NoMergeBaseException.MergeBaseFailureReason.TOO_MANY_MERGE_BASES,
						MessageFormat.format(
						JGitText.get().mergeRecursiveTooManyMergeBasesFor,
						Integer.valueOf(MAX_BASES), a.name(), b.name(),
								Integer.valueOf(baseCommits.size())));
			parents.add(nextBase);
			RevCommit bc = getBaseCommit(currentBase, nextBase,
					callDepth + 1);
			AbstractTreeIterator bcTree = (bc == null) ? new EmptyTreeIterator()
					: openTree(bc.getTree());
			if (mergeTrees(bcTree, currentBase.getTree(),
					nextBase.getTree(), true))
				currentBase = createCommitForTree(resultTree, parents);
			else
				throw new NoMergeBaseException(
						NoMergeBaseException.MergeBaseFailureReason.CONFLICTS_DURING_MERGE_BASE_CALCULATION,
						MessageFormat.format(
								JGitText.get().mergeRecursiveConflictsWhenMergingCommonAncestors,
								currentBase.getName(), nextBase.getName()));
		}
	} finally {
		inCore = oldIncore;
		dircache = oldDircache;
		workingTreeIterator = oldWTreeIt;
		toBeCheckedOut.clear();
		toBeDeleted.clear();
		modifiedFiles.clear();
		unmergedPaths.clear();
		mergeResults.clear();
		failingPaths.clear();
	}
	return currentBase;
}