5

When I use JGit to diff with two files, I must create a repo first. But in Git, I only need to use command git diff --no-index 1.txt 2.txt.

Is there a way to use diff in JGit without creating repo?

doubleDown
  • 8,048
  • 1
  • 32
  • 48
eabiao
  • 99
  • 1
  • 4
  • 1
    in other words : How do I do the equivalent of “git diff --no-index file1 file2” with jgit? when using “git diff --no-index file1 file2”, it doesn't need a git repository – eabiao Oct 21 '12 at 04:40

2 Answers2

4

thank you! I have solved this question!

method is below

private static String getDiff(String file1, String file2) {
    OutputStream out = new ByteArrayOutputStream();
    try {
        RawText rt1 = new RawText(new File(file1));
        RawText rt2 = new RawText(new File(file2));
        EditList diffList = new EditList();
        diffList.addAll(differ.diff(COMP, rt1, rt2));
        new DiffFormatter(out).format(diffList, rt1, rt2);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toString();
}

thank you for your help!

eabiao
  • 99
  • 1
  • 4
0

Hm I don't understand the question exactly, but maybe you are looking for something like gitective which makes it with the help of his BlobUtils "Diff two files" to a more easy task.

Sonson123
  • 10,879
  • 12
  • 54
  • 72