public GoConfigRevisions getCommits(final int pageSize, final int offset) { return doLocked(() -> { GoConfigRevisions goConfigRevisions = new GoConfigRevisions(); try { LogCommand command = git.log().setMaxCount(pageSize).setSkip(offset); Iterable<RevCommit> revisions = command.call(); for (RevCommit revision : revisions) { GoConfigRevision goConfigRevision = new GoConfigRevision((byte[]) null, revision.getFullMessage()); goConfigRevision.setCommitSHA(revision.name()); goConfigRevisions.add(goConfigRevision); } } catch (Exception e) { // ignore } return goConfigRevisions; }); }
@Signature public Memory resolveCommit(String revstr) throws IOException, GitAPIException { ObjectId objectId = getWrappedObject().getRepository().resolve(revstr); if (objectId == null) { return Memory.NULL; } LogCommand command = getWrappedObject() .log() .add(objectId) .setMaxCount(1); Iterable<RevCommit> call = command.call(); for (RevCommit revCommit : call) { return GitUtils.valueOf(revCommit); } return Memory.NULL; }
log.setMaxCount(maxCount.toInteger());
/** * @inheritDoc */ @Override public RevCommit getLastCommit(Git git){ try { return git.log().setMaxCount(1).call().iterator().next(); } catch (GitAPIException e) { throw new RuntimeException("Failed to access local repository data",e); } }
/** * Returns Commit objects for the last n commits. * * @param n - number of commits to return * @return - List of Commit objects * @throws GitAPIException - failed. */ public Stream<RevCommit> getLastNCommits(final int n) throws GitAPIException { return StreamSupport.stream(git.log().setMaxCount(n).call().spliterator(), false); }
private RevCommit getLatestCommit() { try { Iterable<RevCommit> commits = getGit().log().setMaxCount(1).call(); Iterator<RevCommit> it = commits.iterator(); if (!it.hasNext()) return null; return it.next(); } catch (GitAPIException|StopTaskException e) { Timber.e(e); } return null; }
private RevCommit getLatestCommit() { try { Iterable<RevCommit> commits = getGit().log().setMaxCount(1).call(); Iterator<RevCommit> it = commits.iterator(); if (!it.hasNext()) return null; return it.next(); } catch (GitAPIException e) { BasicFunctions.showException(e); } catch (StopTaskException e) { } return null; }
public void logLastCommitsInBranch(Git git, int nbCommitsToLog, String branchName) throws IOException, GitAPIException { Iterable<RevCommit> lastCommitsOnBranch = git.log() .add(git.getRepository().resolve(branchName)) .setMaxCount(nbCommitsToLog) .call(); logDiffCommits(StreamSupport.stream(lastCommitsOnBranch.spliterator(), false).collect(toList()), "last " + nbCommitsToLog + "commits on branch " + branchName); }
/** * Get the hash of the last commit on the current branch of the given repository. * * @param git The repository from which to get the last commit hash. * @return The hash of the last commit. */ public static String getLastHash(Git git) { try { Iterator<RevCommit> revCommitIterator = git.log().setMaxCount(1).call().iterator(); if (revCommitIterator.hasNext()) { return revCommitIterator.next().getName(); } } catch (GitAPIException e) { throw new GitException("Failed to log git repository", e); } return null; }
/** * Returns the hash of the last commit before a job was deleted. * * This method uses the following git command equivalent: * <pre> * git log --all --skip=1 -n 1 -- jobs/[jobId] * </pre> * which only works as intended with deleted jobs. * * @param jobId The id of a deleted job. * @throws GitAPIException * @throws IOException */ public String getCommitHashBeforeJobDeletion(String jobId) throws GitAPIException, IOException { // skip=1 skips the deletion, and -n 1 returns only the commit immediately before that Iterable<RevCommit> iter = git.log().all().addPath(getPathForJobId(jobId)).setSkip(1).setMaxCount(1).call(); RevCommit commit = Iterables.getFirst(iter, null); return commit == null ? null : commit.getName(); }
/** * Return a simplified git commit history list. * * @param repositoryDirectory The directory in which the git repo exists. * @param from Start to query from the given history. * @param count The number of history entries to retrieve. * @return A list of simplified history entries. */ public static List<SimpleGitHistoryEntry> getHistory(Path repositoryDirectory, int from, int count) { Git repository = null; try { repository = Git.open(repositoryDirectory.toFile()); Iterable<RevCommit> commits = repository.log().setSkip(from).setMaxCount(count).call(); List<SimpleGitHistoryEntry> historyEntries = Lists.newArrayList(); for (RevCommit commit : commits) { historyEntries.add(new SimpleGitHistoryEntry(commit.getId().getName(), commit.getAuthorIdent().getName(), commit.getAuthorIdent().getEmailAddress(), commit.getFullMessage(), new Date(commit.getCommitTime() * 1000L))); } return historyEntries; } catch (NoHeadException e) { log.debug("Your repository has no head, you need to save your topology before using the git history."); return Lists.newArrayList(); } catch (GitAPIException | IOException e) { throw new GitException("Unable to get history from the git repository", e); } finally { close(repository); } }
/** * Does not fetch. */ public CommitInfo getCommitInfo(String path) throws Exception { Iterator<RevCommit> revCommits = git.log().addPath(path).setMaxCount(1).call().iterator(); if (revCommits.hasNext()) { RevCommit revCommit = revCommits.next(); CommitInfo commitInfo = new CommitInfo(revCommit.getId().name()); PersonIdent committerIdent = revCommit.getCommitterIdent(); commitInfo.setCommitter(committerIdent.getName()); commitInfo.setEmail(committerIdent.getEmailAddress()); if ((commitInfo.getCommitter() == null || commitInfo.getCommitter().isEmpty()) && commitInfo.getEmail() != null) commitInfo.setCommitter(commitInfo.getEmail()); commitInfo.setDate(committerIdent.getWhen()); commitInfo.setMessage(revCommit.getShortMessage()); return commitInfo; } return null; }
mCommits = mRepo.getGit().log().add(newCommitId).setMaxCount(1).call(); } else { mCommits = new ArrayList<RevCommit>();
@Override public List<Revision> getHistory(final String test, final String revision, final int start, final int limit) throws StoreException { try { final ObjectId commitId = ObjectId.fromString(revision); final LogCommand logCommand = git.log() // TODO: create path to definition.json file, sanitize test name for invalid / relative characters .addPath(getTestDefinitionsDirectory() + File.separator + test + File.separator + FileBasedProctorStore.TEST_DEFINITION_FILENAME) .add(commitId) .setSkip(start) .setMaxCount(limit); return getHistoryFromLogCommand(logCommand); } catch (IOException e) { throw new StoreException("Could not get history for " + test + " starting at " + getGitCore().getRefName(), e); } }
@Override public List<Revision> getHistory(final String test, final String revision, final int start, final int limit) throws StoreException { try { final ObjectId commitId = ObjectId.fromString(revision); final LogCommand logCommand = git.log() // TODO: create path to definition.json file, sanitize test name for invalid / relative characters .addPath(getTestDefinitionsDirectory() + File.separator + test + File.separator + FileBasedProctorStore.TEST_DEFINITION_FILENAME) .add(commitId) .setSkip(start) .setMaxCount(limit); return getHistoryFromLogCommand(logCommand); } catch (IOException e) { throw new StoreException("Could not get history for " + test + " starting at " + getGitCore().getRefName(), e); } }
mCommits = mRepo.getGit().log().add(newCommitId).setMaxCount(1).call(); } else { mCommits = new ArrayList<RevCommit>();
private static void checkoutExistingBranchOrCreateOrphan(Git git, boolean isLocalOnly, String username, String password, String branch) throws IOException, GitAPIException { if (!isLocalOnly) { fetch(git, username, password); } // need to checkout branch ? if (!branch.equals(git.getRepository().getBranch())) { // HEAD is needed for creating a branch try { git.log().setMaxCount(1).call(); } catch (NoHeadException e) { // no HEAD - commit for creating a HEAD git.add().addFilepattern(".").call(); git.commit().setCommitter("a4c-bot", "a4c-bot@robot.org").setMessage("initial commit").call(); } // checkout branch CheckoutCommand checkoutCommand = git.checkout(); checkoutCommand.setName(branch); if (!branchExistsLocally(git, branch)) { checkoutCommand.setCreateBranch(true); String fullReference = getFullBranchReference(git, branch); boolean existRemotely = !fullReference.isEmpty(); if (existRemotely) { checkoutCommand.setStartPoint(getFullBranchReference(git, branch)); } else { checkoutCommand.setOrphan(true); } } checkoutCommand.call(); } }
@Override public List<Revision> getMatrixHistory(final int start, final int limit) throws StoreException { final LogCommand logCommand; try { final ObjectId branchHead = git.getRepository().resolve(getGitCore().getRefName()); logCommand = git.log() .add(branchHead) .setSkip(start) .setMaxCount(limit); return getHistoryFromLogCommand(logCommand); } catch (MissingObjectException e) { throw new StoreException("Could not get history for starting at " + getGitCore().getRefName(), e); } catch (IncorrectObjectTypeException e) { throw new StoreException("Could not get history for starting at " + getGitCore().getRefName(), e); } catch (AmbiguousObjectException e) { throw new StoreException("Could not get history for starting at " + getGitCore().getRefName(), e); } catch (IOException e) { throw new StoreException("Could not get history for starting at " + getGitCore().getRefName(), e); } }
@Override public List<Revision> getMatrixHistory(final int start, final int limit) throws StoreException { final LogCommand logCommand; try { final ObjectId branchHead = git.getRepository().resolve(getGitCore().getRefName()); logCommand = git.log() .add(branchHead) .setSkip(start) .setMaxCount(limit); return getHistoryFromLogCommand(logCommand); } catch (MissingObjectException e) { throw new StoreException("Could not get history for starting at " + getGitCore().getRefName(), e); } catch (IncorrectObjectTypeException e) { throw new StoreException("Could not get history for starting at " + getGitCore().getRefName(), e); } catch (AmbiguousObjectException e) { throw new StoreException("Could not get history for starting at " + getGitCore().getRefName(), e); } catch (IOException e) { throw new StoreException("Could not get history for starting at " + getGitCore().getRefName(), e); } }
@Test public void shouldGetFirstDozenCommitsInHistoryForTag() throws Exception { Ref ref = repository.getRef("modeshape-3.0.0.Final"); ref = repository.peel(ref); RevWalk walker = new RevWalk(repository); walker.setRetainBody(true); try { RevCommit commit = walker.parseCommit(ref.getObjectId()); LogCommand command = git.log(); command.add(commit.getId()); command.setMaxCount(12); for (RevCommit rev : command.call()) { commit = walker.parseCommit(rev); print(commit); } } finally { walker.dispose(); } }