From ff5150e6c9a0e9f82eb3c26df96f8839242589ca Mon Sep 17 00:00:00 2001 From: Johann Weging Date: Tue, 8 Oct 2013 02:07:06 +0200 Subject: branch: Implemented branch functions. --- branch.go | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 branch.go (limited to 'branch.go') diff --git a/branch.go b/branch.go new file mode 100644 index 0000000..8dd55a3 --- /dev/null +++ b/branch.go @@ -0,0 +1,199 @@ +package git + +/* +#cgo pkg-config: libgit2 +#include +#include +*/ +import "C" + +import ( + "errors" + "strings" + "unsafe" +) + +var ErrEUser = errors.New("Error in user callback function") + +type ListFlags uint + +type BranchT uint + +const ( + BRANCH_LOCAL BranchT = C.GIT_BRANCH_LOCAL + BRANCH_REMOTE = C.GIT_BRANCH_REMOTE +) + +const ( + REFS_DIR = "refs/" + REFS_HEADS_DIR = REFS_DIR + "heads/" + REFS_TAGS_DIR = REFS_DIR + "tags/" + REFS_REMOTES_DIR = REFS_DIR + "remotes/" +) + +type Branch struct { + Reference +} + +func (repo *Repository) BranchCreate(branchName string, target *Commit, force bool) (*Reference, error) { + ref := new(Reference) + cBranchName := C.CString(branchName) + cForce := cbool(force) + err := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce) + if err < 0 { + return nil, LastError() + } + return ref, nil +} + +func (branch *Branch) BranchDelete() error { + if err := C.git_branch_delete(branch.ptr); err < 0 { + return LastError() + } + return nil +} + +type BranchForeachCB func(name string, flags ListFlags, payload interface{}) error + +func (repo *Repository) BranchForeach(flags ListFlags, callback BranchForeachCB, payload interface{}) error { + iter, err := repo.NewReferenceIterator() + if err != nil { + return err + } + + for { + ref, err := iter.Next() + if err == ErrIterOver { + break + } + + if (flags == ListFlags(BRANCH_LOCAL)) && strings.HasPrefix(ref.Name(), REFS_HEADS_DIR) { + name := strings.TrimPrefix(ref.Name(), REFS_HEADS_DIR) + err = callback(name, ListFlags(BRANCH_LOCAL), payload) + if err != nil { + return err + } + } + + if (flags == ListFlags(BRANCH_REMOTE)) && strings.HasPrefix(ref.Name(), REFS_REMOTES_DIR) { + name := strings.TrimPrefix(ref.Name(), REFS_REMOTES_DIR) + err = callback(name, ListFlags(BRANCH_REMOTE), payload) + if err != nil { + return err + } + } + } + + if err == ErrIterOver { + err = nil + } + return err +} + +func (branch *Branch) Move(newBranchName string, force bool) (*Branch, error) { + newBranch := new(Branch) + cNewBranchName := C.CString(newBranchName) + cForce := cbool(force) + + err := C.git_branch_move(&newBranch.ptr, branch.ptr, cNewBranchName, cForce) + if err < 0 { + return nil, LastError() + } + return newBranch, nil +} + +func (branch *Branch) IsHead() (bool, error) { + isHead := C.git_branch_is_head(branch.ptr) + switch isHead { + case 1: + return true, nil + case 0: + return false, nil + default: + return false, LastError() + } + +} + +func (repo *Repository) BranchLookup(branchName string, branchType BranchT) (*Branch, error) { + branch := new(Branch) + cName := C.CString(branchName) + + err := C.git_branch_lookup(&branch.ptr, repo.ptr, cName, C.git_branch_t(branchType)) + if err < 0 { + return nil, LastError() + } + return branch, nil +} + +func (branch *Branch) Name() (string, error) { + var cName *C.char + defer C.free(unsafe.Pointer(cName)) + + err := C.git_branch_name(&cName, branch.ptr) + if err < 0 { + return "", LastError() + } + + return C.GoString(cName), nil +} + +func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) { + cName := C.CString(canonicalBranchName) + + // Obtain the length of the name + ret := C.git_branch_remote_name(nil, 0, repo.ptr, cName) + if ret < 0 { + return "", LastError() + } + + cBuf := (*C.char)(C.malloc(C.size_t(ret))) + defer C.free(unsafe.Pointer(cBuf)) + + // Actually obtain the name + ret = C.git_branch_remote_name(cBuf, C.size_t(ret), repo.ptr, cName) + if ret < 0 { + return "", LastError() + } + + return C.GoString(cBuf), nil +} + +func (branch *Branch) SetUpstream(upstreamName string) error { + cName := C.CString(upstreamName) + + err := C.git_branch_set_upstream(branch.ptr, cName) + if err < 0 { + return LastError() + } + return nil +} + +func (branch *Branch) Upstream() (*Branch, error) { + upstream := new(Branch) + err := C.git_branch_upstream(&upstream.ptr, branch.ptr) + if err < 0 { + return nil, LastError() + } + return upstream, nil +} + +func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) { + cName := C.CString(canonicalBranchName) + + // Obtain the length of the name + ret := C.git_branch_upstream_name(nil, 0, repo.ptr, cName) + if ret < 0 { + return "", LastError() + } + + cBuf := (*C.char)(C.malloc(C.size_t(ret))) + defer C.free(unsafe.Pointer(cBuf)) + + // Actually obtain the name + ret = C.git_branch_upstream_name(cBuf, C.size_t(ret), repo.ptr, cName) + if ret < 0 { + return "", LastError() + } + return C.GoString(cBuf), nil +} -- cgit v1.2.3 From f03cec5375d22bda9efebb01e78d9e752ee2b498 Mon Sep 17 00:00:00 2001 From: Johann Weging Date: Tue, 8 Oct 2013 14:39:05 +0200 Subject: branch: Changed BranchT to BranchType --- branch.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index 8dd55a3..651edb2 100644 --- a/branch.go +++ b/branch.go @@ -17,11 +17,11 @@ var ErrEUser = errors.New("Error in user callback function") type ListFlags uint -type BranchT uint +type BranchType uint const ( - BRANCH_LOCAL BranchT = C.GIT_BRANCH_LOCAL - BRANCH_REMOTE = C.GIT_BRANCH_REMOTE + BRANCH_LOCAL BranchType = C.GIT_BRANCH_LOCAL + BRANCH_REMOTE = C.GIT_BRANCH_REMOTE ) const ( @@ -115,7 +115,7 @@ func (branch *Branch) IsHead() (bool, error) { } -func (repo *Repository) BranchLookup(branchName string, branchType BranchT) (*Branch, error) { +func (repo *Repository) BranchLookup(branchName string, branchType BranchType) (*Branch, error) { branch := new(Branch) cName := C.CString(branchName) -- cgit v1.2.3 From 771e0c11bc8b1f00cdd6fdddbfe114957aa77ce2 Mon Sep 17 00:00:00 2001 From: Johann Weging Date: Tue, 8 Oct 2013 14:44:11 +0200 Subject: branch: Variable names don't repeat its type name any longer --- branch.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index 651edb2..e431f07 100644 --- a/branch.go +++ b/branch.go @@ -46,8 +46,8 @@ func (repo *Repository) BranchCreate(branchName string, target *Commit, force bo return ref, nil } -func (branch *Branch) BranchDelete() error { - if err := C.git_branch_delete(branch.ptr); err < 0 { +func (b *Branch) BranchDelete() error { + if err := C.git_branch_delete(b.ptr); err < 0 { return LastError() } return nil @@ -90,20 +90,20 @@ func (repo *Repository) BranchForeach(flags ListFlags, callback BranchForeachCB, return err } -func (branch *Branch) Move(newBranchName string, force bool) (*Branch, error) { +func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) { newBranch := new(Branch) cNewBranchName := C.CString(newBranchName) cForce := cbool(force) - err := C.git_branch_move(&newBranch.ptr, branch.ptr, cNewBranchName, cForce) + err := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce) if err < 0 { return nil, LastError() } return newBranch, nil } -func (branch *Branch) IsHead() (bool, error) { - isHead := C.git_branch_is_head(branch.ptr) +func (b *Branch) IsHead() (bool, error) { + isHead := C.git_branch_is_head(b.ptr) switch isHead { case 1: return true, nil @@ -115,22 +115,22 @@ func (branch *Branch) IsHead() (bool, error) { } -func (repo *Repository) BranchLookup(branchName string, branchType BranchType) (*Branch, error) { +func (repo *Repository) BranchLookup(branchName string, bt BranchType) (*Branch, error) { branch := new(Branch) cName := C.CString(branchName) - err := C.git_branch_lookup(&branch.ptr, repo.ptr, cName, C.git_branch_t(branchType)) + err := C.git_branch_lookup(&branch.ptr, repo.ptr, cName, C.git_branch_t(bt)) if err < 0 { return nil, LastError() } return branch, nil } -func (branch *Branch) Name() (string, error) { +func (b *Branch) Name() (string, error) { var cName *C.char defer C.free(unsafe.Pointer(cName)) - err := C.git_branch_name(&cName, branch.ptr) + err := C.git_branch_name(&cName, b.ptr) if err < 0 { return "", LastError() } @@ -159,19 +159,19 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) { return C.GoString(cBuf), nil } -func (branch *Branch) SetUpstream(upstreamName string) error { +func (b *Branch) SetUpstream(upstreamName string) error { cName := C.CString(upstreamName) - err := C.git_branch_set_upstream(branch.ptr, cName) + err := C.git_branch_set_upstream(b.ptr, cName) if err < 0 { return LastError() } return nil } -func (branch *Branch) Upstream() (*Branch, error) { +func (b *Branch) Upstream() (*Branch, error) { upstream := new(Branch) - err := C.git_branch_upstream(&upstream.ptr, branch.ptr) + err := C.git_branch_upstream(&upstream.ptr, b.ptr) if err < 0 { return nil, LastError() } -- cgit v1.2.3 From 6372ec052fb752122bc0662783b8450bbe2ce983 Mon Sep 17 00:00:00 2001 From: Johann Weging Date: Tue, 8 Oct 2013 14:49:03 +0200 Subject: branch: Renamed BranchCreate to CreateBranch --- branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index e431f07..7a4e4cb 100644 --- a/branch.go +++ b/branch.go @@ -35,7 +35,7 @@ type Branch struct { Reference } -func (repo *Repository) BranchCreate(branchName string, target *Commit, force bool) (*Reference, error) { +func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool) (*Reference, error) { ref := new(Reference) cBranchName := C.CString(branchName) cForce := cbool(force) -- cgit v1.2.3 From 4c4da3a84621cc57e90bfe55b16342796c80aceb Mon Sep 17 00:00:00 2001 From: Johann Weging Date: Tue, 8 Oct 2013 14:52:22 +0200 Subject: branch: Renamed BranchLookup to LookupBrnach --- branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index 7a4e4cb..07f3f41 100644 --- a/branch.go +++ b/branch.go @@ -115,7 +115,7 @@ func (b *Branch) IsHead() (bool, error) { } -func (repo *Repository) BranchLookup(branchName string, bt BranchType) (*Branch, error) { +func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch, error) { branch := new(Branch) cName := C.CString(branchName) -- cgit v1.2.3 From ed86064871639a956beb5592dc5b64e3d536f882 Mon Sep 17 00:00:00 2001 From: Johann Weging Date: Thu, 10 Oct 2013 10:39:49 +0200 Subject: branch:BranchForeach: Correct handling of the ListFlags --- branch.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index 07f3f41..11e12da 100644 --- a/branch.go +++ b/branch.go @@ -15,10 +15,10 @@ import ( var ErrEUser = errors.New("Error in user callback function") -type ListFlags uint - type BranchType uint +type ListFlags BranchType + const ( BRANCH_LOCAL BranchType = C.GIT_BRANCH_LOCAL BRANCH_REMOTE = C.GIT_BRANCH_REMOTE @@ -62,12 +62,18 @@ func (repo *Repository) BranchForeach(flags ListFlags, callback BranchForeachCB, } for { + var branchLocal bool + var branchRemote bool + ref, err := iter.Next() if err == ErrIterOver { break } - if (flags == ListFlags(BRANCH_LOCAL)) && strings.HasPrefix(ref.Name(), REFS_HEADS_DIR) { + if flags&ListFlags(BRANCH_LOCAL) > 0 { + branchLocal = true + } + if branchLocal && strings.HasPrefix(ref.Name(), REFS_HEADS_DIR) { name := strings.TrimPrefix(ref.Name(), REFS_HEADS_DIR) err = callback(name, ListFlags(BRANCH_LOCAL), payload) if err != nil { @@ -75,7 +81,10 @@ func (repo *Repository) BranchForeach(flags ListFlags, callback BranchForeachCB, } } - if (flags == ListFlags(BRANCH_REMOTE)) && strings.HasPrefix(ref.Name(), REFS_REMOTES_DIR) { + if flags&ListFlags(BRANCH_REMOTE) > 0 { + branchRemote = true + } + if branchRemote && strings.HasPrefix(ref.Name(), REFS_REMOTES_DIR) { name := strings.TrimPrefix(ref.Name(), REFS_REMOTES_DIR) err = callback(name, ListFlags(BRANCH_REMOTE), payload) if err != nil { -- cgit v1.2.3 From 961db94aa21da58a77968099c6b97890e6235d10 Mon Sep 17 00:00:00 2001 From: Johann Weging Date: Wed, 30 Oct 2013 15:01:08 +0100 Subject: branch: Deleted BranchForeach --- branch.go | 52 ---------------------------------------------------- 1 file changed, 52 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index 11e12da..d30748f 100644 --- a/branch.go +++ b/branch.go @@ -8,17 +8,11 @@ package git import "C" import ( - "errors" - "strings" "unsafe" ) -var ErrEUser = errors.New("Error in user callback function") - type BranchType uint -type ListFlags BranchType - const ( BRANCH_LOCAL BranchType = C.GIT_BRANCH_LOCAL BRANCH_REMOTE = C.GIT_BRANCH_REMOTE @@ -53,52 +47,6 @@ func (b *Branch) BranchDelete() error { return nil } -type BranchForeachCB func(name string, flags ListFlags, payload interface{}) error - -func (repo *Repository) BranchForeach(flags ListFlags, callback BranchForeachCB, payload interface{}) error { - iter, err := repo.NewReferenceIterator() - if err != nil { - return err - } - - for { - var branchLocal bool - var branchRemote bool - - ref, err := iter.Next() - if err == ErrIterOver { - break - } - - if flags&ListFlags(BRANCH_LOCAL) > 0 { - branchLocal = true - } - if branchLocal && strings.HasPrefix(ref.Name(), REFS_HEADS_DIR) { - name := strings.TrimPrefix(ref.Name(), REFS_HEADS_DIR) - err = callback(name, ListFlags(BRANCH_LOCAL), payload) - if err != nil { - return err - } - } - - if flags&ListFlags(BRANCH_REMOTE) > 0 { - branchRemote = true - } - if branchRemote && strings.HasPrefix(ref.Name(), REFS_REMOTES_DIR) { - name := strings.TrimPrefix(ref.Name(), REFS_REMOTES_DIR) - err = callback(name, ListFlags(BRANCH_REMOTE), payload) - if err != nil { - return err - } - } - } - - if err == ErrIterOver { - err = nil - } - return err -} - func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) { newBranch := new(Branch) cNewBranchName := C.CString(newBranchName) -- cgit v1.2.3 From a728f70358ab58364c643d04c94a66b3ec2cd947 Mon Sep 17 00:00:00 2001 From: Jesse Ezell Date: Wed, 26 Feb 2014 07:33:50 -0800 Subject: cleanup add-branch --- branch.go | 66 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 34 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index d30748f..ba82f93 100644 --- a/branch.go +++ b/branch.go @@ -14,26 +14,33 @@ import ( type BranchType uint const ( - BRANCH_LOCAL BranchType = C.GIT_BRANCH_LOCAL - BRANCH_REMOTE = C.GIT_BRANCH_REMOTE + BranchLocal BranchType = C.GIT_BRANCH_LOCAL + BranchRemote = C.GIT_BRANCH_REMOTE ) const ( - REFS_DIR = "refs/" - REFS_HEADS_DIR = REFS_DIR + "heads/" - REFS_TAGS_DIR = REFS_DIR + "tags/" - REFS_REMOTES_DIR = REFS_DIR + "remotes/" + RefsDir = "refs/" + RefsHeadsDir = RefsDir + "heads/" + RefsTagsDir = RefsDir + "tags/" + RefsRemotesDir = RefsDir + "remotes/" ) type Branch struct { Reference } -func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool) (*Reference, error) { +func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, message string) (*Reference, error) { ref := new(Reference) cBranchName := C.CString(branchName) cForce := cbool(force) - err := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce) + + cSignature := signature.toC() + defer C.git_signature_free(cSignature) + + cMessage := C.CString(message) + defer C.free(unsafe.Pointer(cMessage)) + + err := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce, cSignature, cMessage) if err < 0 { return nil, LastError() } @@ -47,12 +54,18 @@ func (b *Branch) BranchDelete() error { return nil } -func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) { +func (b *Branch) Move(newBranchName string, force bool, signature *Signature, message string) (*Branch, error) { newBranch := new(Branch) cNewBranchName := C.CString(newBranchName) cForce := cbool(force) - err := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce) + cSignature := signature.toC() + defer C.git_signature_free(cSignature) + + cMessage := C.CString(message) + defer C.free(unsafe.Pointer(cMessage)) + + err := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce, cSignature, cMessage) if err < 0 { return nil, LastError() } @@ -98,22 +111,14 @@ func (b *Branch) Name() (string, error) { func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) { cName := C.CString(canonicalBranchName) - // Obtain the length of the name - ret := C.git_branch_remote_name(nil, 0, repo.ptr, cName) - if ret < 0 { - return "", LastError() - } - - cBuf := (*C.char)(C.malloc(C.size_t(ret))) - defer C.free(unsafe.Pointer(cBuf)) + nameBuf := C.git_buf{} - // Actually obtain the name - ret = C.git_branch_remote_name(cBuf, C.size_t(ret), repo.ptr, cName) - if ret < 0 { + if C.git_branch_remote_name(&nameBuf, repo.ptr, cName) < 0 { return "", LastError() } + C.git_buf_free(&nameBuf) - return C.GoString(cBuf), nil + return C.GoStringN(nameBuf.ptr, C.int(nameBuf.size)), nil } func (b *Branch) SetUpstream(upstreamName string) error { @@ -138,19 +143,12 @@ func (b *Branch) Upstream() (*Branch, error) { func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) { cName := C.CString(canonicalBranchName) - // Obtain the length of the name - ret := C.git_branch_upstream_name(nil, 0, repo.ptr, cName) - if ret < 0 { - return "", LastError() - } - - cBuf := (*C.char)(C.malloc(C.size_t(ret))) - defer C.free(unsafe.Pointer(cBuf)) + nameBuf := C.git_buf{} - // Actually obtain the name - ret = C.git_branch_upstream_name(cBuf, C.size_t(ret), repo.ptr, cName) - if ret < 0 { + if C.git_branch_upstream_name(&nameBuf, repo.ptr, cName) < 0 { return "", LastError() } - return C.GoString(cBuf), nil + C.git_buf_free(&nameBuf) + + return C.GoStringN(nameBuf.ptr, C.int(nameBuf.size)), nil } -- cgit v1.2.3 From fe509411a5e8bd45a1c5607d1cc212d8ebf45541 Mon Sep 17 00:00:00 2001 From: Jesse Ezell Date: Wed, 26 Feb 2014 08:45:38 -0800 Subject: Add thread locking --- branch.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index ba82f93..77dbbb8 100644 --- a/branch.go +++ b/branch.go @@ -8,6 +8,7 @@ package git import "C" import ( + "runtime" "unsafe" ) @@ -30,6 +31,7 @@ type Branch struct { } func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, message string) (*Reference, error) { + ref := new(Reference) cBranchName := C.CString(branchName) cForce := cbool(force) @@ -40,6 +42,9 @@ func (repo *Repository) CreateBranch(branchName string, target *Commit, force bo cMessage := C.CString(message) defer C.free(unsafe.Pointer(cMessage)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce, cSignature, cMessage) if err < 0 { return nil, LastError() @@ -47,7 +52,11 @@ func (repo *Repository) CreateBranch(branchName string, target *Commit, force bo return ref, nil } -func (b *Branch) BranchDelete() error { +func (b *Branch) Delete() error { + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if err := C.git_branch_delete(b.ptr); err < 0 { return LastError() } @@ -65,6 +74,9 @@ func (b *Branch) Move(newBranchName string, force bool, signature *Signature, me cMessage := C.CString(message) defer C.free(unsafe.Pointer(cMessage)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce, cSignature, cMessage) if err < 0 { return nil, LastError() @@ -73,6 +85,10 @@ func (b *Branch) Move(newBranchName string, force bool, signature *Signature, me } func (b *Branch) IsHead() (bool, error) { + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + isHead := C.git_branch_is_head(b.ptr) switch isHead { case 1: @@ -89,6 +105,9 @@ func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch, branch := new(Branch) cName := C.CString(branchName) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C.git_branch_lookup(&branch.ptr, repo.ptr, cName, C.git_branch_t(bt)) if err < 0 { return nil, LastError() @@ -100,6 +119,9 @@ func (b *Branch) Name() (string, error) { var cName *C.char defer C.free(unsafe.Pointer(cName)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C.git_branch_name(&cName, b.ptr) if err < 0 { return "", LastError() @@ -113,6 +135,9 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) { nameBuf := C.git_buf{} + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if C.git_branch_remote_name(&nameBuf, repo.ptr, cName) < 0 { return "", LastError() } @@ -124,6 +149,9 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) { func (b *Branch) SetUpstream(upstreamName string) error { cName := C.CString(upstreamName) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C.git_branch_set_upstream(b.ptr, cName) if err < 0 { return LastError() @@ -133,6 +161,10 @@ func (b *Branch) SetUpstream(upstreamName string) error { func (b *Branch) Upstream() (*Branch, error) { upstream := new(Branch) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + err := C.git_branch_upstream(&upstream.ptr, b.ptr) if err < 0 { return nil, LastError() @@ -145,6 +177,9 @@ func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) nameBuf := C.git_buf{} + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if C.git_branch_upstream_name(&nameBuf, repo.ptr, cName) < 0 { return "", LastError() } -- cgit v1.2.3 From a5df6111003cb032911a793f186aefb8f27243ef Mon Sep 17 00:00:00 2001 From: Jesse Ezell Date: Wed, 26 Feb 2014 08:50:47 -0800 Subject: LastError -> MakeGitError --- branch.go | 58 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index 77dbbb8..68e1b93 100644 --- a/branch.go +++ b/branch.go @@ -45,9 +45,9 @@ func (repo *Repository) CreateBranch(branchName string, target *Commit, force bo runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce, cSignature, cMessage) - if err < 0 { - return nil, LastError() + ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce, cSignature, cMessage) + if ret < 0 { + return nil, MakeGitError(ret) } return ref, nil } @@ -56,9 +56,9 @@ func (b *Branch) Delete() error { runtime.LockOSThread() defer runtime.UnlockOSThread() - - if err := C.git_branch_delete(b.ptr); err < 0 { - return LastError() + ret := C.git_branch_delete(b.ptr) + if ret < 0 { + return MakeGitError(ret) } return nil } @@ -77,9 +77,9 @@ func (b *Branch) Move(newBranchName string, force bool, signature *Signature, me runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce, cSignature, cMessage) - if err < 0 { - return nil, LastError() + ret := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce, cSignature, cMessage) + if ret < 0 { + return nil, MakeGitError(ret) } return newBranch, nil } @@ -89,14 +89,14 @@ func (b *Branch) IsHead() (bool, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - isHead := C.git_branch_is_head(b.ptr) - switch isHead { + ret := C.git_branch_is_head(b.ptr) + switch ret { case 1: return true, nil case 0: return false, nil default: - return false, LastError() + return false, MakeGitError(ret) } } @@ -108,9 +108,9 @@ func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch, runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C.git_branch_lookup(&branch.ptr, repo.ptr, cName, C.git_branch_t(bt)) - if err < 0 { - return nil, LastError() + ret := C.git_branch_lookup(&branch.ptr, repo.ptr, cName, C.git_branch_t(bt)) + if ret < 0 { + return nil, MakeGitError(ret) } return branch, nil } @@ -122,9 +122,9 @@ func (b *Branch) Name() (string, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C.git_branch_name(&cName, b.ptr) - if err < 0 { - return "", LastError() + ret := C.git_branch_name(&cName, b.ptr) + if ret < 0 { + return "", MakeGitError(ret) } return C.GoString(cName), nil @@ -138,8 +138,9 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - if C.git_branch_remote_name(&nameBuf, repo.ptr, cName) < 0 { - return "", LastError() + ret := C.git_branch_remote_name(&nameBuf, repo.ptr, cName) + if ret < 0 { + return "", MakeGitError(ret) } C.git_buf_free(&nameBuf) @@ -152,9 +153,9 @@ func (b *Branch) SetUpstream(upstreamName string) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C.git_branch_set_upstream(b.ptr, cName) - if err < 0 { - return LastError() + ret := C.git_branch_set_upstream(b.ptr, cName) + if ret < 0 { + return MakeGitError(ret) } return nil } @@ -165,9 +166,9 @@ func (b *Branch) Upstream() (*Branch, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C.git_branch_upstream(&upstream.ptr, b.ptr) - if err < 0 { - return nil, LastError() + ret := C.git_branch_upstream(&upstream.ptr, b.ptr) + if ret < 0 { + return nil, MakeGitError(ret) } return upstream, nil } @@ -180,8 +181,9 @@ func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) runtime.LockOSThread() defer runtime.UnlockOSThread() - if C.git_branch_upstream_name(&nameBuf, repo.ptr, cName) < 0 { - return "", LastError() + ret := C.git_branch_upstream_name(&nameBuf, repo.ptr, cName) + if ret < 0 { + return "", MakeGitError(ret) } C.git_buf_free(&nameBuf) -- cgit v1.2.3 From 2c56324ca5e2513b386a7b1f94b3b62881183769 Mon Sep 17 00:00:00 2001 From: Jesse Ezell Date: Fri, 28 Feb 2014 10:46:57 -0800 Subject: fix bad git_buf handling --- branch.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index 68e1b93..9f8c22b 100644 --- a/branch.go +++ b/branch.go @@ -142,9 +142,9 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) { if ret < 0 { return "", MakeGitError(ret) } - C.git_buf_free(&nameBuf) + defer C.git_buf_free(&nameBuf) - return C.GoStringN(nameBuf.ptr, C.int(nameBuf.size)), nil + return C.GoString(nameBuf.ptr), nil } func (b *Branch) SetUpstream(upstreamName string) error { @@ -185,7 +185,7 @@ func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) if ret < 0 { return "", MakeGitError(ret) } - C.git_buf_free(&nameBuf) + defer C.git_buf_free(&nameBuf) - return C.GoStringN(nameBuf.ptr, C.int(nameBuf.size)), nil + return C.GoString(nameBuf.ptr), nil } -- cgit v1.2.3 From b404c8b86250b5abdbb02714cfdc08254c67df49 Mon Sep 17 00:00:00 2001 From: Jesse Ezell Date: Fri, 28 Feb 2014 10:47:56 -0800 Subject: Remove unused consts --- branch.go | 7 ------- 1 file changed, 7 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index 9f8c22b..eb22fde 100644 --- a/branch.go +++ b/branch.go @@ -19,13 +19,6 @@ const ( BranchRemote = C.GIT_BRANCH_REMOTE ) -const ( - RefsDir = "refs/" - RefsHeadsDir = RefsDir + "heads/" - RefsTagsDir = RefsDir + "tags/" - RefsRemotesDir = RefsDir + "remotes/" -) - type Branch struct { Reference } -- cgit v1.2.3 From d6332f9526b48e5145db4ee32d8976cdd0f5972c Mon Sep 17 00:00:00 2001 From: Jesse Ezell Date: Fri, 28 Feb 2014 10:54:16 -0800 Subject: fix msg handling to treat empty str as nil --- branch.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index eb22fde..777231c 100644 --- a/branch.go +++ b/branch.go @@ -23,7 +23,7 @@ type Branch struct { Reference } -func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, message string) (*Reference, error) { +func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, msg string) (*Reference, error) { ref := new(Reference) cBranchName := C.CString(branchName) @@ -32,13 +32,18 @@ func (repo *Repository) CreateBranch(branchName string, target *Commit, force bo cSignature := signature.toC() defer C.git_signature_free(cSignature) - cMessage := C.CString(message) - defer C.free(unsafe.Pointer(cMessage)) + var cmsg *C.char + if msg == "" { + cmsg = nil + } else { + cmsg = C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + } runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce, cSignature, cMessage) + ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce, cSignature, cmsg) if ret < 0 { return nil, MakeGitError(ret) } @@ -56,7 +61,7 @@ func (b *Branch) Delete() error { return nil } -func (b *Branch) Move(newBranchName string, force bool, signature *Signature, message string) (*Branch, error) { +func (b *Branch) Move(newBranchName string, force bool, signature *Signature, msg string) (*Branch, error) { newBranch := new(Branch) cNewBranchName := C.CString(newBranchName) cForce := cbool(force) @@ -64,13 +69,18 @@ func (b *Branch) Move(newBranchName string, force bool, signature *Signature, me cSignature := signature.toC() defer C.git_signature_free(cSignature) - cMessage := C.CString(message) - defer C.free(unsafe.Pointer(cMessage)) + var cmsg *C.char + if msg == "" { + cmsg = nil + } else { + cmsg = C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + } runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce, cSignature, cMessage) + ret := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce, cSignature, cmsg) if ret < 0 { return nil, MakeGitError(ret) } -- cgit v1.2.3 From 127643eb543cbeac88466956a6394505abc1176e Mon Sep 17 00:00:00 2001 From: Jesse Ezell Date: Fri, 28 Feb 2014 11:08:15 -0800 Subject: move return outside of switch for go 1.0 / travis --- branch.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'branch.go') diff --git a/branch.go b/branch.go index 777231c..aee23e4 100644 --- a/branch.go +++ b/branch.go @@ -98,9 +98,8 @@ func (b *Branch) IsHead() (bool, error) { return true, nil case 0: return false, nil - default: - return false, MakeGitError(ret) } + return false, MakeGitError(ret) } -- cgit v1.2.3 From a06f4a030a90129db76e5a741a73bba5b27cda29 Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Tue, 1 Apr 2014 12:13:37 +0200 Subject: Adjust to Go tip changes It does not like breaking aliasing rules, so let's keep a casted pointer for when libgit2 wants that. --- blob.go | 7 ++++--- branch.go | 2 +- commit.go | 29 +++++++++++++++-------------- object.go | 19 ++++++++++++++----- repository.go | 6 +++--- tree.go | 11 ++++++----- 6 files changed, 43 insertions(+), 31 deletions(-) (limited to 'branch.go') diff --git a/blob.go b/blob.go index 4cee876..4277127 100644 --- a/blob.go +++ b/blob.go @@ -20,15 +20,16 @@ import ( type Blob struct { gitObject + cast_ptr *C.git_blob } func (v *Blob) Size() int64 { - return int64(C.git_blob_rawsize(v.ptr)) + return int64(C.git_blob_rawsize(v.cast_ptr)) } func (v *Blob) Contents() []byte { - size := C.int(C.git_blob_rawsize(v.ptr)) - buffer := unsafe.Pointer(C.git_blob_rawcontent(v.ptr)) + size := C.int(C.git_blob_rawsize(v.cast_ptr)) + buffer := unsafe.Pointer(C.git_blob_rawcontent(v.cast_ptr)) return C.GoBytes(buffer, size) } diff --git a/branch.go b/branch.go index aee23e4..bd7312b 100644 --- a/branch.go +++ b/branch.go @@ -43,7 +43,7 @@ func (repo *Repository) CreateBranch(branchName string, target *Commit, force bo runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce, cSignature, cmsg) + ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.cast_ptr, cForce, cSignature, cmsg) if ret < 0 { return nil, MakeGitError(ret) } diff --git a/commit.go b/commit.go index 0edebb6..0a5cfce 100644 --- a/commit.go +++ b/commit.go @@ -17,56 +17,57 @@ import ( // Commit type Commit struct { gitObject + cast_ptr *C.git_commit } func (c Commit) Message() string { - return C.GoString(C.git_commit_message(c.ptr)) + return C.GoString(C.git_commit_message(c.cast_ptr)) } func (c Commit) Tree() (*Tree, error) { - var ptr *C.git_object + var ptr *C.git_tree runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C.git_commit_tree(&ptr, c.ptr) + err := C.git_commit_tree(&ptr, c.cast_ptr) if err < 0 { return nil, MakeGitError(err) } - return allocObject(ptr).(*Tree), nil + return allocObject((*C.git_object)(ptr)).(*Tree), nil } func (c Commit) TreeId() *Oid { - return newOidFromC(C.git_commit_tree_id(c.ptr)) + return newOidFromC(C.git_commit_tree_id(c.cast_ptr)) } func (c Commit) Author() *Signature { - ptr := C.git_commit_author(c.ptr) - return newSignatureFromC(ptr) + cast_ptr := C.git_commit_author(c.cast_ptr) + return newSignatureFromC(cast_ptr) } func (c Commit) Committer() *Signature { - ptr := C.git_commit_committer(c.ptr) - return newSignatureFromC(ptr) + cast_ptr := C.git_commit_committer(c.cast_ptr) + return newSignatureFromC(cast_ptr) } func (c *Commit) Parent(n uint) *Commit { - var cobj *C.git_object - ret := C.git_commit_parent(&cobj, c.ptr, C.uint(n)) + var cobj *C.git_commit + ret := C.git_commit_parent(&cobj, c.cast_ptr, C.uint(n)) if ret != 0 { return nil } - return allocObject(cobj).(*Commit) + return allocObject((*C.git_object)(cobj)).(*Commit) } func (c *Commit) ParentId(n uint) *Oid { - return newOidFromC(C.git_commit_parent_id(c.ptr, C.uint(n))) + return newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n))) } func (c *Commit) ParentCount() uint { - return uint(C.git_commit_parentcount(c.ptr)) + return uint(C.git_commit_parentcount(c.cast_ptr)) } // Signature diff --git a/object.go b/object.go index 090be1f..101d15e 100644 --- a/object.go +++ b/object.go @@ -48,7 +48,7 @@ func (t ObjectType) String() (string) { } func (o gitObject) Id() *Oid { - return newOidFromC(C.git_commit_id(o.ptr)) + return newOidFromC(C.git_object_id(o.ptr)) } func (o gitObject) Type() ObjectType { @@ -57,24 +57,33 @@ func (o gitObject) Type() ObjectType { func (o *gitObject) Free() { runtime.SetFinalizer(o, nil) - C.git_commit_free(o.ptr) + C.git_object_free(o.ptr) } func allocObject(cobj *C.git_object) Object { switch ObjectType(C.git_object_type(cobj)) { case ObjectCommit: - commit := &Commit{gitObject{cobj}} + commit := &Commit{ + gitObject: gitObject{cobj}, + cast_ptr: (*C.git_commit)(cobj), + } runtime.SetFinalizer(commit, (*Commit).Free) return commit case ObjectTree: - tree := &Tree{gitObject{cobj}} + tree := &Tree{ + gitObject: gitObject{cobj}, + cast_ptr: (*C.git_tree)(cobj), + } runtime.SetFinalizer(tree, (*Tree).Free) return tree case ObjectBlob: - blob := &Blob{gitObject{cobj}} + blob := &Blob{ + gitObject: gitObject{cobj}, + cast_ptr: (*C.git_blob)(cobj), + } runtime.SetFinalizer(blob, (*Blob).Free) return blob } diff --git a/repository.go b/repository.go index d6eadc8..d757747 100644 --- a/repository.go +++ b/repository.go @@ -239,7 +239,7 @@ func (v *Repository) CreateCommit( if nparents > 0 { cparents = make([]*C.git_commit, nparents) for i, v := range parents { - cparents[i] = v.ptr + cparents[i] = v.cast_ptr } parentsarg = &cparents[0] } @@ -256,7 +256,7 @@ func (v *Repository) CreateCommit( ret := C.git_commit_create( oid.toC(), v.ptr, cref, authorSig, committerSig, - nil, cmsg, tree.ptr, C.size_t(nparents), parentsarg) + nil, cmsg, tree.cast_ptr, C.size_t(nparents), parentsarg) if ret < 0 { return nil, MakeGitError(ret) @@ -331,7 +331,7 @@ func (v *Repository) TreeBuilderFromTree(tree *Tree) (*TreeBuilder, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - if ret := C.git_treebuilder_create(&bld.ptr, tree.ptr); ret < 0 { + if ret := C.git_treebuilder_create(&bld.ptr, tree.cast_ptr); ret < 0 { return nil, MakeGitError(ret) } runtime.SetFinalizer(bld, (*TreeBuilder).Free) diff --git a/tree.go b/tree.go index 7070ac7..8356fba 100644 --- a/tree.go +++ b/tree.go @@ -26,6 +26,7 @@ const ( type Tree struct { gitObject + cast_ptr *C.git_tree } type TreeEntry struct { @@ -48,7 +49,7 @@ func (t Tree) EntryByName(filename string) *TreeEntry { cname := C.CString(filename) defer C.free(unsafe.Pointer(cname)) - entry := C.git_tree_entry_byname(t.ptr, cname) + entry := C.git_tree_entry_byname(t.cast_ptr, cname) if entry == nil { return nil } @@ -66,7 +67,7 @@ func (t Tree) EntryByPath(path string) (*TreeEntry, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_tree_entry_bypath(&entry, t.ptr, cpath) + ret := C.git_tree_entry_bypath(&entry, t.cast_ptr, cpath) if ret < 0 { return nil, MakeGitError(ret) } @@ -75,7 +76,7 @@ func (t Tree) EntryByPath(path string) (*TreeEntry, error) { } func (t Tree) EntryByIndex(index uint64) *TreeEntry { - entry := C.git_tree_entry_byindex(t.ptr, C.size_t(index)) + entry := C.git_tree_entry_byindex(t.cast_ptr, C.size_t(index)) if entry == nil { return nil } @@ -84,7 +85,7 @@ func (t Tree) EntryByIndex(index uint64) *TreeEntry { } func (t Tree) EntryCount() uint64 { - num := C.git_tree_entrycount(t.ptr) + num := C.git_tree_entrycount(t.cast_ptr) return uint64(num) } @@ -104,7 +105,7 @@ func (t Tree) Walk(callback TreeWalkCallback) error { defer runtime.UnlockOSThread() err := C._go_git_treewalk( - t.ptr, + t.cast_ptr, C.GIT_TREEWALK_PRE, unsafe.Pointer(&callback), ) -- cgit v1.2.3