use of org.eclipse.jgit.diff.DiffEntry 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.diff.DiffEntry 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.diff.DiffEntry in project che by eclipse.
the class JGitDiffPage method writeNamesAndStatus.
private void writeNamesAndStatus(List<DiffEntry> diff, OutputStream out) throws IOException {
PrintWriter writer = new PrintWriter(out);
int diffSize = diff.size();
for (DiffEntry de : diff) {
if (de.getChangeType() == ChangeType.ADD) {
writer.print("A\t" + de.getNewPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
} else if (de.getChangeType() == ChangeType.DELETE) {
writer.print("D\t" + de.getOldPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
} else if (de.getChangeType() == ChangeType.MODIFY) {
writer.print("M\t" + de.getNewPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
} else if (de.getChangeType() == ChangeType.COPY) {
writer.print("C\t" + de.getOldPath() + '\t' + de.getNewPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
} else if (de.getChangeType() == ChangeType.RENAME) {
writer.print("R\t" + de.getOldPath() + '\t' + de.getNewPath() + (diffSize != diff.indexOf(de) + 1 ? lineSeparator() : ""));
}
}
writer.flush();
}
use of org.eclipse.jgit.diff.DiffEntry in project che by eclipse.
the class JGitDiffPage method emptyToCommit.
/**
* Show changes between specified revision and empty 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> emptyToCommit(String commitId, DiffFormatter formatter) throws IOException {
ObjectId commit = repository.resolve(commitId);
checkArgument(commit != null, "Invalid commit id " + commitId);
RevTree tree;
try (RevWalk revWalkA = new RevWalk(repository)) {
tree = revWalkA.parseTree(commit);
}
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
CanonicalTreeParser iterator = new CanonicalTreeParser();
iterator.reset(reader, tree);
diff = formatter.scan(new EmptyTreeIterator(), iterator);
}
return diff;
}
use of org.eclipse.jgit.diff.DiffEntry in project compiler by boalang.
the class GitCommit method getChangeFiles.
private void getChangeFiles(final RevCommit parent, final RevCommit rc, final HashMap<String, String> rChangedPaths, final HashMap<String, String> rRemovedPaths, final HashMap<String, String> rAddedPaths) {
final DiffFormatter df = new DiffFormatter(NullOutputStream.INSTANCE);
df.setRepository(repository);
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
try {
final AbstractTreeIterator parentIter;
if (parent == null)
parentIter = new EmptyTreeIterator();
else
parentIter = new CanonicalTreeParser(null, repository.newObjectReader(), parent.getTree());
for (final DiffEntry diff : df.scan(parentIter, new CanonicalTreeParser(null, repository.newObjectReader(), rc.getTree()))) {
if (diff.getChangeType() == ChangeType.MODIFY || diff.getChangeType() == ChangeType.COPY || diff.getChangeType() == ChangeType.RENAME) {
if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB && diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
String path = diff.getNewPath();
rChangedPaths.put(path, diff.getOldPath());
filePathGitObjectIds.put(path, diff.getNewId().toObjectId());
}
} else if (diff.getChangeType() == ChangeType.ADD) {
if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
String path = diff.getNewPath();
rAddedPaths.put(path, null);
filePathGitObjectIds.put(path, diff.getNewId().toObjectId());
}
} else if (diff.getChangeType() == ChangeType.DELETE) {
if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB) {
rRemovedPaths.put(diff.getOldPath(), diff.getOldPath());
}
}
}
} catch (final IOException e) {
if (debug)
System.err.println("Git Error getting commit diffs: " + e.getMessage());
}
df.close();
}
Aggregations