final byte[] loadCanonical(final RevWalk walk) throws IOException, MissingObjectException, IncorrectObjectTypeException, CorruptObjectException { final ObjectLoader ldr = walk.db.openObject(walk.curs, this); if (ldr == null) throw new MissingObjectException(this, getType()); final byte[] data = ldr.getCachedBytes(); if (getType() != ldr.getType()) throw new IncorrectObjectTypeException(this, getType()); return data; }
/** * Gets the type of the object with the specified ID (either a tree, tag, blob, or commit). * * @param id the object ID * @return the object type, one of the {@code OBJ_*} constants in the {@link Constants} class. */ public int typeOfObject(ObjectId id) { ObjectReader reader = repository.newObjectReader(); try { ObjectLoader loader = reader.open(id); return loader.getType(); } catch (Exception e) { return Constants.OBJ_BAD; } finally { reader.release(); } }
private static void copy(TemporaryBuffer.Heap tinyPack, ObjectLoader ldr) throws IOException { final byte[] buf = new byte[64]; final byte[] content = ldr.getCachedBytes(); int dataLength = content.length; int nextLength = dataLength >>> 4; int size = 0; buf[size++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (ldr.getType() << 4) | (dataLength & 0x0F)); dataLength = nextLength; while (dataLength > 0) { nextLength >>>= 7; buf[size++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (dataLength & 0x7F)); dataLength = nextLength; } tinyPack.write(buf, 0, size); deflate(tinyPack, content); }
public static GitObject createGitObject(RepositoryData repositoryData, String hash) throws MissingObjectException, IOException { Repository repository = repositoryData.getRepository(); ObjectId id = repository.resolve(hash); ObjectLoader loader = repository.open(id); GitObject object = null; switch (loader.getType()) { case Constants.OBJ_COMMIT: object = new Commit(hash, new String(loader.getCachedBytes())); break; case Constants.OBJ_TREE: object = new Tree(hash, loader.getBytes()); break; case Constants.OBJ_BLOB: object = new Blob(hash, new String(loader.getCachedBytes())); break; case Constants.OBJ_TAG: object = new Tag(hash, new String(loader.getCachedBytes())); break; } object.setRepositoryData(repositoryData); return object; }
/** * Push a candidate object onto the generator's traversal stack. * * <p>Candidates should be pushed in history order from oldest-to-newest. Applications should push * the starting commit first, then the index revision (if the index is interesting), and finally * the working tree copy (if the working tree is interesting). * * @param description description of the blob revision, such as "Working Tree". * @param id may be a commit or a blob. * @return {@code this} * @throws IOException the repository cannot be read. */ public BlameGenerator push(String description, AnyObjectId id) throws IOException { ObjectLoader ldr = reader.open(id); if (ldr.getType() == OBJ_BLOB) { if (description == null) description = JGitText.get().blameNotCommittedYet; BlobCandidate c = new BlobCandidate(description, resultPath); c.sourceBlob = id.toObjectId(); c.sourceText = new RawText(ldr.getCachedBytes(Integer.MAX_VALUE)); c.regionList = new Region(0, 0, c.sourceText.size()); remaining = c.sourceText.size(); push(c); return this; } RevCommit commit = revPool.parseCommit(id); if (!find(commit, resultPath)) return this; Candidate c = new Candidate(commit, resultPath); c.sourceBlob = idBuf.toObjectId(); c.loadText(reader); c.regionList = new Region(0, 0, c.sourceText.size()); remaining = c.sourceText.size(); push(c); return this; }