Search in sources :

Example 71 with ObjectReader

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

the class GitPatchRepositoryImpl method diff.

@Override
public List<DiffEntry> diff(Git git, RevCommit commit1, RevCommit commit2, boolean showNameAndStatusOnly) throws GitAPIException, IOException {
    ObjectReader reader = git.getRepository().newObjectReader();
    CanonicalTreeParser ctp1 = new CanonicalTreeParser();
    CanonicalTreeParser ctp2 = new CanonicalTreeParser();
    if (commit1.getTree() == null) {
        commit1 = new RevWalk(git.getRepository()).parseCommit(commit1);
    }
    if (commit2.getTree() == null) {
        commit2 = new RevWalk(git.getRepository()).parseCommit(commit2);
    }
    ctp1.reset(reader, commit1.getTree());
    ctp2.reset(reader, commit2.getTree());
    return git.diff().setShowNameAndStatusOnly(showNameAndStatusOnly).setOldTree(ctp1).setNewTree(ctp2).call();
}
Also used : ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Example 72 with ObjectReader

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

the class GitConflictResolutionIT method reportingDiffs.

@Test
public void reportingDiffs() throws Exception {
    prepareChanges();
    ObjectReader reader = git.getRepository().newObjectReader();
    RevWalk rw = new RevWalk(git.getRepository());
    CanonicalTreeParser ctp1 = new CanonicalTreeParser();
    CanonicalTreeParser ctp2 = new CanonicalTreeParser();
    CanonicalTreeParser ctp3 = new CanonicalTreeParser();
    ctp1.reset(reader, rw.parseCommit(git.getRepository().resolve("master")).getTree());
    ctp2.reset(reader, rw.parseCommit(git.getRepository().resolve("custom")).getTree());
    ctp3.reset(reader, rw.parseCommit(git.getRepository().resolve("patched")).getTree());
    TreeWalk walk = new TreeWalk(reader);
    walk.addTree(ctp1);
    walk.addTree(ctp3);
    walk.setRecursive(true);
    List<DiffEntry> diffs = DiffEntry.scan(walk);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DiffFormatter df = new DiffFormatter(baos);
    df.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM));
    df.setDiffComparator(RawTextComparator.DEFAULT);
    df.setRepository(git.getRepository());
    df.format(diffs.get(0));
    // System.out.println(new String(baos.toByteArray()));
    AbbreviatedObjectId id1 = diffs.get(0).getOldId();
    AbbreviatedObjectId id2 = diffs.get(0).getNewId();
    byte[] bytes1 = reader.open(id1.toObjectId()).getBytes();
    byte[] bytes2 = reader.open(id2.toObjectId()).getBytes();
    RawText rt1 = new RawText(bytes1);
    RawText rt2 = new RawText(bytes2);
    EditList edits = DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM).diff(RawTextComparator.DEFAULT, rt1, rt2);
    int aCur = 0;
    for (Edit curEdit : edits) {
        boolean prolog = aCur < curEdit.getBeginA();
        if (prolog) {
            System.out.print("<div class=\"edit unchanged\">");
        }
        while (aCur < curEdit.getBeginA()) {
            System.out.println(rt1.getString(aCur++));
        }
        if (prolog) {
            System.out.print("</div>");
        }
        if (curEdit.getType() == Edit.Type.INSERT) {
            System.out.print("<div class=\"edit added\">");
            for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
                System.out.println(rt2.getString(i));
            }
            System.out.print("</div>");
        }
        if (curEdit.getType() == Edit.Type.REPLACE) {
            System.out.print("<div class=\"edit changed\"><div class=\"edit removed\">");
            for (int i = curEdit.getBeginA(); i < curEdit.getEndA(); i++) {
                System.out.println(rt1.getString(i));
            }
            System.out.print("</div><div class=\"edit added\">");
            for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
                System.out.println(rt2.getString(i));
            }
            aCur = curEdit.getEndA();
            System.out.print("</div></div>");
        }
    }
    boolean prolog = aCur < rt1.size();
    if (prolog) {
        System.out.print("<div class=\"edit unchanged\">");
    }
    while (aCur < rt1.size()) {
        System.out.println(rt1.getString(aCur++));
    }
    if (prolog) {
        System.out.print("</div>");
    }
}
Also used : Edit(org.eclipse.jgit.diff.Edit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RawText(org.eclipse.jgit.diff.RawText) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) EditList(org.eclipse.jgit.diff.EditList) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) DiffEntry(org.eclipse.jgit.diff.DiffEntry) Test(org.junit.Test)

Example 73 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project searchcode-server by boyter.

the class GitService method fetchFileRevision.

/**
 * Given a repository location, revision and file path will retrieve that files contents. N.B. it returns the whole
 * file so you MAY end up running into serious memory issues, and should be aware of this
 */
public String fetchFileRevision(String repoLocation, String revision, String filePath) throws MissingObjectException, IncorrectObjectTypeException, IOException {
    Repository localRepository = new FileRepository(new File(repoLocation));
    ObjectId id = localRepository.resolve(revision);
    ObjectReader reader = localRepository.newObjectReader();
    try {
        RevWalk walk = new RevWalk(reader);
        RevCommit commit = walk.parseCommit(id);
        RevTree tree = commit.getTree();
        TreeWalk treewalk = TreeWalk.forPath(reader, filePath, tree);
        if (treewalk != null) {
            byte[] data = reader.open(treewalk.getObjectId(0)).getBytes();
            return new String(data, "utf-8");
        } else {
            return "";
        }
    } finally {
        reader.close();
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 74 with ObjectReader

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

the class GitCommitProvider method read.

private final String read(Repository repository, RevCommit commit, String path) throws IOException {
    try (TreeWalk treeWalk = TreeWalk.forPath(repository, path, commit.getTree())) {
        ObjectId blobId = treeWalk.getObjectId(0);
        try (ObjectReader objectReader = repository.newObjectReader()) {
            ObjectLoader objectLoader = objectReader.open(blobId);
            byte[] bytes = objectLoader.getBytes();
            return new String(bytes, StandardCharsets.UTF_8);
        }
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 75 with ObjectReader

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

the class GitRepository method determineCurrentVersion.

@Override
public String determineCurrentVersion(CommandTimeoutType cmdType) throws IOException {
    try (org.eclipse.jgit.lib.Repository repository = getJGitRepository(getDirectoryName())) {
        Ref head = repository.exactRef(Constants.HEAD);
        if (head != null && head.getObjectId() != null) {
            try (RevWalk walk = new RevWalk(repository);
                ObjectReader reader = repository.newObjectReader()) {
                RevCommit commit = walk.parseCommit(head.getObjectId());
                int commitTime = commit.getCommitTime();
                Date date = new Date((long) (commitTime) * 1000);
                return String.format("%s %s %s %s", format(date), reader.abbreviate(head.getObjectId()).name(), commit.getAuthorIdent().getName(), commit.getShortMessage());
            }
        }
    }
    return null;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Repository(org.eclipse.jgit.lib.Repository) Date(java.util.Date) 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