List of usage examples for org.eclipse.jgit.revwalk.filter RevFilter RevFilter
RevFilter
From source file:com.chungkwong.jgitgui.NoteTreeItem.java
License:Open Source License
private void gitNoteRemove() { try {//from ww w . j a v a2 s. com RevCommit rev = ((Git) getParent().getParent().getValue()).log().setRevFilter(new RevFilter() { @Override public boolean include(RevWalk walker, RevCommit cmit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException { return cmit.getName().equals(NoteTreeItem.this.toString()); } @Override public RevFilter clone() { return this; } }).call().iterator().next(); ((Git) getParent().getParent().getValue()).notesRemove().setObjectId(rev).call(); getParent().getChildren().remove(this); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); Util.informUser(ex); } }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Search the commit history for a case-insensitive match to the value. * Search results require a specified SearchType of AUTHOR, COMMITTER, or * COMMIT. Results may be paginated using offset and maxCount. If the * repository does not exist or is empty, an empty list is returned. * * @param repository//from w w w. j a va 2 s. c o m * @param objectId * if unspecified, HEAD is assumed. * @param value * @param type * AUTHOR, COMMITTER, COMMIT * @param offset * @param maxCount * if < 0, all matches are returned * @return matching list of commits */ public static List<RevCommit> searchRevlogs(Repository repository, String objectId, String value, final com.gitblit.Constants.SearchType type, int offset, int maxCount) { List<RevCommit> list = new ArrayList<RevCommit>(); if (StringUtils.isEmpty(value)) { return list; } if (maxCount == 0) { return list; } if (!hasCommits(repository)) { return list; } final String lcValue = value.toLowerCase(); try { // resolve branch ObjectId branchObject; if (StringUtils.isEmpty(objectId)) { branchObject = getDefaultBranch(repository); } else { branchObject = repository.resolve(objectId); } RevWalk rw = new RevWalk(repository); rw.setRevFilter(new RevFilter() { @Override public RevFilter clone() { // FindBugs complains about this method name. // This is part of JGit design and unrelated to Cloneable. return this; } @Override public boolean include(RevWalk walker, RevCommit commit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException { boolean include = false; switch (type) { case AUTHOR: include = (commit.getAuthorIdent().getName().toLowerCase().indexOf(lcValue) > -1) || (commit.getAuthorIdent().getEmailAddress().toLowerCase().indexOf(lcValue) > -1); break; case COMMITTER: include = (commit.getCommitterIdent().getName().toLowerCase().indexOf(lcValue) > -1) || (commit.getCommitterIdent().getEmailAddress().toLowerCase() .indexOf(lcValue) > -1); break; case COMMIT: include = commit.getFullMessage().toLowerCase().indexOf(lcValue) > -1; break; } return include; } }); rw.markStart(rw.parseCommit(branchObject)); Iterable<RevCommit> revlog = rw; if (offset > 0) { int count = 0; for (RevCommit rev : revlog) { count++; if (count > offset) { list.add(rev); if (maxCount > 0 && list.size() == maxCount) { break; } } } } else { for (RevCommit rev : revlog) { list.add(rev); if (maxCount > 0 && list.size() == maxCount) { break; } } } rw.dispose(); } catch (Throwable t) { error(t, repository, "{0} failed to {1} search revlogs for {2}", type.name(), value); } return list; }
From source file:de.codesourcery.gittimelapse.GitHelper.java
License:Apache License
protected void visitCommits(File localPath, boolean retainCommitBody, ObjectId startCommit, ICommitVisitor func) throws RevisionSyntaxException, AmbiguousObjectException, IncorrectObjectTypeException, IOException { if (startCommit == null) { throw new RuntimeException("startCommit must not be NULL"); }/*from w w w . j a v a 2s. c o m*/ final RevWalk walk = new RevWalk(repository); try { final String path = stripRepoBaseDir(localPath); final PathFilter filter = createPathFilter(path); TreeFilter group = PathFilterGroup.create(Collections.singleton(filter)); walk.setTreeFilter(group); walk.setRevFilter(new RevFilter() { @Override public boolean include(RevWalk walker, RevCommit cmit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException { return commitChangesFile(path, filter, cmit); } @Override public RevFilter clone() { return this; } }); walk.setRetainBody(retainCommitBody); final RevCommit startingCommit = walk.parseCommit(startCommit); walk.markStart(startingCommit); visitCommits(walk, func); } finally { walk.dispose(); } }
From source file:org.apache.maven.scm.provider.git.jgit.command.JGitUtils.java
License:Apache License
/** * Get a list of commits between two revisions. * * @param repo the repository to work on * @param sortings sorting//from w w w . j av a2 s .c o m * @param fromRev start revision * @param toRev if null, falls back to head * @param fromDate from which date on * @param toDate until which date * @param maxLines max number of lines * @return a list of commits, might be empty, but never <code>null</code> * @throws IOException * @throws MissingObjectException * @throws IncorrectObjectTypeException */ public static List<RevCommit> getRevCommits(Repository repo, RevSort[] sortings, String fromRev, String toRev, final Date fromDate, final Date toDate, int maxLines) throws IOException, MissingObjectException, IncorrectObjectTypeException { List<RevCommit> revs = new ArrayList<RevCommit>(); RevWalk walk = new RevWalk(repo); ObjectId fromRevId = fromRev != null ? repo.resolve(fromRev) : null; ObjectId toRevId = toRev != null ? repo.resolve(toRev) : null; if (sortings == null || sortings.length == 0) { sortings = new RevSort[] { RevSort.TOPO, RevSort.COMMIT_TIME_DESC }; } for (final RevSort s : sortings) { walk.sort(s, true); } if (fromDate != null && toDate != null) { //walk.setRevFilter( CommitTimeRevFilter.between( fromDate, toDate ) ); walk.setRevFilter(new RevFilter() { @Override public boolean include(RevWalk walker, RevCommit cmit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException { int cmtTime = cmit.getCommitTime(); return (cmtTime >= (fromDate.getTime() / 1000)) && (cmtTime <= (toDate.getTime() / 1000)); } @Override public RevFilter clone() { return this; } }); } else { if (fromDate != null) { walk.setRevFilter(CommitTimeRevFilter.after(fromDate)); } if (toDate != null) { walk.setRevFilter(CommitTimeRevFilter.before(toDate)); } } if (fromRevId != null) { RevCommit c = walk.parseCommit(fromRevId); c.add(RevFlag.UNINTERESTING); RevCommit real = walk.parseCommit(c); walk.markUninteresting(real); } if (toRevId != null) { RevCommit c = walk.parseCommit(toRevId); c.remove(RevFlag.UNINTERESTING); RevCommit real = walk.parseCommit(c); walk.markStart(real); } else { final ObjectId head = repo.resolve(Constants.HEAD); if (head == null) { throw new RuntimeException("Cannot resolve " + Constants.HEAD); } RevCommit real = walk.parseCommit(head); walk.markStart(real); } int n = 0; for (final RevCommit c : walk) { n++; if (maxLines != -1 && n > maxLines) { break; } revs.add(c); } return revs; }
From source file:org.eclipse.egit.core.CommitUtil.java
License:Open Source License
/** * Returns whether the commits are on the current branch, ie. if they are * reachable from the current HEAD./*from ww w .ja v a 2s . c o m*/ * * @param commits * the commits to check * @param repository * the repository * @return true if the commits are reachable from HEAD * @throws IOException * if there is an I/O error */ public static boolean areCommitsInCurrentBranch(Collection<RevCommit> commits, Repository repository) throws IOException { try (RevWalk walk = new RevWalk(repository)) { ObjectId headCommitId = repository.resolve(Constants.HEAD); RevCommit headCommit = walk.parseCommit(headCommitId); for (final RevCommit commit : commits) { walk.reset(); walk.markStart(headCommit); RevFilter revFilter = new RevFilter() { @Override public boolean include(RevWalk walker, RevCommit cmit) throws StopWalkException, MissingObjectException, IncorrectObjectTypeException, IOException { return cmit.equals(commit); } @Override public RevFilter clone() { return null; } }; walk.setRevFilter(revFilter); if (walk.next() == null) return false; } return true; } }