// the RevWalk must be reset by the caller upon return! private ArrayList<RevCommit> findCommits( RevWalk walk, ArrayList<RevCommit> included, ArrayList<RevCommit> excluded, boolean getBody, long after) throws MissingObjectException, IncorrectObjectTypeException, IOException { ArrayList<RevCommit> commits = new ArrayList<RevCommit>(); commits.ensureCapacity(allBranches.size()); // heuristic workaround walk.sort(RevSort.COMMIT_TIME_DESC, true); walk.sort(RevSort.TOPO, true); walk.setRetainBody(getBody); if (after > 0) walk.setRevFilter(CommitTimeRevFilter.after(after)); else walk.setRevFilter(null); walk.markStart(included); RevCommit c; Iterator<RevCommit> it = excluded.iterator(); while (it.hasNext()) { walk.markUninteresting(it.next()); } it = walk.iterator(); while (it.hasNext()) { c = it.next(); if (getBody) walk.parseBody(c); // addUnique(commits, c); // commits are naturally ordered by SHA-1 commits.add(c); } return commits.size() > 0 ? commits : null; }
public void markCleanMerges( final RevWalk rw, final RevFlag canMergeFlag, final CodeReviewCommit mergeTip, final Set<RevCommit> alreadyAccepted) throws IntegrationException { if (mergeTip == null) { // If mergeTip is null here, branchTip was null, indicating a new branch // at the start of the merge process. We also elected to merge nothing, // probably due to missing dependencies. Nothing was cleanly merged. // return; } try { rw.resetRetain(canMergeFlag); rw.sort(RevSort.TOPO); rw.sort(RevSort.REVERSE, true); rw.markStart(mergeTip); for (RevCommit c : alreadyAccepted) { rw.markUninteresting(c); } CodeReviewCommit c; while ((c = (CodeReviewCommit) rw.next()) != null) { if (c.getPatchsetId() != null) { c.setStatusCode(CommitMergeStatus.CLEAN_MERGE); } } } catch (IOException e) { throw new IntegrationException("Cannot mark clean merges", e); } }
private void negotiateBegin() throws IOException { walk.resetRetain(REACHABLE, ADVERTISED); walk.markStart(reachableCommits); walk.sort(RevSort.COMMIT_TIME_DESC); walk.setRevFilter( new RevFilter() { @Override public RevFilter clone() { return this; } @Override public boolean include(final RevWalk walker, final RevCommit c) { final boolean remoteKnowsIsCommon = c.has(COMMON); if (c.has(ADVERTISED)) { // Remote advertised this, and we have it, hence common. // Whether or not the remote knows that fact is tested // before we added the flag. If the remote doesn't know // we have to still send them this object. // c.add(COMMON); } return !remoteKnowsIsCommon; } }); }
/** * Parses an array of Git commits and returns an array of commits that affected the specified * object. * * @param repository the Git repository * @param commitIds the Git commit IDs to parse * @param path the object affected * @return an array of {@link GitCommit} objects representing the commits */ public NSArray<GitCommit> commitsWithIds(NSArray<ObjectId> commitIds, String path) { try { RevWalk rw = new RevWalk(repository); try { rw.sort(RevSort.COMMIT_TIME_DESC); if (path != null) { rw.setTreeFilter( AndTreeFilter.create(PathSuffixFilter.create(path), TreeFilter.ANY_DIFF)); } else { rw.setTreeFilter(TreeFilter.ALL); } for (ObjectId commitId : commitIds) { rw.markStart(rw.parseCommit(commitId)); } NSMutableArray<GitCommit> commits = new NSMutableArray<GitCommit>(); for (RevCommit commit : rw) { commits.add(new GitCommit(commit)); } return commits; } finally { rw.release(); } } catch (Exception e) { log.error("An exception occurred while parsing the commit: ", e); return null; } }
// printout all commit messages in a given range -> expensive: creates a one-time only RevWalk void printCommits(String outFile, String from_ref, String to_ref) throws IOException, NoHeadException, GitAPIException { AnyObjectId from = git.getRepository().resolve(from_ref); AnyObjectId to = (to_ref == null || to_ref.equals("")) ? null : git.getRepository().resolve(to_ref); RevWalk walk = new RevWalk(git.getRepository()); walk.sort(RevSort.COMMIT_TIME_DESC, true); walk.sort(RevSort.TOPO, true); walk.markStart(walk.parseCommit(from)); if (to != null) walk.markUninteresting(walk.parseCommit(to)); printCommits(outFile, walk); walk.dispose(); // PrintWriter pout = new PrintWriter(new FileWriter(outFile), true); // Iterator<RevCommit> itc = git.log().add(from).call().iterator(); // RevCommit c; // while (itc.hasNext()) { // c = itc.next(); // pout.println("===========\n" + printCommit(c)); // if (to != null && c.equals(to)) break; // } // pout.close(); }
private void markReachable(final Set<ObjectId> have, final int maxTime) throws IOException { for (final Ref r : local.getAllRefs().values()) { try { final RevCommit o = walk.parseCommit(r.getObjectId()); o.add(REACHABLE); reachableCommits.add(o); } catch (IOException readError) { // If we cannot read the value of the ref skip it. } } for (final ObjectId id : have) { try { final RevCommit o = walk.parseCommit(id); o.add(REACHABLE); reachableCommits.add(o); } catch (IOException readError) { // If we cannot read the value of the ref skip it. } } if (maxTime > 0) { // Mark reachable commits until we reach maxTime. These may // wind up later matching up against things we want and we // can avoid asking for something we already happen to have. // final Date maxWhen = new Date(maxTime * 1000L); walk.sort(RevSort.COMMIT_TIME_DESC); walk.markStart(reachableCommits); walk.setRevFilter(CommitTimeRevFilter.after(maxWhen)); for (; ; ) { final RevCommit c = walk.next(); if (c == null) break; if (c.has(ADVERTISED) && !c.has(COMMON)) { // This is actually going to be a common commit, but // our peer doesn't know that fact yet. // c.add(COMMON); c.carry(COMMON); reachableCommits.add(c); } } } }
@Override public void sort(final RevSort s, final boolean use) { if (s == RevSort.TOPO && !use) throw new IllegalArgumentException(JGitText.get().topologicalSortRequired); super.sort(s, use); }
/** * Create a new revision walker for a given repository. * * @param repo the repository the walker will obtain data from. */ public PlotWalk(final Repository repo) { super(repo); super.sort(RevSort.TOPO, true); reverseRefMap = repo.getAllRefsByPeeledObjectId(); }