Search in sources :

Example 76 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project OpenGrok by OpenGrok.

the class GitRepository method prepareTreeParser.

private static AbstractTreeIterator prepareTreeParser(org.eclipse.jgit.lib.Repository repository, RevCommit commit) throws IOException {
    // from the commit we can build the tree which allows us to construct the TreeParser
    try (RevWalk walk = new RevWalk(repository)) {
        RevTree tree = walk.parseTree(commit.getTree().getId());
        CanonicalTreeParser treeParser = new CanonicalTreeParser();
        try (ObjectReader reader = repository.newObjectReader()) {
            treeParser.reset(reader, tree.getId());
        }
        walk.dispose();
        return treeParser;
    }
}
Also used : ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Example 77 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project curiostack by curioswitch.

the class CurioGenericCiPlugin method computeAffectedFilesForMaster.

// Assume all tested changes are in a single commit, which works when commits are always squashed.
private static Set<String> computeAffectedFilesForMaster(Git git) throws IOException {
    ObjectId oldTreeId = git.getRepository().resolve("HEAD^{tree}");
    ObjectId newTreeId = git.getRepository().resolve("HEAD^^{tree}");
    final CanonicalTreeParser oldTreeParser;
    final CanonicalTreeParser newTreeParser;
    try (ObjectReader reader = git.getRepository().newObjectReader()) {
        oldTreeParser = parser(reader, oldTreeId);
        newTreeParser = parser(reader, newTreeId);
    }
    return computeAffectedFiles(git, oldTreeParser, newTreeParser);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Example 78 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project egit by eclipse.

the class FileDiff method outputDiff.

/**
 * Creates a textual diff together with meta information.
 * TODO So far this works only in case of one parent commit.
 *
 * @param d
 *            the StringBuilder where the textual diff is added to
 * @param db
 *            the Repo
 * @param diffFmt
 *            the DiffFormatter used to create the textual diff
 * @param gitFormat
 *            if false, do not show any source or destination prefix,
 *            and the paths are calculated relative to the eclipse
 *            project, otherwise relative to the git repository
 * @throws IOException
 */
public void outputDiff(final StringBuilder d, final Repository db, final DiffFormatter diffFmt, boolean gitFormat) throws IOException {
    if (gitFormat) {
        diffFmt.setRepository(db);
        diffFmt.format(diffEntry);
        return;
    }
    try (ObjectReader reader = db.newObjectReader()) {
        outputEclipseDiff(d, db, reader, diffFmt);
    }
}
Also used : ObjectReader(org.eclipse.jgit.lib.ObjectReader)

Example 79 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project egit by eclipse.

the class BlameRevision method calculateDiffToParent.

private Diff calculateDiffToParent(RevCommit parentCommit) {
    try (ObjectReader reader = repository.newObjectReader()) {
        DiffEntry diffEntry = CompareCoreUtils.getChangeDiffEntry(repository, sourcePath, commit, parentCommit, reader);
        if (diffEntry == null)
            return null;
        RawText oldText = readText(diffEntry.getOldId(), reader);
        RawText newText = readText(diffEntry.getNewId(), reader);
        StoredConfig config = repository.getConfig();
        DiffAlgorithm diffAlgorithm = DiffAlgorithm.getAlgorithm(config.getEnum(ConfigConstants.CONFIG_DIFF_SECTION, null, ConfigConstants.CONFIG_KEY_ALGORITHM, SupportedAlgorithm.HISTOGRAM));
        EditList editList = diffAlgorithm.diff(RawTextComparator.DEFAULT, oldText, newText);
        return new Diff(diffEntry.getOldPath(), oldText, newText, editList);
    } catch (IOException e) {
        return null;
    }
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) DiffAlgorithm(org.eclipse.jgit.diff.DiffAlgorithm) ObjectReader(org.eclipse.jgit.lib.ObjectReader) EditList(org.eclipse.jgit.diff.EditList) IOException(java.io.IOException) RawText(org.eclipse.jgit.diff.RawText) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 80 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project fuse-karaf by jboss-fuse.

the class GitPatchRepositoryImpl method getFileContent.

@Override
public String getFileContent(Git fork, String sha1, String fileName) throws IOException {
    ObjectReader objectReader = fork.getRepository().newObjectReader();
    RevCommit commit = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve(sha1));
    TreeWalk tw = new TreeWalk(fork.getRepository());
    tw.addTree(commit.getTree());
    tw.setRecursive(false);
    tw.setFilter(PathFilter.create(fileName));
    if (tw.next()) {
        ObjectId objectId = tw.getObjectId(0);
        ObjectLoader loader = fork.getRepository().open(objectId);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        loader.copyTo(out);
        return new String(out.toByteArray(), "UTF-8");
    }
    return null;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ObjectReader (org.eclipse.jgit.lib.ObjectReader)115 RevWalk (org.eclipse.jgit.revwalk.RevWalk)69 ObjectId (org.eclipse.jgit.lib.ObjectId)59 RevCommit (org.eclipse.jgit.revwalk.RevCommit)56 Repository (org.eclipse.jgit.lib.Repository)47 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)31 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)26 IOException (java.io.IOException)24 Test (org.junit.Test)23 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)20 RevTree (org.eclipse.jgit.revwalk.RevTree)19 DiffEntry (org.eclipse.jgit.diff.DiffEntry)18 Ref (org.eclipse.jgit.lib.Ref)17 PersonIdent (org.eclipse.jgit.lib.PersonIdent)14 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)13 TestRepository (org.eclipse.jgit.junit.TestRepository)12 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 Change (com.google.gerrit.reviewdb.client.Change)10 Map (java.util.Map)10 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)9