List of usage examples for org.eclipse.jgit.treewalk TreeWalk getDepth
public int getDepth()
From source file:com.gitblit.utils.IssueUtils.java
License:Apache License
/** * Returns all the issues in the repository. Querying issues from the * repository requires deserializing all changes for all issues. This is an * expensive process and not recommended. Issues should be indexed by Lucene * and queries should be executed against that index. * //from w w w .j a v a2 s. co m * @param repository * @param filter * optional issue filter to only return matching results * @return a list of issues */ public static List<IssueModel> getIssues(Repository repository, IssueFilter filter) { List<IssueModel> list = new ArrayList<IssueModel>(); RefModel issuesBranch = getIssuesBranch(repository); if (issuesBranch == null) { return list; } // Collect the set of all issue paths Set<String> issuePaths = new HashSet<String>(); final TreeWalk tw = new TreeWalk(repository); try { RevCommit head = JGitUtils.getCommit(repository, GB_ISSUES); tw.addTree(head.getTree()); tw.setRecursive(false); while (tw.next()) { if (tw.getDepth() < 2 && tw.isSubtree()) { tw.enterSubtree(); if (tw.getDepth() == 2) { issuePaths.add(tw.getPathString()); } } } } catch (IOException e) { error(e, repository, "{0} failed to query issues"); } finally { tw.release(); } // Build each issue and optionally filter out unwanted issues for (String issuePath : issuePaths) { RevWalk rw = new RevWalk(repository); try { RevCommit start = rw.parseCommit(repository.resolve(GB_ISSUES)); rw.markStart(start); } catch (Exception e) { error(e, repository, "Failed to find {1} in {0}", GB_ISSUES); } TreeFilter treeFilter = AndTreeFilter.create(PathFilterGroup.createFromStrings(issuePath), TreeFilter.ANY_DIFF); rw.setTreeFilter(treeFilter); Iterator<RevCommit> revlog = rw.iterator(); List<RevCommit> commits = new ArrayList<RevCommit>(); while (revlog.hasNext()) { commits.add(revlog.next()); } // release the revwalk rw.release(); if (commits.size() == 0) { LOGGER.warn("Failed to find changes for issue " + issuePath); continue; } // sort by commit order, first commit first Collections.reverse(commits); StringBuilder sb = new StringBuilder("["); boolean first = true; for (RevCommit commit : commits) { if (!first) { sb.append(','); } String message = commit.getFullMessage(); // commit message is formatted: C ISSUEID\n\nJSON // C is an single char commit code // ISSUEID is an SHA-1 hash String json = message.substring(43); sb.append(json); first = false; } sb.append(']'); // Deserialize the JSON array as a Collection<Change>, this seems // slightly faster than deserializing each change by itself. Collection<Change> changes = JsonUtils.fromJsonString(sb.toString(), new TypeToken<Collection<Change>>() { }.getType()); // create an issue object form the changes IssueModel issue = buildIssue(changes, true); // add the issue, conditionally, to the list if (filter == null) { list.add(issue); } else { if (filter.accept(issue)) { list.add(issue); } } } // sort the issues by creation Collections.sort(list); return list; }
From source file:de.codesourcery.gittimelapse.GitHelper.java
License:Apache License
protected void visitCommitTree(String path, PathFilter filter, RevCommit current, ITreeVisitor treeVisitor) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { final String cleanPath = stripRepoBaseDir(path); int cleanPathSize = 0; for (int i = 0; i < cleanPath.length(); i++) { if (cleanPath.charAt(i) == '/') { cleanPathSize++;//from w ww. j a v a 2s . c om } } final TreeWalk tree = new TreeWalk(repository); try { tree.setFilter(filter); tree.setRecursive(true); tree.addTree(current.getTree()); while (tree.next()) { if (tree.getDepth() == cleanPathSize) { if (!treeVisitor.visit(tree)) { return; } } if (tree.isSubtree()) { tree.enterSubtree(); } } } finally { tree.release(); } }
From source file:org.eclipse.ptp.internal.rdt.sync.git.core.GitSyncFileFilter.java
License:Open Source License
/** * Returns ignored files in the index/*from w w w . ja va 2 s. com*/ * * @param ref reference to compute list of files for. If null use index. * * @return set of ignored files * @throws IOException * on file system problems */ public Set<String> getIgnoredFiles(RevTree ref) throws IOException { Repository repo = jgitRepo.getRepository(); TreeWalk treeWalk = new TreeWalk(repo); if (ref == null) { DirCache dirCache = repo.readDirCache(); treeWalk.addTree(new DirCacheIterator(dirCache)); } else { treeWalk.addTree(ref); } HashSet<String> ignoredFiles = new HashSet<String>(); int ignoreDepth = Integer.MAX_VALUE; // if the current subtree is ignored - than this is the depth at which to ignoring // starts while (treeWalk.next()) { boolean isSubtree = treeWalk.isSubtree(); int depth = treeWalk.getDepth(); String path = treeWalk.getPathString(); if (isSubtree) { treeWalk.enterSubtree(); } if (depth > ignoreDepth) { if (!isSubtree) { ignoredFiles.add(path); } continue; } if (depth <= ignoreDepth) { // sibling or parent of ignore subtree => reset ignoreDepth = Integer.MAX_VALUE; } if (shouldIgnore(path, isSubtree)) { if (isSubtree) { ignoreDepth = depth; } else { ignoredFiles.add(path); } } } return ignoredFiles; }
From source file:org.eclipse.ptp.internal.rdt.sync.git.core.GitSyncFileFilter.java
License:Open Source License
/** * Get all different files (modified/changed, missing/removed, untracked/added) * //from ww w.j av a2 s . c o m * assumes that no files are in conflict (don't call during merge) * * @return different files * @throws IOException * on file system problems */ public DiffFiles getDiffFiles() throws IOException { final int INDEX = 0; final int WORKDIR = 1; assert (!jgitRepo.inUnresolvedMergeState()); Repository repo = jgitRepo.getRepository(); TreeWalk treeWalk = new TreeWalk(repo); treeWalk.addTree(new DirCacheIterator(repo.readDirCache())); treeWalk.addTree(new FileTreeIterator(repo)); // don't honor ignores - we do it manually instead. Doing it all with the filter // would require a WorkingTreeIterator that does the ignore handling correctly // (both directory including bugs 401161 and only using info/exclude, not .gitignore) treeWalk.setFilter(new IndexDiffFilter(INDEX, WORKDIR, false)); DiffFiles diffFiles = new DiffFiles(); int ignoreDepth = Integer.MAX_VALUE; // if the current subtree is ignored - than this is the depth at which ignoring // starts while (treeWalk.next()) { DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX, DirCacheIterator.class); String path = treeWalk.getPathString(); boolean isSubtree = treeWalk.isSubtree(); int depth = treeWalk.getDepth(); if (dirCacheIterator != null || // in index => either missing or modified !shouldIgnore(path, isSubtree)) { // not in index => untracked if (depth <= ignoreDepth) { ignoreDepth = Integer.MAX_VALUE; } if (dirCacheIterator != null && isSubtree && ignoreDepth == Integer.MAX_VALUE && shouldIgnore(path, isSubtree)) { ignoreDepth = depth; } if (isSubtree) { treeWalk.enterSubtree(); diffFiles.dirSet.add(path); } else if (dirCacheIterator != null || ignoreDepth == Integer.MAX_VALUE) { WorkingTreeIterator workTreeIter = treeWalk.getTree(WORKDIR, WorkingTreeIterator.class); if (workTreeIter != null) { diffFiles.added.add(path); } else { diffFiles.removed.add(path); } } } } return diffFiles; }