Skip to content

Instantly share code, notes, and snippets.

@ymnk
Created October 30, 2008 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymnk/20950 to your computer and use it in GitHub Desktop.
Save ymnk/20950 to your computer and use it in GitHub Desktop.
/*
Copyright (c) 2008 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// pachage com.jcraft.gis
import java.io.{File, FileInputStream}
import java.util.zip.Inflater
// import GitObject._
object GitLooseObject {
def unpack(path:String)={
val f=new File(path);
val fbuf=new Array[byte](f.length.asInstanceOf[Int]);
new FileInputStream(f) match {
case _f => {_f.read(fbuf); _f.close}
}
if(isLegacy(fbuf)){
val buf=inflate(fbuf, 0, fbuf.length, None)
val header=buf.takeWhile(_!=0)
val typ=header.takeWhile(_!=0x20)
val size=header.slice(typ.length+1, header.length)
val content=buf.slice(header.length+1, buf.length)
"type: "+new String(typ)+"\n"+new String(content)
// new GitObject(namet(new String(typ)), content)
}
else{
var (typ, size, offset)=unpack_header(fbuf)
val content=inflate(fbuf, offset, fbuf.length-offset, Some(size))
"type: "+typ+"\n"+new String(content)
// new GitObject(typ, content)
}
}
private def inflate(deflated_buf:Array[byte],
offset:Int, len:Int,
inflated_size:Option[Int]):Array[byte]={
var inf=new Inflater
inf.reset();
inf.setInput(deflated_buf, offset, len);
var buf=new Array[byte](inflated_size match {
case Some(x) => x
case _ => 1024 // length is unknown ;-<
})
var resultlen=0;
while(inf.getRemaining>0){
resultlen += inf.inflate(buf, resultlen, buf.length-resultlen)
if(inf.getRemaining>0){
if(buf.length-resultlen<inf.getRemaining){
val tmp=new Array[byte](buf.length*2)
System.arraycopy(buf, 0, tmp, 0, buf.length)
buf=tmp
}
}
}
inf.end
if(buf.length>resultlen){
val tmp=new Array[byte](resultlen)
System.arraycopy(buf, 0, tmp, 0, resultlen)
tmp
}
else buf
}
private def unpack_header(buf:Array[byte])={
var offset = 0
var b=buf(offset); offset += 1
var size=b&0x0f
val typ=(b>>>4)&0x07
var shift=4;
while((b&0x80)!=0){
b=buf(offset); offset += 1
size += ((b&0x7f)<<shift)
shift+=7
}
(typ, size, offset)
}
/**
* In the legacy format, it must be comressed by zlib.
*/
private def isLegacy(buf:Array[byte]):Boolean={
val (b0, b1)=(buf(0), buf(1))
b0==0x78 &&
(((b0<<8)+(b1&0xff))&0xffff)%31==0
}
}
object GitLooseObjectTest{
import Iterator.fromArray
def main(arg:Array[String]){
val objects_dir=arg.length match {
case 0 => new File(new File(".git"), "objects")
case _ => new File(arg(0))
}
for(d <- fromArray(objects_dir.listFiles()) if d.getName.length==2){
for(f <- fromArray(d.listFiles())){
println(f)
println(GitLooseObject.unpack(f.getAbsolutePath))
println("----------------")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment