Search in sources :

Example 1 with RawText

use of org.eclipse.jgit.diff.RawText 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 RawText

use of org.eclipse.jgit.diff.RawText 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 RawText

use of org.eclipse.jgit.diff.RawText in project gitblit by gitblit.

the class DiffUtils method blame.

/**
	 * Returns the list of lines in the specified source file annotated with the
	 * source commit metadata.
	 *
	 * @param repository
	 * @param blobPath
	 * @param objectId
	 * @return list of annotated lines
	 */
public static List<AnnotatedLine> blame(Repository repository, String blobPath, String objectId) {
    List<AnnotatedLine> lines = new ArrayList<AnnotatedLine>();
    try {
        ObjectId object;
        if (StringUtils.isEmpty(objectId)) {
            object = JGitUtils.getDefaultBranch(repository);
        } else {
            object = repository.resolve(objectId);
        }
        BlameCommand blameCommand = new BlameCommand(repository);
        blameCommand.setFilePath(blobPath);
        blameCommand.setStartCommit(object);
        BlameResult blameResult = blameCommand.call();
        RawText rawText = blameResult.getResultContents();
        int length = rawText.size();
        for (int i = 0; i < length; i++) {
            RevCommit commit = blameResult.getSourceCommit(i);
            AnnotatedLine line = new AnnotatedLine(commit, i + 1, rawText.getString(i));
            lines.add(line);
        }
    } catch (Throwable t) {
        LOGGER.error(MessageFormat.format("failed to generate blame for {0} {1}!", blobPath, objectId), t);
    }
    return lines;
}
Also used : AnnotatedLine(com.gitblit.models.AnnotatedLine) BlameResult(org.eclipse.jgit.blame.BlameResult) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) BlameCommand(org.eclipse.jgit.api.BlameCommand) RawText(org.eclipse.jgit.diff.RawText) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with RawText

use of org.eclipse.jgit.diff.RawText 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

RawText (org.eclipse.jgit.diff.RawText)4 FileHeader (org.eclipse.jgit.patch.FileHeader)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 Edit (org.eclipse.jgit.diff.Edit)2 Patch (org.eclipse.jgit.patch.Patch)2 Test (org.junit.Test)2 AnnotatedLine (com.gitblit.models.AnnotatedLine)1 ArrayList (java.util.ArrayList)1 BlameCommand (org.eclipse.jgit.api.BlameCommand)1 BlameResult (org.eclipse.jgit.blame.BlameResult)1 EditList (org.eclipse.jgit.diff.EditList)1 HistogramDiff (org.eclipse.jgit.diff.HistogramDiff)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1