use of org.eclipse.jgit.treewalk.FileTreeIterator in project che by eclipse.
the class JGitDiffPage method commitToWorkingTree.
/**
* Show changes between specified revision and working tree.
*
* @param commitId
* id of commit
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> commitToWorkingTree(String commitId, DiffFormatter formatter) throws IOException {
ObjectId commitA = repository.resolve(commitId);
if (commitA == null) {
File heads = new File(repository.getWorkTree().getPath() + "/.git/refs/heads");
if (heads.exists() && heads.list().length == 0) {
return Collections.emptyList();
}
throw new IllegalArgumentException("Invalid commit id " + commitId);
}
RevTree treeA;
try (RevWalk revWalkA = new RevWalk(repository)) {
treeA = revWalkA.parseTree(commitA);
}
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
CanonicalTreeParser iterA = new CanonicalTreeParser();
iterA.reset(reader, treeA);
FileTreeIterator iterB = new FileTreeIterator(repository);
// Seems bug in DiffFormatter when work with working. Disable detect
// renames by formatter and do it later.
formatter.setDetectRenames(false);
diff = formatter.scan(iterA, iterB);
if (!params.isNoRenames()) {
// Detect renames.
RenameDetector renameDetector = createRenameDetector();
ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB));
renameDetector.addAll(diff);
diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE);
}
}
return diff;
}
use of org.eclipse.jgit.treewalk.FileTreeIterator in project che by eclipse.
the class JGitDiffPage method indexToWorkingTree.
/**
* Show changes between index and working tree.
*
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> indexToWorkingTree(DiffFormatter formatter) throws IOException {
DirCache dirCache = null;
ObjectReader reader = repository.newObjectReader();
List<DiffEntry> diff;
try {
dirCache = repository.lockDirCache();
DirCacheIterator iterA = new DirCacheIterator(dirCache);
FileTreeIterator iterB = new FileTreeIterator(repository);
// Seems bug in DiffFormatter when work with working. Disable detect
// renames by formatter and do it later.
formatter.setDetectRenames(false);
diff = formatter.scan(iterA, iterB);
if (!params.isNoRenames()) {
// Detect renames.
RenameDetector renameDetector = createRenameDetector();
ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB));
renameDetector.addAll(diff);
diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE);
}
} finally {
reader.close();
if (dirCache != null) {
dirCache.unlock();
}
}
return diff;
}
use of org.eclipse.jgit.treewalk.FileTreeIterator 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