Example usage for org.eclipse.jgit.lib Repository newObjectReader

List of usage examples for org.eclipse.jgit.lib Repository newObjectReader

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository newObjectReader.

Prototype

@NonNull
public ObjectReader newObjectReader() 

Source Link

Document

Create a new reader to read objects from #getObjectDatabase() .

Usage

From source file:ShowFileDiff.java

License:Apache License

private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId)
        throws IOException, MissingObjectException, IncorrectObjectTypeException {
    // from the commit we can build the tree which allows us to construct the TreeParser
    RevWalk walk = new RevWalk(repository);
    RevCommit commit = walk.parseCommit(ObjectId.fromString(objectId));
    RevTree tree = walk.parseTree(commit.getTree().getId());

    CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
    ObjectReader oldReader = repository.newObjectReader();
    try {// w  w w  . j a v a 2 s .  c o  m
        oldTreeParser.reset(oldReader, tree.getId());
    } finally {
        oldReader.release();
    }

    walk.dispose();

    return oldTreeParser;
}

From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java

License:Open Source License

/**
 * loads all patches of from the given list of {@link DiffEntry}s.
 * // w ww .  j  a v  a 2 s  .c  o  m
 * @param patchSet
 *            the patchSet to add the patches to.
 * @param ref
 *            the ref to the commit of the patch set.
 * @param repository
 *            the git repository instance
 * @throws RepositoryIOException
 */
private void loadPatches(PatchSet patchSet, Ref ref, Git git) throws RepositoryIOException {

    EList<Patch> patches = patchSet.getPatches();
    Repository repository = git.getRepository();

    try {

        RevWalk revWalk = new RevWalk(repository);
        RevCommit newCommit = revWalk.parseCommit(ref.getObjectId());
        RevCommit oldCommit = newCommit.getParent(0);
        revWalk.parseHeaders(oldCommit);
        ObjectReader objectReader = repository.newObjectReader();
        revWalk.close();

        CanonicalTreeParser newTreeIterator = new CanonicalTreeParser();
        newTreeIterator.reset(objectReader, newCommit.getTree().getId());
        CanonicalTreeParser oldTreeIterator = new CanonicalTreeParser();
        oldTreeIterator.reset(objectReader, oldCommit.getTree().getId());

        List<DiffEntry> diffs = git.diff().setOldTree(oldTreeIterator).setNewTree(newTreeIterator).call();

        for (DiffEntry diff : diffs) {

            String newPath = diff.getNewPath();
            String oldPath = diff.getOldPath();
            Patch patch = null;

            /*
             * only papyrus diagrams are supported for now, so models are in
             * .uml files, diagrams in .notation files
             */

            if (diff.getChangeType() != ChangeType.DELETE) {
                if (newPath.endsWith(".uml")) {
                    patch = modelReviewFactory.createModelPatch();

                } else if (newPath.endsWith(".notation")) {
                    patch = modelReviewFactory.createDiagramPatch();
                } else {
                    patch = modelReviewFactory.createPatch();
                }
            } else {
                if (oldPath.endsWith(".uml")) {
                    patch = modelReviewFactory.createModelPatch();

                } else if (oldPath.endsWith(".notation")) {
                    patch = modelReviewFactory.createDiagramPatch();
                } else {
                    patch = modelReviewFactory.createPatch();
                }
            }

            switch (diff.getChangeType()) {
            case ADD:
                patch.setChangeType(PatchChangeType.ADD);
                break;
            case COPY:
                patch.setChangeType(PatchChangeType.COPY);
                break;
            case DELETE:
                patch.setChangeType(PatchChangeType.DELETE);
                break;
            case MODIFY:
                patch.setChangeType(PatchChangeType.MODIFY);
                break;
            case RENAME:
                patch.setChangeType(PatchChangeType.RENAME);
                break;
            }

            patch.setNewPath(newPath);
            patch.setOldPath(oldPath);

            if (diff.getChangeType() != ChangeType.DELETE) {
                ObjectLoader objectLoader = repository.open(diff.getNewId().toObjectId());
                patch.setNewContent(objectLoader.getBytes());
            }
            if (diff.getChangeType() != ChangeType.ADD) {
                ObjectLoader objectLoader = repository.open(diff.getOldId().toObjectId());
                patch.setOldContent(objectLoader.getBytes());
            }
            patches.add(patch);

        }

    } catch (IOException e) {
        throw new RepositoryIOException(MessageFormat.format(
                "An IO error occured during loading the patches for patch set #{0}", patchSet.getId()), e);
    } catch (GitAPIException e) {
        throw new RepositoryIOException(
                MessageFormat.format("An JGit API error occured during loading the patches for patch set #{0}",
                        patchSet.getId()),
                e);
    }
}

