/** * @param gitConfigFolder e.g. /your/project/root/.git * * @return Returns last commit's UUID, "nocommit" if there are no commits and returns null if an exception occured */ public static String getLastCommitUuid( String gitConfigFolder ) throws MojoExecutionException { try { Repository repo = new RepositoryBuilder().setGitDir( new File( gitConfigFolder ) ).readEnvironment().findGitDir() .build(); RevWalk walk = new RevWalk( repo ); ObjectId head = repo.resolve( "HEAD" ); if ( head != null ) { RevCommit lastCommit = walk.parseCommit( head ); return lastCommit.getId().getName(); } else { return "nocommit"; } } catch ( Exception e ) { throw new MojoExecutionException( "Error trying to get the last git commit uuid", e ); } }
private static RevCommitsPair resolveRevCommitsPair(Repository repo) { RevCommitsPair revCommitIteratorPair; try (RevWalk revWalk = new RevWalk(repo); Git git = new Git(repo)) { final Iterator<RevCommit> first; final Iterator<RevCommit> second; final ObjectId headId = repo.resolve(Constants.HEAD); final RevCommit headCommit = revWalk.parseCommit(headId); if (isMergeCommit(headCommit)) { final RevCommit firstParent = headCommit.getParent(0); final RevCommit secondParent = headCommit.getParent(1); first = git.log().add(firstParent).call().iterator(); second = git.log().add(secondParent).call().iterator(); } else { first = git.log().call().iterator(); second = Collections.emptyIterator(); } revCommitIteratorPair = new RevCommitsPair(new OmitMergeCommitsIterator(first), new OmitMergeCommitsIterator(second)); } catch (GitAPIException | IOException ignored) { revCommitIteratorPair = new RevCommitsPair(); } return revCommitIteratorPair; }
private RevCommit parseCommit(final ObjectReader reader, final ObjectId headId) throws IOException { try (RevWalk walk = new RevWalk(reader)) { return walk.parseCommit(headId); } }
private void seeAllParents(@Nonnull RevWalk revWalk, RevCommit child, @Nonnull Set<ObjectId> seen) throws IOException { Queue<RevCommit> q = new ArrayDeque<>(); q.add(child); while (q.size() > 0) { RevCommit commit = q.remove(); for (ObjectId oid : commit.getParents()) { if (seen.contains(oid)) { continue; } seen.add(oid); q.add(revWalk.parseCommit(oid)); } } }
private RevCommit parseCommit(Repository clonedRepo, Ref ref) throws MissingObjectException, IncorrectObjectTypeException, IOException { final RevCommit commit; try (RevWalk rw = new RevWalk(clonedRepo)) { commit = rw.parseCommit(ref.getObjectId()); } return commit; }
private static RevCommit buildRevCommit(Repository repository, String commit) throws IOException { // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk revWalk = new RevWalk(repository)) { return revWalk.parseCommit(ObjectId.fromString(commit)); } }
private static RevCommit buildRevCommit(Repository repository, String commit) throws IOException { // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk revWalk = new RevWalk(repository)) { return revWalk.parseCommit(ObjectId.fromString(commit)); } }
private static RevTree getTree(Repository repository) throws IOException { ObjectId lastCommitId = repository.resolve(Constants.HEAD); // a RevWalk allows to walk over commits based on some filtering try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(lastCommitId); System.out.println("Time of commit (seconds since epoch): " + commit.getCommitTime()); // and using commit's tree find the path RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); return tree; } }
private static RevTree getTree(Repository repository) throws IOException { ObjectId lastCommitId = repository.resolve(Constants.HEAD); // a RevWalk allows to walk over commits based on some filtering try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(lastCommitId); System.out.println("Time of commit (seconds since epoch): " + commit.getCommitTime()); // and using commit's tree find the path RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); return tree; } }
protected RevCommit findEvalCommitObjectId(@Nonnull String evaluateOnCommit, @Nonnull Repository repo) throws RuntimeException { try { ObjectId evalCommitId = repo.resolve(evaluateOnCommit); try (RevWalk walk = new RevWalk(repo)) { RevCommit evalCommit = walk.parseCommit(evalCommitId); walk.dispose(); log.info("evalCommit is [{}]", evalCommit.getName()); return evalCommit; } } catch (IOException ex) { throw new RuntimeException("Unable to obtain " + evaluateOnCommit + " commit!", ex); } }
private void parseReachable(ObjectId id) { try { RevCommit o = walk.parseCommit(id); if (!o.has(REACHABLE)) { o.add(REACHABLE); reachableCommits.add(o); } } catch (IOException readError) { // If we cannot read the value of the ref skip it. } }
private RevCommit parseCommit(ObjectId commitId) { try (RevWalk rw = new RevWalk(repo)) { return rw.parseCommit(commitId); } catch (IOException e) { throw new JGitInternalException(MessageFormat.format( JGitText.get().cannotReadCommit, commitId.toString()), e); } }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { Ref head = repository.findRef("refs/heads/master"); System.out.println("Found head: " + head); // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); System.out.println("\nCommit-Message: " + commit.getFullMessage()); walk.dispose(); } } } }
public static void main(String[] args) throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { Ref head = repository.findRef("refs/heads/master"); System.out.println("Found head: " + head); // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); System.out.println("\nCommit-Message: " + commit.getFullMessage()); walk.dispose(); } } } }
private Collection<Ref> filterRefs(Collection<Ref> refs) throws RefNotFoundException, IOException { if (containsCommitish == null) return refs; try (RevWalk walk = new RevWalk(repo)) { ObjectId resolved = repo.resolve(containsCommitish); if (resolved == null) throw new RefNotFoundException(MessageFormat.format( JGitText.get().refNotResolved, containsCommitish)); RevCommit containsCommit = walk.parseCommit(resolved); return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk, refs); } }
void load() throws IOException { close(); reader = db.newObjectReader(); Ref ref = db.getRefDatabase().exactRef(REF_NAME); if (ref == null) { // No ref, same as empty. return; } try (RevWalk rw = new RevWalk(reader)) { commit = rw.parseCommit(ref.getObjectId()); } }
private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId) throws IOException { // from the commit we can build the tree which allows us to construct the TreeParser //noinspection Duplicates try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(repository.resolve(objectId)); RevTree tree = walk.parseTree(commit.getTree().getId()); CanonicalTreeParser treeParser = new CanonicalTreeParser(); try (ObjectReader reader = repository.newObjectReader()) { treeParser.reset(reader, tree.getId()); } walk.dispose(); return treeParser; } }
private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId) throws IOException { // from the commit we can build the tree which allows us to construct the TreeParser //noinspection Duplicates try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(repository.resolve(objectId)); RevTree tree = walk.parseTree(commit.getTree().getId()); CanonicalTreeParser treeParser = new CanonicalTreeParser(); try (ObjectReader reader = repository.newObjectReader()) { treeParser.reset(reader, tree.getId()); } walk.dispose(); return treeParser; } }
void init(RevWalk rw) throws IOException { src = refdb.getBootstrap().exactRef(refdb.getTxnCommitted()); if (src != null && src.getObjectId() != null) { RevCommit c = rw.parseCommit(src.getObjectId()); parentCommitId = c; parentTreeId = c.getTree(); tree = RefTree.read(rw.getObjectReader(), c.getTree()); } else { parentCommitId = ObjectId.zeroId(); parentTreeId = new ObjectInserter.Formatter() .idFor(OBJ_TREE, new byte[] {}); tree = RefTree.newEmptyTree(); } }
private void initialize() throws IOException { try (Repository git = openRepository(); RevWalk rw = new RevWalk(git)) { self.initialize(git); ObjectId accepted = self.getTxnAccepted(); if (!ObjectId.zeroId().equals(accepted)) { RevCommit c = rw.parseCommit(accepted); headIndex = LogIndex.unknown(accepted); refTree = RefTree.read(rw.getObjectReader(), c.getTree()); } else { headIndex = LogIndex.unknown(ObjectId.zeroId()); refTree = RefTree.newEmptyTree(); } } }