org.eclipse.jgit.dircache.DirCacheIterator Java Examples

The following examples show how to use org.eclipse.jgit.dircache.DirCacheIterator. 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: ExportDiffTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testDiffRenameDetectionProblem () throws Exception {
    File file = new File(workDir, "file");
    File renamed = new File(workDir, "renamed");
    write(file, "hey, i will be renamed\n");
    add(file);
    commit(file);

    file.renameTo(renamed);
    write(renamed, "hey, i will be renamed\nand now i am\n");
    ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
    try (OutputStream out = new BufferedOutputStream(baos);
        DiffFormatter formatter = new DiffFormatter(out);) {
        formatter.setRepository(repository);
        formatter.setDetectRenames(true);
        AbstractTreeIterator firstTree = new DirCacheIterator(repository.readDirCache());
        AbstractTreeIterator secondTree = new FileTreeIterator(repository);
        formatter.format(firstTree, secondTree);
        formatter.flush();
    }
    assertFalse(
        "Fixed in JGit, modify and simplify the sources in ExportDiff command",
        baos.toString().contains("similarity index ")
    );
}
 
Example #3
Source File: GitManagerImpl.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private AbstractTreeIterator prepareTreeParser(Repository repository, String ref) throws IOException {
    if ("~~staged~~".equals(ref)) {
        return new DirCacheIterator(DirCache.read(repository));
    } else if ("~~unstaged~~".equals(ref)) {
        return new FileTreeIterator(repository);
    }

    try (RevWalk walk = new RevWalk(repository)) {
        ObjectId commitObjectId = repository.resolve(ref);
        if (commitObjectId == null) {
            throw new GitInvalidRefException(format("invalid git ref %s", ref));
        }

        log.debug("ref: {}, commit id: {}", ref, commitObjectId.toString());

        RevCommit commit = walk.parseCommit(commitObjectId);
        RevTree tree = walk.parseTree(commit.getTree().getId());

        CanonicalTreeParser treeParser = new CanonicalTreeParser();
        try (ObjectReader objectReader = repository.newObjectReader()) {
            treeParser.reset(objectReader, tree.getId());
        }

        return treeParser;
    }
}
 
Example #4
Source File: DiffCalculator.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Map<String, BlobWrapper> getIndexedFileContentMap(Git git, Set<String> filePathSet) throws Exception {
    if (filePathSet == null || filePathSet.isEmpty()) {
        return Collections.emptyMap();
    }
    DirCache index = git.getRepository().readDirCache();
    TreeFilter filter = filePathSet.size() > 1
            ? OrTreeFilter.create(filePathSet.stream()
                    .map(PathFilter::create)
                    .collect(Collectors.toList()))
            : PathFilter.create(filePathSet.iterator().next());
    return getContentMapByTreeAndFilter(git, new DirCacheIterator(index), filter);
}
 
Example #5
Source File: StatusCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Map<String, DiffEntry> detectRenames (Repository repository, DirCache cache, ObjectId commitId) {
    List<DiffEntry> entries;
    try(TreeWalk treeWalk = new TreeWalk(repository);) {
        treeWalk.setRecursive(true);
        treeWalk.reset();
        if (commitId != null) {
            treeWalk.addTree(new RevWalk(repository).parseTree(commitId));
        } else {
            treeWalk.addTree(new EmptyTreeIterator());
        }
        // Index
        treeWalk.addTree(new DirCacheIterator(cache));
        treeWalk.setFilter(TreeFilter.ANY_DIFF);
        entries = DiffEntry.scan(treeWalk);
        RenameDetector d = new RenameDetector(repository);
        d.addAll(entries);
        entries = d.compute();
    } catch (IOException ex) {
        entries = Collections.<DiffEntry>emptyList();
    }
    Map<String, DiffEntry> renames = new HashMap<>();
    for (DiffEntry e : entries) {
        if (e.getChangeType().equals(DiffEntry.ChangeType.COPY) || e.getChangeType().equals(DiffEntry.ChangeType.RENAME)) {
            renames.put(e.getNewPath(), e);
        }
    }
    return renames;
}
 
Example #6
Source File: ExportDiffCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private AbstractTreeIterator getIterator (String commit, ObjectReader or) throws IOException, GitException {
    Repository repository = getRepository();
    switch (commit) {
        case Constants.HEAD:
            return getHeadIterator(or);
        case GitClient.INDEX:
            return new DirCacheIterator(repository.readDirCache());
        case GitClient.WORKING_TREE:
            return new FileTreeIterator(repository);
        default:
            CanonicalTreeParser p = new CanonicalTreeParser();
            p.reset(or, Utils.findCommit(repository, commit).getTree());
            return p;
    }
}
 