From source file:br.com.metricminer2.scm.GitRepository.java

License:Apache License

private String getSourceCode(Repository repo, DiffEntry diff)
        throws MissingObjectException, IOException, UnsupportedEncodingException {

    try {//from  w ww  . j av a 2  s. c om
        ObjectReader reader = repo.newObjectReader();
        byte[] bytes = reader.open(diff.getNewId().toObjectId()).getBytes();
        return new String(bytes, "utf-8");
    } catch (Throwable e) {
        return "";
    }
}

From source file:br.edu.ifpb.scm.api.loads.Loader.java

public static void showFileDiffs(Git gitRepository) {
    Repository repo = gitRepository.getRepository();

    try {//from   w  w w  .ja va2  s  . c  o  m
        ObjectId head = ObjectId.fromString("c24af304077e4a6d1925db7cd35d0cd1ed488d6a");
        ObjectId previousHead = ObjectId.fromString("c16be41e77bb53a4b639cb864c9a6e4d0f8df7c2");
        ObjectReader reader = repo.newObjectReader();

        CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
        oldTreeIter.reset(reader, previousHead);
        CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
        newTreeIter.reset(reader, head);

        List<DiffEntry> listDiffs = gitRepository.diff().setOldTree(oldTreeIter).setNewTree(newTreeIter).call();

        listDiffs.stream().forEach((DiffEntry diff) -> {

            DiffFormatter formatter = new DiffFormatter(System.out);
            formatter.setRepository(repo);
            System.out.println(diff);
            try {
                formatter.format(diff);
            } catch (IOException ex) {
                Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
    } catch (IOException | GitAPIException ex) {
        Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:br.edu.ifpb.scm.api.loads.LoaderVersions.java

public static void teste2() throws IOException, GitAPIException {
    org.eclipse.jgit.api.Git git = org.eclipse.jgit.api.Git.open(PATH);
    org.eclipse.jgit.lib.Repository repository = git.getRepository();

    repository.findRef(URL);/* www .j a  va2 s  . com*/

    ObjectId oldHead = repository.resolve("HEAD~^{tree}");
    ObjectId newHead = repository.resolve("HEAD^{tree}");

    ObjectReader reader = repository.newObjectReader();
    CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
    //        ObjectId oldTree = git.getRepository().resolve("SHA-1{64c852a8fe9e3673aa381f95c4b0420986d1f925}");

    CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
    //        ObjectId newTree = git.getRepository().resolve("SHA-1{12ae7a9960c49cfe68bdd5f7b0a58e1b3b0c6e56}");

    oldTreeIter.reset(reader, oldHead);
    newTreeIter.reset(reader, newHead);

    try (org.eclipse.jgit.api.Git g = new org.eclipse.jgit.api.Git(repository)) {
        List<DiffEntry> diffs = g.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();
        for (DiffEntry entry : diffs) {
            System.out.println("Entry: " + entry);
        }
    }

    //        DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE);
    //        diffFormatter.setRepository(git.getRepository());
    //        List<DiffEntry> entries = diffFormatter.scan(oldTreeIter, newTreeIter);
    //
    //        entries.stream().forEach((entry) -> {
    //            System.out.println(entry.getChangeType());
    //        });
}

From source file:ch.uzh.ifi.seal.permo.history.git.core.model.jgit.JGitCommit.java

License:Apache License

private AbstractTreeIterator getTreeIteratorOfCommit(final Repository repo, final RevCommit commit)
        throws IncorrectObjectTypeException, IOException {
    final ObjectId head = commit.getTree().getId();
    final ObjectReader reader = repo.newObjectReader();
    final CanonicalTreeParser treeIt = new CanonicalTreeParser();
    treeIt.reset(reader, head);/* w  w w .j ava 2s  . c om*/
    return treeIt;
}

From source file:co.bledo.gitmin.servlet.Review.java

License:Apache License

private AbstractTreeIterator getTreeIterator(Repository repo, String name) throws IOException {
    final ObjectId id = repo.resolve(name);
    if (id == null)
        throw new IllegalArgumentException(name);
    final CanonicalTreeParser p = new CanonicalTreeParser();
    final ObjectReader or = repo.newObjectReader();
    try {/*from w w  w .j a v a 2s .  c  o m*/
        p.reset(or, new RevWalk(repo).parseTree(id));
        return p;
    } finally {
        or.release();
    }
}

From source file:com.buildautomation.jgit.api.ShowBranchDiff.java

License:Apache License

private static AbstractTreeIterator prepareTreeParser(Repository repository, String ref)
        throws IOException, MissingObjectException, IncorrectObjectTypeException {
    // from the commit we can build the tree which allows us to construct the TreeParser
    Ref head = repository.exactRef(ref);
    try (RevWalk walk = new RevWalk(repository)) {
        RevCommit commit = walk.parseCommit(head.getObjectId());
        RevTree tree = walk.parseTree(commit.getTree().getId());

        CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
        try (ObjectReader oldReader = repository.newObjectReader()) {
            oldTreeParser.reset(oldReader, tree.getId());
        }//from   ww  w  .  j a  v  a  2  s .c  om

        walk.dispose();

        return oldTreeParser;
    }
}

From source file:com.buildautomation.jgit.api.ShowFileDiff.java

License:Apache License

private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId)
        throws IOException, MissingObjectException, IncorrectObjectTypeException {
    // from the commit we can build the tree which allows us to construct the TreeParser
    try (RevWalk walk = new RevWalk(repository)) {
        RevCommit commit = walk.parseCommit(ObjectId.fromString(objectId));
        RevTree tree = walk.parseTree(commit.getTree().getId());

        CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
        try (ObjectReader oldReader = repository.newObjectReader()) {
            oldTreeParser.reset(oldReader, tree.getId());
        }/*from   ww w  .  jav  a2 s  .co  m*/

        walk.dispose();

        return oldTreeParser;
    }
}

From source file:com.creactiviti.piper.core.git.JGitTemplate.java

License:Apache License

private List<IdentifiableResource> getHeadFiles(Repository aRepository, String... aSearchPaths) {
    List<String> searchPaths = Arrays.asList(aSearchPaths);
    List<IdentifiableResource> resources = new ArrayList<>();
    try (ObjectReader reader = aRepository.newObjectReader();
            RevWalk walk = new RevWalk(reader);
            TreeWalk treeWalk = new TreeWalk(aRepository, reader);) {
        final ObjectId id = aRepository.resolve(Constants.HEAD);
        RevCommit commit = walk.parseCommit(id);
        RevTree tree = commit.getTree();
        treeWalk.addTree(tree);//from  w ww .j a va  2 s  .co m
        treeWalk.setRecursive(true);
        while (treeWalk.next()) {
            String path = treeWalk.getPathString();
            if (searchPaths.stream().anyMatch((sp) -> path.startsWith(sp))) {
                ObjectId objectId = treeWalk.getObjectId(0);
                logger.debug("Loading {} [{}]", path, objectId.name());
                resources.add(readBlob(aRepository, path.substring(0, path.indexOf('.')), objectId.name()));
            }
        }
        return resources;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}