use of org.eclipse.jgit.treewalk.CanonicalTreeParser 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.CanonicalTreeParser 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.CanonicalTreeParser in project searchcode-server by boyter.
the class IndexGitHistoryJob method getRevisionChanges.
public void getRevisionChanges(Repository localRepository, Git git, GitChangeSet oldRevison, GitChangeSet newRevision) throws IOException, GitAPIException {
ObjectId oldHead = localRepository.resolve(oldRevison.getRevision() + "^{tree}");
ObjectId newHead = localRepository.resolve(newRevision.getRevision() + "^{tree}");
ObjectReader reader = localRepository.newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldHead);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, newHead);
List<DiffEntry> entries = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();
GitService gs = new GitService();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
for (DiffEntry entry : entries) {
if ("DELETE".equals(entry.getChangeType().name())) {
System.out.println("DEL " + entry.getOldPath());
String contents = gs.fetchFileRevision(localRepository.getWorkTree().toString() + "/.git", oldRevison.getRevision(), entry.getOldPath());
CodeIndexDocument cd = new CodeIndexDocument(entry.getNewPath(), "thumbor", entry.getOldPath(), entry.getOldPath(), entry.getOldPath(), "md5hash", "Java", contents.split("\\r?\\n").length, contents, "", oldRevison.getAuthor());
cd.setRevision(oldRevison.getRevision());
cd.setYearMonthDay(sdf.format(oldRevison.getExpiry()));
cd.setYearMonth(cd.getYearMonthDay().substring(0, 6));
cd.setYear(cd.getYearMonthDay().substring(0, 4));
cd.setMessage(oldRevison.getMessage());
cd.setDeleted("TRUE");
Singleton.getCodeIndexer().indexTimeDocument(cd);
} else {
System.out.println("ADD " + entry.getNewPath());
String contents = gs.fetchFileRevision(localRepository.getWorkTree().toString() + "/.git", newRevision.getRevision(), entry.getNewPath());
CodeIndexDocument cd = new CodeIndexDocument(entry.getNewPath(), "thumbor", entry.getNewPath(), entry.getNewPath(), entry.getNewPath(), "md5hash", "Java", contents.split("\\r?\\n").length, contents, "", newRevision.getAuthor());
cd.setRevision(newRevision.getRevision());
cd.setYearMonthDay(sdf.format(oldRevison.getExpiry()));
cd.setYearMonth(cd.getYearMonthDay().substring(0, 6));
cd.setYear(cd.getYearMonthDay().substring(0, 4));
cd.setMessage(newRevision.getMessage());
cd.setDeleted("FALSE");
Singleton.getCodeIndexer().indexTimeDocument(cd);
}
}
}
use of org.eclipse.jgit.treewalk.CanonicalTreeParser 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.CanonicalTreeParser in project che by eclipse.
the class JGitDiffPage method commitToIndex.
/**
* Show changes between specified revision and index. If
* <code>commitId == null</code> then view changes between HEAD and index.
*
* @param commitId
* id of commit, pass <code>null</code> is the same as pass HEAD
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> commitToIndex(String commitId, DiffFormatter formatter) throws IOException {
if (commitId == null) {
commitId = Constants.HEAD;
}
ObjectId commitA = repository.resolve(commitId);
if (commitA == null) {
throw new IllegalArgumentException("Invalid commit id " + commitId);
}
RevTree treeA;
try (RevWalk revWalkA = new RevWalk(repository)) {
treeA = revWalkA.parseTree(commitA);
}
DirCache dirCache = null;
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
dirCache = repository.lockDirCache();
CanonicalTreeParser iterA = new CanonicalTreeParser();
iterA.reset(reader, treeA);
DirCacheIterator iterB = new DirCacheIterator(dirCache);
if (!params.isNoRenames()) {
// Use embedded RenameDetector it works well with index and
// revision history.
formatter.setDetectRenames(true);
int renameLimit = params.getRenameLimit();
if (renameLimit > 0) {
formatter.getRenameDetector().setRenameLimit(renameLimit);
}
}
diff = formatter.scan(iterA, iterB);
} finally {
if (dirCache != null) {
dirCache.unlock();
}
}
return diff;
}
Aggregations