Merge pull request #28 from Merovius/shorten

Implement ShortenOids
This commit is contained in:
Vicent Martí 2013-06-13 10:09:53 -07:00
commit 551e580a79
1 changed files with 20 additions and 0 deletions

20
git.go
View File

@ -96,6 +96,26 @@ func (oid *Oid) NCmp(oid2 *Oid, n uint) int {
return bytes.Compare(oid.bytes[:n], oid2.bytes[:n])
}
func ShortenOids(ids []*Oid, minlen int) (int, error) {
shorten := C.git_oid_shorten_new(C.size_t(minlen))
if shorten == nil {
panic("Out of memory")
}
defer C.git_oid_shorten_free(shorten)
var ret C.int
for _, id := range ids {
buf := make([]byte, 41)
C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), id.toC())
buf[40] = 0
ret = C.git_oid_shorten_add(shorten, (*C.char)(unsafe.Pointer(&buf[0])))
if ret < 0 {
return int(ret), LastError()
}
}
return int(ret), nil
}
type GitError struct {
Message string
Code int