Search in sources :

Example 1 with DiffConfig

use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.

the class CommitSelectionDialog method createTreeFilter.

private TreeFilter createTreeFilter() {
    if (filterResources == null)
        return TreeFilter.ALL;
    List<TreeFilter> filters = new ArrayList<>();
    for (IResource resource : filterResources) {
        RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
        if (mapping != null) {
            DiffConfig diffConfig = mapping.getRepository().getConfig().get(DiffConfig.KEY);
            String path = mapping.getRepoRelativePath(resource);
            if (path != null && !"".equals(path)) {
                // $NON-NLS-1$
                if (resource.getType() == IResource.FILE)
                    filters.add(FollowFilter.create(path, diffConfig));
                else
                    filters.add(AndTreeFilter.create(PathFilter.create(path), TreeFilter.ANY_DIFF));
            }
        }
    }
    if (filters.isEmpty())
        return TreeFilter.ALL;
    else if (filters.size() == 1)
        return filters.get(0);
    else
        return OrTreeFilter.create(filters);
}
Also used : ArrayList(java.util.ArrayList) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) DiffConfig(org.eclipse.jgit.diff.DiffConfig) IResource(org.eclipse.core.resources.IResource)

Example 2 with DiffConfig

use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.

the class GitHistoryPage method createFollowFilterFor.

/**
 * Creates a filter for the given files, will make sure that renames/copies
 * of all files will be followed.
 * @param paths the list of files to follow, must not be <code>null</code> or empty
 * @return the ORed list of {@link FollowFilter follow filters}
 */
private TreeFilter createFollowFilterFor(List<String> paths) {
    if (paths == null || paths.isEmpty())
        // $NON-NLS-1$
        throw new IllegalArgumentException("paths must not be null nor empty");
    DiffConfig diffConfig = currentRepo.getConfig().get(DiffConfig.KEY);
    List<TreeFilter> followFilters = new ArrayList<>(paths.size());
    for (String path : paths) followFilters.add(createFollowFilter(path, diffConfig));
    if (followFilters.size() == 1) {
        FollowFilter followFilter = (FollowFilter) followFilters.get(0);
        renameTracker.reset(followFilter.getPath());
        return followFilters.get(0);
    }
    // FollowFilter for rename detection.
    return OrTreeFilter.create(followFilters);
}
Also used : FollowFilter(org.eclipse.jgit.revwalk.FollowFilter) ArrayList(java.util.ArrayList) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) DiffConfig(org.eclipse.jgit.diff.DiffConfig)

Example 3 with DiffConfig

use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.

the class GitDocument method populate.

