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

The following examples show how to use org.eclipse.jgit.lib.ObjectLoader#getSize() . 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: GitContentRepository.java    From studio with GNU General Public License v3.0 6 votes vote down vote up
@Override
public long getContentSize(final String site, final String path) {
    Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
    try {
        RevTree tree = helper.getTreeForLastCommit(repo);
        try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
            if (tw != null && tw.getObjectId(0) != null) {
                ObjectId id = tw.getObjectId(0);
                ObjectLoader objectLoader = repo.open(id);
                return objectLoader.getSize();
            }
        }
    } catch (IOException e) {
        logger.error("Error while getting content for file at site: " + site + " path: " + path, e);
    }
    return -1L;
}
 
Example 2
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 3
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;
}
 
Example 4
Source File: DefaultIndexManager.java    From onedev with MIT License 4 votes vote down vote up
private void indexBlob(IndexWriter writer, Repository repository, 
		SymbolExtractor<Symbol> extractor, ObjectId blobId, String blobPath) throws IOException {
	Document document = new Document();
	
	document.add(new StoredField(BLOB_INDEX_VERSION.name(), getIndexVersion(extractor)));
	document.add(new StringField(BLOB_HASH.name(), blobId.name(), Store.NO));
	document.add(new StringField(BLOB_PATH.name(), blobPath, Store.NO));
	document.add(new BinaryDocValuesField(BLOB_PATH.name(), new BytesRef(blobPath.getBytes(StandardCharsets.UTF_8))));
	
	String blobName = blobPath;
	if (blobPath.indexOf('/') != -1) 
		blobName = StringUtils.substringAfterLast(blobPath, "/");
	
	document.add(new StringField(BLOB_NAME.name(), blobName.toLowerCase(), Store.NO));
	
	ObjectLoader objectLoader = repository.open(blobId);
	if (objectLoader.getSize() <= MAX_INDEXABLE_SIZE) {
		byte[] bytes = objectLoader.getCachedBytes();
		String content = ContentDetector.convertToText(bytes, blobName);
		if (content != null) {
			document.add(new TextField(BLOB_TEXT.name(), content, Store.NO));
			
			if (extractor != null) {
				List<Symbol> symbols = null;
				try {
					symbols = extractor.extract(blobName, StringUtils.removeBOM(content));
				} catch (Exception e) {
					logger.trace("Can not extract symbols from blob (hash:" + blobId.name() + ", path:" + blobPath + ")", e);
				}
				if (symbols != null) {
					for (Symbol symbol: symbols) {
						String fieldValue = symbol.getName();
						if (fieldValue != null && symbol.isSearchable()) {
							fieldValue = fieldValue.toLowerCase();

							String fieldName;
							if (symbol.isPrimary())
								fieldName = BLOB_PRIMARY_SYMBOLS.name();
							else
								fieldName = BLOB_SECONDARY_SYMBOLS.name();
							document.add(new StringField(fieldName, fieldValue, Store.NO));
						}
					}
					byte[] bytesOfSymbols = SerializationUtils.serialize((Serializable) symbols);
					document.add(new StoredField(BLOB_SYMBOL_LIST.name(), bytesOfSymbols));
				}
			} 
		} else {
			logger.debug("Ignore content of binary file '{}'.", blobPath);
		}
	} else {
		logger.debug("Ignore content of large file '{}'.", blobPath);
	}

	writer.addDocument(document);
}