Compare commits
93 Commits
main
...
cherry-pic
Author | SHA1 | Date |
---|---|---|
|
a164eb120f | |
|
a61660f016 | |
|
732e2ac053 | |
|
7de5189dbe | |
|
daaf419019 | |
|
6dc9c7cc88 | |
|
467e9a7a64 | |
|
2e96f11939 | |
|
cc43b2a826 | |
|
b5a3f3469f | |
|
5ae36de70a | |
|
c0026d20df | |
|
feaae57162 | |
|
f0a6f137f2 | |
|
4638f434c0 | |
|
5d2eae54da | |
|
78bd73e5c2 | |
|
bd435e1623 | |
|
bea83b7ae4 | |
|
ea885e4fa7 | |
|
0bfc9d1536 | |
|
77a92cd976 | |
|
5cc1c58fcd | |
|
8509b7efbe | |
|
317b84a06b | |
|
6af3c98deb | |
|
a8763eaaed | |
|
8def210c8d | |
|
2d32dd2c58 | |
|
5de2c34f33 | |
|
2a14d90d6c | |
|
f9b9359f7f | |
|
f09f9c89ae | |
|
aae7e1b7c8 | |
|
2ab8583abe | |
|
af81980883 | |
|
8d28e2a8dd | |
|
3893405679 | |
|
b00c365f50 | |
|
bd9b40fc67 | |
|
c18989f652 | |
|
2870fabe22 | |
|
6badd3d00d | |
|
8b6f3d8056 | |
|
b6212551e2 | |
|
4b27f5c430 | |
|
3fcab7513d | |
|
1cf6bf8314 | |
|
fb4e5911aa | |
|
4883fbe872 | |
|
61ea21fbd6 | |
|
a3140afde2 | |
|
20c6fefa56 | |
|
fcb8a8cc3b | |
|
19b0d4241c | |
|
64c6ddb513 | |
|
6c1af418ae | |
|
d5fa51db4f | |
|
d6c2d12ee1 | |
|
decab2d988 | |
|
e4b04e142b | |
|
496b346c97 | |
|
354218d9bb | |
|
7b83382e04 | |
|
b8df55f956 | |
|
b2fae9430f | |
|
707a03b581 | |
|
4681adf414 | |
|
752bac6334 | |
|
84b7f45673 | |
|
1526448a89 | |
|
f4a3dd4cc7 | |
|
1c82797e35 | |
|
2fe03c06e7 | |
|
686500ef90 | |
|
d84378238a | |
|
e43846bffd | |
|
bd2a0c830e | |
|
b55ee91e3c | |
|
2fba250690 | |
|
d47253fb80 | |
|
9f2bfe5f32 | |
|
75b56d820b | |
|
61d996a04e | |
|
cba876ecd1 | |
|
f3992bae37 | |
|
e7a003dacc | |
|
19ac948456 | |
|
7694d5f5fc | |
|
437c7c3344 | |
|
70058bd989 | |
|
b1eec9a466 | |
|
a478c6d7ef |
|
@ -0,0 +1,54 @@
|
|||
name: Backport to older releases
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
|
||||
backport:
|
||||
name: Backport change to branch ${{ matrix.branch }}
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
branch: [ '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)"
|
|
@ -0,0 +1,127 @@
|
|||
name: git2go CI
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- release-*
|
||||
- v*
|
||||
|
||||
jobs:
|
||||
|
||||
build-static:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
go: [ '1.11', '1.12', '1.13', '1.14', '1.15', '1.16', '1.17' ]
|
||||
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.17'
|
||||
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
|
||||
name: Go (system-wide, dynamic)
|
||||
|
||||
runs-on: ubuntu-20.04
|
||||
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: '1.17'
|
||||
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 --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.17'
|
||||
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" ./...
|
||||
|
||||
check-generate:
|
||||
name: Check generated files were not modified
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: '1.17'
|
||||
id: go
|
||||
- name: Check out code into the Go module directory
|
||||
uses: actions/checkout@v2
|
||||
- name: Install libgit2 build dependencies
|
||||
run: |
|
||||
git submodule update --init
|
||||
sudo apt-get install -y --no-install-recommends libssh2-1-dev
|
||||
go install golang.org/x/tools/cmd/stringer@latest
|
||||
- name: Generate files
|
||||
run: |
|
||||
export PATH=$(go env GOPATH)/bin:$PATH
|
||||
make generate
|
||||
- name: Check nothing changed
|
||||
run: git diff --quiet --exit-code || (echo "detected changes after generate" ; git status ; exit 1)
|
|
@ -0,0 +1,28 @@
|
|||
name: Tag new releases
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- 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@43ed073f5c1445ca8b80d920ce2f8fa550ae4e8d
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
WITH_V: true
|
||||
DEFAULT_BUMP: patch
|
||||
TAG_CONTEXT: branch
|
||||
RELEASE_BRANCHES: .*
|
|
@ -1 +1,2 @@
|
|||
/static-build/
|
||||
/dynamic-build/
|
||||
|
|
26
.travis.yml
26
.travis.yml
|
@ -1,26 +0,0 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- 1.7
|
||||
- 1.8
|
||||
- 1.9
|
||||
- "1.10"
|
||||
- tip
|
||||
|
||||
script: make test-static
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- go: tip
|
||||
|
||||
git:
|
||||
submodules: false
|
||||
|
||||
before_install:
|
||||
- git submodule update --init
|
||||
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- /v\d+/
|
||||
- next
|
|
@ -1,16 +1,16 @@
|
|||
// +build static
|
||||
// +build static,!system_libgit2
|
||||
|
||||
package git
|
||||
|
||||
/*
|
||||
#cgo windows CFLAGS: -I${SRCDIR}/static-build/install/include/
|
||||
#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp
|
||||
#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp -lws2_32 -lole32 -lrpcrt4 -lcrypt32
|
||||
#cgo !windows pkg-config: --static ${SRCDIR}/static-build/install/lib/pkgconfig/libgit2.pc
|
||||
#cgo CFLAGS: -DLIBGIT2_STATIC
|
||||
#include <git2.h>
|
||||
|
||||
#if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 28
|
||||
# error "Invalid libgit2 version; this git2go supports libgit2 v0.28"
|
||||
#endif
|
||||
|
||||
*/
|
||||
import "C"
|
|
@ -3,12 +3,12 @@
|
|||
package git
|
||||
|
||||
/*
|
||||
#include <git2.h>
|
||||
#cgo pkg-config: libgit2
|
||||
#cgo CFLAGS: -DLIBGIT2_DYNAMIC
|
||||
#include <git2.h>
|
||||
|
||||
#if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 27
|
||||
# error "Invalid libgit2 version; this git2go supports libgit2 v0.27"
|
||||
#if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 28
|
||||
# error "Invalid libgit2 version; this git2go supports libgit2 v0.28"
|
||||
#endif
|
||||
|
||||
*/
|
||||
import "C"
|
|
@ -0,0 +1,14 @@
|
|||
// +build static,system_libgit2
|
||||
|
||||
package git
|
||||
|
||||
/*
|
||||
#cgo pkg-config: libgit2 --static
|
||||
#cgo CFLAGS: -DLIBGIT2_STATIC
|
||||
#include <git2.h>
|
||||
|
||||
#if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 28
|
||||
# error "Invalid libgit2 version; this git2go supports libgit2 v0.28"
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
59
Makefile
59
Makefile
|
@ -1,18 +1,59 @@
|
|||
TEST_ARGS ?= --count=1
|
||||
|
||||
default: test
|
||||
|
||||
test: build-libgit2
|
||||
go run script/check-MakeGitError-thread-lock.go
|
||||
go test ./...
|
||||
|
||||
install: build-libgit2
|
||||
generate: static-build/install/lib/libgit2.a
|
||||
go generate --tags "static" ./...
|
||||
|
||||
# System library
|
||||
# ==============
|
||||
# This uses whatever version of libgit2 can be found in the system.
|
||||
test:
|
||||
go run script/check-MakeGitError-thread-lock.go
|
||||
go test $(TEST_ARGS) ./...
|
||||
|
||||
install:
|
||||
go install ./...
|
||||
|
||||
build-libgit2:
|
||||
# Bundled dynamic library
|
||||
# =======================
|
||||
# In order to avoid having to manipulate `git_dynamic.go`, which would prevent
|
||||
# the system-wide libgit2.so from being used in a sort of ergonomic way, this
|
||||
# instead moves the complexity of overriding the paths so that the built
|
||||
# libraries can be found by the build and tests.
|
||||
.PHONY: build-libgit2-dynamic
|
||||
build-libgit2-dynamic:
|
||||
./script/build-libgit2-dynamic.sh
|
||||
|
||||
dynamic-build/install/lib/libgit2.so:
|
||||
./script/build-libgit2-dynamic.sh
|
||||
|
||||
test-dynamic: dynamic-build/install/lib/libgit2.so
|
||||
PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \
|
||||
go run script/check-MakeGitError-thread-lock.go
|
||||
PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \
|
||||
LD_LIBRARY_PATH=dynamic-build/install/lib \
|
||||
go test $(TEST_ARGS) ./...
|
||||
|
||||
install-dynamic: dynamic-build/install/lib/libgit2.so
|
||||
PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \
|
||||
go install ./...
|
||||
|
||||
# Bundled static library
|
||||
# ======================
|
||||
# This is mostly used in tests, but can also be used to provide a
|
||||
# statically-linked library with the bundled version of libgit2.
|
||||
.PHONY: build-libgit2-static
|
||||
build-libgit2-static:
|
||||
./script/build-libgit2-static.sh
|
||||
|
||||
install-static: build-libgit2
|
||||
go install --tags "static" ./...
|
||||
static-build/install/lib/libgit2.a:
|
||||
./script/build-libgit2-static.sh
|
||||
|
||||
test-static: build-libgit2
|
||||
test-static: static-build/install/lib/libgit2.a
|
||||
go run script/check-MakeGitError-thread-lock.go
|
||||
go test --tags "static" ./...
|
||||
go test --tags "static" $(TEST_ARGS) ./...
|
||||
|
||||
install-static: static-build/install/lib/libgit2.a
|
||||
go install --tags "static" ./...
|
||||
|
|
64
README.md
64
README.md
|
@ -1,18 +1,36 @@
|
|||
git2go
|
||||
======
|
||||
[![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)
|
||||
[![GoDoc](https://godoc.org/github.com/libgit2/git2go?status.svg)](http://godoc.org/github.com/libgit2/git2go/v28) [![Build Status](https://travis-ci.org/libgit2/git2go.svg?branch=main)](https://travis-ci.org/libgit2/git2go)
|
||||
|
||||
Go bindings for [libgit2](http://libgit2.github.com/).
|
||||
|
||||
### Which branch to use
|
||||
### Which Go version to use
|
||||
|
||||
The numbered branches work against the version of libgit2 as specified by their number. You can import them in your project via gopkg.in, e.g. if you have libgit2 v0.25 installed you'd import with
|
||||
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:
|
||||
|
||||
import "gopkg.in/libgit2/git2go.v25"
|
||||
| libgit2 | git2go |
|
||||
|---------|---------------|
|
||||
| main | (will be 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 v0.28 installed, you'd import git2go v28 with
|
||||
|
||||
```sh
|
||||
go get github.com/libgit2/git2go/v28
|
||||
```
|
||||
```go
|
||||
import "github.com/libgit2/git2go/v28"
|
||||
```
|
||||
|
||||
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 its own API nor does it have expectations the stability of libgit2's. Thus this only supports statically linking against libgit2.
|
||||
The `main` 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
|
||||
|
||||
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
|
||||
----------
|
||||
|
@ -24,20 +42,40 @@ This project wraps the functionality provided by libgit2. If you're using a vers
|
|||
|
||||
### Versioned branch, dynamic linking
|
||||
|
||||
When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via gopkg.in, e.g. to work against libgit2 v0.25
|
||||
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 v0.28
|
||||
|
||||
import "gopkg.in/libgit2/git2go.v25"
|
||||
```go
|
||||
import "github.com/libgit2/git2go/v28"
|
||||
```
|
||||
|
||||
### Master branch, or static linking
|
||||
### Versioned branch, static linking
|
||||
|
||||
If using `master` or building a branch 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.
|
||||
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/...
|
||||
|
||||
### `main` branch, or vendored static linking
|
||||
|
||||
If using `main` 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.
|
||||
will compile libgit2, link it into git2go and install it. The `main` 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/v28 => ../../libgit2/git2go
|
||||
|
||||
Parallelism and network operations
|
||||
----------------------------------
|
||||
|
@ -47,14 +85,14 @@ libgit2 may use OpenSSL and LibSSH2 for performing encrypted network connections
|
|||
Running the tests
|
||||
-----------------
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
make test-static
|
||||
|
||||
Alternatively, you can build the library manually first and then run the tests
|
||||
|
||||
./script/build-libgit2-static.sh
|
||||
go test -v --tags "static" ./...
|
||||
make install-static
|
||||
go test -v -tags static ./...
|
||||
|
||||
License
|
||||
-------
|
||||
|
|
2
blame.go
2
blame.go
|
@ -47,6 +47,7 @@ const (
|
|||
BlameTrackCopiesSameCommitCopies BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES
|
||||
BlameTrackCopiesAnyCommitCopies BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES
|
||||
BlameFirstParent BlameOptionsFlag = C.GIT_BLAME_FIRST_PARENT
|
||||
BlameUseMailmap BlameOptionsFlag = C.GIT_BLAME_USE_MAILMAP
|
||||
)
|
||||
|
||||
func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error) {
|
||||
|
@ -85,6 +86,7 @@ func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error)
|
|||
}
|
||||
|
||||
type Blame struct {
|
||||
doNotCompare
|
||||
ptr *C.git_blame
|
||||
}
|
||||
|
||||
|
|
35
blob.go
35
blob.go
|
@ -9,13 +9,13 @@ void _go_git_writestream_free(git_writestream *stream);
|
|||
*/
|
||||
import "C"
|
||||
import (
|
||||
"io"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Blob struct {
|
||||
doNotCompare
|
||||
Object
|
||||
cast_ptr *C.git_blob
|
||||
}
|
||||
|
@ -40,6 +40,12 @@ func (v *Blob) Contents() []byte {
|
|||
return goBytes
|
||||
}
|
||||
|
||||
func (v *Blob) IsBinary() bool {
|
||||
ret := C.git_blob_is_binary(v.cast_ptr) == 1
|
||||
runtime.KeepAlive(v)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
@ -70,32 +76,6 @@ func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
|
|||
return newOidFromC(&id), nil
|
||||
}
|
||||
|
||||
type BlobChunkCallback func(maxLen int) ([]byte, error)
|
||||
|
||||
type BlobCallbackData struct {
|
||||
Callback BlobChunkCallback
|
||||
Error error
|
||||
}
|
||||
|
||||
//export blobChunkCb
|
||||
func blobChunkCb(buffer *C.char, maxLen C.size_t, handle unsafe.Pointer) int {
|
||||
payload := pointerHandles.Get(handle)
|
||||
data, ok := payload.(*BlobCallbackData)
|
||||
if !ok {
|
||||
panic("could not retrieve blob callback data")
|
||||
}
|
||||
|
||||
goBuf, err := data.Callback(int(maxLen))
|
||||
if err == io.EOF {
|
||||
return 0
|
||||
} else if err != nil {
|
||||
data.Error = err
|
||||
return -1
|
||||
}
|
||||
C.memcpy(unsafe.Pointer(buffer), unsafe.Pointer(&goBuf[0]), C.size_t(len(goBuf)))
|
||||
return len(goBuf)
|
||||
}
|
||||
|
||||
func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, error) {
|
||||
var chintPath *C.char = nil
|
||||
var stream *C.git_writestream
|
||||
|
@ -117,6 +97,7 @@ func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, err
|
|||
}
|
||||
|
||||
type BlobWriteStream struct {
|
||||
doNotCompare
|
||||
ptr *C.git_writestream
|
||||
repo *Repository
|
||||
}
|
||||
|
|
20
blob_test.go
20
blob_test.go
|
@ -28,7 +28,21 @@ func TestCreateBlobFromBuffer(t *testing.T) {
|
|||
t.Fatal("Empty buffer did not deliver empty blob id")
|
||||
}
|
||||
|
||||
for _, data := range []([]byte){[]byte("hello there"), doublePointerBytes()} {
|
||||
tests := []struct {
|
||||
data []byte
|
||||
isBinary bool
|
||||
}{
|
||||
{
|
||||
data: []byte("hello there"),
|
||||
isBinary: false,
|
||||
},
|
||||
{
|
||||
data: doublePointerBytes(),
|
||||
isBinary: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
data := tt.data
|
||||
id, err = repo.CreateBlobFromBuffer(data)
|
||||
checkFatal(t, err)
|
||||
|
||||
|
@ -38,5 +52,9 @@ func TestCreateBlobFromBuffer(t *testing.T) {
|
|||
t.Fatal("Loaded bytes don't match original bytes:",
|
||||
blob.Contents(), "!=", data)
|
||||
}
|
||||
want := tt.isBinary
|
||||
if got := blob.IsBinary(); got != want {
|
||||
t.Fatalf("IsBinary() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ const (
|
|||
)
|
||||
|
||||
type Branch struct {
|
||||
doNotCompare
|
||||
*Reference
|
||||
}
|
||||
|
||||
|
@ -27,6 +28,7 @@ func (r *Reference) Branch() *Branch {
|
|||
}
|
||||
|
||||
type BranchIterator struct {
|
||||
doNotCompare
|
||||
ptr *C.git_branch_iterator
|
||||
repo *Repository
|
||||
}
|
||||
|
@ -73,7 +75,7 @@ func (i *BranchIterator) ForEach(f BranchIteratorFunc) error {
|
|||
}
|
||||
}
|
||||
|
||||
if err != nil && IsErrorCode(err, ErrIterOver) {
|
||||
if err != nil && IsErrorCode(err, ErrorCodeIterOver) {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ func TestBranchIterator(t *testing.T) {
|
|||
t.Fatalf("expected BranchLocal, not %v", t)
|
||||
}
|
||||
b, bt, err = i.Next()
|
||||
if !IsErrorCode(err, ErrIterOver) {
|
||||
if !IsErrorCode(err, ErrorCodeIterOver) {
|
||||
t.Fatal("expected iterover")
|
||||
}
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ func TestBranchIteratorEach(t *testing.T) {
|
|||
}
|
||||
|
||||
err = i.ForEach(f)
|
||||
if err != nil && !IsErrorCode(err, ErrIterOver) {
|
||||
if err != nil && !IsErrorCode(err, ErrorCodeIterOver) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
|
171
checkout.go
171
checkout.go
|
@ -3,10 +3,11 @@ package git
|
|||
/*
|
||||
#include <git2.h>
|
||||
|
||||
extern void _go_git_populate_checkout_cb(git_checkout_options *opts);
|
||||
extern void _go_git_populate_checkout_callbacks(git_checkout_options *opts);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
@ -51,7 +52,7 @@ const (
|
|||
type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) ErrorCode
|
||||
type CheckoutProgressCallback func(path string, completed, total uint) ErrorCode
|
||||
|
||||
type CheckoutOpts struct {
|
||||
type CheckoutOptions struct {
|
||||
Strategy CheckoutStrategy // Default will be a dry run
|
||||
DisableFilters bool // Don't apply filters like CRLF conversion
|
||||
DirMode os.FileMode // Default is 0755
|
||||
|
@ -65,19 +66,20 @@ type CheckoutOpts struct {
|
|||
Baseline *Tree
|
||||
}
|
||||
|
||||
func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOpts {
|
||||
opts := CheckoutOpts{}
|
||||
opts.Strategy = CheckoutStrategy(c.checkout_strategy)
|
||||
opts.DisableFilters = c.disable_filters != 0
|
||||
opts.DirMode = os.FileMode(c.dir_mode)
|
||||
opts.FileMode = os.FileMode(c.file_mode)
|
||||
opts.FileOpenFlags = int(c.file_open_flags)
|
||||
opts.NotifyFlags = CheckoutNotifyType(c.notify_flags)
|
||||
func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOptions {
|
||||
opts := CheckoutOptions{
|
||||
Strategy: CheckoutStrategy(c.checkout_strategy),
|
||||
DisableFilters: c.disable_filters != 0,
|
||||
DirMode: os.FileMode(c.dir_mode),
|
||||
FileMode: os.FileMode(c.file_mode),
|
||||
FileOpenFlags: int(c.file_open_flags),
|
||||
NotifyFlags: CheckoutNotifyType(c.notify_flags),
|
||||
}
|
||||
if c.notify_payload != nil {
|
||||
opts.NotifyCallback = pointerHandles.Get(c.notify_payload).(*CheckoutOpts).NotifyCallback
|
||||
opts.NotifyCallback = pointerHandles.Get(c.notify_payload).(*checkoutCallbackData).options.NotifyCallback
|
||||
}
|
||||
if c.progress_payload != nil {
|
||||
opts.ProgressCallback = pointerHandles.Get(c.progress_payload).(*CheckoutOpts).ProgressCallback
|
||||
opts.ProgressCallback = pointerHandles.Get(c.progress_payload).(*checkoutCallbackData).options.ProgressCallback
|
||||
}
|
||||
if c.target_directory != nil {
|
||||
opts.TargetDirectory = C.GoString(c.target_directory)
|
||||
|
@ -85,19 +87,19 @@ func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOpts {
|
|||
return opts
|
||||
}
|
||||
|
||||
func (opts *CheckoutOpts) toC() *C.git_checkout_options {
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
c := C.git_checkout_options{}
|
||||
populateCheckoutOpts(&c, opts)
|
||||
return &c
|
||||
type checkoutCallbackData struct {
|
||||
options *CheckoutOptions
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
//export checkoutNotifyCallback
|
||||
func checkoutNotifyCallback(why C.git_checkout_notify_t, cpath *C.char, cbaseline, ctarget, cworkdir, data unsafe.Pointer) int {
|
||||
if data == nil {
|
||||
return 0
|
||||
func checkoutNotifyCallback(
|
||||
why C.git_checkout_notify_t,
|
||||
cpath *C.char,
|
||||
cbaseline, ctarget, cworkdir, handle unsafe.Pointer,
|
||||
) C.int {
|
||||
if handle == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
path := C.GoString(cpath)
|
||||
var baseline, target, workdir DiffFile
|
||||
|
@ -110,85 +112,105 @@ func checkoutNotifyCallback(why C.git_checkout_notify_t, cpath *C.char, cbaselin
|
|||
if cworkdir != nil {
|
||||
workdir = diffFileFromC((*C.git_diff_file)(cworkdir))
|
||||
}
|
||||
opts := pointerHandles.Get(data).(*CheckoutOpts)
|
||||
if opts.NotifyCallback == nil {
|
||||
return 0
|
||||
data := pointerHandles.Get(handle).(*checkoutCallbackData)
|
||||
if data.options.NotifyCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
return int(opts.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir))
|
||||
ret := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir)
|
||||
if ret < 0 {
|
||||
*data.errorTarget = errors.New(ErrorCode(ret).String())
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export checkoutProgressCallback
|
||||
func checkoutProgressCallback(path *C.char, completed_steps, total_steps C.size_t, data unsafe.Pointer) int {
|
||||
opts := pointerHandles.Get(data).(*CheckoutOpts)
|
||||
if opts.ProgressCallback == nil {
|
||||
return 0
|
||||
func checkoutProgressCallback(
|
||||
path *C.char,
|
||||
completed_steps, total_steps C.size_t,
|
||||
handle unsafe.Pointer,
|
||||
) {
|
||||
data := pointerHandles.Get(handle).(*checkoutCallbackData)
|
||||
if data.options.ProgressCallback == nil {
|
||||
return
|
||||
}
|
||||
return int(opts.ProgressCallback(C.GoString(path), uint(completed_steps), uint(total_steps)))
|
||||
data.options.ProgressCallback(C.GoString(path), uint(completed_steps), uint(total_steps))
|
||||
}
|
||||
|
||||
// Convert the CheckoutOpts struct to the corresponding
|
||||
// C-struct. Returns a pointer to ptr, or nil if opts is nil, in order
|
||||
// to help with what to pass.
|
||||
func populateCheckoutOpts(ptr *C.git_checkout_options, opts *CheckoutOpts) *C.git_checkout_options {
|
||||
// populateCheckoutOptions populates the provided C-struct with the contents of
|
||||
// the provided CheckoutOptions struct. Returns copts, or nil if opts is nil,
|
||||
// in order to help with what to pass.
|
||||
func populateCheckoutOptions(copts *C.git_checkout_options, opts *CheckoutOptions, errorTarget *error) *C.git_checkout_options {
|
||||
C.git_checkout_init_options(copts, C.GIT_CHECKOUT_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
C.git_checkout_init_options(ptr, 1)
|
||||
ptr.checkout_strategy = C.uint(opts.Strategy)
|
||||
ptr.disable_filters = cbool(opts.DisableFilters)
|
||||
ptr.dir_mode = C.uint(opts.DirMode.Perm())
|
||||
ptr.file_mode = C.uint(opts.FileMode.Perm())
|
||||
ptr.notify_flags = C.uint(opts.NotifyFlags)
|
||||
copts.checkout_strategy = C.uint(opts.Strategy)
|
||||
copts.disable_filters = cbool(opts.DisableFilters)
|
||||
copts.dir_mode = C.uint(opts.DirMode.Perm())
|
||||
copts.file_mode = C.uint(opts.FileMode.Perm())
|
||||
copts.notify_flags = C.uint(opts.NotifyFlags)
|
||||
if opts.NotifyCallback != nil || opts.ProgressCallback != nil {
|
||||
C._go_git_populate_checkout_cb(ptr)
|
||||
}
|
||||
payload := pointerHandles.Track(opts)
|
||||
if opts.NotifyCallback != nil {
|
||||
ptr.notify_payload = payload
|
||||
}
|
||||
if opts.ProgressCallback != nil {
|
||||
ptr.progress_payload = payload
|
||||
C._go_git_populate_checkout_callbacks(copts)
|
||||
data := &checkoutCallbackData{
|
||||
options: opts,
|
||||
errorTarget: errorTarget,
|
||||
}
|
||||
payload := pointerHandles.Track(data)
|
||||
if opts.NotifyCallback != nil {
|
||||
copts.notify_payload = payload
|
||||
}
|
||||
if opts.ProgressCallback != nil {
|
||||
copts.progress_payload = payload
|
||||
}
|
||||
}
|
||||
if opts.TargetDirectory != "" {
|
||||
ptr.target_directory = C.CString(opts.TargetDirectory)
|
||||
copts.target_directory = C.CString(opts.TargetDirectory)
|
||||
}
|
||||
if len(opts.Paths) > 0 {
|
||||
ptr.paths.strings = makeCStringsFromStrings(opts.Paths)
|
||||
ptr.paths.count = C.size_t(len(opts.Paths))
|
||||
copts.paths.strings = makeCStringsFromStrings(opts.Paths)
|
||||
copts.paths.count = C.size_t(len(opts.Paths))
|
||||
}
|
||||
|
||||
if opts.Baseline != nil {
|
||||
ptr.baseline = opts.Baseline.cast_ptr
|
||||
copts.baseline = opts.Baseline.cast_ptr
|
||||
}
|
||||
|
||||
return ptr
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeCheckoutOpts(ptr *C.git_checkout_options) {
|
||||
if ptr == nil {
|
||||
func freeCheckoutOptions(copts *C.git_checkout_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
C.free(unsafe.Pointer(ptr.target_directory))
|
||||
if ptr.paths.count > 0 {
|
||||
freeStrarray(&ptr.paths)
|
||||
C.free(unsafe.Pointer(copts.target_directory))
|
||||
if copts.paths.count > 0 {
|
||||
freeStrarray(&copts.paths)
|
||||
}
|
||||
if ptr.notify_payload != nil {
|
||||
pointerHandles.Untrack(ptr.notify_payload)
|
||||
if copts.notify_payload != nil {
|
||||
pointerHandles.Untrack(copts.notify_payload)
|
||||
} else if copts.progress_payload != nil {
|
||||
pointerHandles.Untrack(copts.progress_payload)
|
||||
}
|
||||
}
|
||||
|
||||
// Updates files in the index and the working tree to match the content of
|
||||
// the commit pointed at by HEAD. opts may be nil.
|
||||
func (v *Repository) CheckoutHead(opts *CheckoutOpts) error {
|
||||
func (v *Repository) CheckoutHead(opts *CheckoutOptions) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
cOpts := opts.toC()
|
||||
defer freeCheckoutOpts(cOpts)
|
||||
var err error
|
||||
cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err)
|
||||
defer freeCheckoutOptions(cOpts)
|
||||
|
||||
ret := C.git_checkout_head(v.ptr, cOpts)
|
||||
runtime.KeepAlive(v)
|
||||
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
@ -199,7 +221,7 @@ func (v *Repository) CheckoutHead(opts *CheckoutOpts) error {
|
|||
// Updates files in the working tree to match the content of the given
|
||||
// index. If index is nil, the repository's index will be used. opts
|
||||
// may be nil.
|
||||
func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error {
|
||||
func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOptions) error {
|
||||
var iptr *C.git_index = nil
|
||||
if index != nil {
|
||||
iptr = index.ptr
|
||||
|
@ -208,11 +230,15 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error {
|
|||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
cOpts := opts.toC()
|
||||
defer freeCheckoutOpts(cOpts)
|
||||
var err error
|
||||
cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err)
|
||||
defer freeCheckoutOptions(cOpts)
|
||||
|
||||
ret := C.git_checkout_index(v.ptr, iptr, cOpts)
|
||||
runtime.KeepAlive(v)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
@ -220,15 +246,20 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOpts) error {
|
||||
func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOptions) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
cOpts := opts.toC()
|
||||
defer freeCheckoutOpts(cOpts)
|
||||
var err error
|
||||
cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err)
|
||||
defer freeCheckoutOptions(cOpts)
|
||||
|
||||
ret := C.git_checkout_tree(v.ptr, tree.ptr, cOpts)
|
||||
runtime.KeepAlive(v)
|
||||
runtime.KeepAlive(tree)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ type CherrypickOptions struct {
|
|||
Version uint
|
||||
Mainline uint
|
||||
MergeOpts MergeOptions
|
||||
CheckoutOpts CheckoutOpts
|
||||
CheckoutOpts CheckoutOptions
|
||||
}
|
||||
|
||||
func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions {
|
||||
|
@ -25,23 +25,23 @@ func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions {
|
|||
return opts
|
||||
}
|
||||
|
||||
func (opts *CherrypickOptions) toC() *C.git_cherrypick_options {
|
||||
func populateCherrypickOptions(copts *C.git_cherrypick_options, opts *CherrypickOptions, errorTarget *error) *C.git_cherrypick_options {
|
||||
C.git_cherrypick_init_options(copts, C.GIT_CHERRYPICK_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
c := C.git_cherrypick_options{}
|
||||
c.version = C.uint(opts.Version)
|
||||
c.mainline = C.uint(opts.Mainline)
|
||||
c.merge_opts = *opts.MergeOpts.toC()
|
||||
c.checkout_opts = *opts.CheckoutOpts.toC()
|
||||
return &c
|
||||
copts.mainline = C.uint(opts.Mainline)
|
||||
populateMergeOptions(&copts.merge_opts, &opts.MergeOpts)
|
||||
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOpts, errorTarget)
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeCherrypickOpts(ptr *C.git_cherrypick_options) {
|
||||
if ptr == nil {
|
||||
func freeCherrypickOpts(copts *C.git_cherrypick_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
freeCheckoutOpts(&ptr.checkout_opts)
|
||||
freeMergeOptions(&copts.merge_opts)
|
||||
freeCheckoutOptions(&copts.checkout_opts)
|
||||
}
|
||||
|
||||
func DefaultCherrypickOptions() (CherrypickOptions, error) {
|
||||
|
@ -62,14 +62,35 @@ func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error {
|
|||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
cOpts := opts.toC()
|
||||
var err error
|
||||
cOpts := populateCherrypickOptions(&C.git_cherrypick_options{}, &opts, &err)
|
||||
defer freeCherrypickOpts(cOpts)
|
||||
|
||||
ecode := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts)
|
||||
ret := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts)
|
||||
runtime.KeepAlive(v)
|
||||
runtime.KeepAlive(commit)
|
||||
if ecode < 0 {
|
||||
return MakeGitError(ecode)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) CherrypickCommit(pick, our *Commit, opts CherrypickOptions) (*Index, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
cOpts := populateMergeOptions(&C.git_merge_options{}, &opts.MergeOpts)
|
||||
defer freeMergeOptions(cOpts)
|
||||
|
||||
var ptr *C.git_index
|
||||
ret := C.git_cherrypick_commit(&ptr, r.ptr, pick.cast_ptr, our.cast_ptr, C.uint(opts.Mainline), cOpts)
|
||||
runtime.KeepAlive(pick)
|
||||
runtime.KeepAlive(our)
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return newIndexFromC(ptr, r), nil
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ func checkout(t *testing.T, repo *Repository, commit *Commit) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = repo.CheckoutTree(tree, &CheckoutOpts{Strategy: CheckoutSafe})
|
||||
err = repo.CheckoutTree(tree, &CheckoutOptions{Strategy: CheckoutSafe})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -84,3 +84,57 @@ func TestCherrypick(t *testing.T) {
|
|||
t.Fatal("Incorrect repository state: ", state)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCherrypickCommit(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
c1, _ := seedTestRepo(t, repo)
|
||||
c2, _ := updateReadme(t, repo, content)
|
||||
|
||||
commit1, err := repo.LookupCommit(c1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
commit2, err := repo.LookupCommit(c2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
checkout(t, repo, commit1)
|
||||
|
||||
if got := readReadme(t, repo); got == content {
|
||||
t.Fatalf("README = %q, want %q", got, content)
|
||||
}
|
||||
|
||||
opts, err := DefaultCherrypickOptions()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
idx, err := repo.CherrypickCommit(commit2, commit1, opts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer idx.Free()
|
||||
|
||||
// The file is only updated in the index, not in the working directory.
|
||||
if got := readReadme(t, repo); got == content {
|
||||
t.Errorf("README = %q, want %q", got, content)
|
||||
}
|
||||
if got := repo.State(); got != RepositoryStateNone {
|
||||
t.Errorf("repo.State() = %v, want %v", got, RepositoryStateCherrypick)
|
||||
}
|
||||
|
||||
if got := idx.EntryCount(); got != 1 {
|
||||
t.Fatalf("idx.EntryCount() = %v, want %v", got, 1)
|
||||
}
|
||||
entry, err := idx.EntryByIndex(0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if entry.Path != "README" {
|
||||
t.Errorf("entry.Path = %v, want %v", entry.Path, "README")
|
||||
}
|
||||
}
|
||||
|
|
104
clone.go
104
clone.go
|
@ -3,10 +3,11 @@ package git
|
|||
/*
|
||||
#include <git2.h>
|
||||
|
||||
extern void _go_git_populate_remote_cb(git_clone_options *opts);
|
||||
extern void _go_git_populate_clone_callbacks(git_clone_options *opts);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -28,20 +29,23 @@ func Clone(url string, path string, options *CloneOptions) (*Repository, error)
|
|||
cpath := C.CString(path)
|
||||
defer C.free(unsafe.Pointer(cpath))
|
||||
|
||||
copts := (*C.git_clone_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_clone_options{}))))
|
||||
populateCloneOptions(copts, options)
|
||||
defer freeCloneOptions(copts)
|
||||
var err error
|
||||
cOptions := populateCloneOptions(&C.git_clone_options{}, options, &err)
|
||||
defer freeCloneOptions(cOptions)
|
||||
|
||||
if len(options.CheckoutBranch) != 0 {
|
||||
copts.checkout_branch = C.CString(options.CheckoutBranch)
|
||||
cOptions.checkout_branch = C.CString(options.CheckoutBranch)
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var ptr *C.git_repository
|
||||
ret := C.git_clone(&ptr, curl, cpath, copts)
|
||||
ret := C.git_clone(&ptr, curl, cpath, cOptions)
|
||||
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
@ -50,60 +54,80 @@ func Clone(url string, path string, options *CloneOptions) (*Repository, error)
|
|||
}
|
||||
|
||||
//export remoteCreateCallback
|
||||
func remoteCreateCallback(cremote unsafe.Pointer, crepo unsafe.Pointer, cname, curl *C.char, payload unsafe.Pointer) C.int {
|
||||
func remoteCreateCallback(
|
||||
out **C.git_remote,
|
||||
crepo *C.git_repository,
|
||||
cname, curl *C.char,
|
||||
handle unsafe.Pointer,
|
||||
) C.int {
|
||||
name := C.GoString(cname)
|
||||
url := C.GoString(curl)
|
||||
repo := newRepositoryFromC((*C.git_repository)(crepo))
|
||||
// We don't own this repository, so make sure we don't try to free it
|
||||
runtime.SetFinalizer(repo, nil)
|
||||
repo := newRepositoryFromC(crepo)
|
||||
repo.weak = true
|
||||
defer repo.Free()
|
||||
|
||||
if opts, ok := pointerHandles.Get(payload).(CloneOptions); ok {
|
||||
remote, err := opts.RemoteCreateCallback(repo, name, url)
|
||||
// clear finalizer as the calling C function will
|
||||
// free the remote itself
|
||||
runtime.SetFinalizer(remote, nil)
|
||||
|
||||
if err == ErrOk && remote != nil {
|
||||
cptr := (**C.git_remote)(cremote)
|
||||
*cptr = remote.ptr
|
||||
} else if err == ErrOk && remote == nil {
|
||||
panic("no remote created by callback")
|
||||
}
|
||||
|
||||
return C.int(err)
|
||||
} else {
|
||||
data, ok := pointerHandles.Get(handle).(*cloneCallbackData)
|
||||
if !ok {
|
||||
panic("invalid remote create callback")
|
||||
}
|
||||
|
||||
remote, ret := data.options.RemoteCreateCallback(repo, name, url)
|
||||
if ret < 0 {
|
||||
*data.errorTarget = errors.New(ErrorCode(ret).String())
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
if remote == nil {
|
||||
panic("no remote created by callback")
|
||||
}
|
||||
|
||||
*out = remote.ptr
|
||||
|
||||
// clear finalizer as the calling C function will
|
||||
// free the remote itself
|
||||
runtime.SetFinalizer(remote, nil)
|
||||
remote.repo.Remotes.untrackRemote(remote)
|
||||
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions) {
|
||||
C.git_clone_init_options(ptr, C.GIT_CLONE_OPTIONS_VERSION)
|
||||
type cloneCallbackData struct {
|
||||
options *CloneOptions
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
func populateCloneOptions(copts *C.git_clone_options, opts *CloneOptions, errorTarget *error) *C.git_clone_options {
|
||||
C.git_clone_init_options(copts, C.GIT_CLONE_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
populateCheckoutOpts(&ptr.checkout_opts, opts.CheckoutOpts)
|
||||
populateFetchOptions(&ptr.fetch_opts, opts.FetchOptions)
|
||||
ptr.bare = cbool(opts.Bare)
|
||||
populateCheckoutOptions(&copts.checkout_opts, opts.CheckoutOpts, errorTarget)
|
||||
populateFetchOptions(&copts.fetch_opts, opts.FetchOptions, errorTarget)
|
||||
copts.bare = cbool(opts.Bare)
|
||||
|
||||
if opts.RemoteCreateCallback != nil {
|
||||
data := &cloneCallbackData{
|
||||
options: opts,
|
||||
errorTarget: errorTarget,
|
||||
}
|
||||
// Go v1.1 does not allow to assign a C function pointer
|
||||
C._go_git_populate_remote_cb(ptr)
|
||||
ptr.remote_cb_payload = pointerHandles.Track(*opts)
|
||||
C._go_git_populate_clone_callbacks(copts)
|
||||
copts.remote_cb_payload = pointerHandles.Track(data)
|
||||
}
|
||||
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeCloneOptions(ptr *C.git_clone_options) {
|
||||
if ptr == nil {
|
||||
func freeCloneOptions(copts *C.git_clone_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
|
||||
freeCheckoutOpts(&ptr.checkout_opts)
|
||||
freeCheckoutOptions(&copts.checkout_opts)
|
||||
freeFetchOptions(&copts.fetch_opts)
|
||||
|
||||
if ptr.remote_cb_payload != nil {
|
||||
pointerHandles.Untrack(ptr.remote_cb_payload)
|
||||
if copts.remote_cb_payload != nil {
|
||||
pointerHandles.Untrack(copts.remote_cb_payload)
|
||||
}
|
||||
|
||||
C.free(unsafe.Pointer(ptr.checkout_branch))
|
||||
C.free(unsafe.Pointer(ptr))
|
||||
C.free(unsafe.Pointer(copts.checkout_branch))
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package git
|
|||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -54,10 +55,10 @@ func TestCloneWithCallback(t *testing.T) {
|
|||
|
||||
remote, err := r.Remotes.Create(REMOTENAME, url)
|
||||
if err != nil {
|
||||
return nil, ErrGeneric
|
||||
return nil, ErrorCodeGeneric
|
||||
}
|
||||
|
||||
return remote, ErrOk
|
||||
return remote, ErrorCodeOK
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -74,4 +75,19 @@ func TestCloneWithCallback(t *testing.T) {
|
|||
if err != nil || remote == nil {
|
||||
t.Fatal("Remote was not created properly")
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
49
commit.go
49
commit.go
|
@ -12,8 +12,17 @@ import (
|
|||
"unsafe"
|
||||
)
|
||||
|
||||
// MessageEncoding is the encoding of commit messages.
|
||||
type MessageEncoding string
|
||||
|
||||
const (
|
||||
// MessageEncodingUTF8 is the default message encoding.
|
||||
MessageEncodingUTF8 MessageEncoding = "UTF-8"
|
||||
)
|
||||
|
||||
// Commit
|
||||
type Commit struct {
|
||||
doNotCompare
|
||||
Object
|
||||
cast_ptr *C.git_commit
|
||||
}
|
||||
|
@ -28,12 +37,52 @@ func (c *Commit) Message() string {
|
|||
return ret
|
||||
}
|
||||
|
||||
func (c *Commit) MessageEncoding() string {
|
||||
ret := C.GoString(C.git_commit_message_encoding(c.cast_ptr))
|
||||
runtime.KeepAlive(c)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c *Commit) RawMessage() string {
|
||||
ret := C.GoString(C.git_commit_message_raw(c.cast_ptr))
|
||||
runtime.KeepAlive(c)
|
||||
return ret
|
||||
}
|
||||
|
||||
// RawHeader gets the full raw text of the commit header.
|
||||
func (c *Commit) RawHeader() string {
|
||||
ret := C.GoString(C.git_commit_raw_header(c.cast_ptr))
|
||||
runtime.KeepAlive(c)
|
||||
return ret
|
||||
}
|
||||
|
||||
// ContentToSign returns the content that will be passed to a signing function for this commit
|
||||
func (c *Commit) ContentToSign() string {
|
||||
return c.RawHeader() + "\n" + c.RawMessage()
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
// WithSignatureUsing creates a new signed commit from this one using the given signing callback
|
||||
func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
|
||||
signature, signatureField, err := f(c.ContentToSign())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c.WithSignature(signature, signatureField)
|
||||
}
|
||||
|
||||
// WithSignature creates a new signed commit from the given signature and signature field
|
||||
func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, error) {
|
||||
return c.Owner().CreateCommitWithSignature(
|
||||
c.ContentToSign(),
|
||||
signature,
|
||||
signatureField,
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Commit) ExtractSignature() (string, string, error) {
|
||||
|
||||
var c_signed C.git_buf
|
||||
|
|
16
config.go
16
config.go
|
@ -52,6 +52,7 @@ func newConfigEntryFromC(centry *C.git_config_entry) *ConfigEntry {
|
|||
}
|
||||
|
||||
type Config struct {
|
||||
doNotCompare
|
||||
ptr *C.git_config
|
||||
}
|
||||
|
||||
|
@ -361,6 +362,7 @@ func OpenOndisk(path string) (*Config, error) {
|
|||
}
|
||||
|
||||
type ConfigIterator struct {
|
||||
doNotCompare
|
||||
ptr *C.git_config_iterator
|
||||
cfg *Config
|
||||
}
|
||||
|
@ -450,3 +452,17 @@ func ConfigFindProgramdata() (string, error) {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -107,3 +107,13 @@ func TestConfigLookups(t *testing.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()
|
||||
}
|
||||
|
|
107
credentials.go
107
credentials.go
|
@ -2,9 +2,18 @@ package git
|
|||
|
||||
/*
|
||||
#include <git2.h>
|
||||
|
||||
void _go_git_populate_credential_ssh_custom(git_cred_ssh_custom *cred);
|
||||
*/
|
||||
import "C"
|
||||
import "unsafe"
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type CredType uint
|
||||
|
||||
|
@ -16,6 +25,7 @@ const (
|
|||
)
|
||||
|
||||
type Cred struct {
|
||||
doNotCompare
|
||||
ptr *C.git_cred
|
||||
}
|
||||
|
||||
|
@ -31,7 +41,42 @@ func (o *Cred) Type() CredType {
|
|||
}
|
||||
|
||||
func credFromC(ptr *C.git_cred) *Cred {
|
||||
return &Cred{ptr}
|
||||
return &Cred{ptr: ptr}
|
||||
}
|
||||
|
||||
// GetUserpassPlaintext returns the plaintext username/password combination stored in the Cred.
|
||||
func (o *Cred) GetUserpassPlaintext() (username, password string, err error) {
|
||||
if o.Type() != CredTypeUserpassPlaintext {
|
||||
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 *Cred) GetSSHKey() (username, publickey, privatekey, passphrase string, err error) {
|
||||
if o.Type() != CredTypeSshKey {
|
||||
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 NewCredUsername(username string) (int, Cred) {
|
||||
cred := Cred{}
|
||||
cusername := C.CString(username)
|
||||
ret := C.git_cred_username_new(&cred.ptr, cusername)
|
||||
return int(ret), cred
|
||||
}
|
||||
|
||||
func NewCredUserpassPlaintext(username string, password string) (int, Cred) {
|
||||
|
@ -89,3 +134,61 @@ func NewCredDefault() (int, Cred) {
|
|||
ret := C.git_cred_default_new(&cred.ptr)
|
||||
return int(ret), cred
|
||||
}
|
||||
|
||||
type credentialSSHCustomData struct {
|
||||
signer ssh.Signer
|
||||
}
|
||||
|
||||
//export credentialSSHCustomFree
|
||||
func credentialSSHCustomFree(cred *C.git_cred_ssh_custom) {
|
||||
if cred == nil {
|
||||
return
|
||||
}
|
||||
|
||||
C.free(unsafe.Pointer(cred.username))
|
||||
C.free(unsafe.Pointer(cred.publickey))
|
||||
pointerHandles.Untrack(cred.payload)
|
||||
C.free(unsafe.Pointer(cred))
|
||||
}
|
||||
|
||||
//export credentialSSHSignCallback
|
||||
func credentialSSHSignCallback(
|
||||
errorMessage **C.char,
|
||||
sig **C.uchar,
|
||||
sig_len *C.size_t,
|
||||
data *C.uchar,
|
||||
data_len C.size_t,
|
||||
handle unsafe.Pointer,
|
||||
) C.int {
|
||||
signer := pointerHandles.Get(handle).(*credentialSSHCustomData).signer
|
||||
signature, err := signer.Sign(rand.Reader, C.GoBytes(unsafe.Pointer(data), C.int(data_len)))
|
||||
if err != nil {
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
*sig = (*C.uchar)(C.CBytes(signature.Blob))
|
||||
*sig_len = C.size_t(len(signature.Blob))
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
// NewCredentialSSHKeyFromSigner creates new SSH credentials using the provided signer.
|
||||
func NewCredentialSSHKeyFromSigner(username string, signer ssh.Signer) (*Cred, error) {
|
||||
publicKey := signer.PublicKey().Marshal()
|
||||
|
||||
ccred := (*C.git_cred_ssh_custom)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_cred_ssh_custom{}))))
|
||||
ccred.parent.credtype = C.GIT_CREDTYPE_SSH_CUSTOM
|
||||
ccred.username = C.CString(username)
|
||||
ccred.publickey = (*C.char)(C.CBytes(publicKey))
|
||||
ccred.publickey_len = C.size_t(len(publicKey))
|
||||
C._go_git_populate_credential_ssh_custom(ccred)
|
||||
|
||||
data := credentialSSHCustomData{
|
||||
signer: signer,
|
||||
}
|
||||
ccred.payload = pointerHandles.Track(&data)
|
||||
|
||||
cred := Cred{
|
||||
ptr: &ccred.parent,
|
||||
}
|
||||
|
||||
return &cred, nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
// Code generated by "stringer -type Delta -trimprefix Delta -tags static"; DO NOT EDIT.
|
||||
|
||||
package git
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[DeltaUnmodified-0]
|
||||
_ = x[DeltaAdded-1]
|
||||
_ = x[DeltaDeleted-2]
|
||||
_ = x[DeltaModified-3]
|
||||
_ = x[DeltaRenamed-4]
|
||||
_ = x[DeltaCopied-5]
|
||||
_ = x[DeltaIgnored-6]
|
||||
_ = x[DeltaUntracked-7]
|
||||
_ = x[DeltaTypeChange-8]
|
||||
_ = x[DeltaUnreadable-9]
|
||||
_ = x[DeltaConflicted-10]
|
||||
}
|
||||
|
||||
const _Delta_name = "UnmodifiedAddedDeletedModifiedRenamedCopiedIgnoredUntrackedTypeChangeUnreadableConflicted"
|
||||
|
||||
var _Delta_index = [...]uint8{0, 10, 15, 22, 30, 37, 43, 50, 59, 69, 79, 89}
|
||||
|
||||
func (i Delta) String() string {
|
||||
if i < 0 || i >= Delta(len(_Delta_index)-1) {
|
||||
return "Delta(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _Delta_name[_Delta_index[i]:_Delta_index[i+1]]
|
||||
}
|
|
@ -0,0 +1,202 @@
|
|||
package git
|
||||
|
||||
/*
|
||||
#include <git2.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// The constants, functions, and types in this files are slated for deprecation
|
||||
// in the next major version.
|
||||
|
||||
// blob.go
|
||||
|
||||
// Deprecated: BlobChunkCallback is not used.
|
||||
type BlobChunkCallback func(maxLen int) ([]byte, error)
|
||||
|
||||
// Deprecated: BlobCallbackData is not used.
|
||||
type BlobCallbackData struct {
|
||||
Callback BlobChunkCallback
|
||||
Error error
|
||||
}
|
||||
|
||||
// checkout.go
|
||||
|
||||
// Deprecated: CheckoutOpts is a deprecated alias of CheckoutOptions.
|
||||
type CheckoutOpts = CheckoutOptions
|
||||
|
||||
// diff.go
|
||||
|
||||
const (
|
||||
// Deprecated: DiffIgnoreWhitespaceEol is a deprecated alias of DiffIgnoreWhitespaceEOL.
|
||||
DiffIgnoreWitespaceEol = DiffIgnoreWhitespaceEOL
|
||||
)
|
||||
|
||||
// features.go
|
||||
|
||||
const (
|
||||
// Deprecated: FeatureHttps is a deprecated alias of FeatureHTTPS.
|
||||
FeatureHttps = FeatureHTTPS
|
||||
|
||||
// Deprecated: FeatureSsh is a deprecated alias of FeatureSSH.
|
||||
FeatureSsh = FeatureSSH
|
||||
)
|
||||
|
||||
// git.go
|
||||
|
||||
const (
|
||||
// Deprecated: ErrClassNone is a deprecated alias of ErrorClassNone.
|
||||
ErrClassNone = ErrorClassNone
|
||||
// Deprecated: ErrClassNoMemory is a deprecated alias of ErrorClassNoMemory.
|
||||
ErrClassNoMemory = ErrorClassNoMemory
|
||||
// Deprecated: ErrClassOs is a deprecated alias of ErrorClassOS.
|
||||
ErrClassOs = ErrorClassOS
|
||||
// Deprecated: ErrClassInvalid is a deprecated alias of ErrorClassInvalid.
|
||||
ErrClassInvalid = ErrorClassInvalid
|
||||
// Deprecated: ErrClassReference is a deprecated alias of ErrorClassReference.
|
||||
ErrClassReference = ErrorClassReference
|
||||
// Deprecated: ErrClassZlib is a deprecated alias of ErrorClassZlib.
|
||||
ErrClassZlib = ErrorClassZlib
|
||||
// Deprecated: ErrClassRepository is a deprecated alias of ErrorClassRepository.
|
||||
ErrClassRepository = ErrorClassRepository
|
||||
// Deprecated: ErrClassConfig is a deprecated alias of ErrorClassConfig.
|
||||
ErrClassConfig = ErrorClassConfig
|
||||
// Deprecated: ErrClassRegex is a deprecated alias of ErrorClassRegex.
|
||||
ErrClassRegex = ErrorClassRegex
|
||||
// Deprecated: ErrClassOdb is a deprecated alias of ErrorClassOdb.
|
||||
ErrClassOdb = ErrorClassOdb
|
||||
// Deprecated: ErrClassIndex is a deprecated alias of ErrorClassIndex.
|
||||
ErrClassIndex = ErrorClassIndex
|
||||
// Deprecated: ErrClassObject is a deprecated alias of ErrorClassObject.
|
||||
ErrClassObject = ErrorClassObject
|
||||
// Deprecated: ErrClassNet is a deprecated alias of ErrorClassNet.
|
||||
ErrClassNet = ErrorClassNet
|
||||
// Deprecated: ErrClassTag is a deprecated alias of ErrorClassTag.
|
||||
ErrClassTag = ErrorClassTag
|
||||
// Deprecated: ErrClassTree is a deprecated alias of ErrorClassTree.
|
||||
ErrClassTree = ErrorClassTree
|
||||
// Deprecated: ErrClassIndexer is a deprecated alias of ErrorClassIndexer.
|
||||
ErrClassIndexer = ErrorClassIndexer
|
||||
// Deprecated: ErrClassSSL is a deprecated alias of ErrorClassSSL.
|
||||
ErrClassSSL = ErrorClassSSL
|
||||
// Deprecated: ErrClassSubmodule is a deprecated alias of ErrorClassSubmodule.
|
||||
ErrClassSubmodule = ErrorClassSubmodule
|
||||
// Deprecated: ErrClassThread is a deprecated alias of ErrorClassThread.
|
||||
ErrClassThread = ErrorClassThread
|
||||
// Deprecated: ErrClassStash is a deprecated alias of ErrorClassStash.
|
||||
ErrClassStash = ErrorClassStash
|
||||
// Deprecated: ErrClassCheckout is a deprecated alias of ErrorClassCheckout.
|
||||
ErrClassCheckout = ErrorClassCheckout
|
||||
// Deprecated: ErrClassFetchHead is a deprecated alias of ErrorClassFetchHead.
|
||||
ErrClassFetchHead = ErrorClassFetchHead
|
||||
// Deprecated: ErrClassMerge is a deprecated alias of ErrorClassMerge.
|
||||
ErrClassMerge = ErrorClassMerge
|
||||
// Deprecated: ErrClassSsh is a deprecated alias of ErrorClassSSH.
|
||||
ErrClassSsh = ErrorClassSSH
|
||||
// Deprecated: ErrClassFilter is a deprecated alias of ErrorClassFilter.
|
||||
ErrClassFilter = ErrorClassFilter
|
||||
// Deprecated: ErrClassRevert is a deprecated alias of ErrorClassRevert.
|
||||
ErrClassRevert = ErrorClassRevert
|
||||
// Deprecated: ErrClassCallback is a deprecated alias of ErrorClassCallback.
|
||||
ErrClassCallback = ErrorClassCallback
|
||||
// Deprecated: ErrClassRebase is a deprecated alias of ErrorClassRebase.
|
||||
ErrClassRebase = ErrorClassRebase
|
||||
// Deprecated: ErrClassPatch is a deprecated alias of ErrorClassPatch.
|
||||
ErrClassPatch = ErrorClassPatch
|
||||
)
|
||||
|
||||
const (
|
||||
// Deprecated: ErrOk is a deprecated alias of ErrorCodeOK.
|
||||
ErrOk = ErrorCodeOK
|
||||
// Deprecated: ErrGeneric is a deprecated alias of ErrorCodeGeneric.
|
||||
ErrGeneric = ErrorCodeGeneric
|
||||
// Deprecated: ErrNotFound is a deprecated alias of ErrorCodeNotFound.
|
||||
ErrNotFound = ErrorCodeNotFound
|
||||
// Deprecated: ErrExists is a deprecated alias of ErrorCodeExists.
|
||||
ErrExists = ErrorCodeExists
|
||||
// Deprecated: ErrAmbiguous is a deprecated alias of ErrorCodeAmbiguous.
|
||||
ErrAmbiguous = ErrorCodeAmbiguous
|
||||
// Deprecated: ErrAmbigious is a deprecated alias of ErrorCodeAmbiguous.
|
||||
ErrAmbigious = ErrorCodeAmbiguous
|
||||
// Deprecated: ErrBuffs is a deprecated alias of ErrorCodeBuffs.
|
||||
ErrBuffs = ErrorCodeBuffs
|
||||
// Deprecated: ErrUser is a deprecated alias of ErrorCodeUser.
|
||||
ErrUser = ErrorCodeUser
|
||||
// Deprecated: ErrBareRepo is a deprecated alias of ErrorCodeBareRepo.
|
||||
ErrBareRepo = ErrorCodeBareRepo
|
||||
// Deprecated: ErrUnbornBranch is a deprecated alias of ErrorCodeUnbornBranch.
|
||||
ErrUnbornBranch = ErrorCodeUnbornBranch
|
||||
// Deprecated: ErrUnmerged is a deprecated alias of ErrorCodeUnmerged.
|
||||
ErrUnmerged = ErrorCodeUnmerged
|
||||
// Deprecated: ErrNonFastForward is a deprecated alias of ErrorCodeNonFastForward.
|
||||
ErrNonFastForward = ErrorCodeNonFastForward
|
||||
// Deprecated: ErrInvalidSpec is a deprecated alias of ErrorCodeInvalidSpec.
|
||||
ErrInvalidSpec = ErrorCodeInvalidSpec
|
||||
// Deprecated: ErrConflict is a deprecated alias of ErrorCodeConflict.
|
||||
ErrConflict = ErrorCodeConflict
|
||||
// Deprecated: ErrLocked is a deprecated alias of ErrorCodeLocked.
|
||||
ErrLocked = ErrorCodeLocked
|
||||
// Deprecated: ErrModified is a deprecated alias of ErrorCodeModified.
|
||||
ErrModified = ErrorCodeModified
|
||||
// Deprecated: ErrAuth is a deprecated alias of ErrorCodeAuth.
|
||||
ErrAuth = ErrorCodeAuth
|
||||
// Deprecated: ErrCertificate is a deprecated alias of ErrorCodeCertificate.
|
||||
ErrCertificate = ErrorCodeCertificate
|
||||
// Deprecated: ErrApplied is a deprecated alias of ErrorCodeApplied.
|
||||
ErrApplied = ErrorCodeApplied
|
||||
// Deprecated: ErrPeel is a deprecated alias of ErrorCodePeel.
|
||||
ErrPeel = ErrorCodePeel
|
||||
// Deprecated: ErrEOF is a deprecated alias of ErrorCodeEOF.
|
||||
ErrEOF = ErrorCodeEOF
|
||||
// Deprecated: ErrUncommitted is a deprecated alias of ErrorCodeUncommitted.
|
||||
ErrUncommitted = ErrorCodeUncommitted
|
||||
// Deprecated: ErrDirectory is a deprecated alias of ErrorCodeDirectory.
|
||||
ErrDirectory = ErrorCodeDirectory
|
||||
// Deprecated: ErrMergeConflict is a deprecated alias of ErrorCodeMergeConflict.
|
||||
ErrMergeConflict = ErrorCodeMergeConflict
|
||||
// Deprecated: ErrPassthrough is a deprecated alias of ErrorCodePassthrough.
|
||||
ErrPassthrough = ErrorCodePassthrough
|
||||
// Deprecated: ErrIterOver is a deprecated alias of ErrorCodeIterOver.
|
||||
ErrIterOver = ErrorCodeIterOver
|
||||
// Deprecated: ErrApplyFail is a deprecated alias of ErrorCodeApplyFail.
|
||||
ErrApplyFail = ErrorCodeApplyFail
|
||||
)
|
||||
|
||||
// index.go
|
||||
|
||||
// Deprecated: IndexAddOpts is a deprecated alias of IndexAddOption.
|
||||
type IndexAddOpts = IndexAddOption
|
||||
|
||||
// Deprecated: IndexStageOpts is a deprecated alias of IndexStageState.
|
||||
type IndexStageOpts = IndexStageState
|
||||
|
||||
// submodule.go
|
||||
|
||||
// Deprecated: SubmoduleCbk is a deprecated alias of SubmoduleCallback.
|
||||
type SubmoduleCbk = SubmoduleCallback
|
||||
|
||||
// Deprecated: SubmoduleVisitor is not used.
|
||||
func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int {
|
||||
sub := &Submodule{ptr: (*C.git_submodule)(csub)}
|
||||
|
||||
callback, ok := pointerHandles.Get(handle).(SubmoduleCallback)
|
||||
if !ok {
|
||||
panic("invalid submodule visitor callback")
|
||||
}
|
||||
return (C.int)(callback(sub, C.GoString(name)))
|
||||
}
|
||||
|
||||
// tree.go
|
||||
|
||||
// Deprecated: CallbackGitTreeWalk is not used.
|
||||
func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int {
|
||||
root := C.GoString(_root)
|
||||
|
||||
if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok {
|
||||
return C.int(callback(root, newTreeEntry(entry)))
|
||||
} else {
|
||||
panic("invalid treewalk callback")
|
||||
}
|
||||
}
|
|
@ -176,6 +176,7 @@ func (repo *Repository) DescribeWorkdir(opts *DescribeOptions) (*DescribeResult,
|
|||
//
|
||||
// Use Format() to get a string out of it.
|
||||
type DescribeResult struct {
|
||||
doNotCompare
|
||||
ptr *C.git_describe_result
|
||||
}
|
||||
|
||||
|
|
580
diff.go
580
diff.go
|
@ -3,6 +3,7 @@ package git
|
|||
/*
|
||||
#include <git2.h>
|
||||
|
||||
extern void _go_git_populate_apply_callbacks(git_apply_options *options);
|
||||
extern int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload);
|
||||
extern void _go_git_setup_diff_notify_callbacks(git_diff_options* opts);
|
||||
extern int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload);
|
||||
|
@ -14,7 +15,7 @@ import (
|
|||
"unsafe"
|
||||
)
|
||||
|
||||
type DiffFlag int
|
||||
type DiffFlag uint32
|
||||
|
||||
const (
|
||||
DiffFlagBinary DiffFlag = C.GIT_DIFF_FLAG_BINARY
|
||||
|
@ -39,6 +40,8 @@ const (
|
|||
DeltaConflicted Delta = C.GIT_DELTA_CONFLICTED
|
||||
)
|
||||
|
||||
//go:generate stringer -type Delta -trimprefix Delta -tags static
|
||||
|
||||
type DiffLineType int
|
||||
|
||||
const (
|
||||
|
@ -54,6 +57,8 @@ const (
|
|||
DiffLineBinary DiffLineType = C.GIT_DIFF_LINE_BINARY
|
||||
)
|
||||
|
||||
//go:generate stringer -type DiffLineType -trimprefix DiffLine -tags static
|
||||
|
||||
type DiffFile struct {
|
||||
Path string
|
||||
Oid *Oid
|
||||
|
@ -127,8 +132,10 @@ func diffLineFromC(line *C.git_diff_line) DiffLine {
|
|||
}
|
||||
|
||||
type Diff struct {
|
||||
ptr *C.git_diff
|
||||
repo *Repository
|
||||
doNotCompare
|
||||
ptr *C.git_diff
|
||||
repo *Repository
|
||||
runFinalizer bool
|
||||
}
|
||||
|
||||
func (diff *Diff) NumDeltas() (int, error) {
|
||||
|
@ -140,7 +147,7 @@ func (diff *Diff) NumDeltas() (int, error) {
|
|||
return ret, nil
|
||||
}
|
||||
|
||||
func (diff *Diff) GetDelta(index int) (DiffDelta, error) {
|
||||
func (diff *Diff) Delta(index int) (DiffDelta, error) {
|
||||
if diff.ptr == nil {
|
||||
return DiffDelta{}, ErrInvalid
|
||||
}
|
||||
|
@ -150,14 +157,20 @@ func (diff *Diff) GetDelta(index int) (DiffDelta, error) {
|
|||
return ret, nil
|
||||
}
|
||||
|
||||
// deprecated: You should use `Diff.Delta()` instead.
|
||||
func (diff *Diff) GetDelta(index int) (DiffDelta, error) {
|
||||
return diff.Delta(index)
|
||||
}
|
||||
|
||||
func newDiffFromC(ptr *C.git_diff, repo *Repository) *Diff {
|
||||
if ptr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
diff := &Diff{
|
||||
ptr: ptr,
|
||||
repo: repo,
|
||||
ptr: ptr,
|
||||
repo: repo,
|
||||
runFinalizer: true,
|
||||
}
|
||||
|
||||
runtime.SetFinalizer(diff, (*Diff).Free)
|
||||
|
@ -168,6 +181,11 @@ func (diff *Diff) Free() error {
|
|||
if diff.ptr == nil {
|
||||
return ErrInvalid
|
||||
}
|
||||
if !diff.runFinalizer {
|
||||
// This is the case with the Diff objects that are involved in the DiffNotifyCallback.
|
||||
diff.ptr = nil
|
||||
return nil
|
||||
}
|
||||
runtime.SetFinalizer(diff, nil)
|
||||
C.git_diff_free(diff.ptr)
|
||||
diff.ptr = nil
|
||||
|
@ -202,6 +220,7 @@ func (diff *Diff) FindSimilar(opts *DiffFindOptions) error {
|
|||
}
|
||||
|
||||
type DiffStats struct {
|
||||
doNotCompare
|
||||
ptr *C.git_diff_stats
|
||||
}
|
||||
|
||||
|
@ -277,14 +296,14 @@ func (diff *Diff) Stats() (*DiffStats, error) {
|
|||
return stats, nil
|
||||
}
|
||||
|
||||
type diffForEachData struct {
|
||||
FileCallback DiffForEachFileCallback
|
||||
HunkCallback DiffForEachHunkCallback
|
||||
LineCallback DiffForEachLineCallback
|
||||
Error error
|
||||
type diffForEachCallbackData struct {
|
||||
fileCallback DiffForEachFileCallback
|
||||
hunkCallback DiffForEachHunkCallback
|
||||
lineCallback DiffForEachLineCallback
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
type DiffForEachFileCallback func(DiffDelta, float64) (DiffForEachHunkCallback, error)
|
||||
type DiffForEachFileCallback func(delta DiffDelta, progress float64) (DiffForEachHunkCallback, error)
|
||||
|
||||
type DiffDetail int
|
||||
|
||||
|
@ -309,82 +328,91 @@ func (diff *Diff) ForEach(cbFile DiffForEachFileCallback, detail DiffDetail) err
|
|||
intLines = C.int(1)
|
||||
}
|
||||
|
||||
data := &diffForEachData{
|
||||
FileCallback: cbFile,
|
||||
var err error
|
||||
data := &diffForEachCallbackData{
|
||||
fileCallback: cbFile,
|
||||
errorTarget: &err,
|
||||
}
|
||||
|
||||
handle := pointerHandles.Track(data)
|
||||
defer pointerHandles.Untrack(handle)
|
||||
|
||||
ecode := C._go_git_diff_foreach(diff.ptr, 1, intHunks, intLines, handle)
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C._go_git_diff_foreach(diff.ptr, 1, intHunks, intLines, handle)
|
||||
runtime.KeepAlive(diff)
|
||||
if ecode < 0 {
|
||||
return data.Error
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//export diffForEachFileCb
|
||||
func diffForEachFileCb(delta *C.git_diff_delta, progress C.float, handle unsafe.Pointer) int {
|
||||
//export diffForEachFileCallback
|
||||
func diffForEachFileCallback(delta *C.git_diff_delta, progress C.float, handle unsafe.Pointer) C.int {
|
||||
payload := pointerHandles.Get(handle)
|
||||
data, ok := payload.(*diffForEachData)
|
||||
data, ok := payload.(*diffForEachCallbackData)
|
||||
if !ok {
|
||||
panic("could not retrieve data for handle")
|
||||
}
|
||||
|
||||
data.HunkCallback = nil
|
||||
if data.FileCallback != nil {
|
||||
cb, err := data.FileCallback(diffDeltaFromC(delta), float64(progress))
|
||||
data.hunkCallback = nil
|
||||
if data.fileCallback != nil {
|
||||
cb, err := data.fileCallback(diffDeltaFromC(delta), float64(progress))
|
||||
if err != nil {
|
||||
data.Error = err
|
||||
return -1
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
data.HunkCallback = cb
|
||||
data.hunkCallback = cb
|
||||
}
|
||||
|
||||
return 0
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
type DiffForEachHunkCallback func(DiffHunk) (DiffForEachLineCallback, error)
|
||||
|
||||
//export diffForEachHunkCb
|
||||
func diffForEachHunkCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, handle unsafe.Pointer) int {
|
||||
//export diffForEachHunkCallback
|
||||
func diffForEachHunkCallback(delta *C.git_diff_delta, hunk *C.git_diff_hunk, handle unsafe.Pointer) C.int {
|
||||
payload := pointerHandles.Get(handle)
|
||||
data, ok := payload.(*diffForEachData)
|
||||
data, ok := payload.(*diffForEachCallbackData)
|
||||
if !ok {
|
||||
panic("could not retrieve data for handle")
|
||||
}
|
||||
|
||||
data.LineCallback = nil
|
||||
if data.HunkCallback != nil {
|
||||
cb, err := data.HunkCallback(diffHunkFromC(hunk))
|
||||
data.lineCallback = nil
|
||||
if data.hunkCallback != nil {
|
||||
cb, err := data.hunkCallback(diffHunkFromC(hunk))
|
||||
if err != nil {
|
||||
data.Error = err
|
||||
return -1
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
data.LineCallback = cb
|
||||
data.lineCallback = cb
|
||||
}
|
||||
|
||||
return 0
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
type DiffForEachLineCallback func(DiffLine) error
|
||||
|
||||
//export diffForEachLineCb
|
||||
func diffForEachLineCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, line *C.git_diff_line, handle unsafe.Pointer) int {
|
||||
//export diffForEachLineCallback
|
||||
func diffForEachLineCallback(delta *C.git_diff_delta, hunk *C.git_diff_hunk, line *C.git_diff_line, handle unsafe.Pointer) C.int {
|
||||
payload := pointerHandles.Get(handle)
|
||||
data, ok := payload.(*diffForEachData)
|
||||
data, ok := payload.(*diffForEachCallbackData)
|
||||
if !ok {
|
||||
panic("could not retrieve data for handle")
|
||||
}
|
||||
|
||||
err := data.LineCallback(diffLineFromC(line))
|
||||
err := data.lineCallback(diffLineFromC(line))
|
||||
if err != nil {
|
||||
data.Error = err
|
||||
return -1
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
|
||||
return 0
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
func (diff *Diff) Patch(deltaIndex int) (*Patch, error) {
|
||||
|
@ -405,6 +433,36 @@ func (diff *Diff) Patch(deltaIndex int) (*Patch, error) {
|
|||
return newPatchFromC(patchPtr), nil
|
||||
}
|
||||
|
||||
type DiffFormat int
|
||||
|
||||
const (
|
||||
DiffFormatPatch DiffFormat = C.GIT_DIFF_FORMAT_PATCH
|
||||
DiffFormatPatchHeader DiffFormat = C.GIT_DIFF_FORMAT_PATCH_HEADER
|
||||
DiffFormatRaw DiffFormat = C.GIT_DIFF_FORMAT_RAW
|
||||
DiffFormatNameOnly DiffFormat = C.GIT_DIFF_FORMAT_NAME_ONLY
|
||||
DiffFormatNameStatus DiffFormat = C.GIT_DIFF_FORMAT_NAME_STATUS
|
||||
)
|
||||
|
||||
func (diff *Diff) ToBuf(format DiffFormat) ([]byte, error) {
|
||||
if diff.ptr == nil {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
|
||||
diffBuf := C.git_buf{}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_diff_to_buf(&diffBuf, diff.ptr, C.git_diff_format_t(format))
|
||||
runtime.KeepAlive(diff)
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
}
|
||||
defer C.git_buf_dispose(&diffBuf)
|
||||
|
||||
return C.GoBytes(unsafe.Pointer(diffBuf.ptr), C.int(diffBuf.size)), nil
|
||||
}
|
||||
|
||||
type DiffOptionsFlag int
|
||||
|
||||
const (
|
||||
|
@ -431,7 +489,7 @@ const (
|
|||
|
||||
DiffIgnoreWhitespace DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE
|
||||
DiffIgnoreWhitespaceChange DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_CHANGE
|
||||
DiffIgnoreWitespaceEol DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_EOL
|
||||
DiffIgnoreWhitespaceEOL DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_EOL
|
||||
|
||||
DiffShowUntrackedContent DiffOptionsFlag = C.GIT_DIFF_SHOW_UNTRACKED_CONTENT
|
||||
DiffShowUnmodified DiffOptionsFlag = C.GIT_DIFF_SHOW_UNMODIFIED
|
||||
|
@ -504,7 +562,7 @@ const (
|
|||
DiffFindRemoveUnmodified DiffFindOptionsFlag = C.GIT_DIFF_FIND_REMOVE_UNMODIFIED
|
||||
)
|
||||
|
||||
//TODO implement git_diff_similarity_metric
|
||||
// TODO implement git_diff_similarity_metric
|
||||
type DiffFindOptions struct {
|
||||
Flags DiffFindOptionsFlag
|
||||
RenameThreshold uint16
|
||||
|
@ -539,83 +597,92 @@ var (
|
|||
ErrDeltaSkip = errors.New("Skip delta")
|
||||
)
|
||||
|
||||
type diffNotifyData struct {
|
||||
Callback DiffNotifyCallback
|
||||
Diff *Diff
|
||||
Error error
|
||||
type diffNotifyCallbackData struct {
|
||||
callback DiffNotifyCallback
|
||||
repository *Repository
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
//export diffNotifyCb
|
||||
func diffNotifyCb(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, matched_pathspec *C.char, handle unsafe.Pointer) int {
|
||||
//export diffNotifyCallback
|
||||
func diffNotifyCallback(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, matched_pathspec *C.char, handle unsafe.Pointer) C.int {
|
||||
diff_so_far := (*C.git_diff)(_diff_so_far)
|
||||
|
||||
payload := pointerHandles.Get(handle)
|
||||
data, ok := payload.(*diffNotifyData)
|
||||
data, ok := payload.(*diffNotifyCallbackData)
|
||||
if !ok {
|
||||
panic("could not retrieve data for handle")
|
||||
}
|
||||
|
||||
if data != nil {
|
||||
if data.Diff == nil {
|
||||
data.Diff = newDiffFromC(diff_so_far, nil)
|
||||
}
|
||||
|
||||
err := data.Callback(data.Diff, diffDeltaFromC(delta_to_add), C.GoString(matched_pathspec))
|
||||
|
||||
if err == ErrDeltaSkip {
|
||||
return 1
|
||||
} else if err != nil {
|
||||
data.Error = err
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
if data == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
return 0
|
||||
|
||||
// We are not taking ownership of this diff pointer, so no finalizer is set.
|
||||
diff := &Diff{
|
||||
ptr: diff_so_far,
|
||||
repo: data.repository,
|
||||
runFinalizer: false,
|
||||
}
|
||||
|
||||
err := data.callback(diff, diffDeltaFromC(delta_to_add), C.GoString(matched_pathspec))
|
||||
|
||||
// Since the callback could theoretically keep a reference to the diff
|
||||
// (which could be freed by libgit2 if an error occurs later during the
|
||||
// diffing process), this converts a use-after-free (terrible!) into a nil
|
||||
// dereference ("just" pretty bad).
|
||||
diff.ptr = nil
|
||||
|
||||
if err == ErrDeltaSkip {
|
||||
return 1
|
||||
}
|
||||
if err != nil {
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
func diffOptionsToC(opts *DiffOptions) (copts *C.git_diff_options, notifyData *diffNotifyData) {
|
||||
cpathspec := C.git_strarray{}
|
||||
if opts != nil {
|
||||
notifyData = &diffNotifyData{
|
||||
Callback: opts.NotifyCallback,
|
||||
}
|
||||
|
||||
if opts.Pathspec != nil {
|
||||
cpathspec.count = C.size_t(len(opts.Pathspec))
|
||||
cpathspec.strings = makeCStringsFromStrings(opts.Pathspec)
|
||||
}
|
||||
|
||||
copts = &C.git_diff_options{
|
||||
version: C.GIT_DIFF_OPTIONS_VERSION,
|
||||
flags: C.uint32_t(opts.Flags),
|
||||
ignore_submodules: C.git_submodule_ignore_t(opts.IgnoreSubmodules),
|
||||
pathspec: cpathspec,
|
||||
context_lines: C.uint32_t(opts.ContextLines),
|
||||
interhunk_lines: C.uint32_t(opts.InterhunkLines),
|
||||
id_abbrev: C.uint16_t(opts.IdAbbrev),
|
||||
max_size: C.git_off_t(opts.MaxSize),
|
||||
old_prefix: C.CString(opts.OldPrefix),
|
||||
new_prefix: C.CString(opts.NewPrefix),
|
||||
}
|
||||
|
||||
if opts.NotifyCallback != nil {
|
||||
C._go_git_setup_diff_notify_callbacks(copts)
|
||||
copts.payload = pointerHandles.Track(notifyData)
|
||||
}
|
||||
func populateDiffOptions(copts *C.git_diff_options, opts *DiffOptions, repo *Repository, errorTarget *error) *C.git_diff_options {
|
||||
C.git_diff_init_options(copts, C.GIT_DIFF_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
return
|
||||
|
||||
copts.flags = C.uint32_t(opts.Flags)
|
||||
copts.ignore_submodules = C.git_submodule_ignore_t(opts.IgnoreSubmodules)
|
||||
if len(opts.Pathspec) > 0 {
|
||||
copts.pathspec.count = C.size_t(len(opts.Pathspec))
|
||||
copts.pathspec.strings = makeCStringsFromStrings(opts.Pathspec)
|
||||
}
|
||||
copts.context_lines = C.uint32_t(opts.ContextLines)
|
||||
copts.interhunk_lines = C.uint32_t(opts.InterhunkLines)
|
||||
copts.id_abbrev = C.uint16_t(opts.IdAbbrev)
|
||||
copts.max_size = C.git_off_t(opts.MaxSize)
|
||||
copts.old_prefix = C.CString(opts.OldPrefix)
|
||||
copts.new_prefix = C.CString(opts.NewPrefix)
|
||||
|
||||
if opts.NotifyCallback != nil {
|
||||
notifyData := &diffNotifyCallbackData{
|
||||
callback: opts.NotifyCallback,
|
||||
repository: repo,
|
||||
errorTarget: errorTarget,
|
||||
}
|
||||
C._go_git_setup_diff_notify_callbacks(copts)
|
||||
copts.payload = pointerHandles.Track(notifyData)
|
||||
}
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeDiffOptions(copts *C.git_diff_options) {
|
||||
if copts != nil {
|
||||
cpathspec := copts.pathspec
|
||||
freeStrarray(&cpathspec)
|
||||
C.free(unsafe.Pointer(copts.old_prefix))
|
||||
C.free(unsafe.Pointer(copts.new_prefix))
|
||||
if copts.payload != nil {
|
||||
pointerHandles.Untrack(copts.payload)
|
||||
}
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
freeStrarray(&copts.pathspec)
|
||||
C.free(unsafe.Pointer(copts.old_prefix))
|
||||
C.free(unsafe.Pointer(copts.new_prefix))
|
||||
if copts.payload != nil {
|
||||
pointerHandles.Untrack(copts.payload)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -631,21 +698,21 @@ func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) (
|
|||
newPtr = newTree.cast_ptr
|
||||
}
|
||||
|
||||
copts, notifyData := diffOptionsToC(opts)
|
||||
var err error
|
||||
copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
|
||||
defer freeDiffOptions(copts)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_diff_tree_to_tree(&diffPtr, v.ptr, oldPtr, newPtr, copts)
|
||||
ret := C.git_diff_tree_to_tree(&diffPtr, v.ptr, oldPtr, newPtr, copts)
|
||||
runtime.KeepAlive(oldTree)
|
||||
runtime.KeepAlive(newTree)
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if notifyData != nil && notifyData.Diff != nil {
|
||||
return notifyData.Diff, nil
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return newDiffFromC(diffPtr, v), nil
|
||||
}
|
||||
|
@ -658,21 +725,22 @@ func (v *Repository) DiffTreeToWorkdir(oldTree *Tree, opts *DiffOptions) (*Diff,
|
|||
oldPtr = oldTree.cast_ptr
|
||||
}
|
||||
|
||||
copts, notifyData := diffOptionsToC(opts)
|
||||
var err error
|
||||
copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
|
||||
defer freeDiffOptions(copts)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_diff_tree_to_workdir(&diffPtr, v.ptr, oldPtr, copts)
|
||||
ret := C.git_diff_tree_to_workdir(&diffPtr, v.ptr, oldPtr, copts)
|
||||
runtime.KeepAlive(oldTree)
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
if notifyData != nil && notifyData.Diff != nil {
|
||||
return notifyData.Diff, nil
|
||||
}
|
||||
return newDiffFromC(diffPtr, v), nil
|
||||
}
|
||||
|
||||
|
@ -689,22 +757,23 @@ func (v *Repository) DiffTreeToIndex(oldTree *Tree, index *Index, opts *DiffOpti
|
|||
indexPtr = index.ptr
|
||||
}
|
||||
|
||||
copts, notifyData := diffOptionsToC(opts)
|
||||
var err error
|
||||
copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
|
||||
defer freeDiffOptions(copts)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_diff_tree_to_index(&diffPtr, v.ptr, oldPtr, indexPtr, copts)
|
||||
ret := C.git_diff_tree_to_index(&diffPtr, v.ptr, oldPtr, indexPtr, copts)
|
||||
runtime.KeepAlive(oldTree)
|
||||
runtime.KeepAlive(index)
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
if notifyData != nil && notifyData.Diff != nil {
|
||||
return notifyData.Diff, nil
|
||||
}
|
||||
return newDiffFromC(diffPtr, v), nil
|
||||
}
|
||||
|
||||
|
@ -716,21 +785,22 @@ func (v *Repository) DiffTreeToWorkdirWithIndex(oldTree *Tree, opts *DiffOptions
|
|||
oldPtr = oldTree.cast_ptr
|
||||
}
|
||||
|
||||
copts, notifyData := diffOptionsToC(opts)
|
||||
var err error
|
||||
copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
|
||||
defer freeDiffOptions(copts)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_diff_tree_to_workdir_with_index(&diffPtr, v.ptr, oldPtr, copts)
|
||||
ret := C.git_diff_tree_to_workdir_with_index(&diffPtr, v.ptr, oldPtr, copts)
|
||||
runtime.KeepAlive(oldTree)
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
if notifyData != nil && notifyData.Diff != nil {
|
||||
return notifyData.Diff, nil
|
||||
}
|
||||
return newDiffFromC(diffPtr, v), nil
|
||||
}
|
||||
|
||||
|
@ -742,29 +812,32 @@ func (v *Repository) DiffIndexToWorkdir(index *Index, opts *DiffOptions) (*Diff,
|
|||
indexPtr = index.ptr
|
||||
}
|
||||
|
||||
copts, notifyData := diffOptionsToC(opts)
|
||||
var err error
|
||||
copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
|
||||
defer freeDiffOptions(copts)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_diff_index_to_workdir(&diffPtr, v.ptr, indexPtr, copts)
|
||||
ret := C.git_diff_index_to_workdir(&diffPtr, v.ptr, indexPtr, copts)
|
||||
runtime.KeepAlive(index)
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
if notifyData != nil && notifyData.Diff != nil {
|
||||
return notifyData.Diff, nil
|
||||
}
|
||||
return newDiffFromC(diffPtr, v), nil
|
||||
}
|
||||
|
||||
// DiffBlobs performs a diff between two arbitrary blobs. You can pass
|
||||
// whatever file names you'd like for them to appear as in the diff.
|
||||
func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, opts *DiffOptions, fileCallback DiffForEachFileCallback, detail DiffDetail) error {
|
||||
data := &diffForEachData{
|
||||
FileCallback: fileCallback,
|
||||
var err error
|
||||
data := &diffForEachCallbackData{
|
||||
fileCallback: fileCallback,
|
||||
errorTarget: &err,
|
||||
}
|
||||
|
||||
intHunks := C.int(0)
|
||||
|
@ -780,12 +853,15 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string,
|
|||
handle := pointerHandles.Track(data)
|
||||
defer pointerHandles.Untrack(handle)
|
||||
|
||||
var repo *Repository
|
||||
var oldBlobPtr, newBlobPtr *C.git_blob
|
||||
if oldBlob != nil {
|
||||
oldBlobPtr = oldBlob.cast_ptr
|
||||
repo = oldBlob.repo
|
||||
}
|
||||
if newBlob != nil {
|
||||
newBlobPtr = newBlob.cast_ptr
|
||||
repo = newBlob.repo
|
||||
}
|
||||
|
||||
oldBlobPath := C.CString(oldAsPath)
|
||||
|
@ -793,18 +869,226 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string,
|
|||
newBlobPath := C.CString(newAsPath)
|
||||
defer C.free(unsafe.Pointer(newBlobPath))
|
||||
|
||||
copts, _ := diffOptionsToC(opts)
|
||||
copts := populateDiffOptions(&C.git_diff_options{}, opts, repo, &err)
|
||||
defer freeDiffOptions(copts)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C._go_git_diff_blobs(oldBlobPtr, oldBlobPath, newBlobPtr, newBlobPath, copts, 1, intHunks, intLines, handle)
|
||||
ret := C._go_git_diff_blobs(oldBlobPtr, oldBlobPath, newBlobPtr, newBlobPath, copts, 1, intHunks, intLines, handle)
|
||||
runtime.KeepAlive(oldBlob)
|
||||
runtime.KeepAlive(newBlob)
|
||||
if ecode < 0 {
|
||||
return MakeGitError(ecode)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ApplyHunkCallback is a callback that will be made per delta (file) when applying a patch.
|
||||
type ApplyHunkCallback func(*DiffHunk) (apply bool, err error)
|
||||
|
||||
// ApplyDeltaCallback is a callback that will be made per hunk when applying a patch.
|
||||
type ApplyDeltaCallback func(*DiffDelta) (apply bool, err error)
|
||||
|
||||
// ApplyOptions has 2 callbacks that are called for hunks or deltas
|
||||
// If these functions return an error, abort the apply process immediately.
|
||||
// If the first return value is true, the delta/hunk will be applied. If it is false, the delta/hunk will not be applied. In either case, the rest of the apply process will continue.
|
||||
type ApplyOptions struct {
|
||||
ApplyHunkCallback ApplyHunkCallback
|
||||
ApplyDeltaCallback ApplyDeltaCallback
|
||||
}
|
||||
|
||||
type applyCallbackData struct {
|
||||
options *ApplyOptions
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
//export hunkApplyCallback
|
||||
func hunkApplyCallback(_hunk *C.git_diff_hunk, _payload unsafe.Pointer) C.int {
|
||||
data, ok := pointerHandles.Get(_payload).(*applyCallbackData)
|
||||
if !ok {
|
||||
panic("invalid apply options payload")
|
||||
}
|
||||
|
||||
if data.options.ApplyHunkCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
hunk := diffHunkFromC(_hunk)
|
||||
|
||||
apply, err := data.options.ApplyHunkCallback(&hunk)
|
||||
if err != nil {
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
|
||||
if !apply {
|
||||
return 1
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export deltaApplyCallback
|
||||
func deltaApplyCallback(_delta *C.git_diff_delta, _payload unsafe.Pointer) C.int {
|
||||
data, ok := pointerHandles.Get(_payload).(*applyCallbackData)
|
||||
if !ok {
|
||||
panic("invalid apply options payload")
|
||||
}
|
||||
|
||||
if data.options.ApplyDeltaCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
delta := diffDeltaFromC(_delta)
|
||||
|
||||
apply, err := data.options.ApplyDeltaCallback(&delta)
|
||||
if err != nil {
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
|
||||
if !apply {
|
||||
return 1
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
// DefaultApplyOptions returns default options for applying diffs or patches.
|
||||
func DefaultApplyOptions() (*ApplyOptions, error) {
|
||||
opts := C.git_apply_options{
|
||||
version: C.GIT_APPLY_OPTIONS_VERSION,
|
||||
}
|
||||
|
||||
return applyOptionsFromC(&opts), nil
|
||||
}
|
||||
|
||||
func populateApplyOptions(copts *C.git_apply_options, opts *ApplyOptions, errorTarget *error) *C.git_apply_options {
|
||||
*copts = C.git_apply_options{
|
||||
version: C.GIT_APPLY_OPTIONS_VERSION,
|
||||
}
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if opts.ApplyDeltaCallback != nil || opts.ApplyHunkCallback != nil {
|
||||
data := &applyCallbackData{
|
||||
options: opts,
|
||||
errorTarget: errorTarget,
|
||||
}
|
||||
C._go_git_populate_apply_callbacks(copts)
|
||||
copts.payload = pointerHandles.Track(data)
|
||||
}
|
||||
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeApplyOptions(copts *C.git_apply_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
if copts.payload != nil {
|
||||
pointerHandles.Untrack(copts.payload)
|
||||
}
|
||||
}
|
||||
|
||||
func applyOptionsFromC(copts *C.git_apply_options) *ApplyOptions {
|
||||
return &ApplyOptions{}
|
||||
}
|
||||
|
||||
// ApplyLocation represents the possible application locations for applying
|
||||
// diffs.
|
||||
type ApplyLocation int
|
||||
|
||||
const (
|
||||
// ApplyLocationWorkdir applies the patch to the workdir, leaving the
|
||||
// index untouched. This is the equivalent of `git apply` with no location
|
||||
// argument.
|
||||
ApplyLocationWorkdir ApplyLocation = C.GIT_APPLY_LOCATION_WORKDIR
|
||||
// ApplyLocationIndex applies the patch to the index, leaving the working
|
||||
// directory untouched. This is the equivalent of `git apply --cached`.
|
||||
ApplyLocationIndex ApplyLocation = C.GIT_APPLY_LOCATION_INDEX
|
||||
// ApplyLocationBoth applies the patch to both the working directory and
|
||||
// the index. This is the equivalent of `git apply --index`.
|
||||
ApplyLocationBoth ApplyLocation = C.GIT_APPLY_LOCATION_BOTH
|
||||
)
|
||||
|
||||
// ApplyDiff appllies a Diff to the given repository, making changes directly
|
||||
// in the working directory, the index, or both.
|
||||
func (v *Repository) ApplyDiff(diff *Diff, location ApplyLocation, opts *ApplyOptions) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var err error
|
||||
cOpts := populateApplyOptions(&C.git_apply_options{}, opts, &err)
|
||||
defer freeApplyOptions(cOpts)
|
||||
|
||||
ret := C.git_apply(v.ptr, diff.ptr, C.git_apply_location_t(location), cOpts)
|
||||
runtime.KeepAlive(v)
|
||||
runtime.KeepAlive(diff)
|
||||
runtime.KeepAlive(cOpts)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ApplyToTree applies a Diff to a Tree and returns the resulting image as an Index.
|
||||
func (v *Repository) ApplyToTree(diff *Diff, tree *Tree, opts *ApplyOptions) (*Index, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var err error
|
||||
cOpts := populateApplyOptions(&C.git_apply_options{}, opts, &err)
|
||||
defer freeApplyOptions(cOpts)
|
||||
|
||||
var indexPtr *C.git_index
|
||||
ret := C.git_apply_to_tree(&indexPtr, v.ptr, tree.cast_ptr, diff.ptr, cOpts)
|
||||
runtime.KeepAlive(diff)
|
||||
runtime.KeepAlive(tree)
|
||||
runtime.KeepAlive(cOpts)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
return newIndexFromC(indexPtr, v), nil
|
||||
}
|
||||
|
||||
// DiffFromBuffer reads the contents of a git patch file into a Diff object.
|
||||
//
|
||||
// The diff object produced is similar to the one that would be produced if you
|
||||
// actually produced it computationally by comparing two trees, however there
|
||||
// may be subtle differences. For example, a patch file likely contains
|
||||
// abbreviated object IDs, so the object IDs in a git_diff_delta produced by
|
||||
// this function will also be abbreviated.
|
||||
//
|
||||
// This function will only read patch files created by a git implementation, it
|
||||
// will not read unified diffs produced by the diff program, nor any other
|
||||
// types of patch files.
|
||||
func DiffFromBuffer(buffer []byte, repo *Repository) (*Diff, error) {
|
||||
var diff *C.git_diff
|
||||
|
||||
cBuffer := C.CBytes(buffer)
|
||||
defer C.free(unsafe.Pointer(cBuffer))
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_diff_from_buffer(&diff, (*C.char)(cBuffer), C.size_t(len(buffer)))
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
}
|
||||
runtime.KeepAlive(diff)
|
||||
|
||||
return newDiffFromC(diff, repo), nil
|
||||
}
|
||||
|
|
451
diff_test.go
451
diff_test.go
|
@ -2,6 +2,10 @@ package git
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -169,9 +173,8 @@ func TestDiffTreeToTree(t *testing.T) {
|
|||
}, DiffDetailLines)
|
||||
|
||||
if err != errTest {
|
||||
t.Fatal("Expected custom error to be returned")
|
||||
t.Fatalf("Expected custom error to be returned, got %v, want %v", err, errTest)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func createTestTrees(t *testing.T, repo *Repository) (originalTree *Tree, newTree *Tree) {
|
||||
|
@ -236,3 +239,447 @@ func TestDiffBlobs(t *testing.T) {
|
|||
t.Fatalf("Bad number of lines iterated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyDiffAddfile(t *testing.T) {
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
addFirstFileCommit, addFirstFileTree := addAndGetTree(t, repo, "file1", `hello`)
|
||||
defer addFirstFileCommit.Free()
|
||||
defer addFirstFileTree.Free()
|
||||
addSecondFileCommit, addSecondFileTree := addAndGetTree(t, repo, "file2", `hello2`)
|
||||
defer addSecondFileCommit.Free()
|
||||
defer addSecondFileTree.Free()
|
||||
|
||||
diff, err := repo.DiffTreeToTree(addFirstFileTree, addSecondFileTree, nil)
|
||||
checkFatal(t, err)
|
||||
defer diff.Free()
|
||||
|
||||
t.Run("check does not apply to current tree because file exists", func(t *testing.T) {
|
||||
err = repo.ResetToCommit(addSecondFileCommit, ResetHard, &CheckoutOptions{})
|
||||
checkFatal(t, err)
|
||||
|
||||
err = repo.ApplyDiff(diff, ApplyLocationBoth, nil)
|
||||
if err == nil {
|
||||
t.Error("expecting applying patch to current repo to fail")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("check apply to correct commit", func(t *testing.T) {
|
||||
err = repo.ResetToCommit(addFirstFileCommit, ResetHard, &CheckoutOptions{})
|
||||
checkFatal(t, err)
|
||||
|
||||
err = repo.ApplyDiff(diff, ApplyLocationBoth, nil)
|
||||
checkFatal(t, err)
|
||||
|
||||
t.Run("Check that diff only changed one file", func(t *testing.T) {
|
||||
checkSecondFileStaged(t, repo)
|
||||
|
||||
index, err := repo.Index()
|
||||
checkFatal(t, err)
|
||||
defer index.Free()
|
||||
|
||||
newTreeOID, err := index.WriteTreeTo(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
newTree, err := repo.LookupTree(newTreeOID)
|
||||
checkFatal(t, err)
|
||||
defer newTree.Free()
|
||||
|
||||
_, err = repo.CreateCommit("HEAD", signature(), signature(), fmt.Sprintf("patch apply"), newTree, addFirstFileCommit)
|
||||
checkFatal(t, err)
|
||||
})
|
||||
|
||||
t.Run("test applying patch produced the same diff", func(t *testing.T) {
|
||||
head, err := repo.Head()
|
||||
checkFatal(t, err)
|
||||
|
||||
commit, err := repo.LookupCommit(head.Target())
|
||||
checkFatal(t, err)
|
||||
defer commit.Free()
|
||||
|
||||
tree, err := commit.Tree()
|
||||
checkFatal(t, err)
|
||||
defer tree.Free()
|
||||
|
||||
newDiff, err := repo.DiffTreeToTree(addFirstFileTree, tree, nil)
|
||||
checkFatal(t, err)
|
||||
defer newDiff.Free()
|
||||
|
||||
raw1b, err := diff.ToBuf(DiffFormatPatch)
|
||||
checkFatal(t, err)
|
||||
raw2b, err := newDiff.ToBuf(DiffFormatPatch)
|
||||
checkFatal(t, err)
|
||||
|
||||
raw1 := string(raw1b)
|
||||
raw2 := string(raw2b)
|
||||
|
||||
if raw1 != raw2 {
|
||||
t.Error("diffs should be the same")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("check convert to raw buffer and apply", func(t *testing.T) {
|
||||
err = repo.ResetToCommit(addFirstFileCommit, ResetHard, &CheckoutOptions{})
|
||||
checkFatal(t, err)
|
||||
|
||||
raw, err := diff.ToBuf(DiffFormatPatch)
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(raw) == 0 {
|
||||
t.Error("empty diff created")
|
||||
}
|
||||
|
||||
diff2, err := DiffFromBuffer(raw, repo)
|
||||
checkFatal(t, err)
|
||||
defer diff2.Free()
|
||||
|
||||
err = repo.ApplyDiff(diff2, ApplyLocationBoth, nil)
|
||||
checkFatal(t, err)
|
||||
})
|
||||
|
||||
t.Run("check apply callbacks work", func(t *testing.T) {
|
||||
// reset the state and get new default options for test
|
||||
resetAndGetOpts := func(t *testing.T) *ApplyOptions {
|
||||
err = repo.ResetToCommit(addFirstFileCommit, ResetHard, &CheckoutOptions{})
|
||||
checkFatal(t, err)
|
||||
|
||||
opts, err := DefaultApplyOptions()
|
||||
checkFatal(t, err)
|
||||
|
||||
return opts
|
||||
}
|
||||
|
||||
t.Run("Check hunk callback working applies patch", func(t *testing.T) {
|
||||
opts := resetAndGetOpts(t)
|
||||
|
||||
called := false
|
||||
opts.ApplyHunkCallback = func(hunk *DiffHunk) (apply bool, err error) {
|
||||
called = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
||||
checkFatal(t, err)
|
||||
|
||||
if called == false {
|
||||
t.Error("apply hunk callback was not called")
|
||||
}
|
||||
|
||||
checkSecondFileStaged(t, repo)
|
||||
})
|
||||
|
||||
t.Run("Check delta callback working applies patch", func(t *testing.T) {
|
||||
opts := resetAndGetOpts(t)
|
||||
|
||||
called := false
|
||||
opts.ApplyDeltaCallback = func(hunk *DiffDelta) (apply bool, err error) {
|
||||
if hunk.NewFile.Path != "file2" {
|
||||
t.Error("Unexpected delta in diff application")
|
||||
}
|
||||
called = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
||||
checkFatal(t, err)
|
||||
|
||||
if called == false {
|
||||
t.Error("apply hunk callback was not called")
|
||||
}
|
||||
|
||||
checkSecondFileStaged(t, repo)
|
||||
})
|
||||
|
||||
t.Run("Check delta callback returning false does not apply patch", func(t *testing.T) {
|
||||
opts := resetAndGetOpts(t)
|
||||
|
||||
called := false
|
||||
opts.ApplyDeltaCallback = func(hunk *DiffDelta) (apply bool, err error) {
|
||||
if hunk.NewFile.Path != "file2" {
|
||||
t.Error("Unexpected hunk in diff application")
|
||||
}
|
||||
called = true
|
||||
return false, nil
|
||||
}
|
||||
|
||||
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
||||
checkFatal(t, err)
|
||||
|
||||
if called == false {
|
||||
t.Error("apply hunk callback was not called")
|
||||
}
|
||||
|
||||
checkNoFilesStaged(t, repo)
|
||||
})
|
||||
|
||||
t.Run("Check hunk callback returning causes application to fail", func(t *testing.T) {
|
||||
opts := resetAndGetOpts(t)
|
||||
|
||||
called := false
|
||||
opts.ApplyHunkCallback = func(hunk *DiffHunk) (apply bool, err error) {
|
||||
called = true
|
||||
return false, errors.New("something happened")
|
||||
}
|
||||
|
||||
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
||||
if err == nil {
|
||||
t.Error("expected an error after trying to apply")
|
||||
}
|
||||
|
||||
if called == false {
|
||||
t.Error("apply hunk callback was not called")
|
||||
}
|
||||
|
||||
checkNoFilesStaged(t, repo)
|
||||
})
|
||||
|
||||
t.Run("Check delta callback returning causes application to fail", func(t *testing.T) {
|
||||
opts := resetAndGetOpts(t)
|
||||
|
||||
called := false
|
||||
opts.ApplyDeltaCallback = func(hunk *DiffDelta) (apply bool, err error) {
|
||||
if hunk.NewFile.Path != "file2" {
|
||||
t.Error("Unexpected delta in diff application")
|
||||
}
|
||||
called = true
|
||||
return false, errors.New("something happened")
|
||||
}
|
||||
|
||||
err = repo.ApplyDiff(diff, ApplyLocationBoth, opts)
|
||||
if err == nil {
|
||||
t.Error("expected an error after trying to apply")
|
||||
}
|
||||
|
||||
if called == false {
|
||||
t.Error("apply hunk callback was not called")
|
||||
}
|
||||
|
||||
checkNoFilesStaged(t, repo)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestApplyToTree(t *testing.T) {
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
commitA, treeA := addAndGetTree(t, repo, "file", "a")
|
||||
defer commitA.Free()
|
||||
defer treeA.Free()
|
||||
commitB, treeB := addAndGetTree(t, repo, "file", "b")
|
||||
defer commitB.Free()
|
||||
defer treeB.Free()
|
||||
commitC, treeC := addAndGetTree(t, repo, "file", "c")
|
||||
defer commitC.Free()
|
||||
defer treeC.Free()
|
||||
|
||||
diffAB, err := repo.DiffTreeToTree(treeA, treeB, nil)
|
||||
checkFatal(t, err)
|
||||
|
||||
diffAC, err := repo.DiffTreeToTree(treeA, treeC, nil)
|
||||
checkFatal(t, err)
|
||||
|
||||
errMessageDropped := errors.New("message dropped")
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
tree *Tree
|
||||
diff *Diff
|
||||
applyHunkCallback ApplyHunkCallback
|
||||
applyDeltaCallback ApplyDeltaCallback
|
||||
err error
|
||||
expectedDiff *Diff
|
||||
}{
|
||||
{
|
||||
name: "applying patch produces the same diff",
|
||||
tree: treeA,
|
||||
diff: diffAB,
|
||||
expectedDiff: diffAB,
|
||||
},
|
||||
{
|
||||
name: "applying a conflicting patch errors",
|
||||
tree: treeB,
|
||||
diff: diffAC,
|
||||
err: &GitError{
|
||||
Message: "hunk at line 1 did not apply",
|
||||
Code: ErrorCodeApplyFail,
|
||||
Class: ErrorClassPatch,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "callbacks succeeding apply the diff",
|
||||
tree: treeA,
|
||||
diff: diffAB,
|
||||
applyHunkCallback: func(*DiffHunk) (bool, error) { return true, nil },
|
||||
applyDeltaCallback: func(*DiffDelta) (bool, error) { return true, nil },
|
||||
expectedDiff: diffAB,
|
||||
},
|
||||
{
|
||||
name: "hunk callback returning false does not apply",
|
||||
tree: treeA,
|
||||
diff: diffAB,
|
||||
applyHunkCallback: func(*DiffHunk) (bool, error) { return false, nil },
|
||||
},
|
||||
{
|
||||
name: "hunk callback erroring fails the call",
|
||||
tree: treeA,
|
||||
diff: diffAB,
|
||||
applyHunkCallback: func(*DiffHunk) (bool, error) { return true, errMessageDropped },
|
||||
err: errMessageDropped,
|
||||
},
|
||||
{
|
||||
name: "delta callback returning false does not apply",
|
||||
tree: treeA,
|
||||
diff: diffAB,
|
||||
applyDeltaCallback: func(*DiffDelta) (bool, error) { return false, nil },
|
||||
},
|
||||
{
|
||||
name: "delta callback erroring fails the call",
|
||||
tree: treeA,
|
||||
diff: diffAB,
|
||||
applyDeltaCallback: func(*DiffDelta) (bool, error) { return true, errMessageDropped },
|
||||
err: errMessageDropped,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
opts, err := DefaultApplyOptions()
|
||||
checkFatal(t, err)
|
||||
|
||||
opts.ApplyHunkCallback = tc.applyHunkCallback
|
||||
opts.ApplyDeltaCallback = tc.applyDeltaCallback
|
||||
|
||||
index, err := repo.ApplyToTree(tc.diff, tc.tree, opts)
|
||||
if tc.err != nil {
|
||||
if !reflect.DeepEqual(tc.err, err) {
|
||||
t.Fatalf("expected error %q but got %q", tc.err, err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
checkFatal(t, err)
|
||||
|
||||
patchedTreeOID, err := index.WriteTreeTo(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
patchedTree, err := repo.LookupTree(patchedTreeOID)
|
||||
checkFatal(t, err)
|
||||
|
||||
patchedDiff, err := repo.DiffTreeToTree(tc.tree, patchedTree, nil)
|
||||
checkFatal(t, err)
|
||||
|
||||
appliedRaw, err := patchedDiff.ToBuf(DiffFormatPatch)
|
||||
checkFatal(t, err)
|
||||
|
||||
if tc.expectedDiff == nil {
|
||||
if len(appliedRaw) > 0 {
|
||||
t.Fatalf("expected no diff but got: %s", appliedRaw)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
expectedDiff, err := tc.expectedDiff.ToBuf(DiffFormatPatch)
|
||||
checkFatal(t, err)
|
||||
|
||||
if string(expectedDiff) != string(appliedRaw) {
|
||||
t.Fatalf("diffs do not match:\nexpected: %s\n\nactual: %s", expectedDiff, appliedRaw)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// checkSecondFileStaged checks that there is a single file called "file2" uncommitted in the repo
|
||||
func checkSecondFileStaged(t *testing.T, repo *Repository) {
|
||||
opts := StatusOptions{
|
||||
Show: StatusShowIndexAndWorkdir,
|
||||
Flags: StatusOptIncludeUntracked,
|
||||
}
|
||||
|
||||
statuses, err := repo.StatusList(&opts)
|
||||
checkFatal(t, err)
|
||||
|
||||
count, err := statuses.EntryCount()
|
||||
checkFatal(t, err)
|
||||
|
||||
if count != 1 {
|
||||
t.Error("diff should affect exactly one file")
|
||||
}
|
||||
if count == 0 {
|
||||
t.Fatal("no statuses, cannot continue test")
|
||||
}
|
||||
|
||||
entry, err := statuses.ByIndex(0)
|
||||
checkFatal(t, err)
|
||||
|
||||
if entry.Status != StatusIndexNew {
|
||||
t.Error("status should be 'new' as file has been added between commits")
|
||||
}
|
||||
|
||||
if entry.HeadToIndex.NewFile.Path != "file2" {
|
||||
t.Error("new file should be 'file2")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// checkNoFilesStaged checks that there is a single file called "file2" uncommitted in the repo
|
||||
func checkNoFilesStaged(t *testing.T, repo *Repository) {
|
||||
opts := StatusOptions{
|
||||
Show: StatusShowIndexAndWorkdir,
|
||||
Flags: StatusOptIncludeUntracked,
|
||||
}
|
||||
|
||||
statuses, err := repo.StatusList(&opts)
|
||||
checkFatal(t, err)
|
||||
|
||||
count, err := statuses.EntryCount()
|
||||
checkFatal(t, err)
|
||||
|
||||
if count != 0 {
|
||||
t.Error("files changed unexpectedly")
|
||||
}
|
||||
}
|
||||
|
||||
// addAndGetTree creates a file and commits it, returning the commit and tree
|
||||
func addAndGetTree(t *testing.T, repo *Repository, filename string, content string) (*Commit, *Tree) {
|
||||
headCommit, err := headCommit(repo)
|
||||
checkFatal(t, err)
|
||||
defer headCommit.Free()
|
||||
|
||||
p := repo.Path()
|
||||
p = strings.TrimSuffix(p, ".git")
|
||||
p = strings.TrimSuffix(p, ".git/")
|
||||
|
||||
err = ioutil.WriteFile(path.Join(p, filename), []byte((content)), 0777)
|
||||
checkFatal(t, err)
|
||||
|
||||
index, err := repo.Index()
|
||||
checkFatal(t, err)
|
||||
defer index.Free()
|
||||
|
||||
err = index.AddByPath(filename)
|
||||
checkFatal(t, err)
|
||||
|
||||
newTreeOID, err := index.WriteTreeTo(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
newTree, err := repo.LookupTree(newTreeOID)
|
||||
checkFatal(t, err)
|
||||
defer newTree.Free()
|
||||
|
||||
commitId, err := repo.CreateCommit("HEAD", signature(), signature(), fmt.Sprintf("add %s", filename), newTree, headCommit)
|
||||
checkFatal(t, err)
|
||||
|
||||
commit, err := repo.LookupCommit(commitId)
|
||||
checkFatal(t, err)
|
||||
|
||||
tree, err := commit.Tree()
|
||||
checkFatal(t, err)
|
||||
|
||||
return commit, tree
|
||||
}
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
// Code generated by "stringer -type DiffLineType -trimprefix DiffLine -tags static"; DO NOT EDIT.
|
||||
|
||||
package git
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[DiffLineContext-32]
|
||||
_ = x[DiffLineAddition-43]
|
||||
_ = x[DiffLineDeletion-45]
|
||||
_ = x[DiffLineContextEOFNL-61]
|
||||
_ = x[DiffLineAddEOFNL-62]
|
||||
_ = x[DiffLineDelEOFNL-60]
|
||||
_ = x[DiffLineFileHdr-70]
|
||||
_ = x[DiffLineHunkHdr-72]
|
||||
_ = x[DiffLineBinary-66]
|
||||
}
|
||||
|
||||
const (
|
||||
_DiffLineType_name_0 = "Context"
|
||||
_DiffLineType_name_1 = "Addition"
|
||||
_DiffLineType_name_2 = "Deletion"
|
||||
_DiffLineType_name_3 = "DelEOFNLContextEOFNLAddEOFNL"
|
||||
_DiffLineType_name_4 = "Binary"
|
||||
_DiffLineType_name_5 = "FileHdr"
|
||||
_DiffLineType_name_6 = "HunkHdr"
|
||||
)
|
||||
|
||||
var (
|
||||
_DiffLineType_index_3 = [...]uint8{0, 8, 20, 28}
|
||||
)
|
||||
|
||||
func (i DiffLineType) String() string {
|
||||
switch {
|
||||
case i == 32:
|
||||
return _DiffLineType_name_0
|
||||
case i == 43:
|
||||
return _DiffLineType_name_1
|
||||
case i == 45:
|
||||
return _DiffLineType_name_2
|
||||
case 60 <= i && i <= 62:
|
||||
i -= 60
|
||||
return _DiffLineType_name_3[_DiffLineType_index_3[i]:_DiffLineType_index_3[i+1]]
|
||||
case i == 66:
|
||||
return _DiffLineType_name_4
|
||||
case i == 70:
|
||||
return _DiffLineType_name_5
|
||||
case i == 72:
|
||||
return _DiffLineType_name_6
|
||||
default:
|
||||
return "DiffLineType(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
// Code generated by "stringer -type ErrorClass -trimprefix ErrorClass -tags static"; DO NOT EDIT.
|
||||
|
||||
package git
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[ErrorClassNone-0]
|
||||
_ = x[ErrorClassNoMemory-1]
|
||||
_ = x[ErrorClassOS-2]
|
||||
_ = x[ErrorClassInvalid-3]
|
||||
_ = x[ErrorClassReference-4]
|
||||
_ = x[ErrorClassZlib-5]
|
||||
_ = x[ErrorClassRepository-6]
|
||||
_ = x[ErrorClassConfig-7]
|
||||
_ = x[ErrorClassRegex-8]
|
||||
_ = x[ErrorClassOdb-9]
|
||||
_ = x[ErrorClassIndex-10]
|
||||
_ = x[ErrorClassObject-11]
|
||||
_ = x[ErrorClassNet-12]
|
||||
_ = x[ErrorClassTag-13]
|
||||
_ = x[ErrorClassTree-14]
|
||||
_ = x[ErrorClassIndexer-15]
|
||||
_ = x[ErrorClassSSL-16]
|
||||
_ = x[ErrorClassSubmodule-17]
|
||||
_ = x[ErrorClassThread-18]
|
||||
_ = x[ErrorClassStash-19]
|
||||
_ = x[ErrorClassCheckout-20]
|
||||
_ = x[ErrorClassFetchHead-21]
|
||||
_ = x[ErrorClassMerge-22]
|
||||
_ = x[ErrorClassSSH-23]
|
||||
_ = x[ErrorClassFilter-24]
|
||||
_ = x[ErrorClassRevert-25]
|
||||
_ = x[ErrorClassCallback-26]
|
||||
_ = x[ErrorClassRebase-29]
|
||||
_ = x[ErrorClassPatch-31]
|
||||
}
|
||||
|
||||
const (
|
||||
_ErrorClass_name_0 = "NoneNoMemoryOSInvalidReferenceZlibRepositoryConfigRegexOdbIndexObjectNetTagTreeIndexerSSLSubmoduleThreadStashCheckoutFetchHeadMergeSSHFilterRevertCallback"
|
||||
_ErrorClass_name_1 = "Rebase"
|
||||
_ErrorClass_name_2 = "Patch"
|
||||
)
|
||||
|
||||
var (
|
||||
_ErrorClass_index_0 = [...]uint8{0, 4, 12, 14, 21, 30, 34, 44, 50, 55, 58, 63, 69, 72, 75, 79, 86, 89, 98, 104, 109, 117, 126, 131, 134, 140, 146, 154}
|
||||
)
|
||||
|
||||
func (i ErrorClass) String() string {
|
||||
switch {
|
||||
case 0 <= i && i <= 26:
|
||||
return _ErrorClass_name_0[_ErrorClass_index_0[i]:_ErrorClass_index_0[i+1]]
|
||||
case i == 29:
|
||||
return _ErrorClass_name_1
|
||||
case i == 31:
|
||||
return _ErrorClass_name_2
|
||||
default:
|
||||
return "ErrorClass(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
// Code generated by "stringer -type ErrorCode -trimprefix ErrorCode -tags static"; DO NOT EDIT.
|
||||
|
||||
package git
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[ErrorCodeOK-0]
|
||||
_ = x[ErrorCodeGeneric - -1]
|
||||
_ = x[ErrorCodeNotFound - -3]
|
||||
_ = x[ErrorCodeExists - -4]
|
||||
_ = x[ErrorCodeAmbiguous - -5]
|
||||
_ = x[ErrorCodeBuffs - -6]
|
||||
_ = x[ErrorCodeUser - -7]
|
||||
_ = x[ErrorCodeBareRepo - -8]
|
||||
_ = x[ErrorCodeUnbornBranch - -9]
|
||||
_ = x[ErrorCodeUnmerged - -10]
|
||||
_ = x[ErrorCodeNonFastForward - -11]
|
||||
_ = x[ErrorCodeInvalidSpec - -12]
|
||||
_ = x[ErrorCodeConflict - -13]
|
||||
_ = x[ErrorCodeLocked - -14]
|
||||
_ = x[ErrorCodeModified - -15]
|
||||
_ = x[ErrorCodeAuth - -16]
|
||||
_ = x[ErrorCodeCertificate - -17]
|
||||
_ = x[ErrorCodeApplied - -18]
|
||||
_ = x[ErrorCodePeel - -19]
|
||||
_ = x[ErrorCodeEOF - -20]
|
||||
_ = x[ErrorCodeInvalid - -21]
|
||||
_ = x[ErrorCodeUncommitted - -22]
|
||||
_ = x[ErrorCodeDirectory - -23]
|
||||
_ = x[ErrorCodeMergeConflict - -24]
|
||||
_ = x[ErrorCodePassthrough - -30]
|
||||
_ = x[ErrorCodeIterOver - -31]
|
||||
_ = x[ErrorCodeRetry - -32]
|
||||
_ = x[ErrorCodeMismatch - -33]
|
||||
_ = x[ErrorCodeIndexDirty - -34]
|
||||
_ = x[ErrorCodeApplyFail - -35]
|
||||
}
|
||||
|
||||
const (
|
||||
_ErrorCode_name_0 = "ApplyFailIndexDirtyMismatchRetryIterOverPassthrough"
|
||||
_ErrorCode_name_1 = "MergeConflictDirectoryUncommittedInvalidEOFPeelAppliedCertificateAuthModifiedLockedConflictInvalidSpecNonFastForwardUnmergedUnbornBranchBareRepoUserBuffsAmbiguousExistsNotFound"
|
||||
_ErrorCode_name_2 = "GenericOK"
|
||||
)
|
||||
|
||||
var (
|
||||
_ErrorCode_index_0 = [...]uint8{0, 9, 19, 27, 32, 40, 51}
|
||||
_ErrorCode_index_1 = [...]uint8{0, 13, 22, 33, 40, 43, 47, 54, 65, 69, 77, 83, 91, 102, 116, 124, 136, 144, 148, 153, 162, 168, 176}
|
||||
_ErrorCode_index_2 = [...]uint8{0, 7, 9}
|
||||
)
|
||||
|
||||
func (i ErrorCode) String() string {
|
||||
switch {
|
||||
case -35 <= i && i <= -30:
|
||||
i -= -35
|
||||
return _ErrorCode_name_0[_ErrorCode_index_0[i]:_ErrorCode_index_0[i+1]]
|
||||
case -24 <= i && i <= -3:
|
||||
i -= -24
|
||||
return _ErrorCode_name_1[_ErrorCode_index_1[i]:_ErrorCode_index_1[i+1]]
|
||||
case -1 <= i && i <= 0:
|
||||
i -= -1
|
||||
return _ErrorCode_name_2[_ErrorCode_index_2[i]:_ErrorCode_index_2[i+1]]
|
||||
default:
|
||||
return "ErrorCode(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
}
|
|
@ -12,10 +12,10 @@ const (
|
|||
FeatureThreads Feature = C.GIT_FEATURE_THREADS
|
||||
|
||||
// libgit2 was built with HTTPS support built-in
|
||||
FeatureHttps Feature = C.GIT_FEATURE_HTTPS
|
||||
FeatureHTTPS Feature = C.GIT_FEATURE_HTTPS
|
||||
|
||||
// libgit2 was build with SSH support built-in
|
||||
FeatureSsh Feature = C.GIT_FEATURE_SSH
|
||||
FeatureSSH Feature = C.GIT_FEATURE_SSH
|
||||
|
||||
// libgit2 was built with nanosecond support for files
|
||||
FeatureNSec Feature = C.GIT_FEATURE_NSEC
|
||||
|
|
263
git.go
263
git.go
|
@ -14,127 +14,183 @@ import (
|
|||
"unsafe"
|
||||
)
|
||||
|
||||
//go:generate stringer -type ErrorClass -trimprefix ErrorClass -tags static
|
||||
type ErrorClass int
|
||||
|
||||
const (
|
||||
ErrClassNone ErrorClass = C.GITERR_NONE
|
||||
ErrClassNoMemory ErrorClass = C.GITERR_NOMEMORY
|
||||
ErrClassOs ErrorClass = C.GITERR_OS
|
||||
ErrClassInvalid ErrorClass = C.GITERR_INVALID
|
||||
ErrClassReference ErrorClass = C.GITERR_REFERENCE
|
||||
ErrClassZlib ErrorClass = C.GITERR_ZLIB
|
||||
ErrClassRepository ErrorClass = C.GITERR_REPOSITORY
|
||||
ErrClassConfig ErrorClass = C.GITERR_CONFIG
|
||||
ErrClassRegex ErrorClass = C.GITERR_REGEX
|
||||
ErrClassOdb ErrorClass = C.GITERR_ODB
|
||||
ErrClassIndex ErrorClass = C.GITERR_INDEX
|
||||
ErrClassObject ErrorClass = C.GITERR_OBJECT
|
||||
ErrClassNet ErrorClass = C.GITERR_NET
|
||||
ErrClassTag ErrorClass = C.GITERR_TAG
|
||||
ErrClassTree ErrorClass = C.GITERR_TREE
|
||||
ErrClassIndexer ErrorClass = C.GITERR_INDEXER
|
||||
ErrClassSSL ErrorClass = C.GITERR_SSL
|
||||
ErrClassSubmodule ErrorClass = C.GITERR_SUBMODULE
|
||||
ErrClassThread ErrorClass = C.GITERR_THREAD
|
||||
ErrClassStash ErrorClass = C.GITERR_STASH
|
||||
ErrClassCheckout ErrorClass = C.GITERR_CHECKOUT
|
||||
ErrClassFetchHead ErrorClass = C.GITERR_FETCHHEAD
|
||||
ErrClassMerge ErrorClass = C.GITERR_MERGE
|
||||
ErrClassSsh ErrorClass = C.GITERR_SSH
|
||||
ErrClassFilter ErrorClass = C.GITERR_FILTER
|
||||
ErrClassRevert ErrorClass = C.GITERR_REVERT
|
||||
ErrClassCallback ErrorClass = C.GITERR_CALLBACK
|
||||
ErrClassRebase ErrorClass = C.GITERR_REBASE
|
||||
ErrorClassNone ErrorClass = C.GIT_ERROR_NONE
|
||||
ErrorClassNoMemory ErrorClass = C.GIT_ERROR_NOMEMORY
|
||||
ErrorClassOS ErrorClass = C.GIT_ERROR_OS
|
||||
ErrorClassInvalid ErrorClass = C.GIT_ERROR_INVALID
|
||||
ErrorClassReference ErrorClass = C.GIT_ERROR_REFERENCE
|
||||
ErrorClassZlib ErrorClass = C.GIT_ERROR_ZLIB
|
||||
ErrorClassRepository ErrorClass = C.GIT_ERROR_REPOSITORY
|
||||
ErrorClassConfig ErrorClass = C.GIT_ERROR_CONFIG
|
||||
ErrorClassRegex ErrorClass = C.GIT_ERROR_REGEX
|
||||
ErrorClassOdb ErrorClass = C.GIT_ERROR_ODB
|
||||
ErrorClassIndex ErrorClass = C.GIT_ERROR_INDEX
|
||||
ErrorClassObject ErrorClass = C.GIT_ERROR_OBJECT
|
||||
ErrorClassNet ErrorClass = C.GIT_ERROR_NET
|
||||
ErrorClassTag ErrorClass = C.GIT_ERROR_TAG
|
||||
ErrorClassTree ErrorClass = C.GIT_ERROR_TREE
|
||||
ErrorClassIndexer ErrorClass = C.GIT_ERROR_INDEXER
|
||||
ErrorClassSSL ErrorClass = C.GIT_ERROR_SSL
|
||||
ErrorClassSubmodule ErrorClass = C.GIT_ERROR_SUBMODULE
|
||||
ErrorClassThread ErrorClass = C.GIT_ERROR_THREAD
|
||||
ErrorClassStash ErrorClass = C.GIT_ERROR_STASH
|
||||
ErrorClassCheckout ErrorClass = C.GIT_ERROR_CHECKOUT
|
||||
ErrorClassFetchHead ErrorClass = C.GIT_ERROR_FETCHHEAD
|
||||
ErrorClassMerge ErrorClass = C.GIT_ERROR_MERGE
|
||||
ErrorClassSSH ErrorClass = C.GIT_ERROR_SSH
|
||||
ErrorClassFilter ErrorClass = C.GIT_ERROR_FILTER
|
||||
ErrorClassRevert ErrorClass = C.GIT_ERROR_REVERT
|
||||
ErrorClassCallback ErrorClass = C.GIT_ERROR_CALLBACK
|
||||
ErrorClassRebase ErrorClass = C.GIT_ERROR_REBASE
|
||||
ErrorClassPatch ErrorClass = C.GIT_ERROR_PATCH
|
||||
)
|
||||
|
||||
//go:generate stringer -type ErrorCode -trimprefix ErrorCode -tags static
|
||||
type ErrorCode int
|
||||
|
||||
const (
|
||||
// ErrorCodeOK indicates that the operation completed successfully.
|
||||
ErrorCodeOK ErrorCode = C.GIT_OK
|
||||
|
||||
// No error
|
||||
ErrOk ErrorCode = C.GIT_OK
|
||||
// ErrorCodeGeneric represents a generic error.
|
||||
ErrorCodeGeneric ErrorCode = C.GIT_ERROR
|
||||
// ErrorCodeNotFound represents that the requested object could not be found
|
||||
ErrorCodeNotFound ErrorCode = C.GIT_ENOTFOUND
|
||||
// ErrorCodeExists represents that the object exists preventing operation.
|
||||
ErrorCodeExists ErrorCode = C.GIT_EEXISTS
|
||||
// ErrorCodeAmbiguous represents that more than one object matches.
|
||||
ErrorCodeAmbiguous ErrorCode = C.GIT_EAMBIGUOUS
|
||||
// ErrorCodeBuffs represents that the output buffer is too short to hold data.
|
||||
ErrorCodeBuffs ErrorCode = C.GIT_EBUFS
|
||||
|
||||
// Generic error
|
||||
ErrGeneric ErrorCode = C.GIT_ERROR
|
||||
// Requested object could not be found
|
||||
ErrNotFound ErrorCode = C.GIT_ENOTFOUND
|
||||
// Object exists preventing operation
|
||||
ErrExists ErrorCode = C.GIT_EEXISTS
|
||||
// More than one object matches
|
||||
ErrAmbigious ErrorCode = C.GIT_EAMBIGUOUS
|
||||
// Output buffer too short to hold data
|
||||
ErrBuffs ErrorCode = C.GIT_EBUFS
|
||||
|
||||
// GIT_EUSER is a special error that is never generated by libgit2
|
||||
// ErrorCodeUser is a special error that is never generated by libgit2
|
||||
// code. You can return it from a callback (e.g to stop an iteration)
|
||||
// to know that it was generated by the callback and not by libgit2.
|
||||
ErrUser ErrorCode = C.GIT_EUSER
|
||||
ErrorCodeUser ErrorCode = C.GIT_EUSER
|
||||
|
||||
// Operation not allowed on bare repository
|
||||
ErrBareRepo ErrorCode = C.GIT_EBAREREPO
|
||||
// HEAD refers to branch with no commits
|
||||
ErrUnbornBranch ErrorCode = C.GIT_EUNBORNBRANCH
|
||||
// Merge in progress prevented operation
|
||||
ErrUnmerged ErrorCode = C.GIT_EUNMERGED
|
||||
// Reference was not fast-forwardable
|
||||
ErrNonFastForward ErrorCode = C.GIT_ENONFASTFORWARD
|
||||
// Name/ref spec was not in a valid format
|
||||
ErrInvalidSpec ErrorCode = C.GIT_EINVALIDSPEC
|
||||
// Checkout conflicts prevented operation
|
||||
ErrConflict ErrorCode = C.GIT_ECONFLICT
|
||||
// Lock file prevented operation
|
||||
ErrLocked ErrorCode = C.GIT_ELOCKED
|
||||
// Reference value does not match expected
|
||||
ErrModified ErrorCode = C.GIT_EMODIFIED
|
||||
// Authentication failed
|
||||
ErrAuth ErrorCode = C.GIT_EAUTH
|
||||
// Server certificate is invalid
|
||||
ErrCertificate ErrorCode = C.GIT_ECERTIFICATE
|
||||
// Patch/merge has already been applied
|
||||
ErrApplied ErrorCode = C.GIT_EAPPLIED
|
||||
// The requested peel operation is not possible
|
||||
ErrPeel ErrorCode = C.GIT_EPEEL
|
||||
// Unexpected EOF
|
||||
ErrEOF ErrorCode = C.GIT_EEOF
|
||||
// Uncommitted changes in index prevented operation
|
||||
ErrUncommitted ErrorCode = C.GIT_EUNCOMMITTED
|
||||
// The operation is not valid for a directory
|
||||
ErrDirectory ErrorCode = C.GIT_EDIRECTORY
|
||||
// A merge conflict exists and cannot continue
|
||||
ErrMergeConflict ErrorCode = C.GIT_EMERGECONFLICT
|
||||
// ErrorCodeBareRepo represents that the operation not allowed on bare repository
|
||||
ErrorCodeBareRepo ErrorCode = C.GIT_EBAREREPO
|
||||
// ErrorCodeUnbornBranch represents that HEAD refers to branch with no commits.
|
||||
ErrorCodeUnbornBranch ErrorCode = C.GIT_EUNBORNBRANCH
|
||||
// ErrorCodeUnmerged represents that a merge in progress prevented operation.
|
||||
ErrorCodeUnmerged ErrorCode = C.GIT_EUNMERGED
|
||||
// ErrorCodeNonFastForward represents that the reference was not fast-forwardable.
|
||||
ErrorCodeNonFastForward ErrorCode = C.GIT_ENONFASTFORWARD
|
||||
// ErrorCodeInvalidSpec represents that the name/ref spec was not in a valid format.
|
||||
ErrorCodeInvalidSpec ErrorCode = C.GIT_EINVALIDSPEC
|
||||
// ErrorCodeConflict represents that checkout conflicts prevented operation.
|
||||
ErrorCodeConflict ErrorCode = C.GIT_ECONFLICT
|
||||
// ErrorCodeLocked represents that lock file prevented operation.
|
||||
ErrorCodeLocked ErrorCode = C.GIT_ELOCKED
|
||||
// ErrorCodeModified represents that the reference value does not match expected.
|
||||
ErrorCodeModified ErrorCode = C.GIT_EMODIFIED
|
||||
// ErrorCodeAuth represents that the authentication failed.
|
||||
ErrorCodeAuth ErrorCode = C.GIT_EAUTH
|
||||
// ErrorCodeCertificate represents that the server certificate is invalid.
|
||||
ErrorCodeCertificate ErrorCode = C.GIT_ECERTIFICATE
|
||||
// ErrorCodeApplied represents that the patch/merge has already been applied.
|
||||
ErrorCodeApplied ErrorCode = C.GIT_EAPPLIED
|
||||
// ErrorCodePeel represents that the requested peel operation is not possible.
|
||||
ErrorCodePeel ErrorCode = C.GIT_EPEEL
|
||||
// ErrorCodeEOF represents an unexpected EOF.
|
||||
ErrorCodeEOF ErrorCode = C.GIT_EEOF
|
||||
// ErrorCodeInvalid represents an invalid operation or input.
|
||||
ErrorCodeInvalid ErrorCode = C.GIT_EINVALID
|
||||
// ErrorCodeUIncommitted represents that uncommitted changes in index prevented operation.
|
||||
ErrorCodeUncommitted ErrorCode = C.GIT_EUNCOMMITTED
|
||||
// ErrorCodeDirectory represents that the operation is not valid for a directory.
|
||||
ErrorCodeDirectory ErrorCode = C.GIT_EDIRECTORY
|
||||
// ErrorCodeMergeConflict represents that a merge conflict exists and cannot continue.
|
||||
ErrorCodeMergeConflict ErrorCode = C.GIT_EMERGECONFLICT
|
||||
|
||||
// Internal only
|
||||
ErrPassthrough ErrorCode = C.GIT_PASSTHROUGH
|
||||
// Signals end of iteration with iterator
|
||||
ErrIterOver ErrorCode = C.GIT_ITEROVER
|
||||
// ErrorCodePassthrough represents that a user-configured callback refused to act.
|
||||
ErrorCodePassthrough ErrorCode = C.GIT_PASSTHROUGH
|
||||
// ErrorCodeIterOver signals end of iteration with iterator.
|
||||
ErrorCodeIterOver ErrorCode = C.GIT_ITEROVER
|
||||
// ErrorCodeRetry is an internal-only error code.
|
||||
ErrorCodeRetry ErrorCode = C.GIT_RETRY
|
||||
// ErrorCodeMismatch represents a hashsum mismatch in object.
|
||||
ErrorCodeMismatch ErrorCode = C.GIT_EMISMATCH
|
||||
// ErrorCodeIndexDirty represents that unsaved changes in the index would be overwritten.
|
||||
ErrorCodeIndexDirty ErrorCode = C.GIT_EINDEXDIRTY
|
||||
// ErrorCodeApplyFail represents that a patch application failed.
|
||||
ErrorCodeApplyFail ErrorCode = C.GIT_EAPPLYFAIL
|
||||
)
|
||||
|
||||
var (
|
||||
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 remotePointers *remotePointerList
|
||||
|
||||
func init() {
|
||||
initLibGit2()
|
||||
}
|
||||
|
||||
func initLibGit2() {
|
||||
pointerHandles = NewHandleList()
|
||||
remotePointers = newRemotePointerList()
|
||||
|
||||
C.git_libgit2_init()
|
||||
features := Features()
|
||||
|
||||
// Due to the multithreaded nature of Go and its interaction with
|
||||
// calling C functions, we cannot work with a library that was not built
|
||||
// with multi-threading support. The most likely outcome is a segfault
|
||||
// or panic at an incomprehensible time, so let's make it easy by
|
||||
// panicking right here.
|
||||
if Features()&FeatureThreads == 0 {
|
||||
if features&FeatureThreads == 0 {
|
||||
panic("libgit2 was not built with threading support")
|
||||
}
|
||||
|
||||
// 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()
|
||||
if features&FeatureHTTPS == 0 {
|
||||
if err := registerManagedHTTP(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
// 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()
|
||||
}
|
||||
if features&FeatureSSH == 0 {
|
||||
if err := registerManagedSSH(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown frees all the resources acquired by libgit2. Make sure no
|
||||
// references to any git2go objects are live before calling this.
|
||||
// After this is called, invoking any function from this library will result in
|
||||
// undefined behavior, so make sure this is called carefully.
|
||||
func Shutdown() {
|
||||
if err := unregisterManagedTransports(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
pointerHandles.Clear()
|
||||
remotePointers.clear()
|
||||
|
||||
C.git_libgit2_shutdown()
|
||||
}
|
||||
|
||||
// ReInit reinitializes the global state, this is useful if the effective user
|
||||
// id has changed and you want to update the stored search paths for gitconfig
|
||||
// files. This function frees any references to objects, so it should be called
|
||||
// before any other functions are called.
|
||||
func ReInit() {
|
||||
Shutdown()
|
||||
initLibGit2()
|
||||
}
|
||||
|
||||
// Oid represents the id for a Git object.
|
||||
|
@ -167,13 +223,13 @@ func NewOid(s string) (*Oid, error) {
|
|||
|
||||
o := new(Oid)
|
||||
|
||||
slice, error := hex.DecodeString(s)
|
||||
if error != nil {
|
||||
return nil, error
|
||||
slice, err := hex.DecodeString(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(slice) != 20 {
|
||||
return nil, &GitError{"Invalid Oid", ErrClassNone, ErrGeneric}
|
||||
return nil, &GitError{"Invalid Oid", ErrorClassNone, ErrGeneric}
|
||||
}
|
||||
|
||||
copy(o[:], slice[:20])
|
||||
|
@ -241,7 +297,6 @@ func (e GitError) Error() string {
|
|||
}
|
||||
|
||||
func IsErrorClass(err error, c ErrorClass) bool {
|
||||
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
@ -261,20 +316,23 @@ func IsErrorCode(err error, c ErrorCode) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func MakeGitError(errorCode C.int) error {
|
||||
|
||||
func MakeGitError(c C.int) error {
|
||||
var errMessage string
|
||||
var errClass ErrorClass
|
||||
if errorCode != C.GIT_ITEROVER {
|
||||
err := C.giterr_last()
|
||||
errorCode := ErrorCode(c)
|
||||
if errorCode != ErrorCodeIterOver {
|
||||
err := C.git_error_last()
|
||||
if err != nil {
|
||||
errMessage = C.GoString(err.message)
|
||||
errClass = ErrorClass(err.klass)
|
||||
} else {
|
||||
errClass = ErrClassInvalid
|
||||
errClass = ErrorClassInvalid
|
||||
}
|
||||
}
|
||||
return &GitError{errMessage, errClass, ErrorCode(errorCode)}
|
||||
if errMessage == "" {
|
||||
errMessage = errorCode.String()
|
||||
}
|
||||
return &GitError{errMessage, errClass, errorCode}
|
||||
}
|
||||
|
||||
func MakeGitError2(err int) error {
|
||||
|
@ -295,6 +353,17 @@ func ucbool(b bool) C.uint {
|
|||
return C.uint(0)
|
||||
}
|
||||
|
||||
func setCallbackError(errorMessage **C.char, err error) C.int {
|
||||
if err != nil {
|
||||
*errorMessage = C.CString(err.Error())
|
||||
if gitError, ok := err.(*GitError); ok {
|
||||
return C.int(gitError.Code)
|
||||
}
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
func Discover(start string, across_fs bool, ceiling_dirs []string) (string, error) {
|
||||
ceildirs := C.CString(strings.Join(ceiling_dirs, string(C.GIT_PATH_LIST_SEPARATOR)))
|
||||
defer C.free(unsafe.Pointer(ceildirs))
|
||||
|
|
71
git_test.go
71
git_test.go
|
@ -1,13 +1,51 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if err := registerManagedHTTP(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ret := m.Run()
|
||||
|
||||
if err := unregisterManagedTransports(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Ensure that we are not leaking any pointer handles.
|
||||
pointerHandles.Lock()
|
||||
if len(pointerHandles.handles) > 0 {
|
||||
for h, ptr := range pointerHandles.handles {
|
||||
fmt.Printf("%016p: %v %+v\n", h, reflect.TypeOf(ptr), ptr)
|
||||
}
|
||||
panic("pointer handle list not empty")
|
||||
}
|
||||
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()
|
||||
|
||||
os.Exit(ret)
|
||||
}
|
||||
|
||||
func cleanupTestRepo(t *testing.T, r *Repository) {
|
||||
var err error
|
||||
if r.IsBare() {
|
||||
|
@ -45,7 +83,16 @@ func createBareTestRepo(t *testing.T) *Repository {
|
|||
return repo
|
||||
}
|
||||
|
||||
// commitOptions contains any extra options for creating commits in the seed repo
|
||||
type commitOptions struct {
|
||||
CommitSigningCallback
|
||||
}
|
||||
|
||||
func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) {
|
||||
return seedTestRepoOpt(t, repo, commitOptions{})
|
||||
}
|
||||
|
||||
func seedTestRepoOpt(t *testing.T, repo *Repository, opts commitOptions) (*Oid, *Oid) {
|
||||
loc, err := time.LoadLocation("Europe/Berlin")
|
||||
checkFatal(t, err)
|
||||
sig := &Signature{
|
||||
|
@ -69,6 +116,28 @@ func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) {
|
|||
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree)
|
||||
checkFatal(t, err)
|
||||
|
||||
if opts.CommitSigningCallback != nil {
|
||||
commit, err := repo.LookupCommit(commitId)
|
||||
checkFatal(t, err)
|
||||
|
||||
signature, signatureField, err := opts.CommitSigningCallback(commit.ContentToSign())
|
||||
checkFatal(t, err)
|
||||
|
||||
oid, err := commit.WithSignature(signature, signatureField)
|
||||
checkFatal(t, err)
|
||||
newCommit, err := repo.LookupCommit(oid)
|
||||
checkFatal(t, err)
|
||||
head, err := repo.Head()
|
||||
checkFatal(t, err)
|
||||
_, err = repo.References.Create(
|
||||
head.Name(),
|
||||
newCommit.Id(),
|
||||
true,
|
||||
"repoint to signed commit",
|
||||
)
|
||||
checkFatal(t, err)
|
||||
}
|
||||
|
||||
return commitId, treeId
|
||||
}
|
||||
|
||||
|
@ -124,7 +193,7 @@ func TestOidZero(t *testing.T) {
|
|||
func TestEmptyOid(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := NewOid("")
|
||||
if err == nil || !IsErrorCode(err, ErrGeneric) {
|
||||
if err == nil || !IsErrorCode(err, ErrorCodeGeneric) {
|
||||
t.Fatal("Should have returned invalid error")
|
||||
}
|
||||
}
|
||||
|
|
10
go.mod
10
go.mod
|
@ -1 +1,9 @@
|
|||
module github.com/libgit2/git2go
|
||||
module github.com/libgit2/git2go/v28
|
||||
|
||||
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
|
||||
)
|
||||
|
|
|
@ -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=
|
11
handles.go
11
handles.go
|
@ -11,6 +11,7 @@ import (
|
|||
)
|
||||
|
||||
type HandleList struct {
|
||||
doNotCompare
|
||||
sync.RWMutex
|
||||
// stores the Go pointers
|
||||
handles map[unsafe.Pointer]interface{}
|
||||
|
@ -43,6 +44,16 @@ func (v *HandleList) Untrack(handle unsafe.Pointer) {
|
|||
v.Unlock()
|
||||
}
|
||||
|
||||
// Clear stops tracking all the managed pointers.
|
||||
func (v *HandleList) Clear() {
|
||||
v.Lock()
|
||||
for handle := range v.handles {
|
||||
delete(v.handles, handle)
|
||||
C.free(handle)
|
||||
}
|
||||
v.Unlock()
|
||||
}
|
||||
|
||||
// Get retrieves the pointer from the given handle
|
||||
func (v *HandleList) Get(handle unsafe.Pointer) interface{} {
|
||||
v.RLock()
|
||||
|
|
|
@ -0,0 +1,240 @@
|
|||
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)
|
||||
proxyOpts, err := transport.SmartProxyOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch proxyOpts.Type {
|
||||
case ProxyTypeNone:
|
||||
proxyFn = nil
|
||||
case ProxyTypeAuto:
|
||||
proxyFn = http.ProxyFromEnvironment
|
||||
case ProxyTypeSpecified:
|
||||
parsedUrl, err := url.Parse(proxyOpts.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 (git2go)")
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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("", CredTypeUserpassPlaintext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
130
index.go
130
index.go
|
@ -10,48 +10,56 @@ extern int _go_git_index_remove_all(git_index*, const git_strarray*, void*);
|
|||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type IndexMatchedPathCallback func(string, string) int
|
||||
type indexMatchedPathCallbackData struct {
|
||||
callback IndexMatchedPathCallback
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
type IndexAddOpts uint
|
||||
// IndexAddOption is a set of flags for APIs that add files matching pathspec.
|
||||
type IndexAddOption uint
|
||||
|
||||
const (
|
||||
IndexAddDefault IndexAddOpts = C.GIT_INDEX_ADD_DEFAULT
|
||||
IndexAddForce IndexAddOpts = C.GIT_INDEX_ADD_FORCE
|
||||
IndexAddDisablePathspecMatch IndexAddOpts = C.GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH
|
||||
IndexAddCheckPathspec IndexAddOpts = C.GIT_INDEX_ADD_CHECK_PATHSPEC
|
||||
IndexAddDefault IndexAddOption = C.GIT_INDEX_ADD_DEFAULT
|
||||
IndexAddForce IndexAddOption = C.GIT_INDEX_ADD_FORCE
|
||||
IndexAddDisablePathspecMatch IndexAddOption = C.GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH
|
||||
IndexAddCheckPathspec IndexAddOption = C.GIT_INDEX_ADD_CHECK_PATHSPEC
|
||||
)
|
||||
|
||||
type IndexStageOpts int
|
||||
// IndexStageState indicates the state of the git index.
|
||||
type IndexStageState int
|
||||
|
||||
const (
|
||||
// IndexStageAny matches any index stage.
|
||||
//
|
||||
// Some index APIs take a stage to match; pass this value to match
|
||||
// any entry matching the path regardless of stage.
|
||||
IndexStageAny IndexStageOpts = C.GIT_INDEX_STAGE_ANY
|
||||
IndexStageAny IndexStageState = C.GIT_INDEX_STAGE_ANY
|
||||
// IndexStageNormal is a normal staged file in the index.
|
||||
IndexStageNormal IndexStageOpts = C.GIT_INDEX_STAGE_NORMAL
|
||||
IndexStageNormal IndexStageState = C.GIT_INDEX_STAGE_NORMAL
|
||||
// IndexStageAncestor is the ancestor side of a conflict.
|
||||
IndexStageAncestor IndexStageOpts = C.GIT_INDEX_STAGE_ANCESTOR
|
||||
IndexStageAncestor IndexStageState = C.GIT_INDEX_STAGE_ANCESTOR
|
||||
// IndexStageOurs is the "ours" side of a conflict.
|
||||
IndexStageOurs IndexStageOpts = C.GIT_INDEX_STAGE_OURS
|
||||
IndexStageOurs IndexStageState = C.GIT_INDEX_STAGE_OURS
|
||||
// IndexStageTheirs is the "theirs" side of a conflict.
|
||||
IndexStageTheirs IndexStageOpts = C.GIT_INDEX_STAGE_THEIRS
|
||||
IndexStageTheirs IndexStageState = C.GIT_INDEX_STAGE_THEIRS
|
||||
)
|
||||
|
||||
type Index struct {
|
||||
doNotCompare
|
||||
ptr *C.git_index
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
type IndexTime struct {
|
||||
seconds int32
|
||||
nanoseconds uint32
|
||||
Seconds int32
|
||||
Nanoseconds uint32
|
||||
}
|
||||
|
||||
type IndexEntry struct {
|
||||
|
@ -82,15 +90,17 @@ func newIndexEntryFromC(entry *C.git_index_entry) *IndexEntry {
|
|||
}
|
||||
|
||||
func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) {
|
||||
dest.ctime.seconds = C.int32_t(source.Ctime.seconds)
|
||||
dest.ctime.nanoseconds = C.uint32_t(source.Ctime.nanoseconds)
|
||||
dest.mtime.seconds = C.int32_t(source.Mtime.seconds)
|
||||
dest.mtime.nanoseconds = C.uint32_t(source.Mtime.nanoseconds)
|
||||
dest.ctime.seconds = C.int32_t(source.Ctime.Seconds)
|
||||
dest.ctime.nanoseconds = C.uint32_t(source.Ctime.Nanoseconds)
|
||||
dest.mtime.seconds = C.int32_t(source.Mtime.Seconds)
|
||||
dest.mtime.nanoseconds = C.uint32_t(source.Mtime.Nanoseconds)
|
||||
dest.mode = C.uint32_t(source.Mode)
|
||||
dest.uid = C.uint32_t(source.Uid)
|
||||
dest.gid = C.uint32_t(source.Gid)
|
||||
dest.file_size = C.uint32_t(source.Size)
|
||||
dest.id = *source.Id.toC()
|
||||
if source.Id != nil {
|
||||
dest.id = *source.Id.toC()
|
||||
}
|
||||
dest.path = C.CString(source.Path)
|
||||
}
|
||||
|
||||
|
@ -99,7 +109,7 @@ func freeCIndexEntry(entry *C.git_index_entry) {
|
|||
}
|
||||
|
||||
func newIndexFromC(ptr *C.git_index, repo *Repository) *Index {
|
||||
idx := &Index{ptr, repo}
|
||||
idx := &Index{ptr: ptr, repo: repo}
|
||||
runtime.SetFinalizer(idx, (*Index).Free)
|
||||
return idx
|
||||
}
|
||||
|
@ -195,18 +205,45 @@ func (v *Index) AddByPath(path string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMatchedPathCallback) error {
|
||||
// AddFromBuffer adds or replaces an index entry from a buffer in memory
|
||||
func (v *Index) AddFromBuffer(entry *IndexEntry, buffer []byte) error {
|
||||
var centry C.git_index_entry
|
||||
|
||||
populateCIndexEntry(entry, ¢ry)
|
||||
defer freeCIndexEntry(¢ry)
|
||||
|
||||
var cbuffer unsafe.Pointer
|
||||
if len(buffer) > 0 {
|
||||
cbuffer = unsafe.Pointer(&buffer[0])
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
if err := C.git_index_add_frombuffer(v.ptr, ¢ry, cbuffer, C.size_t(len(buffer))); err < 0 {
|
||||
return MakeGitError(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Index) AddAll(pathspecs []string, flags IndexAddOption, callback IndexMatchedPathCallback) error {
|
||||
cpathspecs := C.git_strarray{}
|
||||
cpathspecs.count = C.size_t(len(pathspecs))
|
||||
cpathspecs.strings = makeCStringsFromStrings(pathspecs)
|
||||
defer freeStrarray(&cpathspecs)
|
||||
|
||||
var err error
|
||||
data := indexMatchedPathCallbackData{
|
||||
callback: callback,
|
||||
errorTarget: &err,
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var handle unsafe.Pointer
|
||||
if callback != nil {
|
||||
handle = pointerHandles.Track(callback)
|
||||
handle = pointerHandles.Track(&data)
|
||||
defer pointerHandles.Untrack(handle)
|
||||
}
|
||||
|
||||
|
@ -217,9 +254,13 @@ func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMat
|
|||
handle,
|
||||
)
|
||||
runtime.KeepAlive(v)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -229,12 +270,17 @@ func (v *Index) UpdateAll(pathspecs []string, callback IndexMatchedPathCallback)
|
|||
cpathspecs.strings = makeCStringsFromStrings(pathspecs)
|
||||
defer freeStrarray(&cpathspecs)
|
||||
|
||||
var err error
|
||||
data := indexMatchedPathCallbackData{
|
||||
callback: callback,
|
||||
errorTarget: &err,
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var handle unsafe.Pointer
|
||||
if callback != nil {
|
||||
handle = pointerHandles.Track(callback)
|
||||
handle = pointerHandles.Track(&data)
|
||||
defer pointerHandles.Untrack(handle)
|
||||
}
|
||||
|
||||
|
@ -244,9 +290,13 @@ func (v *Index) UpdateAll(pathspecs []string, callback IndexMatchedPathCallback)
|
|||
handle,
|
||||
)
|
||||
runtime.KeepAlive(v)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -256,12 +306,17 @@ func (v *Index) RemoveAll(pathspecs []string, callback IndexMatchedPathCallback)
|
|||
cpathspecs.strings = makeCStringsFromStrings(pathspecs)
|
||||
defer freeStrarray(&cpathspecs)
|
||||
|
||||
var err error
|
||||
data := indexMatchedPathCallbackData{
|
||||
callback: callback,
|
||||
errorTarget: &err,
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var handle unsafe.Pointer
|
||||
if callback != nil {
|
||||
handle = pointerHandles.Track(callback)
|
||||
handle = pointerHandles.Track(&data)
|
||||
defer pointerHandles.Untrack(handle)
|
||||
}
|
||||
|
||||
|
@ -271,19 +326,30 @@ func (v *Index) RemoveAll(pathspecs []string, callback IndexMatchedPathCallback)
|
|||
handle,
|
||||
)
|
||||
runtime.KeepAlive(v)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//export indexMatchedPathCallback
|
||||
func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Pointer) int {
|
||||
if callback, ok := pointerHandles.Get(payload).(IndexMatchedPathCallback); ok {
|
||||
return callback(C.GoString(cPath), C.GoString(cMatchedPathspec))
|
||||
} else {
|
||||
func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Pointer) C.int {
|
||||
data, ok := pointerHandles.Get(payload).(*indexMatchedPathCallbackData)
|
||||
if !ok {
|
||||
panic("invalid matched path callback")
|
||||
}
|
||||
|
||||
ret := data.callback(C.GoString(cPath), C.GoString(cMatchedPathspec))
|
||||
if ret < 0 {
|
||||
*data.errorTarget = errors.New(ErrorCode(ret).String())
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
func (v *Index) RemoveByPath(path string) error {
|
||||
|
@ -409,7 +475,7 @@ func (v *Index) EntryByPath(path string, stage int) (*IndexEntry, error) {
|
|||
|
||||
centry := C.git_index_get_bypath(v.ptr, cpath, C.int(stage))
|
||||
if centry == nil {
|
||||
return nil, MakeGitError(C.GIT_ENOTFOUND)
|
||||
return nil, MakeGitError(C.int(ErrorCodeNotFound))
|
||||
}
|
||||
ret := newIndexEntryFromC(centry)
|
||||
runtime.KeepAlive(v)
|
||||
|
@ -504,7 +570,7 @@ type IndexConflict struct {
|
|||
Their *IndexEntry
|
||||
}
|
||||
|
||||
func (v *Index) GetConflict(path string) (IndexConflict, error) {
|
||||
func (v *Index) Conflict(path string) (IndexConflict, error) {
|
||||
|
||||
var cancestor *C.git_index_entry
|
||||
var cour *C.git_index_entry
|
||||
|
@ -529,6 +595,11 @@ func (v *Index) GetConflict(path string) (IndexConflict, error) {
|
|||
return ret, nil
|
||||
}
|
||||
|
||||
// deprecated: You should use `Index.Conflict()` instead.
|
||||
func (v *Index) GetConflict(path string) (IndexConflict, error) {
|
||||
return v.Conflict(path)
|
||||
}
|
||||
|
||||
func (v *Index) RemoveConflict(path string) error {
|
||||
|
||||
cpath := C.CString(path)
|
||||
|
@ -546,6 +617,7 @@ func (v *Index) RemoveConflict(path string) error {
|
|||
}
|
||||
|
||||
type IndexConflictIterator struct {
|
||||
doNotCompare
|
||||
ptr *C.git_index_conflict_iterator
|
||||
index *Index
|
||||
}
|
||||
|
|
|
@ -165,6 +165,30 @@ func TestIndexRemoveDirectory(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIndexAddFromBuffer(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
idx, err := repo.Index()
|
||||
checkFatal(t, err)
|
||||
|
||||
entry := IndexEntry{
|
||||
Path: "README",
|
||||
Mode: FilemodeBlob,
|
||||
}
|
||||
|
||||
err = idx.AddFromBuffer(&entry, []byte("foo\n"))
|
||||
checkFatal(t, err)
|
||||
|
||||
treeId, err := idx.WriteTreeTo(repo)
|
||||
checkFatal(t, err)
|
||||
|
||||
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
|
||||
t.Fatalf("%v", treeId.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexAddAllNoCallback(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
package git
|
||||
|
||||
/*
|
||||
#include <git2.h>
|
||||
|
||||
extern const git_oid * git_indexer_hash(const git_indexer *idx);
|
||||
extern int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_transfer_progress *stats);
|
||||
extern int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats);
|
||||
extern int _go_git_indexer_new(git_indexer **out, const char *path, unsigned int mode, git_odb *odb, void *progress_cb_payload);
|
||||
extern void git_indexer_free(git_indexer *idx);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"reflect"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Indexer can post-process packfiles and create an .idx file for efficient
|
||||
// lookup.
|
||||
type Indexer struct {
|
||||
doNotCompare
|
||||
ptr *C.git_indexer
|
||||
stats C.git_transfer_progress
|
||||
ccallbacks C.git_remote_callbacks
|
||||
}
|
||||
|
||||
// NewIndexer creates a new indexer instance.
|
||||
func NewIndexer(packfilePath string, odb *Odb, callback TransferProgressCallback) (indexer *Indexer, err error) {
|
||||
var odbPtr *C.git_odb = nil
|
||||
if odb != nil {
|
||||
odbPtr = odb.ptr
|
||||
}
|
||||
|
||||
indexer = new(Indexer)
|
||||
populateRemoteCallbacks(&indexer.ccallbacks, &RemoteCallbacks{TransferProgressCallback: callback}, nil)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
cstr := C.CString(packfilePath)
|
||||
defer C.free(unsafe.Pointer(cstr))
|
||||
|
||||
ret := C._go_git_indexer_new(&indexer.ptr, cstr, 0, odbPtr, indexer.ccallbacks.payload)
|
||||
runtime.KeepAlive(odb)
|
||||
if ret < 0 {
|
||||
untrackCallbacksPayload(&indexer.ccallbacks)
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
runtime.SetFinalizer(indexer, (*Indexer).Free)
|
||||
return indexer, nil
|
||||
}
|
||||
|
||||
// Write adds data to the indexer.
|
||||
func (indexer *Indexer) Write(data []byte) (int, error) {
|
||||
header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
||||
ptr := unsafe.Pointer(header.Data)
|
||||
size := C.size_t(header.Len)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_indexer_append(indexer.ptr, ptr, size, &indexer.stats)
|
||||
runtime.KeepAlive(indexer)
|
||||
if ret < 0 {
|
||||
return 0, MakeGitError(ret)
|
||||
}
|
||||
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
// Commit finalizes the pack and index. It resolves any pending deltas and
|
||||
// writes out the index file.
|
||||
//
|
||||
// It also returns the packfile's hash. A packfile's name is derived from the
|
||||
// sorted hashing of all object names.
|
||||
func (indexer *Indexer) Commit() (*Oid, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_indexer_commit(indexer.ptr, &indexer.stats)
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
id := newOidFromC(C.git_indexer_hash(indexer.ptr))
|
||||
runtime.KeepAlive(indexer)
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Free frees the indexer and its resources.
|
||||
func (indexer *Indexer) Free() {
|
||||
untrackCallbacksPayload(&indexer.ccallbacks)
|
||||
runtime.SetFinalizer(indexer, nil)
|
||||
C.git_indexer_free(indexer.ptr)
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
// This is a packfile with three objects. The second is a delta which
|
||||
// depends on the third, which is also a delta.
|
||||
outOfOrderPack = []byte{
|
||||
0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
|
||||
0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76,
|
||||
0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10,
|
||||
0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62,
|
||||
0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x75, 0x01,
|
||||
0xd7, 0x71, 0x36, 0x66, 0xf4, 0xde, 0x82, 0x27, 0x76, 0xc7, 0x62, 0x2c,
|
||||
0x10, 0xf1, 0xb0, 0x7d, 0xe2, 0x80, 0xdc, 0x78, 0x9c, 0x63, 0x62, 0x62,
|
||||
0x62, 0xb7, 0x03, 0x00, 0x00, 0x69, 0x00, 0x4c, 0xde, 0x7d, 0xaa, 0xe4,
|
||||
0x19, 0x87, 0x58, 0x80, 0x61, 0x09, 0x9a, 0x33, 0xca, 0x7a, 0x31, 0x92,
|
||||
0x6f, 0xae, 0x66, 0x75,
|
||||
}
|
||||
)
|
||||
|
||||
func TestIndexerOutOfOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmpPath, err := ioutil.TempDir("", "git2go")
|
||||
checkFatal(t, err)
|
||||
defer os.RemoveAll(tmpPath)
|
||||
|
||||
var finalStats TransferProgress
|
||||
idx, err := NewIndexer(tmpPath, nil, func(stats TransferProgress) ErrorCode {
|
||||
finalStats = stats
|
||||
return ErrorCodeOK
|
||||
})
|
||||
checkFatal(t, err)
|
||||
defer idx.Free()
|
||||
|
||||
_, err = idx.Write(outOfOrderPack)
|
||||
checkFatal(t, err)
|
||||
oid, err := idx.Commit()
|
||||
checkFatal(t, err)
|
||||
|
||||
// The packfile contains the hash as the last 20 bytes.
|
||||
expectedOid := NewOidFromBytes(outOfOrderPack[len(outOfOrderPack)-20:])
|
||||
if !expectedOid.Equal(oid) {
|
||||
t.Errorf("mismatched packfile hash, expected %v, got %v", expectedOid, oid)
|
||||
}
|
||||
if finalStats.TotalObjects != 3 {
|
||||
t.Errorf("mismatched transferred objects, expected 3, got %v", finalStats.TotalObjects)
|
||||
}
|
||||
if finalStats.ReceivedObjects != 3 {
|
||||
t.Errorf("mismatched received objects, expected 3, got %v", finalStats.ReceivedObjects)
|
||||
}
|
||||
if finalStats.IndexedObjects != 3 {
|
||||
t.Errorf("mismatched indexed objects, expected 3, got %v", finalStats.IndexedObjects)
|
||||
}
|
||||
|
||||
odb, err := NewOdb()
|
||||
checkFatal(t, err)
|
||||
defer odb.Free()
|
||||
|
||||
backend, err := NewOdbBackendOnePack(path.Join(tmpPath, fmt.Sprintf("pack-%s.idx", oid.String())))
|
||||
checkFatal(t, err)
|
||||
// Transfer the ownership of the backend to the odb, no freeing needed.
|
||||
err = odb.AddBackend(backend, 1)
|
||||
checkFatal(t, err)
|
||||
|
||||
packfileObjects := 0
|
||||
err = odb.ForEach(func(id *Oid) error {
|
||||
packfileObjects += 1
|
||||
return nil
|
||||
})
|
||||
checkFatal(t, err)
|
||||
if packfileObjects != 3 {
|
||||
t.Errorf("mismatched packfile objects, expected 3, got %v", packfileObjects)
|
||||
}
|
||||
|
||||
// Inspect one of the well-known objects in the packfile.
|
||||
obj, err := odb.Read(NewOidFromBytes([]byte{
|
||||
0x19, 0x10, 0x28, 0x15, 0x66, 0x3d, 0x23, 0xf8, 0xb7, 0x5a, 0x47, 0xe7,
|
||||
0xa0, 0x19, 0x65, 0xdc, 0xdc, 0x96, 0x46, 0x8c,
|
||||
}))
|
||||
checkFatal(t, err)
|
||||
defer obj.Free()
|
||||
if "foo" != string(obj.Data()) {
|
||||
t.Errorf("mismatched packfile object contents, expected foo, got %q", string(obj.Data()))
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@ import (
|
|||
|
||||
// Mempack is a custom ODB backend that permits packing object in-memory.
|
||||
type Mempack struct {
|
||||
doNotCompare
|
||||
ptr *C.git_odb_backend
|
||||
}
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ func TestMempack(t *testing.T) {
|
|||
if err == nil {
|
||||
t.Errorf("object %s unexpectedly found", obj.Id().String())
|
||||
obj.Free()
|
||||
} else if !IsErrorCode(err, ErrNotFound) {
|
||||
} else if !IsErrorCode(err, ErrorCodeNotFound) {
|
||||
t.Errorf("unexpected error %v", err)
|
||||
}
|
||||
}
|
||||
|
|
213
merge.go
213
merge.go
|
@ -17,6 +17,7 @@ import (
|
|||
)
|
||||
|
||||
type AnnotatedCommit struct {
|
||||
doNotCompare
|
||||
ptr *C.git_annotated_commit
|
||||
r *Repository
|
||||
}
|
||||
|
@ -27,6 +28,15 @@ func newAnnotatedCommitFromC(ptr *C.git_annotated_commit, r *Repository) *Annota
|
|||
return mh
|
||||
}
|
||||
|
||||
func (mh *AnnotatedCommit) Id() *Oid {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := newOidFromC(C.git_annotated_commit_id(mh.ptr))
|
||||
runtime.KeepAlive(mh)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (mh *AnnotatedCommit) Free() {
|
||||
runtime.SetFinalizer(mh, nil)
|
||||
C.git_annotated_commit_free(mh.ptr)
|
||||
|
@ -49,7 +59,9 @@ func (r *Repository) AnnotatedCommitFromFetchHead(branchName string, remoteURL s
|
|||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
return newAnnotatedCommitFromC(ptr, r), nil
|
||||
annotatedCommit := newAnnotatedCommitFromC(ptr, r)
|
||||
runtime.KeepAlive(r)
|
||||
return annotatedCommit, nil
|
||||
}
|
||||
|
||||
func (r *Repository) LookupAnnotatedCommit(oid *Oid) (*AnnotatedCommit, error) {
|
||||
|
@ -62,7 +74,10 @@ func (r *Repository) LookupAnnotatedCommit(oid *Oid) (*AnnotatedCommit, error) {
|
|||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return newAnnotatedCommitFromC(ptr, r), nil
|
||||
|
||||
annotatedCommit := newAnnotatedCommitFromC(ptr, r)
|
||||
runtime.KeepAlive(r)
|
||||
return annotatedCommit, nil
|
||||
}
|
||||
|
||||
func (r *Repository) AnnotatedCommitFromRef(ref *Reference) (*AnnotatedCommit, error) {
|
||||
|
@ -76,7 +91,29 @@ func (r *Repository) AnnotatedCommitFromRef(ref *Reference) (*AnnotatedCommit, e
|
|||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return newAnnotatedCommitFromC(ptr, r), nil
|
||||
|
||||
annotatedCommit := newAnnotatedCommitFromC(ptr, r)
|
||||
runtime.KeepAlive(r)
|
||||
return annotatedCommit, nil
|
||||
}
|
||||
|
||||
func (r *Repository) AnnotatedCommitFromRevspec(spec string) (*AnnotatedCommit, error) {
|
||||
crevspec := C.CString(spec)
|
||||
defer C.free(unsafe.Pointer(crevspec))
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var ptr *C.git_annotated_commit
|
||||
ret := C.git_annotated_commit_from_revspec(&ptr, r.ptr, crevspec)
|
||||
runtime.KeepAlive(r)
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
annotatedCommit := newAnnotatedCommitFromC(ptr, r)
|
||||
runtime.KeepAlive(r)
|
||||
return annotatedCommit, nil
|
||||
}
|
||||
|
||||
type MergeTreeFlag int
|
||||
|
@ -90,6 +127,14 @@ const (
|
|||
// continue resolving conflicts. The merge operation will fail with
|
||||
// GIT_EMERGECONFLICT and no index will be returned.
|
||||
MergeTreeFailOnConflict MergeTreeFlag = C.GIT_MERGE_FAIL_ON_CONFLICT
|
||||
// MergeTreeSkipREUC specifies not to write the REUC extension on the
|
||||
// generated index.
|
||||
MergeTreeSkipREUC MergeTreeFlag = C.GIT_MERGE_SKIP_REUC
|
||||
// MergeTreeNoRecursive specifies not to build a recursive merge base (by
|
||||
// merging the multiple merge bases) if the commits being merged have
|
||||
// multiple merge bases. Instead, the first base is used.
|
||||
// This flag provides a similar merge base to `git-merge-resolve`.
|
||||
MergeTreeNoRecursive MergeTreeFlag = C.GIT_MERGE_NO_RECURSIVE
|
||||
)
|
||||
|
||||
type MergeOptions struct {
|
||||
|
@ -98,6 +143,7 @@ type MergeOptions struct {
|
|||
|
||||
RenameThreshold uint
|
||||
TargetLimit uint
|
||||
RecursionLimit uint
|
||||
FileFavor MergeFileFavor
|
||||
|
||||
//TODO: Diff similarity metric
|
||||
|
@ -109,6 +155,7 @@ func mergeOptionsFromC(opts *C.git_merge_options) MergeOptions {
|
|||
TreeFlags: MergeTreeFlag(opts.flags),
|
||||
RenameThreshold: uint(opts.rename_threshold),
|
||||
TargetLimit: uint(opts.target_limit),
|
||||
RecursionLimit: uint(opts.recursion_limit),
|
||||
FileFavor: MergeFileFavor(opts.file_favor),
|
||||
}
|
||||
}
|
||||
|
@ -126,17 +173,20 @@ func DefaultMergeOptions() (MergeOptions, error) {
|
|||
return mergeOptionsFromC(&opts), nil
|
||||
}
|
||||
|
||||
func (mo *MergeOptions) toC() *C.git_merge_options {
|
||||
if mo == nil {
|
||||
func populateMergeOptions(copts *C.git_merge_options, opts *MergeOptions) *C.git_merge_options {
|
||||
C.git_merge_init_options(copts, C.GIT_MERGE_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
return &C.git_merge_options{
|
||||
version: C.uint(mo.Version),
|
||||
flags: C.git_merge_flag_t(mo.TreeFlags),
|
||||
rename_threshold: C.uint(mo.RenameThreshold),
|
||||
target_limit: C.uint(mo.TargetLimit),
|
||||
file_favor: C.git_merge_file_favor_t(mo.FileFavor),
|
||||
}
|
||||
copts.flags = C.git_merge_flag_t(opts.TreeFlags)
|
||||
copts.rename_threshold = C.uint(opts.RenameThreshold)
|
||||
copts.target_limit = C.uint(opts.TargetLimit)
|
||||
copts.recursion_limit = C.uint(opts.RecursionLimit)
|
||||
copts.file_favor = C.git_merge_file_favor_t(opts.FileFavor)
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeMergeOptions(copts *C.git_merge_options) {
|
||||
}
|
||||
|
||||
type MergeFileFavor int
|
||||
|
@ -148,23 +198,28 @@ const (
|
|||
MergeFileFavorUnion MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_UNION
|
||||
)
|
||||
|
||||
func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOptions, checkoutOptions *CheckoutOpts) error {
|
||||
func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOptions, checkoutOptions *CheckoutOptions) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
cMergeOpts := mergeOptions.toC()
|
||||
cCheckoutOpts := checkoutOptions.toC()
|
||||
defer freeCheckoutOpts(cCheckoutOpts)
|
||||
var err error
|
||||
cMergeOpts := populateMergeOptions(&C.git_merge_options{}, mergeOptions)
|
||||
defer freeMergeOptions(cMergeOpts)
|
||||
cCheckoutOptions := populateCheckoutOptions(&C.git_checkout_options{}, checkoutOptions, &err)
|
||||
defer freeCheckoutOptions(cCheckoutOptions)
|
||||
|
||||
gmerge_head_array := make([]*C.git_annotated_commit, len(theirHeads))
|
||||
for i := 0; i < len(theirHeads); i++ {
|
||||
gmerge_head_array[i] = theirHeads[i].ptr
|
||||
}
|
||||
ptr := unsafe.Pointer(&gmerge_head_array[0])
|
||||
err := C.git_merge(r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)), cMergeOpts, cCheckoutOpts)
|
||||
ret := C.git_merge(r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)), cMergeOpts, cCheckoutOptions)
|
||||
runtime.KeepAlive(theirHeads)
|
||||
if err < 0 {
|
||||
return MakeGitError(err)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -214,7 +269,8 @@ func (r *Repository) MergeCommits(ours *Commit, theirs *Commit, options *MergeOp
|
|||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
copts := options.toC()
|
||||
copts := populateMergeOptions(&C.git_merge_options{}, options)
|
||||
defer freeMergeOptions(copts)
|
||||
|
||||
var ptr *C.git_index
|
||||
ret := C.git_merge_commits(&ptr, r.ptr, ours.cast_ptr, theirs.cast_ptr, copts)
|
||||
|
@ -231,7 +287,8 @@ func (r *Repository) MergeTrees(ancestor *Tree, ours *Tree, theirs *Tree, option
|
|||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
copts := options.toC()
|
||||
copts := populateMergeOptions(&C.git_merge_options{}, options)
|
||||
defer freeMergeOptions(copts)
|
||||
|
||||
var ancestor_ptr *C.git_tree
|
||||
if ancestor != nil {
|
||||
|
@ -277,7 +334,7 @@ func (r *Repository) MergeBases(one, two *Oid) ([]*Oid, error) {
|
|||
runtime.KeepAlive(one)
|
||||
runtime.KeepAlive(two)
|
||||
if ret < 0 {
|
||||
return make([]*Oid, 0), MakeGitError(ret)
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
oids := make([]*Oid, coids.count)
|
||||
|
@ -296,15 +353,86 @@ func (r *Repository) MergeBases(one, two *Oid) ([]*Oid, error) {
|
|||
return oids, nil
|
||||
}
|
||||
|
||||
//TODO: int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[]);
|
||||
//TODO: GIT_EXTERN(int) git_merge_base_octopus(git_oid *out,git_repository *repo,size_t length,const git_oid input_array[]);
|
||||
// MergeBaseMany finds a merge base given a list of commits.
|
||||
func (r *Repository) MergeBaseMany(oids []*Oid) (*Oid, error) {
|
||||
coids := make([]C.git_oid, len(oids))
|
||||
for i := 0; i < len(oids); i++ {
|
||||
coids[i] = *oids[i].toC()
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var oid C.git_oid
|
||||
ret := C.git_merge_base_many(&oid, r.ptr, C.size_t(len(oids)), &coids[0])
|
||||
runtime.KeepAlive(r)
|
||||
runtime.KeepAlive(coids)
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return newOidFromC(&oid), nil
|
||||
}
|
||||
|
||||
// MergeBasesMany finds all merge bases given a list of commits.
|
||||
func (r *Repository) MergeBasesMany(oids []*Oid) ([]*Oid, error) {
|
||||
inCoids := make([]C.git_oid, len(oids))
|
||||
for i := 0; i < len(oids); i++ {
|
||||
inCoids[i] = *oids[i].toC()
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var outCoids C.git_oidarray
|
||||
ret := C.git_merge_bases_many(&outCoids, r.ptr, C.size_t(len(oids)), &inCoids[0])
|
||||
runtime.KeepAlive(r)
|
||||
runtime.KeepAlive(inCoids)
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
outOids := make([]*Oid, outCoids.count)
|
||||
hdr := reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(outCoids.ids)),
|
||||
Len: int(outCoids.count),
|
||||
Cap: int(outCoids.count),
|
||||
}
|
||||
goSlice := *(*[]C.git_oid)(unsafe.Pointer(&hdr))
|
||||
|
||||
for i, cid := range goSlice {
|
||||
outOids[i] = newOidFromC(&cid)
|
||||
}
|
||||
|
||||
return outOids, nil
|
||||
}
|
||||
|
||||
// MergeBaseOctopus finds a merge base in preparation for an octopus merge.
|
||||
func (r *Repository) MergeBaseOctopus(oids []*Oid) (*Oid, error) {
|
||||
coids := make([]C.git_oid, len(oids))
|
||||
for i := 0; i < len(oids); i++ {
|
||||
coids[i] = *oids[i].toC()
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var oid C.git_oid
|
||||
ret := C.git_merge_base_octopus(&oid, r.ptr, C.size_t(len(oids)), &coids[0])
|
||||
runtime.KeepAlive(r)
|
||||
runtime.KeepAlive(coids)
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return newOidFromC(&oid), nil
|
||||
}
|
||||
|
||||
type MergeFileResult struct {
|
||||
doNotCompare
|
||||
ptr *C.git_merge_file_result
|
||||
Automergeable bool
|
||||
Path string
|
||||
Mode uint
|
||||
Contents []byte
|
||||
ptr *C.git_merge_file_result
|
||||
}
|
||||
|
||||
func newMergeFileResultFromC(c *C.git_merge_file_result) *MergeFileResult {
|
||||
|
@ -389,19 +517,28 @@ func mergeFileOptionsFromC(c C.git_merge_file_options) MergeFileOptions {
|
|||
}
|
||||
}
|
||||
|
||||
func populateCMergeFileOptions(c *C.git_merge_file_options, options MergeFileOptions) {
|
||||
c.ancestor_label = C.CString(options.AncestorLabel)
|
||||
c.our_label = C.CString(options.OurLabel)
|
||||
c.their_label = C.CString(options.TheirLabel)
|
||||
c.favor = C.git_merge_file_favor_t(options.Favor)
|
||||
c.flags = C.git_merge_file_flag_t(options.Flags)
|
||||
c.marker_size = C.ushort(options.MarkerSize)
|
||||
func populateMergeFileOptions(copts *C.git_merge_file_options, opts *MergeFileOptions) *C.git_merge_file_options {
|
||||
C.git_merge_file_init_options(copts, C.GIT_MERGE_FILE_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
copts.ancestor_label = C.CString(opts.AncestorLabel)
|
||||
copts.our_label = C.CString(opts.OurLabel)
|
||||
copts.their_label = C.CString(opts.TheirLabel)
|
||||
copts.favor = C.git_merge_file_favor_t(opts.Favor)
|
||||
copts.flags = C.git_merge_file_flag_t(opts.Flags)
|
||||
copts.marker_size = C.ushort(opts.MarkerSize)
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeCMergeFileOptions(c *C.git_merge_file_options) {
|
||||
C.free(unsafe.Pointer(c.ancestor_label))
|
||||
C.free(unsafe.Pointer(c.our_label))
|
||||
C.free(unsafe.Pointer(c.their_label))
|
||||
func freeMergeFileOptions(copts *C.git_merge_file_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
C.free(unsafe.Pointer(copts.ancestor_label))
|
||||
C.free(unsafe.Pointer(copts.our_label))
|
||||
C.free(unsafe.Pointer(copts.their_label))
|
||||
}
|
||||
|
||||
func MergeFile(ancestor MergeFileInput, ours MergeFileInput, theirs MergeFileInput, options *MergeFileOptions) (*MergeFileResult, error) {
|
||||
|
@ -434,8 +571,8 @@ func MergeFile(ancestor MergeFileInput, ours MergeFileInput, theirs MergeFileInp
|
|||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
}
|
||||
populateCMergeFileOptions(copts, *options)
|
||||
defer freeCMergeFileOptions(copts)
|
||||
populateMergeFileOptions(copts, options)
|
||||
defer freeMergeFileOptions(copts)
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
|
|
|
@ -5,6 +5,22 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func TestAnnotatedCommitFromRevspec(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
mergeHead, err := repo.AnnotatedCommitFromRevspec("refs/heads/master")
|
||||
checkFatal(t, err)
|
||||
|
||||
expectedId := "473bf778b67b6d53e2ab289e0f1a2e8addef2fc2"
|
||||
if mergeHead.Id().String() != expectedId {
|
||||
t.Errorf("mergeHead.Id() = %v, want %v", mergeHead.Id(), expectedId)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeWithSelf(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
|
@ -18,10 +34,23 @@ func TestMergeWithSelf(t *testing.T) {
|
|||
mergeHead, err := repo.AnnotatedCommitFromRef(master)
|
||||
checkFatal(t, err)
|
||||
|
||||
expectedId := "473bf778b67b6d53e2ab289e0f1a2e8addef2fc2"
|
||||
if mergeHead.Id().String() != expectedId {
|
||||
t.Errorf("mergeHead.Id() = %v, want %v", mergeHead.Id(), expectedId)
|
||||
}
|
||||
|
||||
mergeHeads := make([]*AnnotatedCommit, 1)
|
||||
mergeHeads[0] = mergeHead
|
||||
err = repo.Merge(mergeHeads, nil, nil)
|
||||
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) {
|
||||
|
@ -88,7 +117,7 @@ func TestMergeTreesWithoutAncestor(t *testing.T) {
|
|||
if !index.HasConflicts() {
|
||||
t.Fatal("expected conflicts in the index")
|
||||
}
|
||||
_, err = index.GetConflict("README")
|
||||
_, err = index.Conflict("README")
|
||||
checkFatal(t, err)
|
||||
|
||||
}
|
||||
|
@ -142,6 +171,15 @@ func TestMergeBase(t *testing.T) {
|
|||
if mergeBase.Cmp(commitAId) != 0 {
|
||||
t.Fatalf("unexpected merge base")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeBases(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
commitAId, _ := seedTestRepo(t, repo)
|
||||
commitBId, _ := appendCommit(t, repo)
|
||||
|
||||
mergeBases, err := repo.MergeBases(commitAId, commitBId)
|
||||
checkFatal(t, err)
|
||||
|
@ -155,6 +193,58 @@ func TestMergeBase(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestMergeBaseMany(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
commitAId, _ := seedTestRepo(t, repo)
|
||||
commitBId, _ := appendCommit(t, repo)
|
||||
|
||||
mergeBase, err := repo.MergeBaseMany([]*Oid{commitAId, commitBId})
|
||||
checkFatal(t, err)
|
||||
|
||||
if mergeBase.Cmp(commitAId) != 0 {
|
||||
t.Fatalf("unexpected merge base")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeBasesMany(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
commitAId, _ := seedTestRepo(t, repo)
|
||||
commitBId, _ := appendCommit(t, repo)
|
||||
|
||||
mergeBases, err := repo.MergeBasesMany([]*Oid{commitAId, commitBId})
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(mergeBases) != 1 {
|
||||
t.Fatalf("expected merge bases len to be 1, got %v", len(mergeBases))
|
||||
}
|
||||
|
||||
if mergeBases[0].Cmp(commitAId) != 0 {
|
||||
t.Fatalf("unexpected merge base")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeBaseOctopus(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
commitAId, _ := seedTestRepo(t, repo)
|
||||
commitBId, _ := appendCommit(t, repo)
|
||||
|
||||
mergeBase, err := repo.MergeBaseOctopus([]*Oid{commitAId, commitBId})
|
||||
checkFatal(t, err)
|
||||
|
||||
if mergeBase.Cmp(commitAId) != 0 {
|
||||
t.Fatalf("unexpected merge base")
|
||||
}
|
||||
}
|
||||
|
||||
func compareBytes(t *testing.T, expected, actual []byte) {
|
||||
for i, v := range expected {
|
||||
if actual[i] != v {
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package git
|
||||
|
||||
/*
|
||||
#include <git2.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Trailer represents a single git message trailer.
|
||||
type Trailer struct {
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
|
||||
// MessageTrailers parses trailers out of a message, returning a slice of
|
||||
// Trailer structs. Trailers are key/value pairs in the last paragraph of a
|
||||
// message, not including any patches or conflicts that may be present.
|
||||
func MessageTrailers(message string) ([]Trailer, error) {
|
||||
var trailersC C.git_message_trailer_array
|
||||
|
||||
messageC := C.CString(message)
|
||||
defer C.free(unsafe.Pointer(messageC))
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_message_trailers(&trailersC, messageC)
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
}
|
||||
defer C.git_message_trailer_array_free(&trailersC)
|
||||
trailers := make([]Trailer, trailersC.count)
|
||||
var trailer *C.git_message_trailer
|
||||
for i, p := 0, uintptr(unsafe.Pointer(trailersC.trailers)); i < int(trailersC.count); i, p = i+1, p+unsafe.Sizeof(C.git_message_trailer{}) {
|
||||
trailer = (*C.git_message_trailer)(unsafe.Pointer(p))
|
||||
trailers[i] = Trailer{Key: C.GoString(trailer.key), Value: C.GoString(trailer.value)}
|
||||
}
|
||||
return trailers, nil
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTrailers(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
input string
|
||||
expected []Trailer
|
||||
}{
|
||||
{
|
||||
"commit with zero trailers\n",
|
||||
[]Trailer{},
|
||||
},
|
||||
{
|
||||
"commit with one trailer\n\nCo-authored-by: Alice <alice@example.com>\n",
|
||||
[]Trailer{
|
||||
Trailer{Key: "Co-authored-by", Value: "Alice <alice@example.com>"},
|
||||
},
|
||||
},
|
||||
{
|
||||
"commit with two trailers\n\nCo-authored-by: Alice <alice@example.com>\nSigned-off-by: Bob <bob@example.com>\n",
|
||||
[]Trailer{
|
||||
Trailer{Key: "Co-authored-by", Value: "Alice <alice@example.com>"},
|
||||
Trailer{Key: "Signed-off-by", Value: "Bob <bob@example.com>"}},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
fmt.Printf("%s", test.input)
|
||||
actual, err := MessageTrailers(test.input)
|
||||
if err != nil {
|
||||
t.Errorf("Trailers returned an unexpected error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(test.expected, actual) {
|
||||
t.Errorf("expecting %#v\ngot %#v", test.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
3
note.go
3
note.go
|
@ -13,6 +13,7 @@ import (
|
|||
// This object represents the possible operations which can be
|
||||
// performed on the collection of notes for a repository.
|
||||
type NoteCollection struct {
|
||||
doNotCompare
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
|
@ -139,6 +140,7 @@ func (c *NoteCollection) DefaultRef() (string, error) {
|
|||
|
||||
// Note
|
||||
type Note struct {
|
||||
doNotCompare
|
||||
ptr *C.git_note
|
||||
r *Repository
|
||||
}
|
||||
|
@ -189,6 +191,7 @@ func (n *Note) Message() string {
|
|||
|
||||
// NoteIterator
|
||||
type NoteIterator struct {
|
||||
doNotCompare
|
||||
ptr *C.git_note_iterator
|
||||
r *Repository
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ func TestNoteIterator(t *testing.T) {
|
|||
for {
|
||||
noteId, commitId, err := iter.Next()
|
||||
if err != nil {
|
||||
if !IsErrorCode(err, ErrIterOver) {
|
||||
if !IsErrorCode(err, ErrorCodeIterOver) {
|
||||
checkFatal(t, err)
|
||||
}
|
||||
break
|
||||
|
|
17
object.go
17
object.go
|
@ -22,6 +22,7 @@ const (
|
|||
)
|
||||
|
||||
type Object struct {
|
||||
doNotCompare
|
||||
ptr *C.git_object
|
||||
repo *Repository
|
||||
}
|
||||
|
@ -77,14 +78,14 @@ func (o *Object) Type() ObjectType {
|
|||
return ret
|
||||
}
|
||||
|
||||
// Owner returns a weak reference to the repository which owns this
|
||||
// object. This won't keep the underlying repository alive.
|
||||
// Owner returns a weak reference to the repository which owns this object.
|
||||
// This won't keep the underlying repository alive, but it should still be
|
||||
// Freed.
|
||||
func (o *Object) Owner() *Repository {
|
||||
ret := &Repository{
|
||||
ptr: C.git_object_owner(o.ptr),
|
||||
}
|
||||
repo := newRepositoryFromC(C.git_object_owner(o.ptr))
|
||||
runtime.KeepAlive(o)
|
||||
return ret
|
||||
repo.weak = true
|
||||
return repo
|
||||
}
|
||||
|
||||
func dupObject(obj *Object, kind ObjectType) (*C.git_object, error) {
|
||||
|
@ -201,13 +202,13 @@ func (o *Object) Free() {
|
|||
|
||||
// Peel recursively peels an object until an object of the specified type is met.
|
||||
//
|
||||
// If the query cannot be satisfied due to the object model, ErrInvalidSpec
|
||||
// If the query cannot be satisfied due to the object model, ErrorCodeInvalidSpec
|
||||
// will be returned (e.g. trying to peel a blob to a tree).
|
||||
//
|
||||
// If you pass ObjectAny as the target type, then the object will be peeled
|
||||
// until the type changes. A tag will be peeled until the referenced object
|
||||
// is no longer a tag, and a commit will be peeled to a tree. Any other object
|
||||
// type will return ErrInvalidSpec.
|
||||
// type will return ErrorCodeInvalidSpec.
|
||||
//
|
||||
// If peeling a tag we discover an object which cannot be peeled to the target
|
||||
// type due to the object model, an error will be returned.
|
||||
|
|
|
@ -153,8 +153,8 @@ func TestObjectPeel(t *testing.T) {
|
|||
|
||||
obj, err = commit.Peel(ObjectTag)
|
||||
|
||||
if !IsErrorCode(err, ErrInvalidSpec) {
|
||||
t.Fatalf("Wrong error when peeling a commit to a tag, expected ErrInvalidSpec, have %v", err)
|
||||
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
|
||||
t.Fatalf("Wrong error when peeling a commit to a tag, expected ErrorCodeInvalidSpec, have %v", err)
|
||||
}
|
||||
|
||||
tree, err := repo.LookupTree(treeID)
|
||||
|
@ -162,8 +162,8 @@ func TestObjectPeel(t *testing.T) {
|
|||
|
||||
obj, err = tree.Peel(ObjectAny)
|
||||
|
||||
if !IsErrorCode(err, ErrInvalidSpec) {
|
||||
t.Fatalf("Wrong error when peeling a tree, expected ErrInvalidSpec, have %v", err)
|
||||
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
|
||||
t.Fatalf("Wrong error when peeling a tree, expected ErrorCodeInvalidSpec, have %v", err)
|
||||
}
|
||||
|
||||
entry := tree.EntryByName("README")
|
||||
|
@ -173,8 +173,8 @@ func TestObjectPeel(t *testing.T) {
|
|||
|
||||
obj, err = blob.Peel(ObjectAny)
|
||||
|
||||
if !IsErrorCode(err, ErrInvalidSpec) {
|
||||
t.Fatalf("Wrong error when peeling a blob, expected ErrInvalidSpec, have %v", err)
|
||||
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
|
||||
t.Fatalf("Wrong error when peeling a blob, expected ErrorCodeInvalidSpec, have %v", err)
|
||||
}
|
||||
|
||||
tagID := createTestTag(t, repo, commit)
|
||||
|
|
166
odb.go
166
odb.go
|
@ -3,22 +3,31 @@ package git
|
|||
/*
|
||||
#include <git2.h>
|
||||
|
||||
extern int git_odb_backend_one_pack(git_odb_backend **out, const char *index_file);
|
||||
extern int git_odb_backend_loose(git_odb_backend **out, const char *objects_dir, int compression_level, int do_fsync, unsigned int dir_mode, unsigned int file_mode);
|
||||
extern int _go_git_odb_foreach(git_odb *db, void *payload);
|
||||
extern void _go_git_odb_backend_free(git_odb_backend *backend);
|
||||
extern int _go_git_odb_write_pack(git_odb_writepack **out, git_odb *db, void *progress_payload);
|
||||
extern int _go_git_odb_writepack_append(git_odb_writepack *writepack, const void *, size_t, git_transfer_progress *);
|
||||
extern int _go_git_odb_writepack_commit(git_odb_writepack *writepack, git_transfer_progress *);
|
||||
extern void _go_git_odb_writepack_free(git_odb_writepack *writepack);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Odb struct {
|
||||
doNotCompare
|
||||
ptr *C.git_odb
|
||||
}
|
||||
|
||||
type OdbBackend struct {
|
||||
doNotCompare
|
||||
ptr *C.git_odb_backend
|
||||
}
|
||||
|
||||
|
@ -38,12 +47,24 @@ func NewOdb() (odb *Odb, err error) {
|
|||
}
|
||||
|
||||
func NewOdbBackendFromC(ptr unsafe.Pointer) (backend *OdbBackend) {
|
||||
backend = &OdbBackend{(*C.git_odb_backend)(ptr)}
|
||||
backend = &OdbBackend{ptr: (*C.git_odb_backend)(ptr)}
|
||||
return backend
|
||||
}
|
||||
|
||||
func (v *Odb) AddBackend(backend *OdbBackend, priority int) (err error) {
|
||||
func (v *Odb) AddAlternate(backend *OdbBackend, priority int) (err error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_odb_add_alternate(v.ptr, backend.ptr, C.int(priority))
|
||||
runtime.KeepAlive(v)
|
||||
if ret < 0 {
|
||||
backend.Free()
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Odb) AddBackend(backend *OdbBackend, priority int) (err error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
|
@ -56,6 +77,42 @@ func (v *Odb) AddBackend(backend *OdbBackend, priority int) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
func NewOdbBackendOnePack(packfileIndexPath string) (backend *OdbBackend, err error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
cstr := C.CString(packfileIndexPath)
|
||||
defer C.free(unsafe.Pointer(cstr))
|
||||
|
||||
var odbOnePack *C.git_odb_backend = nil
|
||||
ret := C.git_odb_backend_one_pack(&odbOnePack, cstr)
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return NewOdbBackendFromC(unsafe.Pointer(odbOnePack)), nil
|
||||
}
|
||||
|
||||
// NewOdbBackendLoose creates a backend for loose objects.
|
||||
func NewOdbBackendLoose(objectsDir string, compressionLevel int, doFsync bool, dirMode os.FileMode, fileMode os.FileMode) (backend *OdbBackend, err error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var odbLoose *C.git_odb_backend = nil
|
||||
var doFsyncInt C.int
|
||||
if doFsync {
|
||||
doFsyncInt = C.int(1)
|
||||
}
|
||||
|
||||
cstr := C.CString(objectsDir)
|
||||
defer C.free(unsafe.Pointer(cstr))
|
||||
|
||||
ret := C.git_odb_backend_loose(&odbLoose, cstr, C.int(compressionLevel), doFsyncInt, C.uint(dirMode), C.uint(fileMode))
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return NewOdbBackendFromC(unsafe.Pointer(odbLoose)), nil
|
||||
}
|
||||
|
||||
func (v *Odb) ReadHeader(oid *Oid) (uint64, ObjectType, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
@ -120,35 +177,33 @@ func (v *Odb) Read(oid *Oid) (obj *OdbObject, err error) {
|
|||
}
|
||||
|
||||
type OdbForEachCallback func(id *Oid) error
|
||||
|
||||
type foreachData struct {
|
||||
callback OdbForEachCallback
|
||||
err error
|
||||
type odbForEachCallbackData struct {
|
||||
callback OdbForEachCallback
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
//export odbForEachCb
|
||||
func odbForEachCb(id *C.git_oid, handle unsafe.Pointer) int {
|
||||
data, ok := pointerHandles.Get(handle).(*foreachData)
|
||||
|
||||
//export odbForEachCallback
|
||||
func odbForEachCallback(id *C.git_oid, handle unsafe.Pointer) C.int {
|
||||
data, ok := pointerHandles.Get(handle).(*odbForEachCallbackData)
|
||||
if !ok {
|
||||
panic("could not retrieve handle")
|
||||
}
|
||||
|
||||
err := data.callback(newOidFromC(id))
|
||||
if err != nil {
|
||||
data.err = err
|
||||
return C.GIT_EUSER
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
|
||||
return 0
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
func (v *Odb) ForEach(callback OdbForEachCallback) error {
|
||||
data := foreachData{
|
||||
callback: callback,
|
||||
err: nil,
|
||||
var err error
|
||||
data := odbForEachCallbackData{
|
||||
callback: callback,
|
||||
errorTarget: &err,
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
|
@ -157,9 +212,10 @@ func (v *Odb) ForEach(callback OdbForEachCallback) error {
|
|||
|
||||
ret := C._go_git_odb_foreach(v.ptr, handle)
|
||||
runtime.KeepAlive(v)
|
||||
if ret == C.GIT_EUSER {
|
||||
return data.err
|
||||
} else if ret < 0 {
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
|
@ -231,11 +287,35 @@ func (v *Odb) NewWriteStream(size int64, otype ObjectType) (*OdbWriteStream, err
|
|||
return stream, nil
|
||||
}
|
||||
|
||||
// NewWritePack opens a stream for writing a pack file to the ODB. If the ODB
|
||||
// layer understands pack files, then the given packfile will likely be
|
||||
// streamed directly to disk (and a corresponding index created). If the ODB
|
||||
// layer does not understand pack files, the objects will be stored in whatever
|
||||
// format the ODB layer uses.
|
||||
func (v *Odb) NewWritePack(callback TransferProgressCallback) (*OdbWritepack, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
writepack := new(OdbWritepack)
|
||||
populateRemoteCallbacks(&writepack.ccallbacks, &RemoteCallbacks{TransferProgressCallback: callback}, nil)
|
||||
|
||||
ret := C._go_git_odb_write_pack(&writepack.ptr, v.ptr, writepack.ccallbacks.payload)
|
||||
runtime.KeepAlive(v)
|
||||
if ret < 0 {
|
||||
untrackCallbacksPayload(&writepack.ccallbacks)
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
runtime.SetFinalizer(writepack, (*OdbWritepack).Free)
|
||||
return writepack, nil
|
||||
}
|
||||
|
||||
func (v *OdbBackend) Free() {
|
||||
C._go_git_odb_backend_free(v.ptr)
|
||||
}
|
||||
|
||||
type OdbObject struct {
|
||||
doNotCompare
|
||||
ptr *C.git_odb_object
|
||||
}
|
||||
|
||||
|
@ -279,6 +359,7 @@ func (object *OdbObject) Data() (data []byte) {
|
|||
}
|
||||
|
||||
type OdbReadStream struct {
|
||||
doNotCompare
|
||||
ptr *C.git_odb_stream
|
||||
Size uint64
|
||||
Type ObjectType
|
||||
|
@ -319,6 +400,7 @@ func (stream *OdbReadStream) Free() {
|
|||
}
|
||||
|
||||
type OdbWriteStream struct {
|
||||
doNotCompare
|
||||
ptr *C.git_odb_stream
|
||||
Id Oid
|
||||
}
|
||||
|
@ -360,3 +442,47 @@ func (stream *OdbWriteStream) Free() {
|
|||
runtime.SetFinalizer(stream, nil)
|
||||
C.git_odb_stream_free(stream.ptr)
|
||||
}
|
||||
|
||||
// OdbWritepack is a stream to write a packfile to the ODB.
|
||||
type OdbWritepack struct {
|
||||
doNotCompare
|
||||
ptr *C.git_odb_writepack
|
||||
stats C.git_transfer_progress
|
||||
ccallbacks C.git_remote_callbacks
|
||||
}
|
||||
|
||||
func (writepack *OdbWritepack) Write(data []byte) (int, error) {
|
||||
header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
||||
ptr := unsafe.Pointer(header.Data)
|
||||
size := C.size_t(header.Len)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C._go_git_odb_writepack_append(writepack.ptr, ptr, size, &writepack.stats)
|
||||
runtime.KeepAlive(writepack)
|
||||
if ret < 0 {
|
||||
return 0, MakeGitError(ret)
|
||||
}
|
||||
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func (writepack *OdbWritepack) Commit() error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C._go_git_odb_writepack_commit(writepack.ptr, &writepack.stats)
|
||||
runtime.KeepAlive(writepack)
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (writepack *OdbWritepack) Free() {
|
||||
untrackCallbacksPayload(&writepack.ccallbacks)
|
||||
runtime.SetFinalizer(writepack, nil)
|
||||
C._go_git_odb_writepack_free(writepack.ptr)
|
||||
}
|
||||
|
|
119
odb_test.go
119
odb_test.go
|
@ -3,8 +3,11 @@ package git
|
|||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -58,31 +61,31 @@ func TestOdbStream(t *testing.T) {
|
|||
|
||||
_, _ = seedTestRepo(t, repo)
|
||||
|
||||
odb, error := repo.Odb()
|
||||
checkFatal(t, error)
|
||||
odb, err := repo.Odb()
|
||||
checkFatal(t, err)
|
||||
|
||||
str := "hello, world!"
|
||||
|
||||
writeStream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob)
|
||||
checkFatal(t, error)
|
||||
n, error := io.WriteString(writeStream, str)
|
||||
checkFatal(t, error)
|
||||
writeStream, err := odb.NewWriteStream(int64(len(str)), ObjectBlob)
|
||||
checkFatal(t, err)
|
||||
n, err := io.WriteString(writeStream, str)
|
||||
checkFatal(t, err)
|
||||
if n != len(str) {
|
||||
t.Fatalf("Bad write length %v != %v", n, len(str))
|
||||
}
|
||||
|
||||
error = writeStream.Close()
|
||||
checkFatal(t, error)
|
||||
err = writeStream.Close()
|
||||
checkFatal(t, err)
|
||||
|
||||
expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
|
||||
checkFatal(t, error)
|
||||
expectedId, err := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
|
||||
checkFatal(t, err)
|
||||
if writeStream.Id.Cmp(expectedId) != 0 {
|
||||
t.Fatal("Wrong data written")
|
||||
}
|
||||
|
||||
readStream, error := odb.NewReadStream(&writeStream.Id)
|
||||
checkFatal(t, error)
|
||||
data, error := ioutil.ReadAll(readStream)
|
||||
readStream, err := odb.NewReadStream(&writeStream.Id)
|
||||
checkFatal(t, err)
|
||||
data, err := ioutil.ReadAll(readStream)
|
||||
if str != string(data) {
|
||||
t.Fatalf("Wrong data read %v != %v", str, string(data))
|
||||
}
|
||||
|
@ -95,8 +98,8 @@ func TestOdbHash(t *testing.T) {
|
|||
|
||||
_, _ = seedTestRepo(t, repo)
|
||||
|
||||
odb, error := repo.Odb()
|
||||
checkFatal(t, error)
|
||||
odb, err := repo.Odb()
|
||||
checkFatal(t, err)
|
||||
|
||||
str := `tree 115fcae49287c82eb55bb275cbbd4556fbed72b7
|
||||
parent 66e1c476199ebcd3e304659992233132c5a52c6c
|
||||
|
@ -106,11 +109,11 @@ committer John Doe <john@doe.com> 1390682018 +0000
|
|||
Initial commit.`
|
||||
|
||||
for _, data := range [][]byte{[]byte(str), doublePointerBytes()} {
|
||||
oid, error := odb.Hash(data, ObjectCommit)
|
||||
checkFatal(t, error)
|
||||
oid, err := odb.Hash(data, ObjectCommit)
|
||||
checkFatal(t, err)
|
||||
|
||||
coid, error := odb.Write(data, ObjectCommit)
|
||||
checkFatal(t, error)
|
||||
coid, err := odb.Write(data, ObjectCommit)
|
||||
checkFatal(t, err)
|
||||
|
||||
if oid.Cmp(coid) != 0 {
|
||||
t.Fatal("Hash and write Oids are different")
|
||||
|
@ -152,3 +155,81 @@ func TestOdbForeach(t *testing.T) {
|
|||
t.Fatalf("Odb.ForEach() did not return the expected error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOdbWritepack(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
_, _ = seedTestRepo(t, repo)
|
||||
|
||||
odb, err := repo.Odb()
|
||||
checkFatal(t, err)
|
||||
|
||||
var finalStats TransferProgress
|
||||
writepack, err := odb.NewWritePack(func(stats TransferProgress) ErrorCode {
|
||||
finalStats = stats
|
||||
return ErrorCodeOK
|
||||
})
|
||||
checkFatal(t, err)
|
||||
defer writepack.Free()
|
||||
|
||||
_, err = writepack.Write(outOfOrderPack)
|
||||
checkFatal(t, err)
|
||||
err = writepack.Commit()
|
||||
checkFatal(t, err)
|
||||
|
||||
if finalStats.TotalObjects != 3 {
|
||||
t.Errorf("mismatched transferred objects, expected 3, got %v", finalStats.TotalObjects)
|
||||
}
|
||||
if finalStats.ReceivedObjects != 3 {
|
||||
t.Errorf("mismatched received objects, expected 3, got %v", finalStats.ReceivedObjects)
|
||||
}
|
||||
if finalStats.IndexedObjects != 3 {
|
||||
t.Errorf("mismatched indexed objects, expected 3, got %v", finalStats.IndexedObjects)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOdbBackendLoose(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
_, _ = seedTestRepo(t, repo)
|
||||
|
||||
odb, err := repo.Odb()
|
||||
checkFatal(t, err)
|
||||
|
||||
looseObjectsDir, err := ioutil.TempDir("", fmt.Sprintf("loose_objects_%s", path.Base(repo.Path())))
|
||||
checkFatal(t, err)
|
||||
defer os.RemoveAll(looseObjectsDir)
|
||||
|
||||
looseObjectsBackend, err := NewOdbBackendLoose(looseObjectsDir, -1, false, 0, 0)
|
||||
checkFatal(t, err)
|
||||
if err := odb.AddBackend(looseObjectsBackend, 999); err != nil {
|
||||
looseObjectsBackend.Free()
|
||||
checkFatal(t, err)
|
||||
}
|
||||
|
||||
str := "hello, world!"
|
||||
|
||||
writeStream, err := odb.NewWriteStream(int64(len(str)), ObjectBlob)
|
||||
checkFatal(t, err)
|
||||
n, err := io.WriteString(writeStream, str)
|
||||
checkFatal(t, err)
|
||||
if n != len(str) {
|
||||
t.Fatalf("Bad write length %v != %v", n, len(str))
|
||||
}
|
||||
|
||||
err = writeStream.Close()
|
||||
checkFatal(t, err)
|
||||
|
||||
expectedId, err := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
|
||||
checkFatal(t, err)
|
||||
if !writeStream.Id.Equal(expectedId) {
|
||||
t.Fatalf("writeStream.id = %v; want %v", writeStream.Id, expectedId)
|
||||
}
|
||||
|
||||
_, err = os.Stat(path.Join(looseObjectsDir, expectedId.String()[:2], expectedId.String()[2:]))
|
||||
checkFatal(t, err)
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
)
|
||||
|
||||
type Packbuilder struct {
|
||||
doNotCompare
|
||||
ptr *C.git_packbuilder
|
||||
r *Repository
|
||||
}
|
||||
|
@ -133,15 +134,15 @@ func (pb *Packbuilder) Written() uint32 {
|
|||
}
|
||||
|
||||
type PackbuilderForeachCallback func([]byte) error
|
||||
type packbuilderCbData struct {
|
||||
callback PackbuilderForeachCallback
|
||||
err error
|
||||
type packbuilderCallbackData struct {
|
||||
callback PackbuilderForeachCallback
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
//export packbuilderForEachCb
|
||||
func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, handle unsafe.Pointer) int {
|
||||
//export packbuilderForEachCallback
|
||||
func packbuilderForEachCallback(buf unsafe.Pointer, size C.size_t, handle unsafe.Pointer) C.int {
|
||||
payload := pointerHandles.Get(handle)
|
||||
data, ok := payload.(*packbuilderCbData)
|
||||
data, ok := payload.(*packbuilderCallbackData)
|
||||
if !ok {
|
||||
panic("could not get packbuilder CB data")
|
||||
}
|
||||
|
@ -150,19 +151,20 @@ func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, handle unsafe.Point
|
|||
|
||||
err := data.callback(slice)
|
||||
if err != nil {
|
||||
data.err = err
|
||||
return C.GIT_EUSER
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
|
||||
return 0
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
// ForEach repeatedly calls the callback with new packfile data until
|
||||
// there is no more data or the callback returns an error
|
||||
func (pb *Packbuilder) ForEach(callback PackbuilderForeachCallback) error {
|
||||
data := packbuilderCbData{
|
||||
callback: callback,
|
||||
err: nil,
|
||||
var err error
|
||||
data := packbuilderCallbackData{
|
||||
callback: callback,
|
||||
errorTarget: &err,
|
||||
}
|
||||
handle := pointerHandles.Track(&data)
|
||||
defer pointerHandles.Untrack(handle)
|
||||
|
@ -170,13 +172,13 @@ func (pb *Packbuilder) ForEach(callback PackbuilderForeachCallback) error {
|
|||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
err := C._go_git_packbuilder_foreach(pb.ptr, handle)
|
||||
ret := C._go_git_packbuilder_foreach(pb.ptr, handle)
|
||||
runtime.KeepAlive(pb)
|
||||
if err == C.GIT_EUSER {
|
||||
return data.err
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if err < 0 {
|
||||
return MakeGitError(err)
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
14
patch.go
14
patch.go
|
@ -10,6 +10,7 @@ import (
|
|||
)
|
||||
|
||||
type Patch struct {
|
||||
doNotCompare
|
||||
ptr *C.git_patch
|
||||
}
|
||||
|
||||
|
@ -77,17 +78,22 @@ func (v *Repository) PatchFromBuffers(oldPath, newPath string, oldBuf, newBuf []
|
|||
cNewPath := C.CString(newPath)
|
||||
defer C.free(unsafe.Pointer(cNewPath))
|
||||
|
||||
copts, _ := diffOptionsToC(opts)
|
||||
var err error
|
||||
copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
|
||||
defer freeDiffOptions(copts)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_patch_from_buffers(&patchPtr, oldPtr, C.size_t(len(oldBuf)), cOldPath, newPtr, C.size_t(len(newBuf)), cNewPath, copts)
|
||||
ret := C.git_patch_from_buffers(&patchPtr, oldPtr, C.size_t(len(oldBuf)), cOldPath, newPtr, C.size_t(len(newBuf)), cNewPath, copts)
|
||||
runtime.KeepAlive(oldBuf)
|
||||
runtime.KeepAlive(newBuf)
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
return newPatchFromC(patchPtr), nil
|
||||
}
|
||||
|
|
|
@ -14,15 +14,18 @@ func TestRemotePush(t *testing.T) {
|
|||
|
||||
remote, err := localRepo.Remotes.Create("test_push", repo.Path())
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
seedTestRepo(t, localRepo)
|
||||
|
||||
err = remote.Push([]string{"refs/heads/master"}, nil)
|
||||
checkFatal(t, err)
|
||||
|
||||
_, err = localRepo.References.Lookup("refs/remotes/test_push/master")
|
||||
ref, err := localRepo.References.Lookup("refs/remotes/test_push/master")
|
||||
checkFatal(t, err)
|
||||
defer ref.Free()
|
||||
|
||||
_, err = repo.References.Lookup("refs/heads/master")
|
||||
ref, err = repo.References.Lookup("refs/heads/master")
|
||||
checkFatal(t, err)
|
||||
defer ref.Free()
|
||||
}
|
||||
|
|
103
rebase.go
103
rebase.go
|
@ -76,7 +76,12 @@ type RebaseOptions struct {
|
|||
InMemory int
|
||||
RewriteNotesRef string
|
||||
MergeOptions MergeOptions
|
||||
CheckoutOptions CheckoutOpts
|
||||
CheckoutOptions CheckoutOptions
|
||||
}
|
||||
|
||||
type rebaseOptionsData struct {
|
||||
options *RebaseOptions
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
// DefaultRebaseOptions returns a RebaseOptions with default values.
|
||||
|
@ -104,18 +109,28 @@ func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions {
|
|||
}
|
||||
}
|
||||
|
||||
func (ro *RebaseOptions) toC() *C.git_rebase_options {
|
||||
if ro == nil {
|
||||
func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, errorTarget *error) *C.git_rebase_options {
|
||||
C.git_rebase_init_options(copts, C.GIT_REBASE_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
return &C.git_rebase_options{
|
||||
version: C.uint(ro.Version),
|
||||
quiet: C.int(ro.Quiet),
|
||||
inmemory: C.int(ro.InMemory),
|
||||
rewrite_notes_ref: mapEmptyStringToNull(ro.RewriteNotesRef),
|
||||
merge_options: *ro.MergeOptions.toC(),
|
||||
checkout_options: *ro.CheckoutOptions.toC(),
|
||||
|
||||
copts.quiet = C.int(opts.Quiet)
|
||||
copts.inmemory = C.int(opts.InMemory)
|
||||
copts.rewrite_notes_ref = mapEmptyStringToNull(opts.RewriteNotesRef)
|
||||
populateMergeOptions(&copts.merge_options, &opts.MergeOptions)
|
||||
populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget)
|
||||
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeRebaseOptions(copts *C.git_rebase_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
C.free(unsafe.Pointer(copts.rewrite_notes_ref))
|
||||
freeMergeOptions(&copts.merge_options)
|
||||
freeCheckoutOptions(&copts.checkout_options)
|
||||
}
|
||||
|
||||
func mapEmptyStringToNull(ref string) *C.char {
|
||||
|
@ -127,8 +142,10 @@ func mapEmptyStringToNull(ref string) *C.char {
|
|||
|
||||
// Rebase is the struct representing a Rebase object.
|
||||
type Rebase struct {
|
||||
ptr *C.git_rebase
|
||||
r *Repository
|
||||
doNotCompare
|
||||
ptr *C.git_rebase
|
||||
r *Repository
|
||||
options *C.git_rebase_options
|
||||
}
|
||||
|
||||
// InitRebase initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch.
|
||||
|
@ -149,15 +166,22 @@ func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedComm
|
|||
}
|
||||
|
||||
var ptr *C.git_rebase
|
||||
err := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, opts.toC())
|
||||
var err error
|
||||
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err)
|
||||
ret := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, cOpts)
|
||||
runtime.KeepAlive(branch)
|
||||
runtime.KeepAlive(upstream)
|
||||
runtime.KeepAlive(onto)
|
||||
if err < 0 {
|
||||
return nil, MakeGitError(err)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
freeRebaseOptions(cOpts)
|
||||
return nil, err
|
||||
}
|
||||
if ret < 0 {
|
||||
freeRebaseOptions(cOpts)
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
return newRebaseFromC(ptr), nil
|
||||
return newRebaseFromC(ptr, r, cOpts), nil
|
||||
}
|
||||
|
||||
// OpenRebase opens an existing rebase that was previously started by either an invocation of InitRebase or by another client.
|
||||
|
@ -166,13 +190,20 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) {
|
|||
defer runtime.UnlockOSThread()
|
||||
|
||||
var ptr *C.git_rebase
|
||||
err := C.git_rebase_open(&ptr, r.ptr, opts.toC())
|
||||
var err error
|
||||
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err)
|
||||
ret := C.git_rebase_open(&ptr, r.ptr, cOpts)
|
||||
runtime.KeepAlive(r)
|
||||
if err < 0 {
|
||||
return nil, MakeGitError(err)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
freeRebaseOptions(cOpts)
|
||||
return nil, err
|
||||
}
|
||||
if ret < 0 {
|
||||
freeRebaseOptions(cOpts)
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
return newRebaseFromC(ptr), nil
|
||||
return newRebaseFromC(ptr, r, cOpts), nil
|
||||
}
|
||||
|
||||
// OperationAt gets the rebase operation specified by the given index.
|
||||
|
@ -224,6 +255,27 @@ func (rebase *Rebase) Next() (*RebaseOperation, error) {
|
|||
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.
|
||||
// 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 {
|
||||
|
@ -283,13 +335,14 @@ func (rebase *Rebase) Abort() error {
|
|||
}
|
||||
|
||||
// Free frees the Rebase object.
|
||||
func (rebase *Rebase) Free() {
|
||||
runtime.SetFinalizer(rebase, nil)
|
||||
C.git_rebase_free(rebase.ptr)
|
||||
func (r *Rebase) Free() {
|
||||
runtime.SetFinalizer(r, nil)
|
||||
C.git_rebase_free(r.ptr)
|
||||
freeRebaseOptions(r.options)
|
||||
}
|
||||
|
||||
func newRebaseFromC(ptr *C.git_rebase) *Rebase {
|
||||
rebase := &Rebase{ptr: ptr}
|
||||
func newRebaseFromC(ptr *C.git_rebase, repo *Repository, opts *C.git_rebase_options) *Rebase {
|
||||
rebase := &Rebase{ptr: ptr, r: repo, options: opts}
|
||||
runtime.SetFinalizer(rebase, (*Rebase).Free)
|
||||
return rebase
|
||||
}
|
||||
|
|
133
rebase_test.go
133
rebase_test.go
|
@ -9,6 +9,80 @@ import (
|
|||
|
||||
// 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) {
|
||||
// TEST DATA
|
||||
|
||||
|
@ -33,12 +107,12 @@ func TestRebaseAbort(t *testing.T) {
|
|||
seedTestRepo(t, repo)
|
||||
|
||||
// Setup a repo with 2 branches and a different tree
|
||||
err := setupRepoForRebase(repo, masterCommit, branchName)
|
||||
err := setupRepoForRebase(repo, masterCommit, branchName, commitOptions{})
|
||||
checkFatal(t, err)
|
||||
|
||||
// Create several commits in emile
|
||||
for _, commit := range emileCommits {
|
||||
_, err = commitSomething(repo, commit, commit)
|
||||
_, err = commitSomething(repo, commit, commit, commitOptions{})
|
||||
checkFatal(t, err)
|
||||
}
|
||||
|
||||
|
@ -48,7 +122,7 @@ func TestRebaseAbort(t *testing.T) {
|
|||
assertStringList(t, expectedHistory, actualHistory)
|
||||
|
||||
// Rebase onto master
|
||||
rebase, err := performRebaseOnto(repo, "master")
|
||||
rebase, err := performRebaseOnto(repo, "master", nil)
|
||||
checkFatal(t, err)
|
||||
defer rebase.Free()
|
||||
|
||||
|
@ -94,17 +168,17 @@ func TestRebaseNoConflicts(t *testing.T) {
|
|||
}
|
||||
|
||||
// Setup a repo with 2 branches and a different tree
|
||||
err = setupRepoForRebase(repo, masterCommit, branchName)
|
||||
err = setupRepoForRebase(repo, masterCommit, branchName, commitOptions{})
|
||||
checkFatal(t, err)
|
||||
|
||||
// Create several commits in emile
|
||||
for _, commit := range emileCommits {
|
||||
_, err = commitSomething(repo, commit, commit)
|
||||
_, err = commitSomething(repo, commit, commit, commitOptions{})
|
||||
checkFatal(t, err)
|
||||
}
|
||||
|
||||
// Rebase onto master
|
||||
rebase, err := performRebaseOnto(repo, "master")
|
||||
rebase, err := performRebaseOnto(repo, "master", nil)
|
||||
checkFatal(t, err)
|
||||
defer rebase.Free()
|
||||
|
||||
|
@ -130,11 +204,10 @@ func TestRebaseNoConflicts(t *testing.T) {
|
|||
actualHistory, err := commitMsgsList(repo)
|
||||
checkFatal(t, err)
|
||||
assertStringList(t, expectedHistory, actualHistory)
|
||||
|
||||
}
|
||||
|
||||
// Utils
|
||||
func setupRepoForRebase(repo *Repository, masterCommit, branchName string) error {
|
||||
func setupRepoForRebase(repo *Repository, masterCommit, branchName string, commitOpts commitOptions) error {
|
||||
// Create a new branch from master
|
||||
err := createBranch(repo, branchName)
|
||||
if err != nil {
|
||||
|
@ -142,7 +215,7 @@ func setupRepoForRebase(repo *Repository, masterCommit, branchName string) error
|
|||
}
|
||||
|
||||
// Create a commit in master
|
||||
_, err = commitSomething(repo, masterCommit, masterCommit)
|
||||
_, err = commitSomething(repo, masterCommit, masterCommit, commitOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -161,7 +234,7 @@ func setupRepoForRebase(repo *Repository, masterCommit, branchName string) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) {
|
||||
func performRebaseOnto(repo *Repository, branch string, rebaseOpts *RebaseOptions) (*Rebase, error) {
|
||||
master, err := repo.LookupBranch(branch, BranchLocal)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -175,7 +248,7 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) {
|
|||
defer onto.Free()
|
||||
|
||||
// Init rebase
|
||||
rebase, err := repo.InitRebase(nil, nil, onto, nil)
|
||||
rebase, err := repo.InitRebase(nil, nil, onto, rebaseOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -276,7 +349,7 @@ func headTree(repo *Repository) (*Tree, error) {
|
|||
return tree, nil
|
||||
}
|
||||
|
||||
func commitSomething(repo *Repository, something, content string) (*Oid, error) {
|
||||
func commitSomething(repo *Repository, something, content string, commitOpts commitOptions) (*Oid, error) {
|
||||
headCommit, err := headCommit(repo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -315,18 +388,44 @@ func commitSomething(repo *Repository, something, content string) (*Oid, error)
|
|||
}
|
||||
defer newTree.Free()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commit, err := repo.CreateCommit("HEAD", signature(), signature(), "Test rebase, Baby! "+something, newTree, headCommit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
opts := &CheckoutOpts{
|
||||
if commitOpts.CommitSigningCallback != nil {
|
||||
commit, err := repo.LookupCommit(commit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
oid, err := commit.WithSignatureUsing(commitOpts.CommitSigningCallback)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newCommit, err := repo.LookupCommit(oid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
head, err := repo.Head()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = repo.References.Create(
|
||||
head.Name(),
|
||||
newCommit.Id(),
|
||||
true,
|
||||
"repoint to signed commit",
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
checkoutOpts := &CheckoutOptions{
|
||||
Strategy: CheckoutRemoveUntracked | CheckoutForce,
|
||||
}
|
||||
err = repo.CheckoutIndex(index, opts)
|
||||
err = repo.CheckoutIndex(index, checkoutOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
4
refdb.go
4
refdb.go
|
@ -13,11 +13,13 @@ import (
|
|||
)
|
||||
|
||||
type Refdb struct {
|
||||
doNotCompare
|
||||
ptr *C.git_refdb
|
||||
r *Repository
|
||||
}
|
||||
|
||||
type RefdbBackend struct {
|
||||
doNotCompare
|
||||
ptr *C.git_refdb_backend
|
||||
}
|
||||
|
||||
|
@ -38,7 +40,7 @@ func (v *Repository) NewRefdb() (refdb *Refdb, err error) {
|
|||
}
|
||||
|
||||
func NewRefdbBackendFromC(ptr unsafe.Pointer) (backend *RefdbBackend) {
|
||||
backend = &RefdbBackend{(*C.git_refdb_backend)(ptr)}
|
||||
backend = &RefdbBackend{ptr: (*C.git_refdb_backend)(ptr)}
|
||||
return backend
|
||||
}
|
||||
|
||||
|
|
69
reference.go
69
reference.go
|
@ -17,11 +17,13 @@ const (
|
|||
)
|
||||
|
||||
type Reference struct {
|
||||
doNotCompare
|
||||
ptr *C.git_reference
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
type ReferenceCollection struct {
|
||||
doNotCompare
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
|
@ -293,12 +295,14 @@ func (v *Reference) Peel(t ObjectType) (*Object, error) {
|
|||
return allocObject(cobj, v.repo), nil
|
||||
}
|
||||
|
||||
// Owner returns a weak reference to the repository which owns this
|
||||
// reference.
|
||||
// Owner returns a weak reference to the repository which owns this reference.
|
||||
// This won't keep the underlying repository alive, but it should still be
|
||||
// Freed.
|
||||
func (v *Reference) Owner() *Repository {
|
||||
return &Repository{
|
||||
ptr: C.git_reference_owner(v.ptr),
|
||||
}
|
||||
repo := newRepositoryFromC(C.git_reference_owner(v.ptr))
|
||||
runtime.KeepAlive(v)
|
||||
repo.weak = true
|
||||
return repo
|
||||
}
|
||||
|
||||
// Cmp compares v to ref2. It returns 0 on equality, otherwise a
|
||||
|
@ -361,11 +365,13 @@ func (v *Reference) Free() {
|
|||
}
|
||||
|
||||
type ReferenceIterator struct {
|
||||
doNotCompare
|
||||
ptr *C.git_reference_iterator
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
type ReferenceNameIterator struct {
|
||||
doNotCompare
|
||||
*ReferenceIterator
|
||||
}
|
||||
|
||||
|
@ -420,11 +426,11 @@ func (repo *Repository) NewReferenceIteratorGlob(glob string) (*ReferenceIterato
|
|||
}
|
||||
|
||||
func (i *ReferenceIterator) Names() *ReferenceNameIterator {
|
||||
return &ReferenceNameIterator{i}
|
||||
return &ReferenceNameIterator{ReferenceIterator: i}
|
||||
}
|
||||
|
||||
// NextName retrieves the next reference name. If the iteration is over,
|
||||
// the returned error is git.ErrIterOver
|
||||
// the returned error code is git.ErrorCodeIterOver
|
||||
func (v *ReferenceNameIterator) Next() (string, error) {
|
||||
var ptr *C.char
|
||||
|
||||
|
@ -440,7 +446,7 @@ func (v *ReferenceNameIterator) Next() (string, error) {
|
|||
}
|
||||
|
||||
// Next retrieves the next reference. If the iterationis over, the
|
||||
// returned error is git.ErrIterOver
|
||||
// returned error code is git.ErrorCodeIterOver
|
||||
func (v *ReferenceIterator) Next() (*Reference, error) {
|
||||
var ptr *C.git_reference
|
||||
|
||||
|
@ -470,7 +476,7 @@ func (v *ReferenceIterator) Free() {
|
|||
C.git_reference_iterator_free(v.ptr)
|
||||
}
|
||||
|
||||
// ReferenceIsValidName ensures the reference name is well-formed.
|
||||
// ReferenceIsValidName returns whether the reference name is well-formed.
|
||||
//
|
||||
// Valid reference names must follow one of two patterns:
|
||||
//
|
||||
|
@ -483,8 +489,45 @@ func (v *ReferenceIterator) Free() {
|
|||
func ReferenceIsValidName(name string) bool {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
if C.git_reference_is_valid_name(cname) == 1 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
return C.git_reference_is_valid_name(cname) == 1
|
||||
}
|
||||
|
||||
const (
|
||||
// This should match GIT_REFNAME_MAX in src/refs.h
|
||||
_refnameMaxLength = C.size_t(1024)
|
||||
)
|
||||
|
||||
type ReferenceFormat uint
|
||||
|
||||
const (
|
||||
ReferenceFormatNormal ReferenceFormat = C.GIT_REFERENCE_FORMAT_NORMAL
|
||||
ReferenceFormatAllowOnelevel ReferenceFormat = C.GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL
|
||||
ReferenceFormatRefspecPattern ReferenceFormat = C.GIT_REFERENCE_FORMAT_REFSPEC_PATTERN
|
||||
ReferenceFormatRefspecShorthand ReferenceFormat = C.GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND
|
||||
)
|
||||
|
||||
// ReferenceNormalizeName normalizes the reference name and checks validity.
|
||||
//
|
||||
// This will normalize the reference name by removing any leading slash '/'
|
||||
// characters and collapsing runs of adjacent slashes between name components
|
||||
// into a single slash.
|
||||
//
|
||||
// See git_reference_symbolic_create() for rules about valid names.
|
||||
func ReferenceNormalizeName(name string, flags ReferenceFormat) (string, error) {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
|
||||
buf := (*C.char)(C.malloc(_refnameMaxLength))
|
||||
defer C.free(unsafe.Pointer(buf))
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_reference_normalize_name(buf, _refnameMaxLength, cname, C.uint(flags))
|
||||
if ecode < 0 {
|
||||
return "", MakeGitError(ecode)
|
||||
}
|
||||
|
||||
return C.GoString(buf), nil
|
||||
}
|
||||
|
|
|
@ -106,7 +106,7 @@ func TestReferenceIterator(t *testing.T) {
|
|||
list = append(list, name)
|
||||
name, err = nameIter.Next()
|
||||
}
|
||||
if !IsErrorCode(err, ErrIterOver) {
|
||||
if !IsErrorCode(err, ErrorCodeIterOver) {
|
||||
t.Fatal("Iteration not over")
|
||||
}
|
||||
|
||||
|
@ -122,7 +122,7 @@ func TestReferenceIterator(t *testing.T) {
|
|||
count++
|
||||
_, err = iter.Next()
|
||||
}
|
||||
if !IsErrorCode(err, ErrIterOver) {
|
||||
if !IsErrorCode(err, ErrorCodeIterOver) {
|
||||
t.Fatal("Iteration not over")
|
||||
}
|
||||
|
||||
|
@ -224,6 +224,29 @@ func TestReferenceIsValidName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestReferenceNormalizeName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ref, err := ReferenceNormalizeName("refs/heads//master", ReferenceFormatNormal)
|
||||
checkFatal(t, err)
|
||||
|
||||
if ref != "refs/heads/master" {
|
||||
t.Errorf("ReferenceNormalizeName(%q) = %q; want %q", "refs/heads//master", ref, "refs/heads/master")
|
||||
}
|
||||
|
||||
ref, err = ReferenceNormalizeName("master", ReferenceFormatAllowOnelevel|ReferenceFormatRefspecShorthand)
|
||||
checkFatal(t, err)
|
||||
|
||||
if ref != "master" {
|
||||
t.Errorf("ReferenceNormalizeName(%q) = %q; want %q", "master", ref, "master")
|
||||
}
|
||||
|
||||
ref, err = ReferenceNormalizeName("foo^", ReferenceFormatNormal)
|
||||
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
|
||||
t.Errorf("foo^ should be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func compareStringList(t *testing.T, expected, actual []string) {
|
||||
for i, v := range expected {
|
||||
if actual[i] != v {
|
||||
|
|
|
@ -0,0 +1,149 @@
|
|||
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
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
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)
|
||||
}
|
||||
}
|
656
remote.go
656
remote.go
|
@ -1,21 +1,42 @@
|
|||
package git
|
||||
|
||||
/*
|
||||
#include <git2.h>
|
||||
#include <string.h>
|
||||
|
||||
extern void _go_git_setup_callbacks(git_remote_callbacks *callbacks);
|
||||
#include <git2.h>
|
||||
|
||||
extern void _go_git_populate_remote_callbacks(git_remote_callbacks *callbacks);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"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 {
|
||||
TotalObjects uint
|
||||
IndexedObjects uint
|
||||
|
@ -69,6 +90,11 @@ type RemoteCallbacks struct {
|
|||
PushUpdateReferenceCallback
|
||||
}
|
||||
|
||||
type remoteCallbacksData struct {
|
||||
callbacks *RemoteCallbacks
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
type FetchPrune uint
|
||||
|
||||
const (
|
||||
|
@ -83,7 +109,6 @@ const (
|
|||
type DownloadTags uint
|
||||
|
||||
const (
|
||||
|
||||
// Use the setting from the configuration.
|
||||
DownloadTagsUnspecified DownloadTags = C.GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED
|
||||
// Ask the server for tags pointing to objects we're already
|
||||
|
@ -115,6 +140,9 @@ type FetchOptions struct {
|
|||
|
||||
// Headers are extra headers for the fetch operation.
|
||||
Headers []string
|
||||
|
||||
// Proxy options to use for this fetch operation
|
||||
ProxyOptions ProxyOptions
|
||||
}
|
||||
|
||||
type ProxyType uint
|
||||
|
@ -141,10 +169,79 @@ type ProxyOptions struct {
|
|||
Url string
|
||||
}
|
||||
|
||||
func proxyOptionsFromC(copts *C.git_proxy_options) *ProxyOptions {
|
||||
return &ProxyOptions{
|
||||
Type: ProxyType(copts._type),
|
||||
Url: C.GoString(copts.url),
|
||||
}
|
||||
}
|
||||
|
||||
type Remote struct {
|
||||
doNotCompare
|
||||
ptr *C.git_remote
|
||||
callbacks RemoteCallbacks
|
||||
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
|
||||
|
@ -164,20 +261,25 @@ type Certificate struct {
|
|||
Hostkey HostkeyCertificate
|
||||
}
|
||||
|
||||
// HostkeyKind is a bitmask of the available hashes in HostkeyCertificate.
|
||||
type HostkeyKind uint
|
||||
|
||||
const (
|
||||
HostkeyMD5 HostkeyKind = C.GIT_CERT_SSH_MD5
|
||||
HostkeySHA1 HostkeyKind = C.GIT_CERT_SSH_SHA1
|
||||
HostkeyMD5 HostkeyKind = C.GIT_CERT_SSH_MD5
|
||||
HostkeySHA1 HostkeyKind = C.GIT_CERT_SSH_SHA1
|
||||
HostkeySHA256 HostkeyKind = 1 << 2
|
||||
HostkeyRaw HostkeyKind = 1 << 3
|
||||
)
|
||||
|
||||
// Server host key information. If Kind is HostkeyMD5 the MD5 field
|
||||
// will be filled. If Kind is HostkeySHA1, then HashSHA1 will be
|
||||
// filled.
|
||||
// Server host key information. A bitmask containing the available fields.
|
||||
// Check for combinations of: HostkeyMD5, HostkeySHA1, HostkeySHA256, HostkeyRaw.
|
||||
type HostkeyCertificate struct {
|
||||
Kind HostkeyKind
|
||||
HashMD5 [16]byte
|
||||
HashSHA1 [20]byte
|
||||
Kind HostkeyKind
|
||||
HashMD5 [16]byte
|
||||
HashSHA1 [20]byte
|
||||
HashSHA256 [32]byte
|
||||
Hostkey []byte
|
||||
SSHPublicKey ssh.PublicKey
|
||||
}
|
||||
|
||||
type PushOptions struct {
|
||||
|
@ -188,6 +290,9 @@ type PushOptions struct {
|
|||
|
||||
// Headers are extra headers for the push operation.
|
||||
Headers []string
|
||||
|
||||
// Proxy options to use for this push operation
|
||||
ProxyOptions ProxyOptions
|
||||
}
|
||||
|
||||
type RemoteHead struct {
|
||||
|
@ -202,87 +307,155 @@ func newRemoteHeadFromC(ptr *C.git_remote_head) RemoteHead {
|
|||
}
|
||||
}
|
||||
|
||||
func untrackCalbacksPayload(callbacks *C.git_remote_callbacks) {
|
||||
if callbacks != nil && callbacks.payload != nil {
|
||||
pointerHandles.Untrack(callbacks.payload)
|
||||
}
|
||||
}
|
||||
|
||||
func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) {
|
||||
C.git_remote_init_callbacks(ptr, C.GIT_REMOTE_CALLBACKS_VERSION)
|
||||
if callbacks == nil {
|
||||
func untrackCallbacksPayload(callbacks *C.git_remote_callbacks) {
|
||||
if callbacks == nil || callbacks.payload == nil {
|
||||
return
|
||||
}
|
||||
C._go_git_setup_callbacks(ptr)
|
||||
ptr.payload = pointerHandles.Track(callbacks)
|
||||
pointerHandles.Untrack(callbacks.payload)
|
||||
}
|
||||
|
||||
func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks, errorTarget *error) *C.git_remote_callbacks {
|
||||
C.git_remote_init_callbacks(ptr, C.GIT_REMOTE_CALLBACKS_VERSION)
|
||||
if callbacks == nil {
|
||||
return ptr
|
||||
}
|
||||
C._go_git_populate_remote_callbacks(ptr)
|
||||
data := &remoteCallbacksData{
|
||||
callbacks: callbacks,
|
||||
errorTarget: errorTarget,
|
||||
}
|
||||
ptr.payload = pointerHandles.Track(data)
|
||||
return ptr
|
||||
}
|
||||
|
||||
//export sidebandProgressCallback
|
||||
func sidebandProgressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int {
|
||||
callbacks := pointerHandles.Get(data).(*RemoteCallbacks)
|
||||
if callbacks.SidebandProgressCallback == nil {
|
||||
return 0
|
||||
func sidebandProgressCallback(errorMessage **C.char, _str *C.char, _len C.int, handle unsafe.Pointer) C.int {
|
||||
data := pointerHandles.Get(handle).(*remoteCallbacksData)
|
||||
if data.callbacks.SidebandProgressCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
str := C.GoStringN(_str, _len)
|
||||
return int(callbacks.SidebandProgressCallback(str))
|
||||
ret := data.callbacks.SidebandProgressCallback(str)
|
||||
if ret < 0 {
|
||||
err := errors.New(ErrorCode(ret).String())
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export completionCallback
|
||||
func completionCallback(completion_type C.git_remote_completion_type, data unsafe.Pointer) int {
|
||||
callbacks := pointerHandles.Get(data).(*RemoteCallbacks)
|
||||
if callbacks.CompletionCallback == nil {
|
||||
return 0
|
||||
func completionCallback(errorMessage **C.char, completion_type C.git_remote_completion_type, handle unsafe.Pointer) C.int {
|
||||
data := pointerHandles.Get(handle).(*remoteCallbacksData)
|
||||
if data.callbacks.CompletionCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
return int(callbacks.CompletionCallback(RemoteCompletion(completion_type)))
|
||||
ret := data.callbacks.CompletionCallback(RemoteCompletion(completion_type))
|
||||
if ret < 0 {
|
||||
err := errors.New(ErrorCode(ret).String())
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export credentialsCallback
|
||||
func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C.char, allowed_types uint, data unsafe.Pointer) int {
|
||||
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
|
||||
if callbacks.CredentialsCallback == nil {
|
||||
return C.GIT_PASSTHROUGH
|
||||
func credentialsCallback(
|
||||
errorMessage **C.char,
|
||||
_cred **C.git_cred,
|
||||
_url *C.char,
|
||||
_username_from_url *C.char,
|
||||
allowed_types uint,
|
||||
handle unsafe.Pointer,
|
||||
) C.int {
|
||||
data := pointerHandles.Get(handle).(*remoteCallbacksData)
|
||||
if data.callbacks.CredentialsCallback == nil {
|
||||
return C.int(ErrorCodePassthrough)
|
||||
}
|
||||
url := C.GoString(_url)
|
||||
username_from_url := C.GoString(_username_from_url)
|
||||
ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types))
|
||||
ret, cred := data.callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types))
|
||||
if ret < 0 {
|
||||
err := errors.New(ErrorCode(ret).String())
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
if cred != nil {
|
||||
*_cred = cred.ptr
|
||||
|
||||
// have transferred ownership to libgit, 'forget' the native pointer
|
||||
cred.ptr = nil
|
||||
runtime.SetFinalizer(cred, nil)
|
||||
}
|
||||
return int(ret)
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export transferProgressCallback
|
||||
func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int {
|
||||
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
|
||||
if callbacks.TransferProgressCallback == nil {
|
||||
return 0
|
||||
func transferProgressCallback(errorMessage **C.char, stats *C.git_transfer_progress, handle unsafe.Pointer) C.int {
|
||||
data := pointerHandles.Get(handle).(*remoteCallbacksData)
|
||||
if data.callbacks.TransferProgressCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
return int(callbacks.TransferProgressCallback(newTransferProgressFromC(stats)))
|
||||
ret := data.callbacks.TransferProgressCallback(newTransferProgressFromC(stats))
|
||||
if ret < 0 {
|
||||
err := errors.New(ErrorCode(ret).String())
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export updateTipsCallback
|
||||
func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data unsafe.Pointer) int {
|
||||
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
|
||||
if callbacks.UpdateTipsCallback == nil {
|
||||
return 0
|
||||
func updateTipsCallback(
|
||||
errorMessage **C.char,
|
||||
_refname *C.char,
|
||||
_a *C.git_oid,
|
||||
_b *C.git_oid,
|
||||
handle unsafe.Pointer,
|
||||
) C.int {
|
||||
data := pointerHandles.Get(handle).(*remoteCallbacksData)
|
||||
if data.callbacks.UpdateTipsCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
refname := C.GoString(_refname)
|
||||
a := newOidFromC(_a)
|
||||
b := newOidFromC(_b)
|
||||
return int(callbacks.UpdateTipsCallback(refname, a, b))
|
||||
ret := data.callbacks.UpdateTipsCallback(refname, a, b)
|
||||
if ret < 0 {
|
||||
err := errors.New(ErrorCode(ret).String())
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export certificateCheckCallback
|
||||
func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, data unsafe.Pointer) int {
|
||||
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
|
||||
func certificateCheckCallback(
|
||||
errorMessage **C.char,
|
||||
_cert *C.git_cert,
|
||||
_valid C.int,
|
||||
_host *C.char,
|
||||
handle unsafe.Pointer,
|
||||
) C.int {
|
||||
data := pointerHandles.Get(handle).(*remoteCallbacksData)
|
||||
// if there's no callback set, we need to make sure we fail if the library didn't consider this cert valid
|
||||
if callbacks.CertificateCheckCallback == nil {
|
||||
if _valid == 1 {
|
||||
return 0
|
||||
} else {
|
||||
return C.GIT_ECERTIFICATE
|
||||
if data.callbacks.CertificateCheckCallback == nil {
|
||||
if _valid == 0 {
|
||||
return C.int(ErrorCodeCertificate)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
host := C.GoString(_host)
|
||||
valid := _valid != 0
|
||||
|
||||
|
@ -292,7 +465,17 @@ func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, da
|
|||
ccert := (*C.git_cert_x509)(unsafe.Pointer(_cert))
|
||||
x509_certs, err := x509.ParseCertificates(C.GoBytes(ccert.data, C.int(ccert.len)))
|
||||
if err != nil {
|
||||
return C.GIT_EUSER
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
if len(x509_certs) < 1 {
|
||||
err := errors.New("empty certificate list")
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
|
||||
// we assume there's only one, which should hold true for any web server we want to talk to
|
||||
|
@ -304,78 +487,144 @@ func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, da
|
|||
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)))
|
||||
} else {
|
||||
cstr := C.CString("Unsupported certificate type")
|
||||
C.giterr_set_str(C.GITERR_NET, cstr)
|
||||
C.free(unsafe.Pointer(cstr))
|
||||
return -1 // we don't support anything else atm
|
||||
err := errors.New("unsupported certificate type")
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
|
||||
return int(callbacks.CertificateCheckCallback(&cert, valid, host))
|
||||
ret := data.callbacks.CertificateCheckCallback(&cert, valid, host)
|
||||
if ret < 0 {
|
||||
err := errors.New(ErrorCode(ret).String())
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export packProgressCallback
|
||||
func packProgressCallback(stage C.int, current, total C.uint, data unsafe.Pointer) int {
|
||||
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
|
||||
|
||||
if callbacks.PackProgressCallback == nil {
|
||||
return 0
|
||||
func packProgressCallback(errorMessage **C.char, stage C.int, current, total C.uint, handle unsafe.Pointer) C.int {
|
||||
data := pointerHandles.Get(handle).(*remoteCallbacksData)
|
||||
if data.callbacks.PackProgressCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
return int(callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total)))
|
||||
ret := data.callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total))
|
||||
if ret < 0 {
|
||||
err := errors.New(ErrorCode(ret).String())
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export pushTransferProgressCallback
|
||||
func pushTransferProgressCallback(current, total C.uint, bytes C.size_t, data unsafe.Pointer) int {
|
||||
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
|
||||
if callbacks.PushTransferProgressCallback == nil {
|
||||
return 0
|
||||
func pushTransferProgressCallback(errorMessage **C.char, current, total C.uint, bytes C.size_t, handle unsafe.Pointer) C.int {
|
||||
data := pointerHandles.Get(handle).(*remoteCallbacksData)
|
||||
if data.callbacks.PushTransferProgressCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
return int(callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes)))
|
||||
ret := data.callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes))
|
||||
if ret < 0 {
|
||||
err := errors.New(ErrorCode(ret).String())
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
//export pushUpdateReferenceCallback
|
||||
func pushUpdateReferenceCallback(refname, status *C.char, data unsafe.Pointer) int {
|
||||
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
|
||||
|
||||
if callbacks.PushUpdateReferenceCallback == nil {
|
||||
return 0
|
||||
func pushUpdateReferenceCallback(errorMessage **C.char, refname, status *C.char, handle unsafe.Pointer) C.int {
|
||||
data := pointerHandles.Get(handle).(*remoteCallbacksData)
|
||||
if data.callbacks.PushUpdateReferenceCallback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
return int(callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status)))
|
||||
ret := data.callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status))
|
||||
if ret < 0 {
|
||||
err := errors.New(ErrorCode(ret).String())
|
||||
if data.errorTarget != nil {
|
||||
*data.errorTarget = err
|
||||
}
|
||||
return setCallbackError(errorMessage, err)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
func populateProxyOptions(ptr *C.git_proxy_options, opts *ProxyOptions) {
|
||||
C.git_proxy_init_options(ptr, C.GIT_PROXY_OPTIONS_VERSION)
|
||||
func populateProxyOptions(copts *C.git_proxy_options, opts *ProxyOptions) *C.git_proxy_options {
|
||||
C.git_proxy_init_options(copts, C.GIT_PROXY_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
copts._type = C.git_proxy_t(opts.Type)
|
||||
copts.url = C.CString(opts.Url)
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeProxyOptions(copts *C.git_proxy_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
|
||||
ptr._type = C.git_proxy_t(opts.Type)
|
||||
ptr.url = C.CString(opts.Url)
|
||||
}
|
||||
|
||||
func freeProxyOptions(ptr *C.git_proxy_options) {
|
||||
C.free(unsafe.Pointer(ptr.url))
|
||||
C.free(unsafe.Pointer(copts.url))
|
||||
}
|
||||
|
||||
// RemoteIsValidName returns whether the remote name is well-formed.
|
||||
func RemoteIsValidName(name string) bool {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
if C.git_remote_is_valid_name(cname) == 1 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
return C.git_remote_is_valid_name(cname) == 1
|
||||
}
|
||||
|
||||
func (r *Remote) Free() {
|
||||
// free releases the resources of the Remote.
|
||||
func (r *Remote) free() {
|
||||
runtime.SetFinalizer(r, nil)
|
||||
C.git_remote_free(r.ptr)
|
||||
r.ptr = 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 {
|
||||
doNotCompare
|
||||
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) {
|
||||
|
@ -410,7 +659,29 @@ func (c *RemoteCollection) Create(name string, url string) (*Remote, error) {
|
|||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
runtime.SetFinalizer(remote, (*Remote).Free)
|
||||
c.trackRemote(remote)
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -446,7 +717,7 @@ func (c *RemoteCollection) CreateWithFetchspec(name string, url string, fetch st
|
|||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
runtime.SetFinalizer(remote, (*Remote).Free)
|
||||
c.trackRemote(remote)
|
||||
return remote, nil
|
||||
}
|
||||
|
||||
|
@ -463,7 +734,7 @@ func (c *RemoteCollection) CreateAnonymous(url string) (*Remote, error) {
|
|||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
runtime.SetFinalizer(remote, (*Remote).Free)
|
||||
c.trackRemote(remote)
|
||||
return remote, nil
|
||||
}
|
||||
|
||||
|
@ -480,10 +751,24 @@ func (c *RemoteCollection) Lookup(name string) (*Remote, error) {
|
|||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
runtime.SetFinalizer(remote, (*Remote).Free)
|
||||
c.trackRemote(remote)
|
||||
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 {
|
||||
s := C.git_remote_name(o.ptr)
|
||||
runtime.KeepAlive(o)
|
||||
|
@ -669,34 +954,56 @@ func (o *Remote) RefspecCount() uint {
|
|||
return uint(count)
|
||||
}
|
||||
|
||||
func populateFetchOptions(options *C.git_fetch_options, opts *FetchOptions) {
|
||||
C.git_fetch_init_options(options, C.GIT_FETCH_OPTIONS_VERSION)
|
||||
func populateFetchOptions(copts *C.git_fetch_options, opts *FetchOptions, errorTarget *error) *C.git_fetch_options {
|
||||
C.git_fetch_init_options(copts, C.GIT_FETCH_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
populateRemoteCallbacks(&options.callbacks, &opts.RemoteCallbacks)
|
||||
options.prune = C.git_fetch_prune_t(opts.Prune)
|
||||
options.update_fetchhead = cbool(opts.UpdateFetchhead)
|
||||
options.download_tags = C.git_remote_autotag_option_t(opts.DownloadTags)
|
||||
populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget)
|
||||
copts.prune = C.git_fetch_prune_t(opts.Prune)
|
||||
copts.update_fetchhead = cbool(opts.UpdateFetchhead)
|
||||
copts.download_tags = C.git_remote_autotag_option_t(opts.DownloadTags)
|
||||
|
||||
options.custom_headers = C.git_strarray{}
|
||||
options.custom_headers.count = C.size_t(len(opts.Headers))
|
||||
options.custom_headers.strings = makeCStringsFromStrings(opts.Headers)
|
||||
copts.custom_headers = C.git_strarray{
|
||||
count: C.size_t(len(opts.Headers)),
|
||||
strings: makeCStringsFromStrings(opts.Headers),
|
||||
}
|
||||
populateProxyOptions(&copts.proxy_opts, &opts.ProxyOptions)
|
||||
return copts
|
||||
}
|
||||
|
||||
func populatePushOptions(options *C.git_push_options, opts *PushOptions) {
|
||||
C.git_push_init_options(options, C.GIT_PUSH_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
func freeFetchOptions(copts *C.git_fetch_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
freeStrarray(&copts.custom_headers)
|
||||
untrackCallbacksPayload(&copts.callbacks)
|
||||
freeProxyOptions(&copts.proxy_opts)
|
||||
}
|
||||
|
||||
options.pb_parallelism = C.uint(opts.PbParallelism)
|
||||
func populatePushOptions(copts *C.git_push_options, opts *PushOptions, errorTarget *error) *C.git_push_options {
|
||||
C.git_push_init_options(copts, C.GIT_PUSH_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
options.custom_headers = C.git_strarray{}
|
||||
options.custom_headers.count = C.size_t(len(opts.Headers))
|
||||
options.custom_headers.strings = makeCStringsFromStrings(opts.Headers)
|
||||
copts.pb_parallelism = C.uint(opts.PbParallelism)
|
||||
copts.custom_headers = C.git_strarray{
|
||||
count: C.size_t(len(opts.Headers)),
|
||||
strings: makeCStringsFromStrings(opts.Headers),
|
||||
}
|
||||
populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget)
|
||||
populateProxyOptions(&copts.proxy_opts, &opts.ProxyOptions)
|
||||
return copts
|
||||
}
|
||||
|
||||
populateRemoteCallbacks(&options.callbacks, &opts.RemoteCallbacks)
|
||||
func freePushOptions(copts *C.git_push_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
untrackCallbacksPayload(&copts.callbacks)
|
||||
freeStrarray(&copts.custom_headers)
|
||||
freeProxyOptions(&copts.proxy_opts)
|
||||
}
|
||||
|
||||
// Fetch performs a fetch operation. refspecs specifies which refspecs
|
||||
|
@ -710,26 +1017,29 @@ func (o *Remote) Fetch(refspecs []string, opts *FetchOptions, msg string) error
|
|||
defer C.free(unsafe.Pointer(cmsg))
|
||||
}
|
||||
|
||||
crefspecs := C.git_strarray{}
|
||||
crefspecs.count = C.size_t(len(refspecs))
|
||||
crefspecs.strings = makeCStringsFromStrings(refspecs)
|
||||
var err error
|
||||
crefspecs := C.git_strarray{
|
||||
count: C.size_t(len(refspecs)),
|
||||
strings: makeCStringsFromStrings(refspecs),
|
||||
}
|
||||
defer freeStrarray(&crefspecs)
|
||||
|
||||
coptions := (*C.git_fetch_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_fetch_options{}))))
|
||||
defer C.free(unsafe.Pointer(coptions))
|
||||
|
||||
populateFetchOptions(coptions, opts)
|
||||
defer untrackCalbacksPayload(&coptions.callbacks)
|
||||
defer freeStrarray(&coptions.custom_headers)
|
||||
coptions := populateFetchOptions(&C.git_fetch_options{}, opts, &err)
|
||||
defer freeFetchOptions(coptions)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_remote_fetch(o.ptr, &crefspecs, coptions, cmsg)
|
||||
runtime.KeepAlive(o)
|
||||
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -749,23 +1059,27 @@ func (o *Remote) ConnectPush(callbacks *RemoteCallbacks, proxyOpts *ProxyOptions
|
|||
//
|
||||
// 'headers' are extra HTTP headers to use in this connection.
|
||||
func (o *Remote) Connect(direction ConnectDirection, callbacks *RemoteCallbacks, proxyOpts *ProxyOptions, headers []string) error {
|
||||
var ccallbacks C.git_remote_callbacks
|
||||
populateRemoteCallbacks(&ccallbacks, callbacks)
|
||||
var err error
|
||||
ccallbacks := populateRemoteCallbacks(&C.git_remote_callbacks{}, callbacks, &err)
|
||||
defer untrackCallbacksPayload(ccallbacks)
|
||||
|
||||
var cproxy C.git_proxy_options
|
||||
populateProxyOptions(&cproxy, proxyOpts)
|
||||
defer freeProxyOptions(&cproxy)
|
||||
cproxy := populateProxyOptions(&C.git_proxy_options{}, proxyOpts)
|
||||
defer freeProxyOptions(cproxy)
|
||||
|
||||
cheaders := C.git_strarray{}
|
||||
cheaders.count = C.size_t(len(headers))
|
||||
cheaders.strings = makeCStringsFromStrings(headers)
|
||||
cheaders := C.git_strarray{
|
||||
count: C.size_t(len(headers)),
|
||||
strings: makeCStringsFromStrings(headers),
|
||||
}
|
||||
defer freeStrarray(&cheaders)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_remote_connect(o.ptr, C.git_direction(direction), &ccallbacks, &cproxy, &cheaders)
|
||||
ret := C.git_remote_connect(o.ptr, C.git_direction(direction), ccallbacks, cproxy, &cheaders)
|
||||
runtime.KeepAlive(o)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret != 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
@ -829,23 +1143,24 @@ func (o *Remote) Ls(filterRefs ...string) ([]RemoteHead, error) {
|
|||
}
|
||||
|
||||
func (o *Remote) Push(refspecs []string, opts *PushOptions) error {
|
||||
crefspecs := C.git_strarray{}
|
||||
crefspecs.count = C.size_t(len(refspecs))
|
||||
crefspecs.strings = makeCStringsFromStrings(refspecs)
|
||||
crefspecs := C.git_strarray{
|
||||
count: C.size_t(len(refspecs)),
|
||||
strings: makeCStringsFromStrings(refspecs),
|
||||
}
|
||||
defer freeStrarray(&crefspecs)
|
||||
|
||||
coptions := (*C.git_push_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_push_options{}))))
|
||||
defer C.free(unsafe.Pointer(coptions))
|
||||
|
||||
populatePushOptions(coptions, opts)
|
||||
defer untrackCalbacksPayload(&coptions.callbacks)
|
||||
defer freeStrarray(&coptions.custom_headers)
|
||||
var err error
|
||||
coptions := populatePushOptions(&C.git_push_options{}, opts, &err)
|
||||
defer freePushOptions(coptions)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_remote_push(o.ptr, &crefspecs, coptions)
|
||||
runtime.KeepAlive(o)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
@ -857,16 +1172,71 @@ func (o *Remote) PruneRefs() bool {
|
|||
}
|
||||
|
||||
func (o *Remote) Prune(callbacks *RemoteCallbacks) error {
|
||||
var ccallbacks C.git_remote_callbacks
|
||||
populateRemoteCallbacks(&ccallbacks, callbacks)
|
||||
var err error
|
||||
ccallbacks := populateRemoteCallbacks(&C.git_remote_callbacks{}, callbacks, &err)
|
||||
defer untrackCallbacksPayload(ccallbacks)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_remote_prune(o.ptr, &ccallbacks)
|
||||
ret := C.git_remote_prune(o.ptr, ccallbacks)
|
||||
runtime.KeepAlive(o)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
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_init_options(&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_init_options(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,
|
||||
}
|
||||
}
|
||||
|
|
353
remote_test.go
353
remote_test.go
|
@ -1,8 +1,21 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/shlex"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func TestListRemotes(t *testing.T) {
|
||||
|
@ -10,9 +23,9 @@ func TestListRemotes(t *testing.T) {
|
|||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
_, err := repo.Remotes.Create("test", "git://foo/bar")
|
||||
|
||||
remote, err := repo.Remotes.Create("test", "git://foo/bar")
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
expected := []string{
|
||||
"test",
|
||||
|
@ -27,10 +40,10 @@ func TestListRemotes(t *testing.T) {
|
|||
func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T) ErrorCode {
|
||||
if hostname != "github.com" {
|
||||
t.Fatal("Hostname does not match")
|
||||
return ErrUser
|
||||
return ErrorCodeUser
|
||||
}
|
||||
|
||||
return 0
|
||||
return ErrorCodeOK
|
||||
}
|
||||
|
||||
func TestCertificateCheck(t *testing.T) {
|
||||
|
@ -40,6 +53,7 @@ func TestCertificateCheck(t *testing.T) {
|
|||
|
||||
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
options := FetchOptions{
|
||||
RemoteCallbacks: RemoteCallbacks{
|
||||
|
@ -60,6 +74,30 @@ func TestRemoteConnect(t *testing.T) {
|
|||
|
||||
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
err = remote.ConnectFetch(nil, nil, nil)
|
||||
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)
|
||||
|
@ -72,6 +110,7 @@ func TestRemoteLs(t *testing.T) {
|
|||
|
||||
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
err = remote.ConnectFetch(nil, nil, nil)
|
||||
checkFatal(t, err)
|
||||
|
@ -91,6 +130,7 @@ func TestRemoteLsFiltering(t *testing.T) {
|
|||
|
||||
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
err = remote.ConnectFetch(nil, nil, nil)
|
||||
checkFatal(t, err)
|
||||
|
@ -123,11 +163,13 @@ func TestRemotePruneRefs(t *testing.T) {
|
|||
err = config.SetBool("remote.origin.prune", true)
|
||||
checkFatal(t, err)
|
||||
|
||||
_, err = repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
|
||||
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
remote, err := repo.Remotes.Lookup("origin")
|
||||
remote, err = repo.Remotes.Lookup("origin")
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
if !remote.PruneRefs() {
|
||||
t.Fatal("Expected remote to be configured to prune references")
|
||||
|
@ -146,6 +188,7 @@ func TestRemotePrune(t *testing.T) {
|
|||
|
||||
remoteRef, err := remoteRepo.CreateBranch("test-prune", commit, true)
|
||||
checkFatal(t, err)
|
||||
defer remoteRef.Free()
|
||||
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
@ -157,12 +200,14 @@ func TestRemotePrune(t *testing.T) {
|
|||
remoteUrl := fmt.Sprintf("file://%s", remoteRepo.Workdir())
|
||||
remote, err := repo.Remotes.Create("origin", remoteUrl)
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
err = remote.Fetch([]string{"test-prune"}, nil, "")
|
||||
checkFatal(t, err)
|
||||
|
||||
_, err = repo.References.Create("refs/remotes/origin/test-prune", head, true, "remote reference")
|
||||
ref, err := repo.References.Create("refs/remotes/origin/test-prune", head, true, "remote reference")
|
||||
checkFatal(t, err)
|
||||
defer ref.Free()
|
||||
|
||||
err = remoteRef.Delete()
|
||||
checkFatal(t, err)
|
||||
|
@ -172,6 +217,7 @@ func TestRemotePrune(t *testing.T) {
|
|||
|
||||
rr, err := repo.Remotes.Lookup("origin")
|
||||
checkFatal(t, err)
|
||||
defer rr.Free()
|
||||
|
||||
err = rr.ConnectFetch(nil, nil, nil)
|
||||
checkFatal(t, err)
|
||||
|
@ -179,8 +225,299 @@ func TestRemotePrune(t *testing.T) {
|
|||
err = rr.Prune(nil)
|
||||
checkFatal(t, err)
|
||||
|
||||
_, err = repo.References.Lookup("refs/remotes/origin/test-prune")
|
||||
ref, err = repo.References.Lookup("refs/remotes/origin/test-prune")
|
||||
if err == nil {
|
||||
ref.Free()
|
||||
t.Fatal("Expected error getting a pruned reference")
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
fetchOpts := FetchOptions{
|
||||
RemoteCallbacks: RemoteCallbacks{
|
||||
CredentialsCallback: func(url, username string, allowedTypes CredType) (ErrorCode, *Cred) {
|
||||
return ErrorCodeUser, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err = remote.Fetch(nil, &fetchOpts, "fetch")
|
||||
if IsErrorCode(err, ErrorCodeUser) {
|
||||
t.Fatalf("remote.Fetch() = %v, want %v", err, ErrorCodeUser)
|
||||
}
|
||||
}
|
||||
|
||||
func newChannelPipe(t *testing.T, w io.Writer, wg *sync.WaitGroup) (*os.File, error) {
|
||||
pr, pw, err := os.Pipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
_, err := io.Copy(w, pr)
|
||||
if err != nil && err != io.EOF {
|
||||
t.Logf("Failed to copy: %v", err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
return pw, nil
|
||||
}
|
||||
|
||||
func startSSHServer(t *testing.T, hostKey ssh.Signer, authorizedKeys []ssh.PublicKey) net.Listener {
|
||||
t.Helper()
|
||||
|
||||
marshaledAuthorizedKeys := make([][]byte, len(authorizedKeys))
|
||||
for i, authorizedKey := range authorizedKeys {
|
||||
marshaledAuthorizedKeys[i] = authorizedKey.Marshal()
|
||||
}
|
||||
|
||||
config := &ssh.ServerConfig{
|
||||
PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
|
||||
marshaledPubKey := pubKey.Marshal()
|
||||
for _, marshaledAuthorizedKey := range marshaledAuthorizedKeys {
|
||||
if bytes.Equal(marshaledPubKey, marshaledAuthorizedKey) {
|
||||
return &ssh.Permissions{
|
||||
// Record the public key used for authentication.
|
||||
Extensions: map[string]string{
|
||||
"pubkey-fp": ssh.FingerprintSHA256(pubKey),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
t.Logf("unknown public key for %q:\n\t%+v\n\t%+v\n", c.User(), pubKey.Marshal(), authorizedKeys)
|
||||
return nil, fmt.Errorf("unknown public key for %q", c.User())
|
||||
},
|
||||
}
|
||||
config.AddHostKey(hostKey)
|
||||
|
||||
listener, err := net.Listen("tcp", "localhost:0")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to listen for connection: %v", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
nConn, err := listener.Accept()
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||
return
|
||||
}
|
||||
t.Logf("Failed to accept incoming connection: %v", err)
|
||||
return
|
||||
}
|
||||
defer nConn.Close()
|
||||
|
||||
conn, chans, reqs, err := ssh.NewServerConn(nConn, config)
|
||||
if err != nil {
|
||||
t.Logf("failed to handshake: %+v, %+v", conn, err)
|
||||
return
|
||||
}
|
||||
|
||||
// The incoming Request channel must be serviced.
|
||||
go func() {
|
||||
for newRequest := range reqs {
|
||||
t.Logf("new request %v", newRequest)
|
||||
}
|
||||
}()
|
||||
|
||||
// Service only the first channel request
|
||||
newChannel := <-chans
|
||||
defer func() {
|
||||
for newChannel := range chans {
|
||||
t.Logf("new channel %v", newChannel)
|
||||
newChannel.Reject(ssh.UnknownChannelType, "server closing")
|
||||
}
|
||||
}()
|
||||
|
||||
// Channels have a type, depending on the application level
|
||||
// protocol intended. In the case of a shell, the type is
|
||||
// "session" and ServerShell may be used to present a simple
|
||||
// terminal interface.
|
||||
if newChannel.ChannelType() != "session" {
|
||||
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
|
||||
return
|
||||
}
|
||||
channel, requests, err := newChannel.Accept()
|
||||
if err != nil {
|
||||
t.Logf("Could not accept channel: %v", err)
|
||||
return
|
||||
}
|
||||
defer channel.Close()
|
||||
|
||||
// Sessions have out-of-band requests such as "shell",
|
||||
// "pty-req" and "env". Here we handle only the
|
||||
// "exec" request.
|
||||
req := <-requests
|
||||
if req.Type != "exec" {
|
||||
req.Reply(false, nil)
|
||||
return
|
||||
}
|
||||
// RFC 4254 Section 6.5.
|
||||
var payload struct {
|
||||
Command string
|
||||
}
|
||||
if err := ssh.Unmarshal(req.Payload, &payload); err != nil {
|
||||
t.Logf("invalid payload on channel %v: %v", channel, err)
|
||||
req.Reply(false, nil)
|
||||
return
|
||||
}
|
||||
args, err := shlex.Split(payload.Command)
|
||||
if err != nil {
|
||||
t.Logf("invalid command on channel %v: %v", channel, err)
|
||||
req.Reply(false, nil)
|
||||
return
|
||||
}
|
||||
if len(args) < 2 || (args[0] != "git-upload-pack" && args[0] != "git-receive-pack") {
|
||||
t.Logf("invalid command (%v) on channel %v: %v", args, channel, err)
|
||||
req.Reply(false, nil)
|
||||
return
|
||||
}
|
||||
req.Reply(true, nil)
|
||||
|
||||
go func(in <-chan *ssh.Request) {
|
||||
for req := range in {
|
||||
t.Logf("draining request %v", req)
|
||||
}
|
||||
}(requests)
|
||||
|
||||
// The first parameter is the (absolute) path of the repository.
|
||||
args[1] = "./testdata" + args[1]
|
||||
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Stdin = channel
|
||||
var wg sync.WaitGroup
|
||||
stdoutPipe, err := newChannelPipe(t, channel, &wg)
|
||||
if err != nil {
|
||||
t.Logf("Failed to create stdout pipe: %v", err)
|
||||
return
|
||||
}
|
||||
cmd.Stdout = stdoutPipe
|
||||
stderrPipe, err := newChannelPipe(t, channel.Stderr(), &wg)
|
||||
if err != nil {
|
||||
t.Logf("Failed to create stderr pipe: %v", err)
|
||||
return
|
||||
}
|
||||
cmd.Stderr = stderrPipe
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
channel.CloseWrite()
|
||||
}()
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
t.Logf("Failed to start %v: %v", args, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Once the process has started, we need to close the write end of the
|
||||
// pipes from this process so that we can know when the child has done
|
||||
// writing to it.
|
||||
stdoutPipe.Close()
|
||||
stderrPipe.Close()
|
||||
|
||||
timer := time.AfterFunc(5*time.Second, func() {
|
||||
t.Log("process timed out, terminating")
|
||||
cmd.Process.Kill()
|
||||
})
|
||||
defer timer.Stop()
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
t.Logf("Failed to run %v: %v", args, err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
return listener
|
||||
}
|
||||
|
||||
func TestRemoteSSH(t *testing.T) {
|
||||
t.Parallel()
|
||||
pubKeyUsername := "testuser"
|
||||
|
||||
hostPrivKey, err := rsa.GenerateKey(rand.Reader, 512)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate the host RSA private key: %v", err)
|
||||
}
|
||||
hostSigner, err := ssh.NewSignerFromKey(hostPrivKey)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate SSH hostSigner: %v", err)
|
||||
}
|
||||
|
||||
privKey, err := rsa.GenerateKey(rand.Reader, 512)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate the user RSA private key: %v", err)
|
||||
}
|
||||
signer, err := ssh.NewSignerFromKey(privKey)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate SSH signer: %v", err)
|
||||
}
|
||||
// This is in the format "xx:xx:xx:...", so we remove the colons so that it
|
||||
// matches the fmt.Sprintf() below.
|
||||
// Note that not all libssh2 implementations support the SHA256 fingerprint,
|
||||
// so we use MD5 here for testing.
|
||||
publicKeyFingerprint := strings.Replace(ssh.FingerprintLegacyMD5(hostSigner.PublicKey()), ":", "", -1)
|
||||
|
||||
listener := startSSHServer(t, hostSigner, []ssh.PublicKey{signer.PublicKey()})
|
||||
defer listener.Close()
|
||||
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
certificateCheckCallbackCalled := false
|
||||
fetchOpts := FetchOptions{
|
||||
RemoteCallbacks: RemoteCallbacks{
|
||||
CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) ErrorCode {
|
||||
hostkeyFingerprint := fmt.Sprintf("%x", cert.Hostkey.HashMD5[:])
|
||||
if hostkeyFingerprint != publicKeyFingerprint {
|
||||
t.Logf("server hostkey %q, want %q", hostkeyFingerprint, publicKeyFingerprint)
|
||||
return ErrorCodeAuth
|
||||
}
|
||||
certificateCheckCallbackCalled = true
|
||||
return ErrorCodeOK
|
||||
},
|
||||
CredentialsCallback: func(url, username string, allowedTypes CredType) (ErrorCode, *Cred) {
|
||||
if allowedTypes&(CredTypeSshKey|CredTypeSshCustom) != 0 {
|
||||
cred, err := NewCredentialSSHKeyFromSigner(pubKeyUsername, signer)
|
||||
if err != nil {
|
||||
t.Logf("failed to create credentials: %v", err)
|
||||
return ErrorCodeAuth, nil
|
||||
}
|
||||
return ErrorCodeOK, cred
|
||||
}
|
||||
t.Logf("unknown credential type %+v", allowedTypes)
|
||||
return ErrorCodeAuth, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
remote, err := repo.Remotes.Create(
|
||||
"origin",
|
||||
fmt.Sprintf("ssh://%s@%s/TestGitRepository", pubKeyUsername, listener.Addr().String()),
|
||||
)
|
||||
checkFatal(t, err)
|
||||
defer remote.Free()
|
||||
|
||||
err = remote.Fetch(nil, &fetchOpts, "")
|
||||
checkFatal(t, err)
|
||||
if !certificateCheckCallbackCalled {
|
||||
t.Fatalf("CertificateCheckCallback was not called")
|
||||
}
|
||||
|
||||
heads, err := remote.Ls()
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(heads) == 0 {
|
||||
t.Error("Expected remote heads")
|
||||
}
|
||||
}
|
||||
|
|
259
repository.go
259
repository.go
|
@ -14,6 +14,7 @@ import (
|
|||
|
||||
// Repository
|
||||
type Repository struct {
|
||||
doNotCompare
|
||||
ptr *C.git_repository
|
||||
// Remotes represents the collection of remotes and can be
|
||||
// used to add, remove and configure remotes for this
|
||||
|
@ -35,12 +36,17 @@ type Repository struct {
|
|||
// Stashes represents the collection of stashes and can be used to
|
||||
// save, apply and iterate over stash states in this repository.
|
||||
Stashes StashCollection
|
||||
|
||||
// weak indicates that a repository is a weak pointer and should not be
|
||||
// freed.
|
||||
weak bool
|
||||
}
|
||||
|
||||
func newRepositoryFromC(ptr *C.git_repository) *Repository {
|
||||
repo := &Repository{ptr: ptr}
|
||||
|
||||
repo.Remotes.repo = repo
|
||||
repo.Remotes.remotes = make(map[*C.git_remote]*Remote)
|
||||
repo.Submodules.repo = repo
|
||||
repo.References.repo = repo
|
||||
repo.Notes.repo = repo
|
||||
|
@ -136,8 +142,14 @@ func (v *Repository) SetRefdb(refdb *Refdb) {
|
|||
}
|
||||
|
||||
func (v *Repository) Free() {
|
||||
ptr := v.ptr
|
||||
v.ptr = nil
|
||||
runtime.SetFinalizer(v, nil)
|
||||
C.git_repository_free(v.ptr)
|
||||
v.Remotes.Free()
|
||||
if v.weak {
|
||||
return
|
||||
}
|
||||
C.git_repository_free(ptr)
|
||||
}
|
||||
|
||||
func (v *Repository) Config() (*Config, error) {
|
||||
|
@ -156,6 +168,17 @@ func (v *Repository) Config() (*Config, error) {
|
|||
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 {
|
||||
C.git_repository_set_config(v.ptr, c.ptr)
|
||||
runtime.KeepAlive(v)
|
||||
runtime.KeepAlive(c)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Repository) Index() (*Index, error) {
|
||||
var ptr *C.git_index
|
||||
|
||||
|
@ -185,15 +208,47 @@ func (v *Repository) lookupType(id *Oid, t ObjectType) (*Object, error) {
|
|||
return allocObject(ptr, v), nil
|
||||
}
|
||||
|
||||
func (v *Repository) lookupPrefixType(id *Oid, prefix uint, t ObjectType) (*Object, error) {
|
||||
var ptr *C.git_object
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_object_lookup_prefix(&ptr, v.ptr, id.toC(), C.size_t(prefix), C.git_object_t(t))
|
||||
runtime.KeepAlive(id)
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
return allocObject(ptr, v), nil
|
||||
}
|
||||
|
||||
func (v *Repository) Lookup(id *Oid) (*Object, error) {
|
||||
return v.lookupType(id, ObjectAny)
|
||||
}
|
||||
|
||||
// LookupPrefix looks up an object by its OID given a prefix of its identifier.
|
||||
func (v *Repository) LookupPrefix(id *Oid, prefix uint) (*Object, error) {
|
||||
return v.lookupPrefixType(id, prefix, ObjectAny)
|
||||
}
|
||||
|
||||
func (v *Repository) LookupTree(id *Oid) (*Tree, error) {
|
||||
obj, err := v.lookupType(id, ObjectTree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer obj.Free()
|
||||
|
||||
return obj.AsTree()
|
||||
}
|
||||
|
||||
// LookupPrefixTree looks up a tree by its OID given a prefix of its identifier.
|
||||
func (v *Repository) LookupPrefixTree(id *Oid, prefix uint) (*Tree, error) {
|
||||
obj, err := v.lookupPrefixType(id, prefix, ObjectTree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer obj.Free()
|
||||
|
||||
return obj.AsTree()
|
||||
}
|
||||
|
@ -203,6 +258,18 @@ func (v *Repository) LookupCommit(id *Oid) (*Commit, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer obj.Free()
|
||||
|
||||
return obj.AsCommit()
|
||||
}
|
||||
|
||||
// LookupPrefixCommit looks up a commit by its OID given a prefix of its identifier.
|
||||
func (v *Repository) LookupPrefixCommit(id *Oid, prefix uint) (*Commit, error) {
|
||||
obj, err := v.lookupPrefixType(id, prefix, ObjectCommit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer obj.Free()
|
||||
|
||||
return obj.AsCommit()
|
||||
}
|
||||
|
@ -212,6 +279,18 @@ func (v *Repository) LookupBlob(id *Oid) (*Blob, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer obj.Free()
|
||||
|
||||
return obj.AsBlob()
|
||||
}
|
||||
|
||||
// LookupPrefixBlob looks up a blob by its OID given a prefix of its identifier.
|
||||
func (v *Repository) LookupPrefixBlob(id *Oid, prefix uint) (*Blob, error) {
|
||||
obj, err := v.lookupPrefixType(id, prefix, ObjectBlob)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer obj.Free()
|
||||
|
||||
return obj.AsBlob()
|
||||
}
|
||||
|
@ -221,6 +300,18 @@ func (v *Repository) LookupTag(id *Oid) (*Tag, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer obj.Free()
|
||||
|
||||
return obj.AsTag()
|
||||
}
|
||||
|
||||
// LookupPrefixTag looks up a tag by its OID given a prefix of its identifier.
|
||||
func (v *Repository) LookupPrefixTag(id *Oid, prefix uint) (*Tag, error) {
|
||||
obj, err := v.lookupPrefixType(id, prefix, ObjectTag)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer obj.Free()
|
||||
|
||||
return obj.AsTag()
|
||||
}
|
||||
|
@ -391,6 +482,102 @@ func (v *Repository) CreateCommit(
|
|||
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(
|
||||
refname string, author, committer *Signature,
|
||||
message string, tree *Oid, parents ...*Oid) (*Oid, error) {
|
||||
|
@ -611,3 +798,73 @@ func (r *Repository) ClearGitIgnoreRules() error {
|
|||
}
|
||||
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
|
||||
}
|
||||
|
|
|
@ -5,6 +5,60 @@ import (
|
|||
"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) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
|
@ -40,3 +94,66 @@ func TestCreateCommitFromIds(t *testing.T) {
|
|||
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")
|
||||
}
|
||||
}
|
||||
|
|
11
reset.go
11
reset.go
|
@ -14,11 +14,18 @@ const (
|
|||
ResetHard ResetType = C.GIT_RESET_HARD
|
||||
)
|
||||
|
||||
func (r *Repository) ResetToCommit(commit *Commit, resetType ResetType, opts *CheckoutOpts) error {
|
||||
func (r *Repository) ResetToCommit(commit *Commit, resetType ResetType, opts *CheckoutOptions) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
ret := C.git_reset(r.ptr, commit.ptr, C.git_reset_t(resetType), opts.toC())
|
||||
|
||||
var err error
|
||||
cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err)
|
||||
defer freeCheckoutOptions(cOpts)
|
||||
|
||||
ret := C.git_reset(r.ptr, commit.ptr, C.git_reset_t(resetType), cOpts)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ func TestResetToCommit(t *testing.T) {
|
|||
commitToResetTo, err := repo.LookupCommit(commitId)
|
||||
checkFatal(t, err)
|
||||
|
||||
repo.ResetToCommit(commitToResetTo, ResetHard, &CheckoutOpts{})
|
||||
repo.ResetToCommit(commitToResetTo, ResetHard, &CheckoutOptions{})
|
||||
|
||||
// check that the file now reads "testing reset" like it did before
|
||||
bytes, err := ioutil.ReadFile(pathInRepo(repo, "README"))
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
package git
|
||||
|
||||
/*
|
||||
#include <git2.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// RevertOptions contains options for performing a revert
|
||||
type RevertOptions struct {
|
||||
Mainline uint
|
||||
MergeOpts MergeOptions
|
||||
CheckoutOpts CheckoutOptions
|
||||
}
|
||||
|
||||
func populateRevertOptions(copts *C.git_revert_options, opts *RevertOptions, errorTarget *error) *C.git_revert_options {
|
||||
C.git_revert_init_options(copts, C.GIT_REVERT_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
copts.mainline = C.uint(opts.Mainline)
|
||||
populateMergeOptions(&copts.merge_opts, &opts.MergeOpts)
|
||||
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOpts, errorTarget)
|
||||
return copts
|
||||
}
|
||||
|
||||
func revertOptionsFromC(copts *C.git_revert_options) RevertOptions {
|
||||
return RevertOptions{
|
||||
Mainline: uint(copts.mainline),
|
||||
MergeOpts: mergeOptionsFromC(&copts.merge_opts),
|
||||
CheckoutOpts: checkoutOptionsFromC(&copts.checkout_opts),
|
||||
}
|
||||
}
|
||||
|
||||
func freeRevertOptions(copts *C.git_revert_options) {
|
||||
if copts != nil {
|
||||
return
|
||||
}
|
||||
freeMergeOptions(&copts.merge_opts)
|
||||
freeCheckoutOptions(&copts.checkout_opts)
|
||||
}
|
||||
|
||||
// DefaultRevertOptions initialises a RevertOptions struct with default values
|
||||
func DefaultRevertOptions() (RevertOptions, error) {
|
||||
copts := C.git_revert_options{}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ecode := C.git_revert_init_options(&copts, C.GIT_REVERT_OPTIONS_VERSION)
|
||||
if ecode < 0 {
|
||||
return RevertOptions{}, MakeGitError(ecode)
|
||||
}
|
||||
|
||||
defer freeRevertOptions(&copts)
|
||||
return revertOptionsFromC(&copts), nil
|
||||
}
|
||||
|
||||
// Revert the provided commit leaving the index updated with the results of the revert
|
||||
func (r *Repository) Revert(commit *Commit, revertOptions *RevertOptions) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var err error
|
||||
cOpts := populateRevertOptions(&C.git_revert_options{}, revertOptions, &err)
|
||||
defer freeRevertOptions(cOpts)
|
||||
|
||||
ret := C.git_revert(r.ptr, commit.cast_ptr, cOpts)
|
||||
runtime.KeepAlive(r)
|
||||
runtime.KeepAlive(commit)
|
||||
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RevertCommit reverts the provided commit against "ourCommit"
|
||||
// The returned index contains the result of the revert and should be freed
|
||||
func (r *Repository) RevertCommit(revertCommit *Commit, ourCommit *Commit, mainline uint, mergeOptions *MergeOptions) (*Index, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
cOpts := populateMergeOptions(&C.git_merge_options{}, mergeOptions)
|
||||
defer freeMergeOptions(cOpts)
|
||||
|
||||
var index *C.git_index
|
||||
|
||||
ecode := C.git_revert_commit(&index, r.ptr, revertCommit.cast_ptr, ourCommit.cast_ptr, C.uint(mainline), cOpts)
|
||||
runtime.KeepAlive(revertCommit)
|
||||
runtime.KeepAlive(ourCommit)
|
||||
|
||||
if ecode < 0 {
|
||||
return nil, MakeGitError(ecode)
|
||||
}
|
||||
|
||||
return newIndexFromC(index, r), nil
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
expectedRevertedReadmeContents = "foo\n"
|
||||
)
|
||||
|
||||
func TestRevert(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
seedTestRepo(t, repo)
|
||||
commitID, _ := updateReadme(t, repo, content)
|
||||
|
||||
commit, err := repo.LookupCommit(commitID)
|
||||
checkFatal(t, err)
|
||||
|
||||
revertOptions, err := DefaultRevertOptions()
|
||||
checkFatal(t, err)
|
||||
|
||||
err = repo.Revert(commit, &revertOptions)
|
||||
checkFatal(t, err)
|
||||
|
||||
actualReadmeContents := readReadme(t, repo)
|
||||
|
||||
if actualReadmeContents != expectedRevertedReadmeContents {
|
||||
t.Fatalf(`README has incorrect contents after revert. Expected: "%v", Actual: "%v"`,
|
||||
expectedRevertedReadmeContents, actualReadmeContents)
|
||||
}
|
||||
|
||||
state := repo.State()
|
||||
if state != RepositoryStateRevert {
|
||||
t.Fatalf("Incorrect repository state. Expected: %v, Actual: %v", RepositoryStateRevert, state)
|
||||
}
|
||||
|
||||
err = repo.StateCleanup()
|
||||
checkFatal(t, err)
|
||||
|
||||
state = repo.State()
|
||||
if state != RepositoryStateNone {
|
||||
t.Fatalf("Incorrect repository state. Expected: %v, Actual: %v", RepositoryStateNone, state)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevertCommit(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
seedTestRepo(t, repo)
|
||||
commitID, _ := updateReadme(t, repo, content)
|
||||
|
||||
commit, err := repo.LookupCommit(commitID)
|
||||
checkFatal(t, err)
|
||||
|
||||
revertOptions, err := DefaultRevertOptions()
|
||||
checkFatal(t, err)
|
||||
|
||||
index, err := repo.RevertCommit(commit, commit, 0, &revertOptions.MergeOpts)
|
||||
checkFatal(t, err)
|
||||
defer index.Free()
|
||||
|
||||
err = repo.CheckoutIndex(index, &revertOptions.CheckoutOpts)
|
||||
checkFatal(t, err)
|
||||
|
||||
actualReadmeContents := readReadme(t, repo)
|
||||
|
||||
if actualReadmeContents != expectedRevertedReadmeContents {
|
||||
t.Fatalf(`README has incorrect contents after revert. Expected: "%v", Actual: "%v"`,
|
||||
expectedRevertedReadmeContents, actualReadmeContents)
|
||||
}
|
||||
}
|
|
@ -20,6 +20,7 @@ const (
|
|||
)
|
||||
|
||||
type Revspec struct {
|
||||
doNotCompare
|
||||
to *Object
|
||||
from *Object
|
||||
flags RevparseFlag
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
exec "$(dirname "$0")/build-libgit2.sh" --dynamic
|
|
@ -1,20 +1,5 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -ex
|
||||
set -e
|
||||
|
||||
ROOT="$(cd "$0/../.." && echo "${PWD}")"
|
||||
BUILD_PATH="${ROOT}/static-build"
|
||||
VENDORED_PATH="${ROOT}/vendor/libgit2"
|
||||
|
||||
mkdir -p "${BUILD_PATH}/build" "${BUILD_PATH}/install/lib"
|
||||
|
||||
cd "${BUILD_PATH}/build" &&
|
||||
cmake -DTHREADSAFE=ON \
|
||||
-DBUILD_CLAR=OFF \
|
||||
-DBUILD_SHARED_LIBS=OFF \
|
||||
-DCMAKE_C_FLAGS=-fPIC \
|
||||
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
|
||||
-DCMAKE_INSTALL_PREFIX="${BUILD_PATH}/install" \
|
||||
"${VENDORED_PATH}" &&
|
||||
|
||||
cmake --build . --target install
|
||||
exec "$(dirname "$0")/build-libgit2.sh" --static
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
#!/bin/sh
|
||||
|
||||
# Since CMake cannot build the static and dynamic libraries in the same
|
||||
# directory, this script helps build both static and dynamic versions of it and
|
||||
# have the common flags in one place instead of split between two places.
|
||||
|
||||
set -e
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 <--dynamic|--static> [--system]">&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
if [ "$#" -eq "0" ]; then
|
||||
usage
|
||||
fi
|
||||
|
||||
ROOT=${ROOT-"$(cd "$(dirname "$0")/.." && echo "${PWD}")"}
|
||||
VENDORED_PATH=${VENDORED_PATH-"${ROOT}/vendor/libgit2"}
|
||||
BUILD_SYSTEM=OFF
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--static)
|
||||
BUILD_PATH="${ROOT}/static-build"
|
||||
BUILD_SHARED_LIBS=OFF
|
||||
;;
|
||||
|
||||
--dynamic)
|
||||
BUILD_PATH="${ROOT}/dynamic-build"
|
||||
BUILD_SHARED_LIBS=ON
|
||||
;;
|
||||
|
||||
--system)
|
||||
BUILD_SYSTEM=ON
|
||||
;;
|
||||
|
||||
*)
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if [ -z "${BUILD_SHARED_LIBS}" ]; then
|
||||
usage
|
||||
fi
|
||||
|
||||
if [ "${BUILD_SYSTEM}" = "ON" ]; then
|
||||
BUILD_INSTALL_PREFIX=${SYSTEM_INSTALL_PREFIX-"/usr"}
|
||||
else
|
||||
BUILD_INSTALL_PREFIX="${BUILD_PATH}/install"
|
||||
mkdir -p "${BUILD_PATH}/install/lib"
|
||||
fi
|
||||
|
||||
mkdir -p "${BUILD_PATH}/build" &&
|
||||
cd "${BUILD_PATH}/build" &&
|
||||
cmake -DTHREADSAFE=ON \
|
||||
-DBUILD_CLAR=OFF \
|
||||
-DBUILD_SHARED_LIBS"=${BUILD_SHARED_LIBS}" \
|
||||
-DUSE_HTTPS=OFF \
|
||||
-DUSE_SSH=OFF \
|
||||
-DCMAKE_C_FLAGS=-fPIC \
|
||||
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
|
||||
-DCMAKE_INSTALL_PREFIX="${BUILD_INSTALL_PREFIX}" \
|
||||
-DDEPRECATE_HARD=ON \
|
||||
"${VENDORED_PATH}"
|
||||
|
||||
if which make nproc >/dev/null && [ -f Makefile ]; then
|
||||
# Make the build parallel if make is available and cmake used Makefiles.
|
||||
exec make "-j$(nproc --all)" install
|
||||
else
|
||||
exec cmake --build . --target install
|
||||
fi
|
72
settings.go
72
settings.go
|
@ -18,10 +18,20 @@ int _go_git_opts_set_size_t(int opt, size_t val)
|
|||
return git_libgit2_opts(opt, val);
|
||||
}
|
||||
|
||||
int _go_git_opts_set_cache_object_limit(git_object_t type, size_t size)
|
||||
{
|
||||
return git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, type, size);
|
||||
}
|
||||
|
||||
int _go_git_opts_get_size_t(int opt, size_t *val)
|
||||
{
|
||||
return git_libgit2_opts(opt, val);
|
||||
}
|
||||
|
||||
int _go_git_opts_get_size_t_size_t(int opt, size_t *val1, size_t *val2)
|
||||
{
|
||||
return git_libgit2_opts(opt, val1, val2);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
|
@ -75,6 +85,55 @@ func SetMwindowMappedLimit(size int) error {
|
|||
return setSizet(C.GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, size)
|
||||
}
|
||||
|
||||
func EnableCaching(enabled bool) error {
|
||||
if enabled {
|
||||
return setSizet(C.GIT_OPT_ENABLE_CACHING, 1)
|
||||
} else {
|
||||
return setSizet(C.GIT_OPT_ENABLE_CACHING, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func EnableStrictHashVerification(enabled bool) error {
|
||||
if enabled {
|
||||
return setSizet(C.GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1)
|
||||
} else {
|
||||
return setSizet(C.GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0)
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
return getSizetSizet(C.GIT_OPT_GET_CACHED_MEMORY)
|
||||
}
|
||||
|
||||
// deprecated: You should use `CachedMemory()` instead.
|
||||
func GetCachedMemory() (current int, allowed int, err error) {
|
||||
return CachedMemory()
|
||||
}
|
||||
|
||||
func SetCacheMaxSize(maxSize int) error {
|
||||
return setSizet(C.GIT_OPT_SET_CACHE_MAX_SIZE, maxSize)
|
||||
}
|
||||
|
||||
func SetCacheObjectLimit(objectType ObjectType, size int) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
err := C._go_git_opts_set_cache_object_limit(C.git_object_t(objectType), C.size_t(size))
|
||||
if err < 0 {
|
||||
return MakeGitError(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getSizet(opt C.int) (int, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
@ -88,6 +147,19 @@ func getSizet(opt C.int) (int, error) {
|
|||
return int(val), nil
|
||||
}
|
||||
|
||||
func getSizetSizet(opt C.int) (int, int, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var val1, val2 C.size_t
|
||||
err := C._go_git_opts_get_size_t_size_t(opt, &val1, &val2)
|
||||
if err < 0 {
|
||||
return 0, 0, MakeGitError(err)
|
||||
}
|
||||
|
||||
return int(val1), int(val2), nil
|
||||
}
|
||||
|
||||
func setSizet(opt C.int, val int) error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
|
|
@ -48,3 +48,52 @@ func TestMmapSizes(t *testing.T) {
|
|||
t.Fatal("Sizes don't match")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnableCaching(t *testing.T) {
|
||||
err := EnableCaching(false)
|
||||
checkFatal(t, err)
|
||||
|
||||
err = EnableCaching(true)
|
||||
checkFatal(t, err)
|
||||
}
|
||||
|
||||
func TestEnableStrictHashVerification(t *testing.T) {
|
||||
err := EnableStrictHashVerification(false)
|
||||
checkFatal(t, err)
|
||||
|
||||
err = EnableStrictHashVerification(true)
|
||||
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) {
|
||||
current, allowed, err := CachedMemory()
|
||||
checkFatal(t, err)
|
||||
|
||||
if current < 0 {
|
||||
t.Fatal("current < 0")
|
||||
}
|
||||
|
||||
if allowed < 0 {
|
||||
t.Fatal("allowed < 0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetCacheMaxSize(t *testing.T) {
|
||||
err := SetCacheMaxSize(0)
|
||||
checkFatal(t, err)
|
||||
|
||||
err = SetCacheMaxSize(1024 * 1024)
|
||||
checkFatal(t, err)
|
||||
|
||||
// revert to default 256MB
|
||||
err = SetCacheMaxSize(256 * 1024 * 1024)
|
||||
checkFatal(t, err)
|
||||
}
|
||||
|
|
|
@ -17,6 +17,10 @@ type Signature struct {
|
|||
}
|
||||
|
||||
func newSignatureFromC(sig *C.git_signature) *Signature {
|
||||
if sig == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// git stores minutes, go wants seconds
|
||||
loc := time.FixedZone("", int(sig.when.offset)*60)
|
||||
return &Signature{
|
||||
|
|
|
@ -0,0 +1,245 @@
|
|||
package git
|
||||
|
||||
/*
|
||||
#include <git2.h>
|
||||
|
||||
void _go_git_credential_free(git_cred *cred);
|
||||
*/
|
||||
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("", CredTypeSshKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer C._go_git_credential_free(cred.ptr)
|
||||
|
||||
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 *Cred) (*ssh.ClientConfig, error) {
|
||||
switch cred.Type() {
|
||||
case CredTypeSshCustom:
|
||||
credSSHCustom := (*C.git_cred_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, ret := cred.GetSSHKey()
|
||||
if ret != nil {
|
||||
return nil, ret
|
||||
}
|
||||
|
||||
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
|
||||
}
|
139
stash.go
139
stash.go
|
@ -3,7 +3,7 @@ package git
|
|||
/*
|
||||
#include <git2.h>
|
||||
|
||||
extern void _go_git_setup_stash_apply_progress_callbacks(git_stash_apply_options *opts);
|
||||
extern void _go_git_populate_stash_apply_callbacks(git_stash_apply_options *opts);
|
||||
extern int _go_git_stash_foreach(git_repository *repo, void *payload);
|
||||
*/
|
||||
import "C"
|
||||
|
@ -35,6 +35,7 @@ const (
|
|||
// StashCollection represents the possible operations that can be
|
||||
// performed on the collection of stashes for a repository.
|
||||
type StashCollection struct {
|
||||
doNotCompare
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
|
@ -113,34 +114,34 @@ const (
|
|||
|
||||
// StashApplyProgressCallback is the apply operation notification callback.
|
||||
type StashApplyProgressCallback func(progress StashApplyProgress) error
|
||||
|
||||
type stashApplyProgressData struct {
|
||||
Callback StashApplyProgressCallback
|
||||
Error error
|
||||
type stashApplyProgressCallbackData struct {
|
||||
callback StashApplyProgressCallback
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
//export stashApplyProgressCb
|
||||
func stashApplyProgressCb(progress C.git_stash_apply_progress_t, handle unsafe.Pointer) int {
|
||||
//export stashApplyProgressCallback
|
||||
func stashApplyProgressCallback(progress C.git_stash_apply_progress_t, handle unsafe.Pointer) C.int {
|
||||
payload := pointerHandles.Get(handle)
|
||||
data, ok := payload.(*stashApplyProgressData)
|
||||
data, ok := payload.(*stashApplyProgressCallbackData)
|
||||
if !ok {
|
||||
panic("could not retrieve data for handle")
|
||||
}
|
||||
|
||||
if data != nil {
|
||||
err := data.Callback(StashApplyProgress(progress))
|
||||
if err != nil {
|
||||
data.Error = err
|
||||
return C.GIT_EUSER
|
||||
}
|
||||
if data == nil || data.callback == nil {
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
return 0
|
||||
|
||||
err := data.callback(StashApplyProgress(progress))
|
||||
if err != nil {
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
// StashApplyOptions represents options to control the apply operation.
|
||||
type StashApplyOptions struct {
|
||||
Flags StashApplyFlag
|
||||
CheckoutOptions CheckoutOpts // options to use when writing files to the working directory
|
||||
CheckoutOptions CheckoutOptions // options to use when writing files to the working directory
|
||||
ProgressCallback StashApplyProgressCallback // optional callback to notify the consumer of application progress
|
||||
}
|
||||
|
||||
|
@ -161,51 +162,45 @@ func DefaultStashApplyOptions() (StashApplyOptions, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (opts *StashApplyOptions) toC() (
|
||||
optsC *C.git_stash_apply_options, progressData *stashApplyProgressData) {
|
||||
|
||||
if opts != nil {
|
||||
progressData = &stashApplyProgressData{
|
||||
Callback: opts.ProgressCallback,
|
||||
}
|
||||
|
||||
optsC = &C.git_stash_apply_options{
|
||||
version: C.GIT_STASH_APPLY_OPTIONS_VERSION,
|
||||
flags: C.git_stash_apply_flags(opts.Flags),
|
||||
}
|
||||
populateCheckoutOpts(&optsC.checkout_options, &opts.CheckoutOptions)
|
||||
if opts.ProgressCallback != nil {
|
||||
C._go_git_setup_stash_apply_progress_callbacks(optsC)
|
||||
optsC.progress_payload = pointerHandles.Track(progressData)
|
||||
}
|
||||
func populateStashApplyOptions(copts *C.git_stash_apply_options, opts *StashApplyOptions, errorTarget *error) *C.git_stash_apply_options {
|
||||
C.git_stash_apply_init_options(copts, C.GIT_STASH_APPLY_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
return
|
||||
copts.flags = C.git_stash_apply_flags(opts.Flags)
|
||||
populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget)
|
||||
if opts.ProgressCallback != nil {
|
||||
progressData := &stashApplyProgressCallbackData{
|
||||
callback: opts.ProgressCallback,
|
||||
errorTarget: errorTarget,
|
||||
}
|
||||
C._go_git_populate_stash_apply_callbacks(copts)
|
||||
copts.progress_payload = pointerHandles.Track(progressData)
|
||||
}
|
||||
return copts
|
||||
}
|
||||
|
||||
// should be called after every call to toC() as deferred.
|
||||
func untrackStashApplyOptionsCallback(optsC *C.git_stash_apply_options) {
|
||||
if optsC != nil && optsC.progress_payload != nil {
|
||||
pointerHandles.Untrack(optsC.progress_payload)
|
||||
func freeStashApplyOptions(copts *C.git_stash_apply_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func freeStashApplyOptions(optsC *C.git_stash_apply_options) {
|
||||
if optsC != nil {
|
||||
freeCheckoutOpts(&optsC.checkout_options)
|
||||
if copts.progress_payload != nil {
|
||||
pointerHandles.Untrack(copts.progress_payload)
|
||||
}
|
||||
freeCheckoutOptions(&copts.checkout_options)
|
||||
}
|
||||
|
||||
// Apply applies a single stashed state from the stash list.
|
||||
//
|
||||
// If local changes in the working directory conflict with changes in the
|
||||
// stash then ErrConflict will be returned. In this case, the index
|
||||
// stash then ErrorCodeConflict will be returned. In this case, the index
|
||||
// will always remain unmodified and all files in the working directory will
|
||||
// remain unmodified. However, if you are restoring untracked files or
|
||||
// ignored files and there is a conflict when applying the modified files,
|
||||
// then those files will remain in the working directory.
|
||||
//
|
||||
// If passing the StashApplyReinstateIndex flag and there would be conflicts
|
||||
// when reinstating the index, the function will return ErrConflict
|
||||
// when reinstating the index, the function will return ErrorCodeConflict
|
||||
// and both the working directory and index will be left unmodified.
|
||||
//
|
||||
// Note that a minimum checkout strategy of 'CheckoutSafe' is implied.
|
||||
|
@ -213,15 +208,15 @@ func freeStashApplyOptions(optsC *C.git_stash_apply_options) {
|
|||
// 'index' is the position within the stash list. 0 points to the most
|
||||
// recent stashed state.
|
||||
//
|
||||
// Returns error code ErrNotFound if there's no stashed state for the given
|
||||
// index, error code ErrConflict if local changes in the working directory
|
||||
// Returns error code ErrorCodeNotFound if there's no stashed state for the given
|
||||
// index, error code ErrorCodeConflict if local changes in the working directory
|
||||
// conflict with changes in the stash, the user returned error from the
|
||||
// StashApplyProgressCallback, if any, or other error code.
|
||||
//
|
||||
// Error codes can be interogated with IsErrorCode(err, ErrNotFound).
|
||||
// Error codes can be interogated with IsErrorCode(err, ErrorCodeNotFound).
|
||||
func (c *StashCollection) Apply(index int, opts StashApplyOptions) error {
|
||||
optsC, progressData := opts.toC()
|
||||
defer untrackStashApplyOptionsCallback(optsC)
|
||||
var err error
|
||||
optsC := populateStashApplyOptions(&C.git_stash_apply_options{}, &opts, &err)
|
||||
defer freeStashApplyOptions(optsC)
|
||||
|
||||
runtime.LockOSThread()
|
||||
|
@ -229,8 +224,8 @@ func (c *StashCollection) Apply(index int, opts StashApplyOptions) error {
|
|||
|
||||
ret := C.git_stash_apply(c.repo.ptr, C.size_t(index), optsC)
|
||||
runtime.KeepAlive(c)
|
||||
if ret == C.GIT_EUSER {
|
||||
return progressData.Error
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
|
@ -245,26 +240,25 @@ func (c *StashCollection) Apply(index int, opts StashApplyOptions) error {
|
|||
// 'message' is the message used when creating the stash and 'id'
|
||||
// is the commit id of the stash.
|
||||
type StashCallback func(index int, message string, id *Oid) error
|
||||
|
||||
type stashCallbackData struct {
|
||||
Callback StashCallback
|
||||
Error error
|
||||
callback StashCallback
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
//export stashForeachCb
|
||||
func stashForeachCb(index C.size_t, message *C.char, id *C.git_oid, handle unsafe.Pointer) int {
|
||||
//export stashForeachCallback
|
||||
func stashForeachCallback(index C.size_t, message *C.char, id *C.git_oid, handle unsafe.Pointer) C.int {
|
||||
payload := pointerHandles.Get(handle)
|
||||
data, ok := payload.(*stashCallbackData)
|
||||
if !ok {
|
||||
panic("could not retrieve data for handle")
|
||||
}
|
||||
|
||||
err := data.Callback(int(index), C.GoString(message), newOidFromC(id))
|
||||
err := data.callback(int(index), C.GoString(message), newOidFromC(id))
|
||||
if err != nil {
|
||||
data.Error = err
|
||||
return C.GIT_EUSER
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
return 0
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
// Foreach loops over all the stashed states and calls the callback
|
||||
|
@ -272,10 +266,11 @@ func stashForeachCb(index C.size_t, message *C.char, id *C.git_oid, handle unsaf
|
|||
//
|
||||
// If callback returns an error, this will stop looping.
|
||||
func (c *StashCollection) Foreach(callback StashCallback) error {
|
||||
var err error
|
||||
data := stashCallbackData{
|
||||
Callback: callback,
|
||||
callback: callback,
|
||||
errorTarget: &err,
|
||||
}
|
||||
|
||||
handle := pointerHandles.Track(&data)
|
||||
defer pointerHandles.Untrack(handle)
|
||||
|
||||
|
@ -284,8 +279,8 @@ func (c *StashCollection) Foreach(callback StashCallback) error {
|
|||
|
||||
ret := C._go_git_stash_foreach(c.repo.ptr, handle)
|
||||
runtime.KeepAlive(c)
|
||||
if ret == C.GIT_EUSER {
|
||||
return data.Error
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
|
@ -298,7 +293,7 @@ func (c *StashCollection) Foreach(callback StashCallback) error {
|
|||
// 'index' is the position within the stash list. 0 points
|
||||
// to the most recent stashed state.
|
||||
//
|
||||
// Returns error code ErrNotFound if there's no stashed
|
||||
// Returns error code ErrorCodeNotFound if there's no stashed
|
||||
// state for the given index.
|
||||
func (c *StashCollection) Drop(index int) error {
|
||||
runtime.LockOSThread()
|
||||
|
@ -320,11 +315,11 @@ func (c *StashCollection) Drop(index int) error {
|
|||
//
|
||||
// 'opts' controls how stashes are applied.
|
||||
//
|
||||
// Returns error code ErrNotFound if there's no stashed
|
||||
// Returns error code ErrorCodeNotFound if there's no stashed
|
||||
// state for the given index.
|
||||
func (c *StashCollection) Pop(index int, opts StashApplyOptions) error {
|
||||
optsC, progressData := opts.toC()
|
||||
defer untrackStashApplyOptionsCallback(optsC)
|
||||
var err error
|
||||
optsC := populateStashApplyOptions(&C.git_stash_apply_options{}, &opts, &err)
|
||||
defer freeStashApplyOptions(optsC)
|
||||
|
||||
runtime.LockOSThread()
|
||||
|
@ -332,8 +327,8 @@ func (c *StashCollection) Pop(index int, opts StashApplyOptions) error {
|
|||
|
||||
ret := C.git_stash_pop(c.repo.ptr, C.size_t(index), optsC)
|
||||
runtime.KeepAlive(c)
|
||||
if ret == C.GIT_EUSER {
|
||||
return progressData.Error
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
|
|
|
@ -56,8 +56,8 @@ func TestStash(t *testing.T) {
|
|||
// Apply: no stash for the given index
|
||||
|
||||
err = repo.Stashes.Apply(1, opts)
|
||||
if !IsErrorCode(err, ErrNotFound) {
|
||||
t.Errorf("expecting GIT_ENOTFOUND error code %d, got %v", ErrNotFound, err)
|
||||
if !IsErrorCode(err, ErrorCodeNotFound) {
|
||||
t.Errorf("expecting GIT_ENOTFOUND error code %d, got %v", ErrorCodeNotFound, err)
|
||||
}
|
||||
|
||||
// Apply: callback stopped
|
||||
|
|
|
@ -6,6 +6,7 @@ package git
|
|||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -54,6 +55,7 @@ func statusEntryFromC(statusEntry *C.git_status_entry) StatusEntry {
|
|||
}
|
||||
|
||||
type StatusList struct {
|
||||
doNotCompare
|
||||
ptr *C.git_status_list
|
||||
r *Repository
|
||||
}
|
||||
|
@ -86,6 +88,9 @@ func (statusList *StatusList) ByIndex(index int) (StatusEntry, error) {
|
|||
return StatusEntry{}, ErrInvalid
|
||||
}
|
||||
ptr := C.git_status_byindex(statusList.ptr, C.size_t(index))
|
||||
if ptr == nil {
|
||||
return StatusEntry{}, errors.New("index out of Bounds")
|
||||
}
|
||||
entry := statusEntryFromC(ptr)
|
||||
runtime.KeepAlive(statusList)
|
||||
|
||||
|
|
|
@ -61,3 +61,31 @@ func TestStatusList(t *testing.T) {
|
|||
t.Fatal("Incorrect entry path: ", entry.IndexToWorkdir.NewFile.Path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatusNothing(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
opts := &StatusOptions{
|
||||
Show: StatusShowIndexAndWorkdir,
|
||||
Flags: StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively,
|
||||
}
|
||||
|
||||
statusList, err := repo.StatusList(opts)
|
||||
checkFatal(t, err)
|
||||
|
||||
entryCount, err := statusList.EntryCount()
|
||||
checkFatal(t, err)
|
||||
|
||||
if entryCount != 0 {
|
||||
t.Fatal("expected no statuses in empty repo")
|
||||
}
|
||||
|
||||
_, err = statusList.ByIndex(0)
|
||||
if err == nil {
|
||||
t.Error("expected error getting status by index")
|
||||
}
|
||||
}
|
||||
|
|
83
submodule.go
83
submodule.go
|
@ -6,7 +6,9 @@ package git
|
|||
extern int _go_git_visit_submodule(git_repository *repo, void *fct);
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -19,6 +21,7 @@ type SubmoduleUpdateOptions struct {
|
|||
|
||||
// Submodule
|
||||
type Submodule struct {
|
||||
doNotCompare
|
||||
ptr *C.git_submodule
|
||||
r *Repository
|
||||
}
|
||||
|
@ -80,6 +83,7 @@ const (
|
|||
)
|
||||
|
||||
type SubmoduleCollection struct {
|
||||
doNotCompare
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
|
@ -106,31 +110,52 @@ func (c *SubmoduleCollection) Lookup(name string) (*Submodule, error) {
|
|||
return newSubmoduleFromC(ptr, c.repo), nil
|
||||
}
|
||||
|
||||
type SubmoduleCbk func(sub *Submodule, name string) int
|
||||
|
||||
//export SubmoduleVisitor
|
||||
func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int {
|
||||
sub := &Submodule{(*C.git_submodule)(csub), nil}
|
||||
|
||||
if callback, ok := pointerHandles.Get(handle).(SubmoduleCbk); ok {
|
||||
return (C.int)(callback(sub, C.GoString(name)))
|
||||
} else {
|
||||
panic("invalid submodule visitor callback")
|
||||
}
|
||||
// SubmoduleCallback is a function that is called for every submodule found in SubmoduleCollection.Foreach.
|
||||
type SubmoduleCallback func(sub *Submodule, name string) int
|
||||
type submoduleCallbackData struct {
|
||||
callback SubmoduleCallback
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
func (c *SubmoduleCollection) Foreach(cbk SubmoduleCbk) error {
|
||||
//export submoduleCallback
|
||||
func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int {
|
||||
sub := &Submodule{ptr: (*C.git_submodule)(csub)}
|
||||
|
||||
data, ok := pointerHandles.Get(handle).(submoduleCallbackData)
|
||||
if !ok {
|
||||
panic("invalid submodule visitor callback")
|
||||
}
|
||||
|
||||
ret := data.callback(sub, C.GoString(name))
|
||||
if ret < 0 {
|
||||
*data.errorTarget = errors.New(ErrorCode(ret).String())
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
func (c *SubmoduleCollection) Foreach(callback SubmoduleCallback) error {
|
||||
var err error
|
||||
data := submoduleCallbackData{
|
||||
callback: callback,
|
||||
errorTarget: &err,
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
handle := pointerHandles.Track(cbk)
|
||||
handle := pointerHandles.Track(data)
|
||||
defer pointerHandles.Untrack(handle)
|
||||
|
||||
ret := C._go_git_visit_submodule(c.repo.ptr, handle)
|
||||
runtime.KeepAlive(c)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -341,17 +366,18 @@ func (sub *Submodule) Open() (*Repository, error) {
|
|||
}
|
||||
|
||||
func (sub *Submodule) Update(init bool, opts *SubmoduleUpdateOptions) error {
|
||||
var copts C.git_submodule_update_options
|
||||
err := populateSubmoduleUpdateOptions(&copts, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
cOpts := populateSubmoduleUpdateOptions(&C.git_submodule_update_options{}, opts, &err)
|
||||
defer freeSubmoduleUpdateOptions(cOpts)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_submodule_update(sub.ptr, cbool(init), &copts)
|
||||
ret := C.git_submodule_update(sub.ptr, cbool(init), cOpts)
|
||||
runtime.KeepAlive(sub)
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
@ -359,15 +385,22 @@ func (sub *Submodule) Update(init bool, opts *SubmoduleUpdateOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func populateSubmoduleUpdateOptions(ptr *C.git_submodule_update_options, opts *SubmoduleUpdateOptions) error {
|
||||
C.git_submodule_update_init_options(ptr, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION)
|
||||
|
||||
func populateSubmoduleUpdateOptions(copts *C.git_submodule_update_options, opts *SubmoduleUpdateOptions, errorTarget *error) *C.git_submodule_update_options {
|
||||
C.git_submodule_update_init_options(copts, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION)
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
populateCheckoutOpts(&ptr.checkout_opts, opts.CheckoutOpts)
|
||||
populateFetchOptions(&ptr.fetch_opts, opts.FetchOptions)
|
||||
populateCheckoutOptions(&copts.checkout_opts, opts.CheckoutOpts, errorTarget)
|
||||
populateFetchOptions(&copts.fetch_opts, opts.FetchOptions, errorTarget)
|
||||
|
||||
return nil
|
||||
return copts
|
||||
}
|
||||
|
||||
func freeSubmoduleUpdateOptions(copts *C.git_submodule_update_options) {
|
||||
if copts == nil {
|
||||
return
|
||||
}
|
||||
freeCheckoutOptions(&copts.checkout_opts)
|
||||
freeFetchOptions(&copts.fetch_opts)
|
||||
}
|
||||
|
|
50
tag.go
50
tag.go
|
@ -13,6 +13,7 @@ import (
|
|||
|
||||
// Tag
|
||||
type Tag struct {
|
||||
doNotCompare
|
||||
Object
|
||||
cast_ptr *C.git_tag
|
||||
}
|
||||
|
@ -21,26 +22,26 @@ func (t *Tag) AsObject() *Object {
|
|||
return &t.Object
|
||||
}
|
||||
|
||||
func (t Tag) Message() string {
|
||||
func (t *Tag) Message() string {
|
||||
ret := C.GoString(C.git_tag_message(t.cast_ptr))
|
||||
runtime.KeepAlive(t)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (t Tag) Name() string {
|
||||
func (t *Tag) Name() string {
|
||||
ret := C.GoString(C.git_tag_name(t.cast_ptr))
|
||||
runtime.KeepAlive(t)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (t Tag) Tagger() *Signature {
|
||||
func (t *Tag) Tagger() *Signature {
|
||||
cast_ptr := C.git_tag_tagger(t.cast_ptr)
|
||||
ret := newSignatureFromC(cast_ptr)
|
||||
runtime.KeepAlive(t)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (t Tag) Target() *Object {
|
||||
func (t *Tag) Target() *Object {
|
||||
var ptr *C.git_object
|
||||
ret := C.git_tag_target(&ptr, t.cast_ptr)
|
||||
runtime.KeepAlive(t)
|
||||
|
@ -51,19 +52,20 @@ func (t Tag) Target() *Object {
|
|||
return allocObject(ptr, t.repo)
|
||||
}
|
||||
|
||||
func (t Tag) TargetId() *Oid {
|
||||
func (t *Tag) TargetId() *Oid {
|
||||
ret := newOidFromC(C.git_tag_target_id(t.cast_ptr))
|
||||
runtime.KeepAlive(t)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (t Tag) TargetType() ObjectType {
|
||||
func (t *Tag) TargetType() ObjectType {
|
||||
ret := ObjectType(C.git_tag_target_type(t.cast_ptr))
|
||||
runtime.KeepAlive(t)
|
||||
return ret
|
||||
}
|
||||
|
||||
type TagsCollection struct {
|
||||
doNotCompare
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
|
@ -197,48 +199,48 @@ func (c *TagsCollection) ListWithMatch(pattern string) ([]string, error) {
|
|||
// so repo.LookupTag() will return an error for these tags. Use
|
||||
// repo.References.Lookup() instead.
|
||||
type TagForeachCallback func(name string, id *Oid) error
|
||||
type tagForeachData struct {
|
||||
callback TagForeachCallback
|
||||
err error
|
||||
type tagForeachCallbackData struct {
|
||||
callback TagForeachCallback
|
||||
errorTarget *error
|
||||
}
|
||||
|
||||
//export gitTagForeachCb
|
||||
func gitTagForeachCb(name *C.char, id *C.git_oid, handle unsafe.Pointer) int {
|
||||
//export tagForeachCallback
|
||||
func tagForeachCallback(name *C.char, id *C.git_oid, handle unsafe.Pointer) C.int {
|
||||
payload := pointerHandles.Get(handle)
|
||||
data, ok := payload.(*tagForeachData)
|
||||
data, ok := payload.(*tagForeachCallbackData)
|
||||
if !ok {
|
||||
panic("could not retrieve tag foreach CB handle")
|
||||
}
|
||||
|
||||
err := data.callback(C.GoString(name), newOidFromC(id))
|
||||
if err != nil {
|
||||
data.err = err
|
||||
return C.GIT_EUSER
|
||||
*data.errorTarget = err
|
||||
return C.int(ErrorCodeUser)
|
||||
}
|
||||
|
||||
return 0
|
||||
return C.int(ErrorCodeOK)
|
||||
}
|
||||
|
||||
// Foreach calls the callback for each tag in the repository.
|
||||
func (c *TagsCollection) Foreach(callback TagForeachCallback) error {
|
||||
data := tagForeachData{
|
||||
callback: callback,
|
||||
err: nil,
|
||||
var err error
|
||||
data := tagForeachCallbackData{
|
||||
callback: callback,
|
||||
errorTarget: &err,
|
||||
}
|
||||
|
||||
handle := pointerHandles.Track(&data)
|
||||
defer pointerHandles.Untrack(handle)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
err := C._go_git_tag_foreach(c.repo.ptr, handle)
|
||||
ret := C._go_git_tag_foreach(c.repo.ptr, handle)
|
||||
runtime.KeepAlive(c)
|
||||
if err == C.GIT_EUSER {
|
||||
return data.err
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return err
|
||||
}
|
||||
if err < 0 {
|
||||
return MakeGitError(err)
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
ref: refs/heads/master
|
|
@ -0,0 +1,6 @@
|
|||
[core]
|
||||
repositoryformatversion = 0
|
||||
filemode = true
|
||||
bare = true
|
||||
[remote "origin"]
|
||||
url = https://github.com/libgit2/TestGitRepository
|
|
@ -0,0 +1 @@
|
|||
Unnamed repository; edit this file 'description' to name the repository.
|
|
@ -0,0 +1,6 @@
|
|||
# git ls-files --others --exclude-from=.git/info/exclude
|
||||
# Lines that start with '#' are comments.
|
||||
# For a project mostly in C, the following would be a good set of
|
||||
# exclude patterns (uncomment them if you want to use them):
|
||||
# *.[oa]
|
||||
# *~
|
|
@ -0,0 +1,8 @@
|
|||
0966a434eb1a025db6b71485ab63a3bfbea520b6 refs/heads/first-merge
|
||||
49322bb17d3acc9146f98c97d078513228bbf3c0 refs/heads/master
|
||||
42e4e7c5e507e113ebbb7801b16b52cf867b7ce1 refs/heads/no-parent
|
||||
d96c4e80345534eccee5ac7b07fc7603b56124cb refs/tags/annotated_tag
|
||||
c070ad8c08840c8116da865b2d65593a6bb9cd2a refs/tags/annotated_tag^{}
|
||||
55a1a760df4b86a02094a904dfa511deb5655905 refs/tags/blob
|
||||
8f50ba15d49353813cc6e20298002c0d17b0a9ee refs/tags/commit_tree
|
||||
6e0c7bdb9b4ed93212491ee778ca1c65047cab4e refs/tags/nearly-dangling
|
Binary file not shown.
|
@ -0,0 +1,2 @@
|
|||
P pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.pack
|
||||
|
BIN
testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.bitmap
vendored
Normal file
BIN
testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.bitmap
vendored
Normal file
Binary file not shown.
BIN
testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.idx
vendored
Normal file
BIN
testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.idx
vendored
Normal file
Binary file not shown.
BIN
testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.pack
vendored
Normal file
BIN
testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.pack
vendored
Normal file
Binary file not shown.
|
@ -0,0 +1,9 @@
|
|||
# pack-refs with: peeled fully-peeled sorted
|
||||
0966a434eb1a025db6b71485ab63a3bfbea520b6 refs/heads/first-merge
|
||||
49322bb17d3acc9146f98c97d078513228bbf3c0 refs/heads/master
|
||||
42e4e7c5e507e113ebbb7801b16b52cf867b7ce1 refs/heads/no-parent
|
||||
d96c4e80345534eccee5ac7b07fc7603b56124cb refs/tags/annotated_tag
|
||||
^c070ad8c08840c8116da865b2d65593a6bb9cd2a
|
||||
55a1a760df4b86a02094a904dfa511deb5655905 refs/tags/blob
|
||||
8f50ba15d49353813cc6e20298002c0d17b0a9ee refs/tags/commit_tree
|
||||
6e0c7bdb9b4ed93212491ee778ca1c65047cab4e refs/tags/nearly-dangling
|
|
@ -0,0 +1 @@
|
|||
49322bb17d3acc9146f98c97d078513228bbf3c0
|
|
@ -0,0 +1,492 @@
|
|||
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, git2go-managed
|
||||
// transports.
|
||||
globalRegisteredSmartTransports = struct {
|
||||
sync.Mutex
|
||||
transports map[string]*RegisteredSmartTransport
|
||||
}{
|
||||
transports: make(map[string]*RegisteredSmartTransport),
|
||||
}
|
||||
)
|
||||
|
||||
// unregisterManagedTransports unregisters all git2go-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
|
||||
}
|
||||
|
||||
// SmartProxyOptions gets a copy of the proxy options for this transport.
|
||||
func (t *Transport) SmartProxyOptions() (*ProxyOptions, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var cpopts C.git_proxy_options
|
||||
if ret := C.git_transport_smart_proxy_options(&cpopts, t.ptr); ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
return proxyOptionsFromC(&cpopts), nil
|
||||
}
|
||||
|
||||
// SmartCredentials calls the credentials callback for this transport.
|
||||
func (t *Transport) SmartCredentials(user string, methods CredType) (*Cred, error) {
|
||||
cred := &Cred{}
|
||||
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 {
|
||||
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),
|
||||
}
|
||||
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)))
|
||||
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
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue