use of org.eclipse.jgit.treewalk.filter.TreeFilter in project che by eclipse.
the class JGitDiffPage method writeTo.
@Override
public final void writeTo(OutputStream out) throws IOException {
DiffFormatter formatter = new DiffFormatter(new BufferedOutputStream(out));
formatter.setRepository(repository);
List<String> rawFileFilter = params.getFileFilter();
TreeFilter pathFilter = (rawFileFilter != null && rawFileFilter.size() > 0) ? PathFilterGroup.createFromStrings(rawFileFilter) : TreeFilter.ALL;
formatter.setPathFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));
try {
String commitA = params.getCommitA();
String commitB = params.getCommitB();
boolean cached = params.isCached();
List<DiffEntry> diff;
if (commitA == null && commitB == null && !cached) {
diff = indexToWorkingTree(formatter);
} else if (commitA != null && commitB == null && !cached) {
diff = commitToWorkingTree(commitA, formatter);
} else if (commitA == null && commitB != null) {
diff = emptyToCommit(commitB, formatter);
} else if (commitB == null) {
diff = commitToIndex(commitA, formatter);
} else {
diff = commitToCommit(commitA, commitB, formatter);
}
DiffType type = params.getType();
if (type == DiffType.NAME_ONLY) {
writeNames(diff, out);
} else if (type == DiffType.NAME_STATUS) {
writeNamesAndStatus(diff, out);
} else {
writeRawDiff(diff, formatter);
}
} finally {
formatter.close();
repository.close();
}
}
use of org.eclipse.jgit.treewalk.filter.TreeFilter in project gitblit by gitblit.
the class JGitUtils method getDocuments.
/**
* Returns the list of files in the repository in the specified commit that
* match one of the specified extensions. This is a CASE-SENSITIVE search.
* If the repository does not exist or is empty, an empty list is returned.
*
* @param repository
* @param extensions
* @param objectId
* @return list of files in repository with a matching extension
*/
public static List<PathModel> getDocuments(Repository repository, List<String> extensions, String objectId) {
List<PathModel> list = new ArrayList<PathModel>();
if (!hasCommits(repository)) {
return list;
}
RevCommit commit = getCommit(repository, objectId);
final TreeWalk tw = new TreeWalk(repository);
try {
tw.addTree(commit.getTree());
if (extensions != null && extensions.size() > 0) {
List<TreeFilter> suffixFilters = new ArrayList<TreeFilter>();
for (String extension : extensions) {
if (extension.charAt(0) == '.') {
suffixFilters.add(PathSuffixFilter.create(extension));
} else {
// escape the . since this is a regexp filter
suffixFilters.add(PathSuffixFilter.create("." + extension));
}
}
TreeFilter filter;
if (suffixFilters.size() == 1) {
filter = suffixFilters.get(0);
} else {
filter = OrTreeFilter.create(suffixFilters);
}
tw.setFilter(filter);
tw.setRecursive(true);
}
while (tw.next()) {
list.add(getPathModel(tw, null, commit));
}
} catch (IOException e) {
error(e, repository, "{0} failed to get documents for commit {1}", commit.getName());
} finally {
tw.close();
}
Collections.sort(list);
return list;
}
use of org.eclipse.jgit.treewalk.filter.TreeFilter in project che by eclipse.
the class JGitConnection method getCommitDiffFiles.
private List<DiffCommitFile> getCommitDiffFiles(RevCommit revCommit, String pattern) throws IOException {
List<DiffEntry> diffs;
TreeFilter filter = null;
if (!isNullOrEmpty(pattern)) {
filter = AndTreeFilter.create(PathFilterGroup.createFromStrings(Collections.singleton(pattern)), TreeFilter.ANY_DIFF);
}
List<DiffCommitFile> commitFilesList = new ArrayList<>();
try (TreeWalk tw = new TreeWalk(repository)) {
tw.setRecursive(true);
// and to get the list of DiffEntry.
if (revCommit.getParentCount() > 0) {
RevCommit parent = parseCommit(revCommit.getParent(0));
tw.reset(parent.getTree(), revCommit.getTree());
if (filter != null) {
tw.setFilter(filter);
} else {
tw.setFilter(TreeFilter.ANY_DIFF);
}
diffs = DiffEntry.scan(tw);
} else {
// list of DiffEntry.
try (RevWalk rw = new RevWalk(repository);
DiffFormatter diffFormat = new DiffFormatter(NullOutputStream.INSTANCE)) {
diffFormat.setRepository(repository);
if (filter != null) {
diffFormat.setPathFilter(filter);
}
diffs = diffFormat.scan(new EmptyTreeIterator(), new CanonicalTreeParser(null, rw.getObjectReader(), revCommit.getTree()));
}
}
}
if (diffs != null) {
commitFilesList.addAll(diffs.stream().map(diff -> newDto(DiffCommitFile.class).withOldPath(diff.getOldPath()).withNewPath(diff.getNewPath()).withChangeType(diff.getChangeType().name())).collect(Collectors.toList()));
}
return commitFilesList;
}
use of org.eclipse.jgit.treewalk.filter.TreeFilter in project gitblit by gitblit.
the class JGitUtils method getRevLog.
/**
* Returns a list of commits for the repository or a path within the
* repository. Caller may specify ending revision with objectId. Caller may
* specify offset and maxCount to achieve pagination of results. If the
* repository does not exist or is empty, an empty list is returned.
*
* @param repository
* @param objectId
* if unspecified, HEAD is assumed.
* @param path
* if unspecified, commits for repository are returned. If
* specified, commits for the path are returned.
* @param offset
* @param maxCount
* if < 0, all commits are returned.
* @return a paged list of commits
*/
public static List<RevCommit> getRevLog(Repository repository, String objectId, String path, int offset, int maxCount) {
List<RevCommit> list = new ArrayList<RevCommit>();
if (maxCount == 0) {
return list;
}
if (!hasCommits(repository)) {
return list;
}
try {
// resolve branch
ObjectId startRange = null;
ObjectId endRange;
if (StringUtils.isEmpty(objectId)) {
endRange = getDefaultBranch(repository);
} else {
if (objectId.contains("..")) {
// range expression
String[] parts = objectId.split("\\.\\.");
startRange = repository.resolve(parts[0]);
endRange = repository.resolve(parts[1]);
} else {
// objectid
endRange = repository.resolve(objectId);
}
}
if (endRange == null) {
return list;
}
RevWalk rw = new RevWalk(repository);
rw.markStart(rw.parseCommit(endRange));
if (startRange != null) {
rw.markUninteresting(rw.parseCommit(startRange));
}
if (!StringUtils.isEmpty(path)) {
TreeFilter filter = AndTreeFilter.create(PathFilterGroup.createFromStrings(Collections.singleton(path)), TreeFilter.ANY_DIFF);
rw.setTreeFilter(filter);
}
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 get {1} revlog for path {2}", objectId, path);
}
return list;
}
use of org.eclipse.jgit.treewalk.filter.TreeFilter in project indy by Commonjava.
the class GitManager method verifyChangesExist.
private boolean verifyChangesExist(final Collection<String> paths) throws GitSubsystemException {
return lockAnd(me -> {
try {
final DiffFormatter formatter = new DiffFormatter(System.out);
formatter.setRepository(repo);
final ObjectId oid = repo.resolve(Constants.HEAD);
if (oid == null) {
return true;
}
final RevWalk walk = new RevWalk(repo);
final RevCommit commit = walk.parseCommit(oid);
final RevTree treeWalk = walk.parseTree(commit);
final List<TreeFilter> filters = new ArrayList<>();
for (final String path : paths) {
filters.add(PathFilter.create(path));
}
filters.add(TreeFilter.ANY_DIFF);
walk.setTreeFilter(AndTreeFilter.create(filters));
final CanonicalTreeParser tree = new CanonicalTreeParser();
final ObjectReader oldReader = repo.newObjectReader();
try {
tree.reset(oldReader, treeWalk.getId());
} finally {
oldReader.release();
}
walk.dispose();
final FileTreeIterator files = new FileTreeIterator(repo);
final List<DiffEntry> entries = formatter.scan(tree, files);
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());
}
});
}
Aggregations