void populate() throws IOException {
    if (GitTraceLocation.QUICKDIFF.isActive())
        GitTraceLocation.getTrace().traceEntry(GitTraceLocation.QUICKDIFF.getLocation(), resource);
    // Do not populate if already disposed
    if (disposed)
        return;
    RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
    if (mapping == null) {
        // $NON-NLS-1$
        setResolved(null, null, null, "");
        return;
    }
    final String gitPath = mapping.getRepoRelativePath(resource);
    if (gitPath == null) {
        // $NON-NLS-1$
        setResolved(null, null, null, "");
        return;
    }
    final Repository repository = mapping.getRepository();
    String baseline = GitQuickDiffProvider.baseline.get(repository);
    if (baseline == null)
        baseline = Constants.HEAD;
    ObjectId commitId = repository.resolve(baseline);
    if (commitId != null) {
        if (commitId.equals(lastCommit)) {
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
                "(GitDocument) already resolved");
            return;
        }
    } else {
        if (repository.exactRef(Constants.HEAD) == null) {
            // Complain only if not an unborn branch
            String msg = NLS.bind(UIText.GitDocument_errorResolveQuickdiff, new Object[] { baseline, resource, repository });
            Activator.logError(msg, new Throwable());
        }
        // $NON-NLS-1$
        setResolved(null, null, null, "");
        return;
    }
    RevCommit baselineCommit;
    String oldPath = gitPath;
    try (RevWalk rw = new RevWalk(repository);
        ObjectReader reader = repository.newObjectReader()) {
        baselineCommit = rw.parseCommit(commitId);
        DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
        if (diffConfig.getRenameDetectionType() != RenameDetectionType.FALSE) {
            TreeWalk walk = new TreeWalk(repository);
            CanonicalTreeParser baseLineIterator = new CanonicalTreeParser();
            baseLineIterator.reset(reader, baselineCommit.getTree());
            walk.addTree(baseLineIterator);
            walk.addTree(new DirCacheIterator(repository.readDirCache()));
            List<DiffEntry> diffs = DiffEntry.scan(walk, true);
            RenameDetector renameDetector = new RenameDetector(repository);
            renameDetector.addAll(diffs);
            List<DiffEntry> renames = renameDetector.compute();
            for (DiffEntry e : renames) {
                if (e.getNewPath().equals(gitPath)) {
                    oldPath = e.getOldPath();
                    break;
                }
            }
        }
    } catch (IOException err) {
        String msg = NLS.bind(UIText.GitDocument_errorLoadCommit, new Object[] { commitId, baseline, resource, repository });
        Activator.logError(msg, err);
        // $NON-NLS-1$
        setResolved(null, null, null, "");
        return;
    }
    RevTree treeId = baselineCommit.getTree();
    if (treeId.equals(lastTree)) {
        if (GitTraceLocation.QUICKDIFF.isActive())
            GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
            "(GitDocument) already resolved");
        return;
    }
    try (TreeWalk tw = TreeWalk.forPath(repository, oldPath, treeId)) {
        if (tw == null) {
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                "(GitDocument) resource " + resource + " not found in " + treeId + " in " + repository + ", baseline=" + baseline);
            // $NON-NLS-1$
            setResolved(null, null, null, "");
            return;
        }
        ObjectId id = tw.getObjectId(0);
        if (id.equals(ObjectId.zeroId())) {
            // $NON-NLS-1$
            setResolved(null, null, null, "");
            String msg = NLS.bind(UIText.GitDocument_errorLoadTree, new Object[] { treeId.getName(), baseline, resource, repository });
            Activator.logError(msg, new Throwable());
            // $NON-NLS-1$
            setResolved(null, null, null, "");
            return;
        }
        if (!id.equals(lastBlob)) {
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
                "(GitDocument) compareTo: " + baseline);
            ObjectLoader loader = repository.open(id, Constants.OBJ_BLOB);
            byte[] bytes = loader.getBytes();
            String charset;
            charset = CompareCoreUtils.getResourceEncoding(resource);
            // Finally we could consider validating the content with respect
            // to the content. We don't do that here.
            String s = new String(bytes, charset);
            setResolved(commitId, treeId, id, s);
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$ //$NON-NLS-2$
                "(GitDocument) has reference doc, size=" + s.length() + " bytes");
        } else {
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
                "(GitDocument) already resolved");
        }
    } finally {
        if (GitTraceLocation.QUICKDIFF.isActive()) {
            GitTraceLocation.getTrace().traceExit(GitTraceLocation.QUICKDIFF.getLocation());
        }
    }
}
Also used : AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffConfig(org.eclipse.jgit.diff.DiffConfig) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) Repository(org.eclipse.jgit.lib.Repository) RenameDetector(org.eclipse.jgit.diff.RenameDetector) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 4 with DiffConfig

use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.

the class RepositoryActionHandler method getHeadCommit.

