use of org.eclipse.jgit.diff.DiffEntry in project gitiles by GerritCodeReview.
the class CommitJsonData method toJsonData.
private static List<Diff> toJsonData(DiffList dl) {
if (dl.entries == null) {
return ImmutableList.of();
}
List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size());
for (DiffEntry de : dl.entries) {
Diff d = new Diff();
d.type = de.getChangeType().name().toLowerCase();
d.oldId = de.getOldId().name();
d.oldMode = de.getOldMode().getBits();
d.oldPath = de.getOldPath();
d.newId = de.getNewId().name();
d.newMode = de.getNewMode().getBits();
d.newPath = de.getNewPath();
switch(de.getChangeType()) {
case COPY:
case RENAME:
d.score = de.getScore();
break;
case ADD:
case DELETE:
case MODIFY:
default:
break;
}
result.add(d);
}
return result;
}
use of org.eclipse.jgit.diff.DiffEntry in project gitiles by GerritCodeReview.
the class CommitSoyData method toSoyData.
private static Object toSoyData(GitilesView view, DiffList dl) {
if (dl.oldRevision == null) {
return NullData.INSTANCE;
}
GitilesView.Builder diffUrl = GitilesView.diff().copyFrom(view).setOldRevision(dl.oldRevision).setRevision(dl.revision).setPathPart("");
List<Object> result = Lists.newArrayListWithCapacity(dl.entries.size());
for (DiffEntry e : dl.entries) {
Map<String, Object> entry = Maps.newHashMapWithExpectedSize(5);
ChangeType type = e.getChangeType();
if (type != DELETE) {
entry.put("path", e.getNewPath());
entry.put("url", GitilesView.path().copyFrom(view).setRevision(dl.revision).setPathPart(e.getNewPath()).toUrl());
} else {
entry.put("path", e.getOldPath());
entry.put("url", GitilesView.path().copyFrom(view).setRevision(dl.oldRevision).setPathPart(e.getOldPath()).toUrl());
}
entry.put("diffUrl", diffUrl.setAnchor("F" + result.size()).toUrl());
entry.put("changeType", e.getChangeType().toString());
if (type == COPY || type == RENAME) {
entry.put("oldPath", e.getOldPath());
}
result.add(entry);
}
return result;
}
use of org.eclipse.jgit.diff.DiffEntry in project egit by eclipse.
the class CreatePatchOperation method createDiffFormatter.
private DiffFormatter createDiffFormatter(final ByteArrayOutputStream outputStream, IProgressMonitor monitor) {
DiffFormatter diffFmt = new DiffFormatter(outputStream) {
private IProject project;
@Override
public void format(DiffEntry ent) throws IOException, CorruptObjectException, MissingObjectException {
// changes
if (DiffHeaderFormat.WORKSPACE == headerFormat) {
IProject p = getProject(ent);
if (p != null && !p.equals(project)) {
project = p;
getOutputStream().write(// $NON-NLS-1$ //$NON-NLS-2$
encodeASCII("#P " + project.getName() + "\n"));
}
}
super.format(ent);
}
};
diffFmt.setProgressMonitor(new EclipseGitProgressTransformer(monitor));
return diffFmt;
}
use of org.eclipse.jgit.diff.DiffEntry 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;
}
use of org.eclipse.jgit.diff.DiffEntry in project egit by eclipse.
the class CompareCoreUtils method getChangeDiffEntry.
/**
* Get the {@link DiffEntry} corresponding to a change in file path. If the
* file was renamed, the resulting {@link DiffEntry} will contain the old
* path and blob ID. If the file was only added, null will be returned.
*
* @param repository
* @param newPath
* path of the file in new commit
* @param newCommit
* new commit
* @param oldCommit
* old commit, e.g. parent commit of newCommit
* @param objectReader
* reader for the repository
* @return the diff entry corresponding to the change for path, or null if
* none could be found
* @throws IOException
*/
public static DiffEntry getChangeDiffEntry(Repository repository, String newPath, RevCommit newCommit, RevCommit oldCommit, ObjectReader objectReader) throws IOException {
try (TreeWalk walk = new TreeWalk(objectReader)) {
walk.setRecursive(true);
walk.addTree(oldCommit.getTree());
walk.addTree(newCommit.getTree());
List<DiffEntry> entries = DiffEntry.scan(walk);
for (DiffEntry diff : entries) {
if (diff.getChangeType() == ChangeType.MODIFY && newPath.equals(diff.getNewPath()))
return diff;
}
if (entries.size() < 2)
return null;
RenameDetector detector = new RenameDetector(repository);
detector.addAll(entries);
List<DiffEntry> renames = detector.compute(walk.getObjectReader(), NullProgressMonitor.INSTANCE);
for (DiffEntry diff : renames) {
if (diff.getChangeType() == ChangeType.RENAME && newPath.equals(diff.getNewPath()))
return diff;
}
return null;
}
}
Aggregations