Compare commits

..

213 Commits

Author SHA1 Message Date
Jeff Carr 9c30d41e40 using /opt/libgit2 for test builds 2025-02-14 17:57:12 -06:00
Jeff Carr c9a7e84e5b try build against 1.8.x
Signed-off-by: Jeff Carr <jcarr@wit.com>
2025-01-05 01:21:58 -06:00
Jeff Carr a4ee4aafbc lame
Signed-off-by: Jeff Carr <jcarr@wit.com>
2024-12-17 22:04:22 -06:00
Jeff Carr cf84056045 Merge branch 'devel' 2024-12-17 15:02:44 -06:00
Jeff Carr 585a8dbbe5 rename as libgit2 2024-12-17 15:01:59 -06:00
Jeff Carr d5a0e0056f better gitpb 2024-12-17 13:19:35 -06:00
Jeff Carr 530f3618a8 update to libgit2 version 1.8.4. dump old github and vendor stuff
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/git2go

Signed-off-by: Jeff Carr <jcarr@wit.com>
2024-12-16 17:50:57 -06:00
Roland Shoemaker e65b1c188c Replace golang.org/x/crypto/openpgp with github.com/ProtonMail/go-crypto
The golang.org/x/crypto/openpgp library has been deprecated for over a
year now (see golang.org/issue/44226, and the deprecation notice in the
package documentation). The library is unmaintained and has a number of
API and usability issues. ProtonMail maintains a community fork which
is actively maintained, and for most cases is a drop-in replacement.

This change switches usages of golang.org/x/crypto/openpgp/... with
github.com/ProtonMail/go-crypto/openpgp/..., the only other code changes
are adding a nil packet.Config to a openpgp.CheckArmoredDetachedSignature
call.

