Compare commits

..

111 Commits

Author SHA1 Message Date
lhchavez ef0957a40f Fix the `github-tag-action` workflow (#932)
This has been failing for a while because of some changes in `git`.

(cherry picked from commit 4b14d29c20)
2022-10-05 01:12:40 +00:00
github-actions[bot] bdf3ed46a8
rebase: Add wrapper for `git_rebase_inmemory_index()` (#900) (#903)
* 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>
(cherry picked from commit e7d1b2b69f)

Co-authored-by: Patrick Steinhardt <ps@pks.im>
2022-02-24 19:18:02 -08:00
Dylan Richardson 45c49574c9
readme: link to godoc for current main branch (#897)
This is a release-specific change corresponding to #886
2022-01-22 19:03:13 -08:00
github-actions[bot] d0ff27733f
Add ProxyOptions for push operations (#872) (#883)
Analog to #623 but for push operations rather than fetch.

(cherry picked from commit 5eca48cda9)

Co-authored-by: Aurélien <6292584+au2001@users.noreply.github.com>
2022-01-17 19:07:37 -08:00
github-actions[bot] 04b9efcea2
Add EnableFsyncGitDir to enable synchronized writes to the gitdir (#874) (#878)
This adds support for the GIT_OPT_ENABLE_FSYNC_GITDIR option in libgit2.

Co-authored-by: James Fargher <jfargher@gitlab.com>
(cherry picked from commit 1fcc9d8743)

Co-authored-by: James Fargher <proglottis@gmail.com>
2022-01-17 19:01:01 -08:00
lhchavez 79b93a62df
Generate stringer files automatically (#841) (#869)
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
(cherry picked from commit 5e35338d58)

Co-authored-by: Kirill <g4s8.public@gmail.com>
2021-11-09 06:49:20 -08:00
lhchavez 790cd6fc71
Fix replace statement example in README.md (#859) (#864)
(cherry picked from commit 533c82f270)

Co-authored-by: Ignacio Taranto <ignacio_taranto@protonmail.com>
2021-11-09 06:41:19 -08:00
github-actions[bot] 2c7a081502
Make ssh commands used in the git smart transport compatible with libgit2 (#852) (#853)
* 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>
(cherry picked from commit 6cea7a7a59)

Co-authored-by: Sunny <darkowlzz@protonmail.com>
Co-authored-by: lhchavez <lhchavez@lhchavez.com>
2021-11-09 06:26:34 -08:00
lhchavez e434188460
Add support for managed SSH transport #minor (#814) (#817)
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 18:27:52 -07:00
lhchavez 29eaaff9fd
Add support for managed HTTP/S transports (#810) (#813)
This change uses the newly-exposed Transport interface to use Go's
implementation of http.Client instead of httpclient via libgit2.

(cherry picked from commit b983e1daeb)
2021-09-05 17:01:02 -07:00
lhchavez 6fbd99e60f
Add support for custom smart transports (#806) (#809)
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.

(cherry picked from commit f1fa96c7b7)
2021-09-05 16:03:45 -07:00
lhchavez 2f3b2b5f25
Make all non-user-creatable structures non-comparable (#802) (#805)
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.

(cherry picked from commit dbe032c347)
2021-09-05 14:06:59 -07:00
lhchavez 13670e24c5
Prepare for the v1.2.0 release (#796) (#799)
This change adds a few more deprecation messages just before we remove
them.

(cherry picked from commit 2077003fa5)
2021-09-04 14:37:03 -07:00
lhchavez 0c87b5a542
Add DiffIgnoreWitespaceEol and deprecate DiffIgnoreWitespaceEol (#774) (#795)
DiffIgnoreWitespaceEol contains a typo and does not have the same name as it's libgit2 counterpart.

Fixes #773

(cherry picked from commit d4524761d9)

Co-authored-by: Gustav Westling <gustav@westling.dev>
2021-09-04 14:03:17 -07:00
github-actions[bot] d1b05aeb84
Add `CreateCommitWithSignature` (#782) (#791)
This change adds the wrapper for `git_commit_create_with_signature`.

(cherry picked from commit 15434610fe)

Co-authored-by: lhchavez <lhchavez@lhchavez.com>
2021-09-04 13:51:37 -07:00
lhchavez 0af4066df9
Rename the default branch to `main` (#786) (#788)
We've renamed the default branch from `master` to `main`, so we need to
change a bunch of references to that.

(cherry picked from commit be5a99a807)
2021-09-04 13:44:04 -07:00
lhchavez c7d968e12d
Add `Repository.CreateCommitBuffer` (#781) (#785)
This commit adds the Go binding for `git_commit_create_buffer`. This
will be used to support the 1.2.0 commit create callback.

(cherry picked from commit fbaf9d1d1a)
2021-09-04 13:29:52 -07:00
lhchavez ab793028ba
Remove the legacy builders (#776) (#780)
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.

(cherry picked from commit df7084d36a)
2021-09-04 12:57:39 -07:00
github-actions[bot] deb154820c
add wrapper for git_config_open_default (#758) (#765)
(cherry picked from commit 1e2cb92b48)

Co-authored-by: Vladimir Buzuev <44682889+vladimir-buzuev@users.noreply.github.com>
2021-04-04 08:29:27 -07:00
github-actions[bot] efed131f1f
fix buldled static build on Windows/MinGW (#761) (#763)
seems like need more libraries in LDFLAGS:

* ws2_32 for socket, connect, htonl, etc
* ole32 for CoInitializeEx
* rpcrt4 for UuidCreate
* crypt32 for CertFreeCertificateContext

(cherry picked from commit 0d7c8dadb4)

Co-authored-by: Vladimir Buzuev <44682889+vladimir-buzuev@users.noreply.github.com>
2021-04-04 08:28:49 -07:00
lhchavez bcfd445806
Git repository item path (#757) (#768)
add wrapper for `git_repository_item_path`

(cherry picked from commit a4d202ed7b)

Co-authored-by: Vladimir Buzuev <44682889+vladimir-buzuev@users.noreply.github.com>
2021-04-04 08:28:09 -07:00
github-actions[bot] 275690e61a
Implement git_repository_set_config (#735) (#743)
Closes #732

(cherry picked from commit 2fd0495c43)

Co-authored-by: Byoungchan Lee <daniel.l@hpcnt.com>
2021-03-07 17:55:17 -08:00
github-actions[bot] c0b9936476
Make index time fields public (#750) (#751)
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

(cherry picked from commit aeb22bcf7d)

Co-authored-by: michael boulton <61595820+mbfr@users.noreply.github.com>
2021-02-15 13:57:57 -08:00
github-actions[bot] 6f19ef8843
fix: Use `err` instead of error as a variable name for errors (#746) (#749)
fix #745

(cherry picked from commit f6c5753df8)

Co-authored-by: Suhaib Mujahid <suhaibmujahid@gmail.com>
2021-02-15 13:57:38 -08:00
github-actions[bot] bbb77f1bdf
Support git_repository_message, git_repository_message_remove (#734) (#737)
Closes #646

(cherry picked from commit 07147a8ea8)

Co-authored-by: Byoungchan Lee <thisisbclee@gmail.com>
2021-02-03 06:29:14 -08:00
github-actions[bot] 8c043d028e
Rename the build files (#724) (#725)
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
(cherry picked from commit 4b2ac7c998)

Co-authored-by: lhchavez <lhchavez@lhchavez.com>
2020-12-13 15:47:18 -08:00
github-actions[bot] fb161a6575
Support more MergeBase functions (#720) (#723)
This change adds support for MergeBaseMany, MergeBasesMany, and
MergeBaseOctopus.

(cherry picked from commit 698ddfb4ac)

Co-authored-by: lhchavez <lhchavez@lhchavez.com>
2020-12-13 11:08:53 -08:00
lhchavez 05f743a30f
More callback refactoring (#713) (#719)
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.

(cherry picked from commit 10c67474a8)
2020-12-13 11:08:38 -08:00
github-actions[bot] b9770ebde6
Ensure that no pointer handles leak during the test (#712) (#715)
This change makes sure that pointer handles are correctly cleaned up
during tests.

(cherry picked from commit e28cce87c7)

Co-authored-by: lhchavez <lhchavez@lhchavez.com>
2020-12-10 06:52:11 -08:00
lhchavez 5e09ac76e8 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.

(cherry picked from commit abf02bc7d7)
2020-12-06 13:10:00 -08:00
lhchavez 57d9083efe Build improvements (#707)
This change makes the test be verbose and use parallelization if
possible (when using gmake to build).

(cherry picked from commit 54afccfa0f)
2020-12-06 13:10:00 -08:00
lhchavez 37d110dbf0
Refactor all callbacks (#700) (#705)
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`.

(cherry picked from commit 5d8eaf7e65)
2020-12-05 17:10:18 -08:00
lhchavez 5f0640baed
Mark some symbols to be deprecated #minor (#698) (#702)
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
(cherry picked from commit 137c05e802)
2020-12-05 13:12:04 -08:00
github-actions[bot] 3159d21503
Add ReferenceNormalizeName (#681) (#688)
(cherry picked from commit 2bd574b6bd)

Co-authored-by: Segev Finer <segev@codeocean.com>
2020-11-13 19:01:23 -08:00
github-actions[bot] 81c9eb4670
Travis-ci: added support for ppc64le (#682) (#684)
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.

(cherry picked from commit 2d639d8e49)

Co-authored-by: Devendra <devendranath.thadi3@gmail.com>
2020-11-13 06:31:46 -08:00
github-actions[bot] 800edc61bf
feat: Implement an option to control hash verification (#671) (#673)
Add a binding to enable/disable hash verification using the `GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION` option.

Change type: #minor

(cherry picked from commit c3664193f3)

Co-authored-by: Suhaib Mujahid <suhaibmujahid@gmail.com>
2020-11-02 18:44:37 -08:00
lhchavez 7b9a768b08
CI refresh (#666) (#667)
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.

(cherry picked from commit f83530b18d)
2020-10-26 17:56:51 -07:00
github-actions[bot] f58d71b8a9
repository: Implement wrappers for `git_object_lookup_prefix` (#658) (#659)
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.

(cherry picked from commit 37b81b61f1)

Co-authored-by: Patrick Steinhardt <ps@pks.im>
2020-10-22 06:05:23 -07:00
github-actions[bot] 27f87bd821
feat: Enable change the system install path (#653) (#654)
#### 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`.

(cherry picked from commit 111185838c)

Co-authored-by: Suhaib Mujahid <suhaibmujahid@gmail.com>
2020-09-30 17:31:20 -07:00
github-actions[bot] 6cb9c7cf41
Enable set the VENDORED_PATH for libgit2 (#650) (#652)
### 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
```

(cherry picked from commit f3a746d7b6)

Co-authored-by: Suhaib Mujahid <suhaibmujahid@gmail.com>
2020-09-30 17:16:28 -07:00
github-actions[bot] a81a08606f
Add a ReInit function (#647) (#649)
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.

(cherry picked from commit 3c5c580d78)

Co-authored-by: Jesse Hathaway <jesse@mbuki-mvuki.org>
2020-09-29 15:51:32 -07:00
github-actions[bot] 627f58d403
merge: Expose recursion limit merge option (#642) (#644)
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.

(cherry picked from commit 7e726fda6e)

Co-authored-by: Patrick Steinhardt <ps@pks.im>
2020-09-18 06:23:21 -07:00
lhchavez 3d80bd22ad
Add support for creating signed commits (#626) (#641)
(cherry picked from commit 7d4453198b)

This is only a partial cherry-pick, since signing commits during a
rebase is not supported in v0.27.
2020-08-18 09:52:46 -07:00
lhchavez 9912ed9742
Revert "More diff functionality (#629) (#636)" (#639)
This reverts commit 4d69027787.

v0.27 does not support this API.
2020-08-18 06:52:38 -07:00
github-actions[bot] 4d69027787
More diff functionality (#629) (#636)
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

(cherry picked from commit 7883ec85de)
2020-08-18 06:21:19 -07:00
github-actions[bot] 0430fd700c
Add two more GitHub Actions workflows (#633) (#635)
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.

(cherry picked from commit 2ac9f4e69b)
2020-08-16 07:28:35 -07:00
lhchavez 6453cf9f8a 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.

(cherry picked from commit 5314951759)
2020-08-16 07:19:09 -07:00
michael boulton ff0fed0c32 Fix null pointer dereference in status.ByIndex (#628)
`git_status_byindex` can return a null pointer if there is no statuses.

(cherry picked from commit fc6eaf3638)
2020-08-16 07:19:09 -07:00
Yuichi Watanabe 7a89ced8fc 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
(cherry picked from commit 462ebd83e0)
2020-08-16 07:19:09 -07:00
Jesse Hathaway 5c0c2356cc 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.

(cherry picked from commit b1cad11555)
2020-08-16 07:19:09 -07:00
lhchavez 26287a926a 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.

(cherry picked from commit 20a55cdf92)
2020-08-16 07:19:09 -07:00
lhchavez 14e29a3390 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.

(cherry picked from commit c78ae57de6)
2020-08-16 07:19:09 -07:00
lhchavez 2635b45d90 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.

(cherry picked from commit 619a9c236b)
2020-08-16 07:19:09 -07:00
lhchavez 1233fdebf5 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
(cherry picked from commit 9eaf4fed5f)
2020-08-16 07:19:09 -07:00
Takuji Shimokawa aea6cff7ac Provide missing merge flags (#615)
This change adds two missing merge flags MergeTreeSkipREUC and MergeTreeNoRecursive.

(cherry picked from commit 33dac3d460)
2020-08-16 07:19:09 -07:00
Jesse Hathaway 250a674787 Add support for parsing git trailers (#614)
Adds a wrapper for git_message_trailers which returns a slice of trailer
structs.

(cherry picked from commit 5241c72e6e)
2020-08-16 07:19:09 -07:00
lhchavez d6ab8729d8 Merge pull request #582 from suhaibmujahid/method-rename
It is not Go idiomatic to put Get into the getter's name

(cherry picked from commit 31f877e249)
2020-08-16 07:19:09 -07:00
lhchavez 8334650b1c expose options related to caching
(cherry picked from commit 8b51d0db8e)
2020-08-16 07:19:09 -07:00
Suhaib Mujahid 8e87fa7d72 Check nil signature
(cherry picked from commit 91d08450b6)
2020-08-16 07:19:09 -07:00
lhchavez 447d43c57d Fix SIGSEGV on double free for Cred object
This change removes the Go finalizer when passing ownership to libgit2.

Fixes: #553
(cherry picked from commit 0843b826d2)
2020-08-16 07:19:09 -07:00
Suhaib Mujahid 0dfadb0dd0 Update README.md
(cherry picked from commit 3a2102638d)
2020-08-16 07:19:09 -07:00
lhchavez 59a177dd02 Update README.md
Clarifying the versions since we're using Go 1.11 module version rules now.

(cherry picked from commit 2b66c0f9e7)
2020-08-16 07:19:09 -07:00
lhchavez 891d857c3a Add the version number to go.mod
This is the second take on trying to tag the current release with a Go
version.

(cherry picked from commit a32375a860)
2020-08-16 07:19:09 -07:00
lhchavez 231f89164b Merge pull request #542 from slyphon/fix-error-name
Resolves issue #541 - typo in error code 'ErrAmbigious'

(cherry picked from commit 30de4b2e26)
2020-08-16 07:19:09 -07:00
lhchavez 26397cdaea 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
(cherry picked from commit 93c4c5b30a)
2020-08-16 07:19:09 -07:00
lhchavez 395b2a90a4 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.

(cherry picked from commit 91946a5705)
2020-08-16 07:19:09 -07:00
lhchavez 5acb58e83d Add support for indexers and alternate odb packfiles
This allows for implementations of git servers written in Go.

(cherry picked from commit 05bc5e36ff)
2020-08-16 07:19:09 -07:00
lhchavez 36b0f8ba75 Merge pull request #429 from josharian/cherrypick-commit
cherrypick: wrap git_cherrypick_commit
(cherry picked from commit 45097a857c)
2020-08-16 07:19:09 -07:00
lhchavez 75daa3227d Merge pull request #423 from josharian/more-annotated-commit
merge: add two missing AnnotatedCommit methods
(cherry picked from commit 21d618136f)
2020-08-16 07:19:09 -07:00
lhchavez 23e13acf73 Merge pull request #400 from ramanenka/git_index_add_frombuffer
Add binding for `git_index_add_frombuffer`

(cherry picked from commit 06764f48dc)
2020-08-16 07:19:09 -07:00
Richard Burke 5acdcfaf86 Remove Version from RevertOptions
Version is defaulted to GIT_REVERT_OPTIONS_VERSION

(cherry picked from commit 4bca045e5a)
2020-08-16 07:19:09 -07:00
Richard Burke 0b5abad959 Add revert functionality
Closes #436

(cherry picked from commit 30c3d0ffe2)
2020-08-16 07:19:09 -07:00
lhchavez 37230d2697 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.

(cherry picked from commit 2bb5930733)
2020-08-16 07:19:09 -07:00
lhchavez b6aa16143b 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.

(cherry picked from commit 26edffd5f5)
2020-08-16 07:19:09 -07:00
lhchavez 15641667bd Merge pull request #520 from libgit2/actions
Setup CI via Actions

(cherry picked from commit f21ecd9e74)
2020-08-16 07:19:09 -07:00
lhchavez 5bfa93a8dd Merge pull request #523 from josharian/diff-stringers
make Delta and DiffLineType stringers

(cherry picked from commit 11506ab070)
2020-08-16 07:19:09 -07:00
lhchavez 2ce0cec363 Merge pull request #524 from josharian/doc-params
provide param names in DiffForEachFileCallback

(cherry picked from commit 917d8dcb9e)
2020-08-16 07:19:09 -07:00
lhchavez 61fdd76c76 Merge pull request #503 from jonEbird/static-build-script-cleanup
script/build-libgit2-static.sh: correctly set ROOT

(cherry picked from commit aa802a90db)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto e888805c2b Merge pull request #506 from takuji/git_commit_message_encoding
Add git_commit_message_encoding support

(cherry picked from commit b2e2b2f71b)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto bee1068871 Merge pull request #512 from codeocean/diff-to-buf
Add Diff.ToBuf wrapping git_diff_to_buf

(cherry picked from commit 4fa9349942)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto 544af2b39a Merge pull request #448 ftrom lhchavez/mempack
Add support for mempack

(cherry picked from commit 2f91268f74)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto c1c2b5a730 Merge pull request #466 from lhchavez/repository-create_commit_from_ids
Add support for CreateCommitFromIds

(cherry picked from commit 8766f9f36c)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto 9fd0d987ae Merge pull request #477 from lhchavez/patch-1
Add support for Go 1.11 modules

(cherry picked from commit 2609f4c6f2)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto a13d27e9c0 Merge pull request #475 from lhchavez/self-contained-build
Improve the static build script

(cherry picked from commit b30b050c9c)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto fedb5b8e68 Merge pull request #476 from lhchavez/clean-up-leaked-dir
Clean up one leaked temporary directory

(cherry picked from commit e93f34cf18)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto 7b1c424572 Merge pull request #420 from josharian/rebase-operation-type-stringer
Add RebaseOperationReword, and make RebaseOperationType a stringer

(cherry picked from commit 7ae106611c)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto a07879739f Merge pull request #445 from rmg/exclusive-pkg-config
static: use pkg-config exclusively when using it
(cherry picked from commit bcf325244c)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto d314b459b8 Merge pull request #465 from lhchavez/packbuilder-insert_from_walk
Add support for Packbuilder.InsertFromWalk()

(cherry picked from commit b51a90c133)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto eb38aaaeee Merge pull request #463 from Nivl/patch-1
Add index.Clear() to clear the index object

(cherry picked from commit c27981c283)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto 97d05a1e4c Merge pull request #432 from josharian/simplify-oid
git: simplify some Oid methods
(cherry picked from commit c740e1d83d)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto 13d16e7ac5 Merge pull request #447 from walkenzoy/master
git2go: small fixes to odb module
(cherry picked from commit fc1230ba16)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto 10ce505588 Merge pull request #425 from josharian/more-merge-file-flags
merge: add missing MergeFileFlag constants
(cherry picked from commit e319b9427f)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto da10fee49e Merge pull request #424 from josharian/sigdoc
signature: improve Signature.Offset docs
(cherry picked from commit 7197faee79)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto c74ce46055 Merge pull request #470 from lhchavez/fix-odbreadstream-read
Return io.EOF on OdbReadStream.Read()

(cherry picked from commit 7e9128bd58)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto 13d27a4f62 Merge pull request #468 from wmedlar/patch-1
Fix typo in constant name

(cherry picked from commit 8b368063e9)
2020-08-16 07:19:09 -07:00
Carlos Martín Nieto ca9f5b6523 Merge pull request #443 from walkenzoy/master
git2go: fix reference iterator leak
(cherry picked from commit 14280de4da)
2020-08-16 07:19:09 -07:00
lhchavez 6cc7d3dc6a
Merge pull request #526 from dbolkensteyn/v27
Fixes #513 - Segfault during tree walk
2020-02-16 07:31:32 -08:00
Carlos Martín Nieto 6ee3a5f589
Merge pull request #529 from libgit2/cmn/bump-libgit2-27
Update libgit2 to v0.27.10
2019-12-10 22:41:57 +01:00
Carlos Martín Nieto f0ad5b44b9 Update libgit2 to v0.27.10 2019-12-10 21:36:08 +00:00
Dinesh Bolkensteyn 53ee1f6e9a Similar to #513 Fix potential segfault on Tag objects 2019-11-17 17:41:56 +01:00
Dinesh Bolkensteyn 75a20e5aeb Fixes #513 - Segfault during tree walk 2019-11-17 09:19:36 +01:00
Carlos Martín Nieto 94786f9c8f
Merge pull request #518 from libgit2/cmn/update-27
Update vendored libgit2 to v0.27.9
2019-08-13 19:36:01 +02:00
Carlos Martín Nieto 43550d0b7f Update vendored libgit2 to v0.27.9 2019-08-13 19:24:57 +02:00
Carlos Martín Nieto ecaeb7a21d
Merge pull request #472 from libgit2/cmn/bump-v27
Update vendored libgit2 to v0.27.7
2019-01-04 13:40:18 +00:00
Carlos Martín Nieto e209475ae0 Update vendored libgit2 to v0.27.7 2019-01-04 13:25:56 +00:00
Carlos Martín Nieto 290d16a225
Merge pull request #459 from libgit2/cmn/bump-v27
Update vendored libgit2 to v0.27.5
2018-10-07 18:59:15 +02:00
Carlos Martín Nieto bde7731d51 Update vendored libgit2 to v0.27.5 2018-10-07 18:48:41 +02:00
Carlos Martín Nieto c0c29489d9
Merge pull request #450 from libgit2/cmn/bump-libgit2
Bump vendored libgit2 to v0.27.4
2018-08-08 11:35:01 +02:00
Carlos Martín Nieto 7696f18fed Bump vendored libgit2 to v0.27.4 2018-08-08 11:04:32 +02:00
Carlos Martín Nieto 9abc0506da
Merge pull request #441 from libgit2/cmn/update-v27-1
vendor: update libgit2 to v0.27.1
2018-05-29 20:57:54 +02:00
Carlos Martín Nieto ef79809066 vendor: update libgit2 to v0.27.1 2018-05-29 20:21:28 +02:00
68 changed files with 742 additions and 2278 deletions

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

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

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

@ -0,0 +1,127 @@
name: git2go CI
on:
pull_request:
push:
branches:
- main
- release-*
- v*
jobs:
build-static:
strategy:
fail-fast: false
matrix:
go: [ '1.11', '1.12', '1.13', '1.14', '1.15', '1.16', '1.17' ]
name: Go ${{ matrix.go }}
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go }}
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
make build-libgit2-static
- name: Test
run: make TEST_ARGS=-test.v test-static
build-dynamic:
strategy:
fail-fast: false
name: Go (dynamic)
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.17'
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
make build-libgit2-dynamic
- name: Test
run: make TEST_ARGS=-test.v test-dynamic
build-system-dynamic:
strategy:
fail-fast: false
name: Go (system-wide, dynamic)
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.17'
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build libgit2
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
sudo ./script/build-libgit2.sh --dynamic --system
- name: Test
run: make TEST_ARGS=-test.v test
build-system-static:
strategy:
fail-fast: false
name: Go (system-wide, static)
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.17'
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Build libgit2
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
sudo ./script/build-libgit2.sh --static --system
- name: Test
run: go test --count=1 --tags "static,system_libgit2" ./...
check-generate:
name: Check generated files were not modified
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.17'
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Install libgit2 build dependencies
run: |
git submodule update --init
sudo apt-get install -y --no-install-recommends libssh2-1-dev
go install golang.org/x/tools/cmd/stringer@latest
- name: Generate files
run: |
export PATH=$(go env GOPATH)/bin:$PATH
make generate
- name: Check nothing changed
run: git diff --quiet --exit-code || (echo "detected changes after generate" ; git status ; exit 1)

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

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

2
.gitignore vendored
View File

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

3
.gitmodules vendored Normal file
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,48 +1,86 @@
GO libgit2 git2go
====== ======
[![GoDoc](https://godoc.org/go.wit.com/lib/libgit2?status.svg)](http://godoc.org/go.wit.com/lib/libgit2) [![Build Status](https://travis-ci.org/libgit2/libgit2.svg?branch=main)](https://travis-ci.org/libgit2/libgit2) [![GoDoc](https://godoc.org/github.com/libgit2/git2go?status.svg)](http://godoc.org/github.com/libgit2/git2go/v27) [![Build Status](https://travis-ci.org/libgit2/git2go.svg?branch=main)](https://travis-ci.org/libgit2/git2go)
Go bindings for [libgit2](http://libgit2.github.com/). Go bindings for [libgit2](http://libgit2.github.com/).
### Updated 2024/12/16
### Which Go version to use ### Which Go version to use
* This package is updated to work against libgit2 version 1.8 on Debian sid Due to the fact that Go 1.11 module versions have semantic meaning and don't necessarily align with libgit2's release schedule, please consult the following table for a mapping between libgit2 and git2go module versions:
* There is one line commented out which needs to be fixed in remote.go
* some of the tests seem to run | libgit2 | git2go |
|---------|---------------|
| 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
```sh ```sh
go install go.wit.com/apps/go-clone@latest go get github.com/libgit2/git2go/v27
go install go.wit.com/apps/go-mod-clean@latest ```
go-clone --recusive go.wit.com/lib/libgit2 ```go
import "github.com/libgit2/git2go/v27"
``` ```
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 ### Which branch to send Pull requests to
TODO: not sure yet If there's something version-specific that you'd want to contribute to, you can send them to the `release-${MAJOR}.${MINOR}` branches, which follow libgit2's releases.
Installing Installing
---------- ----------
This project wraps the functionality provided by libgit2. It thus needs it in order to perform the work. This project wraps the functionality provided by libgit2. It thus needs it in order to perform the work.
This project wraps the functionality provided by libgit2. If you're using a versioned branch, install it to your system via your system's package manager and then install libgit2. This project wraps the functionality provided by libgit2. If you're using a versioned branch, install it to your system via your system's package manager and then install git2go.
### Versioned branch, dynamic linking ### Versioned branch, dynamic linking
When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via Go modules, e.g. to work against libgit2 v1.2 When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via Go modules, e.g. to work against libgit2 v0.27
```go ```go
goimports -w *.go import "github.com/libgit2/git2go/v27"
``` ```
### 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 Parallelism and network operations
---------------------------------- ----------------------------------
libgit2 may use OpenSSL and LibSSH2 for performing encrypted network connections. For now, libgit2 asks libgit2 to set locking for OpenSSL. This makes HTTPS connections thread-safe, but it is fragile and will likely stop doing it soon. This may also make SSH connections thread-safe if your copy of libssh2 is linked against OpenSSL. Check libgit2's `THREADSAFE.md` for more information. libgit2 may use OpenSSL and LibSSH2 for performing encrypted network connections. For now, git2go asks libgit2 to set locking for OpenSSL. This makes HTTPS connections thread-safe, but it is fragile and will likely stop doing it soon. This may also make SSH connections thread-safe if your copy of libssh2 is linked against OpenSSL. Check libgit2's `THREADSAFE.md` for more information.
Running the tests Running the tests
----------------- -----------------
@ -64,6 +102,6 @@ M to the I to the T. See the LICENSE file if you've never seen an MIT license be
Authors Authors
------- -------
- Carlos Martín (github@carlosmn) - Carlos Martín (@carlosmn)
- Vicent Martí (github@vmg) - Vicent Martí (@vmg)

View File

@ -23,7 +23,7 @@ func DefaultBlameOptions() (BlameOptions, error) {
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
opts := C.git_blame_options{} opts := C.git_blame_options{}
ecode := C.git_blame_options_init(&opts, C.GIT_BLAME_OPTIONS_VERSION) ecode := C.git_blame_init_options(&opts, C.GIT_BLAME_OPTIONS_VERSION)
if ecode < 0 { if ecode < 0 {
return BlameOptions{}, MakeGitError(ecode) return BlameOptions{}, MakeGitError(ecode)
} }
@ -47,8 +47,6 @@ const (
BlameTrackCopiesSameCommitCopies BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES BlameTrackCopiesSameCommitCopies BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES
BlameTrackCopiesAnyCommitCopies BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES BlameTrackCopiesAnyCommitCopies BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES
BlameFirstParent BlameOptionsFlag = C.GIT_BLAME_FIRST_PARENT 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) { 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) size = C.size_t(0)
} }
ecode := C.git_blob_create_from_buffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size) ecode := C.git_blob_create_frombuffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size)
runtime.KeepAlive(repo) runtime.KeepAlive(repo)
if ecode < 0 { if ecode < 0 {
return nil, MakeGitError(ecode) return nil, MakeGitError(ecode)
@ -88,7 +88,7 @@ func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, err
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ecode := C.git_blob_create_from_stream(&stream, repo.ptr, chintPath) ecode := C.git_blob_create_fromstream(&stream, repo.ptr, chintPath)
if ecode < 0 { if ecode < 0 {
return nil, MakeGitError(ecode) return nil, MakeGitError(ecode)
} }
@ -141,7 +141,7 @@ func (stream *BlobWriteStream) Commit() (*Oid, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ecode := C.git_blob_create_from_stream_commit(&oid, stream.ptr) ecode := C.git_blob_create_fromstream_commit(&oid, stream.ptr)
runtime.KeepAlive(stream) runtime.KeepAlive(stream)
if ecode < 0 { if ecode < 0 {
return nil, MakeGitError(ecode) return nil, MakeGitError(ecode)

View File

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

View File

@ -7,6 +7,7 @@ extern void _go_git_populate_checkout_callbacks(git_checkout_options *opts);
*/ */
import "C" import "C"
import ( import (
"errors"
"os" "os"
"runtime" "runtime"
"unsafe" "unsafe"
@ -35,6 +36,7 @@ const (
CheckoutDontUpdateIndex CheckoutStrategy = C.GIT_CHECKOUT_DONT_UPDATE_INDEX // Normally checkout updates index entries as it goes; this stops that 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 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 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 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 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 CheckoutDisablePathspecMatch CheckoutStrategy = C.GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH // Treat pathspec as simple list of exact match file paths
@ -48,8 +50,8 @@ const (
CheckoutUpdateSubmodulesIfChanged CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED // Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED) CheckoutUpdateSubmodulesIfChanged CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED // Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED)
) )
type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) error type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) ErrorCode
type CheckoutProgressCallback func(path string, completed, total uint) type CheckoutProgressCallback func(path string, completed, total uint) ErrorCode
type CheckoutOptions struct { type CheckoutOptions struct {
Strategy CheckoutStrategy // Default will be a dry run Strategy CheckoutStrategy // Default will be a dry run
@ -115,9 +117,9 @@ func checkoutNotifyCallback(
if data.options.NotifyCallback == nil { if data.options.NotifyCallback == nil {
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
} }
err := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir) ret := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir)
if err != nil { if ret < 0 {
*data.errorTarget = err *data.errorTarget = errors.New(ErrorCode(ret).String())
return C.int(ErrorCodeUser) return C.int(ErrorCodeUser)
} }
return C.int(ErrorCodeOK) return C.int(ErrorCodeOK)
@ -140,7 +142,7 @@ func checkoutProgressCallback(
// the provided CheckoutOptions struct. Returns copts, or nil if opts is nil, // the provided CheckoutOptions struct. Returns copts, or nil if opts is nil,
// in order to help with what to pass. // in order to help with what to pass.
func populateCheckoutOptions(copts *C.git_checkout_options, opts *CheckoutOptions, errorTarget *error) *C.git_checkout_options { func populateCheckoutOptions(copts *C.git_checkout_options, opts *CheckoutOptions, errorTarget *error) *C.git_checkout_options {
C.git_checkout_options_init(copts, C.GIT_CHECKOUT_OPTIONS_VERSION) C.git_checkout_init_options(copts, C.GIT_CHECKOUT_OPTIONS_VERSION)
if opts == nil { if opts == nil {
return nil return nil
} }

View File

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

View File

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

View File

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

View File

@ -37,14 +37,10 @@ func (c *Commit) Message() string {
return ret return ret
} }
func (c *Commit) MessageEncoding() MessageEncoding { func (c *Commit) MessageEncoding() string {
ptr := C.git_commit_message_encoding(c.cast_ptr) ret := C.GoString(C.git_commit_message_encoding(c.cast_ptr))
if ptr == nil {
return MessageEncodingUTF8
}
ret := C.GoString(ptr)
runtime.KeepAlive(c) runtime.KeepAlive(c)
return MessageEncoding(ret) return ret
} }
func (c *Commit) RawMessage() string { func (c *Commit) RawMessage() string {
@ -68,19 +64,6 @@ func (c *Commit) ContentToSign() string {
// CommitSigningCallback defines a function type that takes some data to sign and returns (signature, signature_field, error) // CommitSigningCallback defines a function type that takes some data to sign and returns (signature, signature_field, error)
type CommitSigningCallback func(string) (signature, signatureField string, err error) type CommitSigningCallback func(string) (signature, signatureField string, err error)
// CommitCreateCallback defines a function type that is called when another
// function is going to create commits (for example, Rebase) to allow callers
// to override the commit creation behavior. For example, users may wish to
// sign commits by providing this information to Repository.CreateCommitBuffer,
// signing that buffer, then calling Repository.CreateCommitWithSignature.
type CommitCreateCallback func(
author, committer *Signature,
messageEncoding MessageEncoding,
message string,
tree *Tree,
parents ...*Commit,
) (oid *Oid, err error)
// WithSignatureUsing creates a new signed commit from this one using the given signing callback // WithSignatureUsing creates a new signed commit from this one using the given signing callback
func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) { func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
signature, signatureField, err := f(c.ContentToSign()) signature, signatureField, err := f(c.ContentToSign())
@ -103,10 +86,10 @@ func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, e
func (c *Commit) ExtractSignature() (string, string, error) { func (c *Commit) ExtractSignature() (string, string, error) {
var c_signed C.git_buf var c_signed C.git_buf
defer C.git_buf_dispose(&c_signed) defer C.git_buf_free(&c_signed)
var c_signature C.git_buf var c_signature C.git_buf
defer C.git_buf_dispose(&c_signature) defer C.git_buf_free(&c_signature)
oid := c.Id() oid := c.Id()
repo := C.git_commit_owner(c.cast_ptr) 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 { if ret < 0 {
return "", MakeGitError(ret) return "", MakeGitError(ret)
} }
defer C.git_buf_dispose(&valBuf) defer C.git_buf_free(&valBuf)
return C.GoString(valBuf.ptr), nil 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 // OpenOndisk creates a new config instance containing a single on-disk file
func OpenOndisk(path string) (*Config, error) { func OpenOndisk(parent *Config, path string) (*Config, error) {
cpath := C.CString(path) cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath)) defer C.free(unsafe.Pointer(cpath))
@ -392,7 +392,7 @@ func (iter *ConfigIterator) Free() {
func ConfigFindGlobal() (string, error) { func ConfigFindGlobal() (string, error) {
var buf C.git_buf var buf C.git_buf
defer C.git_buf_dispose(&buf) defer C.git_buf_free(&buf)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -407,7 +407,7 @@ func ConfigFindGlobal() (string, error) {
func ConfigFindSystem() (string, error) { func ConfigFindSystem() (string, error) {
var buf C.git_buf var buf C.git_buf
defer C.git_buf_dispose(&buf) defer C.git_buf_free(&buf)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -422,7 +422,7 @@ func ConfigFindSystem() (string, error) {
func ConfigFindXDG() (string, error) { func ConfigFindXDG() (string, error) {
var buf C.git_buf var buf C.git_buf
defer C.git_buf_dispose(&buf) defer C.git_buf_free(&buf)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -440,7 +440,7 @@ func ConfigFindXDG() (string, error) {
// Look for the file in %PROGRAMDATA%\Git\config used by portable git. // Look for the file in %PROGRAMDATA%\Git\config used by portable git.
func ConfigFindProgramdata() (string, error) { func ConfigFindProgramdata() (string, error) {
var buf C.git_buf var buf C.git_buf
defer C.git_buf_dispose(&buf) defer C.git_buf_free(&buf)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()

View File

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

View File

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

View File

@ -27,51 +27,6 @@ type BlobCallbackData struct {
// Deprecated: CheckoutOpts is a deprecated alias of CheckoutOptions. // Deprecated: CheckoutOpts is a deprecated alias of CheckoutOptions.
type CheckoutOpts = 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 // diff.go
const ( const (
@ -205,8 +160,6 @@ const (
ErrPassthrough = ErrorCodePassthrough ErrPassthrough = ErrorCodePassthrough
// Deprecated: ErrIterOver is a deprecated alias of ErrorCodeIterOver. // Deprecated: ErrIterOver is a deprecated alias of ErrorCodeIterOver.
ErrIterOver = ErrorCodeIterOver ErrIterOver = ErrorCodeIterOver
// Deprecated: ErrApplyFail is a deprecated alias of ErrorCodeApplyFail.
ErrApplyFail = ErrorCodeApplyFail
) )
// index.go // index.go
@ -230,27 +183,7 @@ func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer)
if !ok { if !ok {
panic("invalid submodule visitor callback") panic("invalid submodule visitor callback")
} }
err := callback(sub, C.GoString(name)) return (C.int)(callback(sub, C.GoString(name)))
if err != nil {
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
// reference.go
// Deprecated: ReferenceIsValidName is a deprecated alias of ReferenceNameIsValid.
func ReferenceIsValidName(name string) bool {
valid, _ := ReferenceNameIsValid(name)
return valid
}
// remote.go
// Deprecated: RemoteIsValidName is a deprecated alias of RemoteNameIsValid.
func RemoteIsValidName(name string) bool {
valid, _ := RemoteNameIsValid(name)
return valid
} }
// tree.go // tree.go
@ -259,13 +192,9 @@ func RemoteIsValidName(name string) bool {
func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int { func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int {
root := C.GoString(_root) root := C.GoString(_root)
callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback) if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok {
if !ok { return C.int(callback(root, newTreeEntry(entry)))
} else {
panic("invalid treewalk callback") panic("invalid treewalk callback")
} }
err := callback(root, newTreeEntry(entry))
if err != nil {
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
} }

View File

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

198
diff.go
View File

@ -3,7 +3,6 @@ package git
/* /*
#include <git2.h> #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 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 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); 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);
@ -265,7 +264,7 @@ const (
func (stats *DiffStats) String(format DiffStatsFormat, func (stats *DiffStats) String(format DiffStatsFormat,
width uint) (string, error) { width uint) (string, error) {
buf := C.git_buf{} buf := C.git_buf{}
defer C.git_buf_dispose(&buf) defer C.git_buf_free(&buf)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -458,7 +457,7 @@ func (diff *Diff) ToBuf(format DiffFormat) ([]byte, error) {
if ecode < 0 { if ecode < 0 {
return nil, MakeGitError(ecode) return nil, MakeGitError(ecode)
} }
defer C.git_buf_dispose(&diffBuf) defer C.git_buf_free(&diffBuf)
return C.GoBytes(unsafe.Pointer(diffBuf.ptr), C.int(diffBuf.size)), nil return C.GoBytes(unsafe.Pointer(diffBuf.ptr), C.int(diffBuf.size)), nil
} }
@ -523,7 +522,7 @@ func DefaultDiffOptions() (DiffOptions, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ecode := C.git_diff_options_init(&opts, C.GIT_DIFF_OPTIONS_VERSION) ecode := C.git_diff_init_options(&opts, C.GIT_DIFF_OPTIONS_VERSION)
if ecode < 0 { if ecode < 0 {
return DiffOptions{}, MakeGitError(ecode) return DiffOptions{}, MakeGitError(ecode)
} }
@ -562,7 +561,7 @@ const (
DiffFindRemoveUnmodified DiffFindOptionsFlag = C.GIT_DIFF_FIND_REMOVE_UNMODIFIED DiffFindRemoveUnmodified DiffFindOptionsFlag = C.GIT_DIFF_FIND_REMOVE_UNMODIFIED
) )
// TODO implement git_diff_similarity_metric //TODO implement git_diff_similarity_metric
type DiffFindOptions struct { type DiffFindOptions struct {
Flags DiffFindOptionsFlag Flags DiffFindOptionsFlag
RenameThreshold uint16 RenameThreshold uint16
@ -578,7 +577,7 @@ func DefaultDiffFindOptions() (DiffFindOptions, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ecode := C.git_diff_find_options_init(&opts, C.GIT_DIFF_FIND_OPTIONS_VERSION) ecode := C.git_diff_find_init_options(&opts, C.GIT_DIFF_FIND_OPTIONS_VERSION)
if ecode < 0 { if ecode < 0 {
return DiffFindOptions{}, MakeGitError(ecode) return DiffFindOptions{}, MakeGitError(ecode)
} }
@ -644,7 +643,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 { func populateDiffOptions(copts *C.git_diff_options, opts *DiffOptions, repo *Repository, errorTarget *error) *C.git_diff_options {
C.git_diff_options_init(copts, C.GIT_DIFF_OPTIONS_VERSION) C.git_diff_init_options(copts, C.GIT_DIFF_OPTIONS_VERSION)
if opts == nil { if opts == nil {
return nil return nil
} }
@ -888,191 +887,6 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string,
return nil 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. // 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 // The diff object produced is similar to the one that would be produced if you

View File

@ -2,10 +2,6 @@ package git
import ( import (
"errors" "errors"
"fmt"
"io/ioutil"
"path"
"reflect"
"strings" "strings"
"testing" "testing"
) )
@ -239,447 +235,3 @@ func TestDiffBlobs(t *testing.T) {
t.Fatalf("Bad number of lines iterated") 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,26 +36,24 @@ func _() {
_ = x[ErrorCodeIterOver - -31] _ = x[ErrorCodeIterOver - -31]
_ = x[ErrorCodeRetry - -32] _ = x[ErrorCodeRetry - -32]
_ = x[ErrorCodeMismatch - -33] _ = x[ErrorCodeMismatch - -33]
_ = x[ErrorCodeIndexDirty - -34]
_ = x[ErrorCodeApplyFail - -35]
} }
const ( const (
_ErrorCode_name_0 = "ApplyFailIndexDirtyMismatchRetryIterOverPassthrough" _ErrorCode_name_0 = "MismatchRetryIterOverPassthrough"
_ErrorCode_name_1 = "MergeConflictDirectoryUncommittedInvalidEOFPeelAppliedCertificateAuthModifiedLockedConflictInvalidSpecNonFastForwardUnmergedUnbornBranchBareRepoUserBuffsAmbiguousExistsNotFound" _ErrorCode_name_1 = "MergeConflictDirectoryUncommittedInvalidEOFPeelAppliedCertificateAuthModifiedLockedConflictInvalidSpecNonFastForwardUnmergedUnbornBranchBareRepoUserBuffsAmbiguousExistsNotFound"
_ErrorCode_name_2 = "GenericOK" _ErrorCode_name_2 = "GenericOK"
) )
var ( var (
_ErrorCode_index_0 = [...]uint8{0, 9, 19, 27, 32, 40, 51} _ErrorCode_index_0 = [...]uint8{0, 8, 13, 21, 32}
_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_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} _ErrorCode_index_2 = [...]uint8{0, 7, 9}
) )
func (i ErrorCode) String() string { func (i ErrorCode) String() string {
switch { switch {
case -35 <= i && i <= -30: case -33 <= i && i <= -30:
i -= -35 i -= -33
return _ErrorCode_name_0[_ErrorCode_index_0[i]:_ErrorCode_index_0[i+1]] return _ErrorCode_name_0[_ErrorCode_index_0[i]:_ErrorCode_index_0[i+1]]
case -24 <= i && i <= -3: case -24 <= i && i <= -3:
i -= -24 i -= -24

70
git.go
View File

@ -18,35 +18,35 @@ import (
type ErrorClass int type ErrorClass int
const ( const (
ErrorClassNone ErrorClass = C.GIT_ERROR_NONE ErrorClassNone ErrorClass = C.GITERR_NONE
ErrorClassNoMemory ErrorClass = C.GIT_ERROR_NOMEMORY ErrorClassNoMemory ErrorClass = C.GITERR_NOMEMORY
ErrorClassOS ErrorClass = C.GIT_ERROR_OS ErrorClassOS ErrorClass = C.GITERR_OS
ErrorClassInvalid ErrorClass = C.GIT_ERROR_INVALID ErrorClassInvalid ErrorClass = C.GITERR_INVALID
ErrorClassReference ErrorClass = C.GIT_ERROR_REFERENCE ErrorClassReference ErrorClass = C.GITERR_REFERENCE
ErrorClassZlib ErrorClass = C.GIT_ERROR_ZLIB ErrorClassZlib ErrorClass = C.GITERR_ZLIB
ErrorClassRepository ErrorClass = C.GIT_ERROR_REPOSITORY ErrorClassRepository ErrorClass = C.GITERR_REPOSITORY
ErrorClassConfig ErrorClass = C.GIT_ERROR_CONFIG ErrorClassConfig ErrorClass = C.GITERR_CONFIG
ErrorClassRegex ErrorClass = C.GIT_ERROR_REGEX ErrorClassRegex ErrorClass = C.GITERR_REGEX
ErrorClassOdb ErrorClass = C.GIT_ERROR_ODB ErrorClassOdb ErrorClass = C.GITERR_ODB
ErrorClassIndex ErrorClass = C.GIT_ERROR_INDEX ErrorClassIndex ErrorClass = C.GITERR_INDEX
ErrorClassObject ErrorClass = C.GIT_ERROR_OBJECT ErrorClassObject ErrorClass = C.GITERR_OBJECT
ErrorClassNet ErrorClass = C.GIT_ERROR_NET ErrorClassNet ErrorClass = C.GITERR_NET
ErrorClassTag ErrorClass = C.GIT_ERROR_TAG ErrorClassTag ErrorClass = C.GITERR_TAG
ErrorClassTree ErrorClass = C.GIT_ERROR_TREE ErrorClassTree ErrorClass = C.GITERR_TREE
ErrorClassIndexer ErrorClass = C.GIT_ERROR_INDEXER ErrorClassIndexer ErrorClass = C.GITERR_INDEXER
ErrorClassSSL ErrorClass = C.GIT_ERROR_SSL ErrorClassSSL ErrorClass = C.GITERR_SSL
ErrorClassSubmodule ErrorClass = C.GIT_ERROR_SUBMODULE ErrorClassSubmodule ErrorClass = C.GITERR_SUBMODULE
ErrorClassThread ErrorClass = C.GIT_ERROR_THREAD ErrorClassThread ErrorClass = C.GITERR_THREAD
ErrorClassStash ErrorClass = C.GIT_ERROR_STASH ErrorClassStash ErrorClass = C.GITERR_STASH
ErrorClassCheckout ErrorClass = C.GIT_ERROR_CHECKOUT ErrorClassCheckout ErrorClass = C.GITERR_CHECKOUT
ErrorClassFetchHead ErrorClass = C.GIT_ERROR_FETCHHEAD ErrorClassFetchHead ErrorClass = C.GITERR_FETCHHEAD
ErrorClassMerge ErrorClass = C.GIT_ERROR_MERGE ErrorClassMerge ErrorClass = C.GITERR_MERGE
ErrorClassSSH ErrorClass = C.GIT_ERROR_SSH ErrorClassSSH ErrorClass = C.GITERR_SSH
ErrorClassFilter ErrorClass = C.GIT_ERROR_FILTER ErrorClassFilter ErrorClass = C.GITERR_FILTER
ErrorClassRevert ErrorClass = C.GIT_ERROR_REVERT ErrorClassRevert ErrorClass = C.GITERR_REVERT
ErrorClassCallback ErrorClass = C.GIT_ERROR_CALLBACK ErrorClassCallback ErrorClass = C.GITERR_CALLBACK
ErrorClassRebase ErrorClass = C.GIT_ERROR_REBASE ErrorClassRebase ErrorClass = C.GITERR_REBASE
ErrorClassPatch ErrorClass = C.GIT_ERROR_PATCH ErrorClassPatch ErrorClass = C.GITERR_PATCH
) )
//go:generate stringer -type ErrorCode -trimprefix ErrorCode -tags static //go:generate stringer -type ErrorCode -trimprefix ErrorCode -tags static
@ -115,10 +115,6 @@ const (
ErrorCodeRetry ErrorCode = C.GIT_RETRY ErrorCodeRetry ErrorCode = C.GIT_RETRY
// ErrorCodeMismatch represents a hashsum mismatch in object. // ErrorCodeMismatch represents a hashsum mismatch in object.
ErrorCodeMismatch ErrorCode = C.GIT_EMISMATCH 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 ( var (
@ -171,7 +167,7 @@ func initLibGit2() {
} }
// Shutdown frees all the resources acquired by libgit2. Make sure no // Shutdown frees all the resources acquired by libgit2. Make sure no
// references to any libgit2 go objects are live before calling this. // references to any git2go objects are live before calling this.
// After this is called, invoking any function from this library will result in // After this is called, invoking any function from this library will result in
// undefined behavior, so make sure this is called carefully. // undefined behavior, so make sure this is called carefully.
func Shutdown() { func Shutdown() {
@ -229,7 +225,7 @@ func NewOid(s string) (*Oid, error) {
} }
if len(slice) != 20 { if len(slice) != 20 {
return nil, &GitError{"invalid oid", ErrorClassNone, ErrorCodeGeneric} return nil, &GitError{"Invalid Oid", ErrorClassNone, ErrGeneric}
} }
copy(o[:], slice[:20]) copy(o[:], slice[:20])
@ -321,7 +317,7 @@ func MakeGitError(c C.int) error {
var errClass ErrorClass var errClass ErrorClass
errorCode := ErrorCode(c) errorCode := ErrorCode(c)
if errorCode != ErrorCodeIterOver { if errorCode != ErrorCodeIterOver {
err := C.git_error_last() err := C.giterr_last()
if err != nil { if err != nil {
errMessage = C.GoString(err.message) errMessage = C.GoString(err.message)
errClass = ErrorClass(err.klass) errClass = ErrorClass(err.klass)
@ -372,7 +368,7 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro
defer C.free(unsafe.Pointer(cstart)) defer C.free(unsafe.Pointer(cstart))
var buf C.git_buf var buf C.git_buf
defer C.git_buf_dispose(&buf) defer C.git_buf_free(&buf)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
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 Normal file
View File

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

View File

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

View File

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

15
http.go
View File

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

View File

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

View File

@ -3,7 +3,6 @@ package git
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"runtime" "runtime"
"testing" "testing"
) )
@ -60,29 +59,14 @@ func TestIndexWriteTreeTo(t *testing.T) {
repo := createTestRepo(t) repo := createTestRepo(t)
defer cleanupTestRepo(t, repo) defer cleanupTestRepo(t, repo)
idx, err := NewIndex() repo2 := createTestRepo(t)
checkFatal(t, err) defer cleanupTestRepo(t, repo2)
odb, err := repo.Odb() idx, err := repo.Index()
checkFatal(t, err) checkFatal(t, err)
err = idx.AddByPath("README")
content, err := ioutil.ReadFile(path.Join(repo.Workdir(), "README"))
checkFatal(t, err) 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) checkFatal(t, err)
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" { if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
@ -223,9 +207,9 @@ func TestIndexAddAllCallback(t *testing.T) {
checkFatal(t, err) checkFatal(t, err)
cbPath := "" cbPath := ""
err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) error { err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) int {
cbPath = p cbPath = p
return nil return 0
}) })
checkFatal(t, err) checkFatal(t, err)
if cbPath != "README" { if cbPath != "README" {

View File

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

View File

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

View File

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

View File

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

View File

@ -13,12 +13,12 @@ import (
type ObjectType int type ObjectType int
const ( const (
ObjectAny ObjectType = C.GIT_OBJECT_ANY ObjectAny ObjectType = C.GIT_OBJ_ANY
ObjectInvalid ObjectType = C.GIT_OBJECT_INVALID ObjectBad ObjectType = C.GIT_OBJ_BAD
ObjectCommit ObjectType = C.GIT_OBJECT_COMMIT ObjectCommit ObjectType = C.GIT_OBJ_COMMIT
ObjectTree ObjectType = C.GIT_OBJECT_TREE ObjectTree ObjectType = C.GIT_OBJ_TREE
ObjectBlob ObjectType = C.GIT_OBJECT_BLOB ObjectBlob ObjectType = C.GIT_OBJ_BLOB
ObjectTag ObjectType = C.GIT_OBJECT_TAG ObjectTag ObjectType = C.GIT_OBJ_TAG
) )
type Object struct { type Object struct {
@ -36,8 +36,8 @@ func (t ObjectType) String() string {
switch t { switch t {
case ObjectAny: case ObjectAny:
return "Any" return "Any"
case ObjectInvalid: case ObjectBad:
return "Invalid" return "Bad"
case ObjectCommit: case ObjectCommit:
return "Commit" return "Commit"
case ObjectTree: case ObjectTree:
@ -68,7 +68,7 @@ func (o *Object) ShortId() (string, error) {
if ecode < 0 { if ecode < 0 {
return "", MakeGitError(ecode) return "", MakeGitError(ecode)
} }
defer C.git_buf_dispose(&resultBuf) defer C.git_buf_free(&resultBuf)
return C.GoString(resultBuf.ptr), nil return C.GoString(resultBuf.ptr), nil
} }
@ -218,7 +218,7 @@ func (o *Object) Peel(t ObjectType) (*Object, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
err := C.git_object_peel(&cobj, o.ptr, C.git_object_t(t)) err := C.git_object_peel(&cobj, o.ptr, C.git_otype(t))
runtime.KeepAlive(o) runtime.KeepAlive(o)
if err < 0 { if err < 0 {
return nil, MakeGitError(err) 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() defer runtime.UnlockOSThread()
var sz C.size_t var sz C.size_t
var cotype C.git_object_t var cotype C.git_otype
ret := C.git_odb_read_header(&sz, &cotype, v.ptr, oid.toC()) ret := C.git_odb_read_header(&sz, &cotype, v.ptr, oid.toC())
runtime.KeepAlive(v) runtime.KeepAlive(v)
if ret < 0 { if ret < 0 {
return 0, ObjectInvalid, MakeGitError(ret) return 0, C.GIT_OBJ_BAD, MakeGitError(ret)
} }
return uint64(sz), ObjectType(cotype), nil 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) size = C.size_t(0)
} }
ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(&data[0]), size, C.git_object_t(otype)) ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(&data[0]), size, C.git_otype(otype))
runtime.KeepAlive(v) runtime.KeepAlive(v)
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
@ -176,32 +176,6 @@ func (v *Odb) Read(oid *Oid) (obj *OdbObject, err error) {
return obj, nil return obj, nil
} }
func (odb *Odb) Refresh() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_refresh(odb.ptr)
runtime.KeepAlive(odb)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (odb *Odb) WriteMultiPackIndex() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_write_multi_pack_index(odb.ptr)
runtime.KeepAlive(odb)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
type OdbForEachCallback func(id *Oid) error type OdbForEachCallback func(id *Oid) error
type odbForEachCallbackData struct { type odbForEachCallbackData struct {
callback OdbForEachCallback callback OdbForEachCallback
@ -263,7 +237,7 @@ func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) {
size = C.size_t(0) size = C.size_t(0)
} }
ret := C.git_odb_hash(oid.toC(), unsafe.Pointer(&data[0]), size, C.git_object_t(otype)) ret := C.git_odb_hash(oid.toC(), unsafe.Pointer(&data[0]), size, C.git_otype(otype))
runtime.KeepAlive(data) runtime.KeepAlive(data)
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
@ -275,7 +249,7 @@ func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) {
// contents of the object. // contents of the object.
func (v *Odb) NewReadStream(id *Oid) (*OdbReadStream, error) { func (v *Odb) NewReadStream(id *Oid) (*OdbReadStream, error) {
stream := new(OdbReadStream) stream := new(OdbReadStream)
var ctype C.git_object_t var ctype C.git_otype
var csize C.size_t var csize C.size_t
runtime.LockOSThread() runtime.LockOSThread()
@ -303,7 +277,7 @@ func (v *Odb) NewWriteStream(size int64, otype ObjectType) (*OdbWriteStream, err
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_odb_open_wstream(&stream.ptr, v.ptr, C.git_object_size_t(size), C.git_object_t(otype)) ret := C.git_odb_open_wstream(&stream.ptr, v.ptr, C.git_off_t(size), C.git_otype(otype))
runtime.KeepAlive(v) runtime.KeepAlive(v)
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)

View File

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

View File

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

155
rebase.go
View File

@ -2,14 +2,11 @@ package git
/* /*
#include <git2.h> #include <git2.h>
extern void _go_git_populate_rebase_callbacks(git_rebase_options *opts);
*/ */
import "C" import "C"
import ( import (
"errors" "errors"
"fmt" "fmt"
"reflect"
"runtime" "runtime"
"unsafe" "unsafe"
) )
@ -72,140 +69,18 @@ func newRebaseOperationFromC(c *C.git_rebase_operation) *RebaseOperation {
return operation return operation
} }
//export commitCreateCallback // RebaseOptions are used to tell the rebase machinery how to operate
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 { type RebaseOptions struct {
Version uint
Quiet int Quiet int
InMemory int InMemory int
RewriteNotesRef string RewriteNotesRef string
MergeOptions MergeOptions MergeOptions MergeOptions
CheckoutOptions CheckoutOptions CheckoutOptions CheckoutOptions
// CommitCreateCallback is an optional callback that allows users to override
// commit creation when rebasing. If specified, users can create
// their own commit and provide the commit ID, which may be useful for
// signing commits or otherwise customizing the commit creation. If this
// callback returns a nil Oid, then the rebase will continue to create the
// commit.
CommitCreateCallback CommitCreateCallback
// Deprecated: CommitSigningCallback is an optional callback that will be
// called with the commit content, allowing a signature to be added to the
// rebase commit. This field is only used when rebasing. This callback is
// not invoked if a CommitCreateCallback is specified. CommitCreateCallback
// should be used instead of this.
CommitSigningCallback CommitSigningCallback
} }
type rebaseOptionsData struct { type rebaseOptionsData struct {
options *RebaseOptions options *RebaseOptions
repo *Repository
errorTarget *error errorTarget *error
} }
@ -216,7 +91,7 @@ func DefaultRebaseOptions() (RebaseOptions, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ecode := C.git_rebase_options_init(&opts, C.GIT_REBASE_OPTIONS_VERSION) ecode := C.git_rebase_init_options(&opts, C.GIT_REBASE_OPTIONS_VERSION)
if ecode < 0 { if ecode < 0 {
return RebaseOptions{}, MakeGitError(ecode) return RebaseOptions{}, MakeGitError(ecode)
} }
@ -225,6 +100,7 @@ func DefaultRebaseOptions() (RebaseOptions, error) {
func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions { func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions {
return RebaseOptions{ return RebaseOptions{
Version: uint(opts.version),
Quiet: int(opts.quiet), Quiet: int(opts.quiet),
InMemory: int(opts.inmemory), InMemory: int(opts.inmemory),
RewriteNotesRef: C.GoString(opts.rewrite_notes_ref), RewriteNotesRef: C.GoString(opts.rewrite_notes_ref),
@ -233,8 +109,8 @@ func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions {
} }
} }
func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, repo *Repository, errorTarget *error) *C.git_rebase_options { func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, errorTarget *error) *C.git_rebase_options {
C.git_rebase_options_init(copts, C.GIT_REBASE_OPTIONS_VERSION) C.git_rebase_init_options(copts, C.GIT_REBASE_OPTIONS_VERSION)
if opts == nil { if opts == nil {
return nil return nil
} }
@ -245,16 +121,6 @@ func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, rep
populateMergeOptions(&copts.merge_options, &opts.MergeOptions) populateMergeOptions(&copts.merge_options, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget) populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget)
if opts.CommitCreateCallback != nil || opts.CommitSigningCallback != nil {
data := &rebaseOptionsData{
options: opts,
repo: repo,
errorTarget: errorTarget,
}
C._go_git_populate_rebase_callbacks(copts)
copts.payload = pointerHandles.Track(data)
}
return copts return copts
} }
@ -265,9 +131,6 @@ func freeRebaseOptions(copts *C.git_rebase_options) {
C.free(unsafe.Pointer(copts.rewrite_notes_ref)) C.free(unsafe.Pointer(copts.rewrite_notes_ref))
freeMergeOptions(&copts.merge_options) freeMergeOptions(&copts.merge_options)
freeCheckoutOptions(&copts.checkout_options) freeCheckoutOptions(&copts.checkout_options)
if copts.payload != nil {
pointerHandles.Untrack(copts.payload)
}
} }
func mapEmptyStringToNull(ref string) *C.char { func mapEmptyStringToNull(ref string) *C.char {
@ -304,7 +167,7 @@ func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedComm
var ptr *C.git_rebase var ptr *C.git_rebase
var err error var err error
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, r, &err) cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err)
ret := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, cOpts) ret := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, cOpts)
runtime.KeepAlive(branch) runtime.KeepAlive(branch)
runtime.KeepAlive(upstream) runtime.KeepAlive(upstream)
@ -328,7 +191,7 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) {
var ptr *C.git_rebase var ptr *C.git_rebase
var err error var err error
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, r, &err) cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err)
ret := C.git_rebase_open(&ptr, r.ptr, cOpts) ret := C.git_rebase_open(&ptr, r.ptr, cOpts)
runtime.KeepAlive(r) runtime.KeepAlive(r)
if ret == C.int(ErrorCodeUser) && err != nil { if ret == C.int(ErrorCodeUser) && err != nil {
@ -479,7 +342,7 @@ func (r *Rebase) Free() {
} }
func newRebaseFromC(ptr *C.git_rebase, repo *Repository, opts *C.git_rebase_options) *Rebase { 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) runtime.SetFinalizer(rebase, (*Rebase).Free)
return rebase return rebase
} }

View File

@ -1,15 +1,10 @@
package git package git
import ( import (
"bytes"
"errors" "errors"
"strconv" "strconv"
"strings"
"testing" "testing"
"time" "time"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
) )
// Tests // Tests
@ -19,73 +14,73 @@ func TestRebaseInMemoryWithConflict(t *testing.T) {
defer cleanupTestRepo(t, repo) defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo) seedTestRepo(t, repo)
// Create two branches with common history, where both modify "common-file" // Create two branches with common history, where both modify "common-file"
// in a conflicting way. // in a conflicting way.
_, err := commitSomething(repo, "common-file", "a\nb\nc\n", commitOptions{}) _, err := commitSomething(repo, "common-file", "a\nb\nc\n", commitOptions{})
checkFatal(t, err) checkFatal(t, err)
checkFatal(t, createBranch(repo, "branch-a")) checkFatal(t, createBranch(repo, "branch-a"))
checkFatal(t, createBranch(repo, "branch-b")) checkFatal(t, createBranch(repo, "branch-b"))
checkFatal(t, repo.SetHead("refs/heads/branch-a")) checkFatal(t, repo.SetHead("refs/heads/branch-a"))
_, err = commitSomething(repo, "common-file", "1\nb\nc\n", commitOptions{}) _, err = commitSomething(repo, "common-file", "1\nb\nc\n", commitOptions{})
checkFatal(t, err) checkFatal(t, err)
checkFatal(t, repo.SetHead("refs/heads/branch-b")) checkFatal(t, repo.SetHead("refs/heads/branch-b"))
_, err = commitSomething(repo, "common-file", "x\nb\nc\n", commitOptions{}) _, err = commitSomething(repo, "common-file", "x\nb\nc\n", commitOptions{})
checkFatal(t, err) checkFatal(t, err)
branchA, err := repo.LookupBranch("branch-a", BranchLocal) branchA, err := repo.LookupBranch("branch-a", BranchLocal)
checkFatal(t, err) checkFatal(t, err)
onto, err := repo.AnnotatedCommitFromRef(branchA.Reference) 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) checkFatal(t, err)
// We then rebase "branch-b" onto "branch-a" in-memory, which should result index, err := rebase.InmemoryIndex()
// in a conflict.
rebase, err := repo.InitRebase(nil, nil, onto, &RebaseOptions{InMemory: 1})
checkFatal(t, err) checkFatal(t, err)
_, err = rebase.Next() // We simply resolve the conflict and commit the rebase.
checkFatal(t, err) if !index.HasConflicts() {
t.Fatal("expected index to have conflicts")
}
index, err := rebase.InmemoryIndex() conflict, err := index.Conflict("common-file")
checkFatal(t, err) checkFatal(t, err)
// We simply resolve the conflict and commit the rebase. resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
if !index.HasConflicts() { checkFatal(t, err)
t.Fatal("expected index to have conflicts")
}
conflict, err := index.Conflict("common-file") resolvedEntry := *conflict.Our
checkFatal(t, err) resolvedEntry.Id = resolvedBlobID
checkFatal(t, index.Add(&resolvedEntry))
checkFatal(t, index.RemoveConflict("common-file"))
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents")) var commitID Oid
checkFatal(t, err) checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
checkFatal(t, rebase.Finish())
resolvedEntry := *conflict.Our // And then assert that we can look up the new merge commit, and that the
resolvedEntry.Id = resolvedBlobID // "common-file" has the expected contents.
checkFatal(t, index.Add(&resolvedEntry)) commit, err := repo.LookupCommit(&commitID)
checkFatal(t, index.RemoveConflict("common-file")) checkFatal(t, err)
if commit.Message() != "rebased message" {
t.Fatalf("unexpected commit message %q", commit.Message())
}
var commitID Oid tree, err := commit.Tree()
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message")) checkFatal(t, err)
checkFatal(t, rebase.Finish())
// And then assert that we can look up the new merge commit, and that the blob, err := repo.LookupBlob(tree.EntryByName("common-file").Id)
// "common-file" has the expected contents. checkFatal(t, err)
commit, err := repo.LookupCommit(&commitID) if string(blob.Contents()) != "resolved contents" {
checkFatal(t, err) t.Fatalf("unexpected resolved blob contents %q", string(blob.Contents()))
if commit.Message() != "rebased message" { }
t.Fatalf("unexpected commit message %q", commit.Message())
}
tree, err := commit.Tree()
checkFatal(t, err)
blob, err := repo.LookupBlob(tree.EntryByName("common-file").Id)
checkFatal(t, err)
if string(blob.Contents()) != "resolved contents" {
t.Fatalf("unexpected resolved blob contents %q", string(blob.Contents()))
}
} }
func TestRebaseAbort(t *testing.T) { func TestRebaseAbort(t *testing.T) {
@ -211,123 +206,6 @@ func TestRebaseNoConflicts(t *testing.T) {
assertStringList(t, expectedHistory, actualHistory) 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 // Utils
func setupRepoForRebase(repo *Repository, masterCommit, branchName string, commitOpts commitOptions) error { func setupRepoForRebase(repo *Repository, masterCommit, branchName string, commitOpts commitOptions) error {
// Create a new branch from master // Create a new branch from master

View File

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

View File

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

View File

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

View File

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

229
remote.go
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

29
ssh.go
View File

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

View File

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

View File

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

View File

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

View File

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

4
tag.go
View File

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

View File

@ -22,6 +22,7 @@ void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stre
*/ */
import "C" import "C"
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"reflect" "reflect"
@ -31,7 +32,7 @@ import (
) )
var ( var (
// globalRegisteredSmartTransports is a mapping of global, libgit2 go-managed // globalRegisteredSmartTransports is a mapping of global, git2go-managed
// transports. // transports.
globalRegisteredSmartTransports = struct { globalRegisteredSmartTransports = struct {
sync.Mutex sync.Mutex
@ -41,7 +42,7 @@ var (
} }
) )
// unregisterManagedTransports unregisters all libgit2 go-managed transports. // unregisterManagedTransports unregisters all git2go-managed transports.
func unregisterManagedTransports() error { func unregisterManagedTransports() error {
globalRegisteredSmartTransports.Lock() globalRegisteredSmartTransports.Lock()
originalTransports := globalRegisteredSmartTransports.transports originalTransports := globalRegisteredSmartTransports.transports
@ -84,22 +85,22 @@ type Transport struct {
ptr *C.git_transport ptr *C.git_transport
} }
// SmartRemoteConnectOptions gets a copy of the proxy options for this transport. // SmartProxyOptions gets a copy of the proxy options for this transport.
func (t *Transport) SmartRemoteConnectOptions() (*RemoteConnectOptions, error) { func (t *Transport) SmartProxyOptions() (*ProxyOptions, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
var copts C.git_remote_connect_options var cpopts C.git_proxy_options
if ret := C.git_transport_remote_connect_options(&copts, t.ptr); ret < 0 { if ret := C.git_transport_smart_proxy_options(&cpopts, t.ptr); ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
return remoteConnectOptionsFromC(&copts), nil return proxyOptionsFromC(&cpopts), nil
} }
// SmartCredentials calls the credentials callback for this transport. // SmartCredentials calls the credentials callback for this transport.
func (t *Transport) SmartCredentials(user string, methods CredentialType) (*Credential, error) { func (t *Transport) SmartCredentials(user string, methods CredType) (*Cred, error) {
cred := newCredential() cred := &Cred{}
var cstr *C.char var cstr *C.char
runtime.LockOSThread() runtime.LockOSThread()
@ -111,7 +112,6 @@ func (t *Transport) SmartCredentials(user string, methods CredentialType) (*Cred
} }
ret := C.git_transport_smart_credentials(&cred.ptr, t.ptr, cstr, C.int(methods)) ret := C.git_transport_smart_credentials(&cred.ptr, t.ptr, cstr, C.int(methods))
if ret != 0 { if ret != 0 {
cred.Free()
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
@ -127,21 +127,10 @@ func (t *Transport) SmartCertificateCheck(cert *Certificate, valid bool, hostnam
parent: C.git_cert{ parent: C.git_cert{
cert_type: C.GIT_CERT_HOSTKEY_LIBSSH2, 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_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_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)) ccert = (*C.git_cert)(unsafe.Pointer(&chostkeyCert))
case CertificateX509: case CertificateX509:
@ -305,10 +294,8 @@ func smartTransportCallback(
registeredSmartTransport := pointerHandles.Get(handle).(*RegisteredSmartTransport) registeredSmartTransport := pointerHandles.Get(handle).(*RegisteredSmartTransport)
remote, ok := remotePointers.get(owner) remote, ok := remotePointers.get(owner)
if !ok { if !ok {
// create a new empty remote and set it err := errors.New("remote pointer not found")
// as a weak pointer, so that control stays in golang return setCallbackError(errorMessage, err)
remote = createNewEmptyRemote()
remote.weak = true
} }
managed := &managedSmartSubtransport{ managed := &managedSmartSubtransport{

23
tree.go
View File

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

View File

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

1
vendor/libgit2 vendored Submodule

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

View File

@ -3,7 +3,6 @@
#include <git2.h> #include <git2.h>
#include <git2/sys/odb_backend.h> #include <git2/sys/odb_backend.h>
#include <git2/sys/refdb_backend.h> #include <git2/sys/refdb_backend.h>
#include <git2/sys/cred.h>
// There are two ways in which to declare a callback: // There are two ways in which to declare a callback:
// //
@ -104,50 +103,12 @@ static int set_callback_error(char *error_message, int ret)
{ {
if (error_message != NULL) { if (error_message != NULL) {
if (ret < 0) if (ret < 0)
giterr_set_str(GIT_ERROR_CALLBACK, error_message); giterr_set_str(GITERR_CALLBACK, error_message);
free(error_message); free(error_message);
} }
return ret; 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) void _go_git_populate_clone_callbacks(git_clone_options *opts)
{ {
opts->remote_cb = (git_remote_create_cb)&remoteCreateCallback; opts->remote_cb = (git_remote_create_cb)&remoteCreateCallback;
@ -254,7 +215,7 @@ static int completion_callback(git_remote_completion_type completion_type, void
} }
static int credentials_callback( static int credentials_callback(
git_credential **cred, git_cred **cred,
const char *url, const char *url,
const char *username_from_url, const char *username_from_url,
unsigned int allowed_types, unsigned int allowed_types,
@ -450,11 +411,6 @@ void _go_git_writestream_free(git_writestream *stream)
stream->free(stream); stream->free(stream);
} }
git_credential_t _go_git_credential_credtype(git_credential *cred)
{
return cred->credtype;
}
static int credential_ssh_sign_callback( static int credential_ssh_sign_callback(
LIBSSH2_SESSION *session, LIBSSH2_SESSION *session,
unsigned char **sig, size_t *sig_len, unsigned char **sig, size_t *sig_len,
@ -472,12 +428,17 @@ static int credential_ssh_sign_callback(
return set_callback_error(error_message, ret); return set_callback_error(error_message, ret);
} }
void _go_git_populate_credential_ssh_custom(git_credential_ssh_custom *cred) void _go_git_populate_credential_ssh_custom(git_cred_ssh_custom *cred)
{ {
cred->parent.free = (void (*)(git_credential *))credentialSSHCustomFree; cred->parent.free = (void (*)(git_cred *))credentialSSHCustomFree;
cred->sign_callback = credential_ssh_sign_callback; 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) 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); return git_odb_write_pack(out, db, transfer_progress_callback, progress_payload);
@ -509,10 +470,7 @@ int _go_git_indexer_new(
git_odb *odb, git_odb *odb,
void *progress_cb_payload) void *progress_cb_payload)
{ {
git_indexer_options indexer_options = GIT_INDEXER_OPTIONS_INIT; return git_indexer_new(out, path, mode, odb, transfer_progress_callback, progress_cb_payload);
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( static int smart_transport_callback(