@Override public File get(String file) { try { if (repository == null) { initRepository(); } AnyObjectId id = repository.resolve(Constants.HEAD); RevCommit commit = new RevWalk(repository).parseCommit(id); LOGGER.debug("Looking up file {} in HEAD revision", file); TreeWalk treeWalk = TreeWalk.forPath(repository, file, new AnyObjectId[] { commit.getTree() }); if (treeWalk == null) { return null; } ObjectId objectId = treeWalk.getObjectId(treeWalk.getTreeCount() - 1); if (objectId == ObjectId.zeroId()) { LOGGER.debug("File {} couldn't be found in HEAD revision", file); return null; } String fileName = getFilename(file); LOGGER.debug("Creating file from saved repository content"); File tmp = File.createTempFile(fileName, null); tmp.deleteOnExit(); OutputStream os = new FileOutputStream(tmp); os.write(repository.open(objectId).getCachedBytes()); os.close(); return tmp; } catch (Exception e) { throw new ScmException(e); } }
@Override public File get(String file, CommitRef ref) { try { if (repository == null) { initRepository(); } AnyObjectId id = repository.resolve(ref.getStringRepresentation()); RevCommit commit = new RevWalk(repository).parseCommit(id); LOGGER.debug("Looking up file {} in revision {}", file, ref.getStringRepresentation()); TreeWalk treeWalk = TreeWalk.forPath(repository, file, new AnyObjectId[] { commit.getTree() }); if (treeWalk == null) { return null; } ObjectId objectId = treeWalk.getObjectId(treeWalk.getTreeCount() - 1); if (objectId == ObjectId.zeroId()) { return null; } String fileName = getFilename(file); LOGGER.debug("Creating file from saved repository content"); File tmp = File.createTempFile(fileName, null); tmp.deleteOnExit(); OutputStream os = new FileOutputStream(tmp); os.write(repository.open(objectId).getCachedBytes()); os.close(); return tmp; } catch (Exception e) { throw new ScmException(e); } }