protected RevCommit getHeadCommit(IResource resource) throws IOException {
    Repository repository = getRepository();
    if (resource == null) {
        return null;
    }
    RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
    if (mapping == null) {
        return null;
    }
    String path = mapping.getRepoRelativePath(resource);
    if (path == null) {
        return null;
    }
    try (RevWalk rw = new RevWalk(repository)) {
        rw.sort(RevSort.COMMIT_TIME_DESC, true);
        rw.sort(RevSort.BOUNDARY, true);
        if (path.length() > 0) {
            DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
            FollowFilter filter = FollowFilter.create(path, diffConfig);
            rw.setTreeFilter(filter);
        }
        Ref head = repository.findRef(Constants.HEAD);
        if (head == null) {
            return null;
        }
        RevCommit headCommit = rw.parseCommit(head.getObjectId());
        rw.close();
        return headCommit;
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) FollowFilter(org.eclipse.jgit.revwalk.FollowFilter) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffConfig(org.eclipse.jgit.diff.DiffConfig) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 5 with DiffConfig

use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.

the class RepositoryActionHandler method findPreviousCommits.

/**
 * Returns the previous commit of the given resources.
 *
 * @param resources
 *            The {@link IResource} for which the previous commit shall be
 *            determined.
 * @return The second to last commit which touched any of the given
 *         resources.
 * @throws IOException
 *             When the commit can not be parsed.
 */
protected List<RevCommit> findPreviousCommits(Collection<IResource> resources) throws IOException {
    List<RevCommit> result = new ArrayList<>();
    Repository repository = getRepository();
    RepositoryMapping mapping = RepositoryMapping.getMapping(resources.iterator().next().getProject());
    if (mapping == null) {
        return result;
    }
    try (RevWalk rw = new RevWalk(repository)) {
        rw.sort(RevSort.COMMIT_TIME_DESC, true);
        rw.sort(RevSort.BOUNDARY, true);
        List<TreeFilter> filters = new ArrayList<>();
        DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
        for (IResource resource : resources) {
            String path = mapping.getRepoRelativePath(resource);
            if (path != null && path.length() > 0) {
                filters.add(FollowFilter.create(path, diffConfig));
            }
        }
        if (filters.size() >= 2) {
            TreeFilter filter = OrTreeFilter.create(filters);
            rw.setTreeFilter(filter);
        } else if (filters.size() == 1) {
            rw.setTreeFilter(filters.get(0));
        }
        Ref head = repository.findRef(Constants.HEAD);
        if (head == null) {
            return result;
        }
        RevCommit headCommit = rw.parseCommit(head.getObjectId());
        rw.markStart(headCommit);
        headCommit = rw.next();
        if (headCommit == null)
            return result;
        List<RevCommit> directParents = Arrays.asList(headCommit.getParents());
        RevCommit previousCommit = rw.next();
        while (previousCommit != null && result.size() < directParents.size()) {
            if (directParents.contains(previousCommit)) {
                result.add(previousCommit);
            }
            previousCommit = rw.next();
        }
        rw.dispose();
    }
    return result;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ArrayList(java.util.ArrayList) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffConfig(org.eclipse.jgit.diff.DiffConfig) IResource(org.eclipse.core.resources.IResource) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

DiffConfig (org.eclipse.jgit.diff.DiffConfig)5 RepositoryMapping (org.eclipse.egit.core.project.RepositoryMapping)4 ArrayList (java.util.ArrayList)3 Repository (org.eclipse.jgit.lib.Repository)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 OrTreeFilter (org.eclipse.jgit.treewalk.filter.OrTreeFilter)3 TreeFilter (org.eclipse.jgit.treewalk.filter.TreeFilter)3 IResource (org.eclipse.core.resources.IResource)2 Ref (org.eclipse.jgit.lib.Ref)2 FollowFilter (org.eclipse.jgit.revwalk.FollowFilter)2 AndTreeFilter (org.eclipse.jgit.treewalk.filter.AndTreeFilter)2 IOException (java.io.IOException)1 DiffEntry (org.eclipse.jgit.diff.DiffEntry)1 RenameDetector (org.eclipse.jgit.diff.RenameDetector)1 DirCacheIterator (org.eclipse.jgit.dircache.DirCacheIterator)1 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1