How to Use GIT Java APIs to Diff Two Different Files

In my previous post, I introduced a sample showing how to use JGIT Java APIs to diff two versions of a same file in repository. The JGIT Java APIs can do more than that – it can actually diff two text files that are not even controlled by GIT.
Here is a short sample code on how to do this. Again, it’s not the main use case for the JGIT APIs. After all, it’s mainly for GIT source control system itself. You can always use other diff APIs for this purpose. However, if you already have JGIT included, this can be handy.

/**Copyright Steve Jin 2013 */
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.EditList;
import org.eclipse.jgit.diff.HistogramDiff;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.diff.RawTextComparator;

public class RawDiff
{
  public static void main(String[] args) throws Exception
  {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    
    try 
    {
      RawText rt1 = new RawText(new File("C:\\temp\\twofiles\\file1.txt"));
      RawText rt2 = new RawText(new File("C:\\temp\\twofiles\\file2.txt"));
      EditList diffList = new EditList();
      diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt1, rt2));
      new DiffFormatter(out).format(diffList, rt1, rt2);
    } catch (IOException e)
    {
      e.printStackTrace();
    }
    System.out.println(out.toString());
  }
}

The output of the program is like this:

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

@@ -1,3 +1,4 @@
 This is doublecloud.org
 it just rocks!
+Yes
 what an interesting work
\ No newline at end of file

This entry was posted in Software Development and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. lilo
    Posted July 11, 2013 at 10:11 am | Permalink

    Thanks a lot guy! 😀

    Really needed this.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.