Having fun with Git

1 2 3 Page 3
Page 3 of 3
<span style="color: #003399;">String</span> blobHash <span style="color: #339933;">=</span> entries.<span style="color: #006633;">get</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"README"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #003399;">File</span> blobFile <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">(</span>repository, <span style="color: #0000ff;">"objects/"</span> <span style="color: #339933;">+</span> blobHash.<span style="color: #006633;">substring</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">"/"</span> <span style="color: #339933;">+</span> blobHash.<span style="color: #006633;">substring</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">(</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">InputStream</span> inputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InflaterInputStream</span><span style="color: #009900;">(</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">(</span>blobFile<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    <span style="color: #003399;">String</span> type <span style="color: #339933;">=</span> <span style="color: #003399;">Util</span>.<span style="color: #006633;">stringUntil</span><span style="color: #009900;">(</span>inputStream, <span style="color: #0000ff;">' '</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">long</span> length <span style="color: #339933;">=</span> <span style="color: #003399;">Long</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">(</span><span style="color: #003399;">Util</span>.<span style="color: #006633;">stringUntil</span><span style="color: #009900;">(</span>inputStream, <span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">)</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
 
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">(</span><span style="color: #003399;">Util</span>.<span style="color: #006633;">asString</span><span style="color: #009900;">(</span>inputStream<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

This prints the contents of our file. Obviously, if you want to find a file a subdirectory, you’ll have to do a bit more work: Parse another tree object and look and an entry in that object, etc.

Conclusions

This blog post shows how in less than 50 lines of code, with no dependencies (but a small utility helper class), we can find the head commit of a git repository, parse the file listing of the root of the file tree for that commit and print out the contents of a file. The most difficult part was to discover that it was the InflaterInputStream and not Zip or Gzip that was needed to unpack a git object.

My silly-jgit project supports reading and writing commits, trees and hashes from .git/objects. This is just the core subset of the Git plumbing commands. Furthermore, just as I wrote the article, I noticed that git often packs objects into .git/objects/pack. This adds a totally new dimension that I haven’t dealt with before.

I hope that nobody is crazy enough to actually use my silly Git library for Java. But I do hope that this article gave you some feeling of Git mastery.

This story, "Having fun with Git" was originally published by JavaWorld.

Related:

Copyright © 2013 IDG Communications, Inc.

1 2 3 Page 3
Page 3 of 3