List of usage examples for org.eclipse.jgit.treewalk.filter AndTreeFilter create
public static TreeFilter create(Collection<TreeFilter> list)
From source file:ch.sourcepond.maven.release.scm.git.GitRepository.java
License:Apache License
private void filterOutOtherModulesChanges(final String modulePath, final List<String> childModules, final RevWalk walk) { final boolean isRootModule = ".".equals(modulePath); final boolean isMultiModuleProject = !isRootModule || !childModules.isEmpty(); final List<TreeFilter> treeFilters = new LinkedList<>(); treeFilters.add(TreeFilter.ANY_DIFF); if (isMultiModuleProject) { if (!isRootModule) { // for sub-modules, look for changes only in the sub-module // path... treeFilters.add(PathFilter.create(modulePath)); }//from ww w.j a va 2 s . c o m // ... but ignore any sub-modules of the current sub-module, because // they can change independently of the current module for (final String childModule : childModules) { final String path = isRootModule ? childModule : modulePath + "/" + childModule; treeFilters.add(PathFilter.create(path).negate()); } } final TreeFilter treeFilter = treeFilters.size() == 1 ? treeFilters.get(0) : AndTreeFilter.create(treeFilters); walk.setTreeFilter(treeFilter); }
From source file:org.commonjava.aprox.subsys.git.GitManager.java
License:Apache License
private boolean verifyChangesExist(final Collection<String> paths) throws GitSubsystemException { try {/*from w w w .j a v a 2 s . co m*/ final DiffFormatter formatter = new DiffFormatter(System.out); formatter.setRepository(repo); // resolve the HEAD object final ObjectId oid = repo.resolve(Constants.HEAD); if (oid == null) { // if there's no head, then these must be real changes... return true; } // reset a new tree object to the HEAD final RevWalk walk = new RevWalk(repo); final RevCommit commit = walk.parseCommit(oid); final RevTree treeWalk = walk.parseTree(commit); // construct filters for the paths we're trying to add/commit final List<TreeFilter> filters = new ArrayList<>(); for (final String path : paths) { filters.add(PathFilter.create(path)); } // we're interested in trees with an actual diff. This should improve walk performance. filters.add(TreeFilter.ANY_DIFF); // set the path filters from above walk.setTreeFilter(AndTreeFilter.create(filters)); // setup the tree for doing the comparison vs. uncommitted files final CanonicalTreeParser tree = new CanonicalTreeParser(); final ObjectReader oldReader = repo.newObjectReader(); try { tree.reset(oldReader, treeWalk.getId()); } finally { oldReader.release(); } walk.dispose(); // this iterator will actually scan the uncommitted files for diff'ing final FileTreeIterator files = new FileTreeIterator(repo); // do the scan. final List<DiffEntry> entries = formatter.scan(tree, files); // we're not interested in WHAT the differences are, only that there are differences. return entries != null && !entries.isEmpty(); } catch (final IOException e) { throw new GitSubsystemException("Failed to scan for actual changes among: %s. Reason: %s", e, paths, e.getMessage()); } }
From source file:org.exist.git.xquery.Status.java
License:Open Source License
public void diff(final Repository repository, final String revstr, final WorkingTreeIterator initialWorkingTreeIterator, final StatusBuilder builder, final String folder, boolean recursive) throws IOException { RevTree tree = null;/* w w w. j a va2 s . c om*/ ObjectId objectId = repository.resolve(revstr); if (objectId != null) tree = new RevWalk(repository).parseTree(objectId); else tree = null; PathFilter filter = folder == null || folder.isEmpty() ? null : PathFilter.create(folder); IndexDiffFilter indexDiffFilter; DirCache dirCache = repository.readDirCache(); TreeWalk treeWalk = new TreeWalk(repository); // TreeWalk treeWalk = TreeWalk.forPath(repository, folder, tree); treeWalk.setRecursive(recursive); // add the trees (tree, dirchache, workdir) if (tree != null) treeWalk.addTree(tree); else treeWalk.addTree(new EmptyTreeIterator()); treeWalk.addTree(new DirCacheIterator(dirCache)); treeWalk.addTree(initialWorkingTreeIterator); Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4); if (filter != null) filters.add(filter); filters.add(new SkipWorkTreeFilter(INDEX)); indexDiffFilter = new IndexDiffFilter(INDEX, WORKDIR); filters.add(indexDiffFilter); treeWalk.setFilter(AndTreeFilter.create(filters)); if (filter != null) { while (treeWalk.next()) { if (filter.include(treeWalk)) { if (filter.isDone(treeWalk)) { if (treeWalk.isSubtree()) { treeWalk.enterSubtree(); } break; } else if (treeWalk.isSubtree()) { treeWalk.enterSubtree(); } } } } while (treeWalk.next()) { AbstractTreeIterator treeIterator = treeWalk.getTree(TREE, AbstractTreeIterator.class); DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX, DirCacheIterator.class); WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR, WorkingTreeIterator.class); if (dirCacheIterator != null) { final DirCacheEntry dirCacheEntry = dirCacheIterator.getDirCacheEntry(); if (dirCacheEntry != null && dirCacheEntry.getStage() > 0) { builder.conflict(treeWalk.getFileMode(2), treeWalk.getPathString()); continue; } } if (treeIterator != null) { if (dirCacheIterator != null) { if (!treeIterator.idEqual(dirCacheIterator) || treeIterator.getEntryRawMode() != dirCacheIterator.getEntryRawMode()) { // in repo, in index, content diff => changed builder.changed(treeWalk.getFileMode(2), treeWalk.getPathString()); continue; } } else { // in repo, not in index => removed builder.removed(treeWalk.getFileMode(2), treeWalk.getPathString()); if (workingTreeIterator != null) //XXX: 2 statuses builder.untracked(treeWalk.getFileMode(2), treeWalk.getPathString()); continue; } } else { if (dirCacheIterator != null) { // not in repo, in index => added builder.added(treeWalk.getFileMode(2), treeWalk.getPathString()); continue; } else { // not in repo, not in index => untracked if (workingTreeIterator != null && !workingTreeIterator.isEntryIgnored()) { builder.untracked(treeWalk.getFileMode(2), treeWalk.getPathString()); continue; } } } if (dirCacheIterator != null) { if (workingTreeIterator == null) { // in index, not in workdir => missing builder.missing(treeWalk.getFileMode(2), treeWalk.getPathString()); continue; } else { if (dirCacheIterator.getDirCacheEntry() == null) { //XXX: null on collections - to fix // builder.unchanged(treeWalk.getFileMode(2), treeWalk.getPathString()); } else if (workingTreeIterator.isModified(dirCacheIterator.getDirCacheEntry(), true)) { // in index, in workdir, content differs => modified builder.modified(treeWalk.getFileMode(2), treeWalk.getPathString()); continue; } } } builder.unchanged(treeWalk.getFileMode(2), treeWalk.getPathString()); } // for (String path : indexDiffFilter.getUntrackedFolders()) { // builder.untrackedFolders(path); // } // for (String path : indexDiffFilter.getIgnoredPaths()) { //XXX: to fix FileMode builder.ignored(FileMode.REGULAR_FILE, path); } }
From source file:org.gitective.core.PathFilterUtils.java
License:Open Source License
private static TreeFilter andDiff(final TreeFilter[] filters) { final TreeFilter filter; if (filters.length > 1) filter = AndTreeFilter.create(filters); else/*from w w w . j a v a 2 s.c o m*/ filter = filters[0]; return AndTreeFilter.create(filter, ANY_DIFF); }
From source file:org.ms123.common.git.GitServiceImpl.java
License:Open Source License
public Map getWorkingTree(@PName("name") String repoName, @PName("path") @POptional String path, @PName("depth") @POptional @PDefaultInt(100) Integer depth, @PName("includeTypeList") @POptional List<String> includeTypeList, @PName("includePathList") @POptional List<String> includePathList, @PName("excludePathList") @POptional List<String> excludePathList, @PName("mapping") @POptional Map mapping) throws RpcException { try {//from w w w . j a v a2 s . c om if (depth == null) depth = 100; String gitSpace = System.getProperty("git.repos"); File gitDir = new File(gitSpace, repoName + "/.git"); if (!gitDir.exists()) { throw new RpcException(ERROR_FROM_METHOD, 100, "GitService.getWorkingTree:Repo(" + repoName + ") not exists"); } File repoDir = new File(gitSpace, repoName); Git gitObject = Git.open(new File(gitSpace, repoName)); TreeWalk treeWalk = new TreeWalk(gitObject.getRepository()); FileTreeIterator newTree = null; String rootPath = "root"; String type = "sw.project"; File basePath = repoDir; String title = repoName; if (path == null) { newTree = new FileTreeIterator(gitObject.getRepository()); } else { File f = new File(gitObject.getRepository().getDirectory().getParentFile(), path); debug("f:" + f); newTree = new FileTreeIterator(f, FS.detect(), gitObject.getRepository().getConfig().get(WorkingTreeOptions.KEY)); rootPath = path; type = "sw.directory"; title += "/" + path; basePath = new File(basePath, path); } treeWalk.addTree(newTree); treeWalk.setRecursive(true); Collection<TreeFilter> filterList = new HashSet(); TreeFilter pathFilter = PathPatternFilter.create("^[a-zA-Z].*$", includePathList, excludePathList, 0); filterList.add(pathFilter); if (includeTypeList != null && includeTypeList.size() > 0) { filterList.add(TypeFilter.create(basePath, includeTypeList)); } if (filterList.size() > 1) { TreeFilter andFilter = AndTreeFilter.create(filterList); treeWalk.setFilter(andFilter); } else { treeWalk.setFilter(pathFilter); } treeWalk.setPostOrderTraversal(true); Map<String, Map> parentMap = new HashMap(); Map root = new HashMap(); root.put("path", rootPath); root.put("title", repoName); root.put("value", rootPath); root.put("type", type); root.put("children", new ArrayList()); parentMap.put("root", root); while (true) { if (!treeWalk.next()) { break; } String pathString = new String(treeWalk.getRawPath()); // String pathString = treeWalk.getPathString(); Node[] nodes = getNodes(pathString); for (int i = 0; i < nodes.length && i < depth; i++) { if (parentMap.get(nodes[i].path) == null) { Map map = getNodeMap(nodes[i], i < (nodes.length - 1), basePath, mapping); map.put("children", new ArrayList()); parentMap.put(nodes[i].path, map); Map pmap = parentMap.get(nodes[i].parent); if (pmap != null) { List<Map> children = (List) pmap.get("children"); children.add(map); } } } } // m_js.prettyPrint(true); // String ser = m_js.deepSerialize(parentMap.get("root")); // debug("Tree" + ser); return parentMap.get("root"); } catch (Exception e) { if (e instanceof RpcException) throw (RpcException) e; throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "GitService.getWorkingTree:", e); } finally { } }