Skip to content

libgit2 v1.2.0 #major #777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/backport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
branch: [ 'release-1.0', 'release-0.28', 'release-0.27' ]
branch: [ 'release-1.1', 'release-1.0', 'release-0.28', 'release-0.27' ]

runs-on: ubuntu-20.04

Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ jobs:
fail-fast: false
matrix:
libgit2:
- 'v1.1.0'
- 'v1.2.0'
- '109b4c887ffb63962c7017a66fc4a1f48becb48e' # v1.2.0 with a fixed symbol
name: Go (system-wide, dynamic)

runs-on: ubuntu-20.04
Expand Down
5 changes: 3 additions & 2 deletions Build_bundled_static.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build static && !system_libgit2
// +build static,!system_libgit2

package git
Expand All @@ -9,8 +10,8 @@ package git
#cgo CFLAGS: -DLIBGIT2_STATIC
#include <git2.h>

#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 1 || LIBGIT2_VER_MINOR > 2
# error "Invalid libgit2 version; this git2go supports libgit2 between v1.1.0 and v1.2.0"
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 2 || LIBGIT2_VER_MINOR > 2
# error "Invalid libgit2 version; this git2go supports libgit2 between v1.2.0 and v1.2.0"
#endif
*/
import "C"
5 changes: 3 additions & 2 deletions Build_system_dynamic.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !static
// +build !static

package git
Expand All @@ -7,8 +8,8 @@ package git
#cgo CFLAGS: -DLIBGIT2_DYNAMIC
#include <git2.h>

#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 1 || LIBGIT2_VER_MINOR > 2
# error "Invalid libgit2 version; this git2go supports libgit2 between v1.1.0 and v1.2.0"
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 2 || LIBGIT2_VER_MINOR > 2
# error "Invalid libgit2 version; this git2go supports libgit2 between v1.2.0 and v1.2.0"
#endif
*/
import "C"
5 changes: 3 additions & 2 deletions Build_system_static.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build static && system_libgit2
// +build static,system_libgit2

package git
Expand All @@ -7,8 +8,8 @@ package git
#cgo CFLAGS: -DLIBGIT2_STATIC
#include <git2.h>

#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 1 || LIBGIT2_VER_MINOR > 2
# error "Invalid libgit2 version; this git2go supports libgit2 between v1.1.0 and v1.2.0"
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 2 || LIBGIT2_VER_MINOR > 2
# error "Invalid libgit2 version; this git2go supports libgit2 between v1.2.0 and v1.2.0"
#endif
*/
import "C"
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ Due to the fact that Go 1.11 module versions have semantic meaning and don't nec

| libgit2 | git2go |
|---------|---------------|
| main | (will be v32) |
| main | (will be v33) |
| 1.2 | v32 |
| 1.1 | v31 |
| 1.0 | v30 |
| 0.99 | v29 |
| 0.28 | v28 |
| 0.27 | v27 |

You can import them in your project with the version's major number as a suffix. For example, if you have libgit2 v1.1 installed, you'd import git2go v31 with:
You can import them in your project with the version's major number as a suffix. For example, if you have libgit2 v1.2 installed, you'd import git2go v32 with:

```sh
go get github.com/libgit2/git2go/v31
go get github.com/libgit2/git2go/v32
```
```go
import "github.com/libgit2/git2go/v31"
import "github.com/libgit2/git2go/v32"
```

which will ensure there are no sudden changes to the API.
Expand All @@ -44,10 +45,10 @@ This project wraps the functionality provided by libgit2. If you're using a vers

### Versioned branch, dynamic linking

When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via Go modules, e.g. to work against libgit2 v1.1
When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via Go modules, e.g. to work against libgit2 v1.2

```go
import "github.com/libgit2/git2go/v31"
import "github.com/libgit2/git2go/v32"
```

### Versioned branch, static linking
Expand Down Expand Up @@ -77,7 +78,7 @@ In order to let Go pass the correct flags to `pkg-config`, `-tags static` needs

One thing to take into account is that since Go expects the `pkg-config` file to be within the same directory where `make install-static` was called, so the `go.mod` file may need to have a [`replace` directive](https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive) so that the correct setup is achieved. So if `git2go` is checked out at `$GOPATH/src/github.com/libgit2/git2go` and your project at `$GOPATH/src/github.com/my/project`, the `go.mod` file of `github.com/my/project` might need to have a line like