Example #7
Source File: CheckoutRevisionCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void mergeConflicts (List<String> conflicts, DirCache cache) throws GitException {
    DirCacheBuilder builder = cache.builder();
    DirCacheBuildIterator dci = new DirCacheBuildIterator(builder);
    ObjectDatabase od = null;
    DiffAlgorithm.SupportedAlgorithm diffAlg = getRepository().getConfig().getEnum(
                    ConfigConstants.CONFIG_DIFF_SECTION, null,
                    ConfigConstants.CONFIG_KEY_ALGORITHM,
                    DiffAlgorithm.SupportedAlgorithm.HISTOGRAM);
    MergeAlgorithm merger = new MergeAlgorithm(DiffAlgorithm.getAlgorithm(diffAlg));
    try (TreeWalk walk = new TreeWalk(getRepository());) {
        od = getRepository().getObjectDatabase();
        walk.addTree(dci);
        walk.setFilter(PathFilterGroup.create(Utils.getPathFilters(conflicts)));
        String lastPath = null;
        DirCacheEntry[] entries = new DirCacheEntry[3];
        walk.setRecursive(true);
        while (walk.next()) {
            DirCacheEntry e = walk.getTree(0, DirCacheIterator.class).getDirCacheEntry();
            String path = e.getPathString();
            if (lastPath != null && !lastPath.equals(path)) {
                resolveEntries(merger, lastPath, entries, od, builder);
            }
            if (e.getStage() == 0) {
                DirCacheIterator c = walk.getTree(0, DirCacheIterator.class);
                builder.add(c.getDirCacheEntry());
            } else {
                entries[e.getStage() - 1] = e;
                lastPath = path;
            }
        }
        resolveEntries(merger, lastPath, entries, od, builder);
        builder.commit();
    } catch (IOException ex) {
        throw new GitException(ex);
    } finally {
        if (od != null) {
            od.close();
        }
    }
}
 
Example #8
Source File: CheckoutIndex.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void checkout() throws IOException, GitException {
    try (ObjectReader od = repository.newObjectReader();
        TreeWalk treeWalk = new TreeWalk(repository);) {
        Collection<String> relativePaths = Utils.getRelativePaths(repository.getWorkTree(), roots);
        if (!relativePaths.isEmpty()) {
            treeWalk.setFilter(PathFilterGroup.createFromStrings(relativePaths));
        }
        treeWalk.setRecursive(true);
        treeWalk.reset();
        treeWalk.addTree(new DirCacheIterator(cache));
        treeWalk.addTree(new FileTreeIterator(repository));
        String lastAddedPath = null;
        while (treeWalk.next() && !monitor.isCanceled()) {
            File path = new File(repository.getWorkTree(), treeWalk.getPathString());
            if (treeWalk.getPathString().equals(lastAddedPath)) {
                // skip conflicts
                continue;
            } else {
                lastAddedPath = treeWalk.getPathString();
            }
            DirCacheIterator dit = treeWalk.getTree(0, DirCacheIterator.class);
            FileTreeIterator fit = treeWalk.getTree(1, FileTreeIterator.class);
            if (dit != null && (recursively || directChild(roots, repository.getWorkTree(), path)) && (fit == null || fit.isModified(dit.getDirCacheEntry(), checkContent, od))) {
                // update entry
                listener.notifyFile(path, treeWalk.getPathString());
                checkoutEntry(repository, path, dit.getDirCacheEntry(), od);
            }
        }
    }
}
 
Example #9
Source File: GitRepository.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
private Map<String, Change<?>> blockingPreviewDiff(Revision baseRevision, Iterable<Change<?>> changes) {
    requireNonNull(baseRevision, "baseRevision");
    requireNonNull(changes, "changes");
    baseRevision = normalizeNow(baseRevision);

    readLock();
    try (ObjectReader reader = jGitRepository.newObjectReader();
         RevWalk revWalk = newRevWalk(reader);
         DiffFormatter diffFormatter = new DiffFormatter(null)) {

        final ObjectId baseTreeId = toTree(revWalk, baseRevision);
        final DirCache dirCache = DirCache.newInCore();
        final int numEdits = applyChanges(baseRevision, baseTreeId, dirCache, changes);
        if (numEdits == 0) {
            return Collections.emptyMap();
        }

        final CanonicalTreeParser p = new CanonicalTreeParser();
        p.reset(reader, baseTreeId);
        diffFormatter.setRepository(jGitRepository);
        final List<DiffEntry> result = diffFormatter.scan(p, new DirCacheIterator(dirCache));
        return toChangeMap(result);
    } catch (IOException e) {
        throw new StorageException("failed to perform a dry-run diff", e);
    } finally {
        readUnlock();
    }
}
 
Example #10
Source File: GitStagedFiles.java    From git-code-format-maven-plugin with MIT License 4 votes vote down vote up
private AbstractTreeIterator treeIterator(DirCache dirCache) {
  return new DirCacheIterator(dirCache);
}
 
Example #11
Source File: Index.java    From git-code-format-maven-plugin with MIT License 4 votes vote down vote up
public AbstractTreeIterator treeIterator() {
  return new DirCacheIterator(dirCache);
}
 
Example #12
Source File: GfsDefaultCheckout.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
public void checkout(DirCache cache) throws IOException {
  checkout(new DirCacheIterator(cache));
}
 
Example #13
Source File: GfsDefaultCheckout.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
public void checkout(DirCache cache) throws IOException {
  checkout(new DirCacheIterator(cache));
}