use of org.eclipse.jgit.lib.ObjectLoader in project gerrit by GerritCodeReview.
the class FileContentUtil method downloadContent.
public BinaryResult downloadContent(ProjectState project, ObjectId revstr, String path, @Nullable Integer parent) throws ResourceNotFoundException, IOException {
try (Repository repo = openRepository(project);
RevWalk rw = new RevWalk(repo)) {
String suffix = "new";
RevCommit commit = rw.parseCommit(revstr);
if (parent != null && parent > 0) {
if (commit.getParentCount() == 1) {
suffix = "old";
} else {
suffix = "old" + parent;
}
commit = rw.parseCommit(commit.getParent(parent - 1));
}
try (TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(), path, commit.getTree())) {
if (tw == null) {
throw new ResourceNotFoundException();
}
int mode = tw.getFileMode(0).getObjectType();
if (mode != Constants.OBJ_BLOB) {
throw new ResourceNotFoundException();
}
ObjectId id = tw.getObjectId(0);
ObjectLoader obj = repo.open(id, OBJ_BLOB);
byte[] raw;
try {
raw = obj.getCachedBytes(MAX_SIZE);
} catch (LargeObjectException e) {
raw = null;
}
MimeType contentType = registry.getMimeType(path, raw);
return registry.isSafeInline(contentType) ? wrapBlob(path, obj, raw, contentType, suffix) : zipBlob(path, obj, commit, suffix);
}
}
}
use of org.eclipse.jgit.lib.ObjectLoader in project gerrit by GerritCodeReview.
the class RulesCache method read.
private String read(Project.NameKey project, ObjectId rulesId) throws CompileException {
try (Repository git = gitMgr.openRepository(project)) {
try {
ObjectLoader ldr = git.open(rulesId, Constants.OBJ_BLOB);
byte[] raw = ldr.getCachedBytes(maxSrcBytes);
return RawParseUtils.decode(raw);
} catch (LargeObjectException e) {
throw new CompileException("rules of " + project + " are too large", e);
} catch (RuntimeException | IOException e) {
throw new CompileException("Cannot load rules of " + project, e);
}
} catch (IOException e) {
throw new CompileException("Cannot open repository " + project, e);
}
}
use of org.eclipse.jgit.lib.ObjectLoader in project gerrit by GerritCodeReview.
the class GroupsNoteDbConsistencyChecker method readGroupNames.
private void readGroupNames(Repository repo, List<Ref> refs, Result result, BiMap<AccountGroup.UUID, String> uuidNameBiMap) throws IOException {
Optional<Ref> maybeRef = refs.stream().filter(r -> r.getName().equals(RefNames.REFS_GROUPNAMES)).findFirst();
if (!maybeRef.isPresent()) {
result.problems.add(error("ref %s does not exist", RefNames.REFS_GROUPNAMES));
return;
}
Ref ref = maybeRef.get();
try (RevWalk rw = new RevWalk(repo)) {
RevCommit c = rw.parseCommit(ref.getObjectId());
NoteMap nm = NoteMap.read(rw.getObjectReader(), c);
for (Note note : nm) {
ObjectLoader ld = rw.getObjectReader().open(note.getData());
byte[] data = ld.getCachedBytes();
GroupReference gRef;
try {
gRef = GroupNameNotes.getFromNoteData(data);
} catch (ConfigInvalidException e) {
result.problems.add(error("notename entry %s: %s does not parse: %s", note, new String(data, StandardCharsets.UTF_8), e.getMessage()));
continue;
}
ObjectId nameKey = GroupNameNotes.getNoteKey(AccountGroup.nameKey(gRef.getName()));
if (!Objects.equals(nameKey, note)) {
result.problems.add(error("notename entry %s does not match name %s", note, gRef.getName()));
}
// We trust SHA1 to have no collisions, so no need to check uniqueness of name.
uuidNameBiMap.put(gRef.getUUID(), gRef.getName());
}
}
}
use of org.eclipse.jgit.lib.ObjectLoader in project gitblit by gitblit.
the class JGitUtils method getByteContent.
/**
* Gets the raw byte content of the specified blob object.
*
* @param repository
* @param objectId
* @return byte [] blob content
*/
public static byte[] getByteContent(Repository repository, String objectId) {
RevWalk rw = new RevWalk(repository);
byte[] content = null;
try {
RevBlob blob = rw.lookupBlob(ObjectId.fromString(objectId));
ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB);
content = ldr.getCachedBytes();
} catch (Throwable t) {
error(t, repository, "{0} can't find blob {1}", objectId);
} finally {
rw.dispose();
}
return content;
}
use of org.eclipse.jgit.lib.ObjectLoader in project gitblit by gitblit.
the class JGitUtils method getByteContent.
/**
* Retrieves the raw byte content of a file in the specified tree.
*
* @param repository
* @param tree
* if null, the RevTree from HEAD is assumed.
* @param path
* @return content as a byte []
*/
public static byte[] getByteContent(Repository repository, RevTree tree, final String path, boolean throwError) {
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
byte[] content = null;
try {
if (tree == null) {
ObjectId object = getDefaultBranch(repository);
if (object == null)
return null;
RevCommit commit = rw.parseCommit(object);
tree = commit.getTree();
}
tw.reset(tree);
while (tw.next()) {
if (tw.isSubtree() && !path.equals(tw.getPathString())) {
tw.enterSubtree();
continue;
}
ObjectId entid = tw.getObjectId(0);
FileMode entmode = tw.getFileMode(0);
if (entmode != FileMode.GITLINK) {
ObjectLoader ldr = repository.open(entid, Constants.OBJ_BLOB);
content = ldr.getCachedBytes();
}
}
} catch (Throwable t) {
if (throwError) {
error(t, repository, "{0} can't find {1} in tree {2}", path, tree.name());
}
} finally {
rw.dispose();
tw.close();
}
return content;
}
Aggregations