summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Ezell <[email protected]>2014-03-20 22:02:19 -0700
committerJesse Ezell <[email protected]>2014-03-20 22:02:19 -0700
commit37964e878f70229171f94668cb96f347b798e2b2 (patch)
tree5781761e13e57edb3589844b6faa799c1158b93a
parentd0b334b24409ddc190a7010be0072d87df6b6bfe (diff)
parent2811845a1287d949a74b8ed80a5791fd8875002a (diff)
merge with latest
-rw-r--r--.travis.yml12
-rw-r--r--blob.go65
-rw-r--r--branch.go193
-rw-r--r--checkout.go47
-rw-r--r--clone.go75
-rw-r--r--clone_test.go23
-rw-r--r--commit.go7
-rw-r--r--config.go311
-rw-r--r--credentials.go74
-rw-r--r--git.go86
-rw-r--r--git_test.go19
-rw-r--r--index.go68
-rw-r--r--index_test.go19
-rw-r--r--odb.go30
-rw-r--r--odb_test.go31
-rw-r--r--packbuilder.go10
-rw-r--r--push.go182
-rw-r--r--push_test.go59
-rw-r--r--reference.go88
-rw-r--r--reference_test.go46
-rw-r--r--remote.go436
-rw-r--r--remote_test.go47
-rw-r--r--repository.go159
-rwxr-xr-xscript/build-libgit2.sh17
-rw-r--r--submodule.go45
-rw-r--r--tree.go46
-rw-r--r--walk.go29
-rw-r--r--wrapper.c39
28 files changed, 2071 insertions, 192 deletions
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..86f8265
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,12 @@
+language: go
+
+go:
+ - 1.0
+ - 1.1
+ - tip
+
+env:
+ - PKG_CONFIG_PATH=libgit2/install/lib/pkgconfig LD_LIBRARY_PATH=libgit2/install/lib
+
+install:
+ - script/build-libgit2.sh
diff --git a/blob.go b/blob.go
index b638c4f..4cee876 100644
--- a/blob.go
+++ b/blob.go
@@ -3,9 +3,18 @@ package git
/*
#include <git2.h>
#include <git2/errors.h>
+#include <string.h>
+
+extern int _go_git_blob_create_fromchunks(git_oid *id,
+ git_repository *repo,
+ const char *hintpath,
+ void *payload);
+
*/
import "C"
import (
+ "io"
+ "runtime"
"unsafe"
)
@@ -13,13 +22,65 @@ type Blob struct {
gitObject
}
-func (v Blob) Size() int64 {
+func (v *Blob) Size() int64 {
return int64(C.git_blob_rawsize(v.ptr))
}
-func (v Blob) Contents() []byte {
+func (v *Blob) Contents() []byte {
size := C.int(C.git_blob_rawsize(v.ptr))
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.ptr))
return C.GoBytes(buffer, size)
}
+func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+ oid := C.git_oid{}
+ ecode := C.git_blob_create_frombuffer(&oid, repo.ptr, unsafe.Pointer(&data[0]), C.size_t(len(data)))
+ if ecode < 0 {
+ return nil, MakeGitError(ecode)
+ }
+ return newOidFromC(&oid), nil
+}
+
+type BlobChunkCallback func(maxLen int) ([]byte, error)
+
+type BlobCallbackData struct {
+ Callback BlobChunkCallback
+ Error error
+}
+
+//export blobChunkCb
+func blobChunkCb(buffer *C.char, maxLen C.size_t, payload unsafe.Pointer) int {
+ data := (*BlobCallbackData)(payload)
+ goBuf, err := data.Callback(int(maxLen))
+ if err == io.EOF {
+ return 0
+ } else if err != nil {
+ data.Error = err
+ return -1
+ }
+ C.memcpy(unsafe.Pointer(buffer), unsafe.Pointer(&goBuf[0]), C.size_t(len(goBuf)))
+ return len(goBuf)
+}
+
+func (repo *Repository) CreateBlobFromChunks(hintPath string, callback BlobChunkCallback) (*Oid, error) {
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ var chintPath *C.char = nil
+ if len(hintPath) > 0 {
+ C.CString(hintPath)
+ defer C.free(unsafe.Pointer(chintPath))
+ }
+ oid := C.git_oid{}
+ payload := &BlobCallbackData{Callback: callback}
+ ecode := C._go_git_blob_create_fromchunks(&oid, repo.ptr, chintPath, unsafe.Pointer(payload))
+ if payload.Error != nil {
+ return nil, payload.Error
+ }
+ if ecode < 0 {
+ return nil, MakeGitError(ecode)
+ }
+ return newOidFromC(&oid), nil
+}
diff --git a/branch.go b/branch.go
new file mode 100644
index 0000000..aee23e4
--- /dev/null
+++ b/branch.go
@@ -0,0 +1,193 @@
+package git
+
+/*
+#cgo pkg-config: libgit2
+#include <git2.h>
+#include <git2/errors.h>
+*/
+import "C"
+
+import (
+ "runtime"
+ "unsafe"
+)
+
+type BranchType uint
+
+const (
+ BranchLocal BranchType = C.GIT_BRANCH_LOCAL
+ BranchRemote = C.GIT_BRANCH_REMOTE
+)
+
+type Branch struct {
+ Reference
+}
+
+func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, msg string) (*Reference, error) {
+
+ ref := new(Reference)
+ cBranchName := C.CString(branchName)
+ cForce := cbool(force)
+
+ cSignature := signature.toC()
+ defer C.git_signature_free(cSignature)
+
+ 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, cmsg)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ return ref, nil
+}
+
+func (b *Branch) Delete() error {
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+ ret := C.git_branch_delete(b.ptr)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (b *Branch) Move(newBranchName string, force bool, signature *Signature, msg string) (*Branch, error) {
+ newBranch := new(Branch)
+ cNewBranchName := C.CString(newBranchName)
+ cForce := cbool(force)
+
+ cSignature := signature.toC()
+ defer C.git_signature_free(cSignature)
+
+ 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, cmsg)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ return newBranch, nil
+}
+
+func (b *Branch) IsHead() (bool, error) {
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_branch_is_head(b.ptr)
+ switch ret {
+ case 1:
+ return true, nil
+ case 0:
+ return false, nil
+ }
+ return false, MakeGitError(ret)
+
+}
+
+func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch, error) {
+ branch := new(Branch)
+ cName := C.CString(branchName)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ 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
+}
+
+func (b *Branch) Name() (string, error) {
+ var cName *C.char
+ defer C.free(unsafe.Pointer(cName))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_branch_name(&cName, b.ptr)
+ if ret < 0 {
+ return "", MakeGitError(ret)
+ }
+
+ return C.GoString(cName), nil
+}
+
+func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
+ cName := C.CString(canonicalBranchName)
+
+ nameBuf := C.git_buf{}
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_branch_remote_name(&nameBuf, repo.ptr, cName)
+ if ret < 0 {
+ return "", MakeGitError(ret)
+ }
+ defer C.git_buf_free(&nameBuf)
+
+ return C.GoString(nameBuf.ptr), nil
+}
+
+func (b *Branch) SetUpstream(upstreamName string) error {
+ cName := C.CString(upstreamName)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_branch_set_upstream(b.ptr, cName)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (b *Branch) Upstream() (*Branch, error) {
+ upstream := new(Branch)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_branch_upstream(&upstream.ptr, b.ptr)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ return upstream, nil
+}
+
+func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) {
+ cName := C.CString(canonicalBranchName)
+
+ nameBuf := C.git_buf{}
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_branch_upstream_name(&nameBuf, repo.ptr, cName)
+ if ret < 0 {
+ return "", MakeGitError(ret)
+ }
+ defer C.git_buf_free(&nameBuf)
+
+ return C.GoString(nameBuf.ptr), nil
+}
diff --git a/checkout.go b/checkout.go
index f4c1d4e..5b72b9a 100644
--- a/checkout.go
+++ b/checkout.go
@@ -2,10 +2,6 @@ package git
/*
#include <git2.h>
-git_checkout_opts git_checkout_opts_init() {
- git_checkout_opts ret = GIT_CHECKOUT_OPTS_INIT;
- return ret;
-}
*/
import "C"
import (
@@ -42,46 +38,59 @@ type CheckoutOpts struct {
FileOpenFlags int // Default is O_CREAT | O_TRUNC | O_WRONLY
}
-// Convert the CheckoutOpts struct to the corresponding C-struct
-func populateCheckoutOpts(ptr *C.git_checkout_opts, opts *CheckoutOpts) {
- *ptr = C.git_checkout_opts_init()
+// Convert the CheckoutOpts struct to the corresponding
+// C-struct. Returns a pointer to ptr, or nil if opts is nil, in order
+// to help with what to pass.
+func populateCheckoutOpts(ptr *C.git_checkout_options, opts *CheckoutOpts) *C.git_checkout_options {
if opts == nil {
- return
+ return nil
}
+
+ C.git_checkout_init_opts(ptr, 1)
ptr.checkout_strategy = C.uint(opts.Strategy)
ptr.disable_filters = cbool(opts.DisableFilters)
ptr.dir_mode = C.uint(opts.DirMode.Perm())
ptr.file_mode = C.uint(opts.FileMode.Perm())
+
+ return ptr
}
// Updates files in the index and the working tree to match the content of
-// the commit pointed at by HEAD.
-func (v *Repository) Checkout(opts *CheckoutOpts) error {
- var copts C.git_checkout_opts
- populateCheckoutOpts(&copts, opts)
+// the commit pointed at by HEAD. opts may be nil.
+func (v *Repository) CheckoutHead(opts *CheckoutOpts) error {
+ var copts C.git_checkout_options
+
+ ptr := populateCheckoutOpts(&copts, opts)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_checkout_head(v.ptr, &copts)
+ ret := C.git_checkout_head(v.ptr, ptr)
if ret < 0 {
- return LastError()
+ return MakeGitError(ret)
}
return nil
}
-// Updates files in the working tree to match the content of the index.
+// Updates files in the working tree to match the content of the given
+// index. If index is nil, the repository's index will be used. opts
+// may be nil.
func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error {
- var copts C.git_checkout_opts
- populateCheckoutOpts(&copts, opts)
+ var copts C.git_checkout_options
+ ptr := populateCheckoutOpts(&copts, opts)
+
+ var iptr *C.git_index = nil
+ if index != nil {
+ iptr = index.ptr
+ }
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_checkout_index(v.ptr, index.ptr, &copts)
+ ret := C.git_checkout_index(v.ptr, iptr, ptr)
if ret < 0 {
- return LastError()
+ return MakeGitError(ret)
}
return nil
diff --git a/clone.go b/clone.go
new file mode 100644
index 0000000..1bc3261
--- /dev/null
+++ b/clone.go
@@ -0,0 +1,75 @@
+package git
+
+/*
+#include <git2.h>
+#include <git2/errors.h>
+
+*/
+import "C"
+import (
+ "runtime"
+ "unsafe"
+)
+
+type CloneOptions struct {
+ *CheckoutOpts
+ *RemoteCallbacks
+ Bare bool
+ IgnoreCertErrors bool
+ RemoteName string
+ CheckoutBranch string
+}
+
+func Clone(url string, path string, options *CloneOptions) (*Repository, error) {
+ repo := new(Repository)
+
+ curl := C.CString(url)
+ defer C.free(unsafe.Pointer(curl))
+
+ cpath := C.CString(path)
+ defer C.free(unsafe.Pointer(cpath))
+
+ var copts C.git_clone_options
+ populateCloneOptions(&copts, options)
+
+ // finish populating clone options here so we can defer CString free
+ if len(options.RemoteName) != 0 {
+ copts.remote_name = C.CString(options.RemoteName)
+ defer C.free(unsafe.Pointer(copts.remote_name))
+ }
+
+ if len(options.CheckoutBranch) != 0 {
+ copts.checkout_branch = C.CString(options.CheckoutBranch)
+ defer C.free(unsafe.Pointer(copts.checkout_branch))
+ }
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+ ret := C.git_clone(&repo.ptr, curl, cpath, &copts)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ runtime.SetFinalizer(repo, (*Repository).Free)
+ return repo, nil
+}
+
+func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions) {
+ C.git_clone_init_options(ptr, C.GIT_CLONE_OPTIONS_VERSION)
+
+ if opts == nil {
+ return
+ }
+ populateCheckoutOpts(&ptr.checkout_opts, opts.CheckoutOpts)
+ populateRemoteCallbacks(&ptr.remote_callbacks, opts.RemoteCallbacks)
+ if opts.Bare {
+ ptr.bare = 1
+ } else {
+ ptr.bare = 0
+ }
+ if opts.IgnoreCertErrors {
+ ptr.ignore_cert_errors = 1
+ } else {
+ ptr.ignore_cert_errors = 0
+ }
+}
diff --git a/clone_test.go b/clone_test.go
new file mode 100644
index 0000000..97366bf
--- /dev/null
+++ b/clone_test.go
@@ -0,0 +1,23 @@
+package git
+
+import (
+ "io/ioutil"
+ "os"
+ "testing"
+)
+
+func TestClone(t *testing.T) {
+
+ repo := createTestRepo(t)
+ defer os.RemoveAll(repo.Workdir())
+
+ seedTestRepo(t, repo)
+
+ path, err := ioutil.TempDir("", "git2go")
+ checkFatal(t, err)
+
+ _, err = Clone(repo.Path(), path, &CloneOptions{Bare: true})
+ defer os.RemoveAll(path)
+
+ checkFatal(t, err)
+}
diff --git a/commit.go b/commit.go
index 498669e..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
@@ -94,6 +94,11 @@ func (v *Signature) Offset() int {
}
func (sig *Signature) toC() *C.git_signature {
+
+ if sig == nil {
+ return nil
+ }
+
var out *C.git_signature
name := C.CString(sig.Name)
diff --git a/config.go b/config.go
index 2aa073a..6b1f093 100644
--- a/config.go
+++ b/config.go
@@ -10,11 +10,81 @@ import (
"unsafe"
)
+type ConfigLevel int
+
+const (
+ // System-wide configuration file; /etc/gitconfig on Linux systems
+ ConfigLevelSystem ConfigLevel = C.GIT_CONFIG_LEVEL_SYSTEM
+
+ // XDG compatible configuration file; typically ~/.config/git/config
+ ConfigLevelXDG ConfigLevel = C.GIT_CONFIG_LEVEL_XDG
+
+ // User-specific configuration file (also called Global configuration
+ // file); typically ~/.gitconfig
+ ConfigLevelGlobal ConfigLevel = C.GIT_CONFIG_LEVEL_GLOBAL
+
+ // Repository specific configuration file; $WORK_DIR/.git/config on
+ // non-bare repos
+ ConfigLevelLocal ConfigLevel = C.GIT_CONFIG_LEVEL_LOCAL
+
+ // Application specific configuration file; freely defined by applications
+ ConfigLevelApp ConfigLevel = C.GIT_CONFIG_LEVEL_APP
+
+ // Represents the highest level available config file (i.e. the most
+ // specific config file available that actually is loaded)
+ ConfigLevelHighest ConfigLevel = C.GIT_CONFIG_HIGHEST_LEVEL
+)
+
+type ConfigEntry struct {
+ Name string
+ Value string
+ Level ConfigLevel
+}
+
+func newConfigEntryFromC(centry *C.git_config_entry) *ConfigEntry {
+ return &ConfigEntry{
+ Name: C.GoString(centry.name),
+ Value: C.GoString(centry.value),
+ Level: ConfigLevel(centry.level),
+ }
+}
+
type Config struct {
ptr *C.git_config
}
-func (c *Config) LookupInt32(name string) (v int32, err error) {
+// NewConfig creates a new empty configuration object
+func NewConfig() (*Config, error) {
+ config := new(Config)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ if ret := C.git_config_new(&config.ptr); ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ return config, nil
+}
+
+// AddFile adds a file-backed backend to the config object at the specified level.
+func (c *Config) AddFile(path string, level ConfigLevel, force bool) error {
+ cpath := C.CString(path)
+ defer C.free(unsafe.Pointer(cpath))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+
+ ret := C.git_config_add_file_ondisk(c.ptr, cpath, C.git_config_level_t(level), cbool(force))
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+
+ return nil
+}
+
+func (c *Config) LookupInt32(name string) (int32, error) {
var out C.int32_t
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@@ -24,13 +94,13 @@ func (c *Config) LookupInt32(name string) (v int32, err 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
}
-func (c *Config) LookupInt64(name string) (v int64, err error) {
+func (c *Config) LookupInt64(name string) (int64, error) {
var out C.int64_t
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@@ -40,13 +110,13 @@ func (c *Config) LookupInt64(name string) (v int64, err 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
}
-func (c *Config) LookupString(name string) (v string, err error) {
+func (c *Config) LookupString(name string) (string, error) {
var ptr *C.char
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@@ -54,15 +124,91 @@ func (c *Config) LookupString(name string) (v string, err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_config_get_string(&ptr, c.ptr, cname)
- if ret < 0 {
- return "", LastError()
+ if ret := C.git_config_get_string(&ptr, c.ptr, cname); ret < 0 {
+ return "", MakeGitError(ret)
}
return C.GoString(ptr), nil
}
-func (c *Config) Set(name, value string) (err error) {
+
+func (c *Config) LookupBool(name string) (bool, error) {
+ var out C.int
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_config_get_bool(&out, c.ptr, cname)
+ if ret < 0 {
+ return false, MakeGitError(ret)
+ }
+
+ return out != 0, nil
+}
+
+func (c *Config) NewMultivarIterator(name, regexp string) (*ConfigIterator, error) {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ var cregexp *C.char
+ if regexp == "" {
+ cregexp = nil
+ } else {
+ cregexp = C.CString(regexp)
+ defer C.free(unsafe.Pointer(cregexp))
+ }
+
+ iter := new(ConfigIterator)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_config_multivar_iterator_new(&iter.ptr, c.ptr, cname, cregexp)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ runtime.SetFinalizer(iter, (*ConfigIterator).Free)
+ return iter, nil
+}
+
+// NewIterator creates an iterator over each entry in the
+// configuration
+func (c *Config) NewIterator() (*ConfigIterator, error) {
+ iter := new(ConfigIterator)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_config_iterator_new(&iter.ptr, c.ptr)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ return iter, nil
+}
+
+// NewIteratorGlob creates an iterator over each entry in the
+// configuration whose name matches the given regular expression
+func (c *Config) NewIteratorGlob(regexp string) (*ConfigIterator, error) {
+ iter := new(ConfigIterator)
+ cregexp := C.CString(regexp)
+ defer C.free(unsafe.Pointer(cregexp))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_config_iterator_glob_new(&iter.ptr, c.ptr, cregexp)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ return iter, nil
+}
+
+func (c *Config) SetString(name, value string) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@@ -74,7 +220,7 @@ func (c *Config) Set(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
@@ -84,3 +230,148 @@ func (c *Config) Free() {
runtime.SetFinalizer(c, nil)
C.git_config_free(c.ptr)
}
+
+func (c *Config) SetInt32(name string, value int32) (err error) {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ ret := C.git_config_set_int32(c.ptr, cname, C.int32_t(value))
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+
+ return nil
+}
+
+func (c *Config) SetInt64(name string, value int64) (err error) {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_config_set_int64(c.ptr, cname, C.int64_t(value))
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+
+ return nil
+}
+
+func (c *Config) SetBool(name string, value bool) (err error) {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_config_set_bool(c.ptr, cname, cbool(value))
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+
+ return nil
+}
+
+func (c *Config) SetMultivar(name, regexp, value string) (err error) {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ cregexp := C.CString(regexp)
+ defer C.free(unsafe.Pointer(cregexp))
+
+ cvalue := C.CString(value)
+ defer C.free(unsafe.Pointer(cvalue))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_config_set_multivar(c.ptr, cname, cregexp, cvalue)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+
+ return nil
+}
+
+func (c *Config) Delete(name string) error {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_config_delete_entry(c.ptr, cname)
+
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+
+ return nil
+}
+
+// OpenLevel creates a single-level focused config object from a multi-level one
+func (c *Config) OpenLevel(parent *Config, level ConfigLevel) (*Config, error) {
+ config := new(Config)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_config_open_level(&config.ptr, parent.ptr, C.git_config_level_t(level))
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ return config, nil
+}
+
+// OpenOndisk creates a new config instance containing a single on-disk file
+func OpenOndisk(parent *Config, path string) (*Config, error) {
+ cpath := C.CString(path)
+ defer C.free(unsafe.Pointer(cpath))
+
+ config := new(Config)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ if ret := C.git_config_open_ondisk(&config.ptr, cpath); ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ return config, nil
+}
+
+// Refresh refreshes the configuration to reflect any changes made externally e.g. on disk
+func (c *Config) Refresh() error {
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ if ret := C.git_config_refresh(c.ptr); ret < 0 {
+ return MakeGitError(ret)
+ }
+
+ return nil
+}
+
+type ConfigIterator struct {
+ ptr *C.git_config_iterator
+}
+
+// Next returns the next entry for this iterator
+func (iter *ConfigIterator) Next() (*ConfigEntry, error) {
+ var centry *C.git_config_entry
+
+ ret := C.git_config_next(&centry, iter.ptr)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ return newConfigEntryFromC(centry), nil
+}
+
+func (iter *ConfigIterator) Free() {
+ runtime.SetFinalizer(iter, nil)
+ C.free(unsafe.Pointer(iter.ptr))
+}
+
diff --git a/credentials.go b/credentials.go
new file mode 100644
index 0000000..c5ed055
--- /dev/null
+++ b/credentials.go
@@ -0,0 +1,74 @@
+package git
+
+/*
+#include <git2.h>
+#include <git2/errors.h>
+*/
+import "C"
+import "unsafe"
+
+type CredType uint
+
+const (
+ CredTypeUserpassPlaintext CredType = C.GIT_CREDTYPE_USERPASS_PLAINTEXT
+ CredTypeSshKey = C.GIT_CREDTYPE_SSH_KEY
+ CredTypeSshCustom = C.GIT_CREDTYPE_SSH_CUSTOM
+ CredTypeDefault = C.GIT_CREDTYPE_DEFAULT
+)
+
+type Cred struct {
+ ptr *C.git_cred
+}
+
+func (o *Cred) HasUsername() bool {
+ if C.git_cred_has_username(o.ptr) == 1 {
+ return true
+ }
+ return false
+}
+
+func (o *Cred) Type() CredType {
+ return (CredType)(o.ptr.credtype)
+}
+
+func credFromC(ptr *C.git_cred) *Cred {
+ return &Cred{ptr}
+}
+
+func NewCredUserpassPlaintext(username string, password string) (int, Cred) {
+ cred := Cred{}
+ cusername := C.CString(username)
+ defer C.free(unsafe.Pointer(cusername))
+ cpassword := C.CString(password)
+ defer C.free(unsafe.Pointer(cpassword))
+ ret := C.git_cred_userpass_plaintext_new(&cred.ptr, cusername, cpassword)
+ return int(ret), cred
+}
+
+func NewCredSshKey(username string, publickey string, privatekey string, passphrase string) (int, Cred) {
+ cred := Cred{}
+ cusername := C.CString(username)
+ defer C.free(unsafe.Pointer(cusername))
+ cpublickey := C.CString(publickey)
+ defer C.free(unsafe.Pointer(cpublickey))
+ cprivatekey := C.CString(privatekey)
+ defer C.free(unsafe.Pointer(cprivatekey))
+ cpassphrase := C.CString(passphrase)
+ defer C.free(unsafe.Pointer(cpassphrase))
+ ret := C.git_cred_ssh_key_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
+ return int(ret), cred
+}
+
+func NewCredSshKeyFromAgent(username string) (int, Cred) {
+ cred := Cred{}
+ cusername := C.CString(username)
+ defer C.free(unsafe.Pointer(cusername))
+ ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername)
+ return int(ret), cred
+}
+
+func NewCredDefault() (int, Cred) {
+ cred := Cred{}
+ ret := C.git_cred_default_new(&cred.ptr)
+ return int(ret), cred
+}
diff --git a/git.go b/git.go
index 4c798b3..9afa504 100644
--- a/git.go
+++ b/git.go
@@ -8,6 +8,7 @@ package git
import "C"
import (
"bytes"
+ "encoding/hex"
"errors"
"runtime"
"strings"
@@ -29,10 +30,8 @@ func init() {
C.git_threads_init()
}
-// Oid
-type Oid struct {
- bytes [20]byte
-}
+// Oid represents the id for a Git object.
+type Oid [20]byte
func newOidFromC(coid *C.git_oid) *Oid {
if coid == nil {
@@ -40,62 +39,57 @@ 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 {
+func NewOidFromBytes(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) {
- o := new(Oid)
- cs := C.CString(s)
- defer C.free(unsafe.Pointer(cs))
+func NewOid(s string) (*Oid, error) {
+ 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 C.git_oid_fromstr(o.toC(), cs) < 0 {
- return nil, LastError()
+ 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)
-}
-
-func (oid *Oid) Bytes() []byte {
- return oid.bytes[0:]
+ return hex.EncodeToString(oid[:])
}
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 {
- if a != '0' {
+ for _, a := range oid {
+ if a != 0 {
return false
}
}
@@ -103,7 +97,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) {
@@ -124,27 +118,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 {
@@ -168,17 +171,16 @@ 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))
+ var buf C.git_buf
+ defer C.git_buf_free(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- r := C.git_repository_discover(retpath, C.GIT_PATH_MAX, cstart, cbool(across_fs), ceildirs)
-
- if r == 0 {
- return C.GoString(retpath), nil
+ ret := C.git_repository_discover(&buf, cstart, cbool(across_fs), ceildirs)
+ if ret < 0 {
+ return "", MakeGitError(ret)
}
- return "", LastError()
+ return C.GoString(buf.ptr), nil
}
diff --git a/git_test.go b/git_test.go
index 2f586b5..e6372fb 100644
--- a/git_test.go
+++ b/git_test.go
@@ -15,6 +15,17 @@ func createTestRepo(t *testing.T) *Repository {
tmpfile := "README"
err = ioutil.WriteFile(path+"/"+tmpfile, []byte("foo\n"), 0644)
+
+ checkFatal(t, err)
+
+ return repo
+}
+
+func createBareTestRepo(t *testing.T) *Repository {
+ // figure out where we can create the test repo
+ path, err := ioutil.TempDir("", "git2go")
+ checkFatal(t, err)
+ repo, err := InitRepository(path, true)
checkFatal(t, err)
return repo
@@ -73,3 +84,11 @@ func updateReadme(t *testing.T, repo *Repository, content string) (*Oid, *Oid) {
return commitId, treeId
}
+
+func TestOidZero(t *testing.T) {
+ var zeroId Oid
+
+ if !zeroId.IsZero() {
+ t.Error("Zero Oid is not zero")
+ }
+}
diff --git a/index.go b/index.go
index ac5148c..7336249 100644
--- a/index.go
+++ b/index.go
@@ -6,7 +6,9 @@ package git
*/
import "C"
import (
+ "fmt"
"runtime"
+ "time"
"unsafe"
)
@@ -14,6 +16,17 @@ type Index struct {
ptr *C.git_index
}
+type IndexEntry struct {
+ Ctime time.Time
+ Mtime time.Time
+ Mode uint
+ Uid uint
+ Gid uint
+ Size uint
+ Id *Oid
+ Path string
+}
+
func newIndexFromC(ptr *C.git_index) *Index {
idx := &Index{ptr}
runtime.SetFinalizer(idx, (*Index).Free)
@@ -29,12 +42,26 @@ 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
}
+func (v *Index) WriteTreeTo(repo *Repository) (*Oid, error) {
+ oid := new(Oid)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_index_write_tree_to(oid.toC(), v.ptr, repo.ptr)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ return oid, nil
+}
+
func (v *Index) WriteTree() (*Oid, error) {
oid := new(Oid)
@@ -43,13 +70,50 @@ 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
}
+func (v *Index) Write() error {
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_index_write(v.ptr)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+
+ return nil
+}
+
func (v *Index) Free() {
runtime.SetFinalizer(v, nil)
C.git_index_free(v.ptr)
}
+
+func (v *Index) EntryCount() uint {
+ return uint(C.git_index_entrycount(v.ptr))
+}
+
+func newIndexEntryFromC(entry *C.git_index_entry) *IndexEntry {
+ return &IndexEntry{
+ time.Unix(int64(entry.ctime.seconds), int64(entry.ctime.nanoseconds)),
+ time.Unix(int64(entry.mtime.seconds), int64(entry.mtime.nanoseconds)),
+ uint(entry.mode),
+ uint(entry.uid),
+ uint(entry.gid),
+ uint(entry.file_size),
+ newOidFromC(&entry.id),
+ C.GoString(entry.path),
+ }
+}
+
+func (v *Index) EntryByIndex(index uint) (*IndexEntry, error) {
+ centry := C.git_index_get_byindex(v.ptr, C.size_t(index))
+ if centry == nil {
+ return nil, fmt.Errorf("Index out of Bounds")
+ }
+ return newIndexEntryFromC(centry), nil
+}
diff --git a/index_test.go b/index_test.go
index 9828d0f..5920b93 100644
--- a/index_test.go
+++ b/index_test.go
@@ -22,6 +22,25 @@ func TestCreateRepoAndStage(t *testing.T) {
}
}
+func TestIndexWriteTreeTo(t *testing.T) {
+ repo := createTestRepo(t)
+ defer os.RemoveAll(repo.Workdir())
+
+ repo2 := createTestRepo(t)
+ defer os.RemoveAll(repo.Workdir())
+
+ idx, err := repo.Index()
+ checkFatal(t, err)
+ err = idx.AddByPath("README")
+ checkFatal(t, err)
+ treeId, err := idx.WriteTreeTo(repo2)
+ checkFatal(t, err)
+
+ if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
+ t.Fatalf("%v", treeId.String())
+ }
+}
+
func checkFatal(t *testing.T, err error) {
if err == nil {
return
diff --git a/odb.go b/odb.go
index 638ef74..daf63dd 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)
@@ -79,13 +79,29 @@ func (v *Odb) ForEach() chan *Oid {
return ch
}
+// Hash determines the object-ID (sha1) of a data buffer.
+func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) {
+ oid = new(Oid)
+ header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
+ ptr := unsafe.Pointer(header.Data)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_odb_hash(oid.toC(), ptr, C.size_t(header.Len), C.git_otype(otype));
+ if ret < 0 {
+ err = MakeGitError(ret)
+ }
+ return
+}
+
// NewReadStream opens a read stream from the ODB. Reading from it will give you the
// contents of the object.
func (v *Odb) NewReadStream(id *Oid) (*OdbReadStream, error) {
stream := new(OdbReadStream)
ret := C.git_odb_open_rstream(&stream.ptr, v.ptr, id.toC())
if ret < 0 {
- return nil, LastError()
+ return nil, MakeGitError(ret)
}
runtime.SetFinalizer(stream, (*OdbReadStream).Free)
@@ -99,7 +115,7 @@ func (v *Odb) NewWriteStream(size int, otype ObjectType) (*OdbWriteStream, error
stream := new(OdbWriteStream)
ret := C.git_odb_open_wstream(&stream.ptr, v.ptr, C.size_t(size), C.git_otype(otype))
if ret < 0 {
- return nil, LastError()
+ return nil, MakeGitError(ret)
}
runtime.SetFinalizer(stream, (*OdbWriteStream).Free)
@@ -148,7 +164,7 @@ func (stream *OdbReadStream) Read(data []byte) (int, error) {
size := C.size_t(header.Cap)
ret := C.git_odb_stream_read(stream.ptr, ptr, size)
if ret < 0 {
- return 0, LastError()
+ return 0, MakeGitError(ret)
}
header.Len = int(ret)
@@ -180,7 +196,7 @@ func (stream *OdbWriteStream) Write(data []byte) (int, error) {
ret := C.git_odb_stream_write(stream.ptr, ptr, size)
if ret < 0 {
- return 0, LastError()
+ return 0, MakeGitError(ret)
}
return len(data), nil
@@ -191,7 +207,7 @@ func (stream *OdbWriteStream) Write(data []byte) (int, error) {
func (stream *OdbWriteStream) Close() error {
ret := C.git_odb_stream_finalize_write(stream.Id.toC(), stream.ptr)
if ret < 0 {
- return LastError()
+ return MakeGitError(ret)
}
return nil
diff --git a/odb_test.go b/odb_test.go
index 3c7624c..17b3ad2 100644
--- a/odb_test.go
+++ b/odb_test.go
@@ -27,9 +27,36 @@ 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")
}
-} \ No newline at end of file
+}
+
+func TestOdbHash(t *testing.T) {
+
+ repo := createTestRepo(t)
+ defer os.RemoveAll(repo.Workdir())
+ _, _ = seedTestRepo(t, repo)
+
+ odb, error := repo.Odb()
+ checkFatal(t, error)
+
+ str := `tree 115fcae49287c82eb55bb275cbbd4556fbed72b7
+parent 66e1c476199ebcd3e304659992233132c5a52c6c
+author John Doe <[email protected]> 1390682018 +0000
+committer John Doe <[email protected]> 1390682018 +0000
+
+Initial commit.`;
+
+ oid, error := odb.Hash([]byte(str), ObjectCommit)
+ checkFatal(t, error)
+
+ coid, error := odb.Write([]byte(str), ObjectCommit)
+ checkFatal(t, error)
+
+ if oid.Cmp(coid) != 0 {
+ t.Fatal("Hash and write Oids are different")
+ }
+}
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/push.go b/push.go
new file mode 100644
index 0000000..5fb7f07
--- /dev/null
+++ b/push.go
@@ -0,0 +1,182 @@
+package git
+
+/*
+#include <git2.h>
+#include <git2/errors.h>
+
+int _go_git_push_status_foreach(git_push *push, void *data);
+int _go_git_push_set_callbacks(git_push *push, void *packbuilder_progress_data, void *transfer_progress_data);
+
+*/
+import "C"
+import (
+ "runtime"
+ "unsafe"
+)
+
+type Push struct {
+ ptr *C.git_push
+
+ packbuilderProgress *PackbuilderProgressCallback
+ transferProgress *PushTransferProgressCallback
+}
+
+func newPushFromC(cpush *C.git_push) *Push {
+ p := &Push{ptr: cpush}
+ runtime.SetFinalizer(p, (*Push).Free)
+ return p
+}
+
+func (p *Push) Free() {
+ runtime.SetFinalizer(p, nil)
+ C.git_push_free(p.ptr)
+}
+
+func (remote *Remote) NewPush() (*Push, error) {
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ var cpush *C.git_push
+ ret := C.git_push_new(&cpush, remote.ptr)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ return newPushFromC(cpush), nil
+}
+
+func (p *Push) Finish() error {
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_push_finish(p.ptr)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (p *Push) UnpackOk() bool {
+
+ ret := C.git_push_unpack_ok(p.ptr)
+ if ret == 0 {
+ return false
+ }
+ return true
+
+}
+
+func (p *Push) UpdateTips(sig *Signature, msg string) error {
+
+ var csig *C.git_signature = nil
+ if sig != nil {
+ csig = sig.toC()
+ defer C.free(unsafe.Pointer(csig))
+ }
+
+ 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_push_update_tips(p.ptr, csig, cmsg)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (p *Push) AddRefspec(refspec string) error {
+
+ crefspec := C.CString(refspec)
+ defer C.free(unsafe.Pointer(crefspec))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_push_add_refspec(p.ptr, crefspec)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+type PushOptions struct {
+ Version uint
+ PbParallelism uint
+}
+
+func (p *Push) SetOptions(opts PushOptions) error {
+ copts := C.git_push_options{version: C.uint(opts.Version), pb_parallelism: C.uint(opts.PbParallelism)}
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_push_set_options(p.ptr, &copts)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+type StatusForeachFunc func(ref string, msg string) int
+
+//export statusForeach
+func statusForeach(_ref *C.char, _msg *C.char, _data unsafe.Pointer) C.int {
+ ref := C.GoString(_ref)
+ msg := C.GoString(_msg)
+
+ cb := (*StatusForeachFunc)(_data)
+
+ return C.int((*cb)(ref, msg))
+}
+
+func (p *Push) StatusForeach(callback StatusForeachFunc) error {
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C._go_git_push_status_foreach(p.ptr, unsafe.Pointer(&callback))
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+
+}
+
+type PushCallbacks struct {
+ PackbuilderProgress *PackbuilderProgressCallback
+ TransferProgress *PushTransferProgressCallback
+}
+
+type PackbuilderProgressCallback func(stage int, current uint, total uint) int
+type PushTransferProgressCallback func(current uint, total uint, bytes uint) int
+
+//export packbuilderProgress
+func packbuilderProgress(stage C.int, current C.uint, total C.uint, data unsafe.Pointer) C.int {
+ return C.int((*(*PackbuilderProgressCallback)(data))(int(stage), uint(current), uint(total)))
+}
+
+//export pushTransferProgress
+func pushTransferProgress(current C.uint, total C.uint, bytes C.size_t, data unsafe.Pointer) C.int {
+ return C.int((*(*PushTransferProgressCallback)(data))(uint(current), uint(total), uint(bytes)))
+}
+
+func (p *Push) SetCallbacks(callbacks PushCallbacks) {
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ // save callbacks so they don't get GC'd
+ p.packbuilderProgress = callbacks.PackbuilderProgress
+ p.transferProgress = callbacks.TransferProgress
+
+ C._go_git_push_set_callbacks(p.ptr, unsafe.Pointer(p.packbuilderProgress), unsafe.Pointer(p.transferProgress))
+}
diff --git a/push_test.go b/push_test.go
new file mode 100644
index 0000000..c1e6a22
--- /dev/null
+++ b/push_test.go
@@ -0,0 +1,59 @@
+package git
+
+import (
+ "log"
+ "os"
+ "testing"
+ "time"
+)
+
+func Test_Push_ToRemote(t *testing.T) {
+ repo := createBareTestRepo(t)
+ defer os.RemoveAll(repo.Path())
+ repo2 := createTestRepo(t)
+ defer os.RemoveAll(repo2.Workdir())
+
+ remote, err := repo2.CreateRemote("test_push", repo.Path())
+ checkFatal(t, err)
+
+ index, err := repo2.Index()
+ checkFatal(t, err)
+
+ index.AddByPath("README")
+
+ err = index.Write()
+ checkFatal(t, err)
+
+ newTreeId, err := index.WriteTree()
+ checkFatal(t, err)
+
+ tree, err := repo2.LookupTree(newTreeId)
+ checkFatal(t, err)
+
+ sig := &Signature{Name: "Rand Om Hacker", Email: "[email protected]", When: time.Now()}
+ // this should cause master branch to be created if it does not already exist
+ _, err = repo2.CreateCommit("HEAD", sig, sig, "message", tree)
+ checkFatal(t, err)
+
+ push, err := remote.NewPush()
+ checkFatal(t, err)
+
+ err = push.AddRefspec("refs/heads/master")
+ checkFatal(t, err)
+
+ err = push.Finish()
+ checkFatal(t, err)
+
+ err = push.StatusForeach(func(ref string, msg string) int {
+ log.Printf("%s -> %s", ref, msg)
+ return 0
+ })
+ checkFatal(t, err)
+
+ if !push.UnpackOk() {
+ t.Fatalf("unable to unpack")
+ }
+
+ defer remote.Free()
+ defer repo.Free()
+}
diff --git a/reference.go b/reference.go
index 8e33354..d246c55 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,31 +28,54 @@ 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))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget)
+ csig := sig.toC()
+ defer C.free(unsafe.Pointer(csig))
+
+ var cmsg *C.char
+ if msg == "" {
+ cmsg = nil
+ } else {
+ 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()
+ return nil, MakeGitError(ret)
}
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
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_reference_set_target(&ptr, v.ptr, target.toC())
+ csig := sig.toC()
+ defer C.free(unsafe.Pointer(csig))
+
+ var cmsg *C.char
+ if msg == "" {
+ cmsg = nil
+ } else {
+ 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()
+ return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr), nil
@@ -65,24 +89,35 @@ 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
}
-func (v *Reference) Rename(name string, force bool) (*Reference, error) {
+func (v *Reference) Rename(name string, force bool, sig *Signature, msg string) (*Reference, error) {
var ptr *C.git_reference
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
+ csig := sig.toC()
+ defer C.free(unsafe.Pointer(csig))
+
+ 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_reference_rename(&ptr, v.ptr, cname, cbool(force))
+ 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
@@ -108,12 +143,23 @@ func (v *Reference) Delete() error {
ret := C.git_reference_delete(v.ptr)
if ret < 0 {
- return LastError()
+ return MakeGitError(ret)
}
return nil
}
+// Cmp compares both references, retursn 0 on equality, otherwise a
+// stable sorting.
+func (v *Reference) Cmp(ref2 *Reference) int {
+ return int(C.git_reference_cmp(v.ptr, ref2.ptr))
+}
+
+// Shorthand returns a "human-readable" short reference name
+func (v *Reference) Shorthand() string {
+ return C.GoString(C.git_reference_shorthand(v.ptr))
+}
+
func (v *Reference) Name() string {
return C.GoString(C.git_reference_name(v.ptr))
}
@@ -122,6 +168,18 @@ func (v *Reference) Type() ReferenceType {
return ReferenceType(C.git_reference_type(v.ptr))
}
+func (v *Reference) IsBranch() bool {
+ return C.git_reference_is_branch(v.ptr) == 1
+}
+
+func (v *Reference) IsRemote() bool {
+ return C.git_reference_is_remote(v.ptr) == 1
+}
+
+func (v *Reference) IsTag() bool {
+ return C.git_reference_is_tag(v.ptr) == 1
+}
+
func (v *Reference) Free() {
runtime.SetFinalizer(v, nil)
C.git_reference_free(v.ptr)
@@ -141,7 +199,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}
@@ -162,7 +220,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}
@@ -183,7 +241,7 @@ func (v *ReferenceIterator) NextName() (string, error) {
return "", ErrIterOver
}
if ret < 0 {
- return "", LastError()
+ return "", MakeGitError(ret)
}
return C.GoString(ptr), nil
@@ -215,7 +273,7 @@ func (v *ReferenceIterator) Next() (*Reference, error) {
return nil, ErrIterOver
}
if ret < 0 {
- return nil, LastError()
+ return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr), nil
diff --git a/reference_test.go b/reference_test.go
index f955a2c..ffa9f35 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: "[email protected]",
+ 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")
@@ -45,7 +52,7 @@ func TestRefModification(t *testing.T) {
t.Fatalf("Wrong ref target")
}
- _, err = tag.Rename("refs/tags/renamed", false)
+ _, err = tag.Rename("refs/tags/renamed", false, nil, "")
checkFatal(t, err)
tag, err = repo.LookupReference("refs/tags/renamed")
checkFatal(t, err)
@@ -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()
@@ -154,6 +159,33 @@ func TestIterator(t *testing.T) {
compareStringList(t, expected, list)
}
+func TestUtil(t *testing.T) {
+ repo := createTestRepo(t)
+ defer os.RemoveAll(repo.Workdir())
+
+ commitId, _ := seedTestRepo(t, repo)
+
+ ref, err := repo.CreateReference("refs/heads/foo", commitId, true, nil, "")
+ checkFatal(t, err)
+
+ ref2, err := repo.DwimReference("foo")
+ checkFatal(t, err)
+
+ if ref.Cmp(ref2) != 0 {
+ t.Fatalf("foo didn't dwim to the right thing")
+ }
+
+ if ref.Shorthand() != "foo" {
+ t.Fatalf("refs/heads/foo has no foo shorthand")
+ }
+
+ hasLog, err := repo.HasLog("refs/heads/foo")
+ checkFatal(t, err)
+ if !hasLog {
+ t.Fatalf("branches ahve logs by default")
+ }
+}
+
func compareStringList(t *testing.T, expected, actual []string) {
for i, v := range expected {
if actual[i] != v {
diff --git a/remote.go b/remote.go
new file mode 100644
index 0000000..66097b8
--- /dev/null
+++ b/remote.go
@@ -0,0 +1,436 @@
+package git
+
+/*
+#include <git2.h>
+#include <git2/errors.h>
+
+extern void _go_git_setup_callbacks(git_remote_callbacks *callbacks);
+
+*/
+import "C"
+import "unsafe"
+import "runtime"
+
+type TransferProgress struct {
+ TotalObjects uint
+ IndexedObjects uint
+ ReceivedObjects uint
+ LocalObjects uint
+ TotalDeltas uint
+ ReceivedBytes uint
+}
+
+func newTransferProgressFromC(c *C.git_transfer_progress) TransferProgress {
+ return TransferProgress{
+ TotalObjects: uint(c.total_objects),
+ IndexedObjects: uint(c.indexed_objects),
+ ReceivedObjects: uint(c.received_objects),
+ LocalObjects: uint(c.local_objects),
+ TotalDeltas: uint(c.total_deltas),
+ ReceivedBytes: uint(c.received_bytes)}
+}
+
+type RemoteCompletion uint
+
+const (
+ RemoteCompletionDownload RemoteCompletion = C.GIT_REMOTE_COMPLETION_DOWNLOAD
+ RemoteCompletionIndexing = C.GIT_REMOTE_COMPLETION_INDEXING
+ RemoteCompletionError = C.GIT_REMOTE_COMPLETION_ERROR
+)
+
+type ProgressCallback func(str string) int
+type CompletionCallback func(RemoteCompletion) int
+type CredentialsCallback func(url string, username_from_url string, allowed_types CredType) (int, *Cred)
+type TransferProgressCallback func(stats TransferProgress) int
+type UpdateTipsCallback func(refname string, a *Oid, b *Oid) int
+
+type RemoteCallbacks struct {
+ ProgressCallback
+ CompletionCallback
+ CredentialsCallback
+ TransferProgressCallback
+ UpdateTipsCallback
+}
+
+type Remote struct {
+ ptr *C.git_remote
+}
+
+func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) {
+ C.git_remote_init_callbacks(ptr, C.GIT_REMOTE_CALLBACKS_VERSION)
+ if callbacks == nil {
+ return
+ }
+ C._go_git_setup_callbacks(ptr)
+ ptr.payload = unsafe.Pointer(callbacks)
+}
+
+//export progressCallback
+func progressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int {
+ callbacks := (*RemoteCallbacks)(data)
+ if callbacks.ProgressCallback == nil {
+ return 0
+ }
+ str := C.GoStringN(_str, _len)
+ return callbacks.ProgressCallback(str)
+}
+
+//export completionCallback
+func completionCallback(completion_type C.git_remote_completion_type, data unsafe.Pointer) int {
+ callbacks := (*RemoteCallbacks)(data)
+ if callbacks.CompletionCallback == nil {
+ return 0
+ }
+ return callbacks.CompletionCallback((RemoteCompletion)(completion_type))
+}
+
+//export credentialsCallback
+func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C.char, allowed_types uint, data unsafe.Pointer) int {
+ callbacks := (*RemoteCallbacks)(data)
+ if callbacks.CredentialsCallback == nil {
+ return 0
+ }
+ url := C.GoString(_url)
+ username_from_url := C.GoString(_username_from_url)
+ ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types))
+ *_cred = cred.ptr
+ return ret
+}
+
+//export transferProgressCallback
+func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int {
+ callbacks := (*RemoteCallbacks)(data)
+ if callbacks.TransferProgressCallback == nil {
+ return 0
+ }
+ return callbacks.TransferProgressCallback(newTransferProgressFromC(stats))
+}
+
+//export updateTipsCallback
+func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data unsafe.Pointer) int {
+ callbacks := (*RemoteCallbacks)(data)
+ if callbacks.UpdateTipsCallback == nil {
+ return 0
+ }
+ refname := C.GoString(_refname)
+ a := newOidFromC(_a)
+ b := newOidFromC(_b)
+ return callbacks.UpdateTipsCallback(refname, a, b)
+}
+
+func RemoteIsValidName(name string) bool {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+ if C.git_remote_is_valid_name(cname) == 1 {
+ return true
+ }
+ return false
+}
+
+func (r *Remote) Free() {
+ runtime.SetFinalizer(r, nil)
+ C.git_remote_free(r.ptr)
+}
+
+func (repo *Repository) ListRemotes() ([]string, error) {
+ var r C.git_strarray
+ ecode := C.git_remote_list(&r, repo.ptr)
+ if ecode < 0 {
+ return nil, MakeGitError(ecode)
+ }
+ defer C.git_strarray_free(&r)
+
+ remotes := makeStringsFromCStrings(r.strings, int(r.count))
+ return remotes, nil
+}
+
+func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) {
+ remote := &Remote{}
+
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+ curl := C.CString(url)
+ defer C.free(unsafe.Pointer(curl))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_create(&remote.ptr, repo.ptr, cname, curl)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ runtime.SetFinalizer(remote, (*Remote).Free)
+ return remote, nil
+}
+
+func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch string) (*Remote, error) {
+ remote := &Remote{}
+
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+ curl := C.CString(url)
+ defer C.free(unsafe.Pointer(curl))
+ cfetch := C.CString(fetch)
+ defer C.free(unsafe.Pointer(cfetch))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_create_with_fetchspec(&remote.ptr, repo.ptr, cname, curl, cfetch)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ runtime.SetFinalizer(remote, (*Remote).Free)
+ return remote, nil
+}
+
+func (repo *Repository) CreateRemoteInMemory(fetch string, url string) (*Remote, error) {
+ remote := &Remote{}
+
+ curl := C.CString(url)
+ defer C.free(unsafe.Pointer(curl))
+ cfetch := C.CString(fetch)
+ defer C.free(unsafe.Pointer(cfetch))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_create_inmemory(&remote.ptr, repo.ptr, cfetch, curl)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ runtime.SetFinalizer(remote, (*Remote).Free)
+ return remote, nil
+}
+
+func (repo *Repository) LoadRemote(name string) (*Remote, error) {
+ remote := &Remote{}
+
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_load(&remote.ptr, repo.ptr, cname)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ runtime.SetFinalizer(remote, (*Remote).Free)
+ return remote, nil
+}
+
+func (o *Remote) Save() error {
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_save(o.ptr)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (o *Remote) Owner() Repository {
+ return Repository{C.git_remote_owner(o.ptr)}
+}
+
+func (o *Remote) Name() string {
+ return C.GoString(C.git_remote_name(o.ptr))
+}
+
+func (o *Remote) Url() string {
+ return C.GoString(C.git_remote_url(o.ptr))
+}
+
+func (o *Remote) PushUrl() string {
+ return C.GoString(C.git_remote_pushurl(o.ptr))
+}
+
+func (o *Remote) SetUrl(url string) error {
+ curl := C.CString(url)
+ defer C.free(unsafe.Pointer(curl))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_set_url(o.ptr, curl)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (o *Remote) SetPushUrl(url string) error {
+ curl := C.CString(url)
+ defer C.free(unsafe.Pointer(curl))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_set_pushurl(o.ptr, curl)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (o *Remote) AddFetch(refspec string) error {
+ crefspec := C.CString(refspec)
+ defer C.free(unsafe.Pointer(crefspec))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_add_fetch(o.ptr, crefspec)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func sptr(p uintptr) *C.char {
+ return *(**C.char)(unsafe.Pointer(p))
+}
+
+func makeStringsFromCStrings(x **C.char, l int) []string {
+ s := make([]string, l)
+ i := 0
+ for p := uintptr(unsafe.Pointer(x)); i < l; p += unsafe.Sizeof(uintptr(0)) {
+ s[i] = C.GoString(sptr(p))
+ i++
+ }
+ return s
+}
+
+func makeCStringsFromStrings(s []string) **C.char {
+ l := len(s)
+ x := (**C.char)(C.malloc(C.size_t(unsafe.Sizeof(unsafe.Pointer(nil)) * uintptr(l))))
+ i := 0
+ for p := uintptr(unsafe.Pointer(x)); i < l; p += unsafe.Sizeof(uintptr(0)) {
+ *(**C.char)(unsafe.Pointer(p)) = C.CString(s[i])
+ i++
+ }
+ return x
+}
+
+func freeStrarray(arr *C.git_strarray) {
+ count := int(arr.count)
+ size := unsafe.Sizeof(unsafe.Pointer(nil))
+
+ i := 0
+ for p := uintptr(unsafe.Pointer(arr.strings)); i < count; p += size {
+ C.free(unsafe.Pointer(sptr(p)))
+ i++
+ }
+
+ C.free(unsafe.Pointer(arr.strings))
+}
+
+func (o *Remote) FetchRefspecs() ([]string, error) {
+ crefspecs := C.git_strarray{}
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_get_fetch_refspecs(&crefspecs, o.ptr)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ defer C.git_strarray_free(&crefspecs)
+
+ refspecs := makeStringsFromCStrings(crefspecs.strings, int(crefspecs.count))
+ return refspecs, nil
+}
+
+func (o *Remote) SetFetchRefspecs(refspecs []string) error {
+ crefspecs := C.git_strarray{}
+ crefspecs.count = C.size_t(len(refspecs))
+ crefspecs.strings = makeCStringsFromStrings(refspecs)
+ defer freeStrarray(&crefspecs)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_set_fetch_refspecs(o.ptr, &crefspecs)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (o *Remote) AddPush(refspec string) error {
+ crefspec := C.CString(refspec)
+ defer C.free(unsafe.Pointer(crefspec))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_add_push(o.ptr, crefspec)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (o *Remote) PushRefspecs() ([]string, error) {
+ crefspecs := C.git_strarray{}
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_get_push_refspecs(&crefspecs, o.ptr)
+ if ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+ defer C.git_strarray_free(&crefspecs)
+ refspecs := makeStringsFromCStrings(crefspecs.strings, int(crefspecs.count))
+ return refspecs, nil
+}
+
+func (o *Remote) SetPushRefspecs(refspecs []string) error {
+ crefspecs := C.git_strarray{}
+ crefspecs.count = C.size_t(len(refspecs))
+ crefspecs.strings = makeCStringsFromStrings(refspecs)
+ defer freeStrarray(&crefspecs)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_remote_set_push_refspecs(o.ptr, &crefspecs)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
+
+func (o *Remote) ClearRefspecs() {
+ C.git_remote_clear_refspecs(o.ptr)
+}
+
+func (o *Remote) RefspecCount() uint {
+ return uint(C.git_remote_refspec_count(o.ptr))
+}
+
+func (o *Remote) Fetch(sig *Signature, msg string) error {
+
+ var csig *C.git_signature = nil
+ if sig != nil {
+ csig = sig.toC()
+ defer C.free(unsafe.Pointer(csig))
+ }
+
+ var cmsg *C.char
+ if msg == "" {
+ cmsg = nil
+ } else {
+ cmsg = C.CString(msg)
+ defer C.free(unsafe.Pointer(cmsg))
+ }
+ ret := C.git_remote_fetch(o.ptr, csig, cmsg)
+ if ret < 0 {
+ return MakeGitError(ret)
+ }
+ return nil
+}
diff --git a/remote_test.go b/remote_test.go
new file mode 100644
index 0000000..90e24ae
--- /dev/null
+++ b/remote_test.go
@@ -0,0 +1,47 @@
+package git
+
+import (
+ "os"
+ "testing"
+)
+
+func TestRefspecs(t *testing.T) {
+ repo := createTestRepo(t)
+ defer os.RemoveAll(repo.Workdir())
+ defer repo.Free()
+
+ remote, err := repo.CreateRemoteInMemory("refs/heads/*:refs/heads/*", "git://foo/bar")
+ checkFatal(t, err)
+
+ expected := []string{
+ "refs/heads/*:refs/remotes/origin/*",
+ "refs/pull/*/head:refs/remotes/origin/*",
+ }
+
+ err = remote.SetFetchRefspecs(expected)
+ checkFatal(t, err)
+
+ actual, err := remote.FetchRefspecs()
+ checkFatal(t, err)
+
+ compareStringList(t, expected, actual)
+}
+
+func TestListRemotes(t *testing.T) {
+ repo := createTestRepo(t)
+ defer os.RemoveAll(repo.Workdir())
+ defer repo.Free()
+
+ _, err := repo.CreateRemote("test", "git://foo/bar")
+
+ checkFatal(t, err)
+
+ expected := []string{
+ "test",
+ }
+
+ actual, err := repo.ListRemotes()
+ checkFatal(t, err)
+
+ compareStringList(t, expected, actual)
+}
diff --git a/repository.go b/repository.go
index 8bb061b..50053d9 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,32 +79,32 @@ 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
}
-func (v *Repository) lookupType(oid *Oid, t ObjectType) (Object, error) {
+func (v *Repository) lookupType(id *Oid, t ObjectType) (Object, error) {
var ptr *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_object_lookup(&ptr, v.ptr, oid.toC(), C.git_otype(t))
+ 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
}
-func (v *Repository) Lookup(oid *Oid) (Object, error) {
- return v.lookupType(oid, ObjectAny)
+func (v *Repository) Lookup(id *Oid) (Object, error) {
+ return v.lookupType(id, ObjectAny)
}
-func (v *Repository) LookupTree(oid *Oid) (*Tree, error) {
- obj, err := v.lookupType(oid, ObjectTree)
+func (v *Repository) LookupTree(id *Oid) (*Tree, error) {
+ obj, err := v.lookupType(id, ObjectTree)
if err != nil {
return nil, err
}
@@ -112,8 +112,8 @@ func (v *Repository) LookupTree(oid *Oid) (*Tree, error) {
return obj.(*Tree), nil
}
-func (v *Repository) LookupCommit(oid *Oid) (*Commit, error) {
- obj, err := v.lookupType(oid, ObjectCommit)
+func (v *Repository) LookupCommit(id *Oid) (*Commit, error) {
+ obj, err := v.lookupType(id, ObjectCommit)
if err != nil {
return nil, err
}
@@ -121,8 +121,8 @@ func (v *Repository) LookupCommit(oid *Oid) (*Commit, error) {
return obj.(*Commit), nil
}
-func (v *Repository) LookupBlob(oid *Oid) (*Blob, error) {
- obj, err := v.lookupType(oid, ObjectBlob)
+func (v *Repository) LookupBlob(id *Oid) (*Blob, error) {
+ obj, err := v.lookupType(id, ObjectBlob)
if err != nil {
return nil, err
}
@@ -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
@@ -160,54 +160,78 @@ func (v *Repository) Head() (*Reference, error) {
return newReferenceFromC(ptr), nil
}
-func (v *Repository) CreateReference(name string, oid *Oid, force bool) (*Reference, error) {
+func (v *Repository) CreateReference(name string, id *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))
+
+ var cmsg *C.char
+ if msg == "" {
+ cmsg = nil
+ } else {
+ cmsg = C.CString(msg)
+ defer C.free(unsafe.Pointer(cmsg))
+ }
+
var ptr *C.git_reference
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ecode := C.git_reference_create(&ptr, v.ptr, cname, oid.toC(), cbool(force))
+ 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
}
-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))
+
+ var cmsg *C.char
+ if msg == "" {
+ cmsg = nil
+ } else {
+ cmsg = C.CString(msg)
+ defer C.free(unsafe.Pointer(cmsg))
+ }
+
var ptr *C.git_reference
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- 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()
+ return nil, MakeGitError(ecode)
}
return newReferenceFromC(ptr), nil
}
func (v *Repository) Walk() (*RevWalk, error) {
- walk := new(RevWalk)
+
+ var walkPtr *C.git_revwalk
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ecode := C.git_revwalk_new(&walk.ptr, v.ptr)
+ ecode := C.git_revwalk_new(&walkPtr, v.ptr)
if ecode < 0 {
- return nil, LastError()
+ return nil, MakeGitError(ecode)
}
- walk.repo = v
- runtime.SetFinalizer(walk, freeRevWalk)
- return walk, nil
+ return revWalkFromC(v, walkPtr), nil
}
func (v *Repository) CreateCommit(
@@ -246,10 +270,10 @@ func (v *Repository) CreateCommit(
ret := C.git_commit_create(
oid.toC(), v.ptr, cref,
authorSig, committerSig,
- nil, cmsg, tree.ptr, C.int(nparents), parentsarg)
+ nil, cmsg, tree.ptr, C.size_t(nparents), parentsarg)
if ret < 0 {
- return nil, LastError()
+ return nil, MakeGitError(ret)
}
return oid, nil
@@ -267,7 +291,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)
@@ -293,9 +317,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
}
@@ -306,7 +331,22 @@ 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)
+
+ bld.repo = v
+ return bld, nil
+}
+
+func (v *Repository) TreeBuilderFromTree(tree *Tree) (*TreeBuilder, error) {
+ bld := new(TreeBuilder)
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ if ret := C.git_treebuilder_create(&bld.ptr, tree.ptr); ret < 0 {
+ return nil, MakeGitError(ret)
}
runtime.SetFinalizer(bld, (*TreeBuilder).Free)
@@ -325,8 +365,57 @@ 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
}
+
+// EnsureLog ensures that there is a reflog for the given reference
+// name and creates an empty one if necessary.
+func (v *Repository) EnsureLog(name string) error {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ if ret := C.git_reference_ensure_log(v.ptr, cname); ret < 0 {
+ return MakeGitError(ret)
+ }
+
+ return nil
+}
+
+// HasLog returns whether there is a reflog for the given reference
+// name
+func (v *Repository) HasLog(name string) (bool, error) {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ret := C.git_reference_has_log(v.ptr, cname)
+ if ret < 0 {
+ return false, MakeGitError(ret)
+ }
+
+ return ret == 1, nil
+}
+
+// DwimReference looks up a reference by DWIMing its short name
+func (v *Repository) DwimReference(name string) (*Reference, error) {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ var ptr *C.git_reference
+ if ret := C.git_reference_dwim(&ptr, v.ptr, cname); ret < 0 {
+ return nil, MakeGitError(ret)
+ }
+
+ return newReferenceFromC(ptr), nil
+}
diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh
new file mode 100755
index 0000000..aa43df5
--- /dev/null
+++ b/script/build-libgit2.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+set -ex
+
+git clone --depth 1 --single-branch git://github.com/libgit2/libgit2 libgit2
+
+cd libgit2
+cmake -DTHREADSAFE=ON \
+ -DBUILD_CLAR=OFF \
+ -DCMAKE_INSTALL_PREFIX=$PWD/install \
+ .
+
+make install
+
+# Let the Go build system know where to find libgit2
+export LD_LIBRARY_PATH="$TMPDIR/libgit2/install/lib"
+export PKG_CONFIG_PATH="$TMPDIR/libgit2/install/lib/pkgconfig"
diff --git a/submodule.go b/submodule.go
index 48ea151..a94afd4 100644
--- a/submodule.go
+++ b/submodule.go
@@ -56,6 +56,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)
@@ -73,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
@@ -94,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
}
@@ -112,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
}
@@ -123,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
}
@@ -134,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
}
@@ -145,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
}
@@ -180,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
}
@@ -229,20 +237,17 @@ func (sub *Submodule) SetUpdate(update SubmoduleUpdate) SubmoduleUpdate {
return SubmoduleUpdate(o)
}
-func (sub *Submodule) FetchRecurseSubmodules() bool {
- if 0 == C.git_submodule_fetch_recurse_submodules(sub.ptr) {
- return false
- }
- return true
+func (sub *Submodule) FetchRecurseSubmodules() SubmoduleRecurse {
+ return SubmoduleRecurse(C.git_submodule_fetch_recurse_submodules(sub.ptr))
}
-func (sub *Submodule) SetFetchRecurseSubmodules(v bool) error {
+func (sub *Submodule) SetFetchRecurseSubmodules(recurse SubmoduleRecurse) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, cbool(v))
+ ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, C.git_submodule_recurse_t(recurse))
if ret < 0 {
- return LastError()
+ return MakeGitError(C.int(ret))
}
return nil
}
@@ -253,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
}
@@ -264,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
}
@@ -277,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
}
@@ -288,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
}
@@ -299,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..7070ac7 100644
--- a/tree.go
+++ b/tree.go
@@ -14,13 +14,14 @@ import (
)
type Filemode int
+
const (
- FilemodeNew Filemode = C.GIT_FILEMODE_NEW
- FilemodeTree = C.GIT_FILEMODE_TREE
- FilemodeBlob = C.GIT_FILEMODE_BLOB
- FilemodeBlobExecutable = C.GIT_FILEMODE_BLOB_EXECUTABLE
- FilemodeLink = C.GIT_FILEMODE_LINK
- FilemodeCommit = C.GIT_FILEMODE_COMMIT
+ FilemodeNew Filemode = C.GIT_FILEMODE_NEW
+ FilemodeTree = C.GIT_FILEMODE_TREE
+ FilemodeBlob = C.GIT_FILEMODE_BLOB
+ FilemodeBlobExecutable = C.GIT_FILEMODE_BLOB_EXECUTABLE
+ FilemodeLink = C.GIT_FILEMODE_LINK
+ FilemodeCommit = C.GIT_FILEMODE_COMMIT
)
type Tree struct {
@@ -28,9 +29,9 @@ type Tree struct {
}
type TreeEntry struct {
- Name string
- Id *Oid
- Type ObjectType
+ Name string
+ Id *Oid
+ Type ObjectType
Filemode int
}
@@ -67,7 +68,7 @@ func (t Tree) EntryByPath(path string) (*TreeEntry, error) {
ret := C.git_tree_entry_bypath(&entry, t.ptr, cpath)
if ret < 0 {
- return nil, LastError()
+ return nil, MakeGitError(ret)
}
return newTreeEntry(entry), nil
@@ -109,14 +110,14 @@ func (t Tree) Walk(callback TreeWalkCallback) error {
)
if err < 0 {
- return LastError()
+ return MakeGitError(err)
}
return nil
}
type TreeBuilder struct {
- ptr *C.git_treebuilder
+ ptr *C.git_treebuilder
repo *Repository
}
@@ -125,7 +126,7 @@ func (v *TreeBuilder) Free() {
C.git_treebuilder_free(v.ptr)
}
-func (v *TreeBuilder) Insert(filename string, id *Oid, filemode int) (error) {
+func (v *TreeBuilder) Insert(filename string, id *Oid, filemode int) error {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
@@ -134,7 +135,22 @@ 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
+}
+
+func (v *TreeBuilder) Remove(filename string) error {
+ cfilename := C.CString(filename)
+ defer C.free(unsafe.Pointer(cfilename))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ err := C.git_treebuilder_remove(v.ptr, cfilename)
+ if err < 0 {
+ return MakeGitError(err)
}
return nil
@@ -149,7 +165,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 6979b6b..71df7bb 100644
--- a/walk.go
+++ b/walk.go
@@ -14,11 +14,12 @@ import (
// RevWalk
type SortType uint
+
const (
- SortNone SortType = C.GIT_SORT_NONE
- SortTopological = C.GIT_SORT_TOPOLOGICAL
- SortTime = C.GIT_SORT_TIME
- SortReverse = C.GIT_SORT_REVERSE
+ SortNone SortType = C.GIT_SORT_NONE
+ SortTopological = C.GIT_SORT_TOPOLOGICAL
+ SortTime = C.GIT_SORT_TIME
+ SortReverse = C.GIT_SORT_REVERSE
)
type RevWalk struct {
@@ -26,6 +27,12 @@ type RevWalk struct {
repo *Repository
}
+func revWalkFromC(repo *Repository, c *C.git_revwalk) *RevWalk {
+ v := &RevWalk{ptr: c, repo: repo}
+ runtime.SetFinalizer(v, (*RevWalk).Free)
+ return v
+}
+
func (v *RevWalk) Reset() {
C.git_revwalk_reset(v.ptr)
}
@@ -40,22 +47,22 @@ func (v *RevWalk) PushHead() (err error) {
ecode := C.git_revwalk_push_head(v.ptr)
if ecode < 0 {
- err = LastError()
+ err = MakeGitError(ecode)
}
return
}
-func (v *RevWalk) Next(oid *Oid) (err error) {
+func (v *RevWalk) Next(id *Oid) (err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_revwalk_next(oid.toC(), v.ptr)
+ ret := C.git_revwalk_next(id.toC(), v.ptr)
switch {
case ret == ITEROVER:
err = io.EOF
case ret < 0:
- err = LastError()
+ err = MakeGitError(ret)
}
return
@@ -92,6 +99,8 @@ func (v *RevWalk) Sorting(sm SortType) {
C.git_revwalk_sorting(v.ptr, C.uint(sm))
}
-func freeRevWalk(walk *RevWalk) {
- C.git_revwalk_free(walk.ptr)
+func (v *RevWalk) Free() {
+
+ runtime.SetFinalizer(v, nil)
+ C.git_revwalk_free(v.ptr)
}
diff --git a/wrapper.c b/wrapper.c
index 93331ad..affa8d6 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -25,6 +25,7 @@ int _go_git_odb_foreach(git_odb *db, void *payload)
return git_odb_foreach(db, (git_odb_foreach_cb)&odbForEachCb, payload);
}
+<<<<<<< HEAD
int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload)
{
git_diff_file_cb fcb = NULL;
@@ -44,5 +45,43 @@ int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLin
}
return git_diff_foreach(diff, fcb, hcb, lcb, payload);
+=======
+void _go_git_setup_callbacks(git_remote_callbacks *callbacks) {
+ typedef int (*progress_cb)(const char *str, int len, void *data);
+ typedef int (*completion_cb)(git_remote_completion_type type, void *data);
+ typedef int (*credentials_cb)(git_cred **cred, const char *url, const char *username_from_url, unsigned int allowed_types, void *data);
+ typedef int (*transfer_progress_cb)(const git_transfer_progress *stats, void *data);
+ typedef int (*update_tips_cb)(const char *refname, const git_oid *a, const git_oid *b, void *data);
+ callbacks->progress = (progress_cb)progressCallback;
+ callbacks->completion = (completion_cb)completionCallback;
+ callbacks->credentials = (credentials_cb)credentialsCallback;
+ callbacks->transfer_progress = (transfer_progress_cb)transferProgressCallback;
+ callbacks->update_tips = (update_tips_cb)updateTipsCallback;
+}
+
+typedef int (*status_foreach_cb)(const char *ref, const char *msg, void *data);
+
+int _go_git_push_status_foreach(git_push *push, void *data)
+{
+ return git_push_status_foreach(push, (status_foreach_cb)statusForeach, data);
+}
+
+int _go_git_push_set_callbacks(git_push *push, void *packbuilder_progress_data, void *transfer_progress_data)
+{
+ return git_push_set_callbacks(push, packbuilderProgress, packbuilder_progress_data, pushTransferProgress, transfer_progress_data);
+}
+
+int _go_blob_chunk_cb(char *buffer, size_t maxLen, void *payload)
+{
+ return blobChunkCb(buffer, maxLen, payload);
+}
+
+int _go_git_blob_create_fromchunks(git_oid *id,
+ git_repository *repo,
+ const char *hintpath,
+ void *payload)
+{
+ return git_blob_create_fromchunks(id, repo, hintpath, _go_blob_chunk_cb, payload);
+>>>>>>> 2811845a1287d949a74b8ed80a5791fd8875002a
}
/* EOF */