How to Read GIT Repository Using Java APIs

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.

Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.

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());
    }
}

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

12 Comments

  1. Posted January 27, 2013 at 11:48 pm | Permalink

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

  2. Posted January 27, 2013 at 11:55 pm | Permalink

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

  3. Posted January 28, 2013 at 12:05 pm | Permalink

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

  4. Posted January 28, 2013 at 1:27 pm | Permalink

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

  5. Posted January 31, 2013 at 11:08 am | Permalink

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

  6. samuel
    Posted May 23, 2014 at 9:28 am | Permalink

    how to connect to remote GIT repository by SSH? how to do the authorization?

  7. Posted May 23, 2014 at 10:15 am | Permalink

    Samuel,

    Very good question. I haven’t tried that. Should you find out, please feel free to post the answer here. I am interested to know as well.

    Steve

  8. Navaraj
    Posted July 10, 2014 at 3:40 pm | Permalink

    Hi Steve,

    Thank you for the nice code. I tried and it works well. Can you please elaborate how commit(done by others) is read from HEAD file? For example: when I pull from a shared repo and run commit.getFullMessage() I get the last commit message from other users too, but when I check HEAD file I cannot see the commit message from other users instead I just see the pulled message.
    Also I did not find any documentation for the recommended API, is there any better documented API available now?

    Thanks in advance.

  9. Posted July 13, 2014 at 12:00 am | Permalink

    Hi Navaraj,

    Glad it worked for you. I haven’t touched the API for a while and kind forget what happened there. You may want to check out Eclipse or Netbeans plugin as they use the API extensively.

    Steve

  10. Haridwar Jha
    Posted March 30, 2015 at 8:04 am | Permalink

    Hi,
    Supoose that i have three eclipse project in my repository, through them i have made some changed in one project then while committing , it’s scanning all repository. i want to scan only selected project in repository. how can i do this…????

  11. Hiba
    Posted November 30, 2015 at 2:00 am | Permalink

    I got the following exception:
    Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.eclipse.jgit.util.FS.(FS.java:113)
    at org.eclipse.jgit.api.Git.open(Git.java:99)
    at test2.JGitPrintContent.main(JGitPrintContent.java:32)
    Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    … 3 more

  12. Posted December 4, 2015 at 12:33 pm | Permalink

    It seems you want to include the slf4j package into your classpath. -Steve

2 Trackbacks

  • […] to IDEs like Eclipse, NetBeans. I think even Microsoft VisualStudio has add-on for connecting […]How to Read GIT Repository Using Java APIs originally appeared on DoubleCloud by Steve Jin, author of VMware VI and vSphere SDK (Prentice […]

  • […] week I introduced the JGIT Java API with a simple sample illustrating how to read content from HEAD. If you have multiple versions of a source code or text […]

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.