use of org.eclipse.jgit.treewalk.EmptyTreeIterator 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.treewalk.EmptyTreeIterator 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();
}
use of org.eclipse.jgit.treewalk.EmptyTreeIterator in project MGit by maks.
the class CommitDiffTask method getCommitDiff.
public boolean getCommitDiff() {
try {
Repository repo = mRepo.getGit().getRepository();
mDiffOutput = new ByteArrayOutputStream();
mDiffFormatter = new DiffFormatter(mDiffOutput);
mDiffFormatter.setRepository(repo);
AbstractTreeIterator mOldCommitTreeIterator = mRepo.isInitialCommit(mNewCommit) ? new EmptyTreeIterator() : getTreeIterator(repo, mOldCommit);
AbstractTreeIterator mNewCommitTreeIterator = getTreeIterator(repo, mNewCommit);
mDiffEntries = mDiffFormatter.scan(mOldCommitTreeIterator, mNewCommitTreeIterator);
if (mShowDescription) {
ObjectId newCommitId = repo.resolve(mNewCommit);
mCommits = mRepo.getGit().log().add(newCommitId).setMaxCount(1).call();
} else {
mCommits = new ArrayList<RevCommit>();
}
return true;
} catch (GitAPIException e) {
setException(e);
} catch (IncorrectObjectTypeException e) {
setException(e, R.string.error_diff_failed);
} catch (AmbiguousObjectException e) {
setException(e, R.string.error_diff_failed);
} catch (IOException e) {
setException(e, R.string.error_diff_failed);
} catch (IllegalStateException e) {
setException(e, R.string.error_diff_failed);
} catch (NullPointerException e) {
setException(e, R.string.error_diff_failed);
} catch (StopTaskException e) {
}
return false;
}
use of org.eclipse.jgit.treewalk.EmptyTreeIterator in project gitiles by GerritCodeReview.
the class DiffServlet method getTreeIterator.
private static AbstractTreeIterator getTreeIterator(RevWalk walk, ObjectId id) throws IOException {
if (!id.equals(ObjectId.zeroId())) {
CanonicalTreeParser p = new CanonicalTreeParser();
p.reset(walk.getObjectReader(), walk.parseTree(id));
return p;
}
return new EmptyTreeIterator();
}
use of org.eclipse.jgit.treewalk.EmptyTreeIterator in project egit by eclipse.
the class FileDiff method compute.
/**
* Computer file diffs for specified tree walk and commit
*
* @param repository
* @param walk
* @param commit
* @param parents
* @param markTreeFilters
* optional filters for marking entries, see
* {@link #isMarked(int)}
* @return non-null but possibly empty array of file diffs
* @throws MissingObjectException
* @throws IncorrectObjectTypeException
* @throws CorruptObjectException
* @throws IOException
*/
public static FileDiff[] compute(final Repository repository, final TreeWalk walk, final RevCommit commit, final RevCommit[] parents, final TreeFilter... markTreeFilters) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
final ArrayList<FileDiff> r = new ArrayList<>();
if (parents.length > 0) {
walk.reset(trees(commit, parents));
} else {
walk.reset();
walk.addTree(new EmptyTreeIterator());
walk.addTree(commit.getTree());
}
if (walk.getTreeCount() <= 2) {
List<DiffEntry> entries = DiffEntry.scan(walk, false, markTreeFilters);
List<DiffEntry> xentries = new LinkedList<>(entries);
RenameDetector detector = new RenameDetector(repository);
detector.addAll(entries);
List<DiffEntry> renames = detector.compute(walk.getObjectReader(), org.eclipse.jgit.lib.NullProgressMonitor.INSTANCE);
for (DiffEntry m : renames) {
final FileDiff d = new FileDiff(commit, m);
r.add(d);
for (Iterator<DiffEntry> i = xentries.iterator(); i.hasNext(); ) {
DiffEntry n = i.next();
if (m.getOldPath().equals(n.getOldPath()))
i.remove();
else if (m.getNewPath().equals(n.getNewPath()))
i.remove();
}
}
for (DiffEntry m : xentries) {
final FileDiff d = new FileDiff(commit, m);
r.add(d);
}
} else {
// DiffEntry does not support walks with more than two trees
final int nTree = walk.getTreeCount();
final int myTree = nTree - 1;
TreeFilterMarker treeFilterMarker = new TreeFilterMarker(markTreeFilters);
while (walk.next()) {
if (matchAnyParent(walk, myTree))
continue;
int treeFilterMarks = treeFilterMarker.getMarks(walk);
final FileDiffForMerges d = new FileDiffForMerges(commit, treeFilterMarks);
d.path = walk.getPathString();
int m0 = 0;
for (int i = 0; i < myTree; i++) m0 |= walk.getRawMode(i);
final int m1 = walk.getRawMode(myTree);
d.change = ChangeType.MODIFY;
if (m0 == 0 && m1 != 0)
d.change = ChangeType.ADD;
else if (m0 != 0 && m1 == 0)
d.change = ChangeType.DELETE;
else if (m0 != m1 && walk.idEqual(0, myTree))
// there is no ChangeType.TypeChanged
d.change = ChangeType.MODIFY;
d.blobs = new ObjectId[nTree];
d.modes = new FileMode[nTree];
for (int i = 0; i < nTree; i++) {
d.blobs[i] = walk.getObjectId(i);
d.modes[i] = walk.getFileMode(i);
}
r.add(d);
}
}
final FileDiff[] tmp = new FileDiff[r.size()];
r.toArray(tmp);
return tmp;
}
Aggregations