replace github.com/libgit2/git2go/v31 ../../libgit2/git2go
replace github.com/libgit2/git2go/v32 ../../libgit2/git2go

Parallelism and network operations
----------------------------------
Expand Down
11 changes: 5 additions & 6 deletions checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extern void _go_git_populate_checkout_callbacks(git_checkout_options *opts);
*/
import "C"
import (
"errors"
"os"
"runtime"
"unsafe"
Expand Down Expand Up @@ -49,8 +48,8 @@ const (
CheckoutUpdateSubmodulesIfChanged CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED // Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED)
)

type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) ErrorCode
type CheckoutProgressCallback func(path string, completed, total uint) ErrorCode
type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) error
type CheckoutProgressCallback func(path string, completed, total uint)

type CheckoutOptions struct {
Strategy CheckoutStrategy // Default will be a dry run
Expand Down Expand Up @@ -116,9 +115,9 @@ func checkoutNotifyCallback(
if data.options.NotifyCallback == nil {
return C.int(ErrorCodeOK)
}
ret := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir)
if ret < 0 {
*data.errorTarget = errors.New(ErrorCode(ret).String())
err := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir)
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
Expand Down
20 changes: 9 additions & 11 deletions cherrypick.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ import (
)

type CherrypickOptions struct {
Version uint
Mainline uint
MergeOpts MergeOptions
CheckoutOpts CheckoutOptions
Mainline uint
MergeOptions MergeOptions
CheckoutOptions CheckoutOptions
}

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),
Mainline: uint(c.mainline),
MergeOptions: mergeOptionsFromC(&c.merge_opts),
CheckoutOptions: checkoutOptionsFromC(&c.checkout_opts),
}
return opts
}
Expand All @@ -31,8 +29,8 @@ func populateCherrypickOptions(copts *C.git_cherrypick_options, opts *Cherrypick
return nil
}
copts.mainline = C.uint(opts.Mainline)
populateMergeOptions(&copts.merge_opts, &opts.MergeOpts)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOpts, errorTarget)
populateMergeOptions(&copts.merge_opts, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
return copts
}

Expand Down Expand Up @@ -82,7 +80,7 @@ func (r *Repository) CherrypickCommit(pick, our *Commit, opts CherrypickOptions)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

cOpts := populateMergeOptions(&C.git_merge_options{}, &opts.MergeOpts)
cOpts := populateMergeOptions(&C.git_merge_options{}, &opts.MergeOptions)
defer freeMergeOptions(cOpts)

var ptr *C.git_index
Expand Down
18 changes: 9 additions & 9 deletions clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ extern void _go_git_populate_clone_callbacks(git_clone_options *opts);
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
)

type RemoteCreateCallback func(repo *Repository, name, url string) (*Remote, ErrorCode)
type RemoteCreateCallback func(repo *Repository, name, url string) (*Remote, error)

