Search in sources :

Example 16 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk 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 17 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project che by eclipse.

the class JGitConnection method showFileContent.

@Override
public ShowFileContentResponse showFileContent(String file, String version) throws GitException {
    String content;
    ObjectId revision;
    try {
        revision = getRepository().resolve(version);
        try (RevWalk revWalk = new RevWalk(getRepository())) {
            RevCommit revCommit = revWalk.parseCommit(revision);
            RevTree tree = revCommit.getTree();
            try (TreeWalk treeWalk = new TreeWalk(getRepository())) {
                treeWalk.addTree(tree);
                treeWalk.setRecursive(true);
                treeWalk.setFilter(PathFilter.create(file));
                if (!treeWalk.next()) {
                    throw new GitException("fatal: Path '" + file + "' does not exist in '" + version + "'" + lineSeparator());
                }
                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);
                content = new String(loader.getBytes());
            }
        }
    } catch (IOException exception) {
        throw new GitException(exception.getMessage());
    }
    return newDto(ShowFileContentResponse.class).withContent(content);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) GitException(org.eclipse.che.api.git.exception.GitException) ShowFileContentResponse(org.eclipse.che.api.git.shared.ShowFileContentResponse) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 18 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.

the class JGitUtils method getByteContent.

/**
	 * Retrieves the raw byte content of a file in the specified tree.
	 *
	 * @param repository
	 * @param tree
	 *            if null, the RevTree from HEAD is assumed.
	 * @param path
	 * @return content as a byte []
	 */
public static byte[] getByteContent(Repository repository, RevTree tree, final String path, boolean throwError) {
    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
    byte[] content = null;
    try {
        if (tree == null) {
            ObjectId object = getDefaultBranch(repository);
            if (object == null)
                return null;
            RevCommit commit = rw.parseCommit(object);
            tree = commit.getTree();
        }
        tw.reset(tree);
        while (tw.next()) {
            if (tw.isSubtree() && !path.equals(tw.getPathString())) {
                tw.enterSubtree();
                continue;
            }
            ObjectId entid = tw.getObjectId(0);
            FileMode entmode = tw.getFileMode(0);
            if (entmode != FileMode.GITLINK) {
                ObjectLoader ldr = repository.open(entid, Constants.OBJ_BLOB);
                content = ldr.getCachedBytes();
            }
        }
    } catch (Throwable t) {
        if (throwError) {
            error(t, repository, "{0} can't find {1} in tree {2}", path, tree.name());
        }
    } finally {
        rw.dispose();
        tw.close();
    }
    return content;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 19 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.

the class JGitUtils method getFilesInCommit.

/**
	 * Returns the list of files changed in a specified commit. If the
	 * repository does not exist or is empty, an empty list is returned.
	 *
	 * @param repository
	 * @param commit
	 *            if null, HEAD is assumed.
	 * @param calculateDiffStat
	 *            if true, each PathChangeModel will have insertions/deletions
	 * @return list of files changed in a commit
	 */
public static List<PathChangeModel> getFilesInCommit(Repository repository, RevCommit commit, boolean calculateDiffStat) {
    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()) {
                long size = 0;
                FilestoreModel filestoreItem = null;
                ObjectId objectId = tw.getObjectId(0);
                try {
                    if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {
                        size = tw.getObjectReader().getObjectSize(objectId, Constants.OBJ_BLOB);
                        if (isPossibleFilestoreItem(size)) {
                            filestoreItem = getFilestoreItem(tw.getObjectReader().open(objectId));
                        }
                    }
                } catch (Throwable t) {
                    error(t, null, "failed to retrieve blob size for " + tw.getPathString());
                }
                list.add(new PathChangeModel(tw.getPathString(), tw.getPathString(), filestoreItem, size, tw.getRawMode(0), objectId.getName(), commit.getId().getName(), ChangeType.ADD));
            }
            tw.close();
        } else {
            RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
            DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository);
            df.setRepository(repository);
            df.setDiffComparator(RawTextComparator.DEFAULT);
            df.setDetectRenames(true);
            List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
            for (DiffEntry diff : diffs) {
                // create the path change model
                PathChangeModel pcm = PathChangeModel.from(diff, commit.getName(), repository);
                if (calculateDiffStat) {
                    // update file diffstats
                    df.format(diff);
                    PathChangeModel pathStat = df.getDiffStat().getPath(pcm.path);
                    if (pathStat != null) {
                        pcm.insertions = pathStat.insertions;
                        pcm.deletions = pathStat.deletions;
                    }
                }
                list.add(pcm);
            }
        }
    } catch (Throwable t) {
        error(t, repository, "{0} failed to determine files in commit!");
    } finally {
        rw.dispose();
    }
    return list;
}
Also used : PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) FilestoreModel(com.gitblit.models.FilestoreModel) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 20 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.

the class JGitUtils method getSubmoduleCommitId.

public static String getSubmoduleCommitId(Repository repository, String path, RevCommit commit) {
    String commitId = null;
    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
    try {
        tw.reset(commit.getTree());
        while (tw.next()) {
            if (tw.isSubtree() && !path.equals(tw.getPathString())) {
                tw.enterSubtree();
                continue;
            }
            if (FileMode.GITLINK == tw.getFileMode(0)) {
                commitId = tw.getObjectId(0).getName();
                break;
            }
        }
    } catch (Throwable t) {
        error(t, repository, "{0} can't find {1} in commit {2}", path, commit.name());
    } finally {
        rw.dispose();
        tw.close();
    }
    return commitId;
}
Also used : RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Aggregations

TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)31 RevWalk (org.eclipse.jgit.revwalk.RevWalk)19 RevCommit (org.eclipse.jgit.revwalk.RevCommit)14 ObjectId (org.eclipse.jgit.lib.ObjectId)11 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)9 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)9 ObjectReader (org.eclipse.jgit.lib.ObjectReader)6 Repository (org.eclipse.jgit.lib.Repository)6 FileMode (org.eclipse.jgit.lib.FileMode)5 RevTree (org.eclipse.jgit.revwalk.RevTree)5 PathFilter (org.eclipse.jgit.treewalk.filter.PathFilter)5 FilestoreModel (com.gitblit.models.FilestoreModel)4 PathModel (com.gitblit.models.PathModel)4 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)3 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)3 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)3 RefModel (com.gitblit.models.RefModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2