Add support for getting short object Id
This commit is contained in:
parent
7cd5a4e731
commit
7caac1fa7b
14
object.go
14
object.go
|
@ -49,6 +49,20 @@ func (o *Object) Id() *Oid {
|
|||
return newOidFromC(C.git_object_id(o.ptr))
|
||||
}
|
||||
|
||||
func (o *Object) ShortId() (string, error) {
|
||||
resultBuf := C.git_buf{}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_object_short_id(&resultBuf, o.ptr)
|
||||
if ecode < 0 {
|
||||
return "", MakeGitError(ecode)
|
||||
}
|
||||
defer C.git_buf_free(&resultBuf)
|
||||
return C.GoString(resultBuf.ptr), nil
|
||||
}
|
||||
|
||||
func (o *Object) Type() ObjectType {
|
||||
return ObjectType(C.git_object_type(o.ptr))
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -105,6 +106,32 @@ func TestObjectOwner(t *testing.T) {
|
|||
checkOwner(t, repo, tree.Object)
|
||||
}
|
||||
|
||||
func checkShortId(t *testing.T, Id, shortId string) {
|
||||
if len(shortId) < 7 || len(shortId) >= len(Id) {
|
||||
t.Fatal("bad shortId lenght %s", len(shortId))
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(Id, shortId) {
|
||||
t.Fatalf("bad shortId, should be prefix of %s, but is %s\n", Id, shortId)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObjectShortId(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
commitId, _ := seedTestRepo(t, repo)
|
||||
|
||||
commit, err := repo.LookupCommit(commitId)
|
||||
checkFatal(t, err)
|
||||
|
||||
shortId, err := commit.ShortId()
|
||||
checkFatal(t, err)
|
||||
|
||||
checkShortId(t, commitId.String(), shortId)
|
||||
}
|
||||
|
||||
func TestObjectPeel(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
|
|
Loading…
Reference in New Issue