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

How to Read GIT Repository Using Java APIs (DoubleCloud) http://t.co/1ycfbVxU
How to Read GIT Repository Using Java APIs (DoubleCloud) http://t.co/3VEYyXwl
How to Read #GIT #Repository Using #Java #APIs http://t.co/kGsVMbup via @sjin2008
How to Read GIT Repository Using Java APIs http://t.co/Ac4giWev
How to Read GIT Repository Using Java APIs http://t.co/as2Dx30H #IT CIO