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

The following examples show how to use org.eclipse.jgit.lib.ObjectLoader#openStream() . 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: LfsFilter.java    From git-as-svn with GNU General Public License v2.0 6 votes vote down vote up
@NotNull
@Override
public String getMd5(@NotNull GitObject<? extends ObjectId> objectId) throws IOException {
  final ObjectLoader loader = objectId.openObject();

  try (ObjectStream stream = loader.openStream()) {
    final Meta meta = parseMeta(stream);
    if (meta != null) {
      final String md5 = getReader(meta).getMd5();
      if (md5 != null)
        return md5;
    }
  }

  return GitFilterHelper.getMd5(this, cacheMd5, null, objectId);
}
 
Example 2
Source File: LfsFilter.java    From git-as-svn with GNU General Public License v2.0 6 votes vote down vote up
@NotNull
@Override
public InputStream inputStream(@NotNull GitObject<? extends ObjectId> objectId) throws IOException {
  final ObjectLoader loader = objectId.openObject();
  try (ObjectStream stream = loader.openStream()) {
    final byte[] header = new byte[Constants.POINTER_MAX_SIZE];
    int length = IOUtils.read(stream, header, 0, header.length);
    if (length < header.length) {
      final Meta meta = parseMeta(header, length);
      if (meta != null)
        return getReader(meta).openStream();
    }

    // We need to re-open stream
    return loader.openStream();
  }
}
 
Example 3
Source File: Project.java    From onedev with MIT License 5 votes vote down vote up
public InputStream getInputStream(BlobIdent ident) {
	try (RevWalk revWalk = new RevWalk(getRepository())) {
		ObjectId commitId = getObjectId(ident.revision, true);
		RevTree revTree = revWalk.parseCommit(commitId).getTree();
		TreeWalk treeWalk = TreeWalk.forPath(getRepository(), ident.path, revTree);
		if (treeWalk != null) {
			ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0));
			return objectLoader.openStream();
		} else {
			throw new ObjectNotFoundException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'");
		}
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 4
Source File: GitIndexEntry.java    From git-code-format-maven-plugin with MIT License 5 votes vote down vote up
private void logObjectContent(ObjectLoader objectLoader, String virtualName)
    throws IOException {
  if (!log.isDebugEnabled()) {
    return;
  }

  try (InputStream input = objectLoader.openStream();
      OutputStream output = TemporaryFile.create(log, virtualName).newOutputStream()) {
    IOUtils.copy(input, output);
  }
}
 
Example 5
Source File: LfsFilter.java    From git-as-svn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long getSize(@NotNull GitObject<? extends ObjectId> objectId) throws IOException {
  final ObjectLoader loader = objectId.openObject();

  try (ObjectStream stream = loader.openStream()) {
    final Meta meta = parseMeta(stream);
    if (meta != null)
      return meta.getSize();
  }

  return loader.getSize();
}
 
Example 6
Source File: GitIndexEntry.java    From git-code-format-maven-plugin with MIT License 4 votes vote down vote up
private void doFormat(DirCacheEntry dirCacheEntry, CodeFormatter formatter) {
  LineRanges lineRanges = computeLineRanges(dirCacheEntry);
  if (lineRanges.isAll()) {
    log.info("Formatting '" + dirCacheEntry.getPathString() + "'");
  } else {
    log.info("Formatting lines " + lineRanges + " of '" + dirCacheEntry.getPathString() + "'");
  }

  try (TemporaryFile temporaryFormattedFile =
      TemporaryFile.create(log, dirCacheEntry.getPathString() + ".formatted")) {
    ObjectId unformattedObjectId = dirCacheEntry.getObjectId();
    log.debug("Unformatted object id is '" + unformattedObjectId + "'");
    ObjectDatabase objectDatabase = repository.getObjectDatabase();
    ObjectLoader objectLoader = objectDatabase.open(unformattedObjectId);

    logObjectContent(objectLoader, dirCacheEntry.getPathString() + ".unformatted");

    try (InputStream content = objectLoader.openStream();
        OutputStream formattedContent = temporaryFormattedFile.newOutputStream()) {
      formatter.format(content, lineRanges, formattedContent);
    }

    long formattedSize = temporaryFormattedFile.size();
    ObjectId formattedObjectId;
    try (InputStream formattedContent = temporaryFormattedFile.newInputStream();
        ObjectInserter objectInserter = objectDatabase.newInserter()) {
      formattedObjectId = objectInserter.insert(OBJ_BLOB, formattedSize, formattedContent);
      objectInserter.flush();
    }

    log.debug("Formatted size is " + formattedSize);
    dirCacheEntry.setLength(formattedSize);
    log.debug("Formatted object id is '" + formattedObjectId + "'");
    dirCacheEntry.setObjectId(formattedObjectId);
  } catch (IOException e) {
    throw new MavenGitCodeFormatException(e);
  }

  if (lineRanges.isAll()) {
    log.info("Formatted '" + dirCacheEntry.getPathString() + "'");
  } else {
    log.info("Formatted lines " + lineRanges + " of '" + dirCacheEntry.getPathString() + "'");
  }
}
 
Example 7
Source File: GitFilterRaw.java    From git-as-svn with GNU General Public License v2.0 4 votes vote down vote up
@NotNull
@Override
public InputStream inputStream(@NotNull GitObject<? extends ObjectId> objectId) throws IOException {
  final ObjectLoader loader = objectId.openObject();
  return loader.openStream();
}