@Override public Note merge(Note base, Note ours, Note theirs, ObjectReader reader, ObjectInserter inserter) throws IOException { if (ours == null) { return theirs; } if (theirs == null) { return ours; } if (ours.getData().equals(theirs.getData())) { return ours; } ObjectLoader lo = reader.open(ours.getData()); byte[] sep = new byte[] {'\n'}; ObjectLoader lt = reader.open(theirs.getData()); try (ObjectStream os = lo.openStream(); ByteArrayInputStream b = new ByteArrayInputStream(sep); ObjectStream ts = lt.openStream(); UnionInputStream union = new UnionInputStream(os, b, ts)) { ObjectId noteData = inserter.insert(Constants.OBJ_BLOB, lo.getSize() + sep.length + lt.getSize(), union); return new Note(ours, noteData); } }
@NotNull @Override public String getMd5(@NotNull GitObject<? extends ObjectId> objectId) throws IOException, SVNException { final ObjectLoader loader = objectId.openObject(); final ObjectStream stream = loader.openStream(); final byte[] header = new byte[Constants.POINTER_MAX_SIZE]; int length = ByteStreams.read(stream, header, 0, header.length); if (length < header.length) { final Map<String, String> pointer = Pointer.parsePointer(header, 0, length); if (pointer != null) { String md5 = getReader(pointer).getMd5(); if (md5 != null) { return md5; } } } return GitFilterHelper.getMd5(this, cacheMd5, null, objectId); }
public ObjectStream getRemoteContentStream(String branch, String path) throws Exception { ObjectId id = localRepo.resolve("refs/remotes/origin/" + branch); try (ObjectReader reader = localRepo.newObjectReader(); RevWalk walk = new RevWalk(reader)) { RevCommit commit = walk.parseCommit(id); RevTree tree = commit.getTree(); TreeWalk treewalk = TreeWalk.forPath(reader, path, tree); if (treewalk != null) { return reader.open(treewalk.getObjectId(0)).openStream(); } else { return null; } } }
@Override public Loader<BlobView> onCreateLoader(int id, Bundle b) { return new AsyncLoader<BlobView>(getActivity()) { public BlobView loadInBackground() { Bundle args = getArguments(); try { Repository repo = FileRepositoryBuilder.create(gitDirFrom(args)); ObjectId revision = repo.resolve(args.getString(UNTIL_REVS)); RevWalk revWalk = new RevWalk(repo); RevCommit commit = revWalk.parseCommit(revision); TreeWalk treeWalk = TreeWalk.forPath(repo, args.getString(PATH), commit.getTree()); ObjectId blobId = treeWalk.getObjectId(0); ObjectLoader objectLoader = revWalk.getObjectReader().open(blobId, Constants.OBJ_BLOB); ObjectStream binaryTestStream = objectLoader.openStream(); boolean blobIsBinary = RawText.isBinary(binaryTestStream); binaryTestStream.close(); Log.d(TAG, "blobIsBinary="+blobIsBinary); return blobIsBinary?new BinaryBlobView(objectLoader, treeWalk.getNameString()):new TextBlobView(objectLoader); } catch (IOException e) { throw new RuntimeException(e); } } }; }
BinaryBlobView(ObjectLoader objectLoader, String nameString) throws IOException { this.nameString = nameString; ObjectStream stream = objectLoader.openStream(); tempFile= new File(getActivity().getExternalCacheDir(), nameString); copyInputStreamToFile(stream, tempFile); mimeType=URLConnection.getFileNameMap().getContentTypeFor(nameString); Log.d(TAG, "mimeType="+mimeType+" tempFile="+tempFile); }
private static EntryType of(org.eclipse.jgit.lib.Repository repo, TreeWalk walker, String entry) throws IOException { if (entry.endsWith("/")) { return EntryType.FOLDER; } ObjectId objectId = walker.getObjectId(0); ObjectLoader loader = repo.open(objectId); try (ObjectStream stream = loader.openStream()) { if (RawText.isBinary(stream)) { return EntryType.BINARY; } return EntryType.TEXT; } }
@Override public long getSize(@NotNull GitObject<? extends ObjectId> objectId) throws IOException { final ObjectLoader loader = objectId.openObject(); final ObjectStream stream = loader.openStream(); final byte[] header = new byte[Constants.POINTER_MAX_SIZE]; int length = ByteStreams.read(stream, header, 0, header.length); if (length < header.length) { final Map<String, String> pointer = Pointer.parsePointer(header, 0, length); if (pointer != null) { return getReader(pointer).getSize(); } } return loader.getSize(); }
@NotNull @Override public InputStream inputStream(@NotNull GitObject<? extends ObjectId> objectId) throws IOException { final ObjectLoader loader = objectId.openObject(); final ObjectStream stream = loader.openStream(); final byte[] header = new byte[Constants.POINTER_MAX_SIZE]; int length = ByteStreams.read(stream, header, 0, header.length); if (length < header.length) { final Map<String, String> pointer = Pointer.parsePointer(header, 0, length); if (pointer != null) { return getReader(pointer).openStream(); } } return new TemporaryInputStream(header, length, stream); }
/** * Find the tree that contains the required identity. * * @return The ObjectId of the tree (directory) that contains the matching * identity within the supplied hierarchy. */ private ObjectId findMatchingIdentity( IdentityValidator identityValidator, ObjectId tree) throws IOException { TreeWalk treeWalk = new TreeWalk(repo.getRepository()); try { treeWalk.setRecursive(false); treeWalk.addTree(tree); while (treeWalk.next()) { if (treeWalk.isSubtree()) { ObjectId candidateId = findMatchingIdentity( identityValidator, treeWalk.getObjectId(0)); if (ObjectId.zeroId().equals(candidateId)) { // Keep searching } else { return candidateId; } } else if (identityValidator.getIdentityFilename().equals(treeWalk.getNameString())) { // Read the identity file ObjectLoader loader = repo.getRepository().open( treeWalk.getObjectId(0)); ObjectStream stream = loader.openStream(); InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8); if (identityValidator.isIdentityMatched(new BufferedReader(reader))) { // We found it return tree; } } } return ObjectId.zeroId(); } finally { treeWalk.release(); } }
@Override public BufferedReader getBufferedReader( IdentityValidator identityValidator, String filename, Optional<String> label) throws IOException { if (label.isPresent()) { Pair<String, ObjectId> diff = findDiff(identityValidator, label.get()); if (ObjectId.zeroId().equals(diff.getValue())) { // No such tree throw new FileNotFoundException(identityValidator.getPath() + " does not exist for label " + label.get()); } else { // Find the file in this tree TreeWalk treeWalk = new TreeWalk(repo.getRepository()); try { treeWalk.setRecursive(false); treeWalk.addTree(diff.getValue()); while (treeWalk.next()) { if (filename.equals(treeWalk.getNameString())) { // Read the file ObjectLoader loader = repo.getRepository().open( treeWalk.getObjectId(0)); ObjectStream stream = loader.openStream(); InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8); return new BufferedReader(reader); } } // No such file throw new FileNotFoundException(filename + " does not exist for label " + label.get() ); } finally { treeWalk.release(); } } } else { return delegate.getBufferedReader(identityValidator, filename, label); } }