diff options
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | .travis.yml | 10 | ||||
| -rw-r--r-- | Makefile | 11 | ||||
| -rw-r--r-- | README.md | 25 | ||||
| -rw-r--r-- | blame_test.go | 4 | ||||
| -rw-r--r-- | blob.go | 15 | ||||
| -rw-r--r-- | blob_test.go | 3 | ||||
| -rw-r--r-- | branch.go | 35 | ||||
| -rw-r--r-- | branch_test.go | 37 | ||||
| -rw-r--r-- | checkout.go | 64 | ||||
| -rw-r--r-- | cherrypick.go | 73 | ||||
| -rw-r--r-- | cherrypick_test.go | 85 | ||||
| -rw-r--r-- | clone.go | 11 | ||||
| -rw-r--r-- | clone_test.go | 7 | ||||
| -rw-r--r-- | commit.go | 64 | ||||
| -rw-r--r-- | config.go | 52 | ||||
| -rw-r--r-- | diff.go | 153 | ||||
| -rw-r--r-- | diff_test.go | 54 | ||||
| -rw-r--r-- | git.go | 6 | ||||
| -rw-r--r-- | git_test.go | 19 | ||||
| -rw-r--r-- | graph.go | 36 | ||||
| -rw-r--r-- | handles.go | 84 | ||||
| -rw-r--r-- | index.go | 67 | ||||
| -rw-r--r-- | index_test.go | 74 | ||||
| -rw-r--r-- | merge.go | 1 | ||||
| -rw-r--r-- | merge_test.go | 11 | ||||
| -rw-r--r-- | note_test.go | 10 | ||||
| -rw-r--r-- | object.go | 8 | ||||
| -rw-r--r-- | object_test.go | 7 | ||||
| -rw-r--r-- | odb.go | 21 | ||||
| -rw-r--r-- | odb_test.go | 16 | ||||
| -rw-r--r-- | packbuilder.go | 15 | ||||
| -rw-r--r-- | patch_test.go | 9 | ||||
| -rw-r--r-- | push_test.go | 6 | ||||
| -rw-r--r-- | reference.go | 15 | ||||
| -rw-r--r-- | reference_test.go | 13 | ||||
| -rw-r--r-- | remote.go | 36 | ||||
| -rw-r--r-- | remote_test.go | 28 | ||||
| -rw-r--r-- | repository.go | 88 | ||||
| -rw-r--r-- | revparse_test.go | 7 | ||||
| -rwxr-xr-x | script/build-libgit2-static.sh | 19 | ||||
| -rwxr-xr-x | script/with-static.sh | 12 | ||||
| -rw-r--r-- | signature.go | 73 | ||||
| -rw-r--r-- | status.go | 31 | ||||
| -rw-r--r-- | status_test.go | 13 | ||||
| -rw-r--r-- | submodule.go | 33 | ||||
| -rw-r--r-- | submodule_test.go | 26 | ||||
| -rw-r--r-- | tag_test.go | 6 | ||||
| -rw-r--r-- | tree.go | 16 | ||||
| m--------- | vendor/libgit2 | 0 | ||||
| -rw-r--r-- | wrapper.c | 21 |
51 files changed, 1236 insertions, 297 deletions
diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 8eb5872..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "vendor/libgit2"] - path = vendor/libgit2 - url = https://github.com/libgit2/libgit2 diff --git a/.travis.yml b/.travis.yml index f84d07e..f8b7e93 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,19 @@ language: go +install: + - cd "${HOME}" + - wget -O libgit2-0.22.1.tar.gz https://github.com/libgit2/libgit2/archive/v0.22.1.tar.gz + - tar -xzvf libgit2-0.22.1.tar.gz + - cd libgit2-0.22.1 && mkdir build && cd build + - cmake -DTHREADSAFE=ON -DBUILD_CLAR=OFF -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DCMAKE_INSTALL_PREFIX=/usr/local .. && make && sudo make install + - sudo ldconfig + - cd "${TRAVIS_BUILD_DIR}" + go: - 1.1 - 1.2 - 1.3 + - 1.4 - tip matrix: @@ -1,11 +1,8 @@ default: test -build-libgit2: - ./script/build-libgit2-static.sh - -test: build-libgit2 +test: go run script/check-MakeGitError-thread-lock.go - ./script/with-static.sh go test ./... + go test ./... -install: build-libgit2 - ./script/with-static.sh go install ./... +install: + go install ./... @@ -3,15 +3,34 @@ git2go [](http://godoc.org/github.com/libgit2/git2go) [](https://travis-ci.org/libgit2/git2go) -Go bindings for [libgit2](http://libgit2.github.com/). The master branch follows the latest libgit2 release. +Go bindings for [libgit2](http://libgit2.github.com/). The `master` branch follows the latest libgit2 release. The versioned branches indicate which libgit2 version they work against. Installing ---------- -This project needs libgit2, which is written in C so we need to build that as well. In order to build libgit2, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL and LibSSH2 if you want to use HTTPS and SSH respectively. +This project wraps the functionality provided by libgit2. If you're using a stable version, install it to your system via your system's package manger and then install git2go as usual. + +Otherwise (`next` which tracks an unstable version), we need to build libgit2 as well. In order to build it, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. + +### Stable version + +git2go has `master` which tracks the latest release of libgit2, and versioned branches which indicate which version of libgit2 they work against. Install the development package it on your system via your favourite package manager or from source and you can use a service like gopkg.in to use the appropriate version. For the libgit2 v0.22 case, you can use + + import "gopkg.in/libgit2/git2go.v22" + +to use a version of git2go which will work against libgit2 v0.22 and dynamically link to the library. You can use + + import "github.com/libgit2/git2go" + +to use the version which works against the latest release. + +### From `next` + +The `next` branch follows libgit2's master branch, which means there is no stable API or ABI to link against. git2go can statically link against a vendored version of libgit2. Run `go get -d github.com/libgit2/git2go` to download the code and go to your `$GOPATH/src/github.com/libgit2/git2go` dir. From there, we need to build the C code and put it into the resulting go binary. + git checkout next git submodule update --init # get libgit2 make install @@ -25,7 +44,7 @@ libgit2 uses OpenSSL and LibSSH2 for performing encrypted network connections. F Running the tests ----------------- -Similarly to installing, running the tests requires linking against the local libgit2 library, so the Makefile provides a wrapper +For the stable version, `go test` will work as usual. For the `next` branch, similarly to installing, running the tests requires linking against the local libgit2 library, so the Makefile provides a wrapper make test diff --git a/blame_test.go b/blame_test.go index 1785042..a2a4d38 100644 --- a/blame_test.go +++ b/blame_test.go @@ -1,15 +1,13 @@ package git import ( - "os" "reflect" "testing" ) func TestBlame(t *testing.T) { repo := createTestRepo(t) - defer repo.Free() - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) commitId1, _ := seedTestRepo(t, repo) commitId2, _ := updateReadme(t, repo, "foo\nbar\nbaz\n") @@ -60,8 +60,13 @@ type BlobCallbackData struct { } //export blobChunkCb -func blobChunkCb(buffer *C.char, maxLen C.size_t, payload unsafe.Pointer) int { - data := (*BlobCallbackData)(payload) +func blobChunkCb(buffer *C.char, maxLen C.size_t, handle unsafe.Pointer) int { + payload := pointerHandles.Get(handle) + data, ok := payload.(*BlobCallbackData) + if !ok { + panic("could not retrieve blob callback data") + } + goBuf, err := data.Callback(int(maxLen)) if err == io.EOF { return 0 @@ -83,8 +88,12 @@ func (repo *Repository) CreateBlobFromChunks(hintPath string, callback BlobChunk 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)) + handle := pointerHandles.Track(payload) + defer pointerHandles.Untrack(handle) + + ecode := C._go_git_blob_create_fromchunks(&oid, repo.ptr, chintPath, handle) if payload.Error != nil { return nil, payload.Error } diff --git a/blob_test.go b/blob_test.go index e075192..2b5ec4f 100644 --- a/blob_test.go +++ b/blob_test.go @@ -1,13 +1,12 @@ package git import ( - "os" "testing" ) func TestCreateBlobFromBuffer(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) id, err := repo.CreateBlobFromBuffer(make([]byte, 0)) checkFatal(t, err) @@ -30,10 +30,7 @@ type BranchIterator struct { repo *Repository } -type BranchInfo struct { - Branch *Branch - Type BranchType -} +type BranchIteratorFunc func(*Branch, BranchType) error func newBranchIteratorFromC(repo *Repository, ptr *C.git_branch_iterator) *BranchIterator { i := &BranchIterator{repo: repo, ptr: ptr} @@ -65,8 +62,20 @@ func (i *BranchIterator) Free() { C.git_branch_iterator_free(i.ptr) } -func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, error) { +func (i *BranchIterator) ForEach(f BranchIteratorFunc) error { + b, t, err := i.Next() + + for err == nil { + err = f(b, t) + if err == nil { + b, t, err = i.Next() + } + } + + return err +} +func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, error) { refType := C.git_branch_t(flags) var ptr *C.git_branch_iterator @@ -83,11 +92,14 @@ func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, er func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, msg string) (*Branch, error) { - ref := new(Reference) + var ptr *C.git_reference cBranchName := C.CString(branchName) cForce := cbool(force) - cSignature := signature.toC() + cSignature, err := signature.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(cSignature) var cmsg *C.char @@ -101,11 +113,11 @@ 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.cast_ptr, cForce, cSignature, cmsg) + ret := C.git_branch_create(&ptr, repo.ptr, cBranchName, target.cast_ptr, cForce, cSignature, cmsg) if ret < 0 { return nil, MakeGitError(ret) } - return ref.Branch(), nil + return newReferenceFromC(ptr, repo).Branch(), nil } func (b *Branch) Delete() error { @@ -124,7 +136,10 @@ func (b *Branch) Move(newBranchName string, force bool, signature *Signature, ms cNewBranchName := C.CString(newBranchName) cForce := cbool(force) - cSignature := signature.toC() + cSignature, err := signature.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(cSignature) var cmsg *C.char diff --git a/branch_test.go b/branch_test.go index 44f6338..a0834a8 100644 --- a/branch_test.go +++ b/branch_test.go @@ -5,8 +5,9 @@ import ( ) func TestBranchIterator(t *testing.T) { - repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + seedTestRepo(t, repo) i, err := repo.NewBranchIterator(BranchLocal) @@ -24,3 +25,37 @@ func TestBranchIterator(t *testing.T) { t.Fatal("expected iterover") } } + +func TestBranchIteratorEach(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + seedTestRepo(t, repo) + + i, err := repo.NewBranchIterator(BranchLocal) + checkFatal(t, err) + + var names []string + f := func(b *Branch, t BranchType) error { + name, err := b.Name() + if err != nil { + return err + } + + names = append(names, name) + return nil + } + + err = i.ForEach(f) + if err != nil && !IsErrorCode(err, ErrIterOver) { + t.Fatal(err) + } + + if len(names) != 1 { + t.Fatalf("expect 1 branch, but it was %d\n", len(names)) + } + + if names[0] != "master" { + t.Fatalf("expect branch master, but it was %s\n", names[0]) + } +} diff --git a/checkout.go b/checkout.go index 9c7188e..9874d2b 100644 --- a/checkout.go +++ b/checkout.go @@ -7,6 +7,7 @@ import "C" import ( "os" "runtime" + "unsafe" ) type CheckoutStrategy uint @@ -19,10 +20,10 @@ const ( CheckoutAllowConflicts CheckoutStrategy = C.GIT_CHECKOUT_ALLOW_CONFLICTS // Allow checkout to make safe updates even if conflicts are found CheckoutRemoveUntracked CheckoutStrategy = C.GIT_CHECKOUT_REMOVE_UNTRACKED // Remove untracked files not in index (that are not ignored) CheckoutRemoveIgnored CheckoutStrategy = C.GIT_CHECKOUT_REMOVE_IGNORED // Remove ignored files not in index - CheckotUpdateOnly CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_ONLY // Only update existing files, don't create new ones + CheckoutUpdateOnly CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_ONLY // Only update existing files, don't create new ones CheckoutDontUpdateIndex CheckoutStrategy = C.GIT_CHECKOUT_DONT_UPDATE_INDEX // Normally checkout updates index entries as it goes; this stops that CheckoutNoRefresh CheckoutStrategy = C.GIT_CHECKOUT_NO_REFRESH // Don't refresh index/config/etc before doing checkout - CheckooutDisablePathspecMatch CheckoutStrategy = C.GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH // Treat pathspec as simple list of exact match file paths + CheckoutDisablePathspecMatch CheckoutStrategy = C.GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH // Treat pathspec as simple list of exact match file paths CheckoutSkipUnmerged CheckoutStrategy = C.GIT_CHECKOUT_SKIP_UNMERGED // Allow checkout to skip unmerged files (NOT IMPLEMENTED) CheckoutUserOurs CheckoutStrategy = C.GIT_CHECKOUT_USE_OURS // For unmerged files, checkout stage 2 from index (NOT IMPLEMENTED) CheckoutUseTheirs CheckoutStrategy = C.GIT_CHECKOUT_USE_THEIRS // For unmerged files, checkout stage 3 from index (NOT IMPLEMENTED) @@ -31,11 +32,26 @@ const ( ) type CheckoutOpts struct { - Strategy CheckoutStrategy // Default will be a dry run - DisableFilters bool // Don't apply filters like CRLF conversion - DirMode os.FileMode // Default is 0755 - FileMode os.FileMode // Default is 0644 or 0755 as dictated by blob - FileOpenFlags int // Default is O_CREAT | O_TRUNC | O_WRONLY + Strategy CheckoutStrategy // Default will be a dry run + DisableFilters bool // Don't apply filters like CRLF conversion + DirMode os.FileMode // Default is 0755 + FileMode os.FileMode // Default is 0644 or 0755 as dictated by blob + FileOpenFlags int // Default is O_CREAT | O_TRUNC | O_WRONLY + TargetDirectory string // Alternative checkout path to workdir + Paths []string +} + +func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOpts { + opts := CheckoutOpts{} + opts.Strategy = CheckoutStrategy(c.checkout_strategy) + opts.DisableFilters = c.disable_filters != 0 + opts.DirMode = os.FileMode(c.dir_mode) + opts.FileMode = os.FileMode(c.file_mode) + opts.FileOpenFlags = int(c.file_open_flags) + if c.target_directory != nil { + opts.TargetDirectory = C.GoString(c.target_directory) + } + return opts } func (opts *CheckoutOpts) toC() *C.git_checkout_options { @@ -60,17 +76,37 @@ func populateCheckoutOpts(ptr *C.git_checkout_options, opts *CheckoutOpts) *C.gi ptr.disable_filters = cbool(opts.DisableFilters) ptr.dir_mode = C.uint(opts.DirMode.Perm()) ptr.file_mode = C.uint(opts.FileMode.Perm()) + if opts.TargetDirectory != "" { + ptr.target_directory = C.CString(opts.TargetDirectory) + } + if len(opts.Paths) > 0 { + ptr.paths.strings = makeCStringsFromStrings(opts.Paths) + ptr.paths.count = C.size_t(len(opts.Paths)) + } return ptr } +func freeCheckoutOpts(ptr *C.git_checkout_options) { + if ptr == nil { + return + } + C.free(unsafe.Pointer(ptr.target_directory)) + if ptr.paths.count > 0 { + freeStrarray(&ptr.paths) + } +} + // Updates files in the index and the working tree to match the content of // the commit pointed at by HEAD. opts may be nil. func (v *Repository) CheckoutHead(opts *CheckoutOpts) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_checkout_head(v.ptr, opts.toC()) + cOpts := opts.toC() + defer freeCheckoutOpts(cOpts) + + ret := C.git_checkout_head(v.ptr, cOpts) if ret < 0 { return MakeGitError(ret) } @@ -90,7 +126,10 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_checkout_index(v.ptr, iptr, opts.toC()) + cOpts := opts.toC() + defer freeCheckoutOpts(cOpts) + + ret := C.git_checkout_index(v.ptr, iptr, cOpts) if ret < 0 { return MakeGitError(ret) } @@ -102,10 +141,13 @@ func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOpts) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_checkout_tree(v.ptr, tree.ptr, opts.toC()) + cOpts := opts.toC() + defer freeCheckoutOpts(cOpts) + + ret := C.git_checkout_tree(v.ptr, tree.ptr, cOpts) if ret < 0 { return MakeGitError(ret) } return nil -} +}
\ No newline at end of file diff --git a/cherrypick.go b/cherrypick.go new file mode 100644 index 0000000..afc1b7e --- /dev/null +++ b/cherrypick.go @@ -0,0 +1,73 @@ +package git + +/* +#include <git2.h> +*/ +import "C" +import ( + "runtime" +) + +type CherrypickOptions struct { + Version uint + Mainline uint + MergeOpts MergeOptions + CheckoutOpts CheckoutOpts +} + +func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions { + opts := CherrypickOptions{ + Version: uint(c.version), + Mainline: uint(c.mainline), + MergeOpts: mergeOptionsFromC(&c.merge_opts), + CheckoutOpts: checkoutOptionsFromC(&c.checkout_opts), + } + return opts +} + +func (opts *CherrypickOptions) toC() *C.git_cherrypick_options { + if opts == nil { + return nil + } + c := C.git_cherrypick_options{} + c.version = C.uint(opts.Version) + c.mainline = C.uint(opts.Mainline) + c.merge_opts = *opts.MergeOpts.toC() + c.checkout_opts = *opts.CheckoutOpts.toC() + return &c +} + +func freeCherrypickOpts(ptr *C.git_cherrypick_options) { + if ptr == nil { + return + } + freeCheckoutOpts(&ptr.checkout_opts) +} + +func DefaultCherrypickOptions() (CherrypickOptions, error) { + c := C.git_cherrypick_options{} + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_cherrypick_init_options(&c, C.GIT_CHERRYPICK_OPTIONS_VERSION) + if ecode < 0 { + return CherrypickOptions{}, MakeGitError(ecode) + } + defer freeCherrypickOpts(&c) + return cherrypickOptionsFromC(&c), nil +} + +func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cOpts := opts.toC() + defer freeCherrypickOpts(cOpts) + + ecode := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts) + if ecode < 0 { + return MakeGitError(ecode) + } + return nil +} diff --git a/cherrypick_test.go b/cherrypick_test.go new file mode 100644 index 0000000..09bc524 --- /dev/null +++ b/cherrypick_test.go @@ -0,0 +1,85 @@ +package git + +import ( + "io/ioutil" + "testing" +) + +func checkout(t *testing.T, repo *Repository, commit *Commit) { + tree, err := commit.Tree() + if err != nil { + t.Fatal(err) + } + + err = repo.CheckoutTree(tree, &CheckoutOpts{Strategy: CheckoutSafe}) + if err != nil { + t.Fatal(err) + } + + err = repo.SetHeadDetached(commit.Id(), commit.Author(), "checkout") + if err != nil { + t.Fatal(err) + } +} + +const content = "Herro, Worrd!" + +func readReadme(t *testing.T, repo *Repository) string { + bytes, err := ioutil.ReadFile(pathInRepo(repo, "README")) + if err != nil { + t.Fatal(err) + } + return string(bytes) +} + +func TestCherrypick(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + c1, _ := seedTestRepo(t, repo) + c2, _ := updateReadme(t, repo, content) + + commit1, err := repo.LookupCommit(c1) + if err != nil { + t.Fatal(err) + } + commit2, err := repo.LookupCommit(c2) + if err != nil { + t.Fatal(err) + } + + checkout(t, repo, commit1) + + if readReadme(t, repo) == content { + t.Fatalf("README has wrong content after checking out initial commit") + } + + opts, err := DefaultCherrypickOptions() + if err != nil { + t.Fatal(err) + } + + err = repo.Cherrypick(commit2, opts) + if err != nil { + t.Fatal(err) + } + + if readReadme(t, repo) != content { + t.Fatalf("README has wrong contents after cherry-picking") + } + + state := repo.State() + if state != RepositoryStateCherrypick { + t.Fatal("Incorrect repository state: ", state) + } + + err = repo.StateCleanup() + if err != nil { + t.Fatal(err) + } + + state = repo.State() + if state != RepositoryStateNone { + t.Fatal("Incorrect repository state: ", state) + } +} @@ -28,17 +28,20 @@ func Clone(url string, path string, options *CloneOptions) (*Repository, error) cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) - var copts C.git_clone_options - populateCloneOptions(&copts, options) + copts := (*C.git_clone_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_clone_options{})))) + populateCloneOptions(copts, options) 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) + ret := C.git_clone(&repo.ptr, curl, cpath, copts) + freeCheckoutOpts(&copts.checkout_opts) + C.free(unsafe.Pointer(copts.checkout_branch)) + C.free(unsafe.Pointer(copts)) + if ret < 0 { return nil, MakeGitError(ret) } diff --git a/clone_test.go b/clone_test.go index 97366bf..fd83fec 100644 --- a/clone_test.go +++ b/clone_test.go @@ -2,22 +2,21 @@ package git import ( "io/ioutil" - "os" "testing" ) func TestClone(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) seedTestRepo(t, repo) path, err := ioutil.TempDir("", "git2go") checkFatal(t, err) - _, err = Clone(repo.Path(), path, &CloneOptions{Bare: true}) - defer os.RemoveAll(path) + repo2, err := Clone(repo.Path(), path, &CloneOptions{Bare: true}) + defer cleanupTestRepo(t, repo2) checkFatal(t, err) } @@ -9,7 +9,6 @@ import "C" import ( "runtime" - "time" "unsafe" ) @@ -23,6 +22,10 @@ func (c Commit) Message() string { return C.GoString(C.git_commit_message(c.cast_ptr)) } +func (c Commit) Summary() string { + return C.GoString(C.git_commit_summary(c.cast_ptr)) +} + func (c Commit) Tree() (*Tree, error) { var ptr *C.git_tree @@ -69,48 +72,39 @@ func (c *Commit) ParentCount() uint { return uint(C.git_commit_parentcount(c.cast_ptr)) } -// Signature - -type Signature struct { - Name string - Email string - When time.Time -} - -func newSignatureFromC(sig *C.git_signature) *Signature { - // git stores minutes, go wants seconds - loc := time.FixedZone("", int(sig.when.offset)*60) - return &Signature{ - C.GoString(sig.name), - C.GoString(sig.email), - time.Unix(int64(sig.when.time), 0).In(loc), +func (c *Commit) Amend(refname string, author, committer *Signature, message string, tree *Tree) (*Oid, error) { + var cref *C.char + if refname == "" { + cref = nil + } else { + cref = C.CString(refname) + defer C.free(unsafe.Pointer(cref)) } -} -// the offset in mintes, which is what git wants -func (v *Signature) Offset() int { - _, offset := v.When.Zone() - return offset / 60 -} + cmsg := C.CString(message) + defer C.free(unsafe.Pointer(cmsg)) -func (sig *Signature) toC() *C.git_signature { + runtime.LockOSThread() + defer runtime.UnlockOSThread() - if sig == nil { - return nil + authorSig, err := author.toC() + if err != nil { + return nil, err } + defer C.git_signature_free(authorSig) - var out *C.git_signature - - name := C.CString(sig.Name) - defer C.free(unsafe.Pointer(name)) + committerSig, err := committer.toC() + if err != nil { + return nil, err + } + defer C.git_signature_free(committerSig) - email := C.CString(sig.Email) - defer C.free(unsafe.Pointer(email)) + oid := new(Oid) - ret := C.git_signature_new(&out, name, email, C.git_time_t(sig.When.Unix()), C.int(sig.Offset())) - if ret < 0 { - return nil + cerr := C.git_commit_amend(oid.toC(), c.cast_ptr, cref, authorSig, committerSig, nil, cmsg, tree.cast_ptr) + if cerr < 0 { + return nil, MakeGitError(cerr) } - return out + return oid, nil } @@ -35,14 +35,14 @@ const ( ) type ConfigEntry struct { - Name string + Name string Value string Level ConfigLevel } func newConfigEntryFromC(centry *C.git_config_entry) *ConfigEntry { return &ConfigEntry{ - Name: C.GoString(centry.name), + Name: C.GoString(centry.name), Value: C.GoString(centry.value), Level: ConfigLevel(centry.level), } @@ -74,7 +74,6 @@ func (c *Config) AddFile(path string, level ConfigLevel, force bool) error { 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) @@ -130,7 +129,6 @@ func (c *Config) LookupString(name string) (string, error) { return C.GoString(ptr), nil } - func (c *Config) LookupBool(name string) (bool, error) { var out C.int cname := C.CString(name) @@ -234,7 +232,6 @@ func (c *Config) SetInt32(name string, value int32) (err error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) - runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -368,3 +365,48 @@ func (iter *ConfigIterator) Free() { runtime.SetFinalizer(iter, nil) C.free(unsafe.Pointer(iter.ptr)) } + +func ConfigFindGlobal() (string, error) { + var buf C.git_buf + defer C.git_buf_free(&buf) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C.git_config_find_global(&buf) + if ret < 0 { + return "", MakeGitError(ret) + } + + return C.GoString(buf.ptr), nil +} + +func ConfigFindSystem() (string, error) { + var buf C.git_buf + defer C.git_buf_free(&buf) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C.git_config_find_system(&buf) + if ret < 0 { + return "", MakeGitError(ret) + } + + return C.GoString(buf.ptr), nil +} + +func ConfigFindXDG() (string, error) { + var buf C.git_buf + defer C.git_buf_free(&buf) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C.git_config_find_xdg(&buf) + if ret < 0 { + return "", MakeGitError(ret) + } + + return C.GoString(buf.ptr), nil +} @@ -5,6 +5,7 @@ package git extern int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload); extern void _go_git_setup_diff_notify_callbacks(git_diff_options* opts); +extern int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload); */ import "C" import ( @@ -94,7 +95,7 @@ type DiffHunk struct { Header string } -func diffHunkFromC(delta *C.git_diff_delta, hunk *C.git_diff_hunk) DiffHunk { +func diffHunkFromC(hunk *C.git_diff_hunk) DiffHunk { return DiffHunk{ OldStart: int(hunk.old_start), OldLines: int(hunk.old_lines), @@ -112,7 +113,7 @@ type DiffLine struct { Content string } -func diffLineFromC(delta *C.git_diff_delta, hunk *C.git_diff_hunk, line *C.git_diff_line) DiffLine { +func diffLineFromC(line *C.git_diff_line) DiffLine { return DiffLine{ Origin: DiffLineType(line.origin), OldLineno: int(line.old_lineno), @@ -265,7 +266,11 @@ func (diff *Diff) ForEach(cbFile DiffForEachFileCallback, detail DiffDetail) err data := &diffForEachData{ FileCallback: cbFile, } - ecode := C._go_git_diff_foreach(diff.ptr, 1, intHunks, intLines, unsafe.Pointer(data)) + + handle := pointerHandles.Track(data) + defer pointerHandles.Untrack(handle) + + ecode := C._go_git_diff_foreach(diff.ptr, 1, intHunks, intLines, handle) if ecode < 0 { return data.Error } @@ -273,8 +278,12 @@ func (diff *Diff) ForEach(cbFile DiffForEachFileCallback, detail DiffDetail) err } //export diffForEachFileCb -func diffForEachFileCb(delta *C.git_diff_delta, progress C.float, payload unsafe.Pointer) int { - data := (*diffForEachData)(payload) +func diffForEachFileCb(delta *C.git_diff_delta, progress C.float, handle unsafe.Pointer) int { + payload := pointerHandles.Get(handle) + data, ok := payload.(*diffForEachData) + if !ok { + panic("could not retrieve data for handle") + } data.HunkCallback = nil if data.FileCallback != nil { @@ -292,12 +301,16 @@ func diffForEachFileCb(delta *C.git_diff_delta, progress C.float, payload unsafe type DiffForEachHunkCallback func(DiffHunk) (DiffForEachLineCallback, error) //export diffForEachHunkCb -func diffForEachHunkCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, payload unsafe.Pointer) int { - data := (*diffForEachData)(payload) +func diffForEachHunkCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, handle unsafe.Pointer) int { + payload := pointerHandles.Get(handle) + data, ok := payload.(*diffForEachData) + if !ok { + panic("could not retrieve data for handle") + } data.LineCallback = nil if data.HunkCallback != nil { - cb, err := data.HunkCallback(diffHunkFromC(delta, hunk)) + cb, err := data.HunkCallback(diffHunkFromC(hunk)) if err != nil { data.Error = err return -1 @@ -311,11 +324,14 @@ func diffForEachHunkCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, payload u type DiffForEachLineCallback func(DiffLine) error //export diffForEachLineCb -func diffForEachLineCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, line *C.git_diff_line, payload unsafe.Pointer) int { - - data := (*diffForEachData)(payload) +func diffForEachLineCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, line *C.git_diff_line, handle unsafe.Pointer) int { + payload := pointerHandles.Get(handle) + data, ok := payload.(*diffForEachData) + if !ok { + panic("could not retrieve data for handle") + } - err := data.LineCallback(diffLineFromC(delta, hunk, line)) + err := data.LineCallback(diffLineFromC(line)) if err != nil { data.Error = err return -1 @@ -479,9 +495,15 @@ type diffNotifyData struct { } //export diffNotifyCb -func diffNotifyCb(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, matched_pathspec *C.char, payload unsafe.Pointer) int { +func diffNotifyCb(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, matched_pathspec *C.char, handle unsafe.Pointer) int { diff_so_far := (*C.git_diff)(_diff_so_far) - data := (*diffNotifyData)(payload) + + payload := pointerHandles.Get(handle) + data, ok := payload.(*diffNotifyData) + if !ok { + panic("could not retrieve data for handle") + } + if data != nil { if data.Diff == nil { data.Diff = newDiffFromC(diff_so_far) @@ -507,6 +529,7 @@ func diffOptionsToC(opts *DiffOptions) (copts *C.git_diff_options, notifyData *d notifyData = &diffNotifyData{ Callback: opts.NotifyCallback, } + if opts.Pathspec != nil { cpathspec.count = C.size_t(len(opts.Pathspec)) cpathspec.strings = makeCStringsFromStrings(opts.Pathspec) @@ -527,7 +550,7 @@ func diffOptionsToC(opts *DiffOptions) (copts *C.git_diff_options, notifyData *d if opts.NotifyCallback != nil { C._go_git_setup_diff_notify_callbacks(copts) - copts.notify_payload = unsafe.Pointer(notifyData) + copts.notify_payload = pointerHandles.Track(notifyData) } } return @@ -539,6 +562,9 @@ func freeDiffOptions(copts *C.git_diff_options) { freeStrarray(&cpathspec) C.free(unsafe.Pointer(copts.old_prefix)) C.free(unsafe.Pointer(copts.new_prefix)) + if copts.notify_payload != nil { + pointerHandles.Untrack(copts.notify_payload) + } } } @@ -595,3 +621,100 @@ func (v *Repository) DiffTreeToWorkdir(oldTree *Tree, opts *DiffOptions) (*Diff, } return newDiffFromC(diffPtr), nil } + +func (v *Repository) DiffTreeToWorkdirWithIndex(oldTree *Tree, opts *DiffOptions) (*Diff, error) { + var diffPtr *C.git_diff + var oldPtr *C.git_tree + + if oldTree != nil { + oldPtr = oldTree.cast_ptr + } + + copts, notifyData := diffOptionsToC(opts) + defer freeDiffOptions(copts) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_diff_tree_to_workdir_with_index(&diffPtr, v.ptr, oldPtr, copts) + if ecode < 0 { + return nil, MakeGitError(ecode) + } + + if notifyData != nil && notifyData.Diff != nil { + return notifyData.Diff, nil + } + return newDiffFromC(diffPtr), nil +} + +func (v *Repository) DiffIndexToWorkdir(index *Index, opts *DiffOptions) (*Diff, error) { + var diffPtr *C.git_diff + var indexPtr *C.git_index + + if index != nil { + indexPtr = index.ptr + } + + copts, notifyData := diffOptionsToC(opts) + defer freeDiffOptions(copts) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_diff_index_to_workdir(&diffPtr, v.ptr, indexPtr, copts) + if ecode < 0 { + return nil, MakeGitError(ecode) + } + + if notifyData != nil && notifyData.Diff != nil { + return notifyData.Diff, nil + } + return newDiffFromC(diffPtr), nil +} + +// DiffBlobs performs a diff between two arbitrary blobs. You can pass +// whatever file names you'd like for them to appear as in the diff. +func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, opts *DiffOptions, fileCallback DiffForEachFileCallback, detail DiffDetail) error { + data := &diffForEachData{ + FileCallback: fileCallback, + } + + intHunks := C.int(0) + if detail >= DiffDetailHunks { + intHunks = C.int(1) + } + + intLines := C.int(0) + if detail >= DiffDetailLines { + intLines = C.int(1) + } + + handle := pointerHandles.Track(data) + defer pointerHandles.Untrack(handle) + + var oldBlobPtr, newBlobPtr *C.git_blob + if oldBlob != nil { + oldBlobPtr = oldBlob.cast_ptr + } + if newBlob != nil { + newBlobPtr = newBlob.cast_ptr + } + + oldBlobPath := C.CString(oldAsPath) + defer C.free(unsafe.Pointer(oldBlobPath)) + newBlobPath := C.CString(newAsPath) + defer C.free(unsafe.Pointer(newBlobPath)) + + copts, _ := diffOptionsToC(opts) + defer freeDiffOptions(copts) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C._go_git_diff_blobs(oldBlobPtr, oldBlobPath, newBlobPtr, newBlobPath, copts, 1, intHunks, intLines, handle) + if ecode < 0 { + return MakeGitError(ecode) + } + + return nil +} diff --git a/diff_test.go b/diff_test.go index fc6fed9..850ed8e 100644 --- a/diff_test.go +++ b/diff_test.go @@ -2,15 +2,13 @@ package git import ( "errors" - "os" "strings" "testing" ) func TestFindSimilar(t *testing.T) { repo := createTestRepo(t) - defer repo.Free() - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) originalTree, newTree := createTestTrees(t, repo) @@ -65,8 +63,7 @@ func TestFindSimilar(t *testing.T) { func TestDiffTreeToTree(t *testing.T) { repo := createTestRepo(t) - defer repo.Free() - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) originalTree, newTree := createTestTrees(t, repo) @@ -190,3 +187,50 @@ func createTestTrees(t *testing.T, repo *Repository) (originalTree *Tree, newTre return originalTree, newTree } + +func TestDiffBlobs(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + odb, err := repo.Odb() + checkFatal(t, err) + + id1, err := odb.Write([]byte("hello\nhello\n"), ObjectBlob) + checkFatal(t, err) + + id2, err := odb.Write([]byte("hallo\nhallo\n"), ObjectBlob) + checkFatal(t, err) + + blob1, err := repo.LookupBlob(id1) + checkFatal(t, err) + + blob2, err := repo.LookupBlob(id2) + checkFatal(t, err) + + var files, hunks, lines int + err = DiffBlobs(blob1, "hi", blob2, "hi", nil, + func(delta DiffDelta, progress float64) (DiffForEachHunkCallback, error) { + files++ + return func(hunk DiffHunk) (DiffForEachLineCallback, error) { + hunks++ + return func(line DiffLine) error { + lines++ + return nil + }, nil + }, nil + }, + DiffDetailLines) + + if files != 1 { + t.Fatal("Bad number of files iterated") + } + + if hunks != 1 { + t.Fatal("Bad number of hunks iterated") + } + + // two removals, two additions + if lines != 4 { + t.Fatalf("Bad number of lines iterated") + } +} @@ -87,13 +87,19 @@ const ( ErrPassthrough ErrorCode = C.GIT_PASSTHROUGH // Signals end of iteration with iterator ErrIterOver ErrorCode = C.GIT_ITEROVER + // Authentication failed + ErrAuth ErrorCode = C.GIT_EAUTH ) var ( ErrInvalid = errors.New("Invalid state for operation") ) +var pointerHandles *HandleList + func init() { + pointerHandles = NewHandleList() + C.git_libgit2_init() // This is not something we should be doing, as we may be diff --git a/git_test.go b/git_test.go index 56adeed..58caf71 100644 --- a/git_test.go +++ b/git_test.go @@ -2,11 +2,24 @@ package git import ( "io/ioutil" + "os" "path" "testing" "time" ) +func cleanupTestRepo(t *testing.T, r *Repository) { + var err error + if r.IsBare() { + err = os.RemoveAll(r.Path()) + } else { + err = os.RemoveAll(r.Workdir()) + } + checkFatal(t, err) + + r.Free() +} + func createTestRepo(t *testing.T) *Repository { // figure out where we can create the test repo path, err := ioutil.TempDir("", "git2go") @@ -57,6 +70,10 @@ func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) { return commitId, treeId } +func pathInRepo(repo *Repository, name string) string { + return path.Join(path.Dir(path.Dir(repo.Path())), name) +} + func updateReadme(t *testing.T, repo *Repository, content string) (*Oid, *Oid) { loc, err := time.LoadLocation("Europe/Berlin") checkFatal(t, err) @@ -67,7 +84,7 @@ func updateReadme(t *testing.T, repo *Repository, content string) (*Oid, *Oid) { } tmpfile := "README" - err = ioutil.WriteFile(path.Join(path.Dir(path.Dir(repo.Path())), tmpfile), []byte(content), 0644) + err = ioutil.WriteFile(pathInRepo(repo, tmpfile), []byte(content), 0644) checkFatal(t, err) idx, err := repo.Index() diff --git a/graph.go b/graph.go new file mode 100644 index 0000000..e5d7732 --- /dev/null +++ b/graph.go @@ -0,0 +1,36 @@ +package git + +/* +#include <git2.h> +*/ +import "C" +import ( + "runtime" +) + +func (repo *Repository) DescendantOf(commit, ancestor *Oid) (bool, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C.git_graph_descendant_of(repo.ptr, commit.toC(), ancestor.toC()) + if ret < 0 { + return false, MakeGitError(ret) + } + + return (ret > 0), nil +} + +func (repo *Repository) AheadBehind(local, upstream *Oid) (ahead, behind int, err error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var aheadT C.size_t + var behindT C.size_t + + ret := C.git_graph_ahead_behind(&aheadT, &behindT, repo.ptr, local.toC(), upstream.toC()) + if ret < 0 { + return 0, 0, MakeGitError(ret) + } + + return int(aheadT), int(behindT), nil +} diff --git a/handles.go b/handles.go new file mode 100644 index 0000000..ec62a48 --- /dev/null +++ b/handles.go @@ -0,0 +1,84 @@ +package git + +import ( + "fmt" + "sync" + "unsafe" +) + +type HandleList struct { + sync.RWMutex + // stores the Go pointers + handles []interface{} + // indicates which indices are in use + set map[int]bool +} + +func NewHandleList() *HandleList { + return &HandleList{ + handles: make([]interface{}, 5), + set: make(map[int]bool), + } +} + +// findUnusedSlot finds the smallest-index empty space in our +// list. You must only run this function while holding a write lock. +func (v *HandleList) findUnusedSlot() int { + for i := 1; i < len(v.handles); i++ { + isUsed := v.set[i] + if !isUsed { + return i + } + } + + // reaching here means we've run out of entries so append and + // return the new index, which is equal to the old length. + slot := len(v.handles) + v.handles = append(v.handles, nil) + + return slot +} + +// Track adds the given pointer to the list of pointers to track and +// returns a pointer value which can be passed to C as an opaque +// pointer. +func (v *HandleList) Track(pointer interface{}) unsafe.Pointer { + v.Lock() + + slot := v.findUnusedSlot() + v.handles[slot] = pointer + v.set[slot] = true + + v.Unlock() + + return unsafe.Pointer(&slot) +} + +// Untrack stops tracking the pointer given by the handle +func (v *HandleList) Untrack(handle unsafe.Pointer) { + slot := *(*int)(handle) + + v.Lock() + + v.handles[slot] = nil + delete(v.set, slot) + + v.Unlock() +} + +// Get retrieves the pointer from the given handle +func (v *HandleList) Get(handle unsafe.Pointer) interface{} { + slot := *(*int)(handle) + + v.RLock() + + if _, ok := v.set[slot]; !ok { + panic(fmt.Sprintf("invalid pointer handle: %p", handle)) + } + + ptr := v.handles[slot] + + v.RUnlock() + + return ptr +} @@ -96,6 +96,30 @@ func NewIndex() (*Index, error) { return &Index{ptr: ptr}, nil } +// OpenIndex creates a new index at the given path. If the file does +// not exist it will be created when Write() is called. +func OpenIndex(path string) (*Index, error) { + var ptr *C.git_index + + var cpath = C.CString(path) + defer C.free(unsafe.Pointer(cpath)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if err := C.git_index_open(&ptr, cpath); err < 0 { + return nil, MakeGitError(err) + } + + return &Index{ptr: ptr}, nil +} + +// Path returns the index' path on disk or an empty string if it +// exists only in memory. +func (v *Index) Path() string { + return C.GoString(C.git_index_path(v.ptr)) +} + // Add adds or replaces the given entry to the index, making a copy of // the data func (v *Index) Add(entry *IndexEntry) error { @@ -138,16 +162,17 @@ func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMat runtime.LockOSThread() defer runtime.UnlockOSThread() - var cb *IndexMatchedPathCallback + var handle unsafe.Pointer if callback != nil { - cb = &callback + handle = pointerHandles.Track(callback) + defer pointerHandles.Untrack(handle) } ret := C._go_git_index_add_all( v.ptr, &cpathspecs, C.uint(flags), - unsafe.Pointer(cb), + handle, ) if ret < 0 { return MakeGitError(ret) @@ -164,15 +189,16 @@ func (v *Index) UpdateAll(pathspecs []string, callback IndexMatchedPathCallback) runtime.LockOSThread() defer runtime.UnlockOSThread() - var cb *IndexMatchedPathCallback + var handle unsafe.Pointer if callback != nil { - cb = &callback + handle = pointerHandles.Track(callback) + defer pointerHandles.Untrack(handle) } ret := C._go_git_index_update_all( v.ptr, &cpathspecs, - unsafe.Pointer(cb), + handle, ) if ret < 0 { return MakeGitError(ret) @@ -189,15 +215,16 @@ func (v *Index) RemoveAll(pathspecs []string, callback IndexMatchedPathCallback) runtime.LockOSThread() defer runtime.UnlockOSThread() - var cb *IndexMatchedPathCallback + var handle unsafe.Pointer if callback != nil { - cb = &callback + handle = pointerHandles.Track(callback) + defer pointerHandles.Untrack(handle) } ret := C._go_git_index_remove_all( v.ptr, &cpathspecs, - unsafe.Pointer(cb), + handle, ) if ret < 0 { return MakeGitError(ret) @@ -207,8 +234,11 @@ func (v *Index) RemoveAll(pathspecs []string, callback IndexMatchedPathCallback) //export indexMatchedPathCallback func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Pointer) int { - callback := (*IndexMatchedPathCallback)(payload) - return (*callback)(C.GoString(cPath), C.GoString(cMatchedPathspec)) + if callback, ok := pointerHandles.Get(payload).(IndexMatchedPathCallback); ok { + return callback(C.GoString(cPath), C.GoString(cMatchedPathspec)) + } else { + panic("invalid matched path callback") + } } func (v *Index) RemoveByPath(path string) error { @@ -240,6 +270,20 @@ func (v *Index) WriteTreeTo(repo *Repository) (*Oid, error) { return oid, nil } +// ReadTree replaces the contents of the index with those of the given +// tree +func (v *Index) ReadTree(tree *Tree) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C.git_index_read_tree(v.ptr, tree.cast_ptr); + if ret < 0 { + return MakeGitError(ret) + } + + return nil +} + func (v *Index) WriteTree() (*Oid, error) { oid := new(Oid) @@ -287,6 +331,7 @@ func (v *Index) HasConflicts() bool { return C.git_index_has_conflicts(v.ptr) != 0 } +// FIXME: this might return an error func (v *Index) CleanupConflicts() { C.git_index_conflict_cleanup(v.ptr) } diff --git a/index_test.go b/index_test.go index 98d9a31..7c65f4f 100644 --- a/index_test.go +++ b/index_test.go @@ -9,7 +9,7 @@ import ( func TestCreateRepoAndStage(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) idx, err := repo.Index() checkFatal(t, err) @@ -23,12 +23,40 @@ func TestCreateRepoAndStage(t *testing.T) { } } +func TestIndexReadTree(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + _, _ = seedTestRepo(t, repo) + + ref, err := repo.Head() + checkFatal(t, err) + + obj, err := ref.Peel(ObjectTree); + checkFatal(t, err) + + tree := obj.(*Tree) + + idx, err := NewIndex() + checkFatal(t, err) + + err = idx.ReadTree(tree) + checkFatal(t, err) + + id, err := idx.WriteTreeTo(repo) + checkFatal(t, err) + + if tree.Id().Cmp(id) != 0 { + t.Fatalf("Read and written trees are not the same") + } +} + func TestIndexWriteTreeTo(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) repo2 := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo2) idx, err := repo.Index() checkFatal(t, err) @@ -44,7 +72,7 @@ func TestIndexWriteTreeTo(t *testing.T) { func TestIndexAddAndWriteTreeTo(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) odb, err := repo.Odb() checkFatal(t, err) @@ -55,6 +83,10 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) { idx, err := NewIndex() checkFatal(t, err) + if idx.Path() != "" { + t.Fatal("in-memory repo has a path") + } + entry := IndexEntry{ Path: "README", Id: blobID, @@ -74,7 +106,7 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) { func TestIndexAddAllNoCallback(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) err := ioutil.WriteFile(repo.Workdir()+"/README", []byte("foo\n"), 0644) checkFatal(t, err) @@ -95,7 +127,7 @@ func TestIndexAddAllNoCallback(t *testing.T) { func TestIndexAddAllCallback(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) err := ioutil.WriteFile(repo.Workdir()+"/README", []byte("foo\n"), 0644) checkFatal(t, err) @@ -121,6 +153,33 @@ func TestIndexAddAllCallback(t *testing.T) { } } +func TestIndexOpen(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + path := repo.Workdir() + "/heyindex" + + _, err := os.Stat(path) + if !os.IsNotExist(err) { + t.Fatal("new index file already exists") + } + + idx, err := OpenIndex(path) + checkFatal(t, err) + + if path != idx.Path() { + t.Fatalf("mismatched index paths, expected %v, got %v", path, idx.Path()) + } + + err = idx.Write() + checkFatal(t, err) + + _, err = os.Stat(path) + if os.IsNotExist(err) { + t.Fatal("new index file did not get written") + } +} + func checkFatal(t *testing.T, err error) { if err == nil { return @@ -129,8 +188,7 @@ func checkFatal(t *testing.T, err error) { // The failure happens at wherever we were called, not here _, file, line, ok := runtime.Caller(1) if !ok { - t.Fatal() + t.Fatalf("Unable to get caller") } - t.Fatalf("Fail at %v:%v; %v", file, line, err) } @@ -145,6 +145,7 @@ func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOpt cMergeOpts := mergeOptions.toC() cCheckoutOpts := checkoutOptions.toC() + defer freeCheckoutOpts(cCheckoutOpts) gmerge_head_array := make([]*C.git_annotated_commit, len(theirHeads)) for i := 0; i < len(theirHeads); i++ { diff --git a/merge_test.go b/merge_test.go index 1eba806..0b1faca 100644 --- a/merge_test.go +++ b/merge_test.go @@ -1,13 +1,13 @@ package git import ( - "os" "testing" ) func TestMergeWithSelf(t *testing.T) { - repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + seedTestRepo(t, repo) master, err := repo.LookupReference("refs/heads/master") @@ -23,8 +23,9 @@ func TestMergeWithSelf(t *testing.T) { } func TestMergeAnalysisWithSelf(t *testing.T) { - repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + seedTestRepo(t, repo) master, err := repo.LookupReference("refs/heads/master") @@ -44,7 +45,6 @@ func TestMergeAnalysisWithSelf(t *testing.T) { } func TestMergeSameFile(t *testing.T) { - file := MergeFileInput{ Path: "test", Mode: 33188, @@ -68,8 +68,7 @@ func TestMergeSameFile(t *testing.T) { } func TestMergeTreesWithoutAncestor(t *testing.T) { repo := createTestRepo(t) - defer repo.Free() - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) _, originalTreeId := seedTestRepo(t, repo) originalTree, err := repo.LookupTree(originalTreeId) diff --git a/note_test.go b/note_test.go index f5e9c01..e6c378d 100644 --- a/note_test.go +++ b/note_test.go @@ -2,7 +2,6 @@ package git import ( "fmt" - "os" "reflect" "testing" "time" @@ -10,7 +9,7 @@ import ( func TestCreateNote(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) commitId, _ := seedTestRepo(t, repo) @@ -29,7 +28,8 @@ func TestCreateNote(t *testing.T) { func TestNoteIterator(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) + seedTestRepo(t, repo) notes := make([]*Note, 5) @@ -64,7 +64,7 @@ func TestNoteIterator(t *testing.T) { func TestRemoveNote(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) commitId, _ := seedTestRepo(t, repo) @@ -87,7 +87,7 @@ func TestRemoveNote(t *testing.T) { func TestDefaultNoteRef(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) ref, err := repo.DefaultNoteRef() checkFatal(t, err) @@ -25,12 +25,12 @@ type Object interface { } type gitObject struct { - ptr *C.git_object + ptr *C.git_object repo *Repository } -func (t ObjectType) String() (string) { - switch (t) { +func (t ObjectType) String() string { + switch t { case ObjectAny: return "Any" case ObjectBad: @@ -71,7 +71,7 @@ func (o *gitObject) Free() { func allocObject(cobj *C.git_object, repo *Repository) Object { obj := gitObject{ - ptr: cobj, + ptr: cobj, repo: repo, } diff --git a/object_test.go b/object_test.go index f525351..aa295e5 100644 --- a/object_test.go +++ b/object_test.go @@ -1,13 +1,13 @@ package git import ( - "os" "testing" ) func TestObjectPoymorphism(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) + commitId, treeId := seedTestRepo(t, repo) var obj Object @@ -89,7 +89,8 @@ func checkOwner(t *testing.T, repo *Repository, obj Object) { func TestObjectOwner(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) + commitId, treeId := seedTestRepo(t, repo) commit, err := repo.LookupCommit(commitId) @@ -94,12 +94,16 @@ type OdbForEachCallback func(id *Oid) error type foreachData struct { callback OdbForEachCallback - err error + err error } //export odbForEachCb -func odbForEachCb(id *C.git_oid, payload unsafe.Pointer) int { - data := (*foreachData)(payload) +func odbForEachCb(id *C.git_oid, handle unsafe.Pointer) int { + data, ok := pointerHandles.Get(handle).(*foreachData) + + if !ok { + panic("could not retrieve handle") + } err := data.callback(newOidFromC(id)) if err != nil { @@ -111,15 +115,18 @@ func odbForEachCb(id *C.git_oid, payload unsafe.Pointer) int { } func (v *Odb) ForEach(callback OdbForEachCallback) error { - data := foreachData { + data := foreachData{ callback: callback, - err: nil, + err: nil, } runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C._go_git_odb_foreach(v.ptr, unsafe.Pointer(&data)) + handle := pointerHandles.Track(&data) + defer pointerHandles.Untrack(handle) + + ret := C._go_git_odb_foreach(v.ptr, handle) if ret == C.GIT_EUSER { return data.err } else if ret < 0 { @@ -138,7 +145,7 @@ func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_odb_hash(oid.toC(), ptr, C.size_t(header.Len), C.git_otype(otype)); + ret := C.git_odb_hash(oid.toC(), ptr, C.size_t(header.Len), C.git_otype(otype)) if ret < 0 { return nil, MakeGitError(ret) } diff --git a/odb_test.go b/odb_test.go index 14a3658..2fb6840 100644 --- a/odb_test.go +++ b/odb_test.go @@ -1,15 +1,15 @@ package git import ( - "io" - "os" "errors" + "io" "testing" ) func TestOdbStream(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) + _, _ = seedTestRepo(t, repo) odb, error := repo.Odb() @@ -37,8 +37,9 @@ func TestOdbStream(t *testing.T) { func TestOdbHash(t *testing.T) { - repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + _, _ = seedTestRepo(t, repo) odb, error := repo.Odb() @@ -64,7 +65,8 @@ Initial commit.` func TestOdbForeach(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) + _, _ = seedTestRepo(t, repo) odb, err := repo.Odb() @@ -79,7 +81,7 @@ func TestOdbForeach(t *testing.T) { checkFatal(t, err) if count != expect { - t.Fatalf("Expected %v objects, got %v") + t.Fatalf("Expected %v objects, got %v", expect, count) } expect = 1 diff --git a/packbuilder.go b/packbuilder.go index 7c94926..4dc352c 100644 --- a/packbuilder.go +++ b/packbuilder.go @@ -110,8 +110,13 @@ type packbuilderCbData struct { } //export packbuilderForEachCb -func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, payload unsafe.Pointer) int { - data := (*packbuilderCbData)(payload) +func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, handle unsafe.Pointer) int { + payload := pointerHandles.Get(handle) + data, ok := payload.(*packbuilderCbData) + if !ok { + panic("could not get packbuilder CB data") + } + slice := C.GoBytes(buf, C.int(size)) err := data.callback(slice) @@ -128,13 +133,15 @@ func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, payload unsafe.Poin func (pb *Packbuilder) ForEach(callback PackbuilderForeachCallback) error { data := packbuilderCbData{ callback: callback, - err: nil, + err: nil, } + handle := pointerHandles.Track(&data) + defer pointerHandles.Untrack(handle) runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C._go_git_packbuilder_foreach(pb.ptr, unsafe.Pointer(&data)) + err := C._go_git_packbuilder_foreach(pb.ptr, handle) if err == C.GIT_EUSER { return data.err } diff --git a/patch_test.go b/patch_test.go index 569eac2..2d52fb4 100644 --- a/patch_test.go +++ b/patch_test.go @@ -7,8 +7,7 @@ import ( func TestPatch(t *testing.T) { repo := createTestRepo(t) - defer repo.Free() - //defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) _, originalTreeId := seedTestRepo(t, repo) originalTree, err := repo.LookupTree(originalTreeId) @@ -20,7 +19,11 @@ func TestPatch(t *testing.T) { newTree, err := repo.LookupTree(newTreeId) checkFatal(t, err) - diff, err := repo.DiffTreeToTree(originalTree, newTree, nil) + opts := &DiffOptions{ + OldPrefix: "a", + NewPrefix: "b", + } + diff, err := repo.DiffTreeToTree(originalTree, newTree, opts) checkFatal(t, err) patch, err := diff.Patch(0) diff --git a/push_test.go b/push_test.go index cd708c6..e36e407 100644 --- a/push_test.go +++ b/push_test.go @@ -1,15 +1,15 @@ package git import ( - "os" "testing" ) func TestRemotePush(t *testing.T) { repo := createBareTestRepo(t) - defer os.RemoveAll(repo.Path()) + defer cleanupTestRepo(t, repo) + localRepo := createTestRepo(t) - defer os.RemoveAll(localRepo.Workdir()) + defer cleanupTestRepo(t, localRepo) remote, err := localRepo.CreateRemote("test_push", repo.Path()) checkFatal(t, err) diff --git a/reference.go b/reference.go index 407334c..ac3580c 100644 --- a/reference.go +++ b/reference.go @@ -36,7 +36,10 @@ func (v *Reference) SetSymbolicTarget(target string, sig *Signature, msg string) runtime.LockOSThread() defer runtime.UnlockOSThread() - csig := sig.toC() + csig, err := sig.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(csig) var cmsg *C.char @@ -61,7 +64,10 @@ func (v *Reference) SetTarget(target *Oid, sig *Signature, msg string) (*Referen runtime.LockOSThread() defer runtime.UnlockOSThread() - csig := sig.toC() + csig, err := sig.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(csig) var cmsg *C.char @@ -99,7 +105,10 @@ func (v *Reference) Rename(name string, force bool, sig *Signature, msg string) cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) - csig := sig.toC() + csig, err := sig.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(csig) var cmsg *C.char diff --git a/reference_test.go b/reference_test.go index c7d52fb..5720a66 100644 --- a/reference_test.go +++ b/reference_test.go @@ -1,7 +1,6 @@ package git import ( - "os" "runtime" "sort" "testing" @@ -10,7 +9,7 @@ import ( func TestRefModification(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) commitId, treeId := seedTestRepo(t, repo) @@ -62,7 +61,7 @@ func TestRefModification(t *testing.T) { func TestReferenceIterator(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) loc, err := time.LoadLocation("Europe/Berlin") checkFatal(t, err) @@ -140,7 +139,8 @@ func TestReferenceIterator(t *testing.T) { func TestReferenceOwner(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) + commitId, _ := seedTestRepo(t, repo) ref, err := repo.CreateReference("refs/heads/foo", commitId, true, nil, "") @@ -158,7 +158,7 @@ func TestReferenceOwner(t *testing.T) { func TestUtil(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) commitId, _ := seedTestRepo(t, repo) @@ -199,8 +199,7 @@ func checkRefType(t *testing.T, ref *Reference, kind ReferenceType) { // The failure happens at wherever we were called, not here _, file, line, ok := runtime.Caller(1) if !ok { - t.Fatal() + t.Fatalf("Unable to get caller") } - t.Fatalf("Wrong ref type at %v:%v; have %v, expected %v", file, line, ref.Type(), kind) } @@ -129,12 +129,12 @@ func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallb return } C._go_git_setup_callbacks(ptr) - ptr.payload = unsafe.Pointer(callbacks) + ptr.payload = pointerHandles.Track(callbacks) } //export sidebandProgressCallback func sidebandProgressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int { - callbacks := (*RemoteCallbacks)(data) + callbacks := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.SidebandProgressCallback == nil { return 0 } @@ -144,7 +144,7 @@ func sidebandProgressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int //export completionCallback func completionCallback(completion_type C.git_remote_completion_type, data unsafe.Pointer) int { - callbacks := (*RemoteCallbacks)(data) + callbacks := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.CompletionCallback == nil { return 0 } @@ -153,7 +153,7 @@ func completionCallback(completion_type C.git_remote_completion_type, data unsaf //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) + callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.CredentialsCallback == nil { return 0 } @@ -166,7 +166,7 @@ func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C //export transferProgressCallback func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int { - callbacks := (*RemoteCallbacks)(data) + callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.TransferProgressCallback == nil { return 0 } @@ -175,7 +175,7 @@ func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointe //export updateTipsCallback func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data unsafe.Pointer) int { - callbacks := (*RemoteCallbacks)(data) + callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.UpdateTipsCallback == nil { return 0 } @@ -187,7 +187,7 @@ func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data uns //export certificateCheckCallback func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, data unsafe.Pointer) int { - callbacks := (*RemoteCallbacks)(data) + callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) // if there's no callback set, we need to make sure we fail if the library didn't consider this cert valid if callbacks.CertificateCheckCallback == nil { if _valid == 1 { @@ -228,7 +228,7 @@ func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, da //export packProgressCallback func packProgressCallback(stage C.int, current, total C.uint, data unsafe.Pointer) int { - callbacks := (*RemoteCallbacks)(data) + callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.PackProgressCallback == nil { return 0 @@ -239,7 +239,7 @@ func packProgressCallback(stage C.int, current, total C.uint, data unsafe.Pointe //export pushTransferProgressCallback func pushTransferProgressCallback(current, total C.uint, bytes C.size_t, data unsafe.Pointer) int { - callbacks := (*RemoteCallbacks)(data) + callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.PushTransferProgressCallback == nil { return 0 } @@ -249,7 +249,7 @@ func pushTransferProgressCallback(current, total C.uint, bytes C.size_t, data un //export pushUpdateReferenceCallback func pushUpdateReferenceCallback(refname, status *C.char, data unsafe.Pointer) int { - callbacks := (*RemoteCallbacks)(data) + callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.PushUpdateReferenceCallback == nil { return 0 @@ -286,6 +286,12 @@ func (r *Remote) SetCallbacks(callbacks *RemoteCallbacks) error { func (r *Remote) Free() { runtime.SetFinalizer(r, nil) + + callbacks := C.git_remote_get_callbacks(r.ptr) + if callbacks != nil && callbacks.payload != nil { + pointerHandles.Untrack(callbacks.payload) + } + C.git_remote_free(r.ptr) } @@ -604,7 +610,10 @@ func (o *Remote) Fetch(refspecs []string, sig *Signature, msg string) error { var csig *C.git_signature = nil if sig != nil { - csig = sig.toC() + csig, err := sig.toC() + if err != nil { + return err + } defer C.git_signature_free(csig) } @@ -696,7 +705,10 @@ func (o *Remote) Ls(filterRefs ...string) ([]RemoteHead, error) { func (o *Remote) Push(refspecs []string, opts *PushOptions, sig *Signature, msg string) error { var csig *C.git_signature = nil if sig != nil { - csig = sig.toC() + csig, err := sig.toC() + if err != nil { + return err + } defer C.git_signature_free(csig) } diff --git a/remote_test.go b/remote_test.go index 54a66ed..25ee13d 100644 --- a/remote_test.go +++ b/remote_test.go @@ -2,15 +2,13 @@ package git import ( "fmt" - "os" "testing" "time" ) func TestRefspecs(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) - defer repo.Free() + defer cleanupTestRepo(t, repo) remote, err := repo.CreateAnonymousRemote("git://foo/bar", "refs/heads/*:refs/heads/*") checkFatal(t, err) @@ -31,8 +29,7 @@ func TestRefspecs(t *testing.T) { func TestListRemotes(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) - defer repo.Free() + defer cleanupTestRepo(t, repo) _, err := repo.CreateRemote("test", "git://foo/bar") @@ -59,8 +56,7 @@ func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T func TestCertificateCheck(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) - defer repo.Free() + defer cleanupTestRepo(t, repo) remote, err := repo.CreateRemote("origin", "https://github.com/libgit2/TestGitRepository") checkFatal(t, err) @@ -79,8 +75,7 @@ func TestCertificateCheck(t *testing.T) { func TestRemoteConnect(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) - defer repo.Free() + defer cleanupTestRepo(t, repo) remote, err := repo.CreateRemote("origin", "https://github.com/libgit2/TestGitRepository") checkFatal(t, err) @@ -91,8 +86,7 @@ func TestRemoteConnect(t *testing.T) { func TestRemoteLs(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) - defer repo.Free() + defer cleanupTestRepo(t, repo) remote, err := repo.CreateRemote("origin", "https://github.com/libgit2/TestGitRepository") checkFatal(t, err) @@ -110,8 +104,7 @@ func TestRemoteLs(t *testing.T) { func TestRemoteLsFiltering(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) - defer repo.Free() + defer cleanupTestRepo(t, repo) remote, err := repo.CreateRemote("origin", "https://github.com/libgit2/TestGitRepository") checkFatal(t, err) @@ -137,8 +130,7 @@ func TestRemoteLsFiltering(t *testing.T) { func TestRemotePruneRefs(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) - defer repo.Free() + defer cleanupTestRepo(t, repo) config, err := repo.Config() checkFatal(t, err) @@ -160,8 +152,7 @@ func TestRemotePruneRefs(t *testing.T) { func TestRemotePrune(t *testing.T) { remoteRepo := createTestRepo(t) - defer os.RemoveAll(remoteRepo.Workdir()) - defer remoteRepo.Free() + defer cleanupTestRepo(t, remoteRepo) head, _ := seedTestRepo(t, remoteRepo) commit, err := remoteRepo.LookupCommit(head) @@ -178,8 +169,7 @@ func TestRemotePrune(t *testing.T) { checkFatal(t, err) repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) - defer repo.Free() + defer cleanupTestRepo(t, repo) config, err := repo.Config() checkFatal(t, err) diff --git a/repository.go b/repository.go index 5f51058..5c869ec 100644 --- a/repository.go +++ b/repository.go @@ -210,7 +210,10 @@ func (v *Repository) SetHead(refname string, sig *Signature, msg string) error { cname := C.CString(refname) defer C.free(unsafe.Pointer(cname)) - csig := sig.toC() + csig, err := sig.toC() + if err != nil { + return err + } defer C.git_signature_free(csig) var cmsg *C.char @@ -230,7 +233,10 @@ func (v *Repository) SetHead(refname string, sig *Signature, msg string) error { } func (v *Repository) SetHeadDetached(id *Oid, sig *Signature, msg string) error { - csig := sig.toC() + csig, err := sig.toC() + if err != nil { + return err + } defer C.git_signature_free(csig) var cmsg *C.char @@ -253,7 +259,10 @@ func (v *Repository) CreateReference(name string, id *Oid, force bool, sig *Sign cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) - csig := sig.toC() + csig, err := sig.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(csig) var cmsg *C.char @@ -284,7 +293,10 @@ func (v *Repository) CreateSymbolicReference(name, target string, force bool, si ctarget := C.CString(target) defer C.free(unsafe.Pointer(ctarget)) - csig := sig.toC() + csig, err := sig.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(csig) var cmsg *C.char @@ -352,10 +364,16 @@ func (v *Repository) CreateCommit( parentsarg = &cparents[0] } - authorSig := author.toC() + authorSig, err := author.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(authorSig) - committerSig := committer.toC() + committerSig, err := committer.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(committerSig) runtime.LockOSThread() @@ -384,7 +402,10 @@ func (v *Repository) CreateTag( cmessage := C.CString(message) defer C.free(unsafe.Pointer(cmessage)) - taggerSig := tagger.toC() + taggerSig, err := tagger.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(taggerSig) ctarget := commit.gitObject.ptr @@ -546,10 +567,16 @@ func (v *Repository) CreateNote( defer C.free(unsafe.Pointer(cref)) } - authorSig := author.toC() + authorSig, err := author.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(authorSig) - committerSig := committer.toC() + committerSig, err := committer.toC() + if err != nil { + return nil, err + } defer C.git_signature_free(committerSig) cnote := C.CString(note) @@ -601,10 +628,16 @@ func (v *Repository) RemoveNote(ref string, author, committer *Signature, id *Oi defer C.free(unsafe.Pointer(cref)) } - authorSig := author.toC() + authorSig, err := author.toC() + if err != nil { + return err + } defer C.git_signature_free(authorSig) - committerSig := committer.toC() + committerSig, err := committer.toC() + if err != nil { + return err + } defer C.git_signature_free(committerSig) runtime.LockOSThread() @@ -630,3 +663,36 @@ func (v *Repository) DefaultNoteRef() (string, error) { return C.GoString(ptr), nil } + +type RepositoryState int + +const ( + RepositoryStateNone RepositoryState = C.GIT_REPOSITORY_STATE_NONE + RepositoryStateMerge RepositoryState = C.GIT_REPOSITORY_STATE_MERGE + RepositoryStateRevert RepositoryState = C.GIT_REPOSITORY_STATE_REVERT + RepositoryStateCherrypick RepositoryState = C.GIT_REPOSITORY_STATE_CHERRYPICK + RepositoryStateBisect RepositoryState = C.GIT_REPOSITORY_STATE_BISECT + RepositoryStateRebase RepositoryState = C.GIT_REPOSITORY_STATE_REBASE + RepositoryStateRebaseInteractive RepositoryState = C.GIT_REPOSITORY_STATE_REBASE_INTERACTIVE + RepositoryStateRebaseMerge RepositoryState = C.GIT_REPOSITORY_STATE_REBASE_MERGE + RepositoryStateApplyMailbox RepositoryState = C.GIT_REPOSITORY_STATE_APPLY_MAILBOX + RepositoryStateApplyMailboxOrRebase RepositoryState = C.GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE +) + +func (r *Repository) State() RepositoryState { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + return RepositoryState(C.git_repository_state(r.ptr)) +} + +func (r *Repository) StateCleanup() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cErr := C.git_repository_state_cleanup(r.ptr) + if cErr < 0 { + return MakeGitError(cErr) + } + return nil +} diff --git a/revparse_test.go b/revparse_test.go index c046a20..2ccdca2 100644 --- a/revparse_test.go +++ b/revparse_test.go @@ -1,13 +1,12 @@ package git import ( - "os" "testing" ) func TestRevparse(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) commitId, _ := seedTestRepo(t, repo) @@ -19,7 +18,7 @@ func TestRevparse(t *testing.T) { func TestRevparseSingle(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) commitId, _ := seedTestRepo(t, repo) @@ -31,7 +30,7 @@ func TestRevparseSingle(t *testing.T) { func TestRevparseExt(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) _, treeId := seedTestRepo(t, repo) diff --git a/script/build-libgit2-static.sh b/script/build-libgit2-static.sh deleted file mode 100755 index 5723721..0000000 --- a/script/build-libgit2-static.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -set -ex - -VENDORED_PATH=vendor/libgit2 - -cd $VENDORED_PATH && -mkdir -p install/lib && -mkdir -p build && -cd build && -cmake -DTHREADSAFE=ON \ - -DBUILD_CLAR=OFF \ - -DBUILD_SHARED_LIBS=OFF \ - -DCMAKE_C_FLAGS=-fPIC \ - -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ - -DCMAKE_INSTALL_PREFIX=../install \ - .. && - -cmake --build . diff --git a/script/with-static.sh b/script/with-static.sh deleted file mode 100755 index 3f60e31..0000000 --- a/script/with-static.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -set -ex - -export BUILD="$PWD/vendor/libgit2/build" -export PCFILE="$BUILD/libgit2.pc" - -FLAGS=$(pkg-config --static --libs $PCFILE) || exit 1 -export CGO_LDFLAGS="$BUILD/libgit2.a -L$BUILD ${FLAGS}" -export CGO_CFLAGS="-I$PWD/vendor/libgit2/include" - -$@ diff --git a/signature.go b/signature.go new file mode 100644 index 0000000..0518387 --- /dev/null +++ b/signature.go @@ -0,0 +1,73 @@ +package git + +/* +#include <git2.h> +*/ +import "C" +import ( + "runtime" + "time" + "unsafe" +) + +type Signature struct { + Name string + Email string + When time.Time +} + +func newSignatureFromC(sig *C.git_signature) *Signature { + // git stores minutes, go wants seconds + loc := time.FixedZone("", int(sig.when.offset)*60) + return &Signature{ + C.GoString(sig.name), + C.GoString(sig.email), + time.Unix(int64(sig.when.time), 0).In(loc), + } +} + +// the offset in mintes, which is what git wants +func (v *Signature) Offset() int { + _, offset := v.When.Zone() + return offset / 60 +} + +func (sig *Signature) toC() (*C.git_signature, error) { + if sig == nil { + return nil, nil + } + + var out *C.git_signature + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + name := C.CString(sig.Name) + defer C.free(unsafe.Pointer(name)) + + email := C.CString(sig.Email) + defer C.free(unsafe.Pointer(email)) + + ret := C.git_signature_new(&out, name, email, C.git_time_t(sig.When.Unix()), C.int(sig.Offset())) + if ret < 0 { + return nil, MakeGitError(ret) + } + + return out, nil +} + +func (repo *Repository) DefaultSignature() (*Signature, error) { + var out *C.git_signature + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cErr := C.git_signature_default(&out, repo.ptr) + if cErr < 0 { + return nil, MakeGitError(cErr) + } + + defer C.git_signature_free(out) + + return newSignatureFromC(out), nil +} @@ -45,7 +45,7 @@ func statusEntryFromC(statusEntry *C.git_status_entry) StatusEntry { indexToWorkdir = diffDeltaFromC(statusEntry.index_to_workdir) } - return StatusEntry { + return StatusEntry{ Status: Status(statusEntry.status), HeadToIndex: headToIndex, IndexToWorkdir: indexToWorkdir, @@ -96,20 +96,20 @@ func (statusList *StatusList) EntryCount() (int, error) { type StatusOpt int const ( - StatusOptIncludeUntracked StatusOpt = C.GIT_STATUS_OPT_INCLUDE_UNTRACKED - StatusOptIncludeIgnored StatusOpt = C.GIT_STATUS_OPT_INCLUDE_IGNORED - StatusOptIncludeUnmodified StatusOpt = C.GIT_STATUS_OPT_INCLUDE_UNMODIFIED - StatusOptExcludeSubmodules StatusOpt = C.GIT_STATUS_OPT_EXCLUDE_SUBMODULES - StatusOptRecurseUntrackedDirs StatusOpt = C.GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS - StatusOptDisablePathspecMatch StatusOpt = C.GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH - StatusOptRecurseIgnoredDirs StatusOpt = C.GIT_STATUS_OPT_RECURSE_IGNORED_DIRS - StatusOptRenamesHeadToIndex StatusOpt = C.GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX - StatusOptRenamesIndexToWorkdir StatusOpt = C.GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR - StatusOptSortCaseSensitively StatusOpt = C.GIT_STATUS_OPT_SORT_CASE_SENSITIVELY - StatusOptSortCaseInsensitively StatusOpt = C.GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY - StatusOptRenamesFromRewrites StatusOpt = C.GIT_STATUS_OPT_RENAMES_FROM_REWRITES - StatusOptNoRefresh StatusOpt = C.GIT_STATUS_OPT_NO_REFRESH - StatusOptUpdateIndex StatusOpt = C.GIT_STATUS_OPT_UPDATE_INDEX + StatusOptIncludeUntracked StatusOpt = C.GIT_STATUS_OPT_INCLUDE_UNTRACKED + StatusOptIncludeIgnored StatusOpt = C.GIT_STATUS_OPT_INCLUDE_IGNORED + StatusOptIncludeUnmodified StatusOpt = C.GIT_STATUS_OPT_INCLUDE_UNMODIFIED + StatusOptExcludeSubmodules StatusOpt = C.GIT_STATUS_OPT_EXCLUDE_SUBMODULES + StatusOptRecurseUntrackedDirs StatusOpt = C.GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS + StatusOptDisablePathspecMatch StatusOpt = C.GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH + StatusOptRecurseIgnoredDirs StatusOpt = C.GIT_STATUS_OPT_RECURSE_IGNORED_DIRS + StatusOptRenamesHeadToIndex StatusOpt = C.GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX + StatusOptRenamesIndexToWorkdir StatusOpt = C.GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR + StatusOptSortCaseSensitively StatusOpt = C.GIT_STATUS_OPT_SORT_CASE_SENSITIVELY + StatusOptSortCaseInsensitively StatusOpt = C.GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY + StatusOptRenamesFromRewrites StatusOpt = C.GIT_STATUS_OPT_RENAMES_FROM_REWRITES + StatusOptNoRefresh StatusOpt = C.GIT_STATUS_OPT_NO_REFRESH + StatusOptUpdateIndex StatusOpt = C.GIT_STATUS_OPT_UPDATE_INDEX ) type StatusShow int @@ -173,7 +173,6 @@ func (v *Repository) StatusList(opts *StatusOptions) (*StatusList, error) { return newStatusListFromC(ptr), nil } - func (v *Repository) StatusFile(path string) (Status, error) { var statusFlags C.uint cPath := C.CString(path) diff --git a/status_test.go b/status_test.go index 4be4824..5b97b00 100644 --- a/status_test.go +++ b/status_test.go @@ -2,15 +2,18 @@ package git import ( "io/ioutil" - "os" "path" "testing" ) func TestStatusFile(t *testing.T) { repo := createTestRepo(t) - defer repo.Free() - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) + + state := repo.State() + if state != RepositoryStateNone { + t.Fatal("Incorrect repository state: ", state) + } err := ioutil.WriteFile(path.Join(path.Dir(repo.Workdir()), "hello.txt"), []byte("Hello, World"), 0644) checkFatal(t, err) @@ -25,10 +28,10 @@ func TestStatusFile(t *testing.T) { func TestStatusList(t *testing.T) { repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + // This commits the test repo README, so it doesn't show up in the status list and there's a head to compare to seedTestRepo(t, repo) - defer repo.Free() - defer os.RemoveAll(repo.Workdir()) err := ioutil.WriteFile(path.Join(path.Dir(repo.Workdir()), "hello.txt"), []byte("Hello, World"), 0644) checkFatal(t, err) diff --git a/submodule.go b/submodule.go index f3e9e83..3882462 100644 --- a/submodule.go +++ b/submodule.go @@ -97,17 +97,24 @@ func (repo *Repository) LookupSubmodule(name string) (*Submodule, error) { type SubmoduleCbk func(sub *Submodule, name string) int //export SubmoduleVisitor -func SubmoduleVisitor(csub unsafe.Pointer, name string, cfct unsafe.Pointer) int { +func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { sub := &Submodule{(*C.git_submodule)(csub)} - fct := *(*SubmoduleCbk)(cfct) - return fct(sub, name) + + if callback, ok := pointerHandles.Get(handle).(SubmoduleCbk); ok { + return (C.int)(callback(sub, C.GoString(name))) + } else { + panic("invalid submodule visitor callback") + } } func (repo *Repository) ForeachSubmodule(cbk SubmoduleCbk) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C._go_git_visit_submodule(repo.ptr, unsafe.Pointer(&cbk)) + handle := pointerHandles.Track(cbk) + defer pointerHandles.Untrack(handle) + + ret := C._go_git_visit_submodule(repo.ptr, handle) if ret < 0 { return MakeGitError(ret) } @@ -318,7 +325,10 @@ func (repo *Repository) ReloadAllSubmodules(force bool) error { func (sub *Submodule) Update(init bool, opts *SubmoduleUpdateOptions) error { var copts C.git_submodule_update_options - populateSubmoduleUpdateOptions(&copts, opts) + err := populateSubmoduleUpdateOptions(&copts, opts) + if err != nil { + return err + } runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -331,15 +341,22 @@ func (sub *Submodule) Update(init bool, opts *SubmoduleUpdateOptions) error { return nil } -func populateSubmoduleUpdateOptions(ptr *C.git_submodule_update_options, opts *SubmoduleUpdateOptions) { +func populateSubmoduleUpdateOptions(ptr *C.git_submodule_update_options, opts *SubmoduleUpdateOptions) error { C.git_submodule_update_init_options(ptr, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION) if opts == nil { - return + return nil } populateCheckoutOpts(&ptr.checkout_opts, opts.CheckoutOpts) populateRemoteCallbacks(&ptr.remote_callbacks, opts.RemoteCallbacks) ptr.clone_checkout_strategy = C.uint(opts.CloneCheckoutStrategy) - ptr.signature = opts.Signature.toC() + + sig, err := opts.Signature.toC() + if err != nil { + return err + } + ptr.signature = sig + + return nil } diff --git a/submodule_test.go b/submodule_test.go new file mode 100644 index 0000000..27bc193 --- /dev/null +++ b/submodule_test.go @@ -0,0 +1,26 @@ +package git + +import ( + "testing" +) + +func TestSubmoduleForeach(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + seedTestRepo(t, repo) + + _, err := repo.AddSubmodule("http://example.org/submodule", "submodule", true) + checkFatal(t, err) + + i := 0 + err = repo.ForeachSubmodule(func(sub *Submodule, name string) int { + i++ + return 0 + }) + checkFatal(t, err) + + if i != 1 { + t.Fatalf("expected one submodule found but got %d", i) + } +} diff --git a/tag_test.go b/tag_test.go index 5ebd53d..74f9fec 100644 --- a/tag_test.go +++ b/tag_test.go @@ -1,14 +1,14 @@ package git import ( - "os" - "time" "testing" + "time" ) func TestCreateTag(t *testing.T) { repo := createTestRepo(t) - defer os.RemoveAll(repo.Workdir()) + defer cleanupTestRepo(t, repo) + commitId, _ := seedTestRepo(t, repo) commit, err := repo.LookupCommit(commitId) @@ -90,22 +90,28 @@ func (t Tree) EntryCount() uint64 { type TreeWalkCallback func(string, *TreeEntry) int //export CallbackGitTreeWalk -func CallbackGitTreeWalk(_root unsafe.Pointer, _entry unsafe.Pointer, ptr unsafe.Pointer) C.int { - root := C.GoString((*C.char)(_root)) +func CallbackGitTreeWalk(_root *C.char, _entry unsafe.Pointer, ptr unsafe.Pointer) C.int { + root := C.GoString(_root) entry := (*C.git_tree_entry)(_entry) - callback := *(*TreeWalkCallback)(ptr) - return C.int(callback(root, newTreeEntry(entry))) + if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok { + return C.int(callback(root, newTreeEntry(entry))) + } else { + panic("invalid treewalk callback") + } } func (t Tree) Walk(callback TreeWalkCallback) error { runtime.LockOSThread() defer runtime.UnlockOSThread() + ptr := pointerHandles.Track(callback) + defer pointerHandles.Untrack(ptr) + err := C._go_git_treewalk( t.cast_ptr, C.GIT_TREEWALK_PRE, - unsafe.Pointer(&callback), + ptr, ) if err < 0 { diff --git a/vendor/libgit2 b/vendor/libgit2 deleted file mode 160000 -Subproject 04bdd97f2b63793a8720fd19007911e946ba3c5 @@ -62,6 +62,27 @@ int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLin return git_diff_foreach(diff, fcb, hcb, lcb, payload); } +int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload) +{ + git_diff_file_cb fcb = NULL; + git_diff_hunk_cb hcb = NULL; + git_diff_line_cb lcb = NULL; + + if (eachFile) { + fcb = (git_diff_file_cb)&diffForEachFileCb; + } + + if (eachHunk) { + hcb = (git_diff_hunk_cb)&diffForEachHunkCb; + } + + if (eachLine) { + lcb = (git_diff_line_cb)&diffForEachLineCb; + } + + return git_diff_blobs(old, old_path, new, new_path, opts, fcb, hcb, lcb, payload); +} + void _go_git_setup_diff_notify_callbacks(git_diff_options *opts) { opts->notify_cb = (git_diff_notify_cb)diffNotifyCb; } |
