Java Code Examples for org.eclipse.jgit.lib.ObjectLoader#copyTo()

The following examples show how to use org.eclipse.jgit.lib.ObjectLoader#copyTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommitUtil.java    From SZZUnleashed with MIT License 6 votes vote down vote up
/**
 * Method to read a file from a specific revision.
 *
 * @param tree the revision tree that contains the file.
 * @param path the path that leads to the file in the tree.
 * @return a list containing all lines in the file.
 */
public List<String> getFileLines(RevTree tree, String path) throws IOException, GitAPIException {

  try (TreeWalk walk = new TreeWalk(this.repo)) {
    walk.addTree(tree);
    walk.setRecursive(true);
    walk.setFilter(PathFilter.create(path));

    walk.next();
    ObjectId oId = walk.getObjectId(0);

    if (oId == ObjectId.zeroId()) {
      return new LinkedList<>();
    }

    ObjectLoader loader = this.repo.open(oId);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    loader.copyTo(stream);

    return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray()), "UTF-8");
  } catch (Exception e) {
    return new LinkedList<>();
  }
}
 
Example 2
Source File: PGA.java    From coming with MIT License 6 votes vote down vote up
private void obtainDiff(Repository repository, RevCommit commit, List<String> paths) throws IOException, GitAPIException {
        // and using commit's tree find the path
        RevTree tree = commit.getTree();
        System.out.println("Having tree: " + tree);

        // now try to find a specific file
        TreeWalk treeWalk = new TreeWalk(repository);
        treeWalk.addTree(tree);
        treeWalk.setRecursive(true);
        for (String path : paths) {
            String filePath = SIVA_COMMITS_DIR + commit.getName() + "/" + path;
            File file = new File(filePath);
            if (!file.exists()) {
                treeWalk.setFilter(PathFilter.create(path));
                if (!treeWalk.next()) {
                    throw new IllegalStateException("Did not find expected file '" + path + "'");
                }

                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);
                // and then one can the loader to read the file
//                loader.copyTo(System.out);
                loader.copyTo(FileUtils.openOutputStream(file));
            }
        }
    }
 
Example 3
Source File: AppraiseGitReviewClient.java    From git-appraise-eclipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Utility method that converts a note to a string (assuming it's UTF-8).
 */
private String noteToString(Repository repo, Note note)
    throws MissingObjectException, IOException, UnsupportedEncodingException {
  ObjectLoader loader = repo.open(note.getData());
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  loader.copyTo(baos);
  return new String(baos.toByteArray(), "UTF-8");
}
 
Example 4
Source File: RepositoryObjectTreeWalker.java    From writelatex-git-bridge with MIT License 5 votes vote down vote up
private Map<String, RawFile> walkGitObjectTree(Optional<Long> maxFileSize)
        throws IOException,
                SizeLimitExceededException,
                InvalidGitRepository {
    Map<String, RawFile> fileContentsTable = new HashMap<>();
    if (treeWalk == null) {
        return fileContentsTable;
    }
    while (treeWalk.next()) {
        String path = treeWalk.getPathString();
        ObjectId objectId = treeWalk.getObjectId(0);
        if (!repository.hasObject(objectId)) {
            throw new InvalidGitRepository();
        }
        ObjectLoader obj = repository.open(objectId);
        long size = obj.getSize();
        if (maxFileSize.isPresent() && size > maxFileSize.get()) {
            throw new SizeLimitExceededException(
                    Optional.ofNullable(path), size, maxFileSize.get());
        }
        try (ByteArrayOutputStream o = new ByteArrayOutputStream(
                CastUtil.assumeInt(size))) {
            obj.copyTo(o);
            fileContentsTable.put(
                    path, new RepositoryFile(path, o.toByteArray()));
        };
    }
    return fileContentsTable;
}