type CloneOptions struct {
*CheckoutOpts
*FetchOptions
CheckoutOptions CheckoutOptions
FetchOptions FetchOptions
Bare bool
CheckoutBranch string
RemoteCreateCallback RemoteCreateCallback
Expand Down Expand Up @@ -71,9 +70,10 @@ func remoteCreateCallback(
panic("invalid remote create callback")
}

remote, ret := data.options.RemoteCreateCallback(repo, name, url)
if ret < 0 {
*data.errorTarget = errors.New(ErrorCode(ret).String())
remote, err := data.options.RemoteCreateCallback(repo, name, url)

if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
if remote == nil {
Expand All @@ -100,8 +100,8 @@ func populateCloneOptions(copts *C.git_clone_options, opts *CloneOptions, errorT
if opts == nil {
return nil
}
populateCheckoutOptions(&copts.checkout_opts, opts.CheckoutOpts, errorTarget)
populateFetchOptions(&copts.fetch_opts, opts.FetchOptions, errorTarget)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
populateFetchOptions(&copts.fetch_opts, &opts.FetchOptions, errorTarget)
copts.bare = cbool(opts.Bare)

if opts.RemoteCreateCallback != nil {
Expand Down
10 changes: 2 additions & 8 deletions clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,9 @@ func TestCloneWithCallback(t *testing.T) {

opts := CloneOptions{
Bare: true,
RemoteCreateCallback: func(r *Repository, name, url string) (*Remote, ErrorCode) {
RemoteCreateCallback: func(r *Repository, name, url string) (*Remote, error) {
testPayload += 1

remote, err := r.Remotes.Create(REMOTENAME, url)
if err != nil {
return nil, ErrorCodeGeneric
}

return remote, ErrorCodeOK
return r.Remotes.Create(REMOTENAME, url)
},
}

Expand Down
23 changes: 20 additions & 3 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ func (c *Commit) Message() string {
return ret
}

func (c *Commit) MessageEncoding() string {
ret := C.GoString(C.git_commit_message_encoding(c.cast_ptr))
func (c *Commit) MessageEncoding() MessageEncoding {
ptr := C.git_commit_message_encoding(c.cast_ptr)
if ptr == nil {
return MessageEncodingUTF8
}
ret := C.GoString(ptr)
runtime.KeepAlive(c)
return ret
return MessageEncoding(ret)
}

func (c *Commit) RawMessage() string {
Expand All @@ -64,6 +68,19 @@ func (c *Commit) ContentToSign() string {
// CommitSigningCallback defines a function type that takes some data to sign and returns (signature, signature_field, error)
type CommitSigningCallback func(string) (signature, signatureField string, err error)

// CommitCreateCallback defines a function type that is called when another
// function is going to create commits (for example, Rebase) to allow callers
// to override the commit creation behavior. For example, users may wish to
// sign commits by providing this information to Repository.CreateCommitBuffer,
// signing that buffer, then calling Repository.CreateCommitWithSignature.
type CommitCreateCallback func(
author, committer *Signature,
messageEncoding MessageEncoding,
message string,
tree *Tree,
parents ...*Commit,
) (oid *Oid, err error)

// WithSignatureUsing creates a new signed commit from this one using the given signing callback
func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
signature, signatureField, err := f(c.ContentToSign())
Expand Down
32 changes: 28 additions & 4 deletions deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,27 @@ func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer)
if !ok {
panic("invalid submodule visitor callback")
}
return (C.int)(callback(sub, C.GoString(name)))
err := callback(sub, C.GoString(name))
if err != nil {
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}

// reference.go

// Deprecated: ReferenceIsValidName is a deprecated alias of ReferenceNameIsValid.
func ReferenceIsValidName(name string) bool {
valid, _ := ReferenceNameIsValid(name)
return valid
}

// remote.go

// Deprecated: RemoteIsValidName is a deprecated alias of RemoteNameIsValid.
func RemoteIsValidName(name string) bool {
valid, _ := RemoteNameIsValid(name)
return valid
}

// tree.go
Expand All @@ -239,9 +259,13 @@ func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer)
func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int {
root := C.GoString(_root)

if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok {
return C.int(callback(root, newTreeEntry(entry)))
} else {
callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback)
if !ok {
panic("invalid treewalk callback")
}
err := callback(root, newTreeEntry(entry))
if err != nil {
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
2 changes: 1 addition & 1 deletion git.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func NewOid(s string) (*Oid, error) {
}

if len(slice) != 20 {
return nil, &GitError{"Invalid Oid", ErrorClassNone, ErrGeneric}
return nil, &GitError{"invalid oid", ErrorClassNone, ErrorCodeGeneric}
}

copy(o[:], slice[:20])
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/libgit2/git2go/v31
module github.com/libgit2/git2go/v32

go 1.13

Expand Down
9 changes: 4 additions & 5 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ extern int _go_git_index_remove_all(git_index*, const git_strarray*, void*);
*/
import "C"
import (
"errors"
"fmt"
"runtime"
"unsafe"
)

type IndexMatchedPathCallback func(string, string) int
type IndexMatchedPathCallback func(string, string) error
type indexMatchedPathCallbackData struct {
callback IndexMatchedPathCallback
errorTarget *error
Expand Down Expand Up @@ -343,9 +342,9 @@ func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Po
panic("invalid matched path callback")
}

ret := data.callback(C.GoString(cPath), C.GoString(cMatchedPathspec))
if ret < 0 {
*data.errorTarget = errors.New(ErrorCode(ret).String())
err := data.callback(C.GoString(cPath), C.GoString(cMatchedPathspec))
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}

Expand Down
4 changes: 2 additions & 2 deletions index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ func TestIndexAddAllCallback(t *testing.T) {
checkFatal(t, err)

cbPath := ""
err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) int {
err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) error {
cbPath = p
return 0
return nil
})
checkFatal(t, err)
if cbPath != "README" {
Expand Down
Loading