Search in sources :

Example 31 with ObjectLoader

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);
        }
    }
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) MimeType(eu.medsea.mimeutil.MimeType) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 32 with ObjectLoader

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);
    }
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException)

Example 33 with ObjectLoader

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());
        }
    }
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) InternalGroup(com.google.gerrit.entities.InternalGroup) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ImmutableList(com.google.common.collect.ImmutableList) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) Map(java.util.Map) FormatMethod(com.google.errorprone.annotations.FormatMethod) RefNames(com.google.gerrit.entities.RefNames) ConsistencyProblemInfo.error(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo.error) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) NoteMap(org.eclipse.jgit.notes.NoteMap) AccountGroup(com.google.gerrit.entities.AccountGroup) BiMap(com.google.common.collect.BiMap) Note(org.eclipse.jgit.notes.Note) ConsistencyCheckInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) ObjectId(org.eclipse.jgit.lib.ObjectId) GroupReference(com.google.gerrit.entities.GroupReference) Objects(java.util.Objects) HashBiMap(com.google.common.collect.HashBiMap) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) Ref(org.eclipse.jgit.lib.Ref) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ConsistencyProblemInfo.warning(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo.warning) FluentLogger(com.google.common.flogger.FluentLogger) Repository(org.eclipse.jgit.lib.Repository) Singleton(com.google.inject.Singleton) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Ref(org.eclipse.jgit.lib.Ref) Note(org.eclipse.jgit.notes.Note) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) GroupReference(com.google.gerrit.entities.GroupReference) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 34 with ObjectLoader

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;
}
Also used : RevBlob(org.eclipse.jgit.revwalk.RevBlob) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 35 with ObjectLoader

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;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)37 RevWalk (org.eclipse.jgit.revwalk.RevWalk)24 ObjectId (org.eclipse.jgit.lib.ObjectId)21 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)20 RevCommit (org.eclipse.jgit.revwalk.RevCommit)19 IOException (java.io.IOException)14 Repository (org.eclipse.jgit.lib.Repository)14 ObjectReader (org.eclipse.jgit.lib.ObjectReader)12 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)6 Ref (org.eclipse.jgit.lib.Ref)6 RevTree (org.eclipse.jgit.revwalk.RevTree)6 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)5 FileMode (org.eclipse.jgit.lib.FileMode)5 IArchimateModel (com.archimatetool.model.IArchimateModel)4 HashMap (java.util.HashMap)3 FilestoreModel (com.gitblit.models.FilestoreModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2