use of org.eclipse.jgit.patch.FileHeader in project gitiles by GerritCodeReview.
the class DiffServletTest method diffFileOneParentText.
@Test
public void diffFileOneParentText() throws Exception {
String contents1 = "foo\n";
String contents2 = "foo\ncontents\n";
RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1));
RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2));
FakeHttpServletResponse res = buildText("/repo/+diff/" + c2.name() + "^!/foo");
Patch p = parsePatch(res.getActualBody());
FileHeader f = getOnlyElement(p.getFiles());
assertThat(f.getChangeType()).isEqualTo(ChangeType.MODIFY);
assertThat(f.getPath(Side.OLD)).isEqualTo("foo");
assertThat(f.getPath(Side.NEW)).isEqualTo("foo");
RawText rt2 = new RawText(contents2.getBytes(UTF_8));
Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList());
assertThat(e.getType()).isEqualTo(Type.INSERT);
assertThat(rt2.getString(e.getBeginB(), e.getEndB(), false)).isEqualTo("contents\n");
}
use of org.eclipse.jgit.patch.FileHeader in project gitiles by GerritCodeReview.
the class DiffServletTest method diffFileNoParentsText.
@Test
public void diffFileNoParentsText() throws Exception {
String contents = "foo\ncontents\n";
RevCommit c = repo.update("master", repo.commit().add("foo", contents));
FakeHttpServletResponse res = buildText("/repo/+diff/" + c.name() + "^!/foo");
Patch p = parsePatch(res.getActualBody());
FileHeader f = getOnlyElement(p.getFiles());
assertThat(f.getChangeType()).isEqualTo(ChangeType.ADD);
assertThat(f.getPath(Side.OLD)).isEqualTo(DiffEntry.DEV_NULL);
assertThat(f.getPath(Side.NEW)).isEqualTo("foo");
RawText rt = new RawText(contents.getBytes(UTF_8));
Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList());
assertThat(e.getType()).isEqualTo(Type.INSERT);
assertThat(rt.getString(e.getBeginB(), e.getEndB(), false)).isEqualTo(contents);
}
use of org.eclipse.jgit.patch.FileHeader in project chuidiang-ejemplos by chuidiang.
the class GitAnalyzer method analyze.
public void analyze(HtmlFormat format, String issuePattern, String issueName) throws IOException {
try (RevWalk walk = new RevWalk(repository)) {
RevFilter filter = MessageRevFilter.create(issuePattern);
walk.setRevFilter(filter);
walk.markStart(walk.parseCommit(repository.resolve("HEAD")));
Iterator<RevCommit> iterator = walk.iterator();
if (!iterator.hasNext()) {
return;
}
format.addIssue(issueName);
while (iterator.hasNext()) {
RevCommit commit = iterator.next();
String commitNumber = commit.getId().getName();
Date date = commit.getAuthorIdent().getWhen();
String author = commit.getAuthorIdent().getName();
String comment = commit.getFullMessage();
ObjectId objectId = commit.getTree().getId();
RevCommit parent = commit.getParent(0);
PatchIdDiffFormatter formatter = new PatchIdDiffFormatter();
formatter.setRepository(repository);
ArrayList<String> files = new ArrayList<>();
try {
List<DiffEntry> entries = formatter.scan(parent, objectId);
entries.forEach((entry) -> {
try {
FileHeader header = formatter.toFileHeader(entry);
files.add(header.getChangeType() + " " + getPath(header));
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (Exception e) {
e.printStackTrace();
}
format.addChange(commitNumber, date, author, comment, files.toArray(new String[] {}));
}
format.endIssue();
} catch (Exception e) {
format.endIssue();
}
}
use of org.eclipse.jgit.patch.FileHeader 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()]));
}
}
use of org.eclipse.jgit.patch.FileHeader in project gerrit by GerritCodeReview.
the class PatchListLoader method createPatchListEntry.
private static PatchListEntry createPatchListEntry(RawTextComparator cmp, RevCommit aCommit, Text aText, Text bText, String fileName) {
byte[] rawHdr = getRawHeader(aCommit != null, fileName);
byte[] aContent = aText.getContent();
byte[] bContent = bText.getContent();
long size = bContent.length;
long sizeDelta = bContent.length - aContent.length;
RawText aRawText = new RawText(aContent);
RawText bRawText = new RawText(bContent);
EditList edits = new HistogramDiff().diff(cmp, aRawText, bRawText);
FileHeader fh = new FileHeader(rawHdr, edits, PatchType.UNIFIED);
return new PatchListEntry(fh, edits, size, sizeDelta);
}
Aggregations