use of org.eclipse.jgit.diff.RawTextComparator in project gitblit by gitblit.
the class DiffUtils method getCommitPatch.
/**
* Returns the diff between the two commits for the specified file or folder
* formatted as a patch.
*
* @param repository
* @param baseCommit
* if base commit is unspecified, the patch is generated against
* the primary parent of the specified commit.
* @param commit
* @param path
* if path is specified, the patch is generated only for the
* specified file or folder. if unspecified, the patch is
* generated for the entire diff between the two commits.
* @return patch as a string
*/
public static String getCommitPatch(Repository repository, RevCommit baseCommit, RevCommit commit, String path) {
String diff = null;
try {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
RawTextComparator cmp = RawTextComparator.DEFAULT;
PatchFormatter df = new PatchFormatter(os);
df.setRepository(repository);
df.setDiffComparator(cmp);
df.setDetectRenames(true);
RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
if (commit.getParentCount() > 0) {
final RevWalk rw = new RevWalk(repository);
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
baseTree = parent.getTree();
} else {
// FIXME initial commit. no parent?!
baseTree = commitTree;
}
} else {
baseTree = baseCommit.getTree();
}
List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
if (path != null && path.length() > 0) {
for (DiffEntry diffEntry : diffEntries) {
if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
df.format(diffEntry);
break;
}
}
} else {
df.format(diffEntries);
}
diff = df.getPatch(commit);
df.flush();
} catch (Throwable t) {
LOGGER.error("failed to generate commit diff!", t);
}
return diff;
}
use of org.eclipse.jgit.diff.RawTextComparator in project gitblit by gitblit.
the class DiffUtils method getDiffStat.
/**
* Returns the diffstat between the two commits for the specified file or folder.
*
* @param repository
* @param baseCommit
* if base commit is unspecified, the diffstat is generated against
* the primary parent of the specified commit.
* @param commit
* @param path
* if path is specified, the diffstat is generated only for the
* specified file or folder. if unspecified, the diffstat is
* generated for the entire diff between the two commits.
* @return patch as a string
*/
public static DiffStat getDiffStat(Repository repository, RevCommit baseCommit, RevCommit commit, String path) {
DiffStat stat = null;
try {
RawTextComparator cmp = RawTextComparator.DEFAULT;
DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository);
df.setRepository(repository);
df.setDiffComparator(cmp);
df.setDetectRenames(true);
RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
if (commit.getParentCount() > 0) {
final RevWalk rw = new RevWalk(repository);
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
baseTree = parent.getTree();
} else {
// FIXME initial commit. no parent?!
baseTree = commitTree;
}
} else {
baseTree = baseCommit.getTree();
}
List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
if (path != null && path.length() > 0) {
for (DiffEntry diffEntry : diffEntries) {
if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
df.format(diffEntry);
break;
}
}
} else {
df.format(diffEntries);
}
stat = df.getDiffStat();
df.flush();
} catch (Throwable t) {
LOGGER.error("failed to generate commit diff!", t);
}
return stat;
}
use of org.eclipse.jgit.diff.RawTextComparator in project gerrit by GerritCodeReview.
the class PatchListLoader method readPatchList.
public PatchList readPatchList(Repository repo, RevWalk rw, ObjectInserter ins) throws IOException, PatchListNotAvailableException {
ObjectReader reader = rw.getObjectReader();
checkArgument(reader.getCreatedFromInserter() == ins);
RawTextComparator cmp = comparatorFor(key.getWhitespace());
try (DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
RevCommit b = rw.parseCommit(key.getNewId());
RevObject a = aFor(key, repo, rw, ins, b);
if (a == null) {
// TODO(sop) Remove this case.
// This is an octopus merge commit which should be compared against the
// auto-merge. However since we don't support computing the auto-merge
// for octopus merge commits, we fall back to diffing against the first
// parent, even though this wasn't what was requested.
//
ComparisonType comparisonType = ComparisonType.againstParent(1);
PatchListEntry[] entries = new PatchListEntry[2];
entries[0] = newCommitMessage(cmp, reader, null, b);
entries[1] = newMergeList(cmp, reader, null, b, comparisonType);
return new PatchList(a, b, true, comparisonType, entries);
}
ComparisonType comparisonType = getComparisonType(a, b);
RevCommit aCommit = a instanceof RevCommit ? (RevCommit) a : null;
RevTree aTree = rw.parseTree(a);
RevTree bTree = b.getTree();
df.setReader(reader, repo.getConfig());
df.setDiffComparator(cmp);
df.setDetectRenames(true);
List<DiffEntry> diffEntries = df.scan(aTree, bTree);
Set<String> paths = null;
if (key.getOldId() != null && b.getParentCount() == 1) {
PatchListKey newKey = PatchListKey.againstDefaultBase(key.getNewId(), key.getWhitespace());
PatchListKey oldKey = PatchListKey.againstDefaultBase(key.getOldId(), key.getWhitespace());
paths = Stream.concat(patchListCache.get(newKey, project).getPatches().stream(), patchListCache.get(oldKey, project).getPatches().stream()).map(PatchListEntry::getNewName).collect(toSet());
}
int cnt = diffEntries.size();
List<PatchListEntry> entries = new ArrayList<>();
entries.add(newCommitMessage(cmp, reader, comparisonType.isAgainstParentOrAutoMerge() ? null : aCommit, b));
boolean isMerge = b.getParentCount() > 1;
if (isMerge) {
entries.add(newMergeList(cmp, reader, comparisonType.isAgainstParentOrAutoMerge() ? null : aCommit, b, comparisonType));
}
for (int i = 0; i < cnt; i++) {
DiffEntry e = diffEntries.get(i);
if (paths == null || paths.contains(e.getNewPath()) || paths.contains(e.getOldPath())) {
FileHeader fh = toFileHeader(key, df, e);
long oldSize = getFileSize(reader, e.getOldMode(), e.getOldPath(), aTree);
long newSize = getFileSize(reader, e.getNewMode(), e.getNewPath(), bTree);
entries.add(newEntry(aTree, fh, newSize, newSize - oldSize));
}
}
return new PatchList(a, b, isMerge, comparisonType, entries.toArray(new PatchListEntry[entries.size()]));
}
}
Aggregations