我试图使用pure go解压缩git pack file。我能找到的唯一的lib是github.com/jbrukh/ggit/api -> parse,但我很难弄清楚它应该如何工作。 基本上我只需要提供包文件的主体并获取git对象(我假设使用zlib压缩)。我尝试了几种方法,包括分叉和尝试使用私人功能但没有成功(即恐慌)。
package main
import(
log "github.com/golang/glog"
"io/ioutil"
"bytes"
"bufio"
"flag"
"os"
"github.com/jbrukh/ggit/api/parse"
)
func main(){
flag.Parse()
defer log.Flush()
packPath := "/Users/user/go/src/gopath/test_pack_no_header"
b, err := ioutil.ReadFile(packPath)
if err !=nil{
panic(err)
}
sha := "9eabf5b536662000f79978c4d1b6e4eff5c8d785"
reader := bufio.NewReader(bytes.NewBuffer(b))
openner := func()(*os.File, error){
return os.Open(packPath)
}
po := parse.NewPackIdxParser(reader, openner, sha)
if err !=nil{
log.Error(err)
return
}
pack := po.ParsePack()
log.Error("oo is %s", pack)
}
恐慌:
go run main.go -logtostderr
panic: expected string: �tOc
goroutine 1 [running]:
github.com/jbrukh/ggit/util.PanicErrf(0x1d4b30, 0x13, 0xc8200537d8, 0x1, 0x1)
/Users/user/go/src/github.com/jbrukh/ggit/util/data_parser.go:72 +0x63
github.com/jbrukh/ggit/util.(*DataParser).ConsumeString(0xc820086380, 0x1b3910, 0x4)
/Users/user/go/src/github.com/jbrukh/ggit/util/data_parser.go:226 +0x1a5
github.com/jbrukh/ggit/api/parse.(*packIdxParser).parseIdx(0xc820053ed0, 0xc820053ba0)
/Users/user/go/src/github.com/jbrukh/ggit/api/parse/pack.go:320 +0x66
github.com/jbrukh/ggit/api/parse.(*packIdxParser).ParsePack(0xc820053ed0, 0xc820086380)
/Users/user/go/src/github.com/jbrukh/ggit/api/parse/pack.go:400 +0x4e
main.main()
/Users/user/go/src/gopath/tools/main.go:33 +0x450
exit status 2
exit status 1
2016-02-12 16:28:16.322504062 +0200 EET
注意:我只需要解压缩一个包文件,就像上面尝试的例子中我没有idx文件一样。
答案 0 :(得分:0)
为了它的价值我最终采用了蛮力的方法。不是一个强大的解决方案,但它适用于我需要的东西(解码已知对象类型的包文件)。
package main
import(
"compress/zlib"
log "github.com/golang/glog"
"io/ioutil"
"bytes"
"flag"
"io"
)
func main(){
flag.Parse()
defer log.Flush()
packPath := "/Users/usr/test_pack_no_header"
b, err := ioutil.ReadFile(packPath)
if err !=nil{
panic(err)
}
buf := bytes.NewBuffer(b)
i := 0
for {
decoded, err := readObjectRaw(bytes.NewBuffer(buf.Bytes()))
if err ==nil{
log.Errorf("i %v, decoded is %s", i, decoded)
//return
}
if _, err = buf.ReadByte(); err == io.EOF{
log.Errorf("EOF")
return
}
i++
}
}
func readObjectRaw(reader io.Reader)([]byte, error){
r, err := zlib.NewReader(reader)
if err !=nil{
//log.Error(err)
return nil, err
}
defer r.Close()
def := bytes.NewBuffer(nil)
_, err = io.Copy(def, r)
if err !=nil{
//log.Errorf("%v bytes copied, err %v", i, err)
return nil, err
}
return def.Bytes(), nil
}