Skip to content

Instantly share code, notes, and snippets.

@zenon
Created February 11, 2018 20:11
Show Gist options
  • Save zenon/adea954d68570a6978f9bded5559cdc4 to your computer and use it in GitHub Desktop.
Save zenon/adea954d68570a6978f9bded5559cdc4 to your computer and use it in GitHub Desktop.
A rudimentary Java program to read (i.e. simply inflate) git object files.
package gitchen;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.zip.InflaterInputStream;
// a class to look into the private parts of your git repo
public class Read {
// path to the object store of your git repo: .git/objects/
static String path = "~/projects/test/.git/objects/";
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Please give a SHA as argument.");
return;
}
String sha = args[0];
String prefix = sha.substring(0, 2);
String rest = sha.substring(2);
File file = new File(path+prefix+"\\"+rest);
FileInputStream fin = new FileInputStream(file);
// how many bytes to read? Create the array to take them.
byte[] fileContent = new byte[(int) file.length()];
fin.read(fileContent);
ByteArrayInputStream bais = new ByteArrayInputStream(fileContent);
InflaterInputStream iis = new InflaterInputStream(bais);
String result = "";
byte[] buf = new byte[5];
int rlen = -1;
while ((rlen = iis.read(buf)) != -1) {
result += new String(Arrays.copyOf(buf, rlen));
}
System.out.println("Decompress result: \n" + result);
// for quality programming, this would belong into a finally block, of course:
fin.close();
}
}
@fayeinmay
Copy link

Neat! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment