Search in sources :

Example 31 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project OpenGrok by OpenGrok.

the class GitRepository method getFiles.

/**
 * Assemble list of files that changed between 2 commits.
 * @param repository repository object
 * @param oldCommit parent commit
 * @param newCommit new commit (the mehotd assumes oldCommit is its parent)
 * @param files set of files that changed (excludes renamed files)
 * @param renamedFiles set of renamed files (if renamed handling is enabled)
 * @throws IOException on I/O problem
 */
private void getFiles(org.eclipse.jgit.lib.Repository repository, RevCommit oldCommit, RevCommit newCommit, Set<String> files, Set<String> renamedFiles) throws IOException {
    OutputStream outputStream = NullOutputStream.INSTANCE;
    try (DiffFormatter formatter = new DiffFormatter(outputStream)) {
        formatter.setRepository(repository);
        if (isHandleRenamedFiles()) {
            formatter.setDetectRenames(true);
        }
        List<DiffEntry> diffs = formatter.scan(prepareTreeParser(repository, oldCommit), prepareTreeParser(repository, newCommit));
        for (DiffEntry diff : diffs) {
            if (diff.getChangeType() != DiffEntry.ChangeType.DELETE) {
                if (files != null) {
                    files.add(getNativePath(getDirectoryNameRelative()) + File.separator + getNativePath(diff.getNewPath()));
                }
            }
            if (diff.getChangeType() == DiffEntry.ChangeType.RENAME && isHandleRenamedFiles()) {
                renamedFiles.add(getNativePath(getDirectoryNameRelative()) + File.separator + getNativePath(diff.getNewPath()));
            }
        }
    }
}
Also used : NullOutputStream(org.eclipse.jgit.util.io.NullOutputStream) CountingOutputStream(org.eclipse.jgit.util.io.CountingOutputStream) OutputStream(java.io.OutputStream) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 32 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project omegat by omegat-org.

the class GITRemoteRepository2 method getRecentlyDeletedFiles.

@Override
public String[] getRecentlyDeletedFiles() throws Exception {
    final ArrayList<String> deleted = new ArrayList<>();
    ObjectId head = repository.getRefDatabase().findRef("HEAD").getObjectId();
    String settingKey = "lastDeleteCheckForName" + localDirectory.getName();
    String sinceRevisionString = projectTeamSettings.get(settingKey);
    ObjectId sinceRevision;
    if (sinceRevisionString == null) {
        sinceRevision = head;
    } else {
        sinceRevision = ObjectId.fromString(sinceRevisionString);
    }
    Git git = new Git(repository);
    AbstractTreeIterator startTreeIterator = getTreeIterator(git, sinceRevision);
    AbstractTreeIterator headTreeIterator = new FileTreeIterator(git.getRepository());
    List<DiffEntry> diffEntries = git.diff().setOldTree(startTreeIterator).setNewTree(headTreeIterator).call();
    for (DiffEntry diffEntry : diffEntries) {
        if (diffEntry.getChangeType().equals(DiffEntry.ChangeType.DELETE)) {
            deleted.add(diffEntry.getOldPath().replace('/', File.separatorChar));
        }
    }
    projectTeamSettings.set(settingKey, head.getName());
    String[] result = new String[deleted.size()];
    return deleted.toArray(result);
}
Also used : AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) Git(org.eclipse.jgit.api.Git) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 33 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project che by eclipse.

the class JGitConnection method getCommitDiffFiles.

private List<DiffCommitFile> getCommitDiffFiles(RevCommit revCommit, String pattern) throws IOException {
    List<DiffEntry> diffs;
    TreeFilter filter = null;
    if (!isNullOrEmpty(pattern)) {
        filter = AndTreeFilter.create(PathFilterGroup.createFromStrings(Collections.singleton(pattern)), TreeFilter.ANY_DIFF);
    }
    List<DiffCommitFile> commitFilesList = new ArrayList<>();
    try (TreeWalk tw = new TreeWalk(repository)) {
        tw.setRecursive(true);
        // and to get the list of DiffEntry.
        if (revCommit.getParentCount() > 0) {
            RevCommit parent = parseCommit(revCommit.getParent(0));
            tw.reset(parent.getTree(), revCommit.getTree());
            if (filter != null) {
                tw.setFilter(filter);
            } else {
                tw.setFilter(TreeFilter.ANY_DIFF);
            }
            diffs = DiffEntry.scan(tw);
        } else {
            // list of DiffEntry.
            try (RevWalk rw = new RevWalk(repository);
                DiffFormatter diffFormat = new DiffFormatter(NullOutputStream.INSTANCE)) {
                diffFormat.setRepository(repository);
                if (filter != null) {
                    diffFormat.setPathFilter(filter);
                }
                diffs = diffFormat.scan(new EmptyTreeIterator(), new CanonicalTreeParser(null, rw.getObjectReader(), revCommit.getTree()));
            }
        }
    }
    if (diffs != null) {
        commitFilesList.addAll(diffs.stream().map(diff -> newDto(DiffCommitFile.class).withOldPath(diff.getOldPath()).withNewPath(diff.getNewPath()).withChangeType(diff.getChangeType().name())).collect(Collectors.toList()));
    }
    return commitFilesList;
}
Also used : DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) ArrayList(java.util.ArrayList) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 34 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project che by eclipse.

