Compare commits
6 Commits
main
...
coveord/ad
Author | SHA1 | Date |
---|---|---|
|
36a9eb9566 | |
|
113f589520 | |
|
0d0aa5007a | |
|
4b56a0c04e | |
|
9232cbaf8d | |
|
bdf5a6f6a6 |
|
@ -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-1.6', 'release-1.5', 'release-1.3', 'release-1.2', 'release-1.1', 'release-1.0', 'release-0.28', 'release-0.27' ]
|
||||
|
||||
runs-on: ubuntu-20.04
|
||||
|
||||
steps:
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Create a cherry-pick PR
|
||||
run: |
|
||||
if ! git diff --quiet HEAD^ HEAD -- vendor/libgit2; then
|
||||
echo '::warning::Skipping cherry-pick since it is a vendored libgit2 bump'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
BRANCH_NAME="cherry-pick-${{ github.run_id }}-${{ matrix.branch }}"
|
||||
|
||||
# Setup usernames and authentication
|
||||
git config --global user.name "${{ github.actor }}"
|
||||
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
|
||||
cat <<- EOF > $HOME/.netrc
|
||||
machine github.com
|
||||
login ${{ github.actor }}
|
||||
password ${{ secrets.GITHUB_TOKEN }}
|
||||
machine api.github.com
|
||||
login ${{ github.actor }}
|
||||
password ${{ secrets.GITHUB_TOKEN }}
|
||||
EOF
|
||||
chmod 600 $HOME/.netrc
|
||||
|
||||
# Create the cherry-pick commit and create the PR for it.
|
||||
git checkout "${{ matrix.branch }}"
|
||||
git switch -c "${BRANCH_NAME}"
|
||||
git cherry-pick -x "${{ github.sha }}"
|
||||
git push --set-upstream origin "${BRANCH_NAME}"
|
||||
GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}" gh pr create \
|
||||
--base "${{ matrix.branch }}" \
|
||||
--title "$(git --no-pager show --format="%s" --no-patch HEAD)" \
|
||||
--body "$(git --no-pager show --format="%b" --no-patch HEAD)"
|
|
@ -0,0 +1,130 @@
|
|||
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
|
||||
matrix:
|
||||
libgit2:
|
||||
- 'v1.7.0'
|
||||
name: Go (system-wide, dynamic)
|
||||
|
||||
runs-on: ubuntu-20.04
|
||||
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: '1.17'
|
||||
id: go
|
||||
- name: Check out code into the Go module directory
|
||||
uses: actions/checkout@v1
|
||||
- name: Build libgit2 ${{ matrix.libgit2 }}
|
||||
run: |
|
||||
git submodule update --init
|
||||
sudo apt-get install -y --no-install-recommends libssh2-1-dev
|
||||
sudo env BUILD_LIBGIT_REF=${{ matrix.libgit2 }} ./script/build-libgit2.sh --dynamic --system
|
||||
- name: Test
|
||||
run: make TEST_ARGS=-test.v test
|
||||
|
||||
build-system-static:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
name: Go (system-wide, static)
|
||||
|
||||
runs-on: ubuntu-20.04
|
||||
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: '1.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,4 +1,2 @@
|
|||
/static-build/
|
||||
/dynamic-build/
|
||||
|
||||
go.*
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
[submodule "vendor/libgit2"]
|
||||
path = vendor/libgit2
|
||||
url = https://github.com/libgit2/libgit2
|
|
@ -10,8 +10,8 @@ package git
|
|||
#cgo CFLAGS: -DLIBGIT2_STATIC
|
||||
#include <git2.h>
|
||||
|
||||
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 5 || LIBGIT2_VER_MINOR > 5
|
||||
# error "Invalid libgit2 version; this libgit2 supports libgit2 between v1.5.0 and v1.5.0"
|
||||
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 7 || LIBGIT2_VER_MINOR > 7
|
||||
# error "Invalid libgit2 version; this git2go supports libgit2 v1.7.x"
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
|
|
|
@ -5,10 +5,11 @@ package git
|
|||
|
||||
/*
|
||||
#cgo pkg-config: libgit2
|
||||
#cgo CFLAGS: -DLIBGIT2_DYNAMIC -I/opt/libgit2/include
|
||||
#cgo LDFLAGS: -L/opt/libgit2 -lgit2
|
||||
#cgo CFLAGS: -DLIBGIT2_DYNAMIC
|
||||
#include <git2.h>
|
||||
|
||||
|
||||
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 7 || LIBGIT2_VER_MINOR > 7
|
||||
# error "Invalid libgit2 version; this git2go supports libgit2 v1.7.x"
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
|
|
|
@ -8,8 +8,8 @@ package git
|
|||
#cgo CFLAGS: -DLIBGIT2_STATIC
|
||||
#include <git2.h>
|
||||
|
||||
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 5 || LIBGIT2_VER_MINOR > 5
|
||||
# error "Invalid libgit2 version; this libgit2 supports libgit2 between v1.5.0 and v1.5.0"
|
||||
#if LIBGIT2_VER_MAJOR != 1 || LIBGIT2_VER_MINOR < 7 || LIBGIT2_VER_MINOR > 7
|
||||
# error "Invalid libgit2 version; this git2go supports libgit2 v1.7.x"
|
||||
#endif
|
||||
*/
|
||||
import "C"
|
||||
|
|
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
|||
The MIT License
|
||||
|
||||
Copyright (c) 2013 The libgit2 contributors
|
||||
Copyright (c) 2013 The git2go contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
|
15
Makefile
15
Makefile
|
@ -1,11 +1,7 @@
|
|||
TEST_ARGS ?= --count=1
|
||||
|
||||
PKG_CONFIG_PATH=/opt/libgit2/
|
||||
default: test
|
||||
|
||||
default: goimports test
|
||||
|
||||
goimports:
|
||||
goimports -w *.go
|
||||
|
||||
generate: static-build/install/lib/libgit2.a
|
||||
go generate --tags "static" ./...
|
||||
|
@ -14,19 +10,12 @@ generate: static-build/install/lib/libgit2.a
|
|||
# ==============
|
||||
# This uses whatever version of libgit2 can be found in the system.
|
||||
test:
|
||||
-go-mod-clean # go install go.wit.com/apps/go-mod-clean@latest
|
||||
go run script/check-MakeGitError-thread-lock.go
|
||||
LD_LIBRARY_PATH=/opt/libgit2 PKG_CONFIG_PATH=/opt/libgit2/ go test -v -x $(TEST_ARGS) ./...
|
||||
|
||||
add-remote:
|
||||
git remote add git2go https://github.com/libgit2/git2go.git
|
||||
go test $(TEST_ARGS) ./...
|
||||
|
||||
install:
|
||||
go install ./...
|
||||
|
||||
clean:
|
||||
rm go.*
|
||||
|
||||
# Bundled dynamic library
|
||||
# =======================
|
||||
# In order to avoid having to manipulate `git_dynamic.go`, which would prevent
|
||||
|
|
77
README.md
77
README.md
|
@ -1,34 +1,50 @@
|
|||
GO libgit2
|
||||
git2go
|
||||
======
|
||||
[](http://godoc.org/go.wit.com/lib/libgit2) [](https://travis-ci.org/libgit2/libgit2)
|
||||
[](http://godoc.org/github.com/libgit2/git2go/v34) [](https://travis-ci.org/libgit2/git2go)
|
||||
|
||||
Go bindings for [libgit2](http://libgit2.github.com/).
|
||||
|
||||
### Updated 2024/12/16
|
||||
|
||||
### Which Go version to use
|
||||
|
||||
* This package is updated to work against libgit2 version 1.8 on Debian sid
|
||||
* There is one line commented out which needs to be fixed in remote.go
|
||||
* some of the tests seem to run
|
||||
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:
|
||||
|
||||
| libgit2 | git2go |
|
||||
|---------|---------------|
|
||||
| main | (will be v37) |
|
||||
| 1.7 | v36 |
|
||||
| 1.6 | v35 |
|
||||
| 1.5 | v34 |
|
||||
| 1.3 | v33 |
|
||||
| 1.2 | v32 |
|
||||
| 1.1 | v31 |
|
||||
| 1.0 | v30 |
|
||||
| 0.99 | v29 |
|
||||
| 0.28 | v28 |
|
||||
| 0.27 | v27 |
|
||||
|
||||
You can import them in your project with the version's major number as a suffix. For example, if you have libgit2 v1.2 installed, you'd import git2go v35 with:
|
||||
|
||||
```sh
|
||||
go install go.wit.com/apps/go-clone@latest
|
||||
go install go.wit.com/apps/go-mod-clean@latest
|
||||
go-clone --recusive go.wit.com/lib/libgit2
|
||||
go get github.com/libgit2/git2go/v36
|
||||
```
|
||||
```go
|
||||
import "github.com/libgit2/git2go/v36"
|
||||
```
|
||||
|
||||
which will ensure there are no sudden changes to the API.
|
||||
|
||||
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
|
||||
|
||||
TODO: not sure yet
|
||||
If there's something version-specific that you'd want to contribute to, you can send them to the `release-${MAJOR}.${MINOR}` branches, which follow libgit2's releases.
|
||||
|
||||
Installing
|
||||
----------
|
||||
|
||||
This project wraps the functionality provided by libgit2. It thus needs it in order to perform the work.
|
||||
|
||||
This project wraps the functionality provided by libgit2. If you're using a versioned branch, install it to your system via your system's package manager and then install libgit2.
|
||||
This project wraps the functionality provided by libgit2. If you're using a versioned branch, install it to your system via your system's package manager and then install git2go.
|
||||
|
||||
|
||||
### Versioned branch, dynamic linking
|
||||
|
@ -36,13 +52,42 @@ This project wraps the functionality provided by libgit2. If you're using a vers
|
|||
When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via Go modules, e.g. to work against libgit2 v1.2
|
||||
|
||||
```go
|
||||
goimports -w *.go
|
||||
import "github.com/libgit2/git2go/v36"
|
||||
```
|
||||
|
||||
### Versioned branch, static linking
|
||||
|
||||
Follow the instructions for [Versioned branch, dynamic linking](#versioned-branch-dynamic-linking), but pass the `-tags static,system_libgit2` flag to all `go` commands that build any binaries. For instance:
|
||||
|
||||
go build -tags static,system_libgit2 github.com/my/project/...
|
||||
go test -tags static,system_libgit2 github.com/my/project/...
|
||||
go install -tags static,system_libgit2 github.com/my/project/...
|
||||
|
||||
### `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 `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/v36 => ../../libgit2/git2go
|
||||
|
||||
Parallelism and network operations
|
||||
----------------------------------
|
||||
|
||||
libgit2 may use OpenSSL and LibSSH2 for performing encrypted network connections. For now, libgit2 asks libgit2 to set locking for OpenSSL. This makes HTTPS connections thread-safe, but it is fragile and will likely stop doing it soon. This may also make SSH connections thread-safe if your copy of libssh2 is linked against OpenSSL. Check libgit2's `THREADSAFE.md` for more information.
|
||||
libgit2 may use OpenSSL and LibSSH2 for performing encrypted network connections. For now, git2go asks libgit2 to set locking for OpenSSL. This makes HTTPS connections thread-safe, but it is fragile and will likely stop doing it soon. This may also make SSH connections thread-safe if your copy of libssh2 is linked against OpenSSL. Check libgit2's `THREADSAFE.md` for more information.
|
||||
|
||||
Running the tests
|
||||
-----------------
|
||||
|
@ -64,6 +109,6 @@ M to the I to the T. See the LICENSE file if you've never seen an MIT license be
|
|||
Authors
|
||||
-------
|
||||
|
||||
- Carlos Martín (github@carlosmn)
|
||||
- Vicent Martí (github@vmg)
|
||||
- Carlos Martín (@carlosmn)
|
||||
- Vicent Martí (@vmg)
|
||||
|
||||
|
|
2
git.go
2
git.go
|
@ -171,7 +171,7 @@ func initLibGit2() {
|
|||
}
|
||||
|
||||
// Shutdown frees all the resources acquired by libgit2. Make sure no
|
||||
// references to any libgit2 go objects are live before calling this.
|
||||
// references to any git2go objects are live before calling this.
|
||||
// After this is called, invoking any function from this library will result in
|
||||
// undefined behavior, so make sure this is called carefully.
|
||||
func Shutdown() {
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
module github.com/libgit2/git2go/v36
|
||||
|
||||
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=
|
2
http.go
2
http.go
|
@ -103,7 +103,7 @@ func (t *httpSmartSubtransport) Action(url string, action SmartServiceAction) (S
|
|||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "git/2.0 (libgit2)")
|
||||
req.Header.Set("User-Agent", "git/2.0 (git2go)")
|
||||
|
||||
stream := newManagedHttpStream(t, req)
|
||||
if req.Method == "POST" {
|
||||
|
|
|
@ -479,7 +479,7 @@ func (r *Rebase) Free() {
|
|||
}
|
||||
|
||||
func newRebaseFromC(ptr *C.git_rebase, repo *Repository, opts *C.git_rebase_options) *Rebase {
|
||||
rebase := &Rebase{ptr: ptr, r: repo, options: opts}
|
||||
rebase := &Rebase{ptr: ptr, r: repo, options: opts}
|
||||
runtime.SetFinalizer(rebase, (*Rebase).Free)
|
||||
return rebase
|
||||
}
|
||||
|
|
100
rebase_test.go
100
rebase_test.go
|
@ -8,8 +8,8 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ProtonMail/go-crypto/openpgp"
|
||||
"github.com/ProtonMail/go-crypto/openpgp/packet"
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
)
|
||||
|
||||
// Tests
|
||||
|
@ -19,73 +19,73 @@ func TestRebaseInMemoryWithConflict(t *testing.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)
|
||||
// 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)
|
||||
_, 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)
|
||||
_, err = commitSomething(repo, "common-file", "x\nb\nc\n", commitOptions{})
|
||||
checkFatal(t, err)
|
||||
|
||||
branchA, err := repo.LookupBranch("branch-a", BranchLocal)
|
||||
checkFatal(t, err)
|
||||
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)
|
||||
|
||||
// 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})
|
||||
index, err := rebase.InmemoryIndex()
|
||||
checkFatal(t, err)
|
||||
|
||||
_, err = rebase.Next()
|
||||
checkFatal(t, err)
|
||||
// We simply resolve the conflict and commit the rebase.
|
||||
if !index.HasConflicts() {
|
||||
t.Fatal("expected index to have conflicts")
|
||||
}
|
||||
|
||||
index, err := rebase.InmemoryIndex()
|
||||
checkFatal(t, err)
|
||||
conflict, err := index.Conflict("common-file")
|
||||
checkFatal(t, err)
|
||||
|
||||
// We simply resolve the conflict and commit the rebase.
|
||||
if !index.HasConflicts() {
|
||||
t.Fatal("expected index to have conflicts")
|
||||
}
|
||||
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
|
||||
checkFatal(t, err)
|
||||
|
||||
conflict, err := index.Conflict("common-file")
|
||||
checkFatal(t, err)
|
||||
resolvedEntry := *conflict.Our
|
||||
resolvedEntry.Id = resolvedBlobID
|
||||
checkFatal(t, index.Add(&resolvedEntry))
|
||||
checkFatal(t, index.RemoveConflict("common-file"))
|
||||
|
||||
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
|
||||
checkFatal(t, err)
|
||||
var commitID Oid
|
||||
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
|
||||
checkFatal(t, rebase.Finish())
|
||||
|
||||
resolvedEntry := *conflict.Our
|
||||
resolvedEntry.Id = resolvedBlobID
|
||||
checkFatal(t, index.Add(&resolvedEntry))
|
||||
checkFatal(t, index.RemoveConflict("common-file"))
|
||||
// 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())
|
||||
}
|
||||
|
||||
var commitID Oid
|
||||
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
|
||||
checkFatal(t, rebase.Finish())
|
||||
tree, err := commit.Tree()
|
||||
checkFatal(t, err)
|
||||
|
||||
// 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()))
|
||||
}
|
||||
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) {
|
||||
|
@ -319,7 +319,7 @@ func checkCommitSigned(t *testing.T, entity *openpgp.Entity, commit *Commit) err
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = openpgp.CheckArmoredDetachedSignature(openpgp.EntityList{entity}, strings.NewReader(signedData), bytes.NewBufferString(signature), nil)
|
||||
_, err = openpgp.CheckArmoredDetachedSignature(openpgp.EntityList{entity}, strings.NewReader(signedData), bytes.NewBufferString(signature))
|
||||
if err != nil {
|
||||
t.Logf("Commit is not signed correctly\n%s", commit.ContentToSign())
|
||||
return err
|
||||
|
|
|
@ -12,7 +12,6 @@ import "C"
|
|||
import (
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
@ -686,7 +685,7 @@ func (c *RemoteCollection) Create(name string, url string) (*Remote, error) {
|
|||
return remote, nil
|
||||
}
|
||||
|
||||
// CreateWithOptions Creates a repository object with extended options.
|
||||
//CreateWithOptions Creates a repository object with extended options.
|
||||
func (c *RemoteCollection) CreateWithOptions(url string, option *RemoteCreateOptions) (*Remote, error) {
|
||||
remote := &Remote{repo: c.repo}
|
||||
|
||||
|
@ -984,9 +983,7 @@ func populateFetchOptions(copts *C.git_fetch_options, opts *FetchOptions, errorT
|
|||
}
|
||||
populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget)
|
||||
copts.prune = C.git_fetch_prune_t(opts.Prune)
|
||||
fmt.Println("populateFetchOptions() is broken. fixme!")
|
||||
// fix this line: ./remote.go:988:27: cannot use cbool(opts.UpdateFetchhead) (value of type _Ctype_int) as _Ctype_uint value in assignment
|
||||
// copts.update_fetchhead = cbool(opts.UpdateFetchhead)
|
||||
copts.update_fetchhead = cbool(opts.UpdateFetchhead)
|
||||
copts.download_tags = C.git_remote_autotag_option_t(opts.DownloadTags)
|
||||
|
||||
copts.custom_headers = C.git_strarray{
|
||||
|
|
|
@ -36,6 +36,9 @@ 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
|
||||
// Worktrees represents the collection of worktrees and can be used to
|
||||
// add, list and remove worktrees for this repository
|
||||
Worktrees WorktreeCollection
|
||||
|
||||
// weak indicates that a repository is a weak pointer and should not be
|
||||
// freed.
|
||||
|
@ -52,6 +55,7 @@ func newRepositoryFromC(ptr *C.git_repository) *Repository {
|
|||
repo.Notes.repo = repo
|
||||
repo.Tags.repo = repo
|
||||
repo.Stashes.repo = repo
|
||||
repo.Worktrees.repo = repo
|
||||
|
||||
runtime.SetFinalizer(repo, (*Repository).Free)
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Install libgit2 to go libgit2 in dynamic mode on Travis
|
||||
# Install libgit2 to git2go in dynamic mode on Travis
|
||||
#
|
||||
|
||||
set -ex
|
||||
|
|
|
@ -31,7 +31,7 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
// globalRegisteredSmartTransports is a mapping of global, libgit2 go-managed
|
||||
// globalRegisteredSmartTransports is a mapping of global, git2go-managed
|
||||
// transports.
|
||||
globalRegisteredSmartTransports = struct {
|
||||
sync.Mutex
|
||||
|
@ -41,7 +41,7 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
// unregisterManagedTransports unregisters all libgit2 go-managed transports.
|
||||
// unregisterManagedTransports unregisters all git2go-managed transports.
|
||||
func unregisterManagedTransports() error {
|
||||
globalRegisteredSmartTransports.Lock()
|
||||
originalTransports := globalRegisteredSmartTransports.transports
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 3e2baa6d0bfb42f9016e24cba1733a6ae26a8ae6
|
|
@ -0,0 +1,271 @@
|
|||
package git
|
||||
|
||||
/*
|
||||
#include <git2.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type WorktreeCollection struct {
|
||||
doNotCompare
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
type Worktree struct {
|
||||
doNotCompare
|
||||
ptr *C.git_worktree
|
||||
}
|
||||
|
||||
type AddWorktreeOptions struct {
|
||||
// Lock the newly created worktree
|
||||
Lock bool
|
||||
// Reference to use for the new worktree
|
||||
Reference *Reference
|
||||
// CheckoutOptions is used for configuring the checkout for the newly created worktree
|
||||
CheckoutOptions CheckoutOptions
|
||||
}
|
||||
|
||||
// Add adds a new working tree for the given repository
|
||||
func (c *WorktreeCollection) Add(name string, path string, options *AddWorktreeOptions) (*Worktree, error) {
|
||||
cName := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cName))
|
||||
|
||||
cPath := C.CString(path)
|
||||
defer C.free(unsafe.Pointer(cPath))
|
||||
|
||||
var err error
|
||||
cOptions := populateAddWorktreeOptions(&C.git_worktree_add_options{}, options, &err)
|
||||
defer freeAddWorktreeOptions(cOptions)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var ptr *C.git_worktree
|
||||
ret := C.git_worktree_add(&ptr, c.repo.ptr, cName, cPath, cOptions)
|
||||
runtime.KeepAlive(c)
|
||||
if options != nil && options.Reference != nil {
|
||||
runtime.KeepAlive(options.Reference)
|
||||
}
|
||||
|
||||
if ret == C.int(ErrorCodeUser) && err != nil {
|
||||
return nil, err
|
||||
} else if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return newWorktreeFromC(ptr), nil
|
||||
}
|
||||
|
||||
// List lists names of linked working trees for the given repository
|
||||
func (c *WorktreeCollection) List() ([]string, error) {
|
||||
var strC C.git_strarray
|
||||
defer C.git_strarray_dispose(&strC)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_worktree_list(&strC, c.repo.ptr)
|
||||
runtime.KeepAlive(c)
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
|
||||
w := makeStringsFromCStrings(strC.strings, int(strC.count))
|
||||
return w, nil
|
||||
}
|
||||
|
||||
// Lookup gets a working tree by its name for the given repository
|
||||
func (c *WorktreeCollection) Lookup(name string) (*Worktree, error) {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var ptr *C.git_worktree
|
||||
ret := C.git_worktree_lookup(&ptr, c.repo.ptr, cname)
|
||||
runtime.KeepAlive(c)
|
||||
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
} else if ptr == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return newWorktreeFromC(ptr), nil
|
||||
}
|
||||
|
||||
// OpenFromRepository retrieves a worktree for the given repository
|
||||
func (c *WorktreeCollection) OpenFromRepository() (*Worktree, error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
var ptr *C.git_worktree
|
||||
ret := C.git_worktree_open_from_repository(&ptr, c.repo.ptr)
|
||||
runtime.KeepAlive(c)
|
||||
|
||||
if ret < 0 {
|
||||
return nil, MakeGitError(ret)
|
||||
}
|
||||
return newWorktreeFromC(ptr), nil
|
||||
}
|
||||
|
||||
func newWorktreeFromC(ptr *C.git_worktree) *Worktree {
|
||||
worktree := &Worktree{ptr: ptr}
|
||||
runtime.SetFinalizer(worktree, (*Worktree).Free)
|
||||
return worktree
|
||||
}
|
||||
|
||||
func freeAddWorktreeOptions(cOptions *C.git_worktree_add_options) {
|
||||
if cOptions == nil {
|
||||
return
|
||||
}
|
||||
freeCheckoutOptions(&cOptions.checkout_options)
|
||||
}
|
||||
|
||||
func populateAddWorktreeOptions(cOptions *C.git_worktree_add_options, options *AddWorktreeOptions, errorTarget *error) *C.git_worktree_add_options {
|
||||
C.git_worktree_add_options_init(cOptions, C.GIT_WORKTREE_ADD_OPTIONS_VERSION)
|
||||
if options == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
populateCheckoutOptions(&cOptions.checkout_options, &options.CheckoutOptions, errorTarget)
|
||||
cOptions.lock = cbool(options.Lock)
|
||||
if options.Reference != nil {
|
||||
cOptions.ref = options.Reference.ptr
|
||||
}
|
||||
return cOptions
|
||||
}
|
||||
|
||||
// Free a previously allocated worktree
|
||||
func (w *Worktree) Free() {
|
||||
runtime.SetFinalizer(w, nil)
|
||||
C.git_worktree_free(w.ptr)
|
||||
}
|
||||
|
||||
// IsLocked checks if the given worktree is locked
|
||||
func (w *Worktree) IsLocked() (locked bool, reason string, err error) {
|
||||
buf := C.git_buf{}
|
||||
defer C.git_buf_dispose(&buf)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_worktree_is_locked(&buf, w.ptr)
|
||||
runtime.KeepAlive(w)
|
||||
|
||||
if ret < 0 {
|
||||
return false, "", MakeGitError(ret)
|
||||
}
|
||||
return ret != 0, C.GoString(buf.ptr), nil
|
||||
}
|
||||
|
||||
type WorktreePruneFlag uint32
|
||||
|
||||
const (
|
||||
// WorktreePruneValid means prune working tree even if working tree is valid
|
||||
WorktreePruneValid WorktreePruneFlag = C.GIT_WORKTREE_PRUNE_VALID
|
||||
// WorktreePruneLocked means prune working tree even if it is locked
|
||||
WorktreePruneLocked WorktreePruneFlag = C.GIT_WORKTREE_PRUNE_LOCKED
|
||||
// WorktreePruneWorkingTree means prune checked out working tree
|
||||
WorktreePruneWorkingTree WorktreePruneFlag = C.GIT_WORKTREE_PRUNE_WORKING_TREE
|
||||
)
|
||||
|
||||
// IsPrunable checks that the worktree is prunable with the given flags
|
||||
func (w *Worktree) IsPrunable(flags WorktreePruneFlag) (bool, error) {
|
||||
cOptions := C.git_worktree_prune_options{}
|
||||
C.git_worktree_prune_options_init(&cOptions, C.GIT_WORKTREE_PRUNE_OPTIONS_VERSION)
|
||||
cOptions.flags = C.uint32_t(flags)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_worktree_is_prunable(w.ptr, &cOptions)
|
||||
runtime.KeepAlive(w)
|
||||
|
||||
if ret < 0 {
|
||||
return false, MakeGitError(ret)
|
||||
}
|
||||
return ret != 0, nil
|
||||
}
|
||||
|
||||
// Lock locks the worktree if not already locked
|
||||
func (w *Worktree) Lock(reason string) error {
|
||||
var cReason *C.char
|
||||
if reason != "" {
|
||||
cReason = C.CString(reason)
|
||||
defer C.free(unsafe.Pointer(cReason))
|
||||
}
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_worktree_lock(w.ptr, cReason)
|
||||
runtime.KeepAlive(w)
|
||||
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Name retrieves the name of the worktree
|
||||
func (w *Worktree) Name() string {
|
||||
s := C.GoString(C.git_worktree_name(w.ptr))
|
||||
runtime.KeepAlive(w)
|
||||
return s
|
||||
}
|
||||
|
||||
// Path retrieves the path of the worktree
|
||||
func (w *Worktree) Path() string {
|
||||
s := C.GoString(C.git_worktree_path(w.ptr))
|
||||
runtime.KeepAlive(w)
|
||||
return s
|
||||
}
|
||||
|
||||
// Prune the worktree with the provided flags
|
||||
func (w *Worktree) Prune(flags WorktreePruneFlag) error {
|
||||
cOptions := C.git_worktree_prune_options{}
|
||||
C.git_worktree_prune_options_init(&cOptions, C.GIT_WORKTREE_PRUNE_OPTIONS_VERSION)
|
||||
cOptions.flags = C.uint32_t(flags)
|
||||
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_worktree_prune(w.ptr, &cOptions)
|
||||
runtime.KeepAlive(w)
|
||||
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unlock a locked worktree
|
||||
func (w *Worktree) Unlock() (notLocked bool, err error) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_worktree_unlock(w.ptr)
|
||||
runtime.KeepAlive(w)
|
||||
|
||||
if ret < 0 {
|
||||
return false, MakeGitError(ret)
|
||||
}
|
||||
return ret != 0, nil
|
||||
}
|
||||
|
||||
// Validate checks if the given worktree is valid
|
||||
func (w *Worktree) Validate() error {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
ret := C.git_worktree_validate(w.ptr)
|
||||
runtime.KeepAlive(w)
|
||||
|
||||
if ret < 0 {
|
||||
return MakeGitError(ret)
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,225 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func assertSamePath(t *testing.T, expected string, actual string) {
|
||||
var err error
|
||||
expected, err = filepath.EvalSymlinks(expected)
|
||||
checkFatal(t, err)
|
||||
actual, err = filepath.EvalSymlinks(actual)
|
||||
checkFatal(t, err)
|
||||
|
||||
if expected != actual {
|
||||
t.Fatalf("wrong path (expected %s, got %s)", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddWorkspace(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
worktreeTemporaryPath, err := ioutil.TempDir("", "git2go")
|
||||
checkFatal(t, err)
|
||||
defer func() { checkFatal(t, os.RemoveAll(worktreeTemporaryPath)) }()
|
||||
worktreeName := "testWorktree"
|
||||
worktreePath := filepath.Join(worktreeTemporaryPath, "worktree")
|
||||
|
||||
worktree, err := repo.Worktrees.Add(worktreeName, worktreePath, &AddWorktreeOptions{
|
||||
Lock: true, CheckoutOptions: CheckoutOptions{Strategy: CheckoutForce},
|
||||
})
|
||||
checkFatal(t, err)
|
||||
|
||||
if name := worktree.Name(); name != worktreeName {
|
||||
t.Fatalf("wrong worktree name: %s != %s", worktreeName, name)
|
||||
}
|
||||
locked, _, err := worktree.IsLocked()
|
||||
checkFatal(t, err)
|
||||
if locked != true {
|
||||
t.Fatal("worktree isn't locked")
|
||||
}
|
||||
assertSamePath(t, worktreePath, worktree.Path())
|
||||
checkFatal(t, worktree.Validate())
|
||||
}
|
||||
|
||||
func TestAddWorkspaceWithoutOptions(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
worktreeTemporaryPath, err := ioutil.TempDir("", "git2go")
|
||||
checkFatal(t, err)
|
||||
defer func() { checkFatal(t, os.RemoveAll(worktreeTemporaryPath)) }()
|
||||
worktreeName := "testWorktree"
|
||||
worktreePath := filepath.Join(worktreeTemporaryPath, "worktree")
|
||||
|
||||
worktree, err := repo.Worktrees.Add(worktreeName, worktreePath, nil)
|
||||
checkFatal(t, err)
|
||||
|
||||
if name := worktree.Name(); name != worktreeName {
|
||||
t.Fatalf("wrong worktree name: %s != %s", worktreeName, name)
|
||||
}
|
||||
locked, _, err := worktree.IsLocked()
|
||||
checkFatal(t, err)
|
||||
if locked != false {
|
||||
t.Fatal("worktree is locked")
|
||||
}
|
||||
assertSamePath(t, worktreePath, worktree.Path())
|
||||
checkFatal(t, worktree.Validate())
|
||||
}
|
||||
|
||||
func TestLookupWorkspace(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
worktreeTemporaryPath, err := ioutil.TempDir("", "git2go")
|
||||
checkFatal(t, err)
|
||||
defer func() { checkFatal(t, os.RemoveAll(worktreeTemporaryPath)) }()
|
||||
worktreeName := "testWorktree"
|
||||
|
||||
worktree, err := repo.Worktrees.Add(worktreeName, filepath.Join(worktreeTemporaryPath, "worktree"), nil)
|
||||
checkFatal(t, err)
|
||||
retrievedWorktree, err := repo.Worktrees.Lookup(worktreeName)
|
||||
checkFatal(t, err)
|
||||
|
||||
assertSamePath(t, worktree.Path(), retrievedWorktree.Path())
|
||||
}
|
||||
|
||||
func TestListWorkspaces(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
worktreeTemporaryPath, err := ioutil.TempDir("", "git2go")
|
||||
checkFatal(t, err)
|
||||
defer func() { checkFatal(t, os.RemoveAll(worktreeTemporaryPath)) }()
|
||||
|
||||
worktreeNames := []string{"worktree1", "worktree2", "worktree3"}
|
||||
for _, name := range worktreeNames {
|
||||
_, err = repo.Worktrees.Add(name, filepath.Join(worktreeTemporaryPath, name), nil)
|
||||
checkFatal(t, err)
|
||||
}
|
||||
listedWorktree, err := repo.Worktrees.List()
|
||||
checkFatal(t, err)
|
||||
|
||||
if len(worktreeNames) != len(listedWorktree) {
|
||||
t.Fatalf("len(worktreeNames) != len(listedWorktree) as %d != %d", len(worktreeNames), len(listedWorktree))
|
||||
}
|
||||
for _, name := range worktreeNames {
|
||||
found := false
|
||||
for _, nameToMatch := range listedWorktree {
|
||||
if name == nameToMatch {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Fatalf("worktree %s is missing", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenWorkspaceFromRepository(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
worktreeTemporaryPath, err := ioutil.TempDir("", "git2go")
|
||||
checkFatal(t, err)
|
||||
defer func() { checkFatal(t, os.RemoveAll(worktreeTemporaryPath)) }()
|
||||
|
||||
worktree, err := repo.Worktrees.Add("testWorktree", filepath.Join(worktreeTemporaryPath, "worktree"), nil)
|
||||
checkFatal(t, err)
|
||||
worktreeRepo, err := OpenRepository(worktree.Path())
|
||||
checkFatal(t, err)
|
||||
worktreeFromRepo, err := worktreeRepo.Worktrees.OpenFromRepository()
|
||||
checkFatal(t, err)
|
||||
|
||||
if worktreeFromRepo.Name() != worktree.Name() {
|
||||
t.Fatalf("wrong name (expected %s, got %s)", worktreeFromRepo.Name(), worktree.Name())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorktreeIsPrunable(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
worktreeTemporaryPath, err := ioutil.TempDir("", "git2go")
|
||||
checkFatal(t, err)
|
||||
defer func() { checkFatal(t, os.RemoveAll(worktreeTemporaryPath)) }()
|
||||
|
||||
worktree, err := repo.Worktrees.Add("testWorktree", filepath.Join(worktreeTemporaryPath, "worktree"), nil)
|
||||
checkFatal(t, err)
|
||||
err = worktree.Lock("test")
|
||||
checkFatal(t, err)
|
||||
|
||||
isPrunableWithoutLockedFlag, err := worktree.IsPrunable(WorktreePruneValid)
|
||||
checkFatal(t, err)
|
||||
if isPrunableWithoutLockedFlag {
|
||||
t.Fatal("worktree shouldn't be prunable without the WorktreePruneLocked flag")
|
||||
}
|
||||
isPrunableWithLockedFlag, err := worktree.IsPrunable(WorktreePruneValid | WorktreePruneLocked)
|
||||
checkFatal(t, err)
|
||||
if !isPrunableWithLockedFlag {
|
||||
t.Fatal("worktree should be prunable with the WorktreePruneLocked flag")
|
||||
}
|
||||
|
||||
err = worktree.Prune(WorktreePruneValid | WorktreePruneLocked)
|
||||
checkFatal(t, err)
|
||||
}
|
||||
|
||||
func TestWorktreeCanBeLockedAndUnlocked(t *testing.T) {
|
||||
t.Parallel()
|
||||
repo := createTestRepo(t)
|
||||
defer cleanupTestRepo(t, repo)
|
||||
seedTestRepo(t, repo)
|
||||
|
||||
worktreeTemporaryPath, err := ioutil.TempDir("", "git2go")
|
||||
checkFatal(t, err)
|
||||
defer func() { checkFatal(t, os.RemoveAll(worktreeTemporaryPath)) }()
|
||||
|
||||
worktree, err := repo.Worktrees.Add("testWorktree", filepath.Join(worktreeTemporaryPath, "worktree"), nil)
|
||||
checkFatal(t, err)
|
||||
notLocked, err := worktree.Unlock()
|
||||
checkFatal(t, err)
|
||||
if !notLocked {
|
||||
t.Fatal("worktree should be unlocked by default")
|
||||
}
|
||||
|
||||
expectedReason := "toTestIt"
|
||||
err = worktree.Lock(expectedReason)
|
||||
checkFatal(t, err)
|
||||
isLocked, reason, err := worktree.IsLocked()
|
||||
checkFatal(t, err)
|
||||
if !isLocked {
|
||||
t.Fatal("worktree should be locked after the locking operation")
|
||||
}
|
||||
if expectedReason != reason {
|
||||
t.Fatalf("locked reason doesn't match: %s != %s", expectedReason, reason)
|
||||
}
|
||||
|
||||
notLocked, err = worktree.Unlock()
|
||||
checkFatal(t, err)
|
||||
if notLocked {
|
||||
t.Fatal("worktree was lock before so notLocked should be false")
|
||||
}
|
||||
isLocked, _, err = worktree.IsLocked()
|
||||
checkFatal(t, err)
|
||||
if isLocked {
|
||||
t.Fatal("worktree should be unlocked after the Unlock() call")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue