Compare commits

..

1 Commits

Author SHA1 Message Date
Paul Okstad ce4dd16b1f Reflog support (#467) 2021-01-18 09:00:49 -08:00
80 changed files with 1035 additions and 3059 deletions

54
.github/workflows/backport.yml vendored Normal file
View File

@ -0,0 +1,54 @@
name: Backport to older releases
on:
push:
branches:
- master
jobs:
backport:
name: Backport change to branch ${{ matrix.branch }}
continue-on-error: true
strategy:
fail-fast: false
matrix:
branch: [ 'release-1.0', 'release-0.28', 'release-0.27' ]
runs-on: ubuntu-20.04
steps:
- name: Check out code
uses: actions/checkout@v1
with:
fetch-depth: 0
- name: Create a cherry-pick PR
run: |
if ! git diff --quiet HEAD^ HEAD -- vendor/libgit2; then
echo '::warning::Skipping cherry-pick since it is a vendored libgit2 bump'
exit 0
fi
BRANCH_NAME="cherry-pick-${{ github.run_id }}-${{ matrix.branch }}"
# Setup usernames and authentication
git config --global user.name "${{ github.actor }}"
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
cat <<- EOF > $HOME/.netrc
machine github.com
login ${{ github.actor }}
password ${{ secrets.GITHUB_TOKEN }}
machine api.github.com
login ${{ github.actor }}
password ${{ secrets.GITHUB_TOKEN }}
EOF
chmod 600 $HOME/.netrc
# Create the cherry-pick commit and create the PR for it.
git checkout "${{ matrix.branch }}"
git switch -c "${BRANCH_NAME}"
git cherry-pick -x "${{ github.sha }}"
git push --set-upstream origin "${BRANCH_NAME}"
GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}" gh pr create \
--base "${{ matrix.branch }}" \
--title "$(git --no-pager show --format="%s" --no-patch HEAD)" \
--body "$(git --no-pager show --format="%b" --no-patch HEAD)"

140
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,140 @@
name: git2go CI
on:
pull_request:
push:
branches:
- master
- release-*
- v*
jobs:
build-legacy:
strategy:
fail-fast: false
matrix:
go: [ '1.9', '1.10' ]
name: Go ${{ matrix.go }}
runs-on: ubuntu-18.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go }}
id: go
- name: Check out code into the GOPATH
uses: actions/checkout@v1
with:
fetch-depth: 1
path: src/github.com/${{ github.repository }}
- name: Build
env:
GOPATH: /home/runner/work/git2go
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
make build-libgit2-static
go get -tags static -t github.com/${{ github.repository }}/...
go build -tags static github.com/${{ github.repository }}/...
- name: Test
env:
GOPATH: /home/runner/work/git2go
run: make TEST_ARGS=-test.v test-static
build-static:
strategy:
fail-fast: false
matrix:
go: [ '1.11', '1.12', '1.13', '1.14', '1.15' ]
name: Go ${{ matrix.go }}
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go }}
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
make build-libgit2-static
- name: Test
run: make TEST_ARGS=-test.v test-static
build-dynamic:
strategy:
fail-fast: false
name: Go (dynamic)
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.15'
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
make build-libgit2-dynamic
- name: Test
run: make TEST_ARGS=-test.v test-dynamic
build-system-dynamic:
strategy:
fail-fast: false
matrix:
libgit2: [ '1.1.0' ]
name: Go (system-wide, dynamic)
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.15'
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build libgit2 ${{ matrix.libgit2 }}
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
sudo env BUILD_LIBGIT_REF=v${{ matrix.libgit2 }} ./script/build-libgit2.sh --dynamic --system
- name: Test
run: make TEST_ARGS=-test.v test
build-system-static:
strategy:
fail-fast: false
name: Go (system-wide, static)
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.15'
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build libgit2
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
sudo ./script/build-libgit2.sh --static --system
- name: Test
run: go test --count=1 --tags "static,system_libgit2" ./...

28
.github/workflows/tag.yml vendored Normal file
View File

@ -0,0 +1,28 @@
name: Tag new releases
on:
push:
branches:
- master
- release-*
jobs:
tag-release:
name: Bump tag in ${{ github.ref }}
runs-on: ubuntu-20.04
steps:
- name: Check out code
uses: actions/checkout@v1
with:
fetch-depth: 0
- name: Bump version and push tag
id: bump-version
uses: anothrNick/github-tag-action@9aaabdb5e989894e95288328d8b17a6347217ae3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WITH_V: true
DEFAULT_BUMP: patch
TAG_CONTEXT: branch
RELEASE_BRANCHES: .*

2
.gitignore vendored
View File

@ -1,4 +1,2 @@
/static-build/ /static-build/
/dynamic-build/ /dynamic-build/
go.*

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "vendor/libgit2"]
path = vendor/libgit2
url = https://github.com/libgit2/libgit2

25
.travis.yml Normal file
View File

@ -0,0 +1,25 @@
language: go
arch:
- AMD64
- ppc64le
go:
- tip
install:
- sudo apt-get install -y --no-install-recommends libssh2-1-dev
- make build-libgit2-static
- go get --tags "static" ./...
script:
- make test-static
git:
submodules: true
branches:
only:
- master
- /v\d+/
- /release-.*/

View File

@ -1,17 +1,16 @@
//go:build static && !system_libgit2
// +build static,!system_libgit2 // +build static,!system_libgit2
package git package git
/* /*
#cgo windows CFLAGS: -I${SRCDIR}/static-build/install/include/ #cgo windows CFLAGS: -I${SRCDIR}/static-build/install/include/
#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp -lws2_32 -lole32 -lrpcrt4 -lcrypt32 #cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp
#cgo !windows pkg-config: --static ${SRCDIR}/static-build/install/lib/pkgconfig/libgit2.pc #cgo !windows pkg-config: --static ${SRCDIR}/static-build/install/lib/pkgconfig/libgit2.pc
#cgo CFLAGS: -DLIBGIT2_STATIC #cgo CFLAGS: -DLIBGIT2_STATIC
#include <git2.h> #include <git2.h>
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 5 || LIBGIT2_VER_MINOR > 5 #if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 1 || LIBGIT2_VER_MINOR > 1
# error "Invalid libgit2 version; this libgit2 supports libgit2 between v1.5.0 and v1.5.0" # error "Invalid libgit2 version; this git2go supports libgit2 between v1.1.0 and v1.1.0"
#endif #endif
*/ */
import "C" import "C"

View File

@ -1,14 +1,14 @@
//go:build !static
// +build !static // +build !static
package git package git
/* /*
#cgo pkg-config: libgit2 #cgo pkg-config: libgit2
#cgo CFLAGS: -DLIBGIT2_DYNAMIC -I/opt/libgit2/include #cgo CFLAGS: -DLIBGIT2_DYNAMIC
#cgo LDFLAGS: -L/opt/libgit2 -lgit2
#include <git2.h> #include <git2.h>
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 1 || LIBGIT2_VER_MINOR > 1
# error "Invalid libgit2 version; this git2go supports libgit2 between v1.1.0 and v1.1.0"
#endif
*/ */
import "C" import "C"

View File

@ -1,4 +1,3 @@
//go:build static && system_libgit2
// +build static,system_libgit2 // +build static,system_libgit2
package git package git
@ -8,8 +7,8 @@ package git
#cgo CFLAGS: -DLIBGIT2_STATIC #cgo CFLAGS: -DLIBGIT2_STATIC
#include <git2.h> #include <git2.h>
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 5 || LIBGIT2_VER_MINOR > 5 #if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 1 || LIBGIT2_VER_MINOR > 1
# error "Invalid libgit2 version; this libgit2 supports libgit2 between v1.5.0 and v1.5.0" # error "Invalid libgit2 version; this git2go supports libgit2 between v1.1.0 and v1.1.0"
#endif #endif
*/ */
import "C" import "C"

View File

@ -1,6 +1,6 @@
The MIT License The MIT License
Copyright (c) 2013 The libgit2 contributors Copyright (c) 2013 The git2go contributors
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,32 +1,17 @@
TEST_ARGS ?= --count=1 TEST_ARGS ?= --count=1
PKG_CONFIG_PATH=/opt/libgit2/ default: test
default: goimports test
goimports:
goimports -w *.go
generate: static-build/install/lib/libgit2.a
go generate --tags "static" ./...
# System library # System library
# ============== # ==============
# This uses whatever version of libgit2 can be found in the system. # This uses whatever version of libgit2 can be found in the system.
test: test:
-go-mod-clean # go install go.wit.com/apps/go-mod-clean@latest
go run script/check-MakeGitError-thread-lock.go go run script/check-MakeGitError-thread-lock.go
LD_LIBRARY_PATH=/opt/libgit2 PKG_CONFIG_PATH=/opt/libgit2/ go test -v -x $(TEST_ARGS) ./... go test $(TEST_ARGS) ./...
add-remote:
git remote add git2go https://github.com/libgit2/git2go.git
install: install:
go install ./... go install ./...
clean:
rm go.*
# Bundled dynamic library # Bundled dynamic library
# ======================= # =======================
# In order to avoid having to manipulate `git_dynamic.go`, which would prevent # In order to avoid having to manipulate `git_dynamic.go`, which would prevent

View File

@ -1,53 +1,93 @@
GO libgit2 git2go
====== ======
[![GoDoc](https://godoc.org/go.wit.com/lib/libgit2?status.svg)](http://godoc.org/go.wit.com/lib/libgit2) [![Build Status](https://travis-ci.org/libgit2/libgit2.svg?branch=main)](https://travis-ci.org/libgit2/libgit2) [![GoDoc](https://godoc.org/github.com/libgit2/git2go?status.svg)](http://godoc.org/github.com/libgit2/git2go) [![Build Status](https://travis-ci.org/libgit2/git2go.svg?branch=master)](https://travis-ci.org/libgit2/git2go)
Go bindings for [libgit2](http://libgit2.github.com/). Go bindings for [libgit2](http://libgit2.github.com/).
### Updated 2024/12/16
### Which Go version to use ### Which Go version to use
* This package is updated to work against libgit2 version 1.8 on Debian sid Due to the fact that Go 1.11 module versions have semantic meaning and don't necessarily align with libgit2's release schedule, please consult the following table for a mapping between libgit2 and git2go module versions:
* There is one line commented out which needs to be fixed in remote.go
* some of the tests seem to run | libgit2 | git2go |
|---------|---------------|
| master | (will be 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:
```sh ```sh
go install go.wit.com/apps/go-clone@latest go get github.com/libgit2/git2go/v31
go install go.wit.com/apps/go-mod-clean@latest ```
go-clone --recusive go.wit.com/lib/libgit2 ```go
import "github.com/libgit2/git2go/v31"
``` ```
which will ensure there are no sudden changes to the API.
The `master` branch follows the tip of libgit2 itself (with some lag) and as such has no guarantees on the stability of libgit2's API. Thus this only supports statically linking against libgit2.
### Which branch to send Pull requests to ### Which branch to send Pull requests to
TODO: not sure yet If there's something version-specific that you'd want to contribute to, you can send them to the `release-${MAJOR}.${MINOR}` branches, which follow libgit2's releases.
Installing Installing
---------- ----------
This project wraps the functionality provided by libgit2. It thus needs it in order to perform the work. This project wraps the functionality provided by libgit2. It thus needs it in order to perform the work.
This project wraps the functionality provided by libgit2. If you're using a versioned branch, install it to your system via your system's package manager and then install libgit2. This project wraps the functionality provided by libgit2. If you're using a versioned branch, install it to your system via your system's package manager and then install git2go.
### Versioned branch, dynamic linking ### 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.2 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
```go ```go
goimports -w *.go import "github.com/libgit2/git2go/v31"
``` ```
### Versioned branch, static linking
Follow the instructions for [Versioned branch, dynamic linking](#versioned-branch-dynamic-linking), but pass the `-tags static,system_libgit2` flag to all `go` commands that build any binaries. For instance:
go build -tags static,system_libgit2 github.com/my/project/...
go test -tags static,system_libgit2 github.com/my/project/...
go install -tags static,system_libgit2 github.com/my/project/...
### Master branch, or vendored static linking
If using `master` or building a branch with the vendored libgit2 statically, we need to build libgit2 first. In order to build it, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL (outside of Windows or macOS) and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. Note that even if libgit2 is included in the resulting binary, its dependencies will not be.
Run `go get -d github.com/libgit2/git2go` to download the code and go to your `$GOPATH/src/github.com/libgit2/git2go` directory. From there, we need to build the C code and put it into the resulting go binary.
git submodule update --init # get libgit2
make install-static
will compile libgit2, link it into git2go and install it. The `master` branch is set up to follow the specific libgit2 version that is vendored, so trying dynamic linking may or may not work depending on the exact versions involved.
In order to let Go pass the correct flags to `pkg-config`, `-tags static` needs to be passed to all `go` commands that build any binaries. For instance:
go build -tags static github.com/my/project/...
go test -tags static github.com/my/project/...
go install -tags static github.com/my/project/...
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
Parallelism and network operations Parallelism and network operations
---------------------------------- ----------------------------------
libgit2 may use OpenSSL and LibSSH2 for performing encrypted network connections. For now, libgit2 asks libgit2 to set locking for OpenSSL. This makes HTTPS connections thread-safe, but it is fragile and will likely stop doing it soon. This may also make SSH connections thread-safe if your copy of libssh2 is linked against OpenSSL. Check libgit2's `THREADSAFE.md` for more information. libgit2 may use OpenSSL and LibSSH2 for performing encrypted network connections. For now, git2go asks libgit2 to set locking for OpenSSL. This makes HTTPS connections thread-safe, but it is fragile and will likely stop doing it soon. This may also make SSH connections thread-safe if your copy of libssh2 is linked against OpenSSL. Check libgit2's `THREADSAFE.md` for more information.
Running the tests Running the tests
----------------- -----------------
For the stable version, `go test` will work as usual. For the `main` branch, similarly to installing, running the tests requires building a local libgit2 library, so the Makefile provides a wrapper that makes sure it's built For the stable version, `go test` will work as usual. For the `master` branch, similarly to installing, running the tests requires building a local libgit2 library, so the Makefile provides a wrapper that makes sure it's built
make test-static make test-static
@ -64,6 +104,6 @@ M to the I to the T. See the LICENSE file if you've never seen an MIT license be
Authors Authors
------- -------
- Carlos Martín (github@carlosmn) - Carlos Martín (@carlosmn)
- Vicent Martí (github@vmg) - Vicent Martí (@vmg)

View File

@ -87,7 +87,6 @@ func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error)
} }
type Blame struct { type Blame struct {
doNotCompare
ptr *C.git_blame ptr *C.git_blame
} }

View File

@ -15,7 +15,6 @@ import (
) )
type Blob struct { type Blob struct {
doNotCompare
Object Object
cast_ptr *C.git_blob cast_ptr *C.git_blob
} }
@ -97,7 +96,6 @@ func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, err
} }
type BlobWriteStream struct { type BlobWriteStream struct {
doNotCompare
ptr *C.git_writestream ptr *C.git_writestream
repo *Repository repo *Repository
} }

View File

@ -19,7 +19,6 @@ const (
) )
type Branch struct { type Branch struct {
doNotCompare
*Reference *Reference
} }
@ -28,7 +27,6 @@ func (r *Reference) Branch() *Branch {
} }
type BranchIterator struct { type BranchIterator struct {
doNotCompare
ptr *C.git_branch_iterator ptr *C.git_branch_iterator
repo *Repository repo *Repository
} }

View File

@ -7,6 +7,7 @@ extern void _go_git_populate_checkout_callbacks(git_checkout_options *opts);
*/ */
import "C" import "C"
import ( import (
"errors"
"os" "os"
"runtime" "runtime"
"unsafe" "unsafe"
@ -48,8 +49,8 @@ const (
CheckoutUpdateSubmodulesIfChanged CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED // Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED) 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) error type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) ErrorCode
type CheckoutProgressCallback func(path string, completed, total uint) type CheckoutProgressCallback func(path string, completed, total uint) ErrorCode
type CheckoutOptions struct { type CheckoutOptions struct {
Strategy CheckoutStrategy // Default will be a dry run Strategy CheckoutStrategy // Default will be a dry run
@ -115,9 +116,9 @@ func checkoutNotifyCallback(
if data.options.NotifyCallback == nil { if data.options.NotifyCallback == nil {
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
err := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir) ret := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir)
if err != nil { if ret < 0 {
*data.errorTarget = err *data.errorTarget = errors.New(ErrorCode(ret).String())
return C.int(ErrorCodeUser) return C.int(ErrorCodeUser)
} }
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)

View File

@ -9,16 +9,18 @@ import (
) )
type CherrypickOptions struct { type CherrypickOptions struct {
Version uint
Mainline uint Mainline uint
MergeOptions MergeOptions MergeOpts MergeOptions
CheckoutOptions CheckoutOptions CheckoutOpts CheckoutOptions
} }
func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions { func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions {
opts := CherrypickOptions{ opts := CherrypickOptions{
Version: uint(c.version),
Mainline: uint(c.mainline), Mainline: uint(c.mainline),
MergeOptions: mergeOptionsFromC(&c.merge_opts), MergeOpts: mergeOptionsFromC(&c.merge_opts),
CheckoutOptions: checkoutOptionsFromC(&c.checkout_opts), CheckoutOpts: checkoutOptionsFromC(&c.checkout_opts),
} }
return opts return opts
} }
@ -29,8 +31,8 @@ func populateCherrypickOptions(copts *C.git_cherrypick_options, opts *Cherrypick
return nil return nil
} }
copts.mainline = C.uint(opts.Mainline) copts.mainline = C.uint(opts.Mainline)
populateMergeOptions(&copts.merge_opts, &opts.MergeOptions) populateMergeOptions(&copts.merge_opts, &opts.MergeOpts)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget) populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOpts, errorTarget)
return copts return copts
} }
@ -80,7 +82,7 @@ func (r *Repository) CherrypickCommit(pick, our *Commit, opts CherrypickOptions)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
cOpts := populateMergeOptions(&C.git_merge_options{}, &opts.MergeOptions) cOpts := populateMergeOptions(&C.git_merge_options{}, &opts.MergeOpts)
defer freeMergeOptions(cOpts) defer freeMergeOptions(cOpts)
var ptr *C.git_index var ptr *C.git_index

View File

@ -7,15 +7,16 @@ extern void _go_git_populate_clone_callbacks(git_clone_options *opts);
*/ */
import "C" import "C"
import ( import (
"errors"
"runtime" "runtime"
"unsafe" "unsafe"
) )
type RemoteCreateCallback func(repo *Repository, name, url string) (*Remote, error) type RemoteCreateCallback func(repo *Repository, name, url string) (*Remote, ErrorCode)
type CloneOptions struct { type CloneOptions struct {
CheckoutOptions CheckoutOptions *CheckoutOpts
FetchOptions FetchOptions *FetchOptions
Bare bool Bare bool
CheckoutBranch string CheckoutBranch string
RemoteCreateCallback RemoteCreateCallback RemoteCreateCallback RemoteCreateCallback
@ -70,10 +71,9 @@ func remoteCreateCallback(
panic("invalid remote create callback") panic("invalid remote create callback")
} }
remote, err := data.options.RemoteCreateCallback(repo, name, url) remote, ret := data.options.RemoteCreateCallback(repo, name, url)
if ret < 0 {
if err != nil { *data.errorTarget = errors.New(ErrorCode(ret).String())
*data.errorTarget = err
return C.int(ErrorCodeUser) return C.int(ErrorCodeUser)
} }
if remote == nil { if remote == nil {
@ -85,7 +85,6 @@ func remoteCreateCallback(
// clear finalizer as the calling C function will // clear finalizer as the calling C function will
// free the remote itself // free the remote itself
runtime.SetFinalizer(remote, nil) runtime.SetFinalizer(remote, nil)
remote.repo.Remotes.untrackRemote(remote)
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
@ -100,8 +99,8 @@ func populateCloneOptions(copts *C.git_clone_options, opts *CloneOptions, errorT
if opts == nil { if opts == nil {
return nil return nil
} }
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget) populateCheckoutOptions(&copts.checkout_opts, opts.CheckoutOpts, errorTarget)
populateFetchOptions(&copts.fetch_opts, &opts.FetchOptions, errorTarget) populateFetchOptions(&copts.fetch_opts, opts.FetchOptions, errorTarget)
copts.bare = cbool(opts.Bare) copts.bare = cbool(opts.Bare)
if opts.RemoteCreateCallback != nil { if opts.RemoteCreateCallback != nil {

View File

@ -2,7 +2,6 @@ package git
import ( import (
"io/ioutil" "io/ioutil"
"os"
"testing" "testing"
) )
@ -50,9 +49,15 @@ func TestCloneWithCallback(t *testing.T) {
opts := CloneOptions{ opts := CloneOptions{
Bare: true, Bare: true,
RemoteCreateCallback: func(r *Repository, name, url string) (*Remote, error) { RemoteCreateCallback: func(r *Repository, name, url string) (*Remote, ErrorCode) {
testPayload += 1 testPayload += 1
return r.Remotes.Create(REMOTENAME, url)
remote, err := r.Remotes.Create(REMOTENAME, url)
if err != nil {
return nil, ErrorCodeGeneric
}
return remote, ErrorCodeOK
}, },
} }
@ -71,17 +76,3 @@ func TestCloneWithCallback(t *testing.T) {
} }
defer remote.Free() defer remote.Free()
} }
// TestCloneWithExternalHTTPUrl
func TestCloneWithExternalHTTPUrl(t *testing.T) {
path, err := ioutil.TempDir("", "git2go")
defer os.RemoveAll(path)
// clone the repo
url := "https://github.com/libgit2/TestGitRepository"
_, err = Clone(url, path, &CloneOptions{})
if err != nil {
t.Fatal("cannot clone remote repo via https, error: ", err)
}
}

View File

@ -12,17 +12,8 @@ import (
"unsafe" "unsafe"
) )
// MessageEncoding is the encoding of commit messages.
type MessageEncoding string
const (
// MessageEncodingUTF8 is the default message encoding.
MessageEncodingUTF8 MessageEncoding = "UTF-8"
)
// Commit // Commit
type Commit struct { type Commit struct {
doNotCompare
Object Object
cast_ptr *C.git_commit cast_ptr *C.git_commit
} }
@ -37,14 +28,10 @@ func (c *Commit) Message() string {
return ret return ret
} }
func (c *Commit) MessageEncoding() MessageEncoding { func (c *Commit) MessageEncoding() string {
ptr := C.git_commit_message_encoding(c.cast_ptr) ret := C.GoString(C.git_commit_message_encoding(c.cast_ptr))
if ptr == nil {
return MessageEncodingUTF8
}
ret := C.GoString(ptr)
runtime.KeepAlive(c) runtime.KeepAlive(c)
return MessageEncoding(ret) return ret
} }
func (c *Commit) RawMessage() string { func (c *Commit) RawMessage() string {
@ -68,19 +55,6 @@ func (c *Commit) ContentToSign() string {
// CommitSigningCallback defines a function type that takes some data to sign and returns (signature, signature_field, error) // 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) 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 // WithSignatureUsing creates a new signed commit from this one using the given signing callback
func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) { func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
signature, signatureField, err := f(c.ContentToSign()) signature, signatureField, err := f(c.ContentToSign())
@ -93,11 +67,40 @@ func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
// WithSignature creates a new signed commit from the given signature and signature field // WithSignature creates a new signed commit from the given signature and signature field
func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, error) { func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, error) {
return c.Owner().CreateCommitWithSignature( totalCommit := c.ContentToSign()
c.ContentToSign(),
signature, oid := new(Oid)
signatureField,
var csf *C.char = nil
if signatureField != "" {
csf = C.CString(signatureField)
defer C.free(unsafe.Pointer(csf))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cTotalCommit := C.CString(totalCommit)
cSignature := C.CString(signature)
defer C.free(unsafe.Pointer(cTotalCommit))
defer C.free(unsafe.Pointer(cSignature))
ret := C.git_commit_create_with_signature(
oid.toC(),
c.Owner().ptr,
cTotalCommit,
cSignature,
csf,
) )
runtime.KeepAlive(c)
runtime.KeepAlive(oid)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
} }
func (c *Commit) ExtractSignature() (string, string, error) { func (c *Commit) ExtractSignature() (string, string, error) {

View File

@ -52,7 +52,6 @@ func newConfigEntryFromC(centry *C.git_config_entry) *ConfigEntry {
} }
type Config struct { type Config struct {
doNotCompare
ptr *C.git_config ptr *C.git_config
} }
@ -362,7 +361,6 @@ func OpenOndisk(path string) (*Config, error) {
} }
type ConfigIterator struct { type ConfigIterator struct {
doNotCompare
ptr *C.git_config_iterator ptr *C.git_config_iterator
cfg *Config cfg *Config
} }
@ -452,17 +450,3 @@ func ConfigFindProgramdata() (string, error) {
return C.GoString(buf.ptr), nil return C.GoString(buf.ptr), nil
} }
// OpenDefault opens the default config according to git rules
func OpenDefault() (*Config, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
config := new(Config)
if ret := C.git_config_open_default(&config.ptr); ret < 0 {
return nil, MakeGitError(ret)
}
return config, nil
}

View File

@ -107,13 +107,3 @@ func TestConfigLookups(t *testing.T) {
test(c, t) test(c, t)
} }
} }
func TestOpenDefault(t *testing.T) {
c, err := OpenDefault()
if err != nil {
t.Errorf("OpenDefault error: '%v'. Expected none\n", err)
return
}
defer c.Free()
}