the class JGitDiffPage method commitToIndex.

/**
     * Show changes between specified revision and index. If
     * <code>commitId == null</code> then view changes between HEAD and index.
     *
     * @param commitId
     *            id of commit, pass <code>null</code> is the same as pass HEAD
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> commitToIndex(String commitId, DiffFormatter formatter) throws IOException {
    if (commitId == null) {
        commitId = Constants.HEAD;
    }
    ObjectId commitA = repository.resolve(commitId);
    if (commitA == null) {
        throw new IllegalArgumentException("Invalid commit id " + commitId);
    }
    RevTree treeA;
    try (RevWalk revWalkA = new RevWalk(repository)) {
        treeA = revWalkA.parseTree(commitA);
    }
    DirCache dirCache = null;
    List<DiffEntry> diff;
    try (ObjectReader reader = repository.newObjectReader()) {
        dirCache = repository.lockDirCache();
        CanonicalTreeParser iterA = new CanonicalTreeParser();
        iterA.reset(reader, treeA);
        DirCacheIterator iterB = new DirCacheIterator(dirCache);
        if (!params.isNoRenames()) {
            // Use embedded RenameDetector it works well with index and
            // revision history.
            formatter.setDetectRenames(true);
            int renameLimit = params.getRenameLimit();
            if (renameLimit > 0) {
                formatter.getRenameDetector().setRenameLimit(renameLimit);
            }
        }
        diff = formatter.scan(iterA, iterB);
    } finally {
        if (dirCache != null) {
            dirCache.unlock();
        }
    }
    return diff;
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 35 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project che by eclipse.

the class JGitDiffPage method indexToWorkingTree.

/**
     * Show changes between index and working tree.
     *
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> indexToWorkingTree(DiffFormatter formatter) throws IOException {
    DirCache dirCache = null;
    ObjectReader reader = repository.newObjectReader();
    List<DiffEntry> diff;
    try {
        dirCache = repository.lockDirCache();
        DirCacheIterator iterA = new DirCacheIterator(dirCache);
        FileTreeIterator iterB = new FileTreeIterator(repository);
        // Seems bug in DiffFormatter when work with working. Disable detect
        // renames by formatter and do it later.
        formatter.setDetectRenames(false);
        diff = formatter.scan(iterA, iterB);
        if (!params.isNoRenames()) {
            // Detect renames.
            RenameDetector renameDetector = createRenameDetector();
            ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB));
            renameDetector.addAll(diff);
            diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE);
        }
    } finally {
        reader.close();
        if (dirCache != null) {
            dirCache.unlock();
        }
    }
    return diff;
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) ContentSource(org.eclipse.jgit.diff.ContentSource) RenameDetector(org.eclipse.jgit.diff.RenameDetector) ObjectReader(org.eclipse.jgit.lib.ObjectReader) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

DiffEntry (org.eclipse.jgit.diff.DiffEntry)66 RevCommit (org.eclipse.jgit.revwalk.RevCommit)24 RevWalk (org.eclipse.jgit.revwalk.RevWalk)23 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)22 ObjectReader (org.eclipse.jgit.lib.ObjectReader)21 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)20 IOException (java.io.IOException)19 Git (org.eclipse.jgit.api.Git)18 ObjectId (org.eclipse.jgit.lib.ObjectId)15 ArrayList (java.util.ArrayList)14 File (java.io.File)13 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)12 RevTree (org.eclipse.jgit.revwalk.RevTree)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Path (java.nio.file.Path)8 Test (org.junit.Test)8 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)7 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)7 Ref (org.eclipse.jgit.lib.Ref)6 Repository (org.eclipse.jgit.lib.Repository)6