(This change is part of a wider effort by the Go Security team to remove
usages of golang.org/x/crypto/openpgp from the Go ecosystem.)
2022-12-09 12:43:11 -08:00
lhchavez 4b14d29c20
Fix the `github-tag-action` workflow (#932)
This has been failing for a while because of some changes in `git`.
2022-10-04 18:12:23 -07:00
Sanskar Jaiswal c1ec21d89c
libgit2 v1.5.0 #major (#929)
Update libgit2 to v1.5.0. Replace `SmartProxyOptions()` with `SmartRemoteConnectOptions()`.

Fixes: https://github.com/libgit2/git2go/issues/899

Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
Co-authored-by: lhchavez <lhchavez@lhchavez.com>
2022-10-04 07:50:57 -07:00
Calin 9db5de109c
Set BasicAuth in http.go only if username and password are not empty (#914)
This prevents error "read/write on closed pipe".

Go's http.client::send() always closes req.Body, so if the first request attempt
is unsuccessful, any subsequent requests after calling the `CredentialsCallback`
will attempt to read/write on a closed pipe.
2022-07-13 04:50:31 -07:00
lhchavez 7bff4ca7ad
Uprev libgit to v1.3.1 (#911)
🔒 This is a security release to provide compatibility with git's
changes to address [CVE
2022-24765](https://github.blog/2022-04-12-git-security-vulnerability-announced/).

libgit2 (and by extension git2go) are not directly affected by this
vulnerability, because libgit2 does not directly invoke any executable.
But we are providing these changes as a security release for any users
that use libgit2 for repository discovery and then also use git on that
repository. In this release, we will now validate that the user opening
the repository is the same user that owns the on-disk repository. This
is to match git's behavior.

In addition, we are providing several correctness fixes where invalid
input can lead to a crash. These may prevent possible denial of service
attacks. At this time there are not known exploits to these issues.
2022-04-14 07:09:59 -07:00
William Bain eae00773cc
Add refspec bindings (#898)
This add support for the parse, access, and transform functions for
refspec objects.
2022-02-24 19:21:35 -08:00
Patrick Steinhardt e7d1b2b69f
rebase: Add wrapper for `git_rebase_inmemory_index()` (#900)
* rebase: Fix missing initialization of the repo pointer

While the `Rebase` structure has a pointer to the repository the rebase
is creatde in, this pointer isn't ever initialized. Fix this.

* rebase: Add wrapper for `git_rebase_inmemory_index()`

Add a new wrapper for `git_rebase_inmemory_index()`, which can be used
to retrieve the index for an in-memory rebase.

Co-authored-by: Patrick Steinhardt <psteinhardt@gitlab.com>
2022-02-24 05:27:26 -08:00
Dylan Richardson c598ea5718
readme: link to godoc for current main branch (#886)
The GoDoc link currently refers to the initial version of this package, but usually folks are looking
for whatever matches the current main branch. This should be updated whenever the package
version changes.
2022-01-22 17:54:04 -08:00
Aurélien 5eca48cda9
Add ProxyOptions for push operations (#872)
Analog to #623 but for push operations rather than fetch.
2022-01-17 19:02:14 -08:00
James Fargher 1fcc9d8743
Add EnableFsyncGitDir to enable synchronized writes to the gitdir (#874)
This adds support for the GIT_OPT_ENABLE_FSYNC_GITDIR option in libgit2.

Co-authored-by: James Fargher <jfargher@gitlab.com>
2022-01-17 18:51:39 -08:00
Kirill 5e35338d58
Generate stringer files automatically (#841)
Added `stringer` annotations to `git.go` for `ErrorClass` and
`ErrorCode`. Added `generate` rule for `Makefile` to generate
string representations for these types (first building cgo files
in `_obj` dir to get C constants). Finally, updated `ci` actions
workflow to check that generated files are up to date.

Fixes: #543
2021-11-08 06:38:55 -08:00
Ignacio Taranto 533c82f270
Fix replace statement example in README.md (#859) 2021-11-08 06:02:36 -08:00
Sunny 6cea7a7a59
Make ssh commands used in the git smart transport compatible with libgit2 (#852)
* Fix ssh commands used in go SmartSubtransport

Before the fix, the commands sent were of the form:

```
git-upload-pack "/bar/test-reponame"
```

This resulted in the git server returning error:
`error parsing command: invalid git command`

This change replaces the double quotes with single quotes:

```
git-upload-pack '/bar/test-reponame'
```

* Update ssh.go

Co-authored-by: lhchavez <lhchavez@lhchavez.com>
2021-11-07 12:20:56 -08:00
Yashodhan Ghadge 0e8009f00a
bugfix: HTTPS Clone fails with remote pointer not found using Go transport (#836) (#842)
Fixes: #836 

Changes:

* adding a weak bool param for Remote
* create a new remote in the smartTransportCallback incase one is not found
2021-10-23 10:33:10 -07:00
lhchavez 6eae74c128
libgit2 v1.3.0 #major (#840)
This commit introduces libgit2 v1.3.0 to git2go, which brings a large
number of [bugfixes and
features](https://github.com/libgit2/libgit2/releases/tag/v1.3.0).

This also marks the start of the v33 release.
2021-10-14 04:42:42 -07:00
lhchavez 9b155184fe
Allow skipping an entry expansion in `tree.Walk()` (#838)
It is now possible to skip expanding an entry in `tree.Walk()` by
returning `TreeWalkSkip`, in addition to stopping altogether by
returning a non-nil error.

Fixes: #837
2021-09-30 09:24:49 -07:00
lhchavez c6da3b97a8
Allow building libgit2 with Chromium zlib (#831)
This change allows the caller to set the `USE_CHROMIUM_ZLIB=ON`
environment variable to use the Chromium implementation of zlib when
building libgit2.
2021-09-06 05:13:55 -07:00
lhchavez c8ce59d4eb
Add support for Repository.ReachableFromAny() (#826)
This change exposes the binding for `git_graph_reachable_from_any()`.
2021-09-05 20:06:56 -07:00
lhchavez 922f2f7487
Add support for Odb.MultiPackIndex() (#819)
This change exposes the binding for `git_odb_write_multi_pack_index()`.
2021-09-05 19:03:26 -07:00
lhchavez dcc9331226
Expose the ssh.PublicKey into the CertificateCheckCallback (#818)
This change exposes the raw SSH hostkey and the ssh.PublicKey into the
CertificateCheckCallback, so that callers can do better validations.
2021-09-05 19:00:59 -07:00
lhchavez 018647fd48 libgit2 v1.2.0 #major
This commit introduces libgit2 v1.2.0 to git2go, which brings a large
number of [bugfixes and
features](https://github.com/libgit2/libgit2/releases/tag/v1.2.0).

This also marks the start of the v32 release.
2021-09-05 18:52:01 -07:00
lhchavez b78bde3d74 Make all Options objects consistent
This change makes all Options objects have child Option fields as values
(instead of pointers) to mirror the libgit2 interface. It also names
them Options instead of Opts to match the current libgit2 nomenclature
and removes the Version fields.
2021-09-05 18:52:01 -07:00
lhchavez 5def02a589 The big Callback type adjustment of 2020
This change makes all callbacks that can fail return an `error`. This
makes things a lot more idiomatic.
2021-09-05 18:52:01 -07:00
lhchavez 70e5e419cf
Add support for managed SSH transport #minor (#814)
This change drops the (hard) dependency on libssh2 and instead uses Go's
implementation of SSH when libgit2 is not built with it.
2021-09-05 17:04:40 -07:00
lhchavez b983e1daeb
Add support for managed HTTP/S transports (#810)
This change uses the newly-exposed Transport interface to use Go's
implementation of http.Client instead of httpclient via libgit2.
2021-09-05 16:39:07 -07:00
lhchavez f1fa96c7b7
Add support for custom smart transports (#806)
This change adds support for git smart transports. This will be then
used to implement http, https, and ssh transports that don't rely on the
libgit2 library.
2021-09-05 15:44:18 -07:00
lhchavez dbe032c347
Make all non-user-creatable structures non-comparable (#802)
This change makes all non-user-creatable structures non-comparable. This
makes it easier to add changes later that don't introduce breaking
changes from the go compatibility guarantees perspective.

This, of course, implies that this change _is_ a breaking change, but since
these structures are not intended to be created by users (or de-referenced),
it should be okay.
2021-09-05 13:59:36 -07:00
lhchavez 549706bb57
Declare forward-compatibility with libgit2 v1.2.0 #minor (#800)
We can't yet ship a fully libgit2 v1.2.0-compatible library due to a
missing public symbol, but we can allow the v1.1.0-era codebase to link
against libgit2 v1.2.0 in the meantime.
2021-09-04 20:07:24 -07:00
lhchavez 2077003fa5
Prepare for the v1.2.0 release (#796)
This change adds a few more deprecation messages just before we remove
them.
2021-09-04 14:14:47 -07:00
Gustav Westling d4524761d9
Add DiffIgnoreWitespaceEol and deprecate DiffIgnoreWitespaceEol (#774)
DiffIgnoreWitespaceEol contains a typo and does not have the same name as it's libgit2 counterpart.

Fixes #773
2021-09-04 13:54:21 -07:00
lhchavez 15434610fe
Add `CreateCommitWithSignature` (#782)
This change adds the wrapper for `git_commit_create_with_signature`.
2021-09-04 13:49:01 -07:00
lhchavez be5a99a807
Rename the default branch to `main` (#786)
We've renamed the default branch from `master` to `main`, so we need to
change a bunch of references to that.
2021-09-04 13:33:34 -07:00
lhchavez fbaf9d1d1a
Add `Repository.CreateCommitBuffer` (#781)
This commit adds the Go binding for `git_commit_create_buffer`. This
will be used to support the 1.2.0 commit create callback.
2021-09-04 13:04:58 -07:00
lhchavez df7084d36a
Remove the legacy builders (#776)
These builds are no longer working because some of the dependencies now
require newer versions of Go. Seems like the ecosystem has moved to Go
1.11+, so we are now forced to follow suit.
2021-09-03 06:28:00 -07:00
Vladimir Buzuev 1e2cb92b48
add wrapper for git_config_open_default (#758) 2021-04-03 18:45:55 -07:00
Vladimir Buzuev 0d7c8dadb4
fix buldled static build on Windows/MinGW (#761)
seems like need more libraries in LDFLAGS:

* ws2_32 for socket, connect, htonl, etc
* ole32 for CoInitializeEx
* rpcrt4 for UuidCreate
* crypt32 for CertFreeCertificateContext
2021-04-03 18:45:09 -07:00
Vladimir Buzuev a4d202ed7b
Git repository item path (#757)
add wrapper for `git_repository_item_path`
2021-04-03 16:52:34 -07:00
michael boulton aeb22bcf7d
Make index time fields public (#750)
From gorelease:
```
Compatible changes:
- IndexTime.Nanoseconds: added
- IndexTime.Seconds: added
```
There are no extra tests because there isn't really anything to test

closes #304
2021-02-15 07:37:55 -08:00
Suhaib Mujahid f6c5753df8
fix: Use `err` instead of error as a variable name for errors (#746)
fix #745
2021-02-15 07:26:19 -08:00
Byoungchan Lee 2fd0495c43
Implement git_repository_set_config (#735)
Closes #732
2021-02-03 19:33:03 -08:00
Byoungchan Lee 73d97b9bbe
Support git_remote_create_with_opts (#733)
Closes #645
2021-02-03 18:58:31 -08:00
Byoungchan Lee 07147a8ea8
Support git_repository_message, git_repository_message_remove (#734)
Closes #646
2021-02-02 19:42:21 -08:00
lhchavez 4b2ac7c998
Rename the build files (#724)
This change renames the build files so they come lexicographically
before any source files. This makes the compile errors (due to
mismatched libgit2 versions) easier to understand, since the
`Build_*.go` files will be tried before the rest, and the `#error` in
those files will kick in, leading to a much better experience.

This unfortunately goes a bit against the defacto standard of using only
lowercase characters in filenames, but the better developer experience
(and better self-diagnosis when things go wrong instead of having to
open a new issue) is worth the deviation.

Fixes: #711
Fixes: #617
2020-12-13 15:20:16 -08:00
lhchavez 698ddfb4ac
Support more MergeBase functions (#720)
This change adds support for MergeBaseMany, MergeBasesMany, and
MergeBaseOctopus.
2020-12-13 10:35:34 -08:00
lhchavez 10c67474a8
More callback refactoring (#713)
This change:

* Gets rid of the `.toC()` functions for Options objects, since they
  were redundant with the `populateXxxOptions()`.
* Adds support for `errorTarget` to the `RemoteOptions`, since they are
  used in the same stack for some functions (like `Fetch()`). Now for
  those cases, the error returned by the callback will be preserved
  as-is.
2020-12-10 07:19:41 -08:00
lhchavez e28cce87c7
Ensure that no pointer handles leak during the test (#712)
This change makes sure that pointer handles are correctly cleaned up
during tests.
2020-12-10 05:35:40 -08:00
lhchavez abf02bc7d7
Add `NewCredentialSSHKeyFromSigner` (#706)
This change adds `NewCredentialSSHKeyFromSigner`, which allows idiomatic
use of SSH keys from Go. This also lets us spin off an SSH server in the
tests.
2020-12-06 11:55:04 -08:00
lhchavez 54afccfa0f
Build improvements (#707)
This change makes the test be verbose and use parallelization if
possible (when using gmake to build).
2020-12-06 06:13:38 -08:00
lhchavez 5d8eaf7e65
Refactor all callbacks (#700)
This change is a preparation for another change that makes all callback
types return a Go error instead of an error code / an integer. That is
going to make make things a lot more idiomatic.

The reason this change is split is threefold:

a) This change is mostly mechanical and should contain no semantic
   changes.
b) This change is backwards-compatible (in the Go API compatibility
   sense of the word), and thus can be backported to all other releases.
c) It makes the other change a bit smaller and more focused on just one
   thing.

Concretely, this change makes all callbacks populate a Go error when
they fail. If the callback is invoked from the same stack as the
function to which it was passed (e.g. for `Tree.Walk`), it will preserve
the error object directly into a struct that also holds the callback
function. Otherwise if the callback is pased to one func and will be
invoked when run from another one (e.g. for `Repository.InitRebase`),
the error string is saved into the libgit2 thread-local storage and then
re-created as a `GitError`.
2020-12-05 13:13:59 -08:00
lhchavez 137c05e802
Mark some symbols to be deprecated #minor (#698)
This change introduces the file deprecated.go, which contains any
constants, functions, and types that are slated to be deprecated in the
next major release.

These symbols are deprecated because they refer to old spellings in
pre-1.0 libgit2. This also makes the build be done with the
`-DDEPRECATE_HARD` flag to avoid regressions.

This, together with
[gorelease](https://godoc.org/golang.org/x/exp/cmd/gorelease)[1] should
make releases safer going forward.

1: More information about how that works at
   https://go.googlesource.com/exp/+/refs/heads/master/apidiff/README.md
2020-12-05 07:23:44 -08:00
nmeum 1fabe95fb7
Relax libgit2 minor version check (#696)
The major version must still be an exact match since libgit2 uses
semantic versioning and changes to the major number indicate backwards
incompatible changes to the API.

Fixes: #695
2020-11-28 11:10:34 -08:00
Hans Duedal 7497529f70
Expose GIT_CERT_SSH_SHA256 (#690)
Newer versions of libssh2 use SHA256 fingerprints
2020-11-26 17:32:24 -08:00
Segev Finer 2bd574b6bd
Add ReferenceNormalizeName (#681) 2020-11-13 16:50:59 -08:00
Devendra 2d639d8e49
Travis-ci: added support for ppc64le (#682)
Added power support for the travis.yml file with ppc64le. This is part of the Ubuntu distribution for ppc64le. This helps us simplify testing later when distributions are re-building and re-releasing.
2020-11-13 05:32:44 -08:00
Suhaib Mujahid ccbe4719bd
Add GIT_BLAME_IGNORE_WHITESPACE flag (#677)
The `GIT_BLAME_IGNORE_WHITESPACE` blame option flag was introduced in libgit2 v1.1.0

Change type: #minor
2020-11-07 15:02:03 -08:00
Suhaib Mujahid b46ebfab8c
Add GIT_BLAME_USE_MAILMAP flag (#676)
The `GIT_BLAME_USE_MAILMAP` blame option flag was introduced in libgit2 v0.28

Change type: #minor
2020-11-07 14:54:23 -08:00
lhchavez 77460dd7f0
Use the correct branch name for backporting into v1.0 (#674)
This change uses the correct branch name (`release-1.0`) to backport
changes into the v1.0 branch.
2020-11-02 19:15:32 -08:00
Suhaib Mujahid c3664193f3
feat: Implement an option to control hash verification (#671)
Add a binding to enable/disable hash verification using the `GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION` option.

Change type: #minor
2020-11-02 18:36:20 -08:00
Suhaib Mujahid ad3ec3664d
Create v31 to support libgit2 v1.1.x (#668)
The libgit2 `v1.1.0` is released two weeks ago. This PR allows `git2go` to link against the new version.
2020-10-26 18:09:26 -07:00
lhchavez f83530b18d
CI refresh (#666)
This change:

* Makes the Travis tests only run tip, since the rest of the Go versions are better served by GitHub Actions.
* Use Go 1.15 in the CI. This has been released for a while.
2020-10-23 05:47:14 -07:00
Sami Hiltunen 10d5ebf231
apply: Add bindings for git_apply_to_tree (#657)
Adds bindings to the git_apply_to_tree function that allows applying
a diff directly to a tree.
2020-10-23 05:17:38 -07:00
lhchavez 3a4204bd93
Make `TestApplyDiffAddFile()` explicitly `.Free()` stuff (#661)
This change adds explicit `.Free()` calls in `TestApplyDiffAddFile()`.
It was discovered in #657 that some objects were not explicitly being
freed, so this fixes that!
2020-10-22 06:30:31 -07:00
Suhaib Mujahid 5b6ce70b89
refactor: Use undeprecated options init (#656)
This PR move form linking against the deprecated `init_options` functions to the renamed `options_init` functions.

For more context see libgit2/libgit2@0b5ba0d744 and libgit2/libgit2@c6184f0c4b.
2020-10-22 06:18:11 -07:00
Patrick Steinhardt 37b81b61f1
repository: Implement wrappers for `git_object_lookup_prefix` (#658)
While we already have wrappers for `git_object_lookup`, there are none
yet for the prefixed variant where only the first n bytes of the OID are
used for the lookup. This commit adds them.
2020-10-22 05:21:25 -07:00
Suhaib Mujahid 111185838c
feat: Enable change the system install path (#653)
#### Problem:
The current `CMAKE_INSTALL_PREFIX` value for the `system` build mode is `/usr`. However, in`macOS` as an example, you cannot write to `/usr` without `sudo` permission.

#### Proposed solution:
Enable changing the value to `/usr/local` (or any other path). This change makes the script use the value of the environment variable `SYSTEM_INSTALL_PREFIX` to select the installation path.  If the variable is not set, it fallback to the path `/usr`.
2020-09-30 17:27:48 -07:00
Suhaib Mujahid f3a746d7b6
Enable set the VENDORED_PATH for libgit2 (#650)
### What this change is doing?
This change aims to enable us to set the `VENDORED_PATH` as an environment variable and failback to the default value if the environment variable is not set. Thus, this change should not break current behavior.

### Why we need this?
This will enable using the script to build libgit2 form source code downloaded manually.

Example:
```sh
LIBGIT2_VER="1.0.1"
DOWNLOAD_URL="https://codeload.github.com/libgit2/libgit2/tar.gz/v${LIBGIT2_VER}""
LIBGIT2_PATH="${HOME}/libgit2-${LIBGIT2_VER}"
wget -O "${LIBGIT2_PATH}.tar.gz" "$DOWNLOAD_URL"
tar -xzvf "${LIBGIT2_PATH}.tar.gz"


VENDORED_PATH=$LIBGIT2_PATH sh ./script/build-libgit2.sh --static
```
2020-09-30 16:57:50 -07:00
Jesse Hathaway 3c5c580d78
Add a ReInit function (#647)
libgit2 stores the lookup paths for gitconfig files in its global state.
This means that after a program changes its effective uid libgit2 will
still look for gitconfig files in the home directory of the original
uid. Expose a way to call C.git_libgit2_init so a user can reinitialize
the libgit2 global state.
2020-09-29 15:42:20 -07:00
Patrick Steinhardt 7e726fda6e
merge: Expose recursion limit merge option (#642)
The `recursion_limit` merge option provided by libgit2 is currently not
exposed and thus inaccessible to Git2Go users. Let's expose it to let
users control creation of recursive merge bases.
2020-09-18 05:43:56 -07:00
michael boulton 7d4453198b
Add support for creating signed commits and signing commits during a rebase (#626) 2020-08-18 09:25:31 -07:00
michael boulton 7883ec85de
More diff functionality (#629)
This PR adds

- The ability to apply a Diff object to the repo
- Support for git_apply_hunk_cb and git_apply_delta_cb callbacks in options for applying the diffs
- The ability to import a diff from a raw buffer (for example, one exported by ToBuf) into a Diff object associated with the repo
- Tests for the above
2020-08-18 06:14:02 -07:00
lhchavez 2ac9f4e69b
Add two more GitHub Actions workflows (#633)
This change adds:

* `tag.yml`: Creates a new tag every time the master or a release branch
  is pushed.
* `backport.yml`: Creates a cherry-pick in older release branches to
  keep them up to date with little cost.
2020-08-16 07:27:17 -07:00
lhchavez 5314951759
Refresh the GitHub Actions CI (#632)
This change:

* Builds the library with Go 1.14, too.
* Builds the non-legacy tests with Ubuntu Focal (20.04).
* Adds testing for system-wide libraries, both static and dynamic
  versions.
* Fixes a typo in the README.
2020-08-15 17:19:53 -07:00
michael boulton fc6eaf3638
Fix null pointer dereference in status.ByIndex (#628)
`git_status_byindex` can return a null pointer if there is no statuses.
2020-08-14 11:19:21 -07:00
Yuichi Watanabe 462ebd83e0
Add support for git_blob_is_binary (#625)
This adds IsBinary() func to Blob struct, which simply returns the result of git_blob_is_binary function.
Refs: https://libgit2.org/libgit2/#HEAD/group/blob/git_blob_is_binary
Issue: Add support for git_blob_is_binary #426

Fixes: #426
2020-07-30 05:07:05 -07:00
Patrick Steinhardt d8f9990d4d
Fix installation of libgit2 into wrong libdir (#624)
Upstream libgit2 has migrated to use the GNUInstallDirs module to decide
where things will be installed to by default. While it improves
consistency with the host system, we're not really after that when
building the vendored libgit2 library, and in fact libgit2.{a,pc} may
now be installed into a different directory than before as it started to
depend on the host platform.

Fix this by explicitly specifying that we want the library and pkgconfig
file to be installed into a plain "lib/" directory.
2020-07-25 13:08:04 -07:00
Jesse Hathaway b1cad11555
FetchOptions: add ability to specify ProxyOptions (#623)
Prior to this change you could not specifiy proxy options on the
FetchOptions struct, which made it impossible to specify a proxy for an
initial clone. This change adds the ProxyOptions to the FetchOptions
struct so you can go through a proxy when cloning.
2020-07-10 13:00:52 -07:00
lhchavez 20a55cdf92
Revamp the ways in which the library can be built (#621)
This change allows to link the system version of libgit2 statically.
Since `-tags static` is already used for the bundled version of the
library and to avoid breaking old workflows, `-tags
static,system_libgit2` is now used to select that.

This means that the valid combinations are:

| Flag                          | Effect                                        |
|-------------------------------|-----------------------------------------------|
| _No flags_                    | Dynamically-linked against the system libgit2 |
| `-tags static,system_libgit2` | Statically-linked against the system libgit2  |
| `-tags static`                | Statically-linked against the bundled libgit2 |

Note that there is no way to express dynamically linking against the
bundled libgit2 because that makes very little sense, since the binaries
wouldn't be able to be distributed. If that's still desired, the
`PKG_CONFIG_PATH` environment variable can set before building the code.
[`Makefile`](https://github.com/libgit2/git2go/blob/master/Makefile) has
an example of how it is used in the CI.
2020-06-21 15:40:52 -07:00
lhchavez c78ae57de6
Fix a potential use-after-free in DiffNotifyCallback (#579)
This change makes the DiffNotifyCallback always use an "unowned"
*git.Diff that does _not_ run the finalizer. Since the underlying
git_diff object is still owned by libgit2, we shouldn't be calling
Diff.Free() on it, even by accident, since that would cause a whole lot
of undefined behavior.

Now instead of storing a reference to a *git.Diff in the intermediate
state while the diff operation is being done, create a brand new
*git.Diff for every callback invocation, and only create a fully-owned
*git.Diff when the diff operation is done and the ownership is
transfered to Go.
2020-06-21 06:45:39 -07:00
lhchavez 619a9c236b
Add a way to cleanly shut down the library (#578)
This change adds the Shutdown() method, so that the library can be
cleanly shut down. This helps significanly reduce the amount of noise in
the leak detector.
2020-06-21 06:44:06 -07:00
lhchavez 9eaf4fed5f
Update the `README.md` to clarify some aspects of static libgit2 (#620)
This change improves the documentation surrounding libgit2 static builds
and modules.

Fixes: #618
2020-06-20 16:24:46 -07:00
lhchavez 1c9bef0f9f
Remove a couple of now-unnecessary CMake defines (#619)
This change removes the `LIB_INSTALL_DIR` and `INCLUDE_INSTALL_DIR` from
the `script/build-libgit2.sh` script, since they are now unneeded and
just print a warning if they are used.
2020-06-20 12:39:03 -07:00
Takuji Shimokawa 33dac3d460
Provide missing merge flags (#615)
This change adds two missing merge flags MergeTreeSkipREUC and MergeTreeNoRecursive.
2020-06-04 20:34:37 -07:00
Jesse Hathaway 5241c72e6e
Add support for parsing git trailers (#614)
Adds a wrapper for git_message_trailers which returns a slice of trailer
structs.
2020-06-02 10:30:42 -07:00
lhchavez 31f877e249
Merge pull request #582 from suhaibmujahid/method-rename
It is not Go idiomatic to put Get into the getter's name
2020-05-09 20:39:51 -07:00
Suhaib Mujahid cf6522c7fe refactor: Rename methods with Get prefix
It is not Go idiomatic to put Get into the getter's name.

https: //golang.org/doc/effective_go.html#Getters
Co-Authored-By: lhchavez <lhchavez@users.noreply.github.com>
2020-05-09 23:32:17 -04:00
lhchavez 8b51d0db8e
expose options related to caching 2020-05-04 17:44:13 -07:00
Vladimir Buzuev 862cde393c cache_opts: address PR comments 2020-05-04 11:01:38 -07:00
Suhaib Mujahid 91d08450b6 Check nil signature 2020-04-23 16:26:35 -07:00
Vladimir Buzuev 512f37b369 expose options related to caching 2020-04-03 11:51:20 -07:00
lhchavez 13ca96065e Uprev vendor/libgit2 to v1.0
This uprevs libgit2 to the latest and greatest.
2020-04-02 07:53:59 -07:00
lhchavez 0843b826d2
Fix SIGSEGV on double free for Cred object
This change removes the Go finalizer when passing ownership to libgit2.

Fixes: #553
2020-03-26 17:50:12 -07:00
Vladimir Buzuev 89dc80bbea clear native cred pointer after transfer ownership to libgit 2020-03-26 17:30:05 -07:00
Vladimir Buzuev 033ad6e5ef fix SIGSERV on double free for Cred object 2020-03-26 14:43:43 -07:00
Suhaib Mujahid 3a2102638d Update README.md 2020-03-23 18:22:12 -07:00
lhchavez 2b66c0f9e7
Update README.md
Clarifying the versions since we're using Go 1.11 module version rules now.
2020-03-19 08:50:28 -07:00
lhchavez a32375a860 Add the version number to go.mod
This is the second take on trying to tag the current release with a Go
version.
2020-03-19 08:28:34 -07:00
lhchavez e10c2eeef2 Uprev vendor/libgit2 to v0.99
This uprevs libgit2 to the latest and greatest.
2020-03-04 09:54:42 -08:00
lhchavez 30de4b2e26
Merge pull request #542 from slyphon/fix-error-name
Resolves issue #541 - typo in error code 'ErrAmbigious'
2020-02-27 21:16:46 -08:00
Jonathan Simms 0505eef78d Resolves issue #541 - typo in error code 'ErrAmbigious'
I've added an additional constant with the correct spelling. I did this
rather than removing the existing constant to avoid breaking existing
code with the misspelled name.
2020-02-27 23:50:57 -05:00
lhchavez 93c4c5b30a Fix the DiffFlag type
This change makes the underlying type of DiffFlag be uint32 instead of
int. That makes it possible to build on 32-bit systems.

Fixes: #487
2020-02-23 15:32:29 -08:00
lhchavez 91946a5705 Add odb.NewOdbBackendLoose()
This change adds support for odb.NewOdbBackendLoose(). This, together
with the git.Packbuilder, can do what Mempack does with a lot less
memory.
2020-02-23 13:20:13 -08:00
lhchavez 05bc5e36ff Add support for indexers and alternate odb packfiles
This allows for implementations of git servers written in Go.
2020-02-23 09:13:47 -08:00
lhchavez 37f732a833 Fix the Cred interface
This change adds Cred.Free() and finalizers to prevent memory leaks. It
also makes the interface for Cred more idiomatic and return actual
errors intead of ints.
2020-02-23 08:24:06 -08:00
lhchavez 45097a857c
Merge pull request #429 from josharian/cherrypick-commit
cherrypick: wrap git_cherrypick_commit
2020-02-23 08:08:30 -08:00
lhchavez c1903b47fe Add test 2020-02-23 15:43:20 +00:00
lhchavez 3c88bd9f1a Merge remote-tracking branch 'upstream/master' into cherrypick-commit 2020-02-23 15:08:45 +00:00
lhchavez 21d618136f
Merge pull request #423 from josharian/more-annotated-commit
merge: add two missing AnnotatedCommit methods
2020-02-23 07:05:25 -08:00
lhchavez a140f2310f Add test and runtime.KeepAlive() 2020-02-23 14:58:21 +00:00
lhchavez 627447092f Merge remote-tracking branch 'upstream/master' into more-annotated-commit 2020-02-23 14:49:04 +00:00
lhchavez 06764f48dc
Merge pull request #400 from ramanenka/git_index_add_frombuffer
Add binding for `git_index_add_frombuffer`
2020-02-23 06:47:18 -08:00
lhchavez 00374b39aa Update the method name 2020-02-23 14:38:10 +00:00
lhchavez c20008416a Merge remote-tracking branch 'upstream/master' into git_index_add_frombuffer 2020-02-23 13:53:17 +00:00
Richard Burke 4bca045e5a Remove Version from RevertOptions
Version is defaulted to GIT_REVERT_OPTIONS_VERSION
2020-02-22 19:39:50 -08:00
Richard Burke 30c3d0ffe2 Add revert functionality
Closes #436
2020-02-22 19:39:50 -08:00
lhchavez 2bb5930733 Free() the copies of repository.LookupXxx()
`repository.LookupXxx()` allocate new go `Object`s that have a reference
to a `C.git_object`. Those are then duplicated with `git_object_dup()`,
so the original `Object`s linger unnecessarily until the Go GC kicks in.

This change explicitly calls `Free()` on the originals to avoid
unnecessary accumulation of garbage.
2020-02-22 19:21:44 -08:00
lhchavez 26edffd5f5 Update CI configuration
This change:

* Updates the GitHub actions so that they run different commands for the
  dynamic and static flavors of libgit2.
* Updates the .travis.yml file so that it does roughly the same as the
  GitHub actions.
* Adds the release-* branches to the CI configurations.
2020-02-22 18:21:38 -08:00
lhchavez 419bac9075 Uprev libgit2
This uprevs libgit2 to the latest and greatest. Notably,

* Fixes the interface of `git_mempack_reset`, since it now returns an
  `int` instead of being `void`.

Fixes: #533
2020-02-20 19:01:59 -08:00
lhchavez f21ecd9e74
Merge pull request #520 from libgit2/actions
Setup CI via Actions
2020-02-12 18:58:58 -08:00
lhchavez 22d400832b Fix build for go 1.9
This change makes the #includes between credentials.go and remote.go
consistent to avoid a build error in go 1.9.
2020-02-13 02:52:50 +00:00
lhchavez 150a8c6016 Merge remote-tracking branch 'origin/master' into actions 2020-02-13 02:50:31 +00:00
lhchavez b201c503ab
Update .github/workflows/ci.yml 2020-02-12 17:07:17 -08:00
lhchavez 75c5e41422
Merge pull request #527 from dbolkensteyn/master
Fixes #513 - Segfault during tree walk
2020-02-12 17:06:40 -08:00
lhchavez 11506ab070
Merge pull request #523 from josharian/diff-stringers
make Delta and DiffLineType stringers
2020-02-12 17:03:52 -08:00
lhchavez 917d8dcb9e
Merge pull request #524 from josharian/doc-params
provide param names in DiffForEachFileCallback
2020-02-12 17:00:02 -08:00
lhchavez aa802a90db
Merge pull request #503 from jonEbird/static-build-script-cleanup
script/build-libgit2-static.sh: correctly set ROOT
2020-02-12 16:57:53 -08:00
Carlos Martín Nieto e2d039017f
Merge pull request #515 from Nivl/patch-1
fix invalid guard forcing v27 instead of v28
2020-02-12 13:32:51 +01:00
Carlos Martín Nieto ba1b5f0c23
Merge pull request #528 from libgit2/cmn/bump-libgit2
Bump libgit2 to 6777db8e83
2019-12-10 23:41:38 +01:00
Carlos Martín Nieto c5159e624e credentials: unconfuse Go about the type
For some reason cgo thinks the `credtype` field does not exist in `git_cred` so
let's put it into the C code.
2019-12-10 22:33:00 +00:00
Carlos Martín Nieto ce19fa064f travis: update versions of Go to be tested 2019-12-10 22:20:20 +00:00
Carlos Martín Nieto 97e6392d3a Adjust to libgit2 changes 2019-12-10 22:15:32 +00:00
Carlos Martín Nieto 790b3d2ac0 Makefile: disable test caching
This does not work well when you're changing C stuff underneath.
2019-12-10 22:15:09 +00:00
Carlos Martín Nieto 93a1ee401f Bump libgit2 to 6777db8e83 2019-12-10 21:28:24 +00:00
Dinesh Bolkensteyn 5c79683685 Similar to #513 Fix potential segfault on Tag objects 2019-11-17 17:41:43 +01:00
Dinesh Bolkensteyn bf289c8b36 Fixes #513 - Segfault during tree walk 2019-11-17 17:21:57 +01:00
Josh Bleecher Snyder 79b2cb6ca1 provide param names in DiffForEachFileCallback
Without a parameter name, the float64 param is pretty inscrutable.
2019-08-27 10:23:37 -07:00
Josh Bleecher Snyder 3849c7f52a make Delta and DiffLineType stringers 2019-08-27 10:02:12 -07:00
Carlos Martín Nieto a5b3de11ae submodule 2019-08-15 09:42:58 +02:00
Carlos Martín Nieto 26c983c3a2
Setup CI via Actions 2019-08-15 09:38:02 +02:00
Carlos Martín Nieto 37e5b53f74
Merge pull request #519 from libgit2/cmn/update-libgit2
Update vendored libgit2 to 08cfa43d0e1a921
2019-08-13 20:28:10 +02:00
Carlos Martín Nieto 57ab6bc351 script: tell libgit2 to use the builtin regex backend 2019-08-13 20:15:01 +02:00
Carlos Martín Nieto b7eeb4f405 Update vendored libgit2 to 08cfa43d0e1a921 2019-08-13 19:30:16 +02:00
Melvin 5cdcbf1607
fix invalid guard forcing v27 instead of v28 2019-06-19 10:48:37 -07:00
Carlos Martín Nieto b2e2b2f71b
Merge pull request #506 from takuji/git_commit_message_encoding
Add git_commit_message_encoding support
2019-06-18 11:39:25 +02:00
Carlos Martín Nieto 4fa9349942
Merge pull request #512 from codeocean/diff-to-buf
Add Diff.ToBuf wrapping git_diff_to_buf
2019-06-18 11:33:28 +02:00
Segev Finer ad02c37e6d Add Diff.ToBuf wrapping git_diff_to_buf 2019-05-29 18:00:16 +03:00
Takuji Shimokawa fe0f562cc0 Add Commit.MessageEncoding() method. 2019-05-10 20:31:01 +09:00
Jon Miller 13090d85b4 script/build-libgit2-static.sh: correctly set ROOT
The ROOT variable is not being set correctly. Need to use dirname against
$0 as well as only go up one directory instead of two.
2019-05-07 11:36:34 -04:00
Carlos Martín Nieto bf1e8a4338
Merge pull request #479 from lhchavez/uprev-libgit2
Uprev vendored libgit2 to v.0.28
2019-02-15 14:26:37 +01:00
lhchavez 5fda6dd901 Uprev vendored libgit2 to v0.28
New version is here!
2019-02-11 03:57:50 +00:00
Carlos Martín Nieto 2f91268f74
Merge pull request #448 from lhchavez/mempack
Add support for mempack
2019-01-15 20:46:54 +01:00
Carlos Martín Nieto f4ea2a561d Keep odb alive when adding mempack 2019-01-15 20:28:36 +01:00
Carlos Martín Nieto 8766f9f36c
Merge pull request #466 from lhchavez/repository-create_commit_from_ids
Add support for CreateCommitFromIds
2019-01-08 09:30:36 +00:00
lhchavez 6d67bde74a Merge remote-tracking branch 'upstream/master' into repository-create_commit_from_ids 2019-01-08 02:51:21 +00:00
lhchavez 35518c78df Keeping the tree alive 2019-01-08 02:50:42 +00:00
lhchavez f505e39c9e Add a test and some comments as to the ugly pointer arithmetic 2019-01-08 02:50:25 +00:00
lhchavez a2c93a0bcf Addressed review feedback and added a test 2019-01-08 02:45:04 +00:00
lhchavez b609c04b68 Merge remote-tracking branch 'upstream/master' into mempack 2019-01-08 02:01:44 +00:00
Carlos Martín Nieto 2609f4c6f2
Merge pull request #477 from lhchavez/patch-1
Add support for Go 1.11 modules
2019-01-07 16:00:59 +00:00
Carlos Martín Nieto b30b050c9c
Merge pull request #475 from lhchavez/self-contained-build
Improve the static build script
2019-01-07 14:02:51 +00:00
Carlos Martín Nieto e93f34cf18
Merge pull request #476 from lhchavez/clean-up-leaked-dir
Clean up one leaked temporary directory
2019-01-07 12:33:43 +00:00
lhchavez 30c57ff09e
Add support for Go 1.11 modules
This change adds a `go.mod` file. An empty file is sufficient since this
project has no external dependencies. For people that want to use the static
version of libgit2, this module can be vendored and the following can be added
to their `go.mod` file:

    replace github.com/libgit2/git2go => ./vendor/github.com/libgit2/git2go
2019-01-06 07:34:50 -08:00
lhchavez e9856f2c38 Clean up one leaked temporary directory
A `defer cleanupTestRepo()` was missing.
2019-01-05 22:41:35 +00:00
lhchavez f3c487966d Improve the static build script
This change:

* Uses the installed version of both the library and the pkgconfig file,
  which fixes path resolution on Ubuntu Xenial.
* Uses quoting liberally so that paths with spaces in them are correctly
  handled.
* Moves the build+install directories to static-build/ in the git2go
  repository to avoid having a dirty vendor/libgit2 checkout.
2019-01-05 20:28:35 +00:00
Carlos Martín Nieto 7ae106611c
Merge pull request #420 from josharian/rebase-operation-type-stringer
Add RebaseOperationReword, and make RebaseOperationType a stringer
2019-01-05 11:05:44 +00:00
Carlos Martín Nieto fb438dbf9a
Merge pull request #474 from libgit2/cmn/deprecated-names
Use git_object_t instead of deprecated git_otype
2019-01-05 11:04:03 +00:00
Carlos Martín Nieto ee6dff2f8e Use git_object_t instead of deprecated git_otype 2019-01-05 10:48:42 +00:00
Carlos Martín Nieto b06a2a6900
Merge pull request #471 from libgit2/cmn/bump-libgit2
Bump vendored libgit2 to fba70a9d5f
2019-01-04 13:35:12 +00:00
Carlos Martín Nieto f969cc900d Bump vendored libgit2 to fba70a9d5f
This includes updating the `Index.WriteTreeTo` test as it was abusing an
oversight of the object creation safety checks and creating a tree referencing
a non-existent blob. Instead we update it to the primary purpose of this method
which is to write into a repository from an unattached index.
2019-01-04 13:18:54 +00:00
Carlos Martín Nieto bcf325244c
Merge pull request #445 from rmg/exclusive-pkg-config
static: use pkg-config exclusively when using it
2019-01-04 12:18:20 +00:00
Carlos Martín Nieto b51a90c133
Merge pull request #465 from lhchavez/packbuilder-insert_from_walk
Add support for Packbuilder.InsertFromWalk()
2019-01-04 12:11:05 +00:00
Carlos Martín Nieto c27981c283
Merge pull request #463 from Nivl/patch-1
Add index.Clear() to clear the index object
2019-01-04 00:43:48 +00:00
Carlos Martín Nieto c740e1d83d
Merge pull request #432 from josharian/simplify-oid
git: simplify some Oid methods
2019-01-03 23:53:13 +00:00
Carlos Martín Nieto fc1230ba16
Merge pull request #447 from walkenzoy/master
git2go: small fixes to odb module
2019-01-03 16:22:15 +01:00
Carlos Martín Nieto e319b9427f
Merge pull request #425 from josharian/more-merge-file-flags
merge: add missing MergeFileFlag constants
2019-01-02 23:58:19 +01:00
Carlos Martín Nieto 7197faee79
Merge pull request #424 from josharian/sigdoc
signature: improve Signature.Offset docs
2019-01-02 23:13:50 +01:00
Carlos Martín Nieto 7e9128bd58
Merge pull request #470 from lhchavez/fix-odbreadstream-read
Return io.EOF on OdbReadStream.Read()
2019-01-02 23:03:07 +01:00
Carlos Martín Nieto 69175cb426
Merge pull request #469 from praveentiru/pty/issue#256
Remove unused parameter in OpenOndisk #256
2019-01-02 22:33:10 +01:00
lhchavez ab3470030b Add some tests
This should prevent regressions in the future.
2018-12-28 04:42:09 +00:00
lhchavez 344dc33fae Return io.EOF on OdbReadStream.Read()
This change makes OdbReadStream.Read() comply with the usual io.Reader
semantics.
2018-12-28 04:35:20 +00:00
praveen e0ad45065e Fixed issues with tests 2018-12-25 08:44:01 +05:30
praveen 92ebf4515c Remove unused parameter in OpenOndisk 2018-12-25 08:27:32 +05:30
Carlos Martín Nieto 8b368063e9
Merge pull request #468 from wmedlar/patch-1
Fix typo in constant name
2018-12-17 10:52:34 +00:00
Will Medlar a2ac1b9ed1
Fix typo in constant name 2018-12-17 01:56:37 -06:00
lhchavez 8d27336e8a Add support for Packbuilder.InsertFromWalk()
This change adds support for Packbuilder.InsertFromWalk() from libgit2.
2018-11-15 03:20:51 +00:00
lhchavez 8ff5e43711 Add support for CreateCommitFromIds
This change adds support for CreateCommitFromIds from libgit2.
2018-11-15 03:19:28 +00:00
Melvin dd973b99ad
Add index.Clear() to clear the index object 2018-10-25 15:52:43 -07:00
Carlos Martín Nieto eec1547c20
Merge pull request #460 from libgit2/cmn/bump-master
Update vendored libgit2 to 838a2f291
2018-10-07 19:03:38 +02:00
Carlos Martín Nieto 92501bd663 Update vendored libgit2 to 838a2f291 2018-10-07 18:50:25 +02:00
Ryan Graham b3256d9058
static: use pkg-config exclusively when using it
When using the static linking option on platforms that use pkg-config,
use ONLY pkg-config to get the CFLAGS and LDFLAGS. This prevents pulling
in dependencies and flags for any non-vendored version that may be
present on the host.

The main practical effect of this is that if someone doesn't need/want
any sort of remote access support at all they can completely disable
libcurl, libssh2, libssl, etc and produce a smaller/simpler binary and
greatly simplify their build-time dependencies. When done properly, the
generated pkg-config file will tell cgo everything it needs to know.

This also prevents confusion if there is a system copy of libgit2 that
is being given priority over the vendored build.

Signed-off-by: Ryan Graham <r.m.graham@gmail.com>
2018-08-23 10:45:27 -07:00
Carlos Martín Nieto 5280ceb751
Merge pull request #452 from libgit2/cmn/bump-libgit2-master
Bump vendored libgit2 to 275d84c583
2018-08-08 12:33:28 +02:00
Carlos Martín Nieto 538a05d55c Remove uses of deprecated git_buf_free 2018-08-08 11:51:51 +02:00
Carlos Martín Nieto 973938cbb5 Bump vendored libgit2 to 9275d84c583 2018-08-08 11:45:48 +02:00
lhchavez d7fd15b1e0 Add support for mempack
This allows the creation of git packfiles without needing to write them
to the filesystem.
2018-07-07 23:17:24 +00:00
Michel Lespinasse bdca40d275 git2go: small fixes to odb module
- Fix couple cgo issues in odb.Write() and odb.Hash(). This is the
  same issue - and same solution - as repo.CreateBlobFromBuffer()
  used to have.

- Add test for odb.Read()
2018-07-03 16:43:07 -07:00
Carlos Martín Nieto 14280de4da
Merge pull request #443 from walkenzoy/master
git2go: fix reference iterator leak
2018-05-31 07:26:17 +02:00
Michel Lespinasse a2de5ababa git2go: fix reference iterator leak
This fixes a leak that was apparently introduced in commit 55a1096141.
2018-05-30 14:15:59 -07:00
Carlos Martín Nieto 2c42b80551
Merge pull request #442 from libgit2/cmn/bump-libgit2
vendor: bump libgit2 to 7f6c1ce9760c6
2018-05-29 21:30:38 +02:00
Carlos Martín Nieto 925932a54f vendor: bump libgit2 to 7f6c1ce9760c6 2018-05-29 21:09:45 +02:00
Josh Bleecher Snyder b7ca4a96f5 git: simplify some Oid methods 2018-02-15 10:01:40 -08:00
Josh Bleecher Snyder c75e0221d7 cherrypick: wrap git_cherrypick_commit 2018-02-06 21:55:01 -08:00
Josh Bleecher Snyder 9de57cc90e merge: add missing MergeFileFlag constants
While we're here, pull in comments as well.
While one can pop back and forth between godoc and libgit2 refs,
it's much nicer to have it in one place.

Note that MergeFileStyleSimplifyAlnum probably should have been called
merely MergeFileSimplifyAlnum (no "Style"). It's probably not worth
breaking backwards compatibility to fix it, but we avoid the mistake
going forwards.
2018-01-25 16:55:42 -08:00
Josh Bleecher Snyder 03339f731a merge: add two missing AnnotatedCommit methods 2018-01-25 16:00:39 -08:00
Josh Bleecher Snyder cf2379295a signature: improve Signature.Offset docs
Use standard godoc style; be more precise.
2018-01-25 15:59:00 -08:00
Josh Bleecher Snyder 9b850d084e rebase: make RebaseOperationType a fmt.Stringer
Helps with debugging.
2018-01-21 13:48:51 -08:00
Josh Bleecher Snyder 21fd4ad5f6 rebase: add RebaseOperationReword 2018-01-21 13:48:51 -08:00
Vadzim Ramanenka 79fe156d30 Add binding for `git_index_add_frombuffer` 2017-07-17 17:05:03 +03:00
68 changed files with 2279 additions and 743 deletions

View File

@ -1,54 +0,0 @@
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)"

View File

@ -1,127 +0,0 @@
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)

View File

@ -1,28 +0,0 @@
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: .*

2
.gitignore vendored
View File

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

3
.gitmodules vendored
View File

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

View File

@ -1,3 +1,4 @@
//go:build static && !system_libgit2
// +build static,!system_libgit2
package git
@ -9,8 +10,8 @@ package git
#cgo CFLAGS: -DLIBGIT2_STATIC
#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 != 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"
#endif
*/
import "C"

View File

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

View File

@ -1,3 +1,4 @@
//go:build static && system_libgit2
// +build static,system_libgit2
package git
@ -7,8 +8,8 @@ package git
#cgo CFLAGS: -DLIBGIT2_STATIC
#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 != 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"
#endif
*/
import "C"

View File

@ -1,6 +1,6 @@
The MIT License
Copyright (c) 2013 The git2go contributors
Copyright (c) 2013 The libgit2 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

View File

@ -1,7 +1,11 @@
TEST_ARGS ?= --count=1
default: test
PKG_CONFIG_PATH=/opt/libgit2/
default: goimports test
goimports:
goimports -w *.go
generate: static-build/install/lib/libgit2.a
go generate --tags "static" ./...
@ -10,12 +14,19 @@ 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
go test $(TEST_ARGS) ./...
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
install:
go install ./...
clean:
rm go.*
# Bundled dynamic library
# =======================
# In order to avoid having to manipulate `git_dynamic.go`, which would prevent

View File

@ -1,86 +1,48 @@
git2go
GO libgit2
======
[![GoDoc](https://godoc.org/github.com/libgit2/git2go?status.svg)](http://godoc.org/github.com/libgit2/git2go/v27) [![Build Status](https://travis-ci.org/libgit2/git2go.svg?branch=main)](https://travis-ci.org/libgit2/git2go)
[![GoDoc](https://godoc.org/go.wit.com/lib/libgit2?status.svg)](http://godoc.org/go.wit.com/lib/libgit2) [![Build Status](https://travis-ci.org/libgit2/libgit2.svg?branch=main)](https://travis-ci.org/libgit2/libgit2)
Go bindings for [libgit2](http://libgit2.github.com/).
### Updated 2024/12/16
### Which Go version to use
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 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.27 installed, you'd import git2go v27 with
* 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
```sh
go get github.com/libgit2/git2go/v27
```
```go
import "github.com/libgit2/git2go/v27"
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
```
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
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.
TODO: not sure yet
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 git2go.
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.
### Versioned branch, dynamic linking
When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via Go modules, e.g. to work against libgit2 v0.27
When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via Go modules, e.g. to work against libgit2 v1.2
```go
import "github.com/libgit2/git2go/v27"
goimports -w *.go
```
### 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/v27 => ../../libgit2/git2go
Parallelism and network operations
----------------------------------
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.
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.
Running the tests
-----------------
@ -102,6 +64,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 (@carlosmn)
- Vicent Martí (@vmg)
- Carlos Martín (github@carlosmn)
- Vicent Martí (github@vmg)

View File

@ -23,7 +23,7 @@ func DefaultBlameOptions() (BlameOptions, error) {
defer runtime.UnlockOSThread()
opts := C.git_blame_options{}
ecode := C.git_blame_init_options(&opts, C.GIT_BLAME_OPTIONS_VERSION)
ecode := C.git_blame_options_init(&opts, C.GIT_BLAME_OPTIONS_VERSION)
if ecode < 0 {
return BlameOptions{}, MakeGitError(ecode)
}
@ -47,6 +47,8 @@ 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
BlameIgnoreWhitespace BlameOptionsFlag = C.GIT_BLAME_IGNORE_WHITESPACE
)
func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error) {

View File

@ -68,7 +68,7 @@ func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
size = C.size_t(0)
}
ecode := C.git_blob_create_frombuffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size)
ecode := C.git_blob_create_from_buffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size)
runtime.KeepAlive(repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
@ -88,7 +88,7 @@ func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, err
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_blob_create_fromstream(&stream, repo.ptr, chintPath)
ecode := C.git_blob_create_from_stream(&stream, repo.ptr, chintPath)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
@ -141,7 +141,7 @@ func (stream *BlobWriteStream) Commit() (*Oid, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_blob_create_fromstream_commit(&oid, stream.ptr)
ecode := C.git_blob_create_from_stream_commit(&oid, stream.ptr)
runtime.KeepAlive(stream)
if ecode < 0 {
return nil, MakeGitError(ecode)

View File

@ -210,7 +210,7 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_free(&nameBuf)
defer C.git_buf_dispose(&nameBuf)
return C.GoString(nameBuf.ptr), nil
}
@ -258,7 +258,7 @@ func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_free(&nameBuf)
defer C.git_buf_dispose(&nameBuf)
return C.GoString(nameBuf.ptr), nil
}

View File

@ -7,7 +7,6 @@ extern void _go_git_populate_checkout_callbacks(git_checkout_options *opts);
*/
import "C"
import (
"errors"
"os"
"runtime"
"unsafe"
@ -36,7 +35,6 @@ const (
CheckoutDontUpdateIndex CheckoutStrategy = C.GIT_CHECKOUT_DONT_UPDATE_INDEX // Normally checkout updates index entries as it goes; this stops that
CheckoutNoRefresh CheckoutStrategy = C.GIT_CHECKOUT_NO_REFRESH // Don't refresh index/config/etc before doing checkout
CheckoutSkipUnmerged CheckoutStrategy = C.GIT_CHECKOUT_SKIP_UNMERGED // Allow checkout to skip unmerged files
CheckoutUserOurs CheckoutStrategy = C.GIT_CHECKOUT_USE_OURS // For unmerged files, checkout stage 2 from index
CheckoutUseOurs CheckoutStrategy = C.GIT_CHECKOUT_USE_OURS // For unmerged files, checkout stage 2 from index
CheckoutUseTheirs CheckoutStrategy = C.GIT_CHECKOUT_USE_THEIRS // For unmerged files, checkout stage 3 from index
CheckoutDisablePathspecMatch CheckoutStrategy = C.GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH // Treat pathspec as simple list of exact match file paths
@ -50,8 +48,8 @@ const (
CheckoutUpdateSubmodulesIfChanged CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED // Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED)
)
type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) ErrorCode
type CheckoutProgressCallback func(path string, completed, total uint) ErrorCode
type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) error
type CheckoutProgressCallback func(path string, completed, total uint)
type CheckoutOptions struct {
Strategy CheckoutStrategy // Default will be a dry run
@ -117,9 +115,9 @@ func checkoutNotifyCallback(
if data.options.NotifyCallback == nil {
return C.int(ErrorCodeOK)
}
ret := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir)
if ret < 0 {
*data.errorTarget = errors.New(ErrorCode(ret).String())
err := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir)
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
@ -142,7 +140,7 @@ func checkoutProgressCallback(
// 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)
C.git_checkout_options_init(copts, C.GIT_CHECKOUT_OPTIONS_VERSION)
if opts == nil {
return nil
}

View File

@ -9,30 +9,28 @@ import (
)
type CherrypickOptions struct {
Version uint
Mainline uint
MergeOpts MergeOptions
CheckoutOpts CheckoutOptions
Mainline uint
MergeOptions MergeOptions
CheckoutOptions CheckoutOptions
}
func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions {
opts := CherrypickOptions{
Version: uint(c.version),
Mainline: uint(c.mainline),
MergeOpts: mergeOptionsFromC(&c.merge_opts),
CheckoutOpts: checkoutOptionsFromC(&c.checkout_opts),
Mainline: uint(c.mainline),
MergeOptions: mergeOptionsFromC(&c.merge_opts),
CheckoutOptions: checkoutOptionsFromC(&c.checkout_opts),
}
return opts
}
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)
C.git_cherrypick_options_init(copts, C.GIT_CHERRYPICK_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)
populateMergeOptions(&copts.merge_opts, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
return copts
}
@ -50,7 +48,7 @@ func DefaultCherrypickOptions() (CherrypickOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_cherrypick_init_options(&c, C.GIT_CHERRYPICK_OPTIONS_VERSION)
ecode := C.git_cherrypick_options_init(&c, C.GIT_CHERRYPICK_OPTIONS_VERSION)
if ecode < 0 {
return CherrypickOptions{}, MakeGitError(ecode)
}
@ -82,7 +80,7 @@ func (r *Repository) CherrypickCommit(pick, our *Commit, opts CherrypickOptions)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cOpts := populateMergeOptions(&C.git_merge_options{}, &opts.MergeOpts)
cOpts := populateMergeOptions(&C.git_merge_options{}, &opts.MergeOptions)
defer freeMergeOptions(cOpts)
var ptr *C.git_index

View File

@ -7,16 +7,15 @@ extern void _go_git_populate_clone_callbacks(git_clone_options *opts);
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
)
type RemoteCreateCallback func(repo *Repository, name, url string) (*Remote, ErrorCode)
type RemoteCreateCallback func(repo *Repository, name, url string) (*Remote, error)
type CloneOptions struct {
*CheckoutOpts
*FetchOptions
CheckoutOptions CheckoutOptions
FetchOptions FetchOptions
Bare bool
CheckoutBranch string
RemoteCreateCallback RemoteCreateCallback
@ -71,9 +70,10 @@ func remoteCreateCallback(
panic("invalid remote create callback")
}
remote, ret := data.options.RemoteCreateCallback(repo, name, url)
if ret < 0 {
*data.errorTarget = errors.New(ErrorCode(ret).String())
remote, err := data.options.RemoteCreateCallback(repo, name, url)
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
if remote == nil {
@ -96,12 +96,12 @@ type cloneCallbackData struct {
}
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)
C.git_clone_options_init(copts, C.GIT_CLONE_OPTIONS_VERSION)
if opts == nil {
return nil
}
populateCheckoutOptions(&copts.checkout_opts, opts.CheckoutOpts, errorTarget)
populateFetchOptions(&copts.fetch_opts, opts.FetchOptions, errorTarget)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
populateFetchOptions(&copts.fetch_opts, &opts.FetchOptions, errorTarget)
copts.bare = cbool(opts.Bare)
if opts.RemoteCreateCallback != nil {

View File

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

View File

@ -37,10 +37,14 @@ func (c *Commit) Message() string {
return ret
}
func (c *Commit) MessageEncoding() string {
ret := C.GoString(C.git_commit_message_encoding(c.cast_ptr))
func (c *Commit) MessageEncoding() MessageEncoding {
ptr := C.git_commit_message_encoding(c.cast_ptr)
if ptr == nil {
return MessageEncodingUTF8
}
ret := C.GoString(ptr)
runtime.KeepAlive(c)
return ret
return MessageEncoding(ret)
}
func (c *Commit) RawMessage() string {
@ -64,6 +68,19 @@ func (c *Commit) ContentToSign() string {
// CommitSigningCallback defines a function type that takes some data to sign and returns (signature, signature_field, error)
type CommitSigningCallback func(string) (signature, signatureField string, err error)
// CommitCreateCallback defines a function type that is called when another
// function is going to create commits (for example, Rebase) to allow callers
// to override the commit creation behavior. For example, users may wish to
// sign commits by providing this information to Repository.CreateCommitBuffer,
// signing that buffer, then calling Repository.CreateCommitWithSignature.
type CommitCreateCallback func(
author, committer *Signature,
messageEncoding MessageEncoding,
message string,
tree *Tree,
parents ...*Commit,
) (oid *Oid, err error)
// WithSignatureUsing creates a new signed commit from this one using the given signing callback
func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
signature, signatureField, err := f(c.ContentToSign())
@ -86,10 +103,10 @@ func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, e
func (c *Commit) ExtractSignature() (string, string, error) {
var c_signed C.git_buf
defer C.git_buf_free(&c_signed)
defer C.git_buf_dispose(&c_signed)
var c_signature C.git_buf
defer C.git_buf_free(&c_signature)
defer C.git_buf_dispose(&c_signature)
oid := c.Id()
repo := C.git_commit_owner(c.cast_ptr)

View File

@ -135,7 +135,7 @@ func (c *Config) LookupString(name string) (string, error) {
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_free(&valBuf)
defer C.git_buf_dispose(&valBuf)
return C.GoString(valBuf.ptr), nil
}
@ -345,7 +345,7 @@ func (c *Config) OpenLevel(parent *Config, level ConfigLevel) (*Config, error) {
}
// OpenOndisk creates a new config instance containing a single on-disk file
func OpenOndisk(parent *Config, path string) (*Config, error) {
func OpenOndisk(path string) (*Config, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
@ -392,7 +392,7 @@ func (iter *ConfigIterator) Free() {
func ConfigFindGlobal() (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -407,7 +407,7 @@ func ConfigFindGlobal() (string, error) {
func ConfigFindSystem() (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -422,7 +422,7 @@ func ConfigFindSystem() (string, error) {
func ConfigFindXDG() (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -440,7 +440,7 @@ func ConfigFindXDG() (string, error) {
// Look for the file in %PROGRAMDATA%\Git\config used by portable git.
func ConfigFindProgramdata() (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

View File

@ -13,7 +13,7 @@ func setupConfig() (*Config, error) {
err error
)
c, err = OpenOndisk(nil, tempConfig)
c, err = OpenOndisk(tempConfig)
if err != nil {
return nil, err
}

View File

@ -2,51 +2,114 @@ package git
/*
#include <git2.h>
#include <git2/credential.h>
#include <git2/sys/credential.h>
void _go_git_populate_credential_ssh_custom(git_cred_ssh_custom *cred);
git_credential_t _go_git_credential_credtype(git_credential *cred);
void _go_git_populate_credential_ssh_custom(git_credential_ssh_custom *cred);
*/
import "C"
import (
"crypto/rand"
"errors"
"fmt"
"runtime"
"strings"
"unsafe"
"golang.org/x/crypto/ssh"
)
type CredType uint
// CredentialType is a bitmask of supported credential types.
//
// This represents the various types of authentication methods supported by the
// library.
type CredentialType uint
const (
CredTypeUserpassPlaintext CredType = C.GIT_CREDTYPE_USERPASS_PLAINTEXT
CredTypeSshKey CredType = C.GIT_CREDTYPE_SSH_KEY
CredTypeSshCustom CredType = C.GIT_CREDTYPE_SSH_CUSTOM
CredTypeDefault CredType = C.GIT_CREDTYPE_DEFAULT
CredentialTypeUserpassPlaintext CredentialType = C.GIT_CREDENTIAL_USERPASS_PLAINTEXT
CredentialTypeSSHKey CredentialType = C.GIT_CREDENTIAL_SSH_KEY
CredentialTypeSSHCustom CredentialType = C.GIT_CREDENTIAL_SSH_CUSTOM
CredentialTypeDefault CredentialType = C.GIT_CREDENTIAL_DEFAULT
CredentialTypeSSHInteractive CredentialType = C.GIT_CREDENTIAL_SSH_INTERACTIVE
CredentialTypeUsername CredentialType = C.GIT_CREDENTIAL_USERNAME
CredentialTypeSSHMemory CredentialType = C.GIT_CREDENTIAL_SSH_MEMORY
)
type Cred struct {
doNotCompare
ptr *C.git_cred
func (t CredentialType) String() string {
if t == 0 {
return "CredentialType(0)"
}
var parts []string
if (t & CredentialTypeUserpassPlaintext) != 0 {
parts = append(parts, "UserpassPlaintext")
t &= ^CredentialTypeUserpassPlaintext
}
if (t & CredentialTypeSSHKey) != 0 {
parts = append(parts, "SSHKey")
t &= ^CredentialTypeSSHKey
}
if (t & CredentialTypeSSHCustom) != 0 {
parts = append(parts, "SSHCustom")
t &= ^CredentialTypeSSHCustom
}
if (t & CredentialTypeDefault) != 0 {
parts = append(parts, "Default")
t &= ^CredentialTypeDefault
}
if (t & CredentialTypeSSHInteractive) != 0 {
parts = append(parts, "SSHInteractive")
t &= ^CredentialTypeSSHInteractive
}
if (t & CredentialTypeUsername) != 0 {
parts = append(parts, "Username")
t &= ^CredentialTypeUsername
}
if (t & CredentialTypeSSHMemory) != 0 {
parts = append(parts, "SSHMemory")
t &= ^CredentialTypeSSHMemory
}
if t != 0 {
parts = append(parts, fmt.Sprintf("CredentialType(%#x)", t))
}
return strings.Join(parts, "|")
}
func (o *Cred) HasUsername() bool {
if C.git_cred_has_username(o.ptr) == 1 {
type Credential struct {
doNotCompare
ptr *C.git_credential
}
func newCredential() *Credential {
cred := &Credential{}
runtime.SetFinalizer(cred, (*Credential).Free)
return cred
}
func (o *Credential) HasUsername() bool {
if C.git_credential_has_username(o.ptr) == 1 {
return true
}
return false
}
func (o *Cred) Type() CredType {
return (CredType)(o.ptr.credtype)
func (o *Credential) Type() CredentialType {
return (CredentialType)(C._go_git_credential_credtype(o.ptr))
}
func credFromC(ptr *C.git_cred) *Cred {
return &Cred{ptr: ptr}
func (o *Credential) Free() {
C.git_credential_free(o.ptr)
runtime.SetFinalizer(o, nil)
o.ptr = nil
}
// GetUserpassPlaintext returns the plaintext username/password combination stored in the Cred.
func (o *Cred) GetUserpassPlaintext() (username, password string, err error) {
if o.Type() != CredTypeUserpassPlaintext {
func (o *Credential) GetUserpassPlaintext() (username, password string, err error) {
if o.Type() != CredentialTypeUserpassPlaintext {
err = errors.New("credential is not userpass plaintext")
return
}
@ -58,8 +121,8 @@ func (o *Cred) GetUserpassPlaintext() (username, password string, err error) {
}
// 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 {
func (o *Credential) GetSSHKey() (username, publickey, privatekey, passphrase string, err error) {
if o.Type() != CredentialTypeSSHKey && o.Type() != CredentialTypeSSHMemory {
err = fmt.Errorf("credential is not an SSH key: %v", o.Type())
return
}
@ -72,27 +135,44 @@ func (o *Cred) GetSSHKey() (username, publickey, privatekey, passphrase string,
return
}
func NewCredUsername(username string) (int, Cred) {
cred := Cred{}
func NewCredentialUsername(username string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
ret := C.git_cred_username_new(&cred.ptr, cusername)
return int(ret), cred
ret := C.git_credential_username_new(&cred.ptr, cusername)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
func NewCredUserpassPlaintext(username string, password string) (int, Cred) {
cred := Cred{}
func NewCredentialUserpassPlaintext(username string, password string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
cpassword := C.CString(password)
defer C.free(unsafe.Pointer(cpassword))
ret := C.git_cred_userpass_plaintext_new(&cred.ptr, cusername, cpassword)
return int(ret), cred
ret := C.git_credential_userpass_plaintext_new(&cred.ptr, cusername, cpassword)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
// NewCredSshKey creates new ssh credentials reading the public and private keys
// NewCredentialSSHKey creates new ssh credentials reading the public and private keys
// from the file system.
func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string, passphrase string) (int, Cred) {
cred := Cred{}
func NewCredentialSSHKey(username string, publicKeyPath string, privateKeyPath string, passphrase string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
cpublickey := C.CString(publicKeyPath)
@ -101,14 +181,21 @@ func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string,
defer C.free(unsafe.Pointer(cprivatekey))
cpassphrase := C.CString(passphrase)
defer C.free(unsafe.Pointer(cpassphrase))
ret := C.git_cred_ssh_key_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
return int(ret), cred
ret := C.git_credential_ssh_key_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
// NewCredSshKeyFromMemory creates new ssh credentials using the publicKey and privateKey
// NewCredentialSSHKeyFromMemory creates new ssh credentials using the publicKey and privateKey
// arguments as the values for the public and private keys.
func NewCredSshKeyFromMemory(username string, publicKey string, privateKey string, passphrase string) (int, Cred) {
cred := Cred{}
func NewCredentialSSHKeyFromMemory(username string, publicKey string, privateKey string, passphrase string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
cpublickey := C.CString(publicKey)
@ -117,22 +204,27 @@ func NewCredSshKeyFromMemory(username string, publicKey string, privateKey strin
defer C.free(unsafe.Pointer(cprivatekey))
cpassphrase := C.CString(passphrase)
defer C.free(unsafe.Pointer(cpassphrase))
ret := C.git_cred_ssh_key_memory_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
return int(ret), cred
ret := C.git_credential_ssh_key_memory_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
func NewCredSshKeyFromAgent(username string) (int, Cred) {
cred := Cred{}
func NewCredentialSSHKeyFromAgent(username string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername)
return int(ret), cred
}
func NewCredDefault() (int, Cred) {
cred := Cred{}
ret := C.git_cred_default_new(&cred.ptr)
return int(ret), cred
ret := C.git_credential_ssh_key_from_agent(&cred.ptr, cusername)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
type credentialSSHCustomData struct {
@ -140,7 +232,7 @@ type credentialSSHCustomData struct {
}
//export credentialSSHCustomFree
func credentialSSHCustomFree(cred *C.git_cred_ssh_custom) {
func credentialSSHCustomFree(cred *C.git_credential_ssh_custom) {
if cred == nil {
return
}
@ -171,11 +263,11 @@ func credentialSSHSignCallback(
}
// NewCredentialSSHKeyFromSigner creates new SSH credentials using the provided signer.
func NewCredentialSSHKeyFromSigner(username string, signer ssh.Signer) (*Cred, error) {
func NewCredentialSSHKeyFromSigner(username string, signer ssh.Signer) (*Credential, 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 := (*C.git_credential_ssh_custom)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_credential_ssh_custom{}))))
ccred.parent.credtype = C.GIT_CREDENTIAL_SSH_CUSTOM
ccred.username = C.CString(username)
ccred.publickey = (*C.char)(C.CBytes(publicKey))
ccred.publickey_len = C.size_t(len(publicKey))
@ -186,9 +278,21 @@ func NewCredentialSSHKeyFromSigner(username string, signer ssh.Signer) (*Cred, e
}
ccred.payload = pointerHandles.Track(&data)
cred := Cred{
ptr: &ccred.parent,
}
cred := newCredential()
cred.ptr = &ccred.parent
return &cred, nil
return cred, nil
}
func NewCredentialDefault() (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
ret := C.git_credential_default_new(&cred.ptr)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}

View File

@ -27,6 +27,51 @@ type BlobCallbackData struct {
// Deprecated: CheckoutOpts is a deprecated alias of CheckoutOptions.
type CheckoutOpts = CheckoutOptions
// credentials.go
// Deprecated: CredType is a deprecated alias of CredentialType
type CredType = CredentialType
const (
CredTypeUserpassPlaintext = CredentialTypeUserpassPlaintext
CredTypeSshKey = CredentialTypeSSHKey
CredTypeSshCustom = CredentialTypeSSHCustom
CredTypeDefault = CredentialTypeDefault
)
// Deprecated: Cred is a deprecated alias of Credential
type Cred = Credential
// Deprecated: NewCredUsername is a deprecated alias of NewCredentialUsername.
func NewCredUsername(username string) (*Cred, error) {
return NewCredentialUsername(username)
}
// Deprecated: NewCredUserpassPlaintext is a deprecated alias of NewCredentialUserpassPlaintext.
func NewCredUserpassPlaintext(username string, password string) (*Cred, error) {
return NewCredentialUserpassPlaintext(username, password)
}
// Deprecated: NewCredSshKey is a deprecated alias of NewCredentialSshKey.
func NewCredSshKey(username string, publicKeyPath string, privateKeyPath string, passphrase string) (*Cred, error) {
return NewCredentialSSHKey(username, publicKeyPath, privateKeyPath, passphrase)
}
// Deprecated: NewCredSshKeyFromMemory is a deprecated alias of NewCredentialSSHKeyFromMemory.
func NewCredSshKeyFromMemory(username string, publicKey string, privateKey string, passphrase string) (*Cred, error) {
return NewCredentialSSHKeyFromMemory(username, publicKey, privateKey, passphrase)
}
// Deprecated: NewCredSshKeyFromAgent is a deprecated alias of NewCredentialSSHFromAgent.
func NewCredSshKeyFromAgent(username string) (*Cred, error) {
return NewCredentialSSHKeyFromAgent(username)
}
// Deprecated: NewCredDefault is a deprecated alias fof NewCredentialDefault.
func NewCredDefault() (*Cred, error) {
return NewCredentialDefault()
}
// diff.go
const (
@ -160,6 +205,8 @@ const (
ErrPassthrough = ErrorCodePassthrough
// Deprecated: ErrIterOver is a deprecated alias of ErrorCodeIterOver.
ErrIterOver = ErrorCodeIterOver
// Deprecated: ErrApplyFail is a deprecated alias of ErrorCodeApplyFail.
ErrApplyFail = ErrorCodeApplyFail
)
// index.go
@ -183,7 +230,27 @@ func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer)
if !ok {
panic("invalid submodule visitor callback")
}
return (C.int)(callback(sub, C.GoString(name)))
err := callback(sub, C.GoString(name))
if err != nil {
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
// reference.go
// Deprecated: ReferenceIsValidName is a deprecated alias of ReferenceNameIsValid.
func ReferenceIsValidName(name string) bool {
valid, _ := ReferenceNameIsValid(name)
return valid
}
// remote.go
// Deprecated: RemoteIsValidName is a deprecated alias of RemoteNameIsValid.
func RemoteIsValidName(name string) bool {
valid, _ := RemoteNameIsValid(name)
return valid
}
// tree.go
@ -192,9 +259,13 @@ func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer)
func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int {
root := C.GoString(_root)
if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok {
return C.int(callback(root, newTreeEntry(entry)))
} else {
callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback)
if !ok {
panic("invalid treewalk callback")
}
err := callback(root, newTreeEntry(entry))
if err != nil {
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}

View File

@ -43,7 +43,7 @@ func DefaultDescribeOptions() (DescribeOptions, error) {
defer runtime.UnlockOSThread()
opts := C.git_describe_options{}
ecode := C.git_describe_init_options(&opts, C.GIT_DESCRIBE_OPTIONS_VERSION)
ecode := C.git_describe_options_init(&opts, C.GIT_DESCRIBE_OPTIONS_VERSION)
if ecode < 0 {
return DescribeOptions{}, MakeGitError(ecode)
}
@ -77,7 +77,7 @@ func DefaultDescribeFormatOptions() (DescribeFormatOptions, error) {
defer runtime.UnlockOSThread()
opts := C.git_describe_format_options{}
ecode := C.git_describe_init_format_options(&opts, C.GIT_DESCRIBE_FORMAT_OPTIONS_VERSION)
ecode := C.git_describe_format_options_init(&opts, C.GIT_DESCRIBE_FORMAT_OPTIONS_VERSION)
if ecode < 0 {
return DescribeFormatOptions{}, MakeGitError(ecode)
}
@ -213,7 +213,7 @@ func (result *DescribeResult) Format(opts *DescribeFormatOptions) (string, error
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_free(&resultBuf)
defer C.git_buf_dispose(&resultBuf)
return C.GoString(resultBuf.ptr), nil
}

198
diff.go
View File

@ -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);
@ -264,7 +265,7 @@ const (
func (stats *DiffStats) String(format DiffStatsFormat,
width uint) (string, error) {
buf := C.git_buf{}
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -457,7 +458,7 @@ func (diff *Diff) ToBuf(format DiffFormat) ([]byte, error) {
if ecode < 0 {
return nil, MakeGitError(ecode)
}
defer C.git_buf_free(&diffBuf)
defer C.git_buf_dispose(&diffBuf)
return C.GoBytes(unsafe.Pointer(diffBuf.ptr), C.int(diffBuf.size)), nil
}
@ -522,7 +523,7 @@ func DefaultDiffOptions() (DiffOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_diff_init_options(&opts, C.GIT_DIFF_OPTIONS_VERSION)
ecode := C.git_diff_options_init(&opts, C.GIT_DIFF_OPTIONS_VERSION)
if ecode < 0 {
return DiffOptions{}, MakeGitError(ecode)
}
@ -561,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
@ -577,7 +578,7 @@ func DefaultDiffFindOptions() (DiffFindOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_diff_find_init_options(&opts, C.GIT_DIFF_FIND_OPTIONS_VERSION)
ecode := C.git_diff_find_options_init(&opts, C.GIT_DIFF_FIND_OPTIONS_VERSION)
if ecode < 0 {
return DiffFindOptions{}, MakeGitError(ecode)
}
@ -643,7 +644,7 @@ func diffNotifyCallback(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_de
}
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)
C.git_diff_options_init(copts, C.GIT_DIFF_OPTIONS_VERSION)
if opts == nil {
return nil
}
@ -887,6 +888,191 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string,
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
Flags uint
}
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{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_apply_options_init(&opts, C.GIT_APPLY_OPTIONS_VERSION)
if int(ecode) != 0 {
return nil, MakeGitError(ecode)
}
return applyOptionsFromC(&opts), nil
}
func populateApplyOptions(copts *C.git_apply_options, opts *ApplyOptions, errorTarget *error) *C.git_apply_options {
C.git_apply_options_init(copts, C.GIT_APPLY_OPTIONS_VERSION)
if opts == nil {
return nil
}
copts.flags = C.uint(opts.Flags)
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{
Flags: uint(copts.flags),
}
}
// 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

View File

@ -2,6 +2,10 @@ package git
import (
"errors"
"fmt"
"io/ioutil"
"path"
"reflect"
"strings"
"testing"
)
@ -235,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
}

View File

@ -36,24 +36,26 @@ func _() {
_ = x[ErrorCodeIterOver - -31]
_ = x[ErrorCodeRetry - -32]
_ = x[ErrorCodeMismatch - -33]
_ = x[ErrorCodeIndexDirty - -34]
_ = x[ErrorCodeApplyFail - -35]
}
const (
_ErrorCode_name_0 = "MismatchRetryIterOverPassthrough"
_ErrorCode_name_0 = "ApplyFailIndexDirtyMismatchRetryIterOverPassthrough"
_ErrorCode_name_1 = "MergeConflictDirectoryUncommittedInvalidEOFPeelAppliedCertificateAuthModifiedLockedConflictInvalidSpecNonFastForwardUnmergedUnbornBranchBareRepoUserBuffsAmbiguousExistsNotFound"
_ErrorCode_name_2 = "GenericOK"
)
var (
_ErrorCode_index_0 = [...]uint8{0, 8, 13, 21, 32}
_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 -33 <= i && i <= -30:
i -= -33
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

70
git.go
View File

@ -18,35 +18,35 @@ import (
type ErrorClass int
const (
ErrorClassNone ErrorClass = C.GITERR_NONE
ErrorClassNoMemory ErrorClass = C.GITERR_NOMEMORY
ErrorClassOS ErrorClass = C.GITERR_OS
ErrorClassInvalid ErrorClass = C.GITERR_INVALID
ErrorClassReference ErrorClass = C.GITERR_REFERENCE
ErrorClassZlib ErrorClass = C.GITERR_ZLIB
ErrorClassRepository ErrorClass = C.GITERR_REPOSITORY
ErrorClassConfig ErrorClass = C.GITERR_CONFIG
ErrorClassRegex ErrorClass = C.GITERR_REGEX
ErrorClassOdb ErrorClass = C.GITERR_ODB
ErrorClassIndex ErrorClass = C.GITERR_INDEX
ErrorClassObject ErrorClass = C.GITERR_OBJECT
ErrorClassNet ErrorClass = C.GITERR_NET
ErrorClassTag ErrorClass = C.GITERR_TAG
ErrorClassTree ErrorClass = C.GITERR_TREE
ErrorClassIndexer ErrorClass = C.GITERR_INDEXER
ErrorClassSSL ErrorClass = C.GITERR_SSL
ErrorClassSubmodule ErrorClass = C.GITERR_SUBMODULE
ErrorClassThread ErrorClass = C.GITERR_THREAD
ErrorClassStash ErrorClass = C.GITERR_STASH
ErrorClassCheckout ErrorClass = C.GITERR_CHECKOUT
ErrorClassFetchHead ErrorClass = C.GITERR_FETCHHEAD
ErrorClassMerge ErrorClass = C.GITERR_MERGE
ErrorClassSSH ErrorClass = C.GITERR_SSH
ErrorClassFilter ErrorClass = C.GITERR_FILTER
ErrorClassRevert ErrorClass = C.GITERR_REVERT
ErrorClassCallback ErrorClass = C.GITERR_CALLBACK
ErrorClassRebase ErrorClass = C.GITERR_REBASE
ErrorClassPatch ErrorClass = C.GITERR_PATCH
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
@ -115,6 +115,10 @@ const (
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 (
@ -167,7 +171,7 @@ func initLibGit2() {
}
// Shutdown frees all the resources acquired by libgit2. Make sure no
// references to any git2go objects are live before calling this.
// references to any libgit2 go 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() {
@ -225,7 +229,7 @@ func NewOid(s string) (*Oid, error) {
}
if len(slice) != 20 {
return nil, &GitError{"Invalid Oid", ErrorClassNone, ErrGeneric}
return nil, &GitError{"invalid oid", ErrorClassNone, ErrorCodeGeneric}
}
copy(o[:], slice[:20])
@ -317,7 +321,7 @@ func MakeGitError(c C.int) error {
var errClass ErrorClass
errorCode := ErrorCode(c)
if errorCode != ErrorCodeIterOver {
err := C.giterr_last()
err := C.git_error_last()
if err != nil {
errMessage = C.GoString(err.message)
errClass = ErrorClass(err.klass)
@ -368,7 +372,7 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro
defer C.free(unsafe.Pointer(cstart))
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

9
go.mod
View File

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

13
go.sum
View File

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

View File

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

75
graph_test.go Normal file
View File

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

15
http.go
View File

@ -38,17 +38,17 @@ func registerManagedHTTP() error {
func httpSmartSubtransportFactory(remote *Remote, transport *Transport) (SmartSubtransport, error) {
var proxyFn func(*http.Request) (*url.URL, error)
proxyOpts, err := transport.SmartProxyOptions()
remoteConnectOpts, err := transport.SmartRemoteConnectOptions()
if err != nil {
return nil, err
}
switch proxyOpts.Type {
switch remoteConnectOpts.ProxyOptions.Type {
case ProxyTypeNone:
proxyFn = nil
case ProxyTypeAuto:
proxyFn = http.ProxyFromEnvironment
case ProxyTypeSpecified:
parsedUrl, err := url.Parse(proxyOpts.Url)
parsedUrl, err := url.Parse(remoteConnectOpts.ProxyOptions.Url)
if err != nil {
return nil, err
}
@ -103,7 +103,7 @@ func (t *httpSmartSubtransport) Action(url string, action SmartServiceAction) (S
return nil, err
}
req.Header.Set("User-Agent", "git/2.0 (git2go)")
req.Header.Set("User-Agent", "git/2.0 (libgit2)")
stream := newManagedHttpStream(t, req)
if req.Method == "POST" {
@ -203,7 +203,9 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
req.ContentLength = -1
}
req.SetBasicAuth(userName, password)
if userName != "" && password != "" {
req.SetBasicAuth(userName, password)
}
resp, err = http.DefaultClient.Do(req)
if err != nil {
return err
@ -216,10 +218,11 @@ func (self *httpSmartSubtransportStream) sendRequest() error {
if resp.StatusCode == http.StatusUnauthorized {
resp.Body.Close()
cred, err := self.owner.transport.SmartCredentials("", CredTypeUserpassPlaintext)
cred, err := self.owner.transport.SmartCredentials("", CredentialTypeUserpassPlaintext)
if err != nil {
return err
}
defer cred.Free()
userName, password, err = cred.GetUserpassPlaintext()
if err != nil {

View File

@ -10,13 +10,12 @@ extern int _go_git_index_remove_all(git_index*, const git_strarray*, void*);
*/
import "C"
import (
"errors"
"fmt"
"runtime"
"unsafe"
)
type IndexMatchedPathCallback func(string, string) int
type IndexMatchedPathCallback func(string, string) error
type indexMatchedPathCallbackData struct {
callback IndexMatchedPathCallback
errorTarget *error
@ -220,7 +219,7 @@ func (v *Index) AddFromBuffer(entry *IndexEntry, buffer []byte) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := C.git_index_add_frombuffer(v.ptr, &centry, cbuffer, C.size_t(len(buffer))); err < 0 {
if err := C.git_index_add_from_buffer(v.ptr, &centry, cbuffer, C.size_t(len(buffer))); err < 0 {
return MakeGitError(err)
}
@ -343,9 +342,9 @@ func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Po
panic("invalid matched path callback")
}
ret := data.callback(C.GoString(cPath), C.GoString(cMatchedPathspec))
if ret < 0 {
*data.errorTarget = errors.New(ErrorCode(ret).String())
err := data.callback(C.GoString(cPath), C.GoString(cMatchedPathspec))
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}

View File

@ -3,6 +3,7 @@ package git
import (
"io/ioutil"
"os"
"path"
"runtime"
"testing"
)
@ -59,14 +60,29 @@ func TestIndexWriteTreeTo(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
repo2 := createTestRepo(t)
defer cleanupTestRepo(t, repo2)
idx, err := NewIndex()
checkFatal(t, err)
idx, err := repo.Index()
odb, err := repo.Odb()
checkFatal(t, err)
err = idx.AddByPath("README")
content, err := ioutil.ReadFile(path.Join(repo.Workdir(), "README"))
checkFatal(t, err)
treeId, err := idx.WriteTreeTo(repo2)
id, err := odb.Write(content, ObjectBlob)
checkFatal(t, err)
err = idx.Add(&IndexEntry{
Mode: FilemodeBlob,
Uid: 0,
Gid: 0,
Size: uint32(len(content)),
Id: id,
Path: "README",
})
checkFatal(t, err)
treeId, err := idx.WriteTreeTo(repo)
checkFatal(t, err)
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
@ -207,9 +223,9 @@ func TestIndexAddAllCallback(t *testing.T) {
checkFatal(t, err)
cbPath := ""
err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) int {
err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) error {
cbPath = p
return 0
return nil
})
checkFatal(t, err)
if cbPath != "README" {

View File

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

View File

@ -6,7 +6,7 @@ package git
extern int git_mempack_new(git_odb_backend **out);
extern int git_mempack_dump(git_buf *pack, git_repository *repo, git_odb_backend *backend);
extern void git_mempack_reset(git_odb_backend *backend);
extern int git_mempack_reset(git_odb_backend *backend);
extern void _go_git_odb_backend_free(git_odb_backend *backend);
*/
import "C"
@ -71,7 +71,7 @@ func (mempack *Mempack) Dump(repository *Repository) ([]byte, error) {
if ret < 0 {
return nil, MakeGitError(ret)
}
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
return C.GoBytes(unsafe.Pointer(buf.ptr), C.int(buf.size)), nil
}
@ -80,6 +80,13 @@ func (mempack *Mempack) Dump(repository *Repository) ([]byte, error) {
//
// This assumes that Mempack.Dump has been called before to store all the
// queued objects into a single packfile.
func (mempack *Mempack) Reset() {
C.git_mempack_reset(mempack.ptr)
func (mempack *Mempack) Reset() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_mempack_reset(mempack.ptr)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}

View File

@ -138,7 +138,6 @@ const (
)
type MergeOptions struct {
Version uint
TreeFlags MergeTreeFlag
RenameThreshold uint
@ -151,7 +150,6 @@ type MergeOptions struct {
func mergeOptionsFromC(opts *C.git_merge_options) MergeOptions {
return MergeOptions{
Version: uint(opts.version),
TreeFlags: MergeTreeFlag(opts.flags),
RenameThreshold: uint(opts.rename_threshold),
TargetLimit: uint(opts.target_limit),
@ -166,7 +164,7 @@ func DefaultMergeOptions() (MergeOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_merge_init_options(&opts, C.GIT_MERGE_OPTIONS_VERSION)
ecode := C.git_merge_options_init(&opts, C.GIT_MERGE_OPTIONS_VERSION)
if ecode < 0 {
return MergeOptions{}, MakeGitError(ecode)
}
@ -174,11 +172,11 @@ func DefaultMergeOptions() (MergeOptions, error) {
}
func populateMergeOptions(copts *C.git_merge_options, opts *MergeOptions) *C.git_merge_options {
C.git_merge_init_options(copts, C.GIT_MERGE_OPTIONS_VERSION)
C.git_merge_options_init(copts, C.GIT_MERGE_OPTIONS_VERSION)
if opts == nil {
return nil
}
copts.flags = C.git_merge_flag_t(opts.TreeFlags)
copts.flags = C.uint32_t(opts.TreeFlags)
copts.rename_threshold = C.uint(opts.RenameThreshold)
copts.target_limit = C.uint(opts.TargetLimit)
copts.recursion_limit = C.uint(opts.RecursionLimit)
@ -518,7 +516,7 @@ func mergeFileOptionsFromC(c C.git_merge_file_options) MergeFileOptions {
}
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)
C.git_merge_file_options_init(copts, C.GIT_MERGE_FILE_OPTIONS_VERSION)
if opts == nil {
return nil
}
@ -527,7 +525,7 @@ func populateMergeFileOptions(copts *C.git_merge_file_options, opts *MergeFileOp
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.flags = C.uint32_t(opts.Flags)
copts.marker_size = C.ushort(opts.MarkerSize)
return copts
}
@ -567,7 +565,7 @@ func MergeFile(ancestor MergeFileInput, ours MergeFileInput, theirs MergeFileInp
var copts *C.git_merge_file_options
if options != nil {
copts = &C.git_merge_file_options{}
ecode := C.git_merge_file_init_options(copts, C.GIT_MERGE_FILE_OPTIONS_VERSION)
ecode := C.git_merge_file_options_init(copts, C.GIT_MERGE_FILE_OPTIONS_VERSION)
if ecode < 0 {
return nil, MakeGitError(ecode)
}

View File

@ -133,7 +133,7 @@ func (c *NoteCollection) DefaultRef() (string, error) {
}
ret := C.GoString(buf.ptr)
C.git_buf_free(&buf)
C.git_buf_dispose(&buf)
return ret, nil
}

View File

@ -13,12 +13,12 @@ import (
type ObjectType int
const (
ObjectAny ObjectType = C.GIT_OBJ_ANY
ObjectBad ObjectType = C.GIT_OBJ_BAD
ObjectCommit ObjectType = C.GIT_OBJ_COMMIT
ObjectTree ObjectType = C.GIT_OBJ_TREE
ObjectBlob ObjectType = C.GIT_OBJ_BLOB
ObjectTag ObjectType = C.GIT_OBJ_TAG
ObjectAny ObjectType = C.GIT_OBJECT_ANY
ObjectInvalid ObjectType = C.GIT_OBJECT_INVALID
ObjectCommit ObjectType = C.GIT_OBJECT_COMMIT
ObjectTree ObjectType = C.GIT_OBJECT_TREE
ObjectBlob ObjectType = C.GIT_OBJECT_BLOB
ObjectTag ObjectType = C.GIT_OBJECT_TAG
)
type Object struct {
@ -36,8 +36,8 @@ func (t ObjectType) String() string {
switch t {
case ObjectAny:
return "Any"
case ObjectBad:
return "Bad"
case ObjectInvalid:
return "Invalid"
case ObjectCommit:
return "Commit"
case ObjectTree:
@ -68,7 +68,7 @@ func (o *Object) ShortId() (string, error) {
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_free(&resultBuf)
defer C.git_buf_dispose(&resultBuf)
return C.GoString(resultBuf.ptr), nil
}
@ -218,7 +218,7 @@ func (o *Object) Peel(t ObjectType) (*Object, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_object_peel(&cobj, o.ptr, C.git_otype(t))
err := C.git_object_peel(&cobj, o.ptr, C.git_object_t(t))
runtime.KeepAlive(o)
if err < 0 {
return nil, MakeGitError(err)

38
odb.go
View File

@ -118,12 +118,12 @@ func (v *Odb) ReadHeader(oid *Oid) (uint64, ObjectType, error) {
defer runtime.UnlockOSThread()
var sz C.size_t
var cotype C.git_otype
var cotype C.git_object_t
ret := C.git_odb_read_header(&sz, &cotype, v.ptr, oid.toC())
runtime.KeepAlive(v)
if ret < 0 {
return 0, C.GIT_OBJ_BAD, MakeGitError(ret)
return 0, ObjectInvalid, MakeGitError(ret)
}
return uint64(sz), ObjectType(cotype), nil
@ -150,7 +150,7 @@ func (v *Odb) Write(data []byte, otype ObjectType) (oid *Oid, err error) {
size = C.size_t(0)
}
ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(&data[0]), size, C.git_otype(otype))
ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(&data[0]), size, C.git_object_t(otype))
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
@ -176,6 +176,32 @@ func (v *Odb) Read(oid *Oid) (obj *OdbObject, err error) {
return obj, nil
}
func (odb *Odb) Refresh() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_refresh(odb.ptr)
runtime.KeepAlive(odb)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (odb *Odb) WriteMultiPackIndex() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_write_multi_pack_index(odb.ptr)
runtime.KeepAlive(odb)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
type OdbForEachCallback func(id *Oid) error
type odbForEachCallbackData struct {
callback OdbForEachCallback
@ -237,7 +263,7 @@ func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) {
size = C.size_t(0)
}
ret := C.git_odb_hash(oid.toC(), unsafe.Pointer(&data[0]), size, C.git_otype(otype))
ret := C.git_odb_hash(oid.toC(), unsafe.Pointer(&data[0]), size, C.git_object_t(otype))
runtime.KeepAlive(data)
if ret < 0 {
return nil, MakeGitError(ret)
@ -249,7 +275,7 @@ func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) {
// contents of the object.
func (v *Odb) NewReadStream(id *Oid) (*OdbReadStream, error) {
stream := new(OdbReadStream)
var ctype C.git_otype
var ctype C.git_object_t
var csize C.size_t
runtime.LockOSThread()
@ -277,7 +303,7 @@ func (v *Odb) NewWriteStream(size int64, otype ObjectType) (*OdbWriteStream, err
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_open_wstream(&stream.ptr, v.ptr, C.git_off_t(size), C.git_otype(otype))
ret := C.git_odb_open_wstream(&stream.ptr, v.ptr, C.git_object_size_t(size), C.git_object_t(otype))
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)

View File

@ -167,9 +167,9 @@ func TestOdbWritepack(t *testing.T) {
checkFatal(t, err)
var finalStats TransferProgress
writepack, err := odb.NewWritePack(func(stats TransferProgress) ErrorCode {
writepack, err := odb.NewWritePack(func(stats TransferProgress) error {
finalStats = stats
return ErrorCodeOK
return nil
})
checkFatal(t, err)
defer writepack.Free()

View File

@ -52,7 +52,7 @@ func (patch *Patch) String() (string, error) {
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
return C.GoString(buf.ptr), nil
}

155
rebase.go
View File

@ -2,11 +2,14 @@ package git
/*
#include <git2.h>
extern void _go_git_populate_rebase_callbacks(git_rebase_options *opts);
*/
import "C"
import (
"errors"
"fmt"
"reflect"
"runtime"
"unsafe"
)
@ -69,18 +72,140 @@ func newRebaseOperationFromC(c *C.git_rebase_operation) *RebaseOperation {
return operation
}
// RebaseOptions are used to tell the rebase machinery how to operate
//export commitCreateCallback
func commitCreateCallback(
errorMessage **C.char,
_out *C.git_oid,
_author, _committer *C.git_signature,
_message_encoding, _message *C.char,
_tree *C.git_tree,
_parent_count C.size_t,
_parents **C.git_commit,
handle unsafe.Pointer,
) C.int {
data, ok := pointerHandles.Get(handle).(*rebaseOptionsData)
if !ok {
panic("invalid sign payload")
}
if data.options.CommitCreateCallback == nil && data.options.CommitSigningCallback == nil {
return C.int(ErrorCodePassthrough)
}
messageEncoding := MessageEncodingUTF8
if _message_encoding != nil {
messageEncoding = MessageEncoding(C.GoString(_message_encoding))
}
tree := &Tree{
Object: Object{
ptr: (*C.git_object)(_tree),
repo: data.repo,
},
cast_ptr: _tree,
}
var goParents []*C.git_commit
if _parent_count > 0 {
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(_parents)),
Len: int(_parent_count),
Cap: int(_parent_count),
}
goParents = *(*[]*C.git_commit)(unsafe.Pointer(&hdr))
}
parents := make([]*Commit, int(_parent_count))
for i, p := range goParents {
parents[i] = &Commit{
Object: Object{
ptr: (*C.git_object)(p),
repo: data.repo,
},
cast_ptr: p,
}
}
if data.options.CommitCreateCallback != nil {
oid, err := data.options.CommitCreateCallback(
newSignatureFromC(_author),
newSignatureFromC(_committer),
messageEncoding,
C.GoString(_message),
tree,
parents...,
)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
if oid == nil {
return C.int(ErrorCodePassthrough)
}
*_out = *oid.toC()
} else if data.options.CommitSigningCallback != nil {
commitContent, err := data.repo.CreateCommitBuffer(
newSignatureFromC(_author),
newSignatureFromC(_committer),
messageEncoding,
C.GoString(_message),
tree,
parents...,
)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
signature, signatureField, err := data.options.CommitSigningCallback(string(commitContent))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
oid, err := data.repo.CreateCommitWithSignature(string(commitContent), signature, signatureField)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
*_out = *oid.toC()
}
return C.int(ErrorCodeOK)
}
// RebaseOptions are used to tell the rebase machinery how to operate.
type RebaseOptions struct {
Version uint
Quiet int
InMemory int
RewriteNotesRef string
MergeOptions MergeOptions
CheckoutOptions CheckoutOptions
// CommitCreateCallback is an optional callback that allows users to override
// commit creation when rebasing. If specified, users can create
// their own commit and provide the commit ID, which may be useful for
// signing commits or otherwise customizing the commit creation. If this
// callback returns a nil Oid, then the rebase will continue to create the
// commit.
CommitCreateCallback CommitCreateCallback
// Deprecated: CommitSigningCallback is an optional callback that will be
// called with the commit content, allowing a signature to be added to the
// rebase commit. This field is only used when rebasing. This callback is
// not invoked if a CommitCreateCallback is specified. CommitCreateCallback
// should be used instead of this.
CommitSigningCallback CommitSigningCallback
}
type rebaseOptionsData struct {
options *RebaseOptions
repo *Repository
errorTarget *error
}
@ -91,7 +216,7 @@ func DefaultRebaseOptions() (RebaseOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_rebase_init_options(&opts, C.GIT_REBASE_OPTIONS_VERSION)
ecode := C.git_rebase_options_init(&opts, C.GIT_REBASE_OPTIONS_VERSION)
if ecode < 0 {
return RebaseOptions{}, MakeGitError(ecode)
}
@ -100,7 +225,6 @@ func DefaultRebaseOptions() (RebaseOptions, error) {
func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions {
return RebaseOptions{
Version: uint(opts.version),
Quiet: int(opts.quiet),
InMemory: int(opts.inmemory),
RewriteNotesRef: C.GoString(opts.rewrite_notes_ref),
@ -109,8 +233,8 @@ func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions {
}
}
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)
func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, repo *Repository, errorTarget *error) *C.git_rebase_options {
C.git_rebase_options_init(copts, C.GIT_REBASE_OPTIONS_VERSION)
if opts == nil {
return nil
}
@ -121,6 +245,16 @@ func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, err
populateMergeOptions(&copts.merge_options, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget)
if opts.CommitCreateCallback != nil || opts.CommitSigningCallback != nil {
data := &rebaseOptionsData{
options: opts,
repo: repo,
errorTarget: errorTarget,
}
C._go_git_populate_rebase_callbacks(copts)
copts.payload = pointerHandles.Track(data)
}
return copts
}
@ -131,6 +265,9 @@ func freeRebaseOptions(copts *C.git_rebase_options) {
C.free(unsafe.Pointer(copts.rewrite_notes_ref))
freeMergeOptions(&copts.merge_options)
freeCheckoutOptions(&copts.checkout_options)
if copts.payload != nil {
pointerHandles.Untrack(copts.payload)
}
}
func mapEmptyStringToNull(ref string) *C.char {
@ -167,7 +304,7 @@ func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedComm
var ptr *C.git_rebase
var err error
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err)
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, r, &err)
ret := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, cOpts)
runtime.KeepAlive(branch)
runtime.KeepAlive(upstream)
@ -191,7 +328,7 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) {
var ptr *C.git_rebase
var err error
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err)
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, r, &err)
ret := C.git_rebase_open(&ptr, r.ptr, cOpts)
runtime.KeepAlive(r)
if ret == C.int(ErrorCodeUser) && err != nil {
@ -342,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
}

View File

@ -1,10 +1,15 @@
package git
import (
"bytes"
"errors"
"strconv"
"strings"
"testing"
"time"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
)
// Tests
@ -14,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)
index, err := rebase.InmemoryIndex()
// 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)
// We simply resolve the conflict and commit the rebase.
if !index.HasConflicts() {
t.Fatal("expected index to have conflicts")
}
_, err = rebase.Next()
checkFatal(t, err)
conflict, err := index.Conflict("common-file")
checkFatal(t, err)
index, err := rebase.InmemoryIndex()
checkFatal(t, err)
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
checkFatal(t, err)
// We simply resolve the conflict and commit the rebase.
if !index.HasConflicts() {
t.Fatal("expected index to have conflicts")
}
resolvedEntry := *conflict.Our
resolvedEntry.Id = resolvedBlobID
checkFatal(t, index.Add(&resolvedEntry))
checkFatal(t, index.RemoveConflict("common-file"))
conflict, err := index.Conflict("common-file")
checkFatal(t, err)
var commitID Oid
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
checkFatal(t, rebase.Finish())
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
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())
}
resolvedEntry := *conflict.Our
resolvedEntry.Id = resolvedBlobID
checkFatal(t, index.Add(&resolvedEntry))
checkFatal(t, index.RemoveConflict("common-file"))
tree, err := commit.Tree()
checkFatal(t, err)
var commitID Oid
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
checkFatal(t, rebase.Finish())
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()))
}
// 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) {
@ -206,6 +211,123 @@ func TestRebaseNoConflicts(t *testing.T) {
assertStringList(t, expectedHistory, actualHistory)
}
func TestRebaseGpgSigned(t *testing.T) {
// TEST DATA
entity, err := openpgp.NewEntity("Namey mcnameface", "test comment", "test@example.com", nil)
checkFatal(t, err)
rebaseOpts, err := DefaultRebaseOptions()
checkFatal(t, err)
signCommitContent := func(commitContent string) (string, string, error) {
cipherText := new(bytes.Buffer)
err := openpgp.ArmoredDetachSignText(cipherText, entity, strings.NewReader(commitContent), &packet.Config{})
if err != nil {
return "", "", errors.New("error signing payload")
}
return cipherText.String(), "", nil
}
rebaseOpts.CommitSigningCallback = signCommitContent
commitOpts := commitOptions{
CommitSigningCallback: signCommitContent,
}
// Inputs
branchName := "emile"
masterCommit := "something"
emileCommits := []string{
"fou",
"barre",
"ouich",
}
// Outputs
expectedHistory := []string{
"Test rebase, Baby! " + emileCommits[2],
"Test rebase, Baby! " + emileCommits[1],
"Test rebase, Baby! " + emileCommits[0],
"Test rebase, Baby! " + masterCommit,
"This is a commit\n",
}
// TEST
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepoOpt(t, repo, commitOpts)
// Try to open existing rebase
_, err = repo.OpenRebase(nil)
if err == nil {
t.Fatal("Did not expect to find a rebase in progress")
}
// Setup a repo with 2 branches and a different tree
err = setupRepoForRebase(repo, masterCommit, branchName, commitOpts)
checkFatal(t, err)
// Create several commits in emile
for _, commit := range emileCommits {
_, err = commitSomething(repo, commit, commit, commitOpts)
checkFatal(t, err)
}
// Rebase onto master
rebase, err := performRebaseOnto(repo, "master", &rebaseOpts)
checkFatal(t, err)
defer rebase.Free()
// Finish the rebase properly
err = rebase.Finish()
checkFatal(t, err)
// Check history is in correct order
actualHistory, err := commitMsgsList(repo)
checkFatal(t, err)
assertStringList(t, expectedHistory, actualHistory)
checkAllCommitsSigned(t, entity, repo)
}
func checkAllCommitsSigned(t *testing.T, entity *openpgp.Entity, repo *Repository) {
head, err := headCommit(repo)
checkFatal(t, err)
defer head.Free()
parent := head
err = checkCommitSigned(t, entity, parent)
checkFatal(t, err)
for parent.ParentCount() != 0 {
parent = parent.Parent(0)
defer parent.Free()
err = checkCommitSigned(t, entity, parent)
checkFatal(t, err)
}
}
func checkCommitSigned(t *testing.T, entity *openpgp.Entity, commit *Commit) error {
t.Helper()
signature, signedData, err := commit.ExtractSignature()
if err != nil {
t.Logf("No signature on commit\n%s", commit.ContentToSign())
return err
}
_, err = openpgp.CheckArmoredDetachedSignature(openpgp.EntityList{entity}, strings.NewReader(signedData), bytes.NewBufferString(signature), nil)
if err != nil {
t.Logf("Commit is not signed correctly\n%s", commit.ContentToSign())
return err
}
return nil
}
// Utils
func setupRepoForRebase(repo *Repository, masterCommit, branchName string, commitOpts commitOptions) error {
// Create a new branch from master

View File

@ -286,7 +286,7 @@ func (v *Reference) Peel(t ObjectType) (*Object, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_reference_peel(&cobj, v.ptr, C.git_otype(t))
err := C.git_reference_peel(&cobj, v.ptr, C.git_object_t(t))
runtime.KeepAlive(v)
if err < 0 {
return nil, MakeGitError(err)
@ -476,7 +476,7 @@ func (v *ReferenceIterator) Free() {
C.git_reference_iterator_free(v.ptr)
}
// ReferenceIsValidName returns whether the reference name is well-formed.
// ReferenceNameIsValid returns whether the reference name is well-formed.
//
// Valid reference names must follow one of two patterns:
//
@ -486,11 +486,19 @@ func (v *ReferenceIterator) Free() {
// 2. Names prefixed with "refs/" can be almost anything. You must avoid
// the characters '~', '^', ':', ' \ ', '?', '[', and '*', and the sequences
// ".." and " @ {" which have special meaning to revparse.
func ReferenceIsValidName(name string) bool {
func ReferenceNameIsValid(name string) (bool, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return C.git_reference_is_valid_name(cname) == 1
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var valid C.int
ret := C.git_reference_name_is_valid(&valid, cname)
if ret < 0 {
return false, MakeGitError(ret)
}
return valid == 1, nil
}
const (
@ -501,10 +509,10 @@ const (
type ReferenceFormat uint
const (
ReferenceFormatNormal ReferenceFormat = C.GIT_REF_FORMAT_NORMAL
ReferenceFormatAllowOnelevel ReferenceFormat = C.GIT_REF_FORMAT_ALLOW_ONELEVEL
ReferenceFormatRefspecPattern ReferenceFormat = C.GIT_REF_FORMAT_REFSPEC_PATTERN
ReferenceFormatRefspecShorthand ReferenceFormat = C.GIT_REF_FORMAT_REFSPEC_SHORTHAND
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.

View File

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

149
refspec.go Normal file
View File

@ -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
}

75
refspec_test.go Normal file
View File

@ -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)
}
}

229
remote.go
View File

@ -4,6 +4,7 @@ package git
#include <string.h>
#include <git2.h>
#include <git2/sys/cred.h>
extern void _go_git_populate_remote_callbacks(git_remote_callbacks *callbacks);
*/
@ -11,6 +12,7 @@ import "C"
import (
"crypto/x509"
"errors"
"fmt"
"reflect"
"runtime"
"strings"
@ -20,6 +22,23 @@ import (
"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
@ -51,15 +70,15 @@ const (
ConnectDirectionPush ConnectDirection = C.GIT_DIRECTION_PUSH
)
type TransportMessageCallback func(str string) ErrorCode
type CompletionCallback func(RemoteCompletion) ErrorCode
type CredentialsCallback func(url string, username_from_url string, allowed_types CredType) (ErrorCode, *Cred)
type TransferProgressCallback func(stats TransferProgress) ErrorCode
type UpdateTipsCallback func(refname string, a *Oid, b *Oid) ErrorCode
type CertificateCheckCallback func(cert *Certificate, valid bool, hostname string) ErrorCode
type PackbuilderProgressCallback func(stage int32, current, total uint32) ErrorCode
type PushTransferProgressCallback func(current, total uint32, bytes uint) ErrorCode
type PushUpdateReferenceCallback func(refname, status string) ErrorCode
type TransportMessageCallback func(str string) error
type CompletionCallback func(RemoteCompletion) error
type CredentialsCallback func(url string, username_from_url string, allowed_types CredentialType) (*Credential, error)
type TransferProgressCallback func(stats TransferProgress) error
type UpdateTipsCallback func(refname string, a *Oid, b *Oid) error
type CertificateCheckCallback func(cert *Certificate, valid bool, hostname string) error
type PackbuilderProgressCallback func(stage int32, current, total uint32) error
type PushTransferProgressCallback func(current, total uint32, bytes uint) error
type PushUpdateReferenceCallback func(refname, status string) error
type RemoteCallbacks struct {
SidebandProgressCallback TransportMessageCallback
@ -128,6 +147,17 @@ type FetchOptions struct {
ProxyOptions ProxyOptions
}
type RemoteConnectOptions struct {
// Proxy options to use for this fetch operation
ProxyOptions ProxyOptions
}
func remoteConnectOptionsFromC(copts *C.git_remote_connect_options) *RemoteConnectOptions {
return &RemoteConnectOptions{
ProxyOptions: proxyOptionsFromC(&copts.proxy_opts),
}
}
type ProxyType uint
const (
@ -152,8 +182,8 @@ type ProxyOptions struct {
Url string
}
func proxyOptionsFromC(copts *C.git_proxy_options) *ProxyOptions {
return &ProxyOptions{
func proxyOptionsFromC(copts *C.git_proxy_options) ProxyOptions {
return ProxyOptions{
Type: ProxyType(copts._type),
Url: C.GoString(copts.url),
}
@ -164,6 +194,9 @@ type Remote struct {
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 {
@ -234,7 +267,7 @@ const (
// Certificate represents the two possible certificates which libgit2
// knows it might find. If Kind is CertficateX509 then the X509 field
// will be filled. If Kind is CertificateHostkey then the Hostkey
// field will be fille.d
// field will be filled.
type Certificate struct {
Kind CertificateKind
X509 *x509.Certificate
@ -247,8 +280,8 @@ type HostkeyKind uint
const (
HostkeyMD5 HostkeyKind = C.GIT_CERT_SSH_MD5
HostkeySHA1 HostkeyKind = C.GIT_CERT_SSH_SHA1
HostkeySHA256 HostkeyKind = 1 << 2
HostkeyRaw HostkeyKind = 1 << 3
HostkeySHA256 HostkeyKind = C.GIT_CERT_SSH_SHA256
HostkeyRaw HostkeyKind = C.GIT_CERT_SSH_RAW
)
// Server host key information. A bitmask containing the available fields.
@ -314,10 +347,8 @@ func sidebandProgressCallback(errorMessage **C.char, _str *C.char, _len C.int, h
if data.callbacks.SidebandProgressCallback == nil {
return C.int(ErrorCodeOK)
}
str := C.GoStringN(_str, _len)
ret := data.callbacks.SidebandProgressCallback(str)
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
err := data.callbacks.SidebandProgressCallback(C.GoStringN(_str, _len))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
@ -327,14 +358,13 @@ func sidebandProgressCallback(errorMessage **C.char, _str *C.char, _len C.int, h
}
//export completionCallback
func completionCallback(errorMessage **C.char, completion_type C.git_remote_completion_type, handle unsafe.Pointer) C.int {
func completionCallback(errorMessage **C.char, completionType C.git_remote_completion_type, handle unsafe.Pointer) C.int {
data := pointerHandles.Get(handle).(*remoteCallbacksData)
if data.callbacks.CompletionCallback == nil {
return C.int(ErrorCodeOK)
}
ret := data.callbacks.CompletionCallback(RemoteCompletion(completion_type))
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
err := data.callbacks.CompletionCallback(RemoteCompletion(completionType))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
@ -346,7 +376,7 @@ func completionCallback(errorMessage **C.char, completion_type C.git_remote_comp
//export credentialsCallback
func credentialsCallback(
errorMessage **C.char,
_cred **C.git_cred,
_cred **C.git_credential,
_url *C.char,
_username_from_url *C.char,
allowed_types uint,
@ -358,9 +388,8 @@ func credentialsCallback(
}
url := C.GoString(_url)
username_from_url := C.GoString(_username_from_url)
ret, cred := data.callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types))
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
cred, err := data.callbacks.CredentialsCallback(url, username_from_url, (CredentialType)(allowed_types))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
@ -382,9 +411,8 @@ func transferProgressCallback(errorMessage **C.char, stats *C.git_transfer_progr
if data.callbacks.TransferProgressCallback == nil {
return C.int(ErrorCodeOK)
}
ret := data.callbacks.TransferProgressCallback(newTransferProgressFromC(stats))
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
err := data.callbacks.TransferProgressCallback(newTransferProgressFromC(stats))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
@ -408,9 +436,8 @@ func updateTipsCallback(
refname := C.GoString(_refname)
a := newOidFromC(_a)
b := newOidFromC(_b)
ret := data.callbacks.UpdateTipsCallback(refname, a, b)
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
err := data.callbacks.UpdateTipsCallback(refname, a, b)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
@ -466,6 +493,18 @@ func certificateCheckCallback(
cert.Hostkey.Kind = HostkeyKind(ccert._type)
C.memcpy(unsafe.Pointer(&cert.Hostkey.HashMD5[0]), unsafe.Pointer(&ccert.hash_md5[0]), C.size_t(len(cert.Hostkey.HashMD5)))
C.memcpy(unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), unsafe.Pointer(&ccert.hash_sha1[0]), C.size_t(len(cert.Hostkey.HashSHA1)))
C.memcpy(unsafe.Pointer(&cert.Hostkey.HashSHA256[0]), unsafe.Pointer(&ccert.hash_sha256[0]), C.size_t(len(cert.Hostkey.HashSHA256)))
if (cert.Hostkey.Kind & HostkeyRaw) == HostkeyRaw {
cert.Hostkey.Hostkey = C.GoBytes(unsafe.Pointer(ccert.hostkey), C.int(ccert.hostkey_len))
var err error
cert.Hostkey.SSHPublicKey, err = ssh.ParsePublicKey(cert.Hostkey.Hostkey)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
}
} else {
err := errors.New("unsupported certificate type")
if data.errorTarget != nil {
@ -474,9 +513,8 @@ func certificateCheckCallback(
return setCallbackError(errorMessage, err)
}
ret := data.callbacks.CertificateCheckCallback(&cert, valid, host)
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
err := data.callbacks.CertificateCheckCallback(&cert, valid, host)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
@ -492,9 +530,8 @@ func packProgressCallback(errorMessage **C.char, stage C.int, current, total C.u
return C.int(ErrorCodeOK)
}
ret := data.callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total))
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
err := data.callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
@ -510,9 +547,8 @@ func pushTransferProgressCallback(errorMessage **C.char, current, total C.uint,
return C.int(ErrorCodeOK)
}
ret := data.callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes))
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
err := data.callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
@ -528,9 +564,8 @@ func pushUpdateReferenceCallback(errorMessage **C.char, refname, status *C.char,
return C.int(ErrorCodeOK)
}
ret := data.callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status))
if ret < 0 {
err := errors.New(ErrorCode(ret).String())
err := data.callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
@ -540,7 +575,7 @@ func pushUpdateReferenceCallback(errorMessage **C.char, refname, status *C.char,
}
func populateProxyOptions(copts *C.git_proxy_options, opts *ProxyOptions) *C.git_proxy_options {
C.git_proxy_init_options(copts, C.GIT_PROXY_OPTIONS_VERSION)
C.git_proxy_options_init(copts, C.GIT_PROXY_OPTIONS_VERSION)
if opts == nil {
return nil
}
@ -558,12 +593,20 @@ func freeProxyOptions(copts *C.git_proxy_options) {
C.free(unsafe.Pointer(copts.url))
}
// RemoteIsValidName returns whether the remote name is well-formed.
func RemoteIsValidName(name string) bool {
// RemoteNameIsValid returns whether the remote name is well-formed.
func RemoteNameIsValid(name string) (bool, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return C.git_remote_is_valid_name(cname) == 1
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var valid C.int
ret := C.git_remote_name_is_valid(&valid, cname)
if ret < 0 {
return false, MakeGitError(ret)
}
return valid == 1, nil
}
// free releases the resources of the Remote.
@ -577,6 +620,9 @@ func (r *Remote) free() {
// Free releases the resources of the Remote.
func (r *Remote) Free() {
r.repo.Remotes.untrackRemote(r)
if r.weak {
return
}
r.free()
}
@ -615,7 +661,7 @@ func (c *RemoteCollection) List() ([]string, error) {
if ecode < 0 {
return nil, MakeGitError(ecode)
}
defer C.git_strarray_free(&r)
defer C.git_strarray_dispose(&r)
remotes := makeStringsFromCStrings(r.strings, int(r.count))
return remotes, nil
@ -640,6 +686,28 @@ func (c *RemoteCollection) Create(name string, url string) (*Remote, error) {
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
}
func (c *RemoteCollection) Delete(name string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@ -863,7 +931,7 @@ func (o *Remote) FetchRefspecs() ([]string, error) {
if ret < 0 {
return nil, MakeGitError(ret)
}
defer C.git_strarray_free(&crefspecs)
defer C.git_strarray_dispose(&crefspecs)
refspecs := makeStringsFromCStrings(crefspecs.strings, int(crefspecs.count))
return refspecs, nil
@ -896,7 +964,7 @@ func (o *Remote) PushRefspecs() ([]string, error) {
if ret < 0 {
return nil, MakeGitError(ret)
}
defer C.git_strarray_free(&crefspecs)
defer C.git_strarray_dispose(&crefspecs)
runtime.KeepAlive(o)
refspecs := makeStringsFromCStrings(crefspecs.strings, int(crefspecs.count))
@ -910,13 +978,15 @@ func (o *Remote) RefspecCount() uint {
}
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)
C.git_fetch_options_init(copts, C.GIT_FETCH_OPTIONS_VERSION)
if opts == nil {
return nil
}
populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget)
copts.prune = C.git_fetch_prune_t(opts.Prune)
copts.update_fetchhead = cbool(opts.UpdateFetchhead)
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.download_tags = C.git_remote_autotag_option_t(opts.DownloadTags)
copts.custom_headers = C.git_strarray{
@ -937,7 +1007,7 @@ func freeFetchOptions(copts *C.git_fetch_options) {
}
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)
C.git_push_options_init(copts, C.GIT_PUSH_OPTIONS_VERSION)
if opts == nil {
return nil
}
@ -1144,3 +1214,54 @@ func (o *Remote) Prune(callbacks *RemoteCallbacks) error {
}
return nil
}
// DefaultApplyOptions returns default options for remote create
func DefaultRemoteCreateOptions() (*RemoteCreateOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
opts := C.git_remote_create_options{}
ecode := C.git_remote_create_options_init(&opts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return &RemoteCreateOptions{
Flags: RemoteCreateOptionsFlag(opts.flags),
}, nil
}
func populateRemoteCreateOptions(copts *C.git_remote_create_options, opts *RemoteCreateOptions, repo *Repository) *C.git_remote_create_options {
C.git_remote_create_options_init(copts, C.GIT_REMOTE_CREATE_OPTIONS_VERSION)
if opts == nil {
return nil
}
var cRepository *C.git_repository
if repo != nil {
cRepository = repo.ptr
}
copts.repository = cRepository
copts.name = C.CString(opts.Name)
copts.fetchspec = C.CString(opts.FetchSpec)
copts.flags = C.uint(opts.Flags)
return copts
}
func freeRemoteCreateOptions(ptr *C.git_remote_create_options) {
if ptr == nil {
return
}
C.free(unsafe.Pointer(ptr.name))
C.free(unsafe.Pointer(ptr.fetchspec))
}
// createNewEmptyRemote used to get a new empty object of *Remote
func createNewEmptyRemote() *Remote {
return &Remote{
callbacks: RemoteCallbacks{},
repo: nil,
weak: false,
}
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"crypto/rand"
"crypto/rsa"
"errors"
"fmt"
"io"
"net"
@ -37,13 +38,13 @@ func TestListRemotes(t *testing.T) {
compareStringList(t, expected, actual)
}
func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T) ErrorCode {
func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T) error {
if hostname != "github.com" {
t.Fatal("Hostname does not match")
return ErrorCodeUser
t.Fatal("hostname does not match")
return errors.New("hostname does not match")
}
return ErrorCodeOK
return nil
}
func TestCertificateCheck(t *testing.T) {
@ -57,7 +58,7 @@ func TestCertificateCheck(t *testing.T) {
options := FetchOptions{
RemoteCallbacks: RemoteCallbacks{
CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) ErrorCode {
CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) error {
return assertHostname(cert, valid, hostname, t)
},
},
@ -80,6 +81,29 @@ func TestRemoteConnect(t *testing.T) {
checkFatal(t, err)
}
func TestRemoteConnectOption(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
config, err := repo.Config()
checkFatal(t, err)
err = config.SetString("url.git@github.com:.insteadof", "https://github.com/")
checkFatal(t, err)
option, err := DefaultRemoteCreateOptions()
checkFatal(t, err)
option.Name = "origin"
option.Flags = RemoteCreateSkipInsteadof
remote, err := repo.Remotes.CreateWithOptions("https://github.com/libgit2/TestGitRepository", option)
checkFatal(t, err)
defer remote.Free()
err = remote.ConnectFetch(nil, nil, nil)
checkFatal(t, err)
}
func TestRemoteLs(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
@ -219,17 +243,18 @@ func TestRemoteCredentialsCalled(t *testing.T) {
checkFatal(t, err)
defer remote.Free()
errNonExistent := errors.New("non-existent repository")
fetchOpts := FetchOptions{
RemoteCallbacks: RemoteCallbacks{
CredentialsCallback: func(url, username string, allowedTypes CredType) (ErrorCode, *Cred) {
return ErrorCodeUser, nil
CredentialsCallback: func(url, username string, allowedTypes CredentialType) (*Credential, error) {
return nil, errNonExistent
},
},
}
err = remote.Fetch(nil, &fetchOpts, "fetch")
if IsErrorCode(err, ErrorCodeUser) {
t.Fatalf("remote.Fetch() = %v, want %v", err, ErrorCodeUser)
if err != errNonExistent {
t.Fatalf("remote.Fetch() = %v, want %v", err, errNonExistent)
}
}
@ -454,33 +479,29 @@ func TestRemoteSSH(t *testing.T) {
certificateCheckCallbackCalled := false
fetchOpts := FetchOptions{
RemoteCallbacks: RemoteCallbacks{
CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) ErrorCode {
CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) error {
hostkeyFingerprint := fmt.Sprintf("%x", cert.Hostkey.HashMD5[:])
if hostkeyFingerprint != publicKeyFingerprint {
t.Logf("server hostkey %q, want %q", hostkeyFingerprint, publicKeyFingerprint)
return ErrorCodeAuth
return fmt.Errorf("server hostkey %q, want %q", hostkeyFingerprint, publicKeyFingerprint)
}
certificateCheckCallbackCalled = true
return ErrorCodeOK
return nil
},
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
CredentialsCallback: func(url, username string, allowedTypes CredentialType) (*Credential, error) {
if allowedTypes&(CredentialTypeSSHKey|CredentialTypeSSHCustom|CredentialTypeSSHMemory) != 0 {
return NewCredentialSSHKeyFromSigner(pubKeyUsername, signer)
}
t.Logf("unknown credential type %+v", allowedTypes)
return ErrorCodeAuth, nil
if (allowedTypes & CredentialTypeUsername) != 0 {
return NewCredentialUsername(pubKeyUsername)
}
return nil, fmt.Errorf("unknown credential type %+v", allowedTypes)
},
},
}
remote, err := repo.Remotes.Create(
"origin",
fmt.Sprintf("ssh://%s@%s/TestGitRepository", pubKeyUsername, listener.Addr().String()),
fmt.Sprintf("ssh://%s/TestGitRepository", listener.Addr().String()),
)
checkFatal(t, err)
defer remote.Free()

View File

@ -173,9 +173,15 @@ func (v *Repository) Config() (*Config, error) {
// 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.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_set_config(v.ptr, c.ptr)
runtime.KeepAlive(v)
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
@ -199,7 +205,7 @@ func (v *Repository) lookupType(id *Oid, t ObjectType) (*Object, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_object_lookup(&ptr, v.ptr, id.toC(), C.git_otype(t))
ret := C.git_object_lookup(&ptr, v.ptr, id.toC(), C.git_object_t(t))
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)
@ -214,7 +220,7 @@ func (v *Repository) lookupPrefixType(id *Oid, prefix uint, t ObjectType) (*Obje
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_object_lookup_prefix(&ptr, v.ptr, id.toC(), C.size_t(prefix), C.git_otype(t))
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)
@ -562,7 +568,7 @@ func (v *Repository) CreateCommitBuffer(
defer runtime.UnlockOSThread()
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
ret := C.git_commit_create_buffer(
&buf, v.ptr,
authorSig, committerSig,
@ -809,7 +815,7 @@ func (r *Repository) ClearGitIgnoreRules() error {
// the file after you create the commit.
func (r *Repository) Message() (string, error) {
buf := C.git_buf{}
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -856,7 +862,7 @@ const (
func (r *Repository) ItemPath(item RepositoryItem) (string, error) {
var c_buf C.git_buf
defer C.git_buf_free(&c_buf)
defer C.git_buf_dispose(&c_buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()

View File

@ -10,27 +10,27 @@ import (
// RevertOptions contains options for performing a revert
type RevertOptions struct {
Mainline uint
MergeOpts MergeOptions
CheckoutOpts CheckoutOptions
Mainline uint
MergeOptions MergeOptions
CheckoutOptions 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)
C.git_revert_options_init(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)
populateMergeOptions(&copts.merge_opts, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, 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),
Mainline: uint(copts.mainline),
MergeOptions: mergeOptionsFromC(&copts.merge_opts),
CheckoutOptions: checkoutOptionsFromC(&copts.checkout_opts),
}
}
@ -49,7 +49,7 @@ func DefaultRevertOptions() (RevertOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revert_init_options(&copts, C.GIT_REVERT_OPTIONS_VERSION)
ecode := C.git_revert_options_init(&copts, C.GIT_REVERT_OPTIONS_VERSION)
if ecode < 0 {
return RevertOptions{}, MakeGitError(ecode)
}

View File

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

View File

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

View File

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

View File

@ -18,7 +18,7 @@ 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_otype type, size_t size)
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);
}
@ -41,7 +41,7 @@ import (
func SearchPath(level ConfigLevel) (string, error) {
var buf C.git_buf
defer C.git_buf_free(&buf)
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
@ -126,7 +126,7 @@ func SetCacheObjectLimit(objectType ObjectType, size int) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C._go_git_opts_set_cache_object_limit(C.git_otype(objectType), C.size_t(size))
err := C._go_git_opts_set_cache_object_limit(C.git_object_t(objectType), C.size_t(size))
if err < 0 {
return MakeGitError(err)
}

29
ssh.go
View File

@ -3,7 +3,7 @@ package git
/*
#include <git2.h>
void _go_git_credential_free(git_cred *cred);
#include <git2/sys/credential.h>
*/
import "C"
import (
@ -106,11 +106,11 @@ func (t *sshSmartSubtransport) Action(urlString string, action SmartServiceActio
return nil, fmt.Errorf("unexpected action: %v", action)
}
cred, err := t.transport.SmartCredentials("", CredTypeSshKey)
cred, err := t.transport.SmartCredentials("", CredentialTypeSSHKey|CredentialTypeSSHMemory)
if err != nil {
return nil, err
}
defer C._go_git_credential_free(cred.ptr)
defer cred.Free()
sshConfig, err := getSSHConfigFromCredential(cred)
if err != nil {
@ -201,10 +201,10 @@ func (stream *sshSmartSubtransportStream) Write(buf []byte) (int, error) {
func (stream *sshSmartSubtransportStream) Free() {
}
func getSSHConfigFromCredential(cred *Cred) (*ssh.ClientConfig, error) {
func getSSHConfigFromCredential(cred *Credential) (*ssh.ClientConfig, error) {
switch cred.Type() {
case CredTypeSshCustom:
credSSHCustom := (*C.git_cred_ssh_custom)(unsafe.Pointer(cred.ptr))
case CredentialTypeSSHCustom:
credSSHCustom := (*C.git_credential_ssh_custom)(unsafe.Pointer(cred.ptr))
data, ok := pointerHandles.Get(credSSHCustom.payload).(*credentialSSHCustomData)
if !ok {
return nil, errors.New("unsupported custom SSH credentials")
@ -215,16 +215,21 @@ func getSSHConfigFromCredential(cred *Cred) (*ssh.ClientConfig, error) {
}, nil
}
username, _, privatekey, passphrase, ret := cred.GetSSHKey()
if ret != nil {
return nil, ret
}
pemBytes, err := ioutil.ReadFile(privatekey)
username, _, privatekey, passphrase, err := cred.GetSSHKey()
if err != nil {
return nil, err
}
var pemBytes []byte
if cred.Type() == CredentialTypeSSHMemory {
pemBytes = []byte(privatekey)
} else {
pemBytes, err = ioutil.ReadFile(privatekey)
if err != nil {
return nil, err
}
}
var key ssh.Signer
if passphrase != "" {
key, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(passphrase))

View File

@ -152,7 +152,7 @@ func DefaultStashApplyOptions() (StashApplyOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_stash_apply_init_options(&optsC, C.GIT_STASH_APPLY_OPTIONS_VERSION)
ecode := C.git_stash_apply_options_init(&optsC, C.GIT_STASH_APPLY_OPTIONS_VERSION)
if ecode < 0 {
return StashApplyOptions{}, MakeGitError(ecode)
}
@ -163,11 +163,11 @@ func DefaultStashApplyOptions() (StashApplyOptions, error) {
}
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)
C.git_stash_apply_options_init(copts, C.GIT_STASH_APPLY_OPTIONS_VERSION)
if opts == nil {
return nil
}
copts.flags = C.git_stash_apply_flags(opts.Flags)
copts.flags = C.uint32_t(opts.Flags)
populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget)
if opts.ProgressCallback != nil {
progressData := &stashApplyProgressCallbackData{

View File

@ -160,7 +160,7 @@ func (v *Repository) StatusList(opts *StatusOptions) (*StatusList, error) {
}
} else {
copts = &C.git_status_options{}
ret := C.git_status_init_options(copts, C.GIT_STATUS_OPTIONS_VERSION)
ret := C.git_status_options_init(copts, C.GIT_STATUS_OPTIONS_VERSION)
if ret < 0 {
return nil, MakeGitError(ret)
}

View File

@ -8,15 +8,14 @@ extern int _go_git_visit_submodule(git_repository *repo, void *fct);
import "C"
import (
"errors"
"runtime"
"unsafe"
)
// SubmoduleUpdateOptions
type SubmoduleUpdateOptions struct {
*CheckoutOpts
*FetchOptions
CheckoutOptions CheckoutOptions
FetchOptions FetchOptions
}
// Submodule
@ -111,7 +110,7 @@ func (c *SubmoduleCollection) Lookup(name string) (*Submodule, error) {
}
// SubmoduleCallback is a function that is called for every submodule found in SubmoduleCollection.Foreach.
type SubmoduleCallback func(sub *Submodule, name string) int
type SubmoduleCallback func(sub *Submodule, name string) error
type submoduleCallbackData struct {
callback SubmoduleCallback
errorTarget *error
@ -126,9 +125,9 @@ func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer)
panic("invalid submodule visitor callback")
}
ret := data.callback(sub, C.GoString(name))
if ret < 0 {
*data.errorTarget = errors.New(ErrorCode(ret).String())
err := data.callback(sub, C.GoString(name))
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
@ -386,13 +385,13 @@ func (sub *Submodule) Update(init bool, opts *SubmoduleUpdateOptions) error {
}
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)
C.git_submodule_update_options_init(copts, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION)
if opts == nil {
return nil
}
populateCheckoutOptions(&copts.checkout_opts, opts.CheckoutOpts, errorTarget)
populateFetchOptions(&copts.fetch_opts, opts.FetchOptions, errorTarget)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
populateFetchOptions(&copts.fetch_opts, &opts.FetchOptions, errorTarget)
return copts
}

View File

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

4
tag.go
View File

@ -161,7 +161,7 @@ func (c *TagsCollection) List() ([]string, error) {
if ecode < 0 {
return nil, MakeGitError(ecode)
}
defer C.git_strarray_free(&strC)
defer C.git_strarray_dispose(&strC)
tags := makeStringsFromCStrings(strC.strings, int(strC.count))
return tags, nil
@ -185,7 +185,7 @@ func (c *TagsCollection) ListWithMatch(pattern string) ([]string, error) {
if ecode < 0 {
return nil, MakeGitError(ecode)
}
defer C.git_strarray_free(&strC)
defer C.git_strarray_dispose(&strC)
tags := makeStringsFromCStrings(strC.strings, int(strC.count))
return tags, nil

View File

@ -22,7 +22,6 @@ void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stre
*/
import "C"
import (
"errors"
"fmt"
"io"
"reflect"
@ -32,7 +31,7 @@ import (
)
var (
// globalRegisteredSmartTransports is a mapping of global, git2go-managed
// globalRegisteredSmartTransports is a mapping of global, libgit2 go-managed
// transports.
globalRegisteredSmartTransports = struct {
sync.Mutex
@ -42,7 +41,7 @@ var (
}
)
// unregisterManagedTransports unregisters all git2go-managed transports.
// unregisterManagedTransports unregisters all libgit2 go-managed transports.
func unregisterManagedTransports() error {
globalRegisteredSmartTransports.Lock()
originalTransports := globalRegisteredSmartTransports.transports
@ -85,22 +84,22 @@ type Transport struct {
ptr *C.git_transport
}
// SmartProxyOptions gets a copy of the proxy options for this transport.
func (t *Transport) SmartProxyOptions() (*ProxyOptions, error) {
// SmartRemoteConnectOptions gets a copy of the proxy options for this transport.
func (t *Transport) SmartRemoteConnectOptions() (*RemoteConnectOptions, 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 {
var copts C.git_remote_connect_options
if ret := C.git_transport_remote_connect_options(&copts, t.ptr); ret < 0 {
return nil, MakeGitError(ret)
}
return proxyOptionsFromC(&cpopts), nil
return remoteConnectOptionsFromC(&copts), nil
}
// SmartCredentials calls the credentials callback for this transport.
func (t *Transport) SmartCredentials(user string, methods CredType) (*Cred, error) {
cred := &Cred{}
func (t *Transport) SmartCredentials(user string, methods CredentialType) (*Credential, error) {
cred := newCredential()
var cstr *C.char
runtime.LockOSThread()
@ -112,6 +111,7 @@ func (t *Transport) SmartCredentials(user string, methods CredType) (*Cred, erro
}
ret := C.git_transport_smart_credentials(&cred.ptr, t.ptr, cstr, C.int(methods))
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
@ -127,10 +127,21 @@ func (t *Transport) SmartCertificateCheck(cert *Certificate, valid bool, hostnam
parent: C.git_cert{
cert_type: C.GIT_CERT_HOSTKEY_LIBSSH2,
},
_type: C.git_cert_ssh_t(cert.Kind),
_type: C.git_cert_ssh_t(cert.Kind),
hostkey: (*C.char)(C.CBytes(cert.Hostkey.Hostkey)),
hostkey_len: C.size_t(len(cert.Hostkey.Hostkey)),
}
defer C.free(unsafe.Pointer(chostkeyCert.hostkey))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_md5[0]), unsafe.Pointer(&cert.Hostkey.HashMD5[0]), C.size_t(len(cert.Hostkey.HashMD5)))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_sha1[0]), unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), C.size_t(len(cert.Hostkey.HashSHA1)))
C.memcpy(unsafe.Pointer(&chostkeyCert.hash_sha256[0]), unsafe.Pointer(&cert.Hostkey.HashSHA256[0]), C.size_t(len(cert.Hostkey.HashSHA256)))
if cert.Hostkey.SSHPublicKey.Type() == "ssh-rsa" {
chostkeyCert.raw_type = C.GIT_CERT_SSH_RAW_TYPE_RSA
} else if cert.Hostkey.SSHPublicKey.Type() == "ssh-dss" {
chostkeyCert.raw_type = C.GIT_CERT_SSH_RAW_TYPE_DSS
} else {
chostkeyCert.raw_type = C.GIT_CERT_SSH_RAW_TYPE_UNKNOWN
}
ccert = (*C.git_cert)(unsafe.Pointer(&chostkeyCert))
case CertificateX509:
@ -294,8 +305,10 @@ func smartTransportCallback(
registeredSmartTransport := pointerHandles.Get(handle).(*RegisteredSmartTransport)
remote, ok := remotePointers.get(owner)
if !ok {
err := errors.New("remote pointer not found")
return setCallbackError(errorMessage, err)
// 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{

23
tree.go
View File

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

View File

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

1
vendor/libgit2 vendored

@ -1 +0,0 @@
Subproject commit 45c6187cbfdcfee2bcf59a1ed3a853065142f203

View File

@ -3,6 +3,7 @@
#include <git2.h>
#include <git2/sys/odb_backend.h>
#include <git2/sys/refdb_backend.h>
#include <git2/sys/cred.h>
// There are two ways in which to declare a callback:
//
@ -103,12 +104,50 @@ static int set_callback_error(char *error_message, int ret)
{
if (error_message != NULL) {
if (ret < 0)
giterr_set_str(GITERR_CALLBACK, error_message);
giterr_set_str(GIT_ERROR_CALLBACK, error_message);
free(error_message);
}
return ret;
}
void _go_git_populate_apply_callbacks(git_apply_options *options)
{
options->delta_cb = (git_apply_delta_cb)&deltaApplyCallback;
options->hunk_cb = (git_apply_hunk_cb)&hunkApplyCallback;
}
static int commit_create_callback(
git_oid *out,
const git_signature *author,
const git_signature *committer,
const char *message_encoding,
const char *message,
const git_tree *tree,
size_t parent_count,
const git_commit *parents[],
void *payload)
{
char *error_message = NULL;
const int ret = commitCreateCallback(
&error_message,
out,
(git_signature *)author,
(git_signature *)committer,
(char *)message_encoding,
(char *)message,
(git_tree *)tree,
parent_count,
(git_commit **)parents,
payload
);
return set_callback_error(error_message, ret);
}
void _go_git_populate_rebase_callbacks(git_rebase_options *opts)
{
opts->commit_create_cb = commit_create_callback;
}
void _go_git_populate_clone_callbacks(git_clone_options *opts)
{
opts->remote_cb = (git_remote_create_cb)&remoteCreateCallback;
@ -215,7 +254,7 @@ static int completion_callback(git_remote_completion_type completion_type, void
}
static int credentials_callback(
git_cred **cred,
git_credential **cred,
const char *url,
const char *username_from_url,
unsigned int allowed_types,
@ -411,6 +450,11 @@ void _go_git_writestream_free(git_writestream *stream)
stream->free(stream);
}
git_credential_t _go_git_credential_credtype(git_credential *cred)
{
return cred->credtype;
}
static int credential_ssh_sign_callback(
LIBSSH2_SESSION *session,
unsigned char **sig, size_t *sig_len,
@ -428,17 +472,12 @@ static int credential_ssh_sign_callback(
return set_callback_error(error_message, ret);
}
void _go_git_populate_credential_ssh_custom(git_cred_ssh_custom *cred)
void _go_git_populate_credential_ssh_custom(git_credential_ssh_custom *cred)
{
cred->parent.free = (void (*)(git_cred *))credentialSSHCustomFree;
cred->parent.free = (void (*)(git_credential *))credentialSSHCustomFree;
cred->sign_callback = credential_ssh_sign_callback;
}
void _go_git_credential_free(git_cred *cred)
{
cred->free(cred);
}
int _go_git_odb_write_pack(git_odb_writepack **out, git_odb *db, void *progress_payload)
{
return git_odb_write_pack(out, db, transfer_progress_callback, progress_payload);
@ -470,7 +509,10 @@ int _go_git_indexer_new(
git_odb *odb,
void *progress_cb_payload)
{
return git_indexer_new(out, path, mode, odb, transfer_progress_callback, progress_cb_payload);
git_indexer_options indexer_options = GIT_INDEXER_OPTIONS_INIT;
indexer_options.progress_cb = transfer_progress_callback;
indexer_options.progress_cb_payload = progress_cb_payload;
return git_indexer_new(out, path, mode, odb, &indexer_options);
}
static int smart_transport_callback(