From e825d66fba2cb4169c7f3b0a43c491cf9e8c0738 Mon Sep 17 00:00:00 2001 From: Jason Toffaletti Date: Sat, 4 Jan 2014 00:40:21 +0000 Subject: work in progress wrapping git_clone --- git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git.go') diff --git a/git.go b/git.go index 28196c8..07892e4 100644 --- a/git.go +++ b/git.go @@ -1,7 +1,7 @@ package git /* -#cgo pkg-config: libgit2 +#cgo pkg-config: --static libgit2 #include #include */ -- cgit v1.2.3 From f66502aaf44862a8671285e80327d808afee155f Mon Sep 17 00:00:00 2001 From: Aidan Nulman Date: Wed, 29 Jan 2014 18:01:26 -0500 Subject: update git2go to support latest libgit2 development commit (id: 66af84) --- git.go | 20 ++++++++++---------- reference.go | 22 ++++++++++++++++++---- reference_test.go | 17 +++++++++++------ repository.go | 29 ++++++++++++++++++++++------- submodule.go | 12 ++++++++++-- 5 files changed, 71 insertions(+), 29 deletions(-) (limited to 'git.go') diff --git a/git.go b/git.go index 6f81293..c663d2e 100644 --- a/git.go +++ b/git.go @@ -9,8 +9,8 @@ import "C" import ( "bytes" "errors" - "unsafe" "strings" + "unsafe" ) const ( @@ -89,7 +89,7 @@ func (oid *Oid) Equal(oid2 *Oid) bool { } func (oid *Oid) IsZero() bool { - for _, a := range(oid.bytes) { + for _, a := range oid.bytes { if a != '0' { return false } @@ -123,10 +123,10 @@ func ShortenOids(ids []*Oid, minlen int) (int, error) { type GitError struct { Message string - Code int + Code int } -func (e GitError) Error() string{ +func (e GitError) Error() string { return e.Message } @@ -139,14 +139,14 @@ func LastError() error { } func cbool(b bool) C.int { - if (b) { + if b { return C.int(1) } return C.int(0) } func ucbool(b bool) C.uint { - if (b) { + if b { return C.uint(1) } return C.uint(0) @@ -159,13 +159,13 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro cstart := C.CString(start) defer C.free(unsafe.Pointer(cstart)) - retpath := (*C.char)(C.malloc(C.GIT_PATH_MAX)) - defer C.free(unsafe.Pointer(retpath)) + retpath := (*C.git_buf)(C.malloc(C.GIT_PATH_MAX)) + defer C.git_buf_free(retpath) - r := C.git_repository_discover(retpath, C.GIT_PATH_MAX, cstart, cbool(across_fs), ceildirs) + r := C.git_repository_discover(retpath, cstart, cbool(across_fs), ceildirs) if r == 0 { - return C.GoString(retpath), nil + return C.GoString(retpath.ptr), nil } return "", LastError() diff --git a/reference.go b/reference.go index 525e092..8060146 100644 --- a/reference.go +++ b/reference.go @@ -11,6 +11,7 @@ import ( ) type ReferenceType int + const ( ReferenceSymbolic ReferenceType = C.GIT_REF_SYMBOLIC ReferenceOid = C.GIT_REF_OID @@ -27,12 +28,19 @@ func newReferenceFromC(ptr *C.git_reference) *Reference { return ref } -func (v *Reference) SetSymbolicTarget(target string) (*Reference, error) { +func (v *Reference) SetSymbolicTarget(target string, sig *Signature, msg string) (*Reference, error) { var ptr *C.git_reference + ctarget := C.CString(target) defer C.free(unsafe.Pointer(ctarget)) - ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget) + csig := sig.toC() + defer C.free(unsafe.Pointer(csig)) + + cmsg := C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + + ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget, csig, cmsg) if ret < 0 { return nil, LastError() } @@ -40,10 +48,16 @@ func (v *Reference) SetSymbolicTarget(target string) (*Reference, error) { return newReferenceFromC(ptr), nil } -func (v *Reference) SetTarget(target *Oid) (*Reference, error) { +func (v *Reference) SetTarget(target *Oid, sig *Signature, msg string) (*Reference, error) { var ptr *C.git_reference - ret := C.git_reference_set_target(&ptr, v.ptr, target.toC()) + csig := sig.toC() + defer C.free(unsafe.Pointer(csig)) + + cmsg := C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + + ret := C.git_reference_set_target(&ptr, v.ptr, target.toC(), csig, cmsg) if ret < 0 { return nil, LastError() } diff --git a/reference_test.go b/reference_test.go index f955a2c..ab62522 100644 --- a/reference_test.go +++ b/reference_test.go @@ -14,7 +14,14 @@ func TestRefModification(t *testing.T) { commitId, treeId := seedTestRepo(t, repo) - _, err := repo.CreateReference("refs/tags/tree", treeId, true) + loc, err := time.LoadLocation("Europe/Berlin") + checkFatal(t, err) + sig := &Signature{ + Name: "Rand Om Hacker", + Email: "random@hacker.com", + When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc), + } + _, err = repo.CreateReference("refs/tags/tree", treeId, true, sig, "testTreeTag") checkFatal(t, err) tag, err := repo.LookupReference("refs/tags/tree") @@ -78,13 +85,13 @@ func TestIterator(t *testing.T) { commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree) checkFatal(t, err) - _, err = repo.CreateReference("refs/heads/one", commitId, true) + _, err = repo.CreateReference("refs/heads/one", commitId, true, sig, "headOne") checkFatal(t, err) - _, err = repo.CreateReference("refs/heads/two", commitId, true) + _, err = repo.CreateReference("refs/heads/two", commitId, true, sig, "headTwo") checkFatal(t, err) - _, err = repo.CreateReference("refs/heads/three", commitId, true) + _, err = repo.CreateReference("refs/heads/three", commitId, true, sig, "headThree") checkFatal(t, err) iter, err := repo.NewReferenceIterator() @@ -108,7 +115,6 @@ func TestIterator(t *testing.T) { t.Fatal("Iteration not over") } - sort.Strings(list) compareStringList(t, expected, list) @@ -129,7 +135,6 @@ func TestIterator(t *testing.T) { t.Fatalf("Wrong number of references returned %v", count) } - // test the channel iteration list = []string{} iter, err = repo.NewReferenceIterator() diff --git a/repository.go b/repository.go index 34df0aa..31a2d05 100644 --- a/repository.go +++ b/repository.go @@ -6,8 +6,8 @@ package git */ import "C" import ( - "unsafe" "runtime" + "unsafe" ) // Repository @@ -125,12 +125,19 @@ func (v *Repository) LookupReference(name string) (*Reference, error) { return newReferenceFromC(ptr), nil } -func (v *Repository) CreateReference(name string, oid *Oid, force bool) (*Reference, error) { +func (v *Repository) CreateReference(name string, oid *Oid, force bool, sig *Signature, msg string) (*Reference, error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + + csig := sig.toC() + defer C.free(unsafe.Pointer(csig)) + + cmsg := C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + var ptr *C.git_reference - ecode := C.git_reference_create(&ptr, v.ptr, cname, oid.toC(), cbool(force)) + ecode := C.git_reference_create(&ptr, v.ptr, cname, oid.toC(), cbool(force), csig, cmsg) if ecode < 0 { return nil, LastError() } @@ -138,14 +145,22 @@ func (v *Repository) CreateReference(name string, oid *Oid, force bool) (*Refere return newReferenceFromC(ptr), nil } -func (v *Repository) CreateSymbolicReference(name, target string, force bool) (*Reference, error) { +func (v *Repository) CreateSymbolicReference(name, target string, force bool, sig *Signature, msg string) (*Reference, error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) + ctarget := C.CString(target) defer C.free(unsafe.Pointer(ctarget)) + + csig := sig.toC() + defer C.free(unsafe.Pointer(csig)) + + cmsg := C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + var ptr *C.git_reference - ecode := C.git_reference_symbolic_create(&ptr, v.ptr, cname, ctarget, cbool(force)) + ecode := C.git_reference_symbolic_create(&ptr, v.ptr, cname, ctarget, cbool(force), csig, cmsg) if ecode < 0 { return nil, LastError() } @@ -180,7 +195,7 @@ func (v *Repository) CreateCommit( var cparents []*C.git_commit = nil var parentsarg **C.git_commit = nil - nparents:= len(parents) + nparents := len(parents) if nparents > 0 { cparents = make([]*C.git_commit, nparents) for i, v := range parents { @@ -226,7 +241,7 @@ func (repo *Repository) Path() string { return C.GoString(C.git_repository_path(repo.ptr)) } -func (repo *Repository) IsBare() (bool) { +func (repo *Repository) IsBare() bool { return C.git_repository_is_bare(repo.ptr) != 0 } diff --git a/submodule.go b/submodule.go index 9819b87..903d784 100644 --- a/submodule.go +++ b/submodule.go @@ -55,6 +55,14 @@ const ( SubmoduleStatusWdUntracked = C.GIT_SUBMODULE_STATUS_WD_UNTRACKED ) +type SubmoduleRecurse int + +const ( + SubmoduleRecurseNo SubmoduleRecurse = C.GIT_SUBMODULE_RECURSE_NO + SubmoduleRecurseYes = C.GIT_SUBMODULE_RECURSE_YES + SubmoduleRecurseOndemand = C.GIT_SUBMODULE_RECURSE_ONDEMAND +) + func SubmoduleStatusIsUnmodified(status int) bool { o := SubmoduleStatus(status) & ^(SubmoduleStatusInHead | SubmoduleStatusInIndex | SubmoduleStatusInConfig | SubmoduleStatusInWd) @@ -212,8 +220,8 @@ func (sub *Submodule) FetchRecurseSubmodules() bool { return true } -func (sub *Submodule) SetFetchRecurseSubmodules(v bool) error { - ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, cbool(v)) +func (sub *Submodule) SetFetchRecurseSubmodules(recurse SubmoduleRecurse) error { + ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, C.git_submodule_recurse_t(recurse)) if ret < 0 { return LastError() } -- cgit v1.2.3 From 499f52a3549503604f30663211361e2fbd3cf202 Mon Sep 17 00:00:00 2001 From: Jesper Hansen Date: Sun, 7 Jul 2013 16:43:44 +0200 Subject: Added git error code to the error object. --- checkout.go | 4 ++-- commit.go | 2 +- config.go | 8 ++++---- git.go | 27 ++++++++++++++++++--------- index.go | 4 ++-- odb.go | 4 ++-- packbuilder.go | 10 +++++----- reference.go | 16 ++++++++-------- repository.go | 31 ++++++++++++++++--------------- submodule.go | 26 +++++++++++++------------- tree.go | 6 +++--- walk.go | 4 ++-- 12 files changed, 76 insertions(+), 66 deletions(-) (limited to 'git.go') diff --git a/checkout.go b/checkout.go index f4c1d4e..d3cd47b 100644 --- a/checkout.go +++ b/checkout.go @@ -65,7 +65,7 @@ func (v *Repository) Checkout(opts *CheckoutOpts) error { ret := C.git_checkout_head(v.ptr, &copts) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -81,7 +81,7 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error { ret := C.git_checkout_index(v.ptr, index.ptr, &copts) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil diff --git a/commit.go b/commit.go index 0c64c76..0edebb6 100644 --- a/commit.go +++ b/commit.go @@ -31,7 +31,7 @@ func (c Commit) Tree() (*Tree, error) { err := C.git_commit_tree(&ptr, c.ptr) if err < 0 { - return nil, LastError() + return nil, MakeGitError(err) } return allocObject(ptr).(*Tree), nil diff --git a/config.go b/config.go index 7665b20..2234a43 100644 --- a/config.go +++ b/config.go @@ -94,7 +94,7 @@ func (c *Config) LookupInt32(name string) (int32, error) { ret := C.git_config_get_int32(&out, c.ptr, cname) if ret < 0 { - return 0, LastError() + return 0, MakeGitError(ret) } return int32(out), nil @@ -110,7 +110,7 @@ func (c *Config) LookupInt64(name string) (int64, error) { ret := C.git_config_get_int64(&out, c.ptr, cname) if ret < 0 { - return 0, LastError() + return 0, MakeGitError(ret) } return int64(out), nil @@ -125,7 +125,7 @@ func (c *Config) LookupString(name string) (string, error) { defer runtime.UnlockOSThread() if ret := C.git_config_get_string(&ptr, c.ptr, cname); ret < 0 { - return "", LastError() + return "", MakeGitError(ret) } return C.GoString(ptr), nil @@ -220,7 +220,7 @@ func (c *Config) SetString(name, value string) (err error) { ret := C.git_config_set_string(c.ptr, cname, cvalue) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil diff --git a/git.go b/git.go index 72ea33e..7c76c30 100644 --- a/git.go +++ b/git.go @@ -61,8 +61,8 @@ func NewOidFromString(s string) (*Oid, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - if C.git_oid_fromstr(o.toC(), cs) < 0 { - return nil, LastError() + if ret := C.git_oid_fromstr(o.toC(), cs); ret < 0 { + return nil, MakeGitError(ret) } return o, nil @@ -123,27 +123,36 @@ func ShortenOids(ids []*Oid, minlen int) (int, error) { 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), MakeGitError(ret) } } return int(ret), nil } type GitError struct { - Message string - Code int + Message string + Class int + ErrorCode int } func (e GitError) Error() string { return e.Message } -func LastError() error { +func IsNotExist(err error) bool { + return err.(*GitError).ErrorCode == C.GIT_ENOTFOUND +} + +func IsExist(err error) bool { + return err.(*GitError).ErrorCode == C.GIT_EEXISTS +} + +func MakeGitError(errorCode C.int) error { err := C.giterr_last() if err == nil { - return &GitError{"No message", 0} + return &GitError{"No message", C.GITERR_INVALID, C.GIT_ERROR} } - return &GitError{C.GoString(err.message), int(err.klass)} + return &GitError{C.GoString(err.message), int(err.klass), int(errorCode)} } func cbool(b bool) C.int { @@ -175,7 +184,7 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro ret := C.git_repository_discover(&buf, cstart, cbool(across_fs), ceildirs) if ret < 0 { - return "", LastError() + return "", MakeGitError(ret) } return C.GoString(buf.ptr), nil diff --git a/index.go b/index.go index bc11025..cbb28c4 100644 --- a/index.go +++ b/index.go @@ -42,7 +42,7 @@ func (v *Index) AddByPath(path string) error { ret := C.git_index_add_bypath(v.ptr, cstr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -56,7 +56,7 @@ func (v *Index) WriteTree() (*Oid, error) { ret := C.git_index_write_tree(oid.toC(), v.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return oid, nil diff --git a/odb.go b/odb.go index 953791c..3daf3a4 100644 --- a/odb.go +++ b/odb.go @@ -32,7 +32,7 @@ func (v *Odb) Write(data []byte, otype ObjectType) (oid *Oid, err error) { ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(hdr.Data), C.size_t(hdr.Len), C.git_otype(otype)) if ret < 0 { - err = LastError() + err = MakeGitError(ret) } return @@ -46,7 +46,7 @@ func (v *Odb) Read(oid *Oid) (obj *OdbObject, err error) { ret := C.git_odb_read(&obj.ptr, v.ptr, oid.toC()) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(obj, (*OdbObject).Free) diff --git a/packbuilder.go b/packbuilder.go index 333f183..70c4530 100644 --- a/packbuilder.go +++ b/packbuilder.go @@ -28,7 +28,7 @@ func (repo *Repository) NewPackbuilder() (*Packbuilder, error) { ret := C.git_packbuilder_new(&builder.ptr, repo.ptr) if ret != 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(builder, (*Packbuilder).Free) return builder, nil @@ -48,7 +48,7 @@ func (pb *Packbuilder) Insert(id *Oid, name string) error { ret := C.git_packbuilder_insert(pb.ptr, id.toC(), cname) if ret != 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -59,7 +59,7 @@ func (pb *Packbuilder) InsertCommit(id *Oid) error { ret := C.git_packbuilder_insert_commit(pb.ptr, id.toC()) if ret != 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -70,7 +70,7 @@ func (pb *Packbuilder) InsertTree(id *Oid) error { ret := C.git_packbuilder_insert_tree(pb.ptr, id.toC()) if ret != 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -88,7 +88,7 @@ func (pb *Packbuilder) WriteToFile(name string, mode os.FileMode) error { ret := C.git_packbuilder_write(pb.ptr, cname, C.uint(mode.Perm()), nil, nil) if ret != 0 { - return LastError() + return MakeGitError(ret) } return nil } diff --git a/reference.go b/reference.go index a2f1636..39d328e 100644 --- a/reference.go +++ b/reference.go @@ -45,7 +45,7 @@ func (v *Reference) SetSymbolicTarget(target string, sig *Signature, msg string) ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget, csig, cmsg) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newReferenceFromC(ptr), nil @@ -65,7 +65,7 @@ func (v *Reference) SetTarget(target *Oid, sig *Signature, msg string) (*Referen ret := C.git_reference_set_target(&ptr, v.ptr, target.toC(), csig, cmsg) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newReferenceFromC(ptr), nil @@ -79,7 +79,7 @@ func (v *Reference) Resolve() (*Reference, error) { ret := C.git_reference_resolve(&ptr, v.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newReferenceFromC(ptr), nil @@ -102,7 +102,7 @@ func (v *Reference) Rename(name string, force bool, sig *Signature, msg string) ret := C.git_reference_rename(&ptr, v.ptr, cname, cbool(force), csig, cmsg) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newReferenceFromC(ptr), nil @@ -128,7 +128,7 @@ func (v *Reference) Delete() error { ret := C.git_reference_delete(v.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil @@ -173,7 +173,7 @@ func (repo *Repository) NewReferenceIterator() (*ReferenceIterator, error) { ret := C.git_reference_iterator_new(&ptr, repo.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } iter := &ReferenceIterator{repo: repo, ptr: ptr} @@ -194,7 +194,7 @@ func (repo *Repository) NewReferenceIteratorGlob(glob string) (*ReferenceIterato ret := C.git_reference_iterator_glob_new(&ptr, repo.ptr, cstr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } iter := &ReferenceIterator{repo: repo, ptr: ptr} @@ -215,7 +215,7 @@ func (v *ReferenceIterator) NextName() (string, error) { return "", ErrIterOver } if ret < 0 { - return "", LastError() + return "", MakeGitError(ret) } return C.GoString(ptr), nil diff --git a/repository.go b/repository.go index 1d58d7e..e78422e 100644 --- a/repository.go +++ b/repository.go @@ -26,7 +26,7 @@ func OpenRepository(path string) (*Repository, error) { ret := C.git_repository_open(&repo.ptr, cpath) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(repo, (*Repository).Free) @@ -44,7 +44,7 @@ func InitRepository(path string, isbare bool) (*Repository, error) { ret := C.git_repository_init(&repo.ptr, cpath, ucbool(isbare)) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(repo, (*Repository).Free) @@ -64,7 +64,7 @@ func (v *Repository) Config() (*Config, error) { ret := C.git_repository_config(&config.ptr, v.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(config, (*Config).Free) @@ -79,7 +79,7 @@ func (v *Repository) Index() (*Index, error) { ret := C.git_repository_index(&ptr, v.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return newIndexFromC(ptr), nil @@ -93,7 +93,7 @@ func (v *Repository) lookupType(id *Oid, t ObjectType) (Object, error) { ret := C.git_object_lookup(&ptr, v.ptr, id.toC(), C.git_otype(t)) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return allocObject(ptr), nil @@ -140,7 +140,7 @@ func (v *Repository) LookupReference(name string) (*Reference, error) { ecode := C.git_reference_lookup(&ptr, v.ptr, cname) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } return newReferenceFromC(ptr), nil @@ -163,7 +163,7 @@ func (v *Repository) CreateReference(name string, id *Oid, force bool, sig *Sign ecode := C.git_reference_create(&ptr, v.ptr, cname, id.toC(), cbool(force), csig, cmsg) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } return newReferenceFromC(ptr), nil @@ -189,7 +189,7 @@ func (v *Repository) CreateSymbolicReference(name, target string, force bool, si ecode := C.git_reference_symbolic_create(&ptr, v.ptr, cname, ctarget, cbool(force), csig, cmsg) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } return newReferenceFromC(ptr), nil @@ -203,7 +203,7 @@ func (v *Repository) Walk() (*RevWalk, error) { ecode := C.git_revwalk_new(&walk.ptr, v.ptr) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } walk.repo = v @@ -250,7 +250,7 @@ func (v *Repository) CreateCommit( nil, cmsg, tree.ptr, C.size_t(nparents), parentsarg) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return oid, nil @@ -268,7 +268,7 @@ func (v *Repository) Odb() (odb *Odb, err error) { defer runtime.UnlockOSThread() if ret := C.git_repository_odb(&odb.ptr, v.ptr); ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(odb, (*Odb).Free) @@ -294,9 +294,10 @@ func (repo *Repository) SetWorkdir(workdir string, updateGitlink bool) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - if C.git_repository_set_workdir(repo.ptr, cstr, cbool(updateGitlink)) < 0 { - return LastError() + if ret := C.git_repository_set_workdir(repo.ptr, cstr, cbool(updateGitlink)); ret < 0 { + return MakeGitError(ret) } + return nil } @@ -307,7 +308,7 @@ func (v *Repository) TreeBuilder() (*TreeBuilder, error) { defer runtime.UnlockOSThread() if ret := C.git_treebuilder_create(&bld.ptr, nil); ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } runtime.SetFinalizer(bld, (*TreeBuilder).Free) @@ -326,7 +327,7 @@ func (v *Repository) RevparseSingle(spec string) (Object, error) { ecode := C.git_revparse_single(&ptr, v.ptr, cspec) if ecode < 0 { - return nil, LastError() + return nil, MakeGitError(ecode) } return allocObject(ptr), nil diff --git a/submodule.go b/submodule.go index 9abf333..8390e36 100644 --- a/submodule.go +++ b/submodule.go @@ -81,7 +81,7 @@ func (repo *Repository) LookupSubmodule(name string) (*Submodule, error) { ret := C.git_submodule_lookup(&sub.ptr, repo.ptr, cname) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return sub, nil @@ -102,7 +102,7 @@ func (repo *Repository) ForeachSubmodule(cbk SubmoduleCbk) error { ret := C._go_git_visit_submodule(repo.ptr, unsafe.Pointer(&cbk)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -120,7 +120,7 @@ func (repo *Repository) AddSubmodule(url, path string, use_git_link bool) (*Subm ret := C.git_submodule_add_setup(&sub.ptr, repo.ptr, curl, cpath, cbool(use_git_link)) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return sub, nil } @@ -131,7 +131,7 @@ func (sub *Submodule) FinalizeAdd() error { ret := C.git_submodule_add_finalize(sub.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -142,7 +142,7 @@ func (sub *Submodule) AddToIndex(write_index bool) error { ret := C.git_submodule_add_to_index(sub.ptr, cbool(write_index)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -153,7 +153,7 @@ func (sub *Submodule) Save() error { ret := C.git_submodule_save(sub.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -188,7 +188,7 @@ func (sub *Submodule) SetUrl(url string) error { ret := C.git_submodule_set_url(sub.ptr, curl) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -247,7 +247,7 @@ func (sub *Submodule) SetFetchRecurseSubmodules(recurse SubmoduleRecurse) error ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, C.git_submodule_recurse_t(recurse)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -258,7 +258,7 @@ func (sub *Submodule) Init(overwrite bool) error { ret := C.git_submodule_init(sub.ptr, cbool(overwrite)) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -269,7 +269,7 @@ func (sub *Submodule) Sync() error { ret := C.git_submodule_sync(sub.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -282,7 +282,7 @@ func (sub *Submodule) Open() (*Repository, error) { ret := C.git_submodule_open(&repo.ptr, sub.ptr) if ret < 0 { - return nil, LastError() + return nil, MakeGitError(ret) } return repo, nil } @@ -293,7 +293,7 @@ func (sub *Submodule) Reload() error { ret := C.git_submodule_reload(sub.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } @@ -304,7 +304,7 @@ func (repo *Repository) ReloadAllSubmodules() error { ret := C.git_submodule_reload_all(repo.ptr) if ret < 0 { - return LastError() + return MakeGitError(ret) } return nil } diff --git a/tree.go b/tree.go index 8c74e5d..4efb589 100644 --- a/tree.go +++ b/tree.go @@ -109,7 +109,7 @@ func (t Tree) Walk(callback TreeWalkCallback) error { ) if err < 0 { - return LastError() + return MakeGitError(err) } return nil @@ -134,7 +134,7 @@ func (v *TreeBuilder) Insert(filename string, id *Oid, filemode int) (error) { err := C.git_treebuilder_insert(nil, v.ptr, cfilename, id.toC(), C.git_filemode_t(filemode)) if err < 0 { - return LastError() + return MakeGitError(err) } return nil @@ -149,7 +149,7 @@ func (v *TreeBuilder) Write() (*Oid, error) { err := C.git_treebuilder_write(oid.toC(), v.repo.ptr, v.ptr) if err < 0 { - return nil, LastError() + return nil, MakeGitError(err) } return oid, nil diff --git a/walk.go b/walk.go index 619188d..cdc1a20 100644 --- a/walk.go +++ b/walk.go @@ -40,7 +40,7 @@ func (v *RevWalk) PushHead() (err error) { ecode := C.git_revwalk_push_head(v.ptr) if ecode < 0 { - err = LastError() + err = MakeGitError(ecode) } return @@ -55,7 +55,7 @@ func (v *RevWalk) Next(id *Oid) (err error) { case ret == ITEROVER: err = io.EOF case ret < 0: - err = LastError() + err = MakeGitError(ret) } return -- cgit v1.2.3 From b6703d47671b3a736e8b93ff0447da45e688865c Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Tue, 18 Mar 2014 04:54:40 +0100 Subject: Oid: make the type directly [20]byte There is no need for a struct with a single field. An Oid is 20 bytes which hold the binary representation of the hash, so let's use that directly. Go lets us have methods on this new type just the same. --- git.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'git.go') diff --git a/git.go b/git.go index db9522b..8f543b5 100644 --- a/git.go +++ b/git.go @@ -29,9 +29,7 @@ func init() { } // Oid -type Oid struct { - bytes [20]byte -} +type Oid [20]byte func newOidFromC(coid *C.git_oid) *Oid { if coid == nil { @@ -39,18 +37,18 @@ func newOidFromC(coid *C.git_oid) *Oid { } oid := new(Oid) - copy(oid.bytes[0:20], C.GoBytes(unsafe.Pointer(coid), 20)) + copy(oid[0:20], C.GoBytes(unsafe.Pointer(coid), 20)) return oid } func NewOid(b []byte) *Oid { oid := new(Oid) - copy(oid.bytes[0:20], b[0:20]) + copy(oid[0:20], b[0:20]) return oid } func (oid *Oid) toC() *C.git_oid { - return (*C.git_oid)(unsafe.Pointer(&oid.bytes)) + return (*C.git_oid)(unsafe.Pointer(oid)) } func NewOidFromString(s string) (*Oid, error) { @@ -75,25 +73,25 @@ func (oid *Oid) String() string { } func (oid *Oid) Bytes() []byte { - return oid.bytes[0:] + return oid[0:] } func (oid *Oid) Cmp(oid2 *Oid) int { - return bytes.Compare(oid.bytes[:], oid2.bytes[:]) + return bytes.Compare(oid[:], oid2[:]) } func (oid *Oid) Copy() *Oid { ret := new(Oid) - copy(ret.bytes[:], oid.bytes[:]) + copy(ret[:], oid[:]) return ret } func (oid *Oid) Equal(oid2 *Oid) bool { - return bytes.Equal(oid.bytes[:], oid2.bytes[:]) + return bytes.Equal(oid[:], oid2[:]) } func (oid *Oid) IsZero() bool { - for _, a := range oid.bytes { + for _, a := range oid { if a != '0' { return false } @@ -102,7 +100,7 @@ func (oid *Oid) IsZero() bool { } func (oid *Oid) NCmp(oid2 *Oid, n uint) int { - return bytes.Compare(oid.bytes[:n], oid2.bytes[:n]) + return bytes.Compare(oid[:n], oid2[:n]) } func ShortenOids(ids []*Oid, minlen int) (int, error) { -- cgit v1.2.3 From c9c7c1e77942f88955af0dc3bdfb58d5e7d7f121 Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Tue, 18 Mar 2014 05:04:26 +0100 Subject: Oid: make NewOid take a string This is the most common way of having an id that's not in Oid form, so let's make it the "default" and rename to NewOidFromBytes() the one that takes []byte. --- git.go | 4 ++-- odb_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'git.go') diff --git a/git.go b/git.go index 8f543b5..f3fb7e1 100644 --- a/git.go +++ b/git.go @@ -41,7 +41,7 @@ func newOidFromC(coid *C.git_oid) *Oid { return oid } -func NewOid(b []byte) *Oid { +func NewOidFromBytes(b []byte) *Oid { oid := new(Oid) copy(oid[0:20], b[0:20]) return oid @@ -51,7 +51,7 @@ func (oid *Oid) toC() *C.git_oid { return (*C.git_oid)(unsafe.Pointer(oid)) } -func NewOidFromString(s string) (*Oid, error) { +func NewOid(s string) (*Oid, error) { o := new(Oid) cs := C.CString(s) defer C.free(unsafe.Pointer(cs)) diff --git a/odb_test.go b/odb_test.go index a4f8943..17b3ad2 100644 --- a/odb_test.go +++ b/odb_test.go @@ -27,7 +27,7 @@ func TestOdbStream(t *testing.T) { error = stream.Close() checkFatal(t, error) - expectedId, error := NewOidFromString("30f51a3fba5274d53522d0f19748456974647b4f") + expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f") checkFatal(t, error) if stream.Id.Cmp(expectedId) != 0 { t.Fatal("Wrong data written") @@ -59,4 +59,4 @@ Initial commit.`; if oid.Cmp(coid) != 0 { t.Fatal("Hash and write Oids are different") } -} \ No newline at end of file +} -- cgit v1.2.3 From c243c31f7d428680579a1dd20273cd3888c730e4 Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Wed, 19 Mar 2014 03:11:41 +0100 Subject: Oid: remove Bytes() This is not needed. We can do id[:] to get a slice. --- git.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'git.go') diff --git a/git.go b/git.go index f3fb7e1..9e1d3e7 100644 --- a/git.go +++ b/git.go @@ -28,7 +28,7 @@ func init() { C.git_threads_init() } -// Oid +// Oid represents the id for a Git object. type Oid [20]byte func newOidFromC(coid *C.git_oid) *Oid { @@ -72,10 +72,6 @@ func (oid *Oid) String() string { return string(buf) } -func (oid *Oid) Bytes() []byte { - return oid[0:] -} - func (oid *Oid) Cmp(oid2 *Oid) int { return bytes.Compare(oid[:], oid2[:]) } -- cgit v1.2.3 From 0bb73e43a8f26be8608cdd304d73cacb05753417 Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Wed, 19 Mar 2014 03:39:34 +0100 Subject: Oid: use Go's conversion functions Go already has all the necessary pieces for encoding and decoding hex strings. Using them let's us avoid going into C land. Benchmarks show this takes about half the time as using libgit2's functions. --- git.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'git.go') diff --git a/git.go b/git.go index 9e1d3e7..e5fe812 100644 --- a/git.go +++ b/git.go @@ -8,6 +8,7 @@ package git import "C" import ( "bytes" + "encoding/hex" "errors" "runtime" "strings" @@ -52,24 +53,23 @@ func (oid *Oid) toC() *C.git_oid { } func NewOid(s string) (*Oid, error) { - o := new(Oid) - cs := C.CString(s) - defer C.free(unsafe.Pointer(cs)) + if len(s) > C.GIT_OID_HEXSZ { + return nil, errors.New("string is too long for oid") + } - runtime.LockOSThread() - defer runtime.UnlockOSThread() + o := new(Oid) - if ret := C.git_oid_fromstr(o.toC(), cs); ret < 0 { - return nil, MakeGitError(ret) + slice, error := hex.DecodeString(s) + if error != nil { + return nil, error } + copy(o[:], slice[:20]) return o, nil } func (oid *Oid) String() string { - buf := make([]byte, 40) - C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), oid.toC()) - return string(buf) + return hex.EncodeToString(oid[:]) } func (oid *Oid) Cmp(oid2 *Oid) int { -- cgit v1.2.3 From b82a72a9ce4701a4560288c4ebf1511ffb415b80 Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Wed, 19 Mar 2014 03:51:59 +0100 Subject: Oid: fix IsZero() We need to compare against the number zero, not its ASCII value. --- git.go | 2 +- git_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'git.go') diff --git a/git.go b/git.go index e5fe812..b20f993 100644 --- a/git.go +++ b/git.go @@ -88,7 +88,7 @@ func (oid *Oid) Equal(oid2 *Oid) bool { func (oid *Oid) IsZero() bool { for _, a := range oid { - if a != '0' { + if a != 0 { return false } } diff --git a/git_test.go b/git_test.go index fff3c6c..6542ca0 100644 --- a/git_test.go +++ b/git_test.go @@ -54,3 +54,11 @@ func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) { return commitId, treeId } + +func TestOidZero(t *testing.T) { + var zeroId Oid + + if !zeroId.IsZero() { + t.Error("Zero Oid is not zero") + } +} -- cgit v1.2.3