Home > Software Development > How to Read GIT Repository Using Java APIs

How to Read GIT Repository Using Java APIs

January 27th, 2013 Leave a comment Go to comments

GIT is a source code control system created by Linus and others for managing Linux kernel development. It becomes one of the most popular version control systems especially in the open source community. Most developers use command line or the plugins to IDEs like Eclipse, NetBeans. I think even Microsoft VisualStudio has add-on for connecting to GIT, but I haven’t checked it.

If you want more control or integrate GIT with your application, you can use Java APIs (or APIs in other programming languages) connecting to GIT repositories as well. One of the Java implementations is the JGit APIs which is now a project under the Eclipse foundation. The structure and format of the GIT repositories are well defined. You can use any programming languages to access the repository.

As I said many times, APIs are views to the products which are models (think MVC). If you are not familiar with GIT yet, you may want to read this introduction by Charles Duan. I found it’s very well written and probably the best introduction on GIT you can find.

Once that is clear, let’s move on to a sample which read the content of a file in the HEAD revision. The JGIT API I used is org.eclipse.jgit-2.1.0.201209190230-r.jar. I assume this sample should be easy to read through. If not, feel free to give it a try and leave a comment.

/** GIT sample code with BSD license. Copyright by Steve Jin */

import java.io.ByteArrayOutputStream;
import java.io.File;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;


public class JGitPrintContent
{
  public static void main(String[] args) throws Exception
  {
    File gitWorkDir = new File("C:/temp/gittest/");
    Git git = Git.open(gitWorkDir);
    Repository repo = git.getRepository();

    ObjectId lastCommitId = repo.resolve(Constants.HEAD);

    RevWalk revWalk = new RevWalk(repo);
    RevCommit commit = revWalk.parseCommit(lastCommitId);

    RevTree tree = commit.getTree();

    TreeWalk treeWalk = new TreeWalk(repo);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    treeWalk.setFilter(PathFilter.create("file1.txt"));
    if (!treeWalk.next()) 
    {
      System.out.println("Nothing found!");
      return;
    }

    ObjectId objectId = treeWalk.getObjectId(0);
    ObjectLoader loader = repo.open(objectId);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    loader.copyTo(out);
    System.out.println("file1.txt:\n" + out.toString());
    }
}
  1. January 27th, 2013 at 23:48 | #1

    How to Read GIT Repository Using Java APIs (DoubleCloud) http://t.co/1ycfbVxU

  2. January 27th, 2013 at 23:55 | #2

    How to Read GIT Repository Using Java APIs (DoubleCloud) http://t.co/3VEYyXwl

  3. January 28th, 2013 at 12:05 | #3

    How to Read #GIT #Repository Using #Java #APIs http://t.co/kGsVMbup via @sjin2008

  4. January 28th, 2013 at 13:27 | #4

    How to Read GIT Repository Using Java APIs http://t.co/Ac4giWev

  5. January 31st, 2013 at 11:08 | #5

    How to Read GIT Repository Using Java APIs http://t.co/as2Dx30H #IT CIO

  1. January 28th, 2013 at 02:51 | #1
  2. February 3rd, 2013 at 23:18 | #2