/** * @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 ); } }
/** * Constructor for LogCommand. * * @param repo * the {@link org.eclipse.jgit.lib.Repository} */ protected LogCommand(Repository repo) { super(repo); walk = new RevWalk(repo); }
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); } }
/** * Constructor for DescribeCommand. * * @param repo * the {@link org.eclipse.jgit.lib.Repository} */ protected DescribeCommand(Repository repo) { super(repo); w = new RevWalk(repo); w.setRetainBody(false); }
@SuppressWarnings("resource") private static RevTree toTree(ObjectReader reader, AnyObjectId id) throws IOException { return new RevWalk(reader).parseTree(id); }
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 void initRevWalk() { if (rw != null) { rw.close(); } rw = new RevWalk(repo); rw.setRetainBody(false); }
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); } }
public Collection<String> getTags(Repository repo, final ObjectId objectId) throws GitAPIException { try (Git git = Git.wrap(repo)) { try (RevWalk walk = new RevWalk(repo)) { Collection<String> tags = getTags(git, objectId, walk); walk.dispose(); return tags; } } }
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 RefUpdate.Result updateRef(ObjectId newId) throws IOException { RefUpdate ru = db.updateRef(REF_NAME); ru.setExpectedOldObjectId(commit != null ? commit : ObjectId.zeroId()); ru.setNewObjectId(newId); ru.setRefLogIdent(pending.get(pending.size() - 1).ident); ru.setRefLogMessage(JGitText.get().storePushCertReflog, false); try (RevWalk rw = new RevWalk(reader)) { return ru.update(rw); } }
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 ObjectIdRef doPeel(Ref leaf) throws MissingObjectException, IOException { try (RevWalk rw = new RevWalk(getRepository())) { RevObject obj = rw.parseAny(leaf.getObjectId()); if (obj instanceof RevTag) { return new ObjectIdRef.PeeledTag(leaf.getStorage(), leaf .getName(), leaf.getObjectId(), rw.peel(obj).copy()); } else { return new ObjectIdRef.PeeledNonTag(leaf.getStorage(), leaf .getName(), leaf.getObjectId()); } } }