@Override public RemoteFile readFileWithMode(String uri, String ref, String path) throws GitAPIException, IOException { File dir = FileUtils.createTempDir("jgit_", ".git", null); //$NON-NLS-1$ //$NON-NLS-2$ try (Git git = Git.cloneRepository().setBare(true).setDirectory(dir) .setURI(uri).call()) { Repository repo = git.getRepository(); ObjectId refCommitId = sha1(uri, ref); if (refCommitId == null) { throw new InvalidRefNameException(MessageFormat .format(JGitText.get().refNotResolved, ref)); } RevCommit commit = repo.parseCommit(refCommitId); TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree()); // TODO(ifrade): Cope better with big files (e.g. using // InputStream instead of byte[]) return new RemoteFile( tw.getObjectReader().open(tw.getObjectId(0)) .getCachedBytes(Integer.MAX_VALUE), tw.getFileMode(0)); } finally { FileUtils.delete(dir, FileUtils.RECURSIVE); } } }
private Commit deepestReachableCommit(ObjectId headId, int maxDepth) throws IOException { try (RevWalk revWalk = new RevWalk(repository)) { RevCommit headCommit = repository.parseCommit(headId); revWalk.markStart(headCommit); int depth = 0; RevCommit lastCommit = headCommit; Iterator<RevCommit> iterator = revWalk.iterator(); while (iterator.hasNext() && depth <= maxDepth) { lastCommit = iterator.next(); depth++; } int retainedDepth = depth - 1; // we do not count head return new Commit(lastCommit.getId(), retainedDepth, Collections.emptyList(), Collections.emptyList()); } }
/** * Returns a commit object for {@code HEAD} * * @return The commit object for {@code HEAD} * @see RevCommit * @throws GitRepositoryException if the commit object cannot be retrieved */ RevCommit getHeadRevCommit() throws GitRepositoryException { if (headCommit != null) { return headCommit; } try { return headCommit = repository.parseCommit(getHeadObject()); } catch (IOException e) { throw new GitRepositoryException( String.format("Commit \"%s\" could not be loaded.", getHeadObject().getName()), e); } }
public static String getVersion() throws Exception { Git git = Git.open(new File(".")); Repository repo = git.getRepository(); ObjectId lastCommitId = repo.resolve(Constants.HEAD); RevCommit revc = repo.parseCommit(lastCommitId); ObjectId commitId = revc.getId(); String shortId = commitId.abbreviate(7).name(); return shortId; } }
private static AbstractTreeIterator prepareNewTree(Repository repo) throws IOException { CanonicalTreeParser treeParser = new CanonicalTreeParser(); try (ObjectReader objectReader = repo.newObjectReader()) { treeParser.reset(objectReader, repo.parseCommit(getHead(repo).getObjectId()).getTree()); } return treeParser; }
@Override public void flush(boolean success) { if (success) { try (Git git = gitRepository.getGit()) { Status status = git.status().call(); if (!status.isClean()) { String message = git.getRepository().parseCommit(git.getRepository().resolve(HEAD)).getFullMessage(); if(! status.getUntracked().isEmpty() || ! status.getUntrackedFolders().isEmpty()) { AddCommand addCommand = git.add(); for(String untracked : status.getUntrackedFolders()) { addCommand = addCommand.addFilepattern(untracked); } for(String untracked : status.getUntracked()) { addCommand = addCommand.addFilepattern(untracked); } addCommand.call(); } git.commit().setMessage(message).setAmend(true).setAll(true).setNoVerify(true).call(); } } catch (RevisionSyntaxException | IOException | GitAPIException ex) { throw new RuntimeException(ex); } } }
@Override public void flush(boolean success) { if (success) { try (Git git = gitRepository.getGit()) { Status status = git.status().call(); if (!status.isClean()) { String message = git.getRepository().parseCommit(git.getRepository().resolve(HEAD)).getFullMessage(); if(! status.getUntracked().isEmpty() || ! status.getUntrackedFolders().isEmpty()) { AddCommand addCommand = git.add(); for(String untracked : status.getUntrackedFolders()) { addCommand = addCommand.addFilepattern(untracked); } for(String untracked : status.getUntracked()) { addCommand = addCommand.addFilepattern(untracked); } addCommand.call(); } git.commit().setMessage(message).setAmend(true).setAll(true).setNoVerify(true).call(); } } catch (RevisionSyntaxException | IOException | GitAPIException ex) { throw new RuntimeException(ex); } } }
@Override public RemoteFile readFileWithMode(String uri, String ref, String path) throws GitAPIException, IOException { File dir = FileUtils.createTempDir("jgit_", ".git", null); //$NON-NLS-1$ //$NON-NLS-2$ try (Git git = Git.cloneRepository().setBare(true).setDirectory(dir) .setURI(uri).call()) { Repository repo = git.getRepository(); ObjectId refCommitId = sha1(uri, ref); if (refCommitId == null) { throw new InvalidRefNameException(MessageFormat .format(JGitText.get().refNotResolved, ref)); } RevCommit commit = repo.parseCommit(refCommitId); TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree()); // TODO(ifrade): Cope better with big files (e.g. using // InputStream instead of byte[]) return new RemoteFile( tw.getObjectReader().open(tw.getObjectId(0)) .getCachedBytes(Integer.MAX_VALUE), tw.getFileMode(0)); } finally { FileUtils.delete(dir, FileUtils.RECURSIVE); } } }