View File

@ -11,7 +11,6 @@ void _go_git_populate_credential_ssh_custom(git_credential_ssh_custom *cred);
import "C" import "C"
import ( import (
"crypto/rand" "crypto/rand"
"errors"
"fmt" "fmt"
"runtime" "runtime"
"strings" "strings"
@ -80,7 +79,6 @@ func (t CredentialType) String() string {
} }
type Credential struct { type Credential struct {
doNotCompare
ptr *C.git_credential ptr *C.git_credential
} }
@ -107,34 +105,6 @@ func (o *Credential) Free() {
o.ptr = nil o.ptr = nil
} }
// GetUserpassPlaintext returns the plaintext username/password combination stored in the Cred.
func (o *Credential) GetUserpassPlaintext() (username, password string, err error) {
if o.Type() != CredentialTypeUserpassPlaintext {
err = errors.New("credential is not userpass plaintext")
return
}
plaintextCredPtr := (*C.git_cred_userpass_plaintext)(unsafe.Pointer(o.ptr))
username = C.GoString(plaintextCredPtr.username)
password = C.GoString(plaintextCredPtr.password)
return
}
// GetSSHKey returns the SSH-specific key information from the Cred object.
func (o *Credential) GetSSHKey() (username, publickey, privatekey, passphrase string, err error) {
if o.Type() != CredentialTypeSSHKey && o.Type() != CredentialTypeSSHMemory {
err = fmt.Errorf("credential is not an SSH key: %v", o.Type())
return
}
sshKeyCredPtr := (*C.git_cred_ssh_key)(unsafe.Pointer(o.ptr))
username = C.GoString(sshKeyCredPtr.username)
publickey = C.GoString(sshKeyCredPtr.publickey)
privatekey = C.GoString(sshKeyCredPtr.privatekey)
passphrase = C.GoString(sshKeyCredPtr.passphrase)
return
}
func NewCredentialUsername(username string) (*Credential, error) { func NewCredentialUsername(username string) (*Credential, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()

View File

@ -13,10 +13,10 @@ import (
// blob.go // blob.go
// Deprecated: BlobChunkCallback is not used. // BlobChunkCallback is not used.
type BlobChunkCallback func(maxLen int) ([]byte, error) type BlobChunkCallback func(maxLen int) ([]byte, error)
// Deprecated: BlobCallbackData is not used. // BlobCallbackData is not used.
type BlobCallbackData struct { type BlobCallbackData struct {
Callback BlobChunkCallback Callback BlobChunkCallback
Error error Error error
@ -24,12 +24,12 @@ type BlobCallbackData struct {
// checkout.go // checkout.go
// Deprecated: CheckoutOpts is a deprecated alias of CheckoutOptions. // CheckoutOpts is a deprecated alias of CheckoutOptions.
type CheckoutOpts = CheckoutOptions type CheckoutOpts = CheckoutOptions
// credentials.go // credentials.go
// Deprecated: CredType is a deprecated alias of CredentialType // CredType is a deprecated alias of CredentialType
type CredType = CredentialType type CredType = CredentialType
const ( const (
@ -39,233 +39,146 @@ const (
CredTypeDefault = CredentialTypeDefault CredTypeDefault = CredentialTypeDefault
) )
// Deprecated: Cred is a deprecated alias of Credential // Cred is a deprecated alias of Credential
type Cred = Credential type Cred = Credential
// Deprecated: NewCredUsername is a deprecated alias of NewCredentialUsername. // NewCredUsername is a deprecated alias of NewCredentialUsername.
func NewCredUsername(username string) (*Cred, error) { func NewCredUsername(username string) (*Cred, error) {
return NewCredentialUsername(username) return NewCredentialUsername(username)
} }
// Deprecated: NewCredUserpassPlaintext is a deprecated alias of NewCredentialUserpassPlaintext. // NewCredUserpassPlaintext is a deprecated alias of NewCredentialUserpassPlaintext.
func NewCredUserpassPlaintext(username string, password string) (*Cred, error) { func NewCredUserpassPlaintext(username string, password string) (*Cred, error) {
return NewCredentialUserpassPlaintext(username, password) return NewCredentialUserpassPlaintext(username, password)
} }
// Deprecated: NewCredSshKey is a deprecated alias of NewCredentialSshKey. // NewCredSshKey is a deprecated alias of NewCredentialSshKey.
func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string, passphrase string) (*Cred, error) { func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string, passphrase string) (*Cred, error) {
return NewCredentialSSHKey(username, publicKeyPath, privateKeyPath, passphrase) return NewCredentialSSHKey(username, publicKeyPath, privateKeyPath, passphrase)
} }
// Deprecated: NewCredSshKeyFromMemory is a deprecated alias of NewCredentialSSHKeyFromMemory. // NewCredSshKeyFromMemory is a deprecated alias of NewCredentialSSHKeyFromMemory.
func NewCredSshKeyFromMemory(username string, publicKey string, privateKey string, passphrase string) (*Cred, error) { func NewCredSshKeyFromMemory(username string, publicKey string, privateKey string, passphrase string) (*Cred, error) {
return NewCredentialSSHKeyFromMemory(username, publicKey, privateKey, passphrase) return NewCredentialSSHKeyFromMemory(username, publicKey, privateKey, passphrase)
} }
// Deprecated: NewCredSshKeyFromAgent is a deprecated alias of NewCredentialSSHFromAgent. // NewCredSshKeyFromAgent is a deprecated alias of NewCredentialSSHFromAgent.
func NewCredSshKeyFromAgent(username string) (*Cred, error) { func NewCredSshKeyFromAgent(username string) (*Cred, error) {
return NewCredentialSSHKeyFromAgent(username) return NewCredentialSSHKeyFromAgent(username)
} }
// Deprecated: NewCredDefault is a deprecated alias fof NewCredentialDefault. // NewCredDefault is a deprecated alias fof NewCredentialDefault.
func NewCredDefault() (*Cred, error) { func NewCredDefault() (*Cred, error) {
return NewCredentialDefault() return NewCredentialDefault()
} }
// diff.go
const (
// Deprecated: DiffIgnoreWhitespaceEol is a deprecated alias of DiffIgnoreWhitespaceEOL.
DiffIgnoreWitespaceEol = DiffIgnoreWhitespaceEOL
)
// features.go // features.go
const ( const (
// Deprecated: FeatureHttps is a deprecated alias of FeatureHTTPS. // FeatureHttps is a deprecated alias of FeatureHTTPS.
FeatureHttps = FeatureHTTPS FeatureHttps = FeatureHTTPS
// Deprecated: FeatureSsh is a deprecated alias of FeatureSSH. // FeatureSsh is a deprecated alias of FeatureSSH.
FeatureSsh = FeatureSSH FeatureSsh = FeatureSSH
) )
// git.go // git.go
const ( const (
// Deprecated: ErrClassNone is a deprecated alias of ErrorClassNone.
ErrClassNone = ErrorClassNone ErrClassNone = ErrorClassNone
// Deprecated: ErrClassNoMemory is a deprecated alias of ErrorClassNoMemory.
ErrClassNoMemory = ErrorClassNoMemory ErrClassNoMemory = ErrorClassNoMemory
// Deprecated: ErrClassOs is a deprecated alias of ErrorClassOS.
ErrClassOs = ErrorClassOS ErrClassOs = ErrorClassOS
// Deprecated: ErrClassInvalid is a deprecated alias of ErrorClassInvalid.
ErrClassInvalid = ErrorClassInvalid ErrClassInvalid = ErrorClassInvalid
// Deprecated: ErrClassReference is a deprecated alias of ErrorClassReference.
ErrClassReference = ErrorClassReference ErrClassReference = ErrorClassReference
// Deprecated: ErrClassZlib is a deprecated alias of ErrorClassZlib.
ErrClassZlib = ErrorClassZlib ErrClassZlib = ErrorClassZlib
// Deprecated: ErrClassRepository is a deprecated alias of ErrorClassRepository.
ErrClassRepository = ErrorClassRepository ErrClassRepository = ErrorClassRepository
// Deprecated: ErrClassConfig is a deprecated alias of ErrorClassConfig.
ErrClassConfig = ErrorClassConfig ErrClassConfig = ErrorClassConfig
// Deprecated: ErrClassRegex is a deprecated alias of ErrorClassRegex.
ErrClassRegex = ErrorClassRegex ErrClassRegex = ErrorClassRegex
// Deprecated: ErrClassOdb is a deprecated alias of ErrorClassOdb.
ErrClassOdb = ErrorClassOdb ErrClassOdb = ErrorClassOdb
// Deprecated: ErrClassIndex is a deprecated alias of ErrorClassIndex.
ErrClassIndex = ErrorClassIndex ErrClassIndex = ErrorClassIndex
// Deprecated: ErrClassObject is a deprecated alias of ErrorClassObject.
ErrClassObject = ErrorClassObject ErrClassObject = ErrorClassObject
// Deprecated: ErrClassNet is a deprecated alias of ErrorClassNet.
ErrClassNet = ErrorClassNet ErrClassNet = ErrorClassNet
// Deprecated: ErrClassTag is a deprecated alias of ErrorClassTag.
ErrClassTag = ErrorClassTag ErrClassTag = ErrorClassTag
// Deprecated: ErrClassTree is a deprecated alias of ErrorClassTree.
ErrClassTree = ErrorClassTree ErrClassTree = ErrorClassTree
// Deprecated: ErrClassIndexer is a deprecated alias of ErrorClassIndexer.
ErrClassIndexer = ErrorClassIndexer ErrClassIndexer = ErrorClassIndexer
// Deprecated: ErrClassSSL is a deprecated alias of ErrorClassSSL.
ErrClassSSL = ErrorClassSSL ErrClassSSL = ErrorClassSSL
// Deprecated: ErrClassSubmodule is a deprecated alias of ErrorClassSubmodule.
ErrClassSubmodule = ErrorClassSubmodule ErrClassSubmodule = ErrorClassSubmodule
// Deprecated: ErrClassThread is a deprecated alias of ErrorClassThread.
ErrClassThread = ErrorClassThread ErrClassThread = ErrorClassThread
// Deprecated: ErrClassStash is a deprecated alias of ErrorClassStash.
ErrClassStash = ErrorClassStash ErrClassStash = ErrorClassStash
// Deprecated: ErrClassCheckout is a deprecated alias of ErrorClassCheckout.
ErrClassCheckout = ErrorClassCheckout ErrClassCheckout = ErrorClassCheckout
// Deprecated: ErrClassFetchHead is a deprecated alias of ErrorClassFetchHead.
ErrClassFetchHead = ErrorClassFetchHead ErrClassFetchHead = ErrorClassFetchHead
// Deprecated: ErrClassMerge is a deprecated alias of ErrorClassMerge.
ErrClassMerge = ErrorClassMerge ErrClassMerge = ErrorClassMerge
// Deprecated: ErrClassSsh is a deprecated alias of ErrorClassSSH.
ErrClassSsh = ErrorClassSSH ErrClassSsh = ErrorClassSSH
// Deprecated: ErrClassFilter is a deprecated alias of ErrorClassFilter.
ErrClassFilter = ErrorClassFilter ErrClassFilter = ErrorClassFilter
// Deprecated: ErrClassRevert is a deprecated alias of ErrorClassRevert.
ErrClassRevert = ErrorClassRevert ErrClassRevert = ErrorClassRevert
// Deprecated: ErrClassCallback is a deprecated alias of ErrorClassCallback.
ErrClassCallback = ErrorClassCallback ErrClassCallback = ErrorClassCallback
// Deprecated: ErrClassRebase is a deprecated alias of ErrorClassRebase.
ErrClassRebase = ErrorClassRebase ErrClassRebase = ErrorClassRebase
// Deprecated: ErrClassPatch is a deprecated alias of ErrorClassPatch.
ErrClassPatch = ErrorClassPatch ErrClassPatch = ErrorClassPatch
) )
const ( const (
// Deprecated: ErrOk is a deprecated alias of ErrorCodeOK.
ErrOk = ErrorCodeOK ErrOk = ErrorCodeOK
// Deprecated: ErrGeneric is a deprecated alias of ErrorCodeGeneric.
ErrGeneric = ErrorCodeGeneric ErrGeneric = ErrorCodeGeneric
// Deprecated: ErrNotFound is a deprecated alias of ErrorCodeNotFound.
ErrNotFound = ErrorCodeNotFound ErrNotFound = ErrorCodeNotFound
// Deprecated: ErrExists is a deprecated alias of ErrorCodeExists.
ErrExists = ErrorCodeExists ErrExists = ErrorCodeExists
// Deprecated: ErrAmbiguous is a deprecated alias of ErrorCodeAmbiguous.
ErrAmbiguous = ErrorCodeAmbiguous ErrAmbiguous = ErrorCodeAmbiguous
// Deprecated: ErrAmbigious is a deprecated alias of ErrorCodeAmbiguous.
ErrAmbigious = ErrorCodeAmbiguous ErrAmbigious = ErrorCodeAmbiguous
// Deprecated: ErrBuffs is a deprecated alias of ErrorCodeBuffs.
ErrBuffs = ErrorCodeBuffs ErrBuffs = ErrorCodeBuffs
// Deprecated: ErrUser is a deprecated alias of ErrorCodeUser.
ErrUser = ErrorCodeUser ErrUser = ErrorCodeUser
// Deprecated: ErrBareRepo is a deprecated alias of ErrorCodeBareRepo.
ErrBareRepo = ErrorCodeBareRepo ErrBareRepo = ErrorCodeBareRepo
// Deprecated: ErrUnbornBranch is a deprecated alias of ErrorCodeUnbornBranch.
ErrUnbornBranch = ErrorCodeUnbornBranch ErrUnbornBranch = ErrorCodeUnbornBranch
// Deprecated: ErrUnmerged is a deprecated alias of ErrorCodeUnmerged.
ErrUnmerged = ErrorCodeUnmerged ErrUnmerged = ErrorCodeUnmerged
// Deprecated: ErrNonFastForward is a deprecated alias of ErrorCodeNonFastForward.
ErrNonFastForward = ErrorCodeNonFastForward ErrNonFastForward = ErrorCodeNonFastForward
// Deprecated: ErrInvalidSpec is a deprecated alias of ErrorCodeInvalidSpec.
ErrInvalidSpec = ErrorCodeInvalidSpec ErrInvalidSpec = ErrorCodeInvalidSpec
// Deprecated: ErrConflict is a deprecated alias of ErrorCodeConflict.
ErrConflict = ErrorCodeConflict ErrConflict = ErrorCodeConflict
// Deprecated: ErrLocked is a deprecated alias of ErrorCodeLocked.
ErrLocked = ErrorCodeLocked ErrLocked = ErrorCodeLocked
// Deprecated: ErrModified is a deprecated alias of ErrorCodeModified.
ErrModified = ErrorCodeModified ErrModified = ErrorCodeModified
// Deprecated: ErrAuth is a deprecated alias of ErrorCodeAuth.
ErrAuth = ErrorCodeAuth ErrAuth = ErrorCodeAuth
// Deprecated: ErrCertificate is a deprecated alias of ErrorCodeCertificate.
ErrCertificate = ErrorCodeCertificate ErrCertificate = ErrorCodeCertificate
// Deprecated: ErrApplied is a deprecated alias of ErrorCodeApplied.
ErrApplied = ErrorCodeApplied ErrApplied = ErrorCodeApplied
// Deprecated: ErrPeel is a deprecated alias of ErrorCodePeel.
ErrPeel = ErrorCodePeel ErrPeel = ErrorCodePeel
// Deprecated: ErrEOF is a deprecated alias of ErrorCodeEOF.
ErrEOF = ErrorCodeEOF ErrEOF = ErrorCodeEOF
// Deprecated: ErrUncommitted is a deprecated alias of ErrorCodeUncommitted.
ErrUncommitted = ErrorCodeUncommitted ErrUncommitted = ErrorCodeUncommitted
// Deprecated: ErrDirectory is a deprecated alias of ErrorCodeDirectory.
ErrDirectory = ErrorCodeDirectory ErrDirectory = ErrorCodeDirectory
// Deprecated: ErrMergeConflict is a deprecated alias of ErrorCodeMergeConflict.
ErrMergeConflict = ErrorCodeMergeConflict ErrMergeConflict = ErrorCodeMergeConflict
// Deprecated: ErrPassthrough is a deprecated alias of ErrorCodePassthrough.
ErrPassthrough = ErrorCodePassthrough ErrPassthrough = ErrorCodePassthrough
// Deprecated: ErrIterOver is a deprecated alias of ErrorCodeIterOver.
ErrIterOver = ErrorCodeIterOver ErrIterOver = ErrorCodeIterOver
// Deprecated: ErrApplyFail is a deprecated alias of ErrorCodeApplyFail.
ErrApplyFail = ErrorCodeApplyFail ErrApplyFail = ErrorCodeApplyFail
) )
// index.go // index.go
// Deprecated: IndexAddOpts is a deprecated alias of IndexAddOption. // IndexAddOpts is a deprecated alias of IndexAddOption.
type IndexAddOpts = IndexAddOption type IndexAddOpts = IndexAddOption
// Deprecated: IndexStageOpts is a deprecated alias of IndexStageState. // IndexStageOpts is a deprecated alias of IndexStageState.
type IndexStageOpts = IndexStageState type IndexStageOpts = IndexStageState
// submodule.go // submodule.go
// Deprecated: SubmoduleCbk is a deprecated alias of SubmoduleCallback. // SubmoduleCbk is a deprecated alias of SubmoduleCallback.
type SubmoduleCbk = SubmoduleCallback type SubmoduleCbk = SubmoduleCallback
// Deprecated: SubmoduleVisitor is not used. // SubmoduleVisitor is not used.
func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int {
sub := &Submodule{ptr: (*C.git_submodule)(csub)} sub := &Submodule{(*C.git_submodule)(csub), nil}
callback, ok := pointerHandles.Get(handle).(SubmoduleCallback) callback, ok := pointerHandles.Get(handle).(SubmoduleCallback)
if !ok { if !ok {
panic("invalid submodule visitor callback") panic("invalid submodule visitor callback")
} }
err := callback(sub, C.GoString(name)) return (C.int)(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 // tree.go
// Deprecated: CallbackGitTreeWalk is not used. // CallbackGitTreeWalk is not used.
func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int { func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int {
root := C.GoString(_root) root := C.GoString(_root)
callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback) if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok {
if !ok { return C.int(callback(root, newTreeEntry(entry)))
} else {
panic("invalid treewalk callback") panic("invalid treewalk callback")
} }
err := callback(root, newTreeEntry(entry))
if err != nil {
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
} }

View File

@ -176,7 +176,6 @@ func (repo *Repository) DescribeWorkdir(opts *DescribeOptions) (*DescribeResult,
// //
// Use Format() to get a string out of it. // Use Format() to get a string out of it.
type DescribeResult struct { type DescribeResult struct {
doNotCompare
ptr *C.git_describe_result ptr *C.git_describe_result
} }

View File

@ -132,7 +132,6 @@ func diffLineFromC(line *C.git_diff_line) DiffLine {
} }
type Diff struct { type Diff struct {
doNotCompare
ptr *C.git_diff ptr *C.git_diff
repo *Repository repo *Repository
runFinalizer bool runFinalizer bool
@ -220,7 +219,6 @@ func (diff *Diff) FindSimilar(opts *DiffFindOptions) error {
} }
type DiffStats struct { type DiffStats struct {
doNotCompare
ptr *C.git_diff_stats ptr *C.git_diff_stats
} }
@ -489,7 +487,7 @@ const (
DiffIgnoreWhitespace DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE DiffIgnoreWhitespace DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE
DiffIgnoreWhitespaceChange DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_CHANGE DiffIgnoreWhitespaceChange DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_CHANGE
DiffIgnoreWhitespaceEOL DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_EOL DiffIgnoreWitespaceEol DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_EOL
DiffShowUntrackedContent DiffOptionsFlag = C.GIT_DIFF_SHOW_UNTRACKED_CONTENT DiffShowUntrackedContent DiffOptionsFlag = C.GIT_DIFF_SHOW_UNTRACKED_CONTENT
DiffShowUnmodified DiffOptionsFlag = C.GIT_DIFF_SHOW_UNMODIFIED DiffShowUnmodified DiffOptionsFlag = C.GIT_DIFF_SHOW_UNMODIFIED

45
git.go
View File

@ -14,7 +14,6 @@ import (
"unsafe" "unsafe"
) )
//go:generate stringer -type ErrorClass -trimprefix ErrorClass -tags static
type ErrorClass int type ErrorClass int
const ( const (
@ -49,7 +48,6 @@ const (
ErrorClassPatch ErrorClass = C.GIT_ERROR_PATCH ErrorClassPatch ErrorClass = C.GIT_ERROR_PATCH
) )
//go:generate stringer -type ErrorCode -trimprefix ErrorCode -tags static
type ErrorCode int type ErrorCode int
const ( const (
@ -125,12 +123,7 @@ var (
ErrInvalid = errors.New("Invalid state for operation") ErrInvalid = errors.New("Invalid state for operation")
) )
// doNotCompare is an idiomatic way of making structs non-comparable to avoid
// future field additions to make them non-comparable.
type doNotCompare [0]func()
var pointerHandles *HandleList var pointerHandles *HandleList
var remotePointers *remotePointerList
func init() { func init() {
initLibGit2() initLibGit2()
@ -138,48 +131,32 @@ func init() {
func initLibGit2() { func initLibGit2() {
pointerHandles = NewHandleList() pointerHandles = NewHandleList()
remotePointers = newRemotePointerList()
C.git_libgit2_init() C.git_libgit2_init()
features := Features()
// Due to the multithreaded nature of Go and its interaction with // Due to the multithreaded nature of Go and its interaction with
// calling C functions, we cannot work with a library that was not built // calling C functions, we cannot work with a library that was not built
// with multi-threading support. The most likely outcome is a segfault // with multi-threading support. The most likely outcome is a segfault
// or panic at an incomprehensible time, so let's make it easy by // or panic at an incomprehensible time, so let's make it easy by
// panicking right here. // panicking right here.
if features&FeatureThreads == 0 { if Features()&FeatureThreads == 0 {
panic("libgit2 was not built with threading support") panic("libgit2 was not built with threading support")
} }
if features&FeatureHTTPS == 0 { // This is not something we should be doing, as we may be
if err := registerManagedHTTP(); err != nil { // stomping all over someone else's setup. The user should do
panic(err) // this themselves or use some binding/wrapper which does it
} // in such a way that they can be sure they're the only ones
} else { // setting it up.
// This is not something we should be doing, as we may be stomping all over
// someone else's setup. The user should do this themselves or use some
// binding/wrapper which does it in such a way that they can be sure
// they're the only ones setting it up.
C.git_openssl_set_locking() C.git_openssl_set_locking()
}
if features&FeatureSSH == 0 {
if err := registerManagedSSH(); err != nil {
panic(err)
}
}
} }
// Shutdown frees all the resources acquired by libgit2. Make sure no // Shutdown frees all the resources acquired by libgit2. Make sure no
// references to any libgit2 go objects are live before calling this. // references to any git2go objects are live before calling this.
// After this is called, invoking any function from this library will result in // After this is called, invoking any function from this library will result in
// undefined behavior, so make sure this is called carefully. // undefined behavior, so make sure this is called carefully.
func Shutdown() { func Shutdown() {
if err := unregisterManagedTransports(); err != nil {
panic(err)
}
pointerHandles.Clear() pointerHandles.Clear()
remotePointers.clear()
C.git_libgit2_shutdown() C.git_libgit2_shutdown()
} }
@ -223,13 +200,13 @@ func NewOid(s string) (*Oid, error) {
o := new(Oid) o := new(Oid)
slice, err := hex.DecodeString(s) slice, error := hex.DecodeString(s)
if err != nil { if error != nil {
return nil, err return nil, error
} }
if len(slice) != 20 { if len(slice) != 20 {
return nil, &GitError{"invalid oid", ErrorClassNone, ErrorCodeGeneric} return nil, &GitError{"Invalid Oid", ErrorClassNone, ErrGeneric}
} }
copy(o[:], slice[:20]) copy(o[:], slice[:20])

View File

@ -11,16 +11,8 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
if err := registerManagedHTTP(); err != nil {
panic(err)
}
ret := m.Run() ret := m.Run()
if err := unregisterManagedTransports(); err != nil {
panic(err)
}
// Ensure that we are not leaking any pointer handles. // Ensure that we are not leaking any pointer handles.
pointerHandles.Lock() pointerHandles.Lock()
if len(pointerHandles.handles) > 0 { if len(pointerHandles.handles) > 0 {
@ -31,16 +23,6 @@ func TestMain(m *testing.M) {
} }
pointerHandles.Unlock() pointerHandles.Unlock()
// Or remote pointers.
remotePointers.Lock()
if len(remotePointers.pointers) > 0 {
for ptr, remote := range remotePointers.pointers {
fmt.Printf("%016p: %+v\n", ptr, remote)
}
panic("remote pointer list not empty")
}
remotePointers.Unlock()
Shutdown() Shutdown()
os.Exit(ret) os.Exit(ret)

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
module github.com/libgit2/git2go/v31
go 1.13
require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c
golang.org/x/sys v0.0.0-20201204225414-ed752295db88 // indirect
)

13
go.sum Normal file
View File

@ -0,0 +1,13 @@
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c h1:9HhBz5L/UjnK9XLtiZhYAdue5BVKep3PMmS2LuPDt8k=
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88 h1:KmZPnMocC93w341XZp26yTJg8Za7lhb2KhkYmixoeso=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View File

@ -40,30 +40,3 @@ func (repo *Repository) AheadBehind(local, upstream *Oid) (ahead, behind int, er
return int(aheadT), int(behindT), nil return int(aheadT), int(behindT), nil
} }
// ReachableFromAny returns whether a commit is reachable from any of a list of
// commits by following parent edges.
func (repo *Repository) ReachableFromAny(commit *Oid, descendants []*Oid) (bool, error) {
if len(descendants) == 0 {
return false, nil
}
coids := make([]C.git_oid, len(descendants))
for i := 0; i < len(descendants); i++ {
coids[i] = *descendants[i].toC()
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_graph_reachable_from_any(repo.ptr, commit.toC(), &coids[0], C.size_t(len(descendants)))
runtime.KeepAlive(repo)
runtime.KeepAlive(commit)
runtime.KeepAlive(coids)
runtime.KeepAlive(descendants)
if ret < 0 {
return false, MakeGitError(ret)
}
return (ret > 0), nil
}

View File

@ -1,75 +0,0 @@
package git
import (
"testing"
)
func TestReachableFromAny(t *testing.T) {
repo, err := OpenRepository("testdata/TestGitRepository.git")
checkFatal(t, err)
defer repo.Free()
for name, tc := range map[string]struct {
reachable bool
commit string
descendants []string
}{
"empty": {
reachable: false,
commit: "49322bb17d3acc9146f98c97d078513228bbf3c0",
},
"same": {
reachable: true,
commit: "49322bb17d3acc9146f98c97d078513228bbf3c0",
descendants: []string{"49322bb17d3acc9146f98c97d078513228bbf3c0"},
},
"unreachable": {
reachable: false,
commit: "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
descendants: []string{"58be4659bb571194ed4562d04b359d26216f526e"},
},
"unreachable-reverse": {
reachable: false,
commit: "58be4659bb571194ed4562d04b359d26216f526e",
descendants: []string{"ac7e7e44c1885efb472ad54a78327d66bfc4ecef"},
},
"root": {
reachable: false,
commit: "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",
descendants: []string{
"ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
"d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864",
"f73b95671f326616d66b2afb3bdfcdbbce110b44",
"d0114ab8ac326bab30e3a657a0397578c5a1af88",
},
},
"head": {
reachable: false,
commit: "49322bb17d3acc9146f98c97d078513228bbf3c0",
descendants: []string{
"ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
"d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864",
"f73b95671f326616d66b2afb3bdfcdbbce110b44",
"d0114ab8ac326bab30e3a657a0397578c5a1af88",
},
},
} {
tc := tc
t.Run(name, func(t *testing.T) {
commit, err := NewOid(tc.commit)
checkFatal(t, err)
descendants := make([]*Oid, len(tc.descendants))
for i, o := range tc.descendants {
descendants[i], err = NewOid(o)
checkFatal(t, err)
}
reachable, err := repo.ReachableFromAny(commit, descendants)
checkFatal(t, err)
if reachable != tc.reachable {
t.Errorf("ReachableFromAny(%s, %v) = %v, wanted %v", tc.commit, tc.descendants, reachable, tc.reachable)
}
})
}
}

View File

@ -11,7 +11,6 @@ import (
) )
type HandleList struct { type HandleList struct {
doNotCompare
sync.RWMutex sync.RWMutex
// stores the Go pointers // stores the Go pointers
handles map[unsafe.Pointer]interface{} handles map[unsafe.Pointer]interface{}

243
http.go
View File

@ -1,243 +0,0 @@
package git
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"sync"
)
// RegisterManagedHTTPTransport registers a Go-native implementation of an
// HTTP/S transport that doesn't rely on any system libraries (e.g.
// libopenssl/libmbedtls).
//
// If Shutdown or ReInit are called, make sure that the smart transports are
// freed before it.
func RegisterManagedHTTPTransport(protocol string) (*RegisteredSmartTransport, error) {
return NewRegisteredSmartTransport(protocol, true, httpSmartSubtransportFactory)
}
func registerManagedHTTP() error {
globalRegisteredSmartTransports.Lock()
defer globalRegisteredSmartTransports.Unlock()
for _, protocol := range []string{"http", "https"} {
if _, ok := globalRegisteredSmartTransports.transports[protocol]; ok {
continue
}
managed, err := newRegisteredSmartTransport(protocol, true, httpSmartSubtransportFactory, true)
if err != nil {
return fmt.Errorf("failed to register transport for %q: %v", protocol, err)
}
globalRegisteredSmartTransports.transports[protocol] = managed
}
return nil
}
func httpSmartSubtransportFactory(remote *Remote, transport *Transport) (SmartSubtransport, error) {
var proxyFn func(*http.Request) (*url.URL, error)
remoteConnectOpts, err := transport.SmartRemoteConnectOptions()
if err != nil {
return nil, err
}
switch remoteConnectOpts.ProxyOptions.Type {
case ProxyTypeNone:
proxyFn = nil
case ProxyTypeAuto:
proxyFn = http.ProxyFromEnvironment
case ProxyTypeSpecified:
parsedUrl, err := url.Parse(remoteConnectOpts.ProxyOptions.Url)
if err != nil {
return nil, err
}
proxyFn = http.ProxyURL(parsedUrl)
}
return &httpSmartSubtransport{
transport: transport,
client: &http.Client{
Transport: &http.Transport{
Proxy: proxyFn,
},
},
}, nil
}
type httpSmartSubtransport struct {
transport *Transport
client *http.Client
}
func (t *httpSmartSubtransport) Action(url string, action SmartServiceAction) (SmartSubtransportStream, error) {
var req *http.Request
var err error
switch action {
case SmartServiceActionUploadpackLs:
req, err = http.NewRequest("GET", url+"/info/refs?service=git-upload-pack", nil)
case SmartServiceActionUploadpack:
req, err = http.NewRequest("POST", url+"/git-upload-pack", nil)
if err != nil {
break
}
req.Header.Set("Content-Type", "application/x-git-upload-pack-request")
case SmartServiceActionReceivepackLs:
req, err = http.NewRequest("GET", url+"/info/refs?service=git-receive-pack", nil)
case SmartServiceActionReceivepack:
req, err = http.NewRequest("POST", url+"/info/refs?service=git-upload-pack", nil)
if err != nil {
break
}
req.Header.Set("Content-Type", "application/x-git-receive-pack-request")
default:
err = errors.New("unknown action")
}
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "git/2.0 (libgit2)")
stream := newManagedHttpStream(t, req)
if req.Method == "POST" {
stream.recvReply.Add(1)
stream.sendRequestBackground()
}
return stream, nil
}
func (t *httpSmartSubtransport) Close() error {
return nil
}
func (t *httpSmartSubtransport) Free() {
t.client = nil
}
type httpSmartSubtransportStream struct {
owner *httpSmartSubtransport
req *http.Request
resp *http.Response
reader *io.PipeReader
writer *io.PipeWriter
sentRequest bool
recvReply sync.WaitGroup
httpError error
}
func newManagedHttpStream(owner *httpSmartSubtransport, req *http.Request) *httpSmartSubtransportStream {
r, w := io.Pipe()
return &httpSmartSubtransportStream{
owner: owner,
req: req,
reader: r,
writer: w,
}
}
func (self *httpSmartSubtransportStream) Read(buf []byte) (int, error) {
if !self.sentRequest {
self.recvReply.Add(1)
if err := self.sendRequest(); err != nil {
return 0, err
}
}
if err := self.writer.Close(); err != nil {
return 0, err
}
self.recvReply.Wait()
if self.httpError != nil {
return 0, self.httpError
}
return self.resp.Body.Read(buf)
}
func (self *httpSmartSubtransportStream) Write(buf []byte) (int, error) {
if self.httpError != nil {
return 0, self.httpError
}
return self.writer.Write(buf)
}
func (self *httpSmartSubtransportStream) Free() {
if self.resp != nil {
self.resp.Body.Close()
}
}
func (self *httpSmartSubtransportStream) sendRequestBackground() {
go func() {
self.httpError = self.sendRequest()
}()
self.sentRequest = true
}
func (self *httpSmartSubtransportStream) sendRequest() error {
defer self.recvReply.Done()
self.resp = nil
var resp *http.Response
var err error
var userName string
var password string
for {
req := &http.Request{
Method: self.req.Method,
URL: self.req.URL,
Header: self.req.Header,
}
if req.Method == "POST" {
req.Body = self.reader
req.ContentLength = -1
}
if userName != "" && password != "" {
req.SetBasicAuth(userName, password)
}
resp, err = http.DefaultClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode == http.StatusOK {
break
}
if resp.StatusCode == http.StatusUnauthorized {
resp.Body.Close()
cred, err := self.owner.transport.SmartCredentials("", CredentialTypeUserpassPlaintext)
if err != nil {
return err
}
defer cred.Free()
userName, password, err = cred.GetUserpassPlaintext()
if err != nil {
return err
}
continue
}
// Any other error we treat as a hard error and punt back to the caller
resp.Body.Close()
return fmt.Errorf("Unhandled HTTP error %s", resp.Status)
}
self.sentRequest = true
self.resp = resp
return nil
}

View File

@ -10,12 +10,13 @@ extern int _go_git_index_remove_all(git_index*, const git_strarray*, void*);
*/ */
import "C" import "C"
import ( import (
"errors"
"fmt" "fmt"
"runtime" "runtime"
"unsafe" "unsafe"
) )
type IndexMatchedPathCallback func(string, string) error type IndexMatchedPathCallback func(string, string) int
type indexMatchedPathCallbackData struct { type indexMatchedPathCallbackData struct {
callback IndexMatchedPathCallback callback IndexMatchedPathCallback
errorTarget *error errorTarget *error
@ -51,14 +52,13 @@ const (
) )
type Index struct { type Index struct {
doNotCompare
ptr *C.git_index ptr *C.git_index
repo *Repository repo *Repository
} }
type IndexTime struct { type IndexTime struct {
Seconds int32 seconds int32
Nanoseconds uint32 nanoseconds uint32
} }
type IndexEntry struct { type IndexEntry struct {
@ -89,10 +89,10 @@ func newIndexEntryFromC(entry *C.git_index_entry) *IndexEntry {
} }
func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) { func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) {
dest.ctime.seconds = C.int32_t(source.Ctime.Seconds) dest.ctime.seconds = C.int32_t(source.Ctime.seconds)
dest.ctime.nanoseconds = C.uint32_t(source.Ctime.Nanoseconds) dest.ctime.nanoseconds = C.uint32_t(source.Ctime.nanoseconds)
dest.mtime.seconds = C.int32_t(source.Mtime.Seconds) dest.mtime.seconds = C.int32_t(source.Mtime.seconds)
dest.mtime.nanoseconds = C.uint32_t(source.Mtime.Nanoseconds) dest.mtime.nanoseconds = C.uint32_t(source.Mtime.nanoseconds)
dest.mode = C.uint32_t(source.Mode) dest.mode = C.uint32_t(source.Mode)
dest.uid = C.uint32_t(source.Uid) dest.uid = C.uint32_t(source.Uid)
dest.gid = C.uint32_t(source.Gid) dest.gid = C.uint32_t(source.Gid)
@ -108,7 +108,7 @@ func freeCIndexEntry(entry *C.git_index_entry) {
} }
func newIndexFromC(ptr *C.git_index, repo *Repository) *Index { func newIndexFromC(ptr *C.git_index, repo *Repository) *Index {
idx := &Index{ptr: ptr, repo: repo} idx := &Index{ptr, repo}
runtime.SetFinalizer(idx, (*Index).Free) runtime.SetFinalizer(idx, (*Index).Free)
return idx return idx
} }
@ -342,9 +342,9 @@ func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Po
panic("invalid matched path callback") panic("invalid matched path callback")
} }
err := data.callback(C.GoString(cPath), C.GoString(cMatchedPathspec)) ret := data.callback(C.GoString(cPath), C.GoString(cMatchedPathspec))
if err != nil { if ret < 0 {
*data.errorTarget = err *data.errorTarget = errors.New(ErrorCode(ret).String())
return C.int(ErrorCodeUser) return C.int(ErrorCodeUser)
} }
@ -616,7 +616,6 @@ func (v *Index) RemoveConflict(path string) error {
} }
type IndexConflictIterator struct { type IndexConflictIterator struct {
doNotCompare
ptr *C.git_index_conflict_iterator ptr *C.git_index_conflict_iterator
index *Index index *Index
} }

View File

@ -223,9 +223,9 @@ func TestIndexAddAllCallback(t *testing.T) {
checkFatal(t, err) checkFatal(t, err)
cbPath := "" cbPath := ""
err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) error { err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) int {
cbPath = p cbPath = p
return nil return 0
}) })
checkFatal(t, err) checkFatal(t, err)
if cbPath != "README" { if cbPath != "README" {

View File

@ -19,7 +19,6 @@ import (
// Indexer can post-process packfiles and create an .idx file for efficient // Indexer can post-process packfiles and create an .idx file for efficient
// lookup. // lookup.
type Indexer struct { type Indexer struct {
doNotCompare
ptr *C.git_indexer ptr *C.git_indexer
stats C.git_transfer_progress stats C.git_transfer_progress
ccallbacks C.git_remote_callbacks ccallbacks C.git_remote_callbacks

View File

@ -33,9 +33,9 @@ func TestIndexerOutOfOrder(t *testing.T) {
defer os.RemoveAll(tmpPath) defer os.RemoveAll(tmpPath)
var finalStats TransferProgress var finalStats TransferProgress
idx, err := NewIndexer(tmpPath, nil, func(stats TransferProgress) error { idx, err := NewIndexer(tmpPath, nil, func(stats TransferProgress) ErrorCode {
finalStats = stats finalStats = stats
return nil return ErrorCodeOK
}) })
checkFatal(t, err) checkFatal(t, err)
defer idx.Free() defer idx.Free()

View File

@ -18,7 +18,6 @@ import (
// Mempack is a custom ODB backend that permits packing object in-memory. // Mempack is a custom ODB backend that permits packing object in-memory.
type Mempack struct { type Mempack struct {
doNotCompare
ptr *C.git_odb_backend ptr *C.git_odb_backend
} }

View File

@ -17,7 +17,6 @@ import (
) )
type AnnotatedCommit struct { type AnnotatedCommit struct {
doNotCompare
ptr *C.git_annotated_commit ptr *C.git_annotated_commit
r *Repository r *Repository
} }
@ -138,6 +137,7 @@ const (
) )
type MergeOptions struct { type MergeOptions struct {
Version uint
TreeFlags MergeTreeFlag TreeFlags MergeTreeFlag
RenameThreshold uint RenameThreshold uint
@ -150,6 +150,7 @@ type MergeOptions struct {
func mergeOptionsFromC(opts *C.git_merge_options) MergeOptions { func mergeOptionsFromC(opts *C.git_merge_options) MergeOptions {
return MergeOptions{ return MergeOptions{
Version: uint(opts.version),
TreeFlags: MergeTreeFlag(opts.flags), TreeFlags: MergeTreeFlag(opts.flags),
RenameThreshold: uint(opts.rename_threshold), RenameThreshold: uint(opts.rename_threshold),
TargetLimit: uint(opts.target_limit), TargetLimit: uint(opts.target_limit),
@ -425,12 +426,11 @@ func (r *Repository) MergeBaseOctopus(oids []*Oid) (*Oid, error) {
} }
type MergeFileResult struct { type MergeFileResult struct {
doNotCompare
ptr *C.git_merge_file_result
Automergeable bool Automergeable bool
Path string Path string
Mode uint Mode uint
Contents []byte Contents []byte
ptr *C.git_merge_file_result
} }
func newMergeFileResultFromC(c *C.git_merge_file_result) *MergeFileResult { func newMergeFileResultFromC(c *C.git_merge_file_result) *MergeFileResult {

View File

@ -43,14 +43,6 @@ func TestMergeWithSelf(t *testing.T) {
mergeHeads[0] = mergeHead mergeHeads[0] = mergeHead
err = repo.Merge(mergeHeads, nil, nil) err = repo.Merge(mergeHeads, nil, nil)
checkFatal(t, err) checkFatal(t, err)
mergeMessage, err := repo.Message()
checkFatal(t, err)
expectedMessage := "Merge branch 'master'\n"
if mergeMessage != expectedMessage {
t.Errorf("merge Message = %v, want %v", mergeMessage, expectedMessage)
}
} }
func TestMergeAnalysisWithSelf(t *testing.T) { func TestMergeAnalysisWithSelf(t *testing.T) {

View File

@ -13,7 +13,6 @@ import (
// This object represents the possible operations which can be // This object represents the possible operations which can be
// performed on the collection of notes for a repository. // performed on the collection of notes for a repository.
type NoteCollection struct { type NoteCollection struct {
doNotCompare
repo *Repository repo *Repository
} }
@ -140,7 +139,6 @@ func (c *NoteCollection) DefaultRef() (string, error) {
// Note // Note
type Note struct { type Note struct {
doNotCompare
ptr *C.git_note ptr *C.git_note
r *Repository r *Repository
} }
@ -191,7 +189,6 @@ func (n *Note) Message() string {
// NoteIterator // NoteIterator
type NoteIterator struct { type NoteIterator struct {
doNotCompare
ptr *C.git_note_iterator ptr *C.git_note_iterator
r *Repository r *Repository
} }

View File

@ -22,7 +22,6 @@ const (
) )
type Object struct { type Object struct {
doNotCompare
ptr *C.git_object ptr *C.git_object
repo *Repository repo *Repository
} }

34
odb.go
View File

@ -22,12 +22,10 @@ import (
) )
type Odb struct { type Odb struct {
doNotCompare
ptr *C.git_odb ptr *C.git_odb
} }
type OdbBackend struct { type OdbBackend struct {
doNotCompare
ptr *C.git_odb_backend ptr *C.git_odb_backend
} }
@ -47,7 +45,7 @@ func NewOdb() (odb *Odb, err error) {
} }
func NewOdbBackendFromC(ptr unsafe.Pointer) (backend *OdbBackend) { func NewOdbBackendFromC(ptr unsafe.Pointer) (backend *OdbBackend) {
backend = &OdbBackend{ptr: (*C.git_odb_backend)(ptr)} backend = &OdbBackend{(*C.git_odb_backend)(ptr)}
return backend return backend
} }
@ -176,32 +174,6 @@ func (v *Odb) Read(oid *Oid) (obj *OdbObject, err error) {
return obj, nil return obj, nil
} }
func (odb *Odb) Refresh() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_refresh(odb.ptr)
runtime.KeepAlive(odb)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (odb *Odb) WriteMultiPackIndex() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_write_multi_pack_index(odb.ptr)
runtime.KeepAlive(odb)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
type OdbForEachCallback func(id *Oid) error type OdbForEachCallback func(id *Oid) error
type odbForEachCallbackData struct { type odbForEachCallbackData struct {
callback OdbForEachCallback callback OdbForEachCallback
@ -341,7 +313,6 @@ func (v *OdbBackend) Free() {
} }
type OdbObject struct { type OdbObject struct {
doNotCompare
ptr *C.git_odb_object ptr *C.git_odb_object
} }
@ -385,7 +356,6 @@ func (object *OdbObject) Data() (data []byte) {
} }
type OdbReadStream struct { type OdbReadStream struct {
doNotCompare
ptr *C.git_odb_stream ptr *C.git_odb_stream
Size uint64 Size uint64
Type ObjectType Type ObjectType
@ -426,7 +396,6 @@ func (stream *OdbReadStream) Free() {
} }
type OdbWriteStream struct { type OdbWriteStream struct {
doNotCompare
ptr *C.git_odb_stream ptr *C.git_odb_stream
Id Oid Id Oid
} }
@ -471,7 +440,6 @@ func (stream *OdbWriteStream) Free() {
// OdbWritepack is a stream to write a packfile to the ODB. // OdbWritepack is a stream to write a packfile to the ODB.
type OdbWritepack struct { type OdbWritepack struct {
doNotCompare
ptr *C.git_odb_writepack ptr *C.git_odb_writepack
stats C.git_transfer_progress stats C.git_transfer_progress
ccallbacks C.git_remote_callbacks ccallbacks C.git_remote_callbacks

View File

@ -61,31 +61,31 @@ func TestOdbStream(t *testing.T) {
_, _ = seedTestRepo(t, repo) _, _ = seedTestRepo(t, repo)
odb, err := repo.Odb() odb, error := repo.Odb()
checkFatal(t, err) checkFatal(t, error)
str := "hello, world!" str := "hello, world!"
writeStream, err := odb.NewWriteStream(int64(len(str)), ObjectBlob) writeStream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob)
checkFatal(t, err) checkFatal(t, error)
n, err := io.WriteString(writeStream, str) n, error := io.WriteString(writeStream, str)
checkFatal(t, err) checkFatal(t, error)
if n != len(str) { if n != len(str) {
t.Fatalf("Bad write length %v != %v", n, len(str)) t.Fatalf("Bad write length %v != %v", n, len(str))
} }
err = writeStream.Close() error = writeStream.Close()
checkFatal(t, err) checkFatal(t, error)
expectedId, err := NewOid("30f51a3fba5274d53522d0f19748456974647b4f") expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
checkFatal(t, err) checkFatal(t, error)
if writeStream.Id.Cmp(expectedId) != 0 { if writeStream.Id.Cmp(expectedId) != 0 {
t.Fatal("Wrong data written") t.Fatal("Wrong data written")
} }
readStream, err := odb.NewReadStream(&writeStream.Id) readStream, error := odb.NewReadStream(&writeStream.Id)
checkFatal(t, err) checkFatal(t, error)
data, err := ioutil.ReadAll(readStream) data, error := ioutil.ReadAll(readStream)
if str != string(data) { if str != string(data) {
t.Fatalf("Wrong data read %v != %v", str, string(data)) t.Fatalf("Wrong data read %v != %v", str, string(data))
} }
@ -98,8 +98,8 @@ func TestOdbHash(t *testing.T) {
_, _ = seedTestRepo(t, repo) _, _ = seedTestRepo(t, repo)
odb, err := repo.Odb() odb, error := repo.Odb()
checkFatal(t, err) checkFatal(t, error)
str := `tree 115fcae49287c82eb55bb275cbbd4556fbed72b7 str := `tree 115fcae49287c82eb55bb275cbbd4556fbed72b7
parent 66e1c476199ebcd3e304659992233132c5a52c6c parent 66e1c476199ebcd3e304659992233132c5a52c6c
@ -109,11 +109,11 @@ committer John Doe <john@doe.com> 1390682018 +0000
Initial commit.` Initial commit.`
for _, data := range [][]byte{[]byte(str), doublePointerBytes()} { for _, data := range [][]byte{[]byte(str), doublePointerBytes()} {
oid, err := odb.Hash(data, ObjectCommit) oid, error := odb.Hash(data, ObjectCommit)
checkFatal(t, err) checkFatal(t, error)
coid, err := odb.Write(data, ObjectCommit) coid, error := odb.Write(data, ObjectCommit)
checkFatal(t, err) checkFatal(t, error)
if oid.Cmp(coid) != 0 { if oid.Cmp(coid) != 0 {
t.Fatal("Hash and write Oids are different") t.Fatal("Hash and write Oids are different")
@ -167,9 +167,9 @@ func TestOdbWritepack(t *testing.T) {
checkFatal(t, err) checkFatal(t, err)
var finalStats TransferProgress var finalStats TransferProgress
writepack, err := odb.NewWritePack(func(stats TransferProgress) error { writepack, err := odb.NewWritePack(func(stats TransferProgress) ErrorCode {
finalStats = stats finalStats = stats
return nil return ErrorCodeOK
}) })
checkFatal(t, err) checkFatal(t, err)
defer writepack.Free() defer writepack.Free()

View File

@ -16,7 +16,6 @@ import (
) )
type Packbuilder struct { type Packbuilder struct {
doNotCompare
ptr *C.git_packbuilder ptr *C.git_packbuilder
r *Repository r *Repository
} }

View File

@ -10,7 +10,6 @@ import (
) )
type Patch struct { type Patch struct {
doNotCompare
ptr *C.git_patch ptr *C.git_patch
} }

165
rebase.go
View File

@ -9,7 +9,6 @@ import "C"
import ( import (
"errors" "errors"
"fmt" "fmt"
"reflect"
"runtime" "runtime"
"unsafe" "unsafe"
) )
@ -72,87 +71,20 @@ func newRebaseOperationFromC(c *C.git_rebase_operation) *RebaseOperation {
return operation return operation
} }
//export commitCreateCallback //export commitSigningCallback
func commitCreateCallback( func commitSigningCallback(errorMessage **C.char, _signature *C.git_buf, _signature_field *C.git_buf, _commit_content *C.char, handle unsafe.Pointer) C.int {
errorMessage **C.char,
_out *C.git_oid,
_author, _committer *C.git_signature,
_message_encoding, _message *C.char,
_tree *C.git_tree,
_parent_count C.size_t,
_parents **C.git_commit,
handle unsafe.Pointer,
) C.int {
data, ok := pointerHandles.Get(handle).(*rebaseOptionsData) data, ok := pointerHandles.Get(handle).(*rebaseOptionsData)
if !ok { if !ok {
panic("invalid sign payload") panic("invalid sign payload")
} }
if data.options.CommitCreateCallback == nil && data.options.CommitSigningCallback == nil { if data.options.CommitSigningCallback == nil {
return C.int(ErrorCodePassthrough) return C.int(ErrorCodePassthrough)
} }
messageEncoding := MessageEncodingUTF8 commitContent := C.GoString(_commit_content)
if _message_encoding != nil {
messageEncoding = MessageEncoding(C.GoString(_message_encoding))
}
tree := &Tree{
Object: Object{
ptr: (*C.git_object)(_tree),
repo: data.repo,
},
cast_ptr: _tree,
}
var goParents []*C.git_commit signature, signatureField, err := data.options.CommitSigningCallback(commitContent)
if _parent_count > 0 {
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(_parents)),
Len: int(_parent_count),
Cap: int(_parent_count),
}
goParents = *(*[]*C.git_commit)(unsafe.Pointer(&hdr))
}
parents := make([]*Commit, int(_parent_count))
for i, p := range goParents {
parents[i] = &Commit{
Object: Object{
ptr: (*C.git_object)(p),
repo: data.repo,
},
cast_ptr: p,
}
}
if data.options.CommitCreateCallback != nil {
oid, err := data.options.CommitCreateCallback(
newSignatureFromC(_author),
newSignatureFromC(_committer),
messageEncoding,
C.GoString(_message),
tree,
parents...,
)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
if oid == nil {
return C.int(ErrorCodePassthrough)
}
*_out = *oid.toC()
} else if data.options.CommitSigningCallback != nil {
commitContent, err := data.repo.CreateCommitBuffer(
newSignatureFromC(_author),
newSignatureFromC(_committer),
messageEncoding,
C.GoString(_message),
tree,
parents...,
)
if err != nil { if err != nil {
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
@ -160,52 +92,55 @@ func commitCreateCallback(
return setCallbackError(errorMessage, err) return setCallbackError(errorMessage, err)
} }
signature, signatureField, err := data.options.CommitSigningCallback(string(commitContent)) fillBuf := func(bufData string, buf *C.git_buf) error {
if err != nil { clen := C.size_t(len(bufData))
if data.errorTarget != nil { cstr := unsafe.Pointer(C.CString(bufData))
*data.errorTarget = err defer C.free(cstr)
}
return setCallbackError(errorMessage, err) // libgit2 requires the contents of the buffer to be NULL-terminated.
// C.CString() guarantees that the returned buffer will be
// NULL-terminated, so we can safely copy the terminator.
if int(C.git_buf_set(buf, cstr, clen+1)) != 0 {
return errors.New("could not set buffer")
} }
oid, err := data.repo.CreateCommitWithSignature(string(commitContent), signature, signatureField) return nil
}
if signatureField != "" {
err := fillBuf(signatureField, _signature_field)
if err != nil { if err != nil {
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
} }
return setCallbackError(errorMessage, err) return setCallbackError(errorMessage, err)
} }
*_out = *oid.toC() }
err = fillBuf(signature, _signature)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
} }
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
// RebaseOptions are used to tell the rebase machinery how to operate. // RebaseOptions are used to tell the rebase machinery how to operate
type RebaseOptions struct { type RebaseOptions struct {
Version uint
Quiet int Quiet int
InMemory int InMemory int
RewriteNotesRef string RewriteNotesRef string
MergeOptions MergeOptions MergeOptions MergeOptions
CheckoutOptions CheckoutOptions CheckoutOptions CheckoutOptions
// CommitCreateCallback is an optional callback that allows users to override
// commit creation when rebasing. If specified, users can create
// their own commit and provide the commit ID, which may be useful for
// signing commits or otherwise customizing the commit creation. If this
// callback returns a nil Oid, then the rebase will continue to create the
// commit.
CommitCreateCallback CommitCreateCallback
// Deprecated: CommitSigningCallback is an optional callback that will be
// called with the commit content, allowing a signature to be added to the
// rebase commit. This field is only used when rebasing. This callback is
// not invoked if a CommitCreateCallback is specified. CommitCreateCallback
// should be used instead of this.
CommitSigningCallback CommitSigningCallback CommitSigningCallback CommitSigningCallback
} }
type rebaseOptionsData struct { type rebaseOptionsData struct {
options *RebaseOptions options *RebaseOptions
repo *Repository
errorTarget *error errorTarget *error
} }
@ -225,6 +160,7 @@ func DefaultRebaseOptions() (RebaseOptions, error) {
func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions { func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions {
return RebaseOptions{ return RebaseOptions{
Version: uint(opts.version),
Quiet: int(opts.quiet), Quiet: int(opts.quiet),
InMemory: int(opts.inmemory), InMemory: int(opts.inmemory),
RewriteNotesRef: C.GoString(opts.rewrite_notes_ref), RewriteNotesRef: C.GoString(opts.rewrite_notes_ref),
@ -233,7 +169,7 @@ func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions {
} }
} }
func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, repo *Repository, errorTarget *error) *C.git_rebase_options { func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, errorTarget *error) *C.git_rebase_options {
C.git_rebase_options_init(copts, C.GIT_REBASE_OPTIONS_VERSION) C.git_rebase_options_init(copts, C.GIT_REBASE_OPTIONS_VERSION)
if opts == nil { if opts == nil {
return nil return nil
@ -245,10 +181,9 @@ func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, rep
populateMergeOptions(&copts.merge_options, &opts.MergeOptions) populateMergeOptions(&copts.merge_options, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget) populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget)
if opts.CommitCreateCallback != nil || opts.CommitSigningCallback != nil { if opts.CommitSigningCallback != nil {
data := &rebaseOptionsData{ data := &rebaseOptionsData{
options: opts, options: opts,
repo: repo,
errorTarget: errorTarget, errorTarget: errorTarget,
} }
C._go_git_populate_rebase_callbacks(copts) C._go_git_populate_rebase_callbacks(copts)
@ -279,7 +214,6 @@ func mapEmptyStringToNull(ref string) *C.char {
// Rebase is the struct representing a Rebase object. // Rebase is the struct representing a Rebase object.
type Rebase struct { type Rebase struct {
doNotCompare
ptr *C.git_rebase ptr *C.git_rebase
r *Repository r *Repository
options *C.git_rebase_options options *C.git_rebase_options
@ -304,7 +238,7 @@ func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedComm
var ptr *C.git_rebase var ptr *C.git_rebase
var err error var err error
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, r, &err) cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err)
ret := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, cOpts) ret := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, cOpts)
runtime.KeepAlive(branch) runtime.KeepAlive(branch)
runtime.KeepAlive(upstream) runtime.KeepAlive(upstream)
@ -318,7 +252,7 @@ func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedComm
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
return newRebaseFromC(ptr, r, cOpts), nil return newRebaseFromC(ptr, cOpts), nil
} }
// OpenRebase opens an existing rebase that was previously started by either an invocation of InitRebase or by another client. // OpenRebase opens an existing rebase that was previously started by either an invocation of InitRebase or by another client.
@ -328,7 +262,7 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) {
var ptr *C.git_rebase var ptr *C.git_rebase
var err error var err error
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, r, &err) cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err)
ret := C.git_rebase_open(&ptr, r.ptr, cOpts) ret := C.git_rebase_open(&ptr, r.ptr, cOpts)
runtime.KeepAlive(r) runtime.KeepAlive(r)
if ret == C.int(ErrorCodeUser) && err != nil { if ret == C.int(ErrorCodeUser) && err != nil {
@ -340,7 +274,7 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
return newRebaseFromC(ptr, r, cOpts), nil return newRebaseFromC(ptr, cOpts), nil
} }
// OperationAt gets the rebase operation specified by the given index. // OperationAt gets the rebase operation specified by the given index.
@ -392,27 +326,6 @@ func (rebase *Rebase) Next() (*RebaseOperation, error) {
return newRebaseOperationFromC(ptr), nil return newRebaseOperationFromC(ptr), nil
} }
// InmemoryIndex gets the index produced by the last operation, which is the
// result of `Next()` and which will be committed by the next invocation of
// `Commit()`. This is useful for resolving conflicts in an in-memory rebase
// before committing them.
//
// This is only applicable for in-memory rebases; for rebases within a working
// directory, the changes were applied to the repository's index.
func (rebase *Rebase) InmemoryIndex() (*Index, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_index
err := C.git_rebase_inmemory_index(&ptr, rebase.ptr)
runtime.KeepAlive(rebase)
if err < 0 {
return nil, MakeGitError(err)
}
return newIndexFromC(ptr, rebase.r), nil
}
// Commit commits the current patch. // Commit commits the current patch.
// You must have resolved any conflicts that were introduced during the patch application from the Next() invocation. // You must have resolved any conflicts that were introduced during the patch application from the Next() invocation.
func (rebase *Rebase) Commit(ID *Oid, author, committer *Signature, message string) error { func (rebase *Rebase) Commit(ID *Oid, author, committer *Signature, message string) error {
@ -478,8 +391,8 @@ func (r *Rebase) Free() {
freeRebaseOptions(r.options) freeRebaseOptions(r.options)
} }
func newRebaseFromC(ptr *C.git_rebase, repo *Repository, opts *C.git_rebase_options) *Rebase { func newRebaseFromC(ptr *C.git_rebase, opts *C.git_rebase_options) *Rebase {
rebase := &Rebase{ptr: ptr, r: repo, options: opts} rebase := &Rebase{ptr: ptr, options: opts}
runtime.SetFinalizer(rebase, (*Rebase).Free) runtime.SetFinalizer(rebase, (*Rebase).Free)
return rebase return rebase
} }

View File

@ -8,86 +8,12 @@ import (
"testing" "testing"
"time" "time"
"github.com/ProtonMail/go-crypto/openpgp" "golang.org/x/crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet" "golang.org/x/crypto/openpgp/packet"
) )
// Tests // Tests
func TestRebaseInMemoryWithConflict(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
// Create two branches with common history, where both modify "common-file"
// in a conflicting way.
_, err := commitSomething(repo, "common-file", "a\nb\nc\n", commitOptions{})
checkFatal(t, err)
checkFatal(t, createBranch(repo, "branch-a"))
checkFatal(t, createBranch(repo, "branch-b"))
checkFatal(t, repo.SetHead("refs/heads/branch-a"))
_, err = commitSomething(repo, "common-file", "1\nb\nc\n", commitOptions{})
checkFatal(t, err)
checkFatal(t, repo.SetHead("refs/heads/branch-b"))
_, err = commitSomething(repo, "common-file", "x\nb\nc\n", commitOptions{})
checkFatal(t, err)
branchA, err := repo.LookupBranch("branch-a", BranchLocal)
checkFatal(t, err)
onto, err := repo.AnnotatedCommitFromRef(branchA.Reference)
checkFatal(t, err)
// We then rebase "branch-b" onto "branch-a" in-memory, which should result
// in a conflict.
rebase, err := repo.InitRebase(nil, nil, onto, &RebaseOptions{InMemory: 1})
checkFatal(t, err)
_, err = rebase.Next()
checkFatal(t, err)
index, err := rebase.InmemoryIndex()
checkFatal(t, err)
// We simply resolve the conflict and commit the rebase.
if !index.HasConflicts() {
t.Fatal("expected index to have conflicts")
}
conflict, err := index.Conflict("common-file")
checkFatal(t, err)
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
checkFatal(t, err)
resolvedEntry := *conflict.Our
resolvedEntry.Id = resolvedBlobID
checkFatal(t, index.Add(&resolvedEntry))
checkFatal(t, index.RemoveConflict("common-file"))
var commitID Oid
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
checkFatal(t, rebase.Finish())
// And then assert that we can look up the new merge commit, and that the
// "common-file" has the expected contents.
commit, err := repo.LookupCommit(&commitID)
checkFatal(t, err)
if commit.Message() != "rebased message" {
t.Fatalf("unexpected commit message %q", commit.Message())
}
tree, err := commit.Tree()
checkFatal(t, err)
blob, err := repo.LookupBlob(tree.EntryByName("common-file").Id)
checkFatal(t, err)
if string(blob.Contents()) != "resolved contents" {
t.Fatalf("unexpected resolved blob contents %q", string(blob.Contents()))
}
}
func TestRebaseAbort(t *testing.T) { func TestRebaseAbort(t *testing.T) {
// TEST DATA // TEST DATA
@ -319,7 +245,7 @@ func checkCommitSigned(t *testing.T, entity *openpgp.Entity, commit *Commit) err
return err return err
} }
_, err = openpgp.CheckArmoredDetachedSignature(openpgp.EntityList{entity}, strings.NewReader(signedData), bytes.NewBufferString(signature), nil) _, err = openpgp.CheckArmoredDetachedSignature(openpgp.EntityList{entity}, strings.NewReader(signedData), bytes.NewBufferString(signature))
if err != nil { if err != nil {
t.Logf("Commit is not signed correctly\n%s", commit.ContentToSign()) t.Logf("Commit is not signed correctly\n%s", commit.ContentToSign())
return err return err

View File

@ -13,13 +13,11 @@ import (
) )
type Refdb struct { type Refdb struct {
doNotCompare
ptr *C.git_refdb ptr *C.git_refdb
r *Repository r *Repository
} }
type RefdbBackend struct { type RefdbBackend struct {
doNotCompare
ptr *C.git_refdb_backend ptr *C.git_refdb_backend
} }
@ -40,7 +38,7 @@ func (v *Repository) NewRefdb() (refdb *Refdb, err error) {
} }
func NewRefdbBackendFromC(ptr unsafe.Pointer) (backend *RefdbBackend) { func NewRefdbBackendFromC(ptr unsafe.Pointer) (backend *RefdbBackend) {
backend = &RefdbBackend{ptr: (*C.git_refdb_backend)(ptr)} backend = &RefdbBackend{(*C.git_refdb_backend)(ptr)}
return backend return backend
} }

View File

@ -17,13 +17,11 @@ const (
) )
type Reference struct { type Reference struct {
doNotCompare
ptr *C.git_reference ptr *C.git_reference
repo *Repository repo *Repository
} }
type ReferenceCollection struct { type ReferenceCollection struct {
doNotCompare
repo *Repository repo *Repository
} }
@ -365,13 +363,11 @@ func (v *Reference) Free() {
} }
type ReferenceIterator struct { type ReferenceIterator struct {
doNotCompare
ptr *C.git_reference_iterator ptr *C.git_reference_iterator
repo *Repository repo *Repository
} }
type ReferenceNameIterator struct { type ReferenceNameIterator struct {
doNotCompare
*ReferenceIterator *ReferenceIterator
} }
@ -426,7 +422,7 @@ func (repo *Repository) NewReferenceIteratorGlob(glob string) (*ReferenceIterato
} }
func (i *ReferenceIterator) Names() *ReferenceNameIterator { func (i *ReferenceIterator) Names() *ReferenceNameIterator {
return &ReferenceNameIterator{ReferenceIterator: i} return &ReferenceNameIterator{i}
} }
// NextName retrieves the next reference name. If the iteration is over, // NextName retrieves the next reference name. If the iteration is over,
@ -476,7 +472,7 @@ func (v *ReferenceIterator) Free() {
C.git_reference_iterator_free(v.ptr) C.git_reference_iterator_free(v.ptr)
} }
// ReferenceNameIsValid returns whether the reference name is well-formed. // ReferenceIsValidName returns whether the reference name is well-formed.
// //
// Valid reference names must follow one of two patterns: // Valid reference names must follow one of two patterns:
// //
@ -486,19 +482,11 @@ func (v *ReferenceIterator) Free() {
// 2. Names prefixed with "refs/" can be almost anything. You must avoid // 2. Names prefixed with "refs/" can be almost anything. You must avoid
// the characters '~', '^', ':', ' \ ', '?', '[', and '*', and the sequences // the characters '~', '^', ':', ' \ ', '?', '[', and '*', and the sequences
// ".." and " @ {" which have special meaning to revparse. // ".." and " @ {" which have special meaning to revparse.
func ReferenceNameIsValid(name string) (bool, error) { func ReferenceIsValidName(name string) bool {
cname := C.CString(name) cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread() return C.git_reference_is_valid_name(cname) == 1
defer runtime.UnlockOSThread()
var valid C.int
ret := C.git_reference_name_is_valid(&valid, cname)
if ret < 0 {
return false, MakeGitError(ret)
}
return valid == 1, nil
} }
const ( const (

View File

@ -214,16 +214,12 @@ func TestIsNote(t *testing.T) {
} }
} }
func TestReferenceNameIsValid(t *testing.T) { func TestReferenceIsValidName(t *testing.T) {
t.Parallel() t.Parallel()
valid, err := ReferenceNameIsValid("HEAD") if !ReferenceIsValidName("HEAD") {
checkFatal(t, err)
if !valid {
t.Errorf("HEAD should be a valid reference name") t.Errorf("HEAD should be a valid reference name")
} }
valid, err = ReferenceNameIsValid("HEAD1") if ReferenceIsValidName("HEAD1") {
checkFatal(t, err)
if valid {
t.Errorf("HEAD1 should not be a valid reference name") t.Errorf("HEAD1 should not be a valid reference name")
} }
} }

175
reflog.go Normal file
View File

@ -0,0 +1,175 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
// Reflog is a log of changes for a reference
type Reflog struct {
ptr *C.git_reflog
repo *Repository
name string
}
func newRefLogFromC(ptr *C.git_reflog, repo *Repository, name string) *Reflog {
l := &Reflog{
ptr: ptr,
repo: repo,
name: name,
}
runtime.SetFinalizer(l, (*Reflog).Free)
return l
}
func (repo *Repository) ReadReflog(name string) (*Reflog, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var ptr *C.git_reflog
ecode := C.git_reflog_read(&ptr, repo.ptr, cname)
runtime.KeepAlive(repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newRefLogFromC(ptr, repo, name), nil
}
func (repo *Repository) DeleteReflog(name string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ecode := C.git_reflog_delete(repo.ptr, cname)
runtime.KeepAlive(repo)
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (repo *Repository) RenameReflog(oldName, newName string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cOldName := C.CString(oldName)
defer C.free(unsafe.Pointer(cOldName))
cNewName := C.CString(newName)
defer C.free(unsafe.Pointer(cNewName))
ecode := C.git_reflog_rename(repo.ptr, cOldName, cNewName)
runtime.KeepAlive(repo)
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (l *Reflog) Write() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_reflog_write(l.ptr)
runtime.KeepAlive(l)
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (l *Reflog) EntryCount() uint {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
count := C.git_reflog_entrycount(l.ptr)
runtime.KeepAlive(l)
return uint(count)
}
// ReflogEntry specifies a reference change
type ReflogEntry struct {
Old *Oid
New *Oid
Committer *Signature
Message string // may be empty
}
func newReflogEntry(entry *C.git_reflog_entry) *ReflogEntry {
return &ReflogEntry{
New: newOidFromC(C.git_reflog_entry_id_new(entry)),
Old: newOidFromC(C.git_reflog_entry_id_old(entry)),
Committer: newSignatureFromC(C.git_reflog_entry_committer(entry)),
Message: C.GoString(C.git_reflog_entry_message(entry)),
}
}
func (l *Reflog) EntryByIndex(index uint) *ReflogEntry {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
entry := C.git_reflog_entry_byindex(l.ptr, C.size_t(index))
if entry == nil {
return nil
}
goEntry := newReflogEntry(entry)
runtime.KeepAlive(l)
return goEntry
}
func (l *Reflog) DropEntry(index uint, rewriteHistory bool) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var rewriteHistoryInt int
if rewriteHistory {
rewriteHistoryInt = 1
}
ecode := C.git_reflog_drop(l.ptr, C.size_t(index), C.int(rewriteHistoryInt))
runtime.KeepAlive(l)
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
func (l *Reflog) AppendEntry(oid *Oid, committer *Signature, message string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cSignature, err := committer.toC()
if err != nil {
return err
}
defer C.git_signature_free(cSignature)
cMsg := C.CString(message)
defer C.free(unsafe.Pointer(cMsg))
C.git_reflog_append(l.ptr, oid.toC(), cSignature, cMsg)
runtime.KeepAlive(l)
return nil
}
func (l *Reflog) Free() {
runtime.SetFinalizer(l, nil)
C.git_reflog_free(l.ptr)
}

162
reflog_test.go Normal file
View File

@ -0,0 +1,162 @@
package git
import (
"fmt"
"reflect"
"testing"
"time"
)
func allReflogEntries(t *testing.T, repo *Repository, refName string) (entries []*ReflogEntry) {
rl, err := repo.ReadReflog(refName)
checkFatal(t, err)
defer rl.Free()
for i := uint(0); i < rl.EntryCount(); i++ {
entries = append(entries, rl.EntryByIndex(i))
}
return entries
}
// assertEntriesEqual will assert that the reflogs match with the exception of
// the signature time (it is not reliably deterministic to predict the
// signature time during many reference updates)
func assertEntriesEqual(t *testing.T, got, want []*ReflogEntry) {
if len(got) != len(want) {
t.Fatalf("got %d length, wanted %d length", len(got), len(want))
}
for i := 0; i < len(got); i++ {
gi := got[i]
wi := want[i]
// remove the signature time to make the results deterministic
gi.Committer.When = time.Time{}
wi.Committer.When = time.Time{}
// check committer separately to print results clearly
if !reflect.DeepEqual(gi.Committer, wi.Committer) {
t.Fatalf("got committer %v, want committer %v",
gi.Committer, wi.Committer)
}
if !reflect.DeepEqual(gi, wi) {
t.Fatalf("got %v, want %v", gi, wi)
}
}
}
func TestReflog(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitID, treeId := seedTestRepo(t, repo)
testRefName := "refs/heads/test"
// configure committer for deterministic reflog entries
cfg, err := repo.Config()
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
}
checkFatal(t, cfg.SetString("user.name", sig.Name))
checkFatal(t, cfg.SetString("user.email", sig.Email))
checkFatal(t, repo.References.EnsureLog(testRefName))
_, err = repo.References.Create(testRefName, commitID, true, "first update")
checkFatal(t, err)
got := allReflogEntries(t, repo, testRefName)
want := []*ReflogEntry{
&ReflogEntry{
New: commitID,
Old: &Oid{},
Committer: sig,
Message: "first update",
},
}
// create additional commits and verify they are added to reflog
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
for i := 0; i < 10; i++ {
nextEntry := &ReflogEntry{
Old: commitID,
Committer: sig,
Message: fmt.Sprintf("commit: %d", i),
}
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
commitID, err = repo.CreateCommit(testRefName, sig, sig, fmt.Sprint(i), tree, commit)
checkFatal(t, err)
nextEntry.New = commitID
want = append([]*ReflogEntry{nextEntry}, want...)
}
t.Run("ReadReflog", func(t *testing.T) {
got = allReflogEntries(t, repo, testRefName)
assertEntriesEqual(t, got, want)
})
t.Run("DropEntry", func(t *testing.T) {
rl, err := repo.ReadReflog(testRefName)
checkFatal(t, err)
defer rl.Free()
gotBefore := allReflogEntries(t, repo, testRefName)
checkFatal(t, rl.DropEntry(0, false))
checkFatal(t, rl.Write())
gotAfter := allReflogEntries(t, repo, testRefName)
assertEntriesEqual(t, gotAfter, gotBefore[1:])
})
t.Run("AppendEntry", func(t *testing.T) {
logs := allReflogEntries(t, repo, testRefName)
rl, err := repo.ReadReflog(testRefName)
checkFatal(t, err)
defer rl.Free()
newOID := NewOidFromBytes([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
checkFatal(t, rl.AppendEntry(newOID, sig, "synthetic"))
checkFatal(t, rl.Write())
want := append([]*ReflogEntry{
&ReflogEntry{
New: newOID,
Old: logs[0].New,
Committer: sig,
Message: "synthetic",
},
}, logs...)
got := allReflogEntries(t, repo, testRefName)
assertEntriesEqual(t, got, want)
})
t.Run("RenameReflog", func(t *testing.T) {
logs := allReflogEntries(t, repo, testRefName)
newRefName := "refs/heads/new"
checkFatal(t, repo.RenameReflog(testRefName, newRefName))
assertEntriesEqual(t, allReflogEntries(t, repo, testRefName), nil)
assertEntriesEqual(t, allReflogEntries(t, repo, newRefName), logs)
checkFatal(t, repo.RenameReflog(newRefName, testRefName))
assertEntriesEqual(t, allReflogEntries(t, repo, testRefName), logs)
assertEntriesEqual(t, allReflogEntries(t, repo, newRefName), nil)
})
t.Run("DeleteReflog", func(t *testing.T) {
checkFatal(t, repo.DeleteReflog(testRefName))
assertEntriesEqual(t, allReflogEntries(t, repo, testRefName), nil)
})
}

View File

@ -1,149 +0,0 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
type Refspec struct {
doNotCompare
ptr *C.git_refspec
}
// ParseRefspec parses a given refspec string
func ParseRefspec(input string, isFetch bool) (*Refspec, error) {
var ptr *C.git_refspec
cinput := C.CString(input)
defer C.free(unsafe.Pointer(cinput))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_refspec_parse(&ptr, cinput, cbool(isFetch))
if ret < 0 {
return nil, MakeGitError(ret)
}
spec := &Refspec{ptr: ptr}
runtime.SetFinalizer(spec, (*Refspec).Free)
return spec, nil
}
// Free releases a refspec object which has been created by ParseRefspec
func (s *Refspec) Free() {
runtime.SetFinalizer(s, nil)
C.git_refspec_free(s.ptr)
}
// Direction returns the refspec's direction
func (s *Refspec) Direction() ConnectDirection {
direction := C.git_refspec_direction(s.ptr)
return ConnectDirection(direction)
}
// Src returns the refspec's source specifier
func (s *Refspec) Src() string {
var ret string
cstr := C.git_refspec_src(s.ptr)
if cstr != nil {
ret = C.GoString(cstr)
}
runtime.KeepAlive(s)
return ret
}
// Dst returns the refspec's destination specifier
func (s *Refspec) Dst() string {
var ret string
cstr := C.git_refspec_dst(s.ptr)
if cstr != nil {
ret = C.GoString(cstr)
}
runtime.KeepAlive(s)
return ret
}
// Force returns the refspec's force-update setting
func (s *Refspec) Force() bool {
force := C.git_refspec_force(s.ptr)
return force != 0
}
// String returns the refspec's string representation
func (s *Refspec) String() string {
var ret string
cstr := C.git_refspec_string(s.ptr)
if cstr != nil {
ret = C.GoString(cstr)
}
runtime.KeepAlive(s)
return ret
}
// SrcMatches checks if a refspec's source descriptor matches a reference
func (s *Refspec) SrcMatches(refname string) bool {
cname := C.CString(refname)
defer C.free(unsafe.Pointer(cname))
matches := C.git_refspec_src_matches(s.ptr, cname)
return matches != 0
}
// SrcMatches checks if a refspec's destination descriptor matches a reference
func (s *Refspec) DstMatches(refname string) bool {
cname := C.CString(refname)
defer C.free(unsafe.Pointer(cname))
matches := C.git_refspec_dst_matches(s.ptr, cname)
return matches != 0
}
// Transform a reference to its target following the refspec's rules
func (s *Refspec) Transform(refname string) (string, error) {
buf := C.git_buf{}
cname := C.CString(refname)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_refspec_transform(&buf, s.ptr, cname)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_dispose(&buf)
return C.GoString(buf.ptr), nil
}
// Rtransform converts a target reference to its source reference following the
// refspec's rules
func (s *Refspec) Rtransform(refname string) (string, error) {
buf := C.git_buf{}
cname := C.CString(refname)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_refspec_rtransform(&buf, s.ptr, cname)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_dispose(&buf)
return C.GoString(buf.ptr), nil
}

View File

@ -1,75 +0,0 @@
package git
import (
"testing"
)
func TestRefspec(t *testing.T) {
t.Parallel()
const (
input = "+refs/heads/*:refs/remotes/origin/*"
mainLocal = "refs/heads/main"
mainRemote = "refs/remotes/origin/main"
)
refspec, err := ParseRefspec(input, true)
checkFatal(t, err)
// Accessors
s := refspec.String()
if s != input {
t.Errorf("expected string %q, got %q", input, s)
}
if d := refspec.Direction(); d != ConnectDirectionFetch {
t.Errorf("expected fetch refspec, got direction %v", d)
}
if pat, expected := refspec.Src(), "refs/heads/*"; pat != expected {
t.Errorf("expected refspec src %q, got %q", expected, pat)
}
if pat, expected := refspec.Dst(), "refs/remotes/origin/*"; pat != expected {
t.Errorf("expected refspec dst %q, got %q", expected, pat)
}
if !refspec.Force() {
t.Error("expected refspec force flag")
}
// SrcMatches
if !refspec.SrcMatches(mainLocal) {
t.Errorf("refspec source did not match %q", mainLocal)
}
if refspec.SrcMatches("refs/tags/v1.0") {
t.Error("refspec source matched under refs/tags")
}
// DstMatches
if !refspec.DstMatches(mainRemote) {
t.Errorf("refspec destination did not match %q", mainRemote)
}
if refspec.DstMatches("refs/tags/v1.0") {
t.Error("refspec destination matched under refs/tags")
}
// Transforms
fromLocal, err := refspec.Transform(mainLocal)
checkFatal(t, err)
if fromLocal != mainRemote {
t.Errorf("transform by refspec returned %s; expected %s", fromLocal, mainRemote)
}
fromRemote, err := refspec.Rtransform(mainRemote)
checkFatal(t, err)
if fromRemote != mainLocal {
t.Errorf("rtransform by refspec returned %s; expected %s", fromRemote, mainLocal)
}
}

330
remote.go
View File

@ -12,33 +12,12 @@ import "C"
import ( import (
"crypto/x509" "crypto/x509"
"errors" "errors"
"fmt"
"reflect" "reflect"
"runtime" "runtime"
"strings" "strings"
"sync"
"unsafe" "unsafe"
"golang.org/x/crypto/ssh"
) )
// RemoteCreateOptionsFlag is Remote creation options flags
type RemoteCreateOptionsFlag uint
const (
// Ignore the repository apply.insteadOf configuration
RemoteCreateSkipInsteadof RemoteCreateOptionsFlag = C.GIT_REMOTE_CREATE_SKIP_INSTEADOF
// Don't build a fetchspec from the name if none is set
RemoteCreateSkipDefaultFetchspec RemoteCreateOptionsFlag = C.GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC
)
// RemoteCreateOptions contains options for creating a remote
type RemoteCreateOptions struct {
Name string
FetchSpec string
Flags RemoteCreateOptionsFlag
}
type TransferProgress struct { type TransferProgress struct {
TotalObjects uint TotalObjects uint
IndexedObjects uint IndexedObjects uint
@ -70,15 +49,15 @@ const (
ConnectDirectionPush ConnectDirection = C.GIT_DIRECTION_PUSH ConnectDirectionPush ConnectDirection = C.GIT_DIRECTION_PUSH
) )
type TransportMessageCallback func(str string) error type TransportMessageCallback func(str string) ErrorCode
type CompletionCallback func(RemoteCompletion) error type CompletionCallback func(RemoteCompletion) ErrorCode
type CredentialsCallback func(url string, username_from_url string, allowed_types CredentialType) (*Credential, error) type CredentialsCallback func(url string, username_from_url string, allowed_types CredentialType) (*Credential, error)
type TransferProgressCallback func(stats TransferProgress) error type TransferProgressCallback func(stats TransferProgress) ErrorCode
type UpdateTipsCallback func(refname string, a *Oid, b *Oid) error type UpdateTipsCallback func(refname string, a *Oid, b *Oid) ErrorCode
type CertificateCheckCallback func(cert *Certificate, valid bool, hostname string) error type CertificateCheckCallback func(cert *Certificate, valid bool, hostname string) ErrorCode
type PackbuilderProgressCallback func(stage int32, current, total uint32) error type PackbuilderProgressCallback func(stage int32, current, total uint32) ErrorCode
type PushTransferProgressCallback func(current, total uint32, bytes uint) error type PushTransferProgressCallback func(current, total uint32, bytes uint) ErrorCode
type PushUpdateReferenceCallback func(refname, status string) error type PushUpdateReferenceCallback func(refname, status string) ErrorCode
type RemoteCallbacks struct { type RemoteCallbacks struct {
SidebandProgressCallback TransportMessageCallback SidebandProgressCallback TransportMessageCallback
@ -147,17 +126,6 @@ type FetchOptions struct {
ProxyOptions ProxyOptions ProxyOptions ProxyOptions
} }
type RemoteConnectOptions struct {
// Proxy options to use for this fetch operation
ProxyOptions ProxyOptions
}
func remoteConnectOptionsFromC(copts *C.git_remote_connect_options) *RemoteConnectOptions {
return &RemoteConnectOptions{
ProxyOptions: proxyOptionsFromC(&copts.proxy_opts),
}
}
type ProxyType uint type ProxyType uint
const ( const (
@ -182,79 +150,10 @@ type ProxyOptions struct {
Url string Url string
} }
func proxyOptionsFromC(copts *C.git_proxy_options) ProxyOptions {
return ProxyOptions{
Type: ProxyType(copts._type),
Url: C.GoString(copts.url),
}
}
type Remote struct { type Remote struct {
doNotCompare
ptr *C.git_remote ptr *C.git_remote
callbacks RemoteCallbacks callbacks RemoteCallbacks
repo *Repository repo *Repository
// weak indicates that a remote is a weak pointer and should not be
// freed.
weak bool
}
type remotePointerList struct {
sync.RWMutex
// stores the Go pointers
pointers map[*C.git_remote]*Remote
}
func newRemotePointerList() *remotePointerList {
return &remotePointerList{
pointers: make(map[*C.git_remote]*Remote),
}
}
// 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 *remotePointerList) track(remote *Remote) {
v.Lock()
v.pointers[remote.ptr] = remote
v.Unlock()
runtime.SetFinalizer(remote, (*Remote).Free)
}
// untrack stops tracking the git_remote pointer.
func (v *remotePointerList) untrack(remote *Remote) {
v.Lock()
delete(v.pointers, remote.ptr)
v.Unlock()
}
// clear stops tracking all the git_remote pointers.
func (v *remotePointerList) clear() {
v.Lock()
var remotes []*Remote
for remotePtr, remote := range v.pointers {
remotes = append(remotes, remote)
delete(v.pointers, remotePtr)
}
v.Unlock()
for _, remote := range remotes {
remote.free()
}
}
// get retrieves the pointer from the given *git_remote.
func (v *remotePointerList) get(ptr *C.git_remote) (*Remote, bool) {
v.RLock()
defer v.RUnlock()
r, ok := v.pointers[ptr]
if !ok {
return nil, false
}
return r, true
} }
type CertificateKind uint type CertificateKind uint
@ -267,32 +166,28 @@ const (
// Certificate represents the two possible certificates which libgit2 // Certificate represents the two possible certificates which libgit2
// knows it might find. If Kind is CertficateX509 then the X509 field // knows it might find. If Kind is CertficateX509 then the X509 field
// will be filled. If Kind is CertificateHostkey then the Hostkey // will be filled. If Kind is CertificateHostkey then the Hostkey
// field will be filled. // field will be fille.d
type Certificate struct { type Certificate struct {
Kind CertificateKind Kind CertificateKind
X509 *x509.Certificate X509 *x509.Certificate
Hostkey HostkeyCertificate Hostkey HostkeyCertificate
} }
// HostkeyKind is a bitmask of the available hashes in HostkeyCertificate.
type HostkeyKind uint type HostkeyKind uint
const ( const (
HostkeyMD5 HostkeyKind = C.GIT_CERT_SSH_MD5 HostkeyMD5 HostkeyKind = C.GIT_CERT_SSH_MD5
HostkeySHA1 HostkeyKind = C.GIT_CERT_SSH_SHA1 HostkeySHA1 HostkeyKind = C.GIT_CERT_SSH_SHA1
HostkeySHA256 HostkeyKind = C.GIT_CERT_SSH_SHA256 HostkeySHA256 HostkeyKind = C.GIT_CERT_SSH_SHA256
HostkeyRaw HostkeyKind = C.GIT_CERT_SSH_RAW
) )
// Server host key information. A bitmask containing the available fields. // Server host key information. A bitmask containing the available fields.
// Check for combinations of: HostkeyMD5, HostkeySHA1, HostkeySHA256, HostkeyRaw. // Check for combinations of: HostkeyMD5, HostkeySHA1, HostkeySHA256.
type HostkeyCertificate struct { type HostkeyCertificate struct {
Kind HostkeyKind Kind HostkeyKind
HashMD5 [16]byte HashMD5 [16]byte
HashSHA1 [20]byte HashSHA1 [20]byte
HashSHA256 [32]byte HashSHA256 [32]byte
Hostkey []byte
SSHPublicKey ssh.PublicKey
} }
type PushOptions struct { type PushOptions struct {
@ -303,9 +198,6 @@ type PushOptions struct {
// Headers are extra headers for the push operation. // Headers are extra headers for the push operation.
Headers []string Headers []string
// Proxy options to use for this push operation
ProxyOptions ProxyOptions
} }
type RemoteHead struct { type RemoteHead struct {
@ -347,8 +239,10 @@ func sidebandProgressCallback(errorMessage **C.char, _str *C.char, _len C.int, h
if data.callbacks.SidebandProgressCallback == nil { if data.callbacks.SidebandProgressCallback == nil {
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
err := data.callbacks.SidebandProgressCallback(C.GoStringN(_str, _len)) str := C.GoStringN(_str, _len)
if err != nil { ret := data.callbacks.SidebandProgressCallback(str)
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
} }
@ -358,13 +252,14 @@ func sidebandProgressCallback(errorMessage **C.char, _str *C.char, _len C.int, h
} }
//export completionCallback //export completionCallback
func completionCallback(errorMessage **C.char, completionType C.git_remote_completion_type, handle unsafe.Pointer) C.int { func completionCallback(errorMessage **C.char, completion_type C.git_remote_completion_type, handle unsafe.Pointer) C.int {
data := pointerHandles.Get(handle).(*remoteCallbacksData) data := pointerHandles.Get(handle).(*remoteCallbacksData)
if data.callbacks.CompletionCallback == nil { if data.callbacks.CompletionCallback == nil {
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
err := data.callbacks.CompletionCallback(RemoteCompletion(completionType)) ret := data.callbacks.CompletionCallback(RemoteCompletion(completion_type))
if err != nil { if ret < 0 {
err := errors.New(ErrorCode(ret).String())
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
} }
@ -411,8 +306,9 @@ func transferProgressCallback(errorMessage **C.char, stats *C.git_transfer_progr
if data.callbacks.TransferProgressCallback == nil { if data.callbacks.TransferProgressCallback == nil {
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
err := data.callbacks.TransferProgressCallback(newTransferProgressFromC(stats)) ret := data.callbacks.TransferProgressCallback(newTransferProgressFromC(stats))
if err != nil { if ret < 0 {
err := errors.New(ErrorCode(ret).String())
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
} }
@ -436,8 +332,9 @@ func updateTipsCallback(
refname := C.GoString(_refname) refname := C.GoString(_refname)
a := newOidFromC(_a) a := newOidFromC(_a)
b := newOidFromC(_b) b := newOidFromC(_b)
err := data.callbacks.UpdateTipsCallback(refname, a, b) ret := data.callbacks.UpdateTipsCallback(refname, a, b)
if err != nil { if ret < 0 {
err := errors.New(ErrorCode(ret).String())
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
} }
@ -494,17 +391,6 @@ func certificateCheckCallback(
C.memcpy(unsafe.Pointer(&cert.Hostkey.HashMD5[0]), unsafe.Pointer(&ccert.hash_md5[0]), C.size_t(len(cert.Hostkey.HashMD5))) C.memcpy(unsafe.Pointer(&cert.Hostkey.HashMD5[0]), unsafe.Pointer(&ccert.hash_md5[0]), C.size_t(len(cert.Hostkey.HashMD5)))
C.memcpy(unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), unsafe.Pointer(&ccert.hash_sha1[0]), C.size_t(len(cert.Hostkey.HashSHA1))) C.memcpy(unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), unsafe.Pointer(&ccert.hash_sha1[0]), C.size_t(len(cert.Hostkey.HashSHA1)))
C.memcpy(unsafe.Pointer(&cert.Hostkey.HashSHA256[0]), unsafe.Pointer(&ccert.hash_sha256[0]), C.size_t(len(cert.Hostkey.HashSHA256))) C.memcpy(unsafe.Pointer(&cert.Hostkey.HashSHA256[0]), unsafe.Pointer(&ccert.hash_sha256[0]), C.size_t(len(cert.Hostkey.HashSHA256)))
if (cert.Hostkey.Kind & HostkeyRaw) == HostkeyRaw {
cert.Hostkey.Hostkey = C.GoBytes(unsafe.Pointer(ccert.hostkey), C.int(ccert.hostkey_len))
var err error
cert.Hostkey.SSHPublicKey, err = ssh.ParsePublicKey(cert.Hostkey.Hostkey)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
}
} else { } else {
err := errors.New("unsupported certificate type") err := errors.New("unsupported certificate type")
if data.errorTarget != nil { if data.errorTarget != nil {
@ -513,8 +399,9 @@ func certificateCheckCallback(
return setCallbackError(errorMessage, err) return setCallbackError(errorMessage, err)
} }
err := data.callbacks.CertificateCheckCallback(&cert, valid, host) ret := data.callbacks.CertificateCheckCallback(&cert, valid, host)
if err != nil { if ret < 0 {
err := errors.New(ErrorCode(ret).String())
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
} }
@ -530,8 +417,9 @@ func packProgressCallback(errorMessage **C.char, stage C.int, current, total C.u
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
err := data.callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total)) ret := data.callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total))
if err != nil { if ret < 0 {
err := errors.New(ErrorCode(ret).String())
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
} }
@ -547,8 +435,9 @@ func pushTransferProgressCallback(errorMessage **C.char, current, total C.uint,
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
err := data.callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes)) ret := data.callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes))
if err != nil { if ret < 0 {
err := errors.New(ErrorCode(ret).String())
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
} }
@ -564,8 +453,9 @@ func pushUpdateReferenceCallback(errorMessage **C.char, refname, status *C.char,
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
err := data.callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status)) ret := data.callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status))
if err != nil { if ret < 0 {
err := errors.New(ErrorCode(ret).String())
if data.errorTarget != nil { if data.errorTarget != nil {
*data.errorTarget = err *data.errorTarget = err
} }
@ -593,61 +483,24 @@ func freeProxyOptions(copts *C.git_proxy_options) {
C.free(unsafe.Pointer(copts.url)) C.free(unsafe.Pointer(copts.url))
} }
// RemoteNameIsValid returns whether the remote name is well-formed. // RemoteIsValidName returns whether the remote name is well-formed.
func RemoteNameIsValid(name string) (bool, error) { func RemoteIsValidName(name string) bool {
cname := C.CString(name) cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread() return C.git_remote_is_valid_name(cname) == 1
defer runtime.UnlockOSThread()
var valid C.int
ret := C.git_remote_name_is_valid(&valid, cname)
if ret < 0 {
return false, MakeGitError(ret)
}
return valid == 1, nil
} }
// free releases the resources of the Remote. // Free releases the resources of the Remote.
func (r *Remote) free() { func (r *Remote) Free() {
runtime.SetFinalizer(r, nil) runtime.SetFinalizer(r, nil)
C.git_remote_free(r.ptr) C.git_remote_free(r.ptr)
r.ptr = nil r.ptr = nil
r.repo = nil r.repo = nil
} }
// Free releases the resources of the Remote.
func (r *Remote) Free() {
r.repo.Remotes.untrackRemote(r)
if r.weak {
return
}
r.free()
}
type RemoteCollection struct { type RemoteCollection struct {
doNotCompare
repo *Repository repo *Repository
sync.RWMutex
remotes map[*C.git_remote]*Remote
}
func (c *RemoteCollection) trackRemote(r *Remote) {
c.Lock()
c.remotes[r.ptr] = r
c.Unlock()
remotePointers.track(r)
}
func (c *RemoteCollection) untrackRemote(r *Remote) {
c.Lock()
delete(c.remotes, r.ptr)
c.Unlock()
remotePointers.untrack(r)
} }
func (c *RemoteCollection) List() ([]string, error) { func (c *RemoteCollection) List() ([]string, error) {
@ -682,29 +535,7 @@ func (c *RemoteCollection) Create(name string, url string) (*Remote, error) {
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
c.trackRemote(remote) runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
}
// CreateWithOptions Creates a repository object with extended options.
func (c *RemoteCollection) CreateWithOptions(url string, option *RemoteCreateOptions) (*Remote, error) {
remote := &Remote{repo: c.repo}
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
copts := populateRemoteCreateOptions(&C.git_remote_create_options{}, option, c.repo)
defer freeRemoteCreateOptions(copts)
ret := C.git_remote_create_with_opts(&remote.ptr, curl, copts)
runtime.KeepAlive(c.repo)
if ret < 0 {
return nil, MakeGitError(ret)
}
c.trackRemote(remote)
return remote, nil return remote, nil
} }
@ -740,7 +571,7 @@ func (c *RemoteCollection) CreateWithFetchspec(name string, url string, fetch st
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
c.trackRemote(remote) runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil return remote, nil
} }
@ -757,7 +588,7 @@ func (c *RemoteCollection) CreateAnonymous(url string) (*Remote, error) {
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
c.trackRemote(remote) runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil return remote, nil
} }
@ -774,24 +605,10 @@ func (c *RemoteCollection) Lookup(name string) (*Remote, error) {
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
c.trackRemote(remote) runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil return remote, nil
} }
func (c *RemoteCollection) Free() {
var remotes []*Remote
c.Lock()
for remotePtr, remote := range c.remotes {
remotes = append(remotes, remote)
delete(c.remotes, remotePtr)
}
c.Unlock()
for _, remote := range remotes {
remotePointers.untrack(remote)
}
}
func (o *Remote) Name() string { func (o *Remote) Name() string {
s := C.git_remote_name(o.ptr) s := C.git_remote_name(o.ptr)
runtime.KeepAlive(o) runtime.KeepAlive(o)
@ -984,9 +801,7 @@ func populateFetchOptions(copts *C.git_fetch_options, opts *FetchOptions, errorT
} }
populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget) populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget)
copts.prune = C.git_fetch_prune_t(opts.Prune) copts.prune = C.git_fetch_prune_t(opts.Prune)
fmt.Println("populateFetchOptions() is broken. fixme!") copts.update_fetchhead = cbool(opts.UpdateFetchhead)
// fix this line: ./remote.go:988:27: cannot use cbool(opts.UpdateFetchhead) (value of type _Ctype_int) as _Ctype_uint value in assignment
// copts.update_fetchhead = cbool(opts.UpdateFetchhead)
copts.download_tags = C.git_remote_autotag_option_t(opts.DownloadTags) copts.download_tags = C.git_remote_autotag_option_t(opts.DownloadTags)
copts.custom_headers = C.git_strarray{ copts.custom_headers = C.git_strarray{
@ -1018,7 +833,6 @@ func populatePushOptions(copts *C.git_push_options, opts *PushOptions, errorTarg
strings: makeCStringsFromStrings(opts.Headers), strings: makeCStringsFromStrings(opts.Headers),
} }
populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget) populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget)
populateProxyOptions(&copts.proxy_opts, &opts.ProxyOptions)
return copts return copts
} }
@ -1028,7 +842,6 @@ func freePushOptions(copts *C.git_push_options) {
} }
untrackCallbacksPayload(&copts.callbacks) untrackCallbacksPayload(&copts.callbacks)
freeStrarray(&copts.custom_headers) freeStrarray(&copts.custom_headers)
freeProxyOptions(&copts.proxy_opts)
} }
// Fetch performs a fetch operation. refspecs specifies which refspecs // Fetch performs a fetch operation. refspecs specifies which refspecs
@ -1214,54 +1027,3 @@ func (o *Remote) Prune(callbacks *RemoteCallbacks) error {
} }
return nil return nil
} }
// DefaultApplyOptions returns default options for remote create
func DefaultRemoteCreateOptions() (*RemoteCreateOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
opts := C.git_remote_create_options{}
ecode := C.git_remote_create_options_init(&opts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return &RemoteCreateOptions{
Flags: RemoteCreateOptionsFlag(opts.flags),
}, nil
}
func populateRemoteCreateOptions(copts *C.git_remote_create_options, opts *RemoteCreateOptions, repo *Repository) *C.git_remote_create_options {
C.git_remote_create_options_init(copts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION)
if opts == nil {
return nil
}
var cRepository *C.git_repository
if repo != nil {
cRepository = repo.ptr
}
copts.repository = cRepository
copts.name = C.CString(opts.Name)
copts.fetchspec = C.CString(opts.FetchSpec)
copts.flags = C.uint(opts.Flags)
return copts
}
func freeRemoteCreateOptions(ptr *C.git_remote_create_options) {
if ptr == nil {
return
}
C.free(unsafe.Pointer(ptr.name))
C.free(unsafe.Pointer(ptr.fetchspec))
}
// createNewEmptyRemote used to get a new empty object of *Remote
func createNewEmptyRemote() *Remote {
return &Remote{
callbacks: RemoteCallbacks{},
repo: nil,
weak: false,
}
}

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"errors"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -38,13 +37,13 @@ func TestListRemotes(t *testing.T) {
compareStringList(t, expected, actual) compareStringList(t, expected, actual)
} }
func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T) error { func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T) ErrorCode {
if hostname != "github.com" { if hostname != "github.com" {
t.Fatal("hostname does not match") t.Fatal("Hostname does not match")
return errors.New("hostname does not match") return ErrorCodeUser
} }
return nil return ErrorCodeOK
} }
func TestCertificateCheck(t *testing.T) { func TestCertificateCheck(t *testing.T) {
@ -58,7 +57,7 @@ func TestCertificateCheck(t *testing.T) {
options := FetchOptions{ options := FetchOptions{
RemoteCallbacks: RemoteCallbacks{ RemoteCallbacks: RemoteCallbacks{
CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) error { CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) ErrorCode {
return assertHostname(cert, valid, hostname, t) return assertHostname(cert, valid, hostname, t)
}, },
}, },
@ -81,29 +80,6 @@ func TestRemoteConnect(t *testing.T) {
checkFatal(t, err) checkFatal(t, err)
} }
func TestRemoteConnectOption(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
config, err := repo.Config()
checkFatal(t, err)
err = config.SetString("url.git@github.com:.insteadof", "https://github.com/")
checkFatal(t, err)
option, err := DefaultRemoteCreateOptions()
checkFatal(t, err)
option.Name = "origin"
option.Flags = RemoteCreateSkipInsteadof
remote, err := repo.Remotes.CreateWithOptions("https://github.com/libgit2/TestGitRepository", option)
checkFatal(t, err)
defer remote.Free()
err = remote.ConnectFetch(nil, nil, nil)
checkFatal(t, err)
}
func TestRemoteLs(t *testing.T) { func TestRemoteLs(t *testing.T) {
t.Parallel() t.Parallel()
repo := createTestRepo(t) repo := createTestRepo(t)
@ -233,31 +209,6 @@ func TestRemotePrune(t *testing.T) {
} }
} }
func TestRemoteCredentialsCalled(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
remote, err := repo.Remotes.CreateAnonymous("https://github.com/libgit2/non-existent")
checkFatal(t, err)
defer remote.Free()
errNonExistent := errors.New("non-existent repository")
fetchOpts := FetchOptions{
RemoteCallbacks: RemoteCallbacks{
CredentialsCallback: func(url, username string, allowedTypes CredentialType) (*Credential, error) {
return nil, errNonExistent
},
},
}
err = remote.Fetch(nil, &fetchOpts, "fetch")
if err != errNonExistent {
t.Fatalf("remote.Fetch() = %v, want %v", err, errNonExistent)
}
}
func newChannelPipe(t *testing.T, w io.Writer, wg *sync.WaitGroup) (*os.File, error) { func newChannelPipe(t *testing.T, w io.Writer, wg *sync.WaitGroup) (*os.File, error) {
pr, pw, err := os.Pipe() pr, pw, err := os.Pipe()
if err != nil { if err != nil {
@ -479,13 +430,14 @@ func TestRemoteSSH(t *testing.T) {
certificateCheckCallbackCalled := false certificateCheckCallbackCalled := false
fetchOpts := FetchOptions{ fetchOpts := FetchOptions{
RemoteCallbacks: RemoteCallbacks{ RemoteCallbacks: RemoteCallbacks{
CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) error { CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) ErrorCode {
hostkeyFingerprint := fmt.Sprintf("%x", cert.Hostkey.HashMD5[:]) hostkeyFingerprint := fmt.Sprintf("%x", cert.Hostkey.HashMD5[:])
if hostkeyFingerprint != publicKeyFingerprint { if hostkeyFingerprint != publicKeyFingerprint {
return fmt.Errorf("server hostkey %q, want %q", hostkeyFingerprint, publicKeyFingerprint) t.Logf("server hostkey %q, want %q", hostkeyFingerprint, publicKeyFingerprint)
return ErrorCodeAuth
} }
certificateCheckCallbackCalled = true certificateCheckCallbackCalled = true
return nil return ErrorCodeOK
}, },
CredentialsCallback: func(url, username string, allowedTypes CredentialType) (*Credential, error) { CredentialsCallback: func(url, username string, allowedTypes CredentialType) (*Credential, error) {
if allowedTypes&(CredentialTypeSSHKey|CredentialTypeSSHCustom|CredentialTypeSSHMemory) != 0 { if allowedTypes&(CredentialTypeSSHKey|CredentialTypeSSHCustom|CredentialTypeSSHMemory) != 0 {

View File

@ -14,7 +14,6 @@ import (
// Repository // Repository
type Repository struct { type Repository struct {
doNotCompare
ptr *C.git_repository ptr *C.git_repository
// Remotes represents the collection of remotes and can be // Remotes represents the collection of remotes and can be
// used to add, remove and configure remotes for this // used to add, remove and configure remotes for this
@ -46,7 +45,6 @@ func newRepositoryFromC(ptr *C.git_repository) *Repository {
repo := &Repository{ptr: ptr} repo := &Repository{ptr: ptr}
repo.Remotes.repo = repo repo.Remotes.repo = repo
repo.Remotes.remotes = make(map[*C.git_remote]*Remote)
repo.Submodules.repo = repo repo.Submodules.repo = repo
repo.References.repo = repo repo.References.repo = repo
repo.Notes.repo = repo repo.Notes.repo = repo
@ -145,7 +143,6 @@ func (v *Repository) Free() {
ptr := v.ptr ptr := v.ptr
v.ptr = nil v.ptr = nil
runtime.SetFinalizer(v, nil) runtime.SetFinalizer(v, nil)
v.Remotes.Free()
if v.weak { if v.weak {
return return
} }
@ -168,23 +165,6 @@ func (v *Repository) Config() (*Config, error) {
return config, nil return config, nil
} }
// SetConfig sets the configuration file for this repository.
//
// This configuration file will be used for all configuration queries involving
// this repository.
func (v *Repository) SetConfig(c *Config) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_set_config(v.ptr, c.ptr)
runtime.KeepAlive(v)
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (v *Repository) Index() (*Index, error) { func (v *Repository) Index() (*Index, error) {
var ptr *C.git_index var ptr *C.git_index
@ -488,102 +468,6 @@ func (v *Repository) CreateCommit(
return oid, nil return oid, nil
} }
// CreateCommitWithSignature creates a commit object from the given contents and
// signature.
func (v *Repository) CreateCommitWithSignature(
commitContent, signature, signatureField string,
) (*Oid, error) {
cCommitContent := C.CString(commitContent)
defer C.free(unsafe.Pointer(cCommitContent))
var cSignature *C.char
if signature != "" {
cSignature = C.CString(string(signature))
defer C.free(unsafe.Pointer(cSignature))
}
var cSignatureField *C.char
if signatureField != "" {
cSignatureField = C.CString(string(signatureField))
defer C.free(unsafe.Pointer(cSignatureField))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
oid := new(Oid)
ret := C.git_commit_create_with_signature(oid.toC(), v.ptr, cCommitContent, cSignature, cSignatureField)
runtime.KeepAlive(v)
runtime.KeepAlive(oid)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}
// CreateCommitBuffer creates a commit and write it into a buffer.
func (v *Repository) CreateCommitBuffer(
author, committer *Signature,
messageEncoding MessageEncoding,
message string,
tree *Tree,
parents ...*Commit,
) ([]byte, error) {
cmsg := C.CString(message)
defer C.free(unsafe.Pointer(cmsg))
var cencoding *C.char
// Since the UTF-8 encoding is the default, pass in nil whenever UTF-8 is
// provided. That will cause the commit to not have an explicit header for
// it.
if messageEncoding != MessageEncodingUTF8 && messageEncoding != MessageEncoding("") {
cencoding = C.CString(string(messageEncoding))
defer C.free(unsafe.Pointer(cencoding))
}
var cparents []*C.git_commit = nil
var parentsarg **C.git_commit = nil
nparents := len(parents)
if nparents > 0 {
cparents = make([]*C.git_commit, nparents)
for i, v := range parents {
cparents[i] = v.cast_ptr
}
parentsarg = &cparents[0]
}
authorSig, err := author.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(committerSig)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var buf C.git_buf
defer C.git_buf_dispose(&buf)
ret := C.git_commit_create_buffer(
&buf, v.ptr,
authorSig, committerSig,
cencoding, cmsg, tree.cast_ptr, C.size_t(nparents), parentsarg)
runtime.KeepAlive(v)
runtime.KeepAlive(buf)
runtime.KeepAlive(parents)
if ret < 0 {
return nil, MakeGitError(ret)
}
return C.GoBytes(unsafe.Pointer(buf.ptr), C.int(buf.size)), nil
}
func (v *Repository) CreateCommitFromIds( func (v *Repository) CreateCommitFromIds(
refname string, author, committer *Signature, refname string, author, committer *Signature,
message string, tree *Oid, parents ...*Oid) (*Oid, error) { message string, tree *Oid, parents ...*Oid) (*Oid, error) {
@ -804,73 +688,3 @@ func (r *Repository) ClearGitIgnoreRules() error {
} }
return nil return nil
} }
// Message retrieves git's prepared message.
// Operations such as git revert/cherry-pick/merge with the -n option stop just
// short of creating a commit with the changes and save their prepared message
// in .git/MERGE_MSG so the next git-commit execution can present it to the
// user for them to amend if they wish.
//
// Use this function to get the contents of this file. Don't forget to remove
// the file after you create the commit.
func (r *Repository) Message() (string, error) {
buf := C.git_buf{}
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cErr := C.git_repository_message(&buf, r.ptr)
runtime.KeepAlive(r)
if cErr < 0 {
return "", MakeGitError(cErr)
}
return C.GoString(buf.ptr), nil
}
// RemoveMessage removes git's prepared message.
func (r *Repository) RemoveMessage() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cErr := C.git_repository_message_remove(r.ptr)
runtime.KeepAlive(r)
if cErr < 0 {
return MakeGitError(cErr)
}
return nil
}
type RepositoryItem int
const (
RepositoryItemGitDir RepositoryItem = C.GIT_REPOSITORY_ITEM_GITDIR
RepositoryItemWorkDir RepositoryItem = C.GIT_REPOSITORY_ITEM_WORKDIR
RepositoryItemCommonDir RepositoryItem = C.GIT_REPOSITORY_ITEM_COMMONDIR
RepositoryItemIndex RepositoryItem = C.GIT_REPOSITORY_ITEM_INDEX
RepositoryItemObjects RepositoryItem = C.GIT_REPOSITORY_ITEM_OBJECTS
RepositoryItemRefs RepositoryItem = C.GIT_REPOSITORY_ITEM_REFS
RepositoryItemPackedRefs RepositoryItem = C.GIT_REPOSITORY_ITEM_PACKED_REFS
RepositoryItemRemotes RepositoryItem = C.GIT_REPOSITORY_ITEM_REMOTES
RepositoryItemConfig RepositoryItem = C.GIT_REPOSITORY_ITEM_CONFIG
RepositoryItemInfo RepositoryItem = C.GIT_REPOSITORY_ITEM_INFO
RepositoryItemHooks RepositoryItem = C.GIT_REPOSITORY_ITEM_HOOKS
RepositoryItemLogs RepositoryItem = C.GIT_REPOSITORY_ITEM_LOGS
RepositoryItemModules RepositoryItem = C.GIT_REPOSITORY_ITEM_MODULES
RepositoryItemWorkTrees RepositoryItem = C.GIT_REPOSITORY_ITEM_WORKTREES
)
func (r *Repository) ItemPath(item RepositoryItem) (string, error) {
var c_buf C.git_buf
defer C.git_buf_dispose(&c_buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_item_path(&c_buf, r.ptr, C.git_repository_item_t(item))
runtime.KeepAlive(r)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(c_buf.ptr), nil
}

View File

@ -5,60 +5,6 @@ import (
"time" "time"
) )
func TestCreateCommitBuffer(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
loc, err := time.LoadLocation("Europe/Berlin")
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc),
}
idx, err := repo.Index()
checkFatal(t, err)
err = idx.AddByPath("README")
checkFatal(t, err)
err = idx.Write()
checkFatal(t, err)
treeId, err := idx.WriteTree()
checkFatal(t, err)
message := "This is a commit\n"
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
for encoding, expected := range map[MessageEncoding]string{
MessageEncodingUTF8: `tree b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e
author Rand Om Hacker <random@hacker.com> 1362576600 +0100
committer Rand Om Hacker <random@hacker.com> 1362576600 +0100
This is a commit
`,
MessageEncoding("ASCII"): `tree b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e
author Rand Om Hacker <random@hacker.com> 1362576600 +0100
committer Rand Om Hacker <random@hacker.com> 1362576600 +0100
encoding ASCII
This is a commit
`,
} {
encoding := encoding
expected := expected
t.Run(string(encoding), func(t *testing.T) {
buf, err := repo.CreateCommitBuffer(sig, sig, encoding, message, tree)
checkFatal(t, err)
if expected != string(buf) {
t.Errorf("mismatched commit buffer, expected %v, got %v", expected, string(buf))
}
})
}
}
func TestCreateCommitFromIds(t *testing.T) { func TestCreateCommitFromIds(t *testing.T) {
t.Parallel() t.Parallel()
repo := createTestRepo(t) repo := createTestRepo(t)
@ -94,66 +40,3 @@ func TestCreateCommitFromIds(t *testing.T) {
t.Errorf("mismatched commit ids, expected %v, got %v", expectedCommitId.String(), commitId.String()) t.Errorf("mismatched commit ids, expected %v, got %v", expectedCommitId.String(), commitId.String())
} }
} }
func TestRepositorySetConfig(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
loc, err := time.LoadLocation("Europe/Berlin")
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc),
}
idx, err := repo.Index()
checkFatal(t, err)
err = idx.AddByPath("README")
treeId, err := idx.WriteTree()
checkFatal(t, err)
message := "This is a commit\n"
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
_, err = repo.CreateCommit("HEAD", sig, sig, message, tree)
checkFatal(t, err)
repoConfig, err := repo.Config()
checkFatal(t, err)
temp := Config{}
localConfig, err := temp.OpenLevel(repoConfig, ConfigLevelLocal)
checkFatal(t, err)
repoConfig = nil
err = repo.SetConfig(localConfig)
checkFatal(t, err)
configFieldName := "core.filemode"
err = localConfig.SetBool(configFieldName, true)
checkFatal(t, err)
localConfig = nil
repoConfig, err = repo.Config()
checkFatal(t, err)
result, err := repoConfig.LookupBool(configFieldName)
checkFatal(t, err)
if result != true {
t.Fatal("result must be true")
}
}
func TestRepositoryItemPath(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
gitDir, err := repo.ItemPath(RepositoryItemGitDir)
checkFatal(t, err)
if gitDir == "" {
t.Error("expected not empty gitDir")
}
}

View File

@ -11,8 +11,8 @@ import (
// RevertOptions contains options for performing a revert // RevertOptions contains options for performing a revert
type RevertOptions struct { type RevertOptions struct {
Mainline uint Mainline uint
MergeOptions MergeOptions MergeOpts MergeOptions
CheckoutOptions CheckoutOptions CheckoutOpts CheckoutOptions
} }
func populateRevertOptions(copts *C.git_revert_options, opts *RevertOptions, errorTarget *error) *C.git_revert_options { func populateRevertOptions(copts *C.git_revert_options, opts *RevertOptions, errorTarget *error) *C.git_revert_options {
@ -21,16 +21,16 @@ func populateRevertOptions(copts *C.git_revert_options, opts *RevertOptions, err
return nil return nil
} }
copts.mainline = C.uint(opts.Mainline) copts.mainline = C.uint(opts.Mainline)
populateMergeOptions(&copts.merge_opts, &opts.MergeOptions) populateMergeOptions(&copts.merge_opts, &opts.MergeOpts)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget) populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOpts, errorTarget)
return copts return copts
} }
func revertOptionsFromC(copts *C.git_revert_options) RevertOptions { func revertOptionsFromC(copts *C.git_revert_options) RevertOptions {
return RevertOptions{ return RevertOptions{
Mainline: uint(copts.mainline), Mainline: uint(copts.mainline),
MergeOptions: mergeOptionsFromC(&copts.merge_opts), MergeOpts: mergeOptionsFromC(&copts.merge_opts),
CheckoutOptions: checkoutOptionsFromC(&copts.checkout_opts), CheckoutOpts: checkoutOptionsFromC(&copts.checkout_opts),
} }
} }

View File

@ -60,11 +60,11 @@ func TestRevertCommit(t *testing.T) {
revertOptions, err := DefaultRevertOptions() revertOptions, err := DefaultRevertOptions()
checkFatal(t, err) checkFatal(t, err)
index, err := repo.RevertCommit(commit, commit, 0, &revertOptions.MergeOptions) index, err := repo.RevertCommit(commit, commit, 0, &revertOptions.MergeOpts)
checkFatal(t, err) checkFatal(t, err)
defer index.Free() defer index.Free()
err = repo.CheckoutIndex(index, &revertOptions.CheckoutOptions) err = repo.CheckoutIndex(index, &revertOptions.CheckoutOpts)
checkFatal(t, err) checkFatal(t, err)
actualReadmeContents := readReadme(t, repo) actualReadmeContents := readReadme(t, repo)

View File

@ -20,7 +20,6 @@ const (
) )
type Revspec struct { type Revspec struct {
doNotCompare
to *Object to *Object
from *Object from *Object
flags RevparseFlag flags RevparseFlag

View File

@ -51,35 +51,24 @@ if [ -n "${BUILD_LIBGIT_REF}" ]; then
trap "git submodule update --init" EXIT trap "git submodule update --init" EXIT
fi fi
BUILD_DEPRECATED_HARD="ON"
if [ "${BUILD_SYSTEM}" = "ON" ]; then if [ "${BUILD_SYSTEM}" = "ON" ]; then
BUILD_INSTALL_PREFIX=${SYSTEM_INSTALL_PREFIX-"/usr"} BUILD_INSTALL_PREFIX=${SYSTEM_INSTALL_PREFIX-"/usr"}
# Most system-wide installations won't intentionally omit deprecated symbols.
BUILD_DEPRECATED_HARD="OFF"
else else
BUILD_INSTALL_PREFIX="${BUILD_PATH}/install" BUILD_INSTALL_PREFIX="${BUILD_PATH}/install"
mkdir -p "${BUILD_PATH}/install/lib" mkdir -p "${BUILD_PATH}/install/lib"
fi fi
USE_BUNDLED_ZLIB="ON"
if [ "${USE_CHROMIUM_ZLIB}" = "ON" ]; then
USE_BUNDLED_ZLIB="Chromium"
fi
mkdir -p "${BUILD_PATH}/build" && mkdir -p "${BUILD_PATH}/build" &&
cd "${BUILD_PATH}/build" && cd "${BUILD_PATH}/build" &&
cmake -DTHREADSAFE=ON \ cmake -DTHREADSAFE=ON \
-DBUILD_CLAR=OFF \ -DBUILD_CLAR=OFF \
-DBUILD_SHARED_LIBS"=${BUILD_SHARED_LIBS}" \ -DBUILD_SHARED_LIBS"=${BUILD_SHARED_LIBS}" \
-DREGEX_BACKEND=builtin \ -DREGEX_BACKEND=builtin \
-DUSE_BUNDLED_ZLIB="${USE_BUNDLED_ZLIB}" \
-DUSE_HTTPS=OFF \
-DUSE_SSH=OFF \
-DCMAKE_C_FLAGS=-fPIC \ -DCMAKE_C_FLAGS=-fPIC \
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \ -DCMAKE_BUILD_TYPE="RelWithDebInfo" \
-DCMAKE_INSTALL_PREFIX="${BUILD_INSTALL_PREFIX}" \ -DCMAKE_INSTALL_PREFIX="${BUILD_INSTALL_PREFIX}" \
-DCMAKE_INSTALL_LIBDIR="lib" \ -DCMAKE_INSTALL_LIBDIR="lib" \
-DDEPRECATE_HARD="${BUILD_DEPRECATE_HARD}" \ -DDEPRECATE_HARD=ON \
"${VENDORED_PATH}" "${VENDORED_PATH}"
if which make nproc >/dev/null && [ -f Makefile ]; then if which make nproc >/dev/null && [ -f Makefile ]; then

View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# #
# Install libgit2 to go libgit2 in dynamic mode on Travis # Install libgit2 to git2go in dynamic mode on Travis
# #
set -ex set -ex

View File

@ -101,14 +101,6 @@ func EnableStrictHashVerification(enabled bool) error {
} }
} }
func EnableFsyncGitDir(enabled bool) error {
if enabled {
return setSizet(C.GIT_OPT_ENABLE_FSYNC_GITDIR, 1)
} else {
return setSizet(C.GIT_OPT_ENABLE_FSYNC_GITDIR, 0)
}
}
func CachedMemory() (current int, allowed int, err error) { func CachedMemory() (current int, allowed int, err error) {
return getSizetSizet(C.GIT_OPT_GET_CACHED_MEMORY) return getSizetSizet(C.GIT_OPT_GET_CACHED_MEMORY)
} }

View File

@ -65,14 +65,6 @@ func TestEnableStrictHashVerification(t *testing.T) {
checkFatal(t, err) checkFatal(t, err)
} }
func TestEnableFsyncGitDir(t *testing.T) {
err := EnableFsyncGitDir(false)
checkFatal(t, err)
err = EnableFsyncGitDir(true)
checkFatal(t, err)
}
func TestCachedMemory(t *testing.T) { func TestCachedMemory(t *testing.T) {
current, allowed, err := CachedMemory() current, allowed, err := CachedMemory()
checkFatal(t, err) checkFatal(t, err)

250
ssh.go
View File

@ -1,250 +0,0 @@
package git
/*
#include <git2.h>
#include <git2/sys/credential.h>
*/
import "C"
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/url"
"runtime"
"strings"
"unsafe"
"golang.org/x/crypto/ssh"
)
// RegisterManagedSSHTransport registers a Go-native implementation of an SSH
// transport that doesn't rely on any system libraries (e.g. libssh2).
//
// If Shutdown or ReInit are called, make sure that the smart transports are
// freed before it.
func RegisterManagedSSHTransport(protocol string) (*RegisteredSmartTransport, error) {
return NewRegisteredSmartTransport(protocol, false, sshSmartSubtransportFactory)
}
func registerManagedSSH() error {
globalRegisteredSmartTransports.Lock()
defer globalRegisteredSmartTransports.Unlock()
for _, protocol := range []string{"ssh", "ssh+git", "git+ssh"} {
if _, ok := globalRegisteredSmartTransports.transports[protocol]; ok {
continue
}
managed, err := newRegisteredSmartTransport(protocol, false, sshSmartSubtransportFactory, true)
if err != nil {
return fmt.Errorf("failed to register transport for %q: %v", protocol, err)
}
globalRegisteredSmartTransports.transports[protocol] = managed
}
return nil
}
func sshSmartSubtransportFactory(remote *Remote, transport *Transport) (SmartSubtransport, error) {
return &sshSmartSubtransport{
transport: transport,
}, nil
}
type sshSmartSubtransport struct {
transport *Transport
lastAction SmartServiceAction
client *ssh.Client
session *ssh.Session
stdin io.WriteCloser
stdout io.Reader
currentStream *sshSmartSubtransportStream
}
func (t *sshSmartSubtransport) Action(urlString string, action SmartServiceAction) (SmartSubtransportStream, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
u, err := url.Parse(urlString)
if err != nil {
return nil, err
}
// Escape \ and '.
uPath := strings.Replace(u.Path, `\`, `\\`, -1)
uPath = strings.Replace(uPath, `'`, `\'`, -1)
// TODO: Add percentage decode similar to libgit2.
// Refer: https://github.com/libgit2/libgit2/blob/358a60e1b46000ea99ef10b4dd709e92f75ff74b/src/str.c#L455-L481
var cmd string
switch action {
case SmartServiceActionUploadpackLs, SmartServiceActionUploadpack:
if t.currentStream != nil {
if t.lastAction == SmartServiceActionUploadpackLs {
return t.currentStream, nil
}
t.Close()
}
cmd = fmt.Sprintf("git-upload-pack '%s'", uPath)
case SmartServiceActionReceivepackLs, SmartServiceActionReceivepack:
if t.currentStream != nil {
if t.lastAction == SmartServiceActionReceivepackLs {
return t.currentStream, nil
}
t.Close()
}
cmd = fmt.Sprintf("git-receive-pack '%s'", uPath)
default:
return nil, fmt.Errorf("unexpected action: %v", action)
}
cred, err := t.transport.SmartCredentials("", CredentialTypeSSHKey|CredentialTypeSSHMemory)
if err != nil {
return nil, err
}
defer cred.Free()
sshConfig, err := getSSHConfigFromCredential(cred)
if err != nil {
return nil, err
}
sshConfig.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
marshaledKey := key.Marshal()
cert := &Certificate{
Kind: CertificateHostkey,
Hostkey: HostkeyCertificate{
Kind: HostkeySHA1 | HostkeyMD5 | HostkeySHA256 | HostkeyRaw,
HashMD5: md5.Sum(marshaledKey),
HashSHA1: sha1.Sum(marshaledKey),
HashSHA256: sha256.Sum256(marshaledKey),
Hostkey: marshaledKey,
SSHPublicKey: key,
},
}
return t.transport.SmartCertificateCheck(cert, true, hostname)
}
var addr string
if u.Port() != "" {
addr = fmt.Sprintf("%s:%s", u.Hostname(), u.Port())
} else {
addr = fmt.Sprintf("%s:22", u.Hostname())
}
t.client, err = ssh.Dial("tcp", addr, sshConfig)
if err != nil {
return nil, err
}
t.session, err = t.client.NewSession()
if err != nil {
return nil, err
}
t.stdin, err = t.session.StdinPipe()
if err != nil {
return nil, err
}
t.stdout, err = t.session.StdoutPipe()
if err != nil {
return nil, err
}
if err := t.session.Start(cmd); err != nil {
return nil, err
}
t.lastAction = action
t.currentStream = &sshSmartSubtransportStream{
owner: t,
}
return t.currentStream, nil
}
func (t *sshSmartSubtransport) Close() error {
t.currentStream = nil
if t.client != nil {
t.stdin.Close()
t.session.Wait()
t.session.Close()
t.client = nil
}
return nil
}
func (t *sshSmartSubtransport) Free() {
}
type sshSmartSubtransportStream struct {
owner *sshSmartSubtransport
}
func (stream *sshSmartSubtransportStream) Read(buf []byte) (int, error) {
return stream.owner.stdout.Read(buf)
}
func (stream *sshSmartSubtransportStream) Write(buf []byte) (int, error) {
return stream.owner.stdin.Write(buf)
}
func (stream *sshSmartSubtransportStream) Free() {
}
func getSSHConfigFromCredential(cred *Credential) (*ssh.ClientConfig, error) {
switch cred.Type() {
case CredentialTypeSSHCustom:
credSSHCustom := (*C.git_credential_ssh_custom)(unsafe.Pointer(cred.ptr))
data, ok := pointerHandles.Get(credSSHCustom.payload).(*credentialSSHCustomData)
if !ok {
return nil, errors.New("unsupported custom SSH credentials")
}
return &ssh.ClientConfig{
User: C.GoString(credSSHCustom.username),
Auth: []ssh.AuthMethod{ssh.PublicKeys(data.signer)},
}, nil
}
username, _, privatekey, passphrase, err := cred.GetSSHKey()
if err != nil {
return nil, err
}
var pemBytes []byte
if cred.Type() == CredentialTypeSSHMemory {
pemBytes = []byte(privatekey)
} else {
pemBytes, err = ioutil.ReadFile(privatekey)
if err != nil {
return nil, err
}
}
var key ssh.Signer
if passphrase != "" {
key, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(passphrase))
if err != nil {
return nil, err
}
} else {
key, err = ssh.ParsePrivateKey(pemBytes)
if err != nil {
return nil, err
}
}
return &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
}, nil
}

View File

@ -35,7 +35,6 @@ const (
// StashCollection represents the possible operations that can be // StashCollection represents the possible operations that can be
// performed on the collection of stashes for a repository. // performed on the collection of stashes for a repository.
type StashCollection struct { type StashCollection struct {
doNotCompare
repo *Repository repo *Repository
} }

View File

@ -55,7 +55,6 @@ func statusEntryFromC(statusEntry *C.git_status_entry) StatusEntry {
} }
type StatusList struct { type StatusList struct {
doNotCompare
ptr *C.git_status_list ptr *C.git_status_list
r *Repository r *Repository
} }

View File

@ -8,19 +8,19 @@ extern int _go_git_visit_submodule(git_repository *repo, void *fct);
import "C" import "C"
import ( import (
"errors"
"runtime" "runtime"
"unsafe" "unsafe"
) )
// SubmoduleUpdateOptions // SubmoduleUpdateOptions
type SubmoduleUpdateOptions struct { type SubmoduleUpdateOptions struct {
CheckoutOptions CheckoutOptions *CheckoutOpts
FetchOptions FetchOptions *FetchOptions
} }
// Submodule // Submodule
type Submodule struct { type Submodule struct {
doNotCompare
ptr *C.git_submodule ptr *C.git_submodule
r *Repository r *Repository
} }
@ -82,7 +82,6 @@ const (
) )
type SubmoduleCollection struct { type SubmoduleCollection struct {
doNotCompare
repo *Repository repo *Repository
} }
@ -110,7 +109,7 @@ func (c *SubmoduleCollection) Lookup(name string) (*Submodule, error) {
} }
// SubmoduleCallback is a function that is called for every submodule found in SubmoduleCollection.Foreach. // SubmoduleCallback is a function that is called for every submodule found in SubmoduleCollection.Foreach.
type SubmoduleCallback func(sub *Submodule, name string) error type SubmoduleCallback func(sub *Submodule, name string) int
type submoduleCallbackData struct { type submoduleCallbackData struct {
callback SubmoduleCallback callback SubmoduleCallback
errorTarget *error errorTarget *error
@ -118,16 +117,16 @@ type submoduleCallbackData struct {
//export submoduleCallback //export submoduleCallback
func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int {
sub := &Submodule{ptr: (*C.git_submodule)(csub)} sub := &Submodule{(*C.git_submodule)(csub), nil}
data, ok := pointerHandles.Get(handle).(submoduleCallbackData) data, ok := pointerHandles.Get(handle).(submoduleCallbackData)
if !ok { if !ok {
panic("invalid submodule visitor callback") panic("invalid submodule visitor callback")
} }
err := data.callback(sub, C.GoString(name)) ret := data.callback(sub, C.GoString(name))
if err != nil { if ret < 0 {
*data.errorTarget = err *data.errorTarget = errors.New(ErrorCode(ret).String())
return C.int(ErrorCodeUser) return C.int(ErrorCodeUser)
} }
@ -390,8 +389,8 @@ func populateSubmoduleUpdateOptions(copts *C.git_submodule_update_options, opts
return nil return nil
} }
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget) populateCheckoutOptions(&copts.checkout_opts, opts.CheckoutOpts, errorTarget)
populateFetchOptions(&copts.fetch_opts, &opts.FetchOptions, errorTarget) populateFetchOptions(&copts.fetch_opts, opts.FetchOptions, errorTarget)
return copts return copts
} }

View File

@ -15,9 +15,9 @@ func TestSubmoduleForeach(t *testing.T) {
checkFatal(t, err) checkFatal(t, err)
i := 0 i := 0
err = repo.Submodules.Foreach(func(sub *Submodule, name string) error { err = repo.Submodules.Foreach(func(sub *Submodule, name string) int {
i++ i++
return nil return 0
}) })
checkFatal(t, err) checkFatal(t, err)

2
tag.go
View File

@ -13,7 +13,6 @@ import (
// Tag // Tag
type Tag struct { type Tag struct {
doNotCompare
Object Object
cast_ptr *C.git_tag cast_ptr *C.git_tag
} }
@ -65,7 +64,6 @@ func (t *Tag) TargetType() ObjectType {
} }
type TagsCollection struct { type TagsCollection struct {
doNotCompare
repo *Repository repo *Repository
} }

View File

@ -1,504 +0,0 @@
package git
/*
#include <string.h>
#include <git2.h>
#include <git2/sys/transport.h>
typedef struct {
git_smart_subtransport parent;
void *handle;
} _go_managed_smart_subtransport;
typedef struct {
git_smart_subtransport_stream parent;
void *handle;
} _go_managed_smart_subtransport_stream;
int _go_git_transport_register(const char *prefix, void *handle);
int _go_git_transport_smart(git_transport **out, git_remote *owner, int stateless, _go_managed_smart_subtransport *subtransport_payload);
void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stream *stream);
*/
import "C"
import (
"fmt"
"io"
"reflect"
"runtime"
"sync"
"unsafe"
)
var (
// globalRegisteredSmartTransports is a mapping of global, libgit2 go-managed
// transports.
globalRegisteredSmartTransports = struct {
sync.Mutex
transports map[string]*RegisteredSmartTransport
}{
transports: make(map[string]*RegisteredSmartTransport),
}
)
// unregisterManagedTransports unregisters all libgit2 go-managed transports.
func unregisterManagedTransports() error {
globalRegisteredSmartTransports.Lock()
originalTransports := globalRegisteredSmartTransports.transports
globalRegisteredSmartTransports.transports = make(map[string]*RegisteredSmartTransport)
globalRegisteredSmartTransports.Unlock()
var err error
for protocol, managed := range originalTransports {
unregisterErr := managed.Free()
if err == nil && unregisterErr != nil {
err = fmt.Errorf("failed to unregister transport for %q: %v", protocol, unregisterErr)
}
}
return err
}
// SmartServiceAction is an action that the smart transport can ask a
// subtransport to perform.
type SmartServiceAction int
const (
// SmartServiceActionUploadpackLs is used upon connecting to a remote, and is
// used to perform reference discovery prior to performing a pull operation.
SmartServiceActionUploadpackLs SmartServiceAction = C.GIT_SERVICE_UPLOADPACK_LS
// SmartServiceActionUploadpack is used when performing a pull operation.
SmartServiceActionUploadpack SmartServiceAction = C.GIT_SERVICE_UPLOADPACK
// SmartServiceActionReceivepackLs is used upon connecting to a remote, and is
// used to perform reference discovery prior to performing a push operation.
SmartServiceActionReceivepackLs SmartServiceAction = C.GIT_SERVICE_RECEIVEPACK_LS
// SmartServiceActionReceivepack is used when performing a push operation.
SmartServiceActionReceivepack SmartServiceAction = C.GIT_SERVICE_RECEIVEPACK
)
// Transport encapsulates a way to communicate with a Remote.
type Transport struct {
doNotCompare
ptr *C.git_transport
}
// SmartRemoteConnectOptions gets a copy of the proxy options for this transport.
func (t *Transport) SmartRemoteConnectOptions() (*RemoteConnectOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var copts C.git_remote_connect_options
if ret := C.git_transport_remote_connect_options(&copts, t.ptr); ret < 0 {
return nil, MakeGitError(ret)
}
return remoteConnectOptionsFromC(&copts), nil
}
// SmartCredentials calls the credentials callback for this transport.
func (t *Transport) SmartCredentials(user string, methods CredentialType) (*Credential, error) {
cred := newCredential()
var cstr *C.char
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if user != "" {
cstr = C.CString(user)
defer C.free(unsafe.Pointer(cstr))
}
ret := C.git_transport_smart_credentials(&cred.ptr, t.ptr, cstr, C.int(methods))
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
// SmartCertificateCheck calls the certificate check for this transport.
func (t *Transport) SmartCertificateCheck(cert *Certificate, valid bool, hostname string) error {
var ccert *C.git_cert
switch cert.Kind {
case CertificateHostkey:
chostkeyCert := C.git_cert_hostkey{
parent: C.git_cert{
cert_type: C.GIT_CERT_HOSTKEY_LIBSSH2,
},
_type: C.git_cert_ssh_t(cert.Kind),
hostkey: (*C.char)(C.CBytes(cert.Hostkey.Hostkey)),
hostkey_len: C.size_t(len(cert.Hostkey.Hostkey)),
}
defer C.free(unsafe.Pointer(chostkeyCert.hostkey))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_md5[0]), unsafe.Pointer(&cert.Hostkey.HashMD5[0]), C.size_t(len(cert.Hostkey.HashMD5)))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_sha1[0]), unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), C.size_t(len(cert.Hostkey.HashSHA1)))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_sha256[0]), unsafe.Pointer(&cert.Hostkey.HashSHA256[0]), C.size_t(len(cert.Hostkey.HashSHA256)))
if cert.Hostkey.SSHPublicKey.Type() == "ssh-rsa" {
chostkeyCert.raw_type = C.GIT_CERT_SSH_RAW_TYPE_RSA
} else if cert.Hostkey.SSHPublicKey.Type() == "ssh-dss" {
chostkeyCert.raw_type = C.GIT_CERT_SSH_RAW_TYPE_DSS
} else {
chostkeyCert.raw_type = C.GIT_CERT_SSH_RAW_TYPE_UNKNOWN
}
ccert = (*C.git_cert)(unsafe.Pointer(&chostkeyCert))
case CertificateX509:
cx509Cert := C.git_cert_x509{
parent: C.git_cert{
cert_type: C.GIT_CERT_X509,
},
len: C.size_t(len(cert.X509.Raw)),
data: C.CBytes(cert.X509.Raw),
}
defer C.free(cx509Cert.data)
ccert = (*C.git_cert)(unsafe.Pointer(&cx509Cert))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
chostname := C.CString(hostname)
defer C.free(unsafe.Pointer(chostname))
cvalid := C.int(0)
if valid {
cvalid = C.int(1)
}
ret := C.git_transport_smart_certificate_check(t.ptr, ccert, cvalid, chostname)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
// SmartSubtransport is the interface for custom subtransports which carry data
// for the smart transport.
type SmartSubtransport interface {
// Action creates a SmartSubtransportStream for the provided url and
// requested action.
Action(url string, action SmartServiceAction) (SmartSubtransportStream, error)
// Close closes the SmartSubtransport.
//
// Subtransports are guaranteed a call to Close between
// calls to Action, except for the following two "natural" progressions
// of actions against a constant URL.
//
// 1. UPLOADPACK_LS -> UPLOADPACK
// 2. RECEIVEPACK_LS -> RECEIVEPACK
Close() error
// Free releases the resources of the SmartSubtransport.
Free()
}
// SmartSubtransportStream is the interface for streams used by the smart
// transport to read and write data from a subtransport.
type SmartSubtransportStream interface {
io.Reader
io.Writer
// Free releases the resources of the SmartSubtransportStream.
Free()
}
// SmartSubtransportCallback is a function which creates a new subtransport for
// the smart transport.
type SmartSubtransportCallback func(remote *Remote, transport *Transport) (SmartSubtransport, error)
// RegisteredSmartTransport represents a transport that has been registered.
type RegisteredSmartTransport struct {
doNotCompare
name string
stateless bool
callback SmartSubtransportCallback
handle unsafe.Pointer
}
// NewRegisteredSmartTransport adds a custom transport definition, to be used
// in addition to the built-in set of transports that come with libgit2.
func NewRegisteredSmartTransport(
name string,
stateless bool,
callback SmartSubtransportCallback,
) (*RegisteredSmartTransport, error) {
return newRegisteredSmartTransport(name, stateless, callback, false)
}
func newRegisteredSmartTransport(
name string,
stateless bool,
callback SmartSubtransportCallback,
global bool,
) (*RegisteredSmartTransport, error) {
if !global {
// Check if we had already registered a smart transport for this protocol. If
// we had, free it. The user is now responsible for this transport for the
// lifetime of the library.
globalRegisteredSmartTransports.Lock()
if managed, ok := globalRegisteredSmartTransports.transports[name]; ok {
delete(globalRegisteredSmartTransports.transports, name)
globalRegisteredSmartTransports.Unlock()
err := managed.Free()
if err != nil {
return nil, err
}
} else {
globalRegisteredSmartTransports.Unlock()
}
}
cstr := C.CString(name)
defer C.free(unsafe.Pointer(cstr))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
registeredSmartTransport := &RegisteredSmartTransport{
name: name,
stateless: stateless,
callback: callback,
}
registeredSmartTransport.handle = pointerHandles.Track(registeredSmartTransport)
ret := C._go_git_transport_register(cstr, registeredSmartTransport.handle)
if ret != 0 {
pointerHandles.Untrack(registeredSmartTransport.handle)
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(registeredSmartTransport, (*RegisteredSmartTransport).Free)
return registeredSmartTransport, nil
}
// Free releases all resources used by the RegisteredSmartTransport and
// unregisters the custom transport definition referenced by it.
func (t *RegisteredSmartTransport) Free() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(t.name)
defer C.free(unsafe.Pointer(cstr))
if ret := C.git_transport_unregister(cstr); ret < 0 {
return MakeGitError(ret)
}
pointerHandles.Untrack(t.handle)
runtime.SetFinalizer(t, nil)
t.handle = nil
return nil
}
//export smartTransportCallback
func smartTransportCallback(
errorMessage **C.char,
out **C.git_transport,
owner *C.git_remote,
handle unsafe.Pointer,
) C.int {
registeredSmartTransport := pointerHandles.Get(handle).(*RegisteredSmartTransport)
remote, ok := remotePointers.get(owner)
if !ok {
// create a new empty remote and set it
// as a weak pointer, so that control stays in golang
remote = createNewEmptyRemote()
remote.weak = true
}
managed := &managedSmartSubtransport{
remote: remote,
callback: registeredSmartTransport.callback,
subtransport: (*C._go_managed_smart_subtransport)(C.calloc(1, C.size_t(unsafe.Sizeof(C._go_managed_smart_subtransport{})))),
}
managedHandle := pointerHandles.Track(managed)
managed.handle = managedHandle
managed.subtransport.handle = managedHandle
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C._go_git_transport_smart(out, owner, cbool(registeredSmartTransport.stateless), managed.subtransport)
if ret != 0 {
pointerHandles.Untrack(managedHandle)
}
return ret
}
//export smartTransportSubtransportCallback
func smartTransportSubtransportCallback(
errorMessage **C.char,
wrapperPtr *C._go_managed_smart_subtransport,
owner *C.git_transport,
) C.int {
subtransport := pointerHandles.Get(wrapperPtr.handle).(*managedSmartSubtransport)
underlyingSmartSubtransport, err := subtransport.callback(subtransport.remote, &Transport{ptr: owner})
if err != nil {
return setCallbackError(errorMessage, err)
}
subtransport.underlying = underlyingSmartSubtransport
return C.int(ErrorCodeOK)
}
type managedSmartSubtransport struct {
owner *C.git_transport
callback SmartSubtransportCallback
remote *Remote
subtransport *C._go_managed_smart_subtransport
underlying SmartSubtransport
handle unsafe.Pointer
currentManagedStream *managedSmartSubtransportStream
}
func getSmartSubtransportInterface(subtransport *C.git_smart_subtransport) *managedSmartSubtransport {
wrapperPtr := (*C._go_managed_smart_subtransport)(unsafe.Pointer(subtransport))
return pointerHandles.Get(wrapperPtr.handle).(*managedSmartSubtransport)
}
//export smartSubtransportActionCallback
func smartSubtransportActionCallback(
errorMessage **C.char,
out **C.git_smart_subtransport_stream,
t *C.git_smart_subtransport,
url *C.char,
action C.git_smart_service_t,
) C.int {
subtransport := getSmartSubtransportInterface(t)
underlyingStream, err := subtransport.underlying.Action(C.GoString(url), SmartServiceAction(action))
if err != nil {
return setCallbackError(errorMessage, err)
}
// It's okay to do strict equality here: we expect both to be identical.
if subtransport.currentManagedStream == nil || subtransport.currentManagedStream.underlying != underlyingStream {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
stream := (*C._go_managed_smart_subtransport_stream)(C.calloc(1, C.size_t(unsafe.Sizeof(C._go_managed_smart_subtransport_stream{}))))
managed := &managedSmartSubtransportStream{
underlying: underlyingStream,
streamPtr: stream,
}
managedHandle := pointerHandles.Track(managed)
managed.handle = managedHandle
stream.handle = managedHandle
C._go_git_setup_smart_subtransport_stream(stream)
subtransport.currentManagedStream = managed
}
*out = &subtransport.currentManagedStream.streamPtr.parent
return C.int(ErrorCodeOK)
}
//export smartSubtransportCloseCallback
func smartSubtransportCloseCallback(errorMessage **C.char, t *C.git_smart_subtransport) C.int {
subtransport := getSmartSubtransportInterface(t)
subtransport.currentManagedStream = nil
if subtransport.underlying != nil {
err := subtransport.underlying.Close()
if err != nil {
return setCallbackError(errorMessage, err)
}
}
return C.int(ErrorCodeOK)
}
//export smartSubtransportFreeCallback
func smartSubtransportFreeCallback(t *C.git_smart_subtransport) {
subtransport := getSmartSubtransportInterface(t)
if subtransport.underlying != nil {
subtransport.underlying.Free()
subtransport.underlying = nil
}
pointerHandles.Untrack(subtransport.handle)
C.free(unsafe.Pointer(subtransport.subtransport))
subtransport.handle = nil
subtransport.subtransport = nil
}
type managedSmartSubtransportStream struct {
owner *C.git_smart_subtransport_stream
streamPtr *C._go_managed_smart_subtransport_stream
underlying SmartSubtransportStream
handle unsafe.Pointer
}
func getSmartSubtransportStreamInterface(subtransportStream *C.git_smart_subtransport_stream) *managedSmartSubtransportStream {
managedSubtransportStream := (*C._go_managed_smart_subtransport_stream)(unsafe.Pointer(subtransportStream))
return pointerHandles.Get(managedSubtransportStream.handle).(*managedSmartSubtransportStream)
}
//export smartSubtransportStreamReadCallback
func smartSubtransportStreamReadCallback(
errorMessage **C.char,
s *C.git_smart_subtransport_stream,
buffer *C.char,
bufSize C.size_t,
bytesRead *C.size_t,
) C.int {
stream := getSmartSubtransportStreamInterface(s)
var p []byte
header := (*reflect.SliceHeader)(unsafe.Pointer(&p))
header.Cap = int(bufSize)
header.Len = int(bufSize)
header.Data = uintptr(unsafe.Pointer(buffer))
n, err := stream.underlying.Read(p)
*bytesRead = C.size_t(n)
if n == 0 && err != nil {
if err == io.EOF {
return C.int(ErrorCodeOK)
}
return setCallbackError(errorMessage, err)
}
return C.int(ErrorCodeOK)
}
//export smartSubtransportStreamWriteCallback
func smartSubtransportStreamWriteCallback(
errorMessage **C.char,
s *C.git_smart_subtransport_stream,
buffer *C.char,
bufLen C.size_t,
) C.int {
stream := getSmartSubtransportStreamInterface(s)
var p []byte
header := (*reflect.SliceHeader)(unsafe.Pointer(&p))
header.Cap = int(bufLen)
header.Len = int(bufLen)
header.Data = uintptr(unsafe.Pointer(buffer))
if _, err := stream.underlying.Write(p); err != nil {
return setCallbackError(errorMessage, err)
}
return C.int(ErrorCodeOK)
}
//export smartSubtransportStreamFreeCallback
func smartSubtransportStreamFreeCallback(s *C.git_smart_subtransport_stream) {
stream := getSmartSubtransportStreamInterface(s)
stream.underlying.Free()
pointerHandles.Untrack(stream.handle)
C.free(unsafe.Pointer(stream.streamPtr))
stream.handle = nil
stream.streamPtr = nil
}

View File

@ -1,72 +0,0 @@
package git
import (
"io"
"reflect"
"testing"
)
type testSmartSubtransport struct {
}
func (t *testSmartSubtransport) Action(url string, action SmartServiceAction) (SmartSubtransportStream, error) {
return &testSmartSubtransportStream{}, nil
}
func (t *testSmartSubtransport) Close() error {
return nil
}
func (t *testSmartSubtransport) Free() {
}
type testSmartSubtransportStream struct {
}
func (s *testSmartSubtransportStream) Read(buf []byte) (int, error) {
payload := "" +
"001e# service=git-upload-pack\n" +
"0000005d0000000000000000000000000000000000000000 HEAD\x00symref=HEAD:refs/heads/master agent=libgit\n" +
"003f0000000000000000000000000000000000000000 refs/heads/master\n" +
"0000"
return copy(buf, []byte(payload)), io.EOF
}
func (s *testSmartSubtransportStream) Write(buf []byte) (int, error) {
return 0, io.EOF
}
func (s *testSmartSubtransportStream) Free() {
}
func TestTransport(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
callback := func(remote *Remote, transport *Transport) (SmartSubtransport, error) {
return &testSmartSubtransport{}, nil
}
registeredSmartTransport, err := NewRegisteredSmartTransport("foo", true, callback)
checkFatal(t, err)
defer registeredSmartTransport.Free()
remote, err := repo.Remotes.Create("test", "foo://bar")
checkFatal(t, err)
defer remote.Free()
err = remote.ConnectFetch(nil, nil, nil)
checkFatal(t, err)
remoteHeads, err := remote.Ls()
checkFatal(t, err)
expectedRemoteHeads := []RemoteHead{
{&Oid{}, "HEAD"},
{&Oid{}, "refs/heads/master"},
}
if !reflect.DeepEqual(expectedRemoteHeads, remoteHeads) {
t.Errorf("mismatched remote heads. expected %v, got %v", expectedRemoteHeads, remoteHeads)
}
}

25
tree.go
View File

@ -24,7 +24,6 @@ const (
) )
type Tree struct { type Tree struct {
doNotCompare
Object Object
cast_ptr *C.git_tree cast_ptr *C.git_tree
} }
@ -121,7 +120,7 @@ func (t *Tree) EntryCount() uint64 {
return uint64(num) return uint64(num)
} }
type TreeWalkCallback func(string, *TreeEntry) error type TreeWalkCallback func(string, *TreeEntry) int
type treeWalkCallbackData struct { type treeWalkCallbackData struct {
callback TreeWalkCallback callback TreeWalkCallback
errorTarget *error errorTarget *error
@ -134,30 +133,15 @@ func treeWalkCallback(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer
panic("invalid treewalk callback") panic("invalid treewalk callback")
} }
err := data.callback(C.GoString(_root), newTreeEntry(entry)) ret := data.callback(C.GoString(_root), newTreeEntry(entry))
if err == TreeWalkSkip { if ret < 0 {
return C.int(1) *data.errorTarget = errors.New(ErrorCode(ret).String())
} else if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser) return C.int(ErrorCodeUser)
} }
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
// TreeWalkSkip is an error that can be returned form TreeWalkCallback to skip
// a subtree from being expanded.
var TreeWalkSkip = errors.New("skip")
// Walk traverses the entries in a tree and its subtrees in pre order.
//
// The entries will be traversed in the pre order, children subtrees will be
// automatically loaded as required, and the callback will be called once per
// entry with the current (relative) root for the entry and the entry data
// itself.
//
// If the callback returns TreeWalkSkip, the passed entry will be skipped on
// the traversal. Any other non-nil error stops the walk.
func (t *Tree) Walk(callback TreeWalkCallback) error { func (t *Tree) Walk(callback TreeWalkCallback) error {
var err error var err error
data := treeWalkCallbackData{ data := treeWalkCallbackData{
@ -183,7 +167,6 @@ func (t *Tree) Walk(callback TreeWalkCallback) error {
} }
type TreeBuilder struct { type TreeBuilder struct {
doNotCompare
ptr *C.git_treebuilder ptr *C.git_treebuilder
repo *Repository repo *Repository
} }

View File

@ -1,9 +1,6 @@
package git package git
import ( import "testing"
"errors"
"testing"
)
func TestTreeEntryById(t *testing.T) { func TestTreeEntryById(t *testing.T) {
t.Parallel() t.Parallel()
@ -66,72 +63,3 @@ func TestTreeBuilderInsert(t *testing.T) {
t.Fatalf("got oid %v, want %v", entry.Id, blobId) t.Fatalf("got oid %v, want %v", entry.Id, blobId)
} }
} }
func TestTreeWalk(t *testing.T) {
t.Parallel()
repo, err := OpenRepository("testdata/TestGitRepository.git")
checkFatal(t, err)
treeID, err := NewOid("6020a3b8d5d636e549ccbd0c53e2764684bb3125")
checkFatal(t, err)
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
var callCount int
err = tree.Walk(func(name string, entry *TreeEntry) error {
callCount++
return nil
})
checkFatal(t, err)
if callCount != 11 {
t.Fatalf("got called %v times, want %v", callCount, 11)
}
}
func TestTreeWalkSkip(t *testing.T) {
t.Parallel()
repo, err := OpenRepository("testdata/TestGitRepository.git")
checkFatal(t, err)
treeID, err := NewOid("6020a3b8d5d636e549ccbd0c53e2764684bb3125")
checkFatal(t, err)
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
var callCount int
err = tree.Walk(func(name string, entry *TreeEntry) error {
callCount++
return TreeWalkSkip
})
checkFatal(t, err)
if callCount != 4 {
t.Fatalf("got called %v times, want %v", callCount, 4)
}
}
func TestTreeWalkStop(t *testing.T) {
t.Parallel()
repo, err := OpenRepository("testdata/TestGitRepository.git")
checkFatal(t, err)
treeID, err := NewOid("6020a3b8d5d636e549ccbd0c53e2764684bb3125")
checkFatal(t, err)
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
var callCount int
stopError := errors.New("stop")
err = tree.Walk(func(name string, entry *TreeEntry) error {
callCount++
return stopError
})
if err != stopError {
t.Fatalf("got error %v, want %v", err, stopError)
}
if callCount != 1 {
t.Fatalf("got called %v times, want %v", callCount, 1)
}
}

1
vendor/libgit2 vendored Submodule

@ -0,0 +1 @@
Subproject commit 7f4fa178629d559c037a1f72f79f79af9c1ef8ce

View File

@ -22,7 +22,6 @@ const (
) )
type RevWalk struct { type RevWalk struct {
doNotCompare
ptr *C.git_revwalk ptr *C.git_revwalk
repo *Repository repo *Repository
} }

143
wrapper.c
View File

@ -104,7 +104,7 @@ static int set_callback_error(char *error_message, int ret)
{ {
if (error_message != NULL) { if (error_message != NULL) {
if (ret < 0) if (ret < 0)
giterr_set_str(GIT_ERROR_CALLBACK, error_message); git_error_set_str(GIT_ERROR_CALLBACK, error_message);
free(error_message); free(error_message);
} }
return ret; return ret;
@ -116,28 +116,18 @@ void _go_git_populate_apply_callbacks(git_apply_options *options)
options->hunk_cb = (git_apply_hunk_cb)&hunkApplyCallback; options->hunk_cb = (git_apply_hunk_cb)&hunkApplyCallback;
} }
static int commit_create_callback( static int commit_signing_callback(
git_oid *out, git_buf *signature,
const git_signature *author, git_buf *signature_field,
const git_signature *committer, const char *commit_contents,
const char *message_encoding,
const char *message,
const git_tree *tree,
size_t parent_count,
const git_commit *parents[],
void *payload) void *payload)
{ {
char *error_message = NULL; char *error_message = NULL;
const int ret = commitCreateCallback( const int ret = commitSigningCallback(
&error_message, &error_message,
out, signature,
(git_signature *)author, signature_field,
(git_signature *)committer, (char *)commit_contents,
(char *)message_encoding,
(char *)message,
(git_tree *)tree,
parent_count,
(git_commit **)parents,
payload payload
); );
return set_callback_error(error_message, ret); return set_callback_error(error_message, ret);
@ -145,7 +135,7 @@ static int commit_create_callback(
void _go_git_populate_rebase_callbacks(git_rebase_options *opts) void _go_git_populate_rebase_callbacks(git_rebase_options *opts)
{ {
opts->commit_create_cb = commit_create_callback; opts->signing_cb = commit_signing_callback;
} }
void _go_git_populate_clone_callbacks(git_clone_options *opts) void _go_git_populate_clone_callbacks(git_clone_options *opts)
@ -514,116 +504,3 @@ int _go_git_indexer_new(
indexer_options.progress_cb_payload = progress_cb_payload; indexer_options.progress_cb_payload = progress_cb_payload;
return git_indexer_new(out, path, mode, odb, &indexer_options); return git_indexer_new(out, path, mode, odb, &indexer_options);
} }
static int smart_transport_callback(
git_transport **out,
git_remote *owner,
void *param)
{
char *error_message = NULL;
const int ret = smartTransportCallback(
&error_message,
out,
owner,
param);
return set_callback_error(error_message, ret);
}
int _go_git_transport_register(const char *prefix, void *param)
{
return git_transport_register(prefix, smart_transport_callback, param);
}
static int smart_subtransport_action_callback(
git_smart_subtransport_stream **out,
git_smart_subtransport *transport,
const char *url,
git_smart_service_t action)
{
char *error_message = NULL;
const int ret = smartSubtransportActionCallback(
&error_message,
out,
transport,
(char *)url,
action);
return set_callback_error(error_message, ret);
}
static int smart_subtransport_close_callback(git_smart_subtransport *transport)
{
char *error_message = NULL;
const int ret = smartSubtransportCloseCallback(
&error_message,
transport);
return set_callback_error(error_message, ret);
}
static int smart_subtransport_callback(
git_smart_subtransport **out,
git_transport *owner,
void *param)
{
_go_managed_smart_subtransport *subtransport = (_go_managed_smart_subtransport *)param;
subtransport->parent.action = smart_subtransport_action_callback;
subtransport->parent.close = smart_subtransport_close_callback;
subtransport->parent.free = smartSubtransportFreeCallback;
*out = &subtransport->parent;
char *error_message = NULL;
const int ret = smartTransportSubtransportCallback(&error_message, subtransport, owner);
return set_callback_error(error_message, ret);
}
int _go_git_transport_smart(
git_transport **out,
git_remote *owner,
int stateless,
_go_managed_smart_subtransport *subtransport_payload)
{
git_smart_subtransport_definition definition = {
smart_subtransport_callback,
stateless,
subtransport_payload,
};
return git_transport_smart(out, owner, &definition);
}
static int smart_subtransport_stream_read_callback(
git_smart_subtransport_stream *stream,
char *buffer,
size_t buf_size,
size_t *bytes_read)
{
char *error_message = NULL;
const int ret = smartSubtransportStreamReadCallback(
&error_message,
stream,
buffer,
buf_size,
bytes_read);
return set_callback_error(error_message, ret);
}
static int smart_subtransport_stream_write_callback(
git_smart_subtransport_stream *stream,
const char *buffer,
size_t len)
{
char *error_message = NULL;
const int ret = smartSubtransportStreamWriteCallback(
&error_message,
stream,
(char *)buffer,
len);
return set_callback_error(error_message, ret);
}
void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stream *stream)
{
_go_managed_smart_subtransport_stream *managed_stream = (_go_managed_smart_subtransport_stream *)stream;
managed_stream->parent.read = smart_subtransport_stream_read_callback;
managed_stream->parent.write = smart_subtransport_stream_write_callback;
managed_stream->parent.free = smartSubtransportStreamFreeCallback;
}