Search in sources :

Example 1 with FileHeader

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");
}
Also used : Edit(org.eclipse.jgit.diff.Edit) RawText(org.eclipse.jgit.diff.RawText) Patch(org.eclipse.jgit.patch.Patch) FileHeader(org.eclipse.jgit.patch.FileHeader) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 2 with FileHeader

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);
}
Also used : Edit(org.eclipse.jgit.diff.Edit) RawText(org.eclipse.jgit.diff.RawText) Patch(org.eclipse.jgit.patch.Patch) FileHeader(org.eclipse.jgit.patch.FileHeader) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 3 with FileHeader

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();
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Date(java.util.Date) IOException(java.io.IOException) MessageRevFilter(org.eclipse.jgit.revwalk.filter.MessageRevFilter) RevFilter(org.eclipse.jgit.revwalk.filter.RevFilter) FileHeader(org.eclipse.jgit.patch.FileHeader) RevCommit(org.eclipse.jgit.revwalk.RevCommit) PatchIdDiffFormatter(org.eclipse.jgit.diff.PatchIdDiffFormatter) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 4 with FileHeader

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()]));
    }
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) ArrayList(java.util.ArrayList) ObjectReader(org.eclipse.jgit.lib.ObjectReader) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) FileHeader(org.eclipse.jgit.patch.FileHeader) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 5 with FileHeader

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);
}
Also used : HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) EditList(org.eclipse.jgit.diff.EditList) RawText(org.eclipse.jgit.diff.RawText) FileHeader(org.eclipse.jgit.patch.FileHeader)

Aggregations

FileHeader (org.eclipse.jgit.patch.FileHeader)5 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 RawText (org.eclipse.jgit.diff.RawText)3 ArrayList (java.util.ArrayList)2 DiffEntry (org.eclipse.jgit.diff.DiffEntry)2 Edit (org.eclipse.jgit.diff.Edit)2 Patch (org.eclipse.jgit.patch.Patch)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Date (java.util.Date)1 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)1 EditList (org.eclipse.jgit.diff.EditList)1 HistogramDiff (org.eclipse.jgit.diff.HistogramDiff)1 PatchIdDiffFormatter (org.eclipse.jgit.diff.PatchIdDiffFormatter)1 RawTextComparator (org.eclipse.jgit.diff.RawTextComparator)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 RevObject (org.eclipse.jgit.revwalk.RevObject)1 RevTree (org.eclipse.jgit.revwalk.RevTree)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1