Compare commits

...

914 Commits

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

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

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

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

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

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

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

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

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

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

* rebase: Add wrapper for `git_rebase_inmemory_index()`

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

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

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

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

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

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

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

This change replaces the double quotes with single quotes:

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

* Update ssh.go

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

Changes:

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

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

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

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

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

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

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

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

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

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

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

The reason this change is split is threefold:

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

This means that the valid combinations are:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

- Add test for odb.Read()
2018-07-03 16:43:07 -07:00
Carlos Martín Nieto 14280de4da
Merge pull request #443 from walkenzoy/master
git2go: fix reference iterator leak
2018-05-31 07:26:17 +02:00
Michel Lespinasse a2de5ababa git2go: fix reference iterator leak
This fixes a leak that was apparently introduced in commit 55a1096141.
2018-05-30 14:15:59 -07:00
Carlos Martín Nieto 2c42b80551
Merge pull request #442 from libgit2/cmn/bump-libgit2
vendor: bump libgit2 to 7f6c1ce9760c6
2018-05-29 21:30:38 +02:00
Carlos Martín Nieto 925932a54f vendor: bump libgit2 to 7f6c1ce9760c6 2018-05-29 21:09:45 +02:00
Carlos Martín Nieto 1381380f34 travis: let yaml know these strings are strings 2018-03-26 12:58:53 +02:00
Carlos Martín Nieto b479bd1463
Merge pull request #438 from libgit2/cmn/bump-27
vendor: update libgit2 to v0.27.0
2018-03-26 12:50:50 +02:00
Carlos Martín Nieto bcb303ad17 travis: build against Go 1.10 2018-03-26 12:35:03 +02:00
Carlos Martín Nieto 304d67b0fb vendor: update libgit2 to v0.27.0 2018-03-26 12:31:09 +02:00
Carlos Martín Nieto ec48b0a307
Merge pull request #430 from sprohaska/pr/keep-alive
tree: keep tree receiver alive as long as dependent entry is used
2018-03-11 14:26:00 +01:00
Carlos Martín Nieto 127b998807
Merge pull request #431 from josharian/refdocs
reference: minor doc improvements
2018-03-11 14:21:25 +01:00
Carlos Martín Nieto e57025a372
Merge pull request #434 from libgit2/cmn/bumping
Update vendored libgit2 to v0.27.0-rc1
2018-02-22 10:06:47 +01:00
Carlos Martín Nieto 661e1a6f1b merge: expose the conflict marker size option 2018-02-22 09:46:42 +01:00
Carlos Martín Nieto cff71166ec Adjust to the change in the git_odb_open_rstream signature 2018-02-22 09:28:58 +01:00
Carlos Martín Nieto b52e13f37d Switch over the version contraints to v0.27 2018-02-22 09:28:49 +01:00
Carlos Martín Nieto ec429ccdfc vendor: bump libgit2 to 809b0ca6b (v0.27.0-rc1) 2018-02-22 09:07:57 +01:00
Josh Bleecher Snyder b7ca4a96f5 git: simplify some Oid methods 2018-02-15 10:01:40 -08:00
Josh Bleecher Snyder 589ec2bc28 reference: minor doc improvements 2018-02-15 09:39:26 -08:00
Steffen Prohaska fcb86e6f8e
tree: keep tree receiver alive as long as dependent entry is used
Signed-off-by: Steffen Prohaska <prohaska@zib.de>
2018-02-09 11:17:10 +01:00
Josh Bleecher Snyder c75e0221d7 cherrypick: wrap git_cherrypick_commit 2018-02-06 21:55:01 -08:00
Josh Bleecher Snyder 9de57cc90e merge: add missing MergeFileFlag constants
While we're here, pull in comments as well.
While one can pop back and forth between godoc and libgit2 refs,
it's much nicer to have it in one place.

Note that MergeFileStyleSimplifyAlnum probably should have been called
merely MergeFileSimplifyAlnum (no "Style"). It's probably not worth
breaking backwards compatibility to fix it, but we avoid the mistake
going forwards.
2018-01-25 16:55:42 -08:00
Josh Bleecher Snyder 03339f731a merge: add two missing AnnotatedCommit methods 2018-01-25 16:00:39 -08:00
Josh Bleecher Snyder cf2379295a signature: improve Signature.Offset docs
Use standard godoc style; be more precise.
2018-01-25 15:59:00 -08:00
Josh Bleecher Snyder 9b850d084e rebase: make RebaseOperationType a fmt.Stringer
Helps with debugging.
2018-01-21 13:48:51 -08:00
Josh Bleecher Snyder 21fd4ad5f6 rebase: add RebaseOperationReword 2018-01-21 13:48:51 -08:00
Carlos Martín Nieto 432a164805
Merge pull request #419 from libgit2/cmn/bump-libgit2
Cmn/bump libgit2
2018-01-17 12:18:08 +00:00
Carlos Martín Nieto a38a8e8ede diff: add two missing options 2018-01-17 11:47:06 +00:00
Carlos Martín Nieto ec5d2bee0f Bump libgit2 to f1323d9c 2018-01-17 11:43:25 +00:00
Carlos Martín Nieto f439cc93e9 Merge pull request #413 from libgit2/cmn/master-static
README: master wants to use install-static
2017-10-16 15:42:45 +02:00
Carlos Martín Nieto cd779176d1 README: correct the branches in the testing block 2017-10-16 15:36:57 +02:00
Carlos Martín Nieto 72510e9d3a README: master wants to use install-static 2017-10-16 15:14:26 +02:00
Carlos Martín Nieto dc745f54c5 Merge pull request #411 from harsimranmaan/patch-1
Fix typo in the error message
2017-10-03 11:30:33 +02:00
Harsimran Singh Maan 3452fe984d Fix typo in the error message 2017-10-01 21:04:08 -07:00
Carlos Martín Nieto b011f6b248 Merge pull request #409 from libgit2/cmn/bumping
Bump libgit2 to c7c5f2c4ec
2017-09-29 09:56:41 +02:00
Carlos Martín Nieto a69a0b1919 patch: adjust to new function signature 2017-09-29 09:49:21 +02:00
Carlos Martín Nieto 028ea07d77 Bump libgit2 to c7c5f2c4ec 2017-09-29 09:41:46 +02:00
Carlos Martín Nieto a2e6f1db85 Merge pull request #398 from reujab/master
Fixed typos
2017-09-03 20:12:52 +02:00
Carlos Martín Nieto 7718e373f6 Merge pull request #406 from libgit2/cmn/go19
travis: include go 1.9
2017-09-03 20:12:11 +02:00
Carlos Martín Nieto b8ead33448 travis: include go 1.9 2017-09-03 20:05:05 +02:00
Carlos Martín Nieto 7f31dccb8f travis: don't update submodules recursively
We have invalid submodules for testing and Travis gets unhappy.
2017-09-03 19:53:33 +02:00
Carlos Martín Nieto 0ebe7220d7 Go back to the v0.26 libgit2 release
Newer commits have changed the build scripts such that the HTTP parser is not
included in the static library.

This also reverts commit ecf4f7a137.
2017-09-03 19:49:30 +02:00
Carlos Martín Nieto ecf4f7a137 patch: correct a pointer type 2017-09-03 16:59:05 +02:00
Mikołaj Baranowski a395d3f635 clone_checkout_strategy removed
https://github.com/libgit2/libgit2/releases/tag/v0.26.0
2017-09-03 16:51:11 +02:00
Carlos Martín Nieto 1c1346d364 Update vendored libgit2 to latest 2017-09-03 16:50:12 +02:00
Vadzim Ramanenka 79fe156d30 Add binding for `git_index_add_frombuffer` 2017-07-17 17:05:03 +03:00
reujab 24637b369f a MIT -> an MIT 2017-07-11 23:58:08 -04:00
reujab a119b21dbb fixed typos 2017-07-11 23:52:13 -04:00
Carlos Martín Nieto 7969aefd42 Merge pull request #397 from libgit2/cmn/tag-generic
Tag any kind of object
2017-07-09 20:10:07 +02:00
Carlos Martín Nieto 916d555644 tag: accept an Objecter for creating a tag
This lets us create a tag for any kind of object.
2017-07-08 23:22:33 +02:00
Carlos Martín Nieto 7f685a6ee6 Add Objecer interface
We do want to be able to accept generic objects in functions. Add this interface
so we can accept that instead of specific object types.
2017-07-08 22:53:50 +02:00
Carlos Martín Nieto 2cff3f2ef4 Merge pull request #396 from libgit2/cmn/rebase-no-operation
rebase: correct the return values for CurrentOperationIndex
2017-07-08 22:22:54 +02:00
Carlos Martín Nieto b98b0e7640 rebase: correct the return values for CurrentOperationIndex
We were incorectly reporting `C.GIT_REBASE_NO_OPERATION` as an error code when
it is none. We should instead return it as the value. The compiler doesn't seem
to actually look at the sizes so instead we must recreate the value ourselves
with `^uint(0)`.

The error return is kept for API compatibility but should go away eventually.
2017-07-08 21:34:10 +02:00
Carlos Martín Nieto ce65c2303e Merge pull request #395 from libgit2/cmn/check-thread-lock
Deduplicate Makefile static target and add thread locking check
2017-07-08 20:49:16 +02:00
Carlos Martín Nieto 27d123f631 Deduplicate Makefile static target and add thread locking check
It turns out we had been running CI without performing the thread locking check.
2017-07-08 18:24:41 +02:00
Carlos Martín Nieto 08db2e2c16 Merge pull request #393 from libgit2/cmn/keepalive-all-the-things
KeepAlive all the things
2017-07-08 16:51:22 +02:00
Carlos Martín Nieto 55a1096141 Third round of keep-alive aditions 2017-07-08 16:07:51 +02:00
Carlos Martín Nieto 7d29d68644 Second round of keep-alives 2017-07-08 11:38:19 +02:00
Carlos Martín Nieto 58334cf604 First round of mass keep-alive additions 2017-07-08 09:11:38 +02:00
Carlos Martín Nieto 0e9336be3f commit: add keep-alives for those that need conversion to pointer receivers
We can't work on the copies here, we need to have pointer receivers so we know
we're keeping alive the object whose finalizer would free the unmanaged memory
we're working with.
2017-07-07 23:36:04 +02:00
Carlos Martín Nieto 5d466ffbc0 commit: add thread locking to signature extraction 2017-07-07 23:24:54 +02:00
Carlos Martín Nieto 29c0b73007 Merge pull request #389 from KatolaZ/master
Added Commit.ExtractSignature to wrap git_commit_extract_signature
2017-07-06 22:22:03 +02:00
Carlos Martín Nieto 7929e49881 Merge pull request #390 from libgit2/cmn/go-vet
Fix a couple of issues reported by go-vet
2017-07-06 22:20:00 +02:00
Carlos Martín Nieto b09f1ab739 Fix a couple of issues reported by go-vet 2017-07-06 21:09:01 +02:00
KatolaZ f7e15669c8 Added Commit.ExtractSignature to wrap git_commit_extract_signature 2017-07-06 08:40:58 +01:00
Carlos Martín Nieto c71c935ad1 Merge pull request #387 from libgit2/cmn/remote-refs
remote: add keep-alive and references to the repository
2017-07-04 14:09:42 +02:00
Carlos Martín Nieto bcf8c1bf40 travis: update the Go versions
We need to use `runtime.KeepAlive()` which only exists past Go 1.7. Furthermore,
Go 1.7 is the latest supported by the language team.
2017-07-04 13:02:12 +02:00
Carlos Martín Nieto 544d29e18b remote: add keep-alive and references to the repository
Especially in 1.8, the garbage collector can decide to finalize an object even
as we are in one of its methods. This means it can free a remote while we're in
one of its calls, as we're referencing the pointer inside the object, rather
than the `Remote` itself.
2017-07-04 12:53:51 +02:00
Carlos Martín Nieto daee43b891 Merge pull request #376 from ankurmittal/short-hash
Add support for getting short object Id
2017-05-20 19:17:15 +02:00
Ankur Mittal 7caac1fa7b Add support for getting short object Id 2017-04-19 18:42:13 -07:00
Carlos Martín Nieto 7cd5a4e731 Merge pull request #371 from libgit2/static-dynamic
Allow building statically via the "static" build tag
2017-04-14 13:11:28 +02:00
Carlos Martín Nieto e7747d3174 travis: build statically 2017-04-14 12:51:13 +02:00
Carlos Martín Nieto 5001942b59 Allow building statically via the "static" build tag 2017-04-14 12:36:42 +02:00
Carlos Martín Nieto 71bdf97227 Merge pull request #366 from LinuxBozo/repository-open-flags
Add new repository open flags
2017-04-14 10:48:01 +02:00
Carlos Martín Nieto 490e8a8722 Merge pull request #368 from cbguder/master
Fix memory leak in Patch.String()
2017-04-14 10:47:39 +02:00
Carlos Martín Nieto 89fc9e7761 Merge pull request #365 from AaronO/fix/memleak-tree-entrybyname
Fix memleaks in Tree.EntryBy(Name/Path/Index), fixes #313
2017-04-13 21:16:30 +02:00
Carlos Martín Nieto 97f1722244 Merge pull request #332 from ezwiebel/rebase-wrapper
Rebase wrapper
2017-04-13 20:50:41 +02:00
Adam Berkovec de521bb2b4 Fix memory leak in Patch.String()
Buffer allocated in Patch.String() was never freed

Signed-off-by: Can Berk Güder <cbguder@pivotal.io>
2017-03-23 12:16:43 -07:00
M. Adam Kendall 7738ebe30a Add new repository open flags 2017-03-07 23:23:24 -05:00
Aaron O'Mullan ee2a023868 Remove unnecessary C.git_tree_entry_free calls ...
As per the docs, these entries belong to the tree and don’t need to be
freed by the caller:
*
https://libgit2.github.com/libgit2/#HEAD/group/tree/git_tree_entry_bynam
e
2017-02-25 02:13:57 +01:00
Aaron O'Mullan f503d918aa Fix memleaks in Tree.EntryBy(Name/Path/Index), fixes #313 2017-02-24 22:55:14 +01:00
Carlos Martín Nieto 4a14260153 Merge pull request #362 from libgit2/cmn/master-tip-static
Update master to latest libgit2 and build statically
2017-01-20 22:55:25 +00:00
Carlos Martín Nieto b020c1140a Update the description of the branches in README. 2017-01-20 13:48:39 +00:00
Carlos Martín Nieto 9f4e0a46b6 Update libgit2 to df4dfaad 2017-01-20 13:20:30 +00:00
Carlos Martín Nieto f037074198 Merge remote-tracking branch 'origin/next' 2017-01-20 00:46:34 +00:00
Carlos Martín Nieto 5d0a4c752a Bump vendored libgit2 to ee89941fa 2017-01-09 22:13:18 +00:00
Carlos Martín Nieto b8a9efd21f Build /v\d+/ branches 2016-12-23 19:14:05 +00:00
ezwiebel 6118c9ba37 LockOSThread in CurrentOperationIndex for git error creation 2016-11-23 17:10:59 +11:00
Carlos Martín Nieto a41e2b6644 Update to 0.25-rc1 2016-11-19 15:09:41 +01:00
Carlos Martín Nieto 4eecbd8d44 Merge pull request #338 from libgit2/cmn/variable-subst
Use variable substitution instead of a wrapper script
2016-11-13 19:31:57 +01:00
ezwiebel a671e67ee8 Took @carlosmn PR review into account 2016-11-01 10:59:32 +11:00
Carlos Martín Nieto 7f426f2435 Only test against 1.5 and up
Go 1.5 is the first one which supports the variable replacement we're
using for the flags. Any older versions aren't supported by the Go team
now that 1.7 is out, so you should be on one of these versions.
2016-11-01 00:17:51 +01:00
Carlos Martín Nieto 9c5fb973fc Get rid of the with-static.sh script
CGO can perform variable substitution in the directives, so we don't
need to use a script to set up the variables; we can let the go tool do
it for us.
2016-11-01 00:17:51 +01:00
Carlos Martín Nieto 1c8297ab83 Merge pull request #355 from libgit2/cmn/travis-branches
travis: only build master and next
2016-11-01 00:16:51 +01:00
Carlos Martín Nieto 9af9dd3ad7 Merge pull request #354 from libgit2/cmn/panic-threading
Add Feature query support & panic if libgit2 is not thread-aware
2016-11-01 00:16:27 +01:00
Carlos Martín Nieto 28bc42ce82 travis: only build master and next
The other branches will get built as part of a PR.
2016-10-31 21:34:00 +01:00
Carlos Martín Nieto a37f7f30ff Panic if libgit2 is not thread-aware
Go calling C is inherently multi-threaded. If libgit2 cannot handle
threading, then we're going to crash at some random point. Crash right
at the start so we know what's happening.
2016-10-31 21:14:08 +01:00
Carlos Martín Nieto adb1770ff3 Add Features() to retrieve the compile-time features of libgit2 2016-10-31 21:09:24 +01:00
Carlos Martín Nieto 0b98f7beae Merge pull request #348 from MagicalTux/git2go_issue_314
Make New*BackendFromC take unsafe.Pointer as argument
2016-10-31 20:35:54 +01:00
Carlos Martín Nieto 098cd42070 Merge pull request #353 from libgit2/update-next
Update to libgit2 a051ee3
2016-10-31 20:15:48 +01:00
Carlos Martín Nieto 3cc2126300 Merge pull request #319 from netnose/remote-refinements
Remote Refinements
2016-10-31 20:04:35 +01:00
Carlos Martín Nieto 42a90d4e68 Merge pull request #351 from ezwiebel/index-remove-directory
Implement git_index_remove_directory in index wrapper
2016-10-31 20:03:05 +01:00
Carlos Martín Nieto e8062bcadd Update to libgit2 a051ee3 2016-10-31 19:31:43 +01:00
ezwiebel c18c8693fe Implement git_index_remove_directory in index wrapper 2016-10-20 15:49:24 +11:00
Mirko Nosenzo 4567e4f7fa Merge remote-tracking branch 'upstream/v24' into remote-refinements 2016-10-08 11:33:56 +02:00
Mirko Nosenzo 717a47f754
Remote Rename Fix
Problem string array is returned if no error is occurred
2016-10-08 11:18:07 +02:00
Carlos Martín Nieto 53594d7581 Merge pull request #345 from kdambekalns/commit-message-raw
Add method to fetch raw commit message
2016-10-03 02:54:48 -07:00
Carlos Martín Nieto e9668545c9 Merge pull request #321 from netnose/checkout-callbacks
Checkout callbacks
2016-10-03 02:53:29 -07:00
Carlos Martín Nieto 2209188637 Merge pull request #322 from calavera/ssh_memory_credentials
Add NewCredSshKeyFromMemory to the credentials helpers.
2016-10-03 02:39:58 -07:00
Carlos Martín Nieto a3c2ac18dc Merge pull request #347 from geordie/master
Write index before writing index tree in seedTestRepo test helper
2016-10-03 02:37:05 -07:00
Carlos Martín Nieto c94904ea37 Merge pull request #341 from mdaffin/branchiterator-foreach-errors
Add check for ErrIterOver in BranchIterator.ForEach
2016-10-03 02:31:58 -07:00
Carlos Martín Nieto ddc1515c8e Merge commit 'refs/pull/331/head' of github.com:libgit2/git2go 2016-10-03 11:28:38 +02:00
Mark Karpeles b829eb1edb odb & refdb: make New*BackendFromC take unsafe.Pointer as argument allowing argument to be set from different package 2016-09-30 23:00:20 +09:00
Geordie Henderson a16e24a99e Write the index before writing the index tree in seedTestRepo test helper func 2016-09-16 21:49:54 -07:00
ezwiebel 03e10c5639 Fix reference bug introduced with RebaseOptions implementation 2016-09-14 14:42:58 +10:00
ezwiebel adc3a4bd89 Add DefaultRebaseOptions() [git_rebase_init_options(GIT_REBASE_OPTIONS_VERSION)] service to wrapper 2016-09-13 16:03:16 +10:00
ezwiebel e00b0831aa Add RebaseOpen() service to wrapper 2016-09-12 15:58:53 +10:00
Karsten Dambekalns d2b8c99ba7 Add method to fetch raw commit message
The existing `Commit.Message()` returns the trimmed commit message. In some cases
it is important to retrieve the exact commit message, even if it contains surrounding
newlines.

This adds a new `Commit.RawMessage()` to be able to do that.
2016-09-09 15:27:07 +02:00
Michael Daffin 74bc3c6242 Add check for ErrIterOver in BranchIterator.ForEach
The BranchIterator.ForEach currently returns the ErrIterOver error if no
error had occured during the iteration. This leads to a rather unhelpful
blank error message with the error code -31 when iterating over the
branches.

This commit adds a check for ErrIterOver at the end of the ForEach
method so that the client code only has to worry about checking for nil
as apose to checking for the ErrIterOver error.
2016-09-05 15:56:09 +01:00
Alan Johnson 208cdaef76 Removes redundant iteration over check. 2016-08-29 09:10:56 -04:00
Mirko Nosenzo 6ffad323ba
Removed Useless Argument Check 2016-08-28 11:21:10 +02:00
Carlos Martín Nieto 8eb8fa3725 Also remove a pkg-config directive that snuck in 2016-08-27 21:35:26 +02:00
Carlos Martín Nieto 92fa6357ae Bring back the Makefile from 'next' 2016-08-27 21:11:22 +02:00
Carlos Martín Nieto aadd0c2035 Merge remote-tracking branch 'upstream/master' into next 2016-08-27 21:07:44 +02:00
Carlos Martín Nieto 241aa34d83 Merge pull request #336 from libgit2/cmn/test-parallel
Run the tests in parallel
2016-08-27 21:03:29 +02:00
Carlos Martín Nieto e1467c0641 Merge pull request #337 from libgit2/cmn/go16-blob-pointer
Work around the finnicky 1.6 CGo pointer checks
2016-08-27 21:02:47 +02:00
Carlos Martín Nieto 5c678353fa Add Go 1.7 to the build list 2016-08-27 20:52:07 +02:00
Carlos Martín Nieto b41e4c4ac7 Work around Go 1.6's CGo pointer check
It depends heavily on the expression at the call site an whether it can
figure out whether we're using a slice or not, so provid an incantation
that does this.
2016-08-27 20:51:13 +02:00
Carlos Martín Nieto b5d213c2c1 Remove unecessary copy 2016-08-27 20:47:41 +02:00
Itamar Turner-Trauring 3c1ba8c40e Add test for slice-to-slice and GCo pointer detection 2016-08-27 20:47:01 +02:00
Carlos Martín Nieto 0703dae9b2 Merge pull request #320 from netnose/enum-mappings
Enum Mappings
2016-08-27 19:27:10 +02:00
Carlos Martín Nieto e55c00eca7 Run the tests in parallel
This saves about 1s, or 1/3 of the test runtime. The linking is still
much slower, but this we can control.
2016-08-27 19:21:05 +02:00
ezwiebel b2d71f4fbc Fix Free() service in Rebase wrapper 2016-08-08 10:49:40 +10:00
ezwiebel 193b21398b Add CurrentOperationIndex() and OperationAt(index uint) services to wrapper 2016-08-07 18:09:33 +10:00
ezwiebel e1f0949740 Add Abort() service to wrapper 2016-08-07 17:48:18 +10:00
ezwiebel a62a8c3b92 Add operation OperationCount() service and enrich UTs 2016-08-07 17:48:18 +10:00
ezwiebel b1a9de8037 Initial rebase wrapper version 2016-08-07 17:48:18 +10:00
Carlos Martín Nieto 1670c49c7e Merge pull request #323 from Coderlane/diff_stats_string
Add DiffStats String
2016-08-05 16:23:34 +02:00
Carlos Martín Nieto 37d3c2d9ad Update libgit2 to 73dab769
This version reloads the index on checkout, which showed we were not
persisting the updated index to disk and thus would have conflicts on
checkout.
2016-08-05 15:15:22 +02:00
Alan Johnson 380684bb10 Fixing issue with error conversion. 2016-08-04 09:43:44 -04:00
Carlos Martín Nieto 3ed398a78e Merge pull request #328 from navytux/y/odb-type
odb: Expose git_odb_object_type() as OdbObject.Type()
2016-07-24 18:09:12 +02:00
Mirko Nosenzo aa7dfab1cf
Clone Fix
freeCheckoutOpts called twice
2016-07-24 14:20:12 +02:00
Kirill Smelkov cf7553e72c odb: Expose git_odb_object_type() as OdbObject.Type()
It might be needed when one is writing `git cat-file --batch` equivalent
which has output format

    <sha1> SP <type> SP <size> LF
    <contents> LF
2016-07-19 22:48:52 +03:00
Mirko Nosenzo c6b94a160e
Returning Problems on Remote Rename
Problems can be returned as string array on RemoteCollection Rename
2016-07-07 12:01:24 +02:00
Carlos Martín Nieto f720800b50 Merge pull request #318 from netnose/tag-remove
Tag Remove
2016-07-06 23:54:40 +02:00
Carlos Martín Nieto 30cffcb526 Merge pull request #317 from netnose/reset-default
ResetDefaultToCommit maps git_reset_default
2016-07-06 23:53:43 +02:00
Mirko Nosenzo 6fa4c8ea15 Checkout Payload Pointer Fix
- Tracking pointer once
- Tracking pointer and not struct
2016-06-25 11:39:44 +02:00
David Calavera a2f93e91d2
Add NewCredSshKeyFromMemory to the credentials helpers.
Allowing to use public and private keys from memory
without reading them from disk and without using an
ssh agent.

Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-06-22 14:50:29 -07:00
Travis Lane 981538924c diff: Add DiffStats String
This implements git_diff_stats_to_buf which provides the output for
git diff --stats.
2016-06-19 15:19:39 -07:00
Mirko Nosenzo 18802c24e3 Checkout Callbacks Payload Check in Callback Functions
Checking payloads in callback functions
2016-05-29 15:27:48 +02:00
Mirko Nosenzo db6f44c71c Checkout Callbacks Payload Check
Checking payloads before using them
2016-05-29 15:14:16 +02:00
Mirko Nosenzo 82f86f2f13 StatusConflicted maps GIT_STATUS_CONFLICTED
Added support for file in conflicted status
2016-05-29 15:01:17 +02:00
Mirko Nosenzo 298f2e2111 BranchAll maps GIT_BRANCH_ALL
Added support to All Branch Iteration and Lookup
2016-05-29 15:01:07 +02:00
Mirko Nosenzo 393098522c Remote Refinements
- Fixed credentialsCallback return value for missing callback
- Added Remote Rename
- Added Remote Disconnect
2016-05-29 14:59:17 +02:00
Mirko Nosenzo 8b855ce765 Tag Remove
Added support for removal of a Tag
2016-05-29 14:57:00 +02:00
Mirko Nosenzo 8919236801 ResetDefaultToCommit maps git_reset_default
Added support for default reset behavior
2016-05-29 14:53:26 +02:00
Mirko Nosenzo 278dc9ac4f Checkout Callbacks
- Added CheckoutNotifyType mapping git_checkout_notify_t
- Added CheckoutOpts.NotifyFlags of type CheckoutNotifyType
- Added CheckoutNotifyCallback mapping git_checkout_notify_cb
- Added CheckoutProgressCallback mapping git_checkout_progress_cb
2016-05-29 13:44:18 +02:00
Carlos Martín Nieto 8eaae73f85 Error out if we detect an incompatible libgit2 version
The master version supports only v0.24 so let's enforce that via the
compiler.
2016-04-27 14:53:21 +02:00
Carlos Martín Nieto 9163ca7d50 Update to 1dc4491 2016-04-23 15:35:22 +02:00
Carlos Martín Nieto 652a14f732 Merge pull request #301 from hansrodtang/next
Add some constants and repository methods.
2016-03-31 04:44:23 -07:00
Hans Rødtang 2be7d7987b Add Repository.IsShallow 2016-03-29 20:42:41 +02:00
Hans Rødtang 726331dfde Add Repository.IsEmpty 2016-03-29 20:42:30 +02:00
Hans Rødtang b99dbb1361 Add Repository.IsHeadUnborn 2016-03-29 20:42:19 +02:00
Hans Rødtang 094bb3767b Add unimplemented diff delta/flag values. 2016-03-29 20:41:46 +02:00
Hans Rødtang 1e8b7ef380 Add "Conflicted" git.Delta and git.Status. 2016-03-29 20:37:22 +02:00
Carlos Martín Nieto 836b6c56be Merge pull request #257 from clns/stash-support
[next] Add stash support
2016-03-17 18:14:30 +01:00
Carlos Martín Nieto 95793ac11d Merge pull request #298 from hirochachacha/add_git_diff_tree_to_index
Add git_diff_tree_to_index
2016-03-11 14:34:20 +01:00
Hiroshi Ioka 975228d55c add DiffTreeToIndex
This is equivalent to `git diff --cached <treeish` or `diff --cached`.
2016-03-11 02:25:33 +09:00
Carlos Martín Nieto d3bd8903f8 Update libgit2 version to install on Travis to 24 2016-03-07 11:33:44 +01:00
Carlos Martín Nieto 2ae7d13ba1 Merge branch 'next' 2016-03-07 11:22:53 +01:00
Carlos Martín Nieto e095c85fd0 Update to 785d8c 2016-03-07 11:16:07 +01:00
Calin Seciu 71ff6ab0d5 Fix error after updating to latest changes 2016-02-20 14:58:48 +02:00
Calin Seciu c6f394e407 Merge branch 'next' into stash-support 2016-02-20 14:52:57 +02:00
Calin Seciu dc4409793d Remove Untrack() from free() function
https://github.com/libgit2/git2go/pull/257#discussion_r53443211
2016-02-20 14:44:20 +02:00
Calin Seciu 5191254a66 Fix problems based on PR comments
https://github.com/libgit2/git2go/pull/257#discussion_r53432957
https://github.com/libgit2/git2go/pull/257#discussion_r53443418
2016-02-20 14:43:46 +02:00
Carlos Martín Nieto 251d89e1d4 Update vendored libgit2 2016-02-19 13:59:50 +01:00
Carlos Martín Nieto 6941cccb69 Merge branch 'master' into next 2016-02-18 20:11:54 +01:00
Carlos Martín Nieto fa644d2fc9 Merge pull request #296 from libgit2/cmn/track-real-pointers
handles: use real pointers to keep track of handles
2016-02-18 18:43:56 +01:00
Carlos Martín Nieto f1240e6565 handles: use real pointers to keep track of handles
With the change to 1.6 rules, we couldn't use the Go pointers, so we
went with casting the list indices into pointers.

The runtime does not like this, however. It will sometimes detect that
we have a pointer with a very small value and consider it an invalid
pointer, bringing down the application with it.

Work around that by asking libc for the smallest amount of memory it'll
give us so we have an actual allocated pointer to use. We then use this
pointer value as the key in our map to find the Go object we're
tracking.
2016-02-18 17:33:44 +01:00
Carlos Martín Nieto ac719c6759 Merge branch 'master' into next 2016-02-18 15:57:46 +01:00
Carlos Martín Nieto 4a9a59c6fa Merge commit 'refs/pull/285/head' of github.com:libgit2/git2go into next 2016-02-18 15:43:58 +01:00
Carlos Martín Nieto f05417aaba Merge pull request #282 from ianlancetaylor/master
handles, merge, odb: changes for Go 1.6 pointer passing rules
2016-02-18 13:10:08 +01:00
Han-Wen Nienhuys 090dc7ee39 Use Filemode in TreeBuilder.Insert, and add test coverage for some
TreeBuilder methods.
2016-02-17 18:23:52 +01:00
Han-Wen Nienhuys aa59dccea7 Upgrade to libgit2 to 0f9d15493d5d8ad4353dd7beed52c9567334f6e5 2016-02-17 18:23:52 +01:00
Ian Lance Taylor dc8b154f4f odb: don't copy buffer 2016-02-16 21:40:05 -08:00
Ian Lance Taylor ddbf1baab1 merge: remove whitespace change 2016-02-16 21:36:57 -08:00
Ian Lance Taylor a1f25eafec handles, merge: simplify code, don't copy file contents 2016-02-16 21:34:43 -08:00
Ian Lance Taylor b70973e5c7 Merge remote-tracking branch 'upstream/master' 2016-02-16 21:06:45 -08:00
Carlos Martín Nieto 6d6736b2bd Merge remote-tracking branch 'upstream/master' into next 2016-02-15 15:02:19 +01:00
Carlos Martín Nieto 55594814c9 Merge pull request #263 from joseferminj/master
Expose AddGitIgnoreRules and ClearGitIgnoreRules funcs
2016-02-15 14:47:53 +01:00
Carlos Martín Nieto ae1de83894 Merge pull request #292 from orivej/git_revwalk_simplify_first_parent
Add RevWalk.SimplifyFirstParent()
2016-02-09 06:03:27 +01:00
Orivej Desh b876e836fa Add RevWalk.SimplifyFirstParent() 2016-02-07 03:15:40 +00:00
Carlos Martín Nieto 4a7794664e Merge pull request #288 from hanwen/readheader
Expose git_odb_read_header as Odb.ReadHeader.
2016-02-06 13:16:59 -08:00
Han-Wen Nienhuys 773ac24a16 Expose git_odb_read_header as Odb.ReadHeader.
This function is much faster for discovering sizes for a given OID.
2016-02-03 15:56:39 +01:00
Carlos Martín Nieto 6d60e0f2c9 Merge pull request #283 from ebfe/repo-open
Add missing RepositoryOpenExtended arguments
2016-01-16 01:44:09 +01:00
Michael Gehring 1bc7cf60bd Add missing RepositoryOpenExtended arguments
Fixes #277
2016-01-15 22:31:21 +01:00
Carlos Martín Nieto 2e17c3d55f Merge pull request #280 from ebfe/hint-path
Don't drop CreateBlobFromChunks hintPath argument
2016-01-15 22:22:36 +01:00
Ian Lance Taylor 42b11d403d handles, merge, odb: changes for Go 1.6 pointer passing rules
See http://tip.golang.org/cmd/cgo/#hdr-Passing_pointers .
2016-01-07 18:37:46 -08:00
Michael Gehring 51d3ead30d Don't drop CreateBlobFromChunks hintPath argument 2016-01-04 15:02:21 +01:00
Carlos Martín Nieto 105573ac7a Merge pull request #275 from clearr/index-find
Add Index.Find() and Index.FindPrefix()
2015-12-29 13:21:41 +00:00
FUJII Ryota 20ab28bfea Add Index.Find() and Index.FindPrefix() 2015-12-21 18:19:03 +09:00
Carlos Martín Nieto 9022ab9c19 Merge pull request #273 from clearr/fix-index-entrybypath-leak
Fix a memory leak in Index.EntryByPath()
2015-12-16 15:19:35 +01:00
FUJII Ryota 1cdf1d70a2 Fix a memory leak in Index.EntryByPath() 2015-12-16 16:43:26 +09:00
Carlos Martín Nieto 4ee13db86d Merge pull request #271 from joseferminj/fix-memory-problems
Fix Fetch/Push memory allocation problems
2015-11-13 18:33:02 +01:00
Jose Alvarez 92d736d12c Fix Fetch/Push memory allocation problems
The Fetch/Push operations didn't allocate the git_*_options structure
and this causes a memory problem in the libgit2 code. Following the
example of Clone operation, the Fetch/Push functions allocates the
options structure before calling the C.
2015-11-12 21:15:24 -05:00
Carlos Martín Nieto f05a6a3384 Merge pull request #269 from durin42/small-fixes
Small fixes I noticed while perusing the code.
2015-11-03 08:30:59 -08:00
Augie Fackler 714cd56c71 odb: remove debug fmt.Printlns
These appear to be left over debug statements, and they also look like
they were intended to be fmt.Printf calls anyway.
2015-11-02 16:00:19 -05:00
Augie Fackler f18ea412dc config_test: properly detect failed config writes
This patch fixes the setup stage of the config tests to notice when
the writes fail (eg $PWD is a read-only filesystem) and to correctly
skip the entire test function as a result.
2015-11-02 16:00:04 -05:00
Carlos Martín Nieto c646a2eb30 Merge pull request #268 from clearr/fix-index-leaks
Fix memory leaks in NewIndex() and OpenIndex()
2015-10-29 13:04:01 +01:00
FUJII Ryota ae107d5f56 Fix memory leaks in NewIndex() and OpenIndex() 2015-10-27 15:20:50 +09:00
Carlos Martín Nieto 749963ce55 Merge pull request #266 from clns/update-libgit2
[next] Update libgit2 to 821131f
2015-10-26 21:44:16 +01:00
Carlos Martín Nieto 3b5633de21 Mention that MergeAnalysis is a bitmask 2015-10-26 21:22:22 +01:00
Calin Seciu 367cd8eb9b Update libgit2 to 821131f
The API changes are:

- `*Remote.Connect`

  ```go
  // from:
  func (o *Remote) Connect(direction ConnectDirection, callbacks *RemoteCallbacks) error
  // to:
  func (o *Remote) Connect(direction ConnectDirection, callbacks *RemoteCallbacks, headers []string) error
  ```

- `*Remote.ConnectFetch` - `headers` was added as above
- `*Remote.ConnectPush` - `headers` was added as above
2015-10-26 16:20:18 +02:00
Carlos Martín Nieto c4868aef6c Merge pull request #262 from clearr/ignore-support
Add support for ignore
2015-10-21 16:33:05 +02:00
FUJII Ryota 56cc9e1b0e Add support for ignore 2015-10-20 15:07:54 +09:00
Jose Alvarez 22495763b7 Expose AddGitIgnoreRules and ClearGitIgnoreRules funcs 2015-10-13 11:33:37 -04:00
Carlos Martín Nieto 22da351b1e Merge pull request #258 from TheDahv/feat-config-snapshot
Fix bug in Config LookupString
2015-10-09 13:00:26 +02:00
David Pierce 81e0b16d9f Tests config lookup methods 2015-10-08 09:47:23 -07:00
David Pierce 80cf533fe4 Config#LookupString uses git_buf to load value 2015-10-08 09:37:36 -07:00
Carlos Martín Nieto 43b39805bd Merge pull request #261 from jbranchaud/fix-readme-typo
Fix typo in README: manaager -> manager
2015-10-07 12:29:34 +02:00
jbranchaud b1d97c1ebd Fix typo in README: manaager -> manager 2015-10-06 15:12:49 -05:00
Carlos Martín Nieto 698ed4a42d Merge pull request #260 from clearr/index-entrybypath
Add EntryByPath method to Index
2015-09-30 08:53:05 +02:00
FUJII Ryota b8283e7277 Add EntryByPath method to Index 2015-09-28 20:38:04 +09:00
Calin Seciu d54ea1d6a8 Add stash support 2015-09-21 14:50:57 +03:00
Carlos Martín Nieto 1c855246ca Merge pull request #253 from joseferminj/checkout-baseline
Expose baseline field in CheckoutOptions
2015-09-18 22:55:04 +02:00
Jose Alvarez 9397af0854 Expose baseline field in CheckoutOptions 2015-09-18 09:50:59 -04:00
Carlos Martín Nieto 0522886781 Merge remote-tracking branch 'origin/master' into next 2015-09-18 10:52:37 +02:00
Carlos Martín Nieto ebf7f15bf9 Merge branch 'status-options' 2015-09-18 10:49:14 +02:00
Calin Seciu 34fb7e03ec Fix crash when using Pathspec in StatusOptions
Using `StatusOptions.Pathspec` results in a fatal error panic with
the message 'unexpected signal during runtime execution'.

This is because the `&cpathspec` C.git_strarray gets freed in
`*StatusOptions.toC()` before being passed to
`C.git_status_init_options()` in `*Repository.StatusList()`
(see b3e7705c48/status.go (L138))

The relevant panic trace is:

```
fatal error: unexpected signal during runtime execution
[signal 0xb code=0x1 addr=0xb01dfacedebac1e pc=0x4062609]

runtime stack:
runtime.throw(0x469a080, 0x2a)
    /usr/local/Cellar/go/1.5.1/libexec/src/runtime/panic.go:527 +0x90
runtime.sigpanic()
    /usr/local/Cellar/go/1.5.1/libexec/src/runtime/sigpanic_unix.go:12
+0x5a

goroutine 71 [syscall, locked to thread]:
runtime.cgocall(0x400a720, 0xc8204e9998, 0x0)
    /usr/local/Cellar/go/1.5.1/libexec/src/runtime/cgocall.go:120 +0x11b
fp=0xc8204e9968 sp=0xc8204e9938
github.com/libgit2/git2go._Cfunc_git_status_list_new(0xc8204c39c8,
0x5e17780, 0xc820478c40, 0xc800000000)
    ??:0 +0x39 fp=0xc8204e9998 sp=0xc8204e9968
github.com/libgit2/git2go.(*Repository).StatusList(0xc820013290,
0xc8204e9b58, 0x0, 0x0, 0x0)
    /Users/calin/go/src/github.com/libgit2/git2go/status.go:168 +0x11d
fp=0xc8204e99e8 sp=0xc8204e9998
```
2015-09-18 10:48:26 +02:00
Carlos Martín Nieto b3e7705c48 Update vendored libgit2 2015-08-31 20:24:54 +02:00
Carlos Martín Nieto d5890f58e8 Run go fmt
As it seems to be something that many people can't get over, reformat
all the files; as we're breaking things, whoever depended on 'next' will
have to take many changes into account anyway, so let's include this to
reduce the noise of incoming patches.
2015-08-31 20:22:17 +02:00
Carlos Martín Nieto 876ddd17eb Update missing bit to the new object model 2015-08-31 20:21:28 +02:00
Carlos Martín Nieto d59752528c Merge pull request #237 from libgit2/object-type
Move from an Object interface to a type
2015-08-31 20:12:45 +02:00
Carlos Martín Nieto 6d3a3499f1 Merge branch 'master-v23' 2015-08-31 19:58:29 +02:00
Carlos Martín Nieto 4090c401c8 Don't call the finalizer on a borrowed repository
When libgit2 gives us the repository for us to create the remote in, we
do not own it, so we must make sure we don't try to free it.
2015-08-31 16:07:37 +02:00
Carlos Martín Nieto 2743bbfca3 Test against Go 1.5 2015-08-31 16:07:37 +02:00
Carlos Martín Nieto 1ea9965824 Install v23 on Travis 2015-08-31 16:07:37 +02:00
Carlos Martín Nieto 337f25d47e Remove the vendored libgit2 submodule
This is a left-over from the merge from 'next'.
2015-08-31 16:07:08 +02:00
Carlos Martín Nieto f72db33baf Merge branch 'next' 2015-08-31 13:55:46 +02:00
Carlos Martín Nieto 157593f38d Don't trat a revwalk's ITEROVER as an error 2015-08-31 13:13:27 +02:00
Carlos Martín Nieto e4b2222888 Merge pull request #242 from pks-t/fix-populate-clone-options
clone: do not free clone options' payload
2015-08-31 13:05:36 +02:00
Carlos Martín Nieto c6c2e9389f Merge branch 'push-cb' into next 2015-08-31 12:52:20 +02:00
Aaron O'Mullan 803ef7dad5 Add nil check on CredentialsCallback wrapper 2015-08-31 12:48:11 +02:00
Aaron O'Mullan a572b15df6 Add back support for RemoteCallbacks in Remote.Push() 2015-08-31 12:43:36 +02:00
Patrick Steinhardt 0b530c15cf clone: improve handling of remote create callback
The clone options contain fields for ae remote create callback
and its payload, which can be used to override the behavior when
the default remote is being created for newly cloned
repositories.

Currently we only accept a C function as callback, though, making
it overly complicated to use it. We also unconditionally `free`
the payload if its address is non-`nil`, which may cause the
program to segfault when the memory is not dynamically allocated.

Instead, we want callers to provide a Go function that is
subsequently being called by us. To do this, we introduce an
indirection such that we are able to extract the provided
function and payload when being called by `git_clone` and handle
the return values of the user-provided function.
2015-08-18 14:01:51 +02:00
Carlos Martín Nieto 3d15c877d8 Merge pull request #241 from pks-t/memleak-fixes
Memleak fixes
2015-08-13 02:04:23 +02:00
Patrick Steinhardt 37bb1a6025 merge: fix memory leak related to merge file opts 2015-08-12 10:24:59 +02:00
Patrick Steinhardt cce14aa58b branch: fix memory leaks related to CStrings 2015-08-12 10:24:59 +02:00
Carlos Martín Nieto ff6d4a7dfd We do require sudo on Travis 2015-08-04 15:01:17 +02:00
Carlos Martín Nieto 47949510f1 Merge remote-tracking branch 'origin/master' into next 2015-08-04 14:59:10 +02:00
Carlos Martín Nieto b7159b0cd4 Move from an Object interface to a type
An Object should be about representing a libgit2 object rather than
showing which methods it should support.

Change any return of Object to *Object and provide methods to convert
between this and the particular type.
2015-08-04 14:47:10 +02:00
Carlos Martín Nieto 5d989f2cad Merge pull request #236 from clns/object-peel
Add ability to peel any git object
2015-08-03 10:56:47 +02:00
Carlos Martín Nieto fba081ddbb Merge pull request #227 from clns/describe
Add git-describe support
2015-08-03 10:50:11 +02:00
Calin Seciu 17950c198b Add ability to peel any git object
Includes support for 'git_object_peel'.
2015-08-01 14:28:20 +02:00
Calin Seciu 1018ff76d0 Add git-describe support
Includes 'git_describe_commit' and 'git_describe_workdir'.
2015-07-31 22:39:33 +02:00
Carlos Martín Nieto ed62fda34d Merge pull request #234 from libgit2/cmn/merge-base
Wrap MergeBases
2015-07-31 20:27:15 +02:00
Carlos Martín Nieto c0c6caed3a Merge branch 'tags-col' into next 2015-07-31 20:24:18 +02:00
Carlos Martín Nieto def4494b74 Move CreateTag to the tags collection 2015-07-31 20:23:05 +02:00
Carlos Martín Nieto efd61a4bc0 Wrap MergeBases
While here, test MergeBase as well.
2015-07-31 11:37:18 +02:00
Calin Seciu 6c4af98c5b Add more support for tags
Implement support for the following libgit2 functions:

- 'git_tag_list' and 'git_tag_list_match'
- 'git_tag_foreach'
- 'git_tag_create_lightweight'
2015-07-31 10:07:26 +02:00
Calin Seciu 12311c8528 Add TagsCollection 2015-07-31 09:51:19 +02:00
Carlos Martín Nieto 3de64ea8af Merge pull request #233 from tuexss/typos
Fixed typos/unclarities in README.md
2015-07-29 21:05:09 +02:00
Andreas Beer 08d30893b6 Headline typo 2015-07-29 11:28:34 +02:00
Andreas Beer 47d82916e2 Typos/unclarities in readme. 2015-07-29 11:28:05 +02:00
Carlos Martín Nieto a2e4e9259b Merge pull request #230 from clns/tree-entry-by-id
Find tree entry by id
2015-07-29 01:53:10 +02:00
Carlos Martín Nieto f2d8797a96 Merge pull request #231 from shurcooL/master-fix-HandleList-Track-gc-issue
[master] Prevent slot int variable from being GCed.
2015-07-28 08:09:01 +02:00
Dmitri Shuralyov e1e1b4b1e1 Prevent slot int variable from being GCed.
Before this change, there were no users of slot int variable in the Go
world (just a pointer to it that ended up in C world only), so Go's
garbage collector would free it and its value could not retrieved later
(once a pointer to it comes back to Go world from C world).

Keep a pointer to it in the Go world so that does not happen.

Fixes #218.
2015-07-26 15:02:40 -07:00
Calin Seciu 64c160f6f2 Find tree entry by id
Add support for 'git_tree_entry_byid'.
2015-07-24 19:52:51 +03:00
Carlos Martín Nieto b4ba35d85c Merge pull request #228 from clns/ref-additions
Reference additions
2015-07-24 14:23:15 +02:00
Carlos Martín Nieto d307391e08 Merge pull request #219 from shurcooL/next-fix-HandleList-Track-gc-issue
[next] Prevent slot int variable from being GCed.
2015-07-24 13:43:19 +02:00
Calin Seciu ec93213f21 Add ReferenceIsValidName() 2015-07-24 12:14:53 +03:00
Calin Seciu 4b88210cbf Add check if reference is a note 2015-07-24 12:14:24 +03:00
Carlos Martín Nieto 28dee704ca Bring back CheckoutForce
This was mistakenly deleted when the SafeCreate was replaced with
RecreateMissing.
2015-07-10 00:31:32 +02:00
Dmitri Shuralyov b5693c1429 Prevent slot int variable from being GCed.
Before this change, there were no users of slot int variable in the Go
world (just a pointer to it that ended up in C world only), so Go's
garbage collector would free it and its value could not retrieved later
(once a pointer to it comes back to Go world from C world).

Keep a pointer to it in the Go world so that does not happen.

Fixes #218.
2015-07-06 19:27:58 -07:00
Carlos Martín Nieto b4ade2b9c6 Merge remote-tracking branch 'upstream/master' into next 2015-07-01 16:07:02 +02:00
Carlos Martín Nieto 4eae20ec27 Adjust style 2015-07-01 16:00:17 +02:00
Carlos Martín Nieto 84275e691f Merge commit 'refs/pull/198/head' of github.com:libgit2/git2go 2015-07-01 15:58:21 +02:00
Carlos Martín Nieto 2475907105 Merge commit 'refs/pull/174/head' of github.com:libgit2/git2go 2015-07-01 15:56:06 +02:00
Carlos Martín Nieto 47191d1274 Submodule: use the Repository constructor 2015-06-30 19:12:39 +02:00
Carlos Martín Nieto b6811196e4 Clone: test we clone something usable and fix constructor
Clone was still trying to do its own initialisation, which was missing
all of the namespacing changes.
2015-06-30 19:08:29 +02:00
Carlos Martín Nieto 66d266f971 Repository: move to use an actual constructor
This should further reduce the changes of the creation of the object
going badly.
2015-06-30 19:03:52 +02:00
Carlos Martín Nieto d478f4b111 Merge pull request #216 from libgit2/diff
Add blob diffing
2015-06-30 10:56:47 +02:00
Carlos Martín Nieto e066d24efb Add DiffBlobs
This lets you diff two arbitrary blobs with arbitrary names.
2015-06-29 21:29:47 +02:00
Carlos Martín Nieto 86e9917919 diff: remove unnecessary args to Hunk and Line ctors 2015-06-29 21:29:47 +02:00
Carlos Martín Nieto 0ce52d9aeb Update to libgit2 fb84cde8 2015-06-28 14:35:57 +02:00
Carlos Martín Nieto 2d6b1ebd4c travis: make the script executable 2015-06-28 13:19:03 +02:00
Carlos Martín Nieto 5f3a9d76b8 travis: don't install libgit2 for 'next' branch
We have our own libgit2 in a submodule, so this is unnecessary. While in
the area, update which version of libgit2 would be downloaded and remove
unnecessary CMake flags.
2015-06-28 13:17:14 +02:00
Carlos Martín Nieto 3115b6c762 travis: allow containerized builds
These builds should be quicker and faster. We don't need sudo, so write
that down for Travis to move us to these builds.
2015-06-28 01:32:59 +02:00
Carlos Martín Nieto e50203a253 Merge remote-tracking branch 'upstream/master' into next
Conflicts:
	branch.go
2015-06-28 01:32:13 +02:00
Carlos Martín Nieto 70c95a7655 Create a NotesCollection for managing notes
As with the others, move these methods into their own namespace.
2015-06-28 01:19:22 +02:00
Carlos Martín Nieto 01a2d8d38d Create a ReferenceCollection for managing references
As with the other commits, this clears up the clutter in naming and
around the Repository's API.
2015-06-28 01:12:32 +02:00
Carlos Martín Nieto d2808d1610 Create a SubmoduleCollection for managing submodules
Similarly to RemoteCollection, this allows us to namespace the submodule
operations much more concisely and removes API on the Repository.
2015-06-28 00:58:31 +02:00
Carlos Martín Nieto 4b9cbd78fd Create a RemoteCollection for managing remotes
Instead of making the 'Remote' part of the function calls, create a
collection object which serves to namespace the operations for the
remotes.
2015-06-28 00:51:17 +02:00
Carlos Martín Nieto ba0a24087a Get rid of Owner() on Remote and Submdoule
These are inherently unsafe. The underlying pointer might get released
at any moment.
2015-06-28 00:49:56 +02:00
Carlos Martín Nieto d400f1d5b2 Update to libgit2 fa39975 2015-06-28 00:34:54 +02:00
Carlos Martín Nieto 2488de286c Merge pull request #212 from libgit2/remote-handle
Make the network code use handles
2015-06-23 13:33:34 +02:00
Carlos Martín Nieto c00a05586b Make the network code use handles
This wasn't ported together with the rest, but it does exhibit the same
issues, so let's port it over now.
2015-06-10 13:37:59 +02:00
Carlos Martín Nieto 53fd8ea011 Merge pull request #211 from shinningstar/master
Free reference resource allocated by libgit2 during go garbage collecting
2015-06-09 11:27:44 +02:00
shinningstar 830e171463 Free reference resource allocated by libgit2 during go garbage collecting 2015-06-08 22:36:31 +08:00
Carlos Martín Nieto aefe85039e Merge pull request #210 from michaeledgar/master
Add error code matching GIT_EAUTH for authentication failures
2015-06-08 05:13:32 +02:00
Carlos Martín Nieto 36e0a256fe Update to libgit2 b6011e29 2015-06-08 04:11:21 +02:00
Carlos Martín Nieto 85fde1fcfb Merge remote-tracking branch 'origin/master' into next 2015-06-08 04:07:49 +02:00
Mike Edgar af7739787c Add error code matching GIT_EAUTH for authentication failures 2015-06-07 19:20:43 -04:00
taylorchu 53c158fbd7 Fix test error messages 2015-05-30 22:27:08 +02:00
Carlos Martín Nieto 35ff0de855 Merge pull request #196 from pks-t/pointer-indirection
[WIP/RFC] Pointer indirection
2015-05-30 22:23:23 +02:00
Patrick Steinhardt e8531dd5c3 diff: only untrack notify payload when it is set 2015-05-22 10:01:50 +02:00
Patrick Steinhardt c43afaf9c4 tree: use correct C callback signature 2015-05-22 09:56:21 +02:00
Patrick Steinhardt 1bd338af5e handles: do not store handles by uintptr
If we store values by uintptrs the GC may try to inspect their
values when it kicks in. As the pointers are most likely invalid,
this will result in an invalid pointer dereference, causing the
program to panic. We can fix this by storing values by an int
index value instead, returning pointers to those indices as
handles instead.
2015-05-22 09:50:16 +02:00
Patrick Steinhardt d95932c84a handles: panic when we cannot retrieve handle data 2015-05-22 09:02:39 +02:00
Patrick Steinhardt a843b7247f packbuilder: use HandleList for C function callbacks. 2015-05-22 09:02:24 +02:00
Patrick Steinhardt 83f9e6a705 blob: use HandleList for C function callbacks. 2015-05-22 09:02:24 +02:00
Patrick Steinhardt fe902f56a8 diff: use HandleList for C function callbacks. 2015-05-22 09:02:24 +02:00
Patrick Steinhardt 7ee534d0c5 handles: print pointer handle on panic. 2015-05-22 09:02:24 +02:00
Patrick Steinhardt e919653755 odb: use HandleList for C function callbacks. 2015-05-22 09:02:24 +02:00
Patrick Steinhardt 9bbec34885 index: use HandleList for C function callbacks. 2015-05-22 09:02:24 +02:00
Patrick Steinhardt 0a336e4abd handles: start slot indices with 1
Using 0 as the first slot indice leads to not being able to
differentiate between a handle to the first element or a
NULL-handle. As current code may check whether the pointer is
NULL, change the first indice to be 1 instead.
2015-05-22 09:02:24 +02:00
Patrick Steinhardt de45a4b8ed submodule: use HandleList for C function callbacks 2015-05-22 09:02:24 +02:00
Patrick Steinhardt be3a626f2e tree: use HandleList for C function callbacks. 2015-05-22 09:02:24 +02:00
Patrick Steinhardt bde012f3d4 handles: correctly initialize all members 2015-05-22 09:02:24 +02:00
Carlos Martín Nieto 7750e85fd1 Introduce an indirection layer for pointers
As the Go runtime can move stacks at any point and the C code runs
concurrently with the rest of the system, we cannot assume that the
payloads we give to the C code will stay valid for any particular
duration.

We must therefore give the C code handles which we can then look up in
our own list when the callbacks get called.
2015-05-22 09:02:24 +02:00
Carlos Martín Nieto 193deb7ae3 Merge pull request #202 from libgit2/index-basics
Add a few basic index operations
2015-05-19 15:21:49 +02:00
Carlos Martín Nieto 72c19f73c9 Index: Add Path() accessor 2015-05-19 15:05:00 +02:00
Carlos Martín Nieto d7a0495000 Index: Add OpenIndex
This lets you persist an index at an arbitrary location.
2015-05-19 14:56:01 +02:00
Carlos Martín Nieto a8ad0d2040 Index: Add ReadTree() 2015-05-19 14:49:05 +02:00
Fernando Oliveira c1df2dcdc1 Add method to check if repo is detached 2015-04-30 23:16:12 -03:00
Carlos Martín Nieto f7781c0e00 Merge pull request #179 from schani/master
Additions
2015-04-27 23:29:49 +02:00
Carlos Martín Nieto 9538c7f750 Merge pull request #197 from pks-t/test-cleanups
tests: always clean up temporary repository dirs
2015-04-24 16:47:42 +02:00
Patrick Steinhardt e300945a3d tests: always clean up temporary repository dirs
Some test repositories are not correctly removed after the tests
did run. Fix by introducing a function that is to be used for
cleaning up temporary test repositories.
2015-04-24 13:01:51 +02:00
Carlos Martín Nieto 1011b03e41 Merge pull request #195 from shurcooL/next-ignore-script
Avoid installing script binary to user's $GOPATH/bin.
2015-04-21 12:32:35 +02:00
Carlos Martín Nieto b374e16db8 Update to libgit2 4c02d3937 2015-04-18 04:50:39 +02:00
Dmitri Shuralyov acc05926f7 Avoid installing script binary to user's $GOPATH/bin. 2015-04-16 18:45:53 -07:00
Carlos Martín Nieto e021457f27 Merge pull request #180 from arcamael/master
Add possibiliy of checkout on specific path
2015-04-03 14:45:38 +02:00
Carlos Martín Nieto 1ba7e13a3a Merge pull request #191 from kron4eg/patch-1
Test on travis using Go1.4
2015-04-03 14:43:11 +02:00
Artiom Di 6454808f69 Test on travis using Go1.4 too 2015-04-03 14:53:15 +03:00
Mark Probst b3e7304abf Add a FIXME. 2015-03-23 12:02:17 -07:00
Mark Probst 524cc7967b Add DiffIndexToWorkdir 2015-03-23 12:02:17 -07:00
Mark Probst 8622831b11 Add DiffTreeToWorkdirWithIndex 2015-03-23 12:02:17 -07:00
Mark Probst 43102043fb Add Commit.Amend 2015-03-23 12:02:16 -07:00
Carlos Martín Nieto a4d5118374 Merge pull request #185 from griffindy/dg-add-reset
Add support for libgit2's git_reset.
2015-03-22 07:25:28 +01:00
Dylan Griffin 89d67328f3 Add support for libgit2's git_reset.
Adds a new method to *Repository called ResetToCommit as well as constants for
the three reset modes that libgit2 currently supports. This does not need to be
limited to Commit, we actually just need something with a gitObject, which blobs
and other Objects have, they will just require different methods. I only need
to be able to reset to commits, so that's all I'm implementing right now.

Also adds a test which updates the test repository README twice and then resets
to the first commit.
2015-03-21 18:37:00 -04:00
Geoffrey Ragot c4b8861b34 Add possibiliy of checkout on specific path 2015-03-15 10:14:29 +01:00
Carlos Martín Nieto c4fce1a218 Update to libgit2 d675982a153
There's been some changes to the checkout strategy, especially the
SAFE_CREATE mode, which is now the RECREATE_MISSING flag, though that
shouldn't be necessary to use in the general case.

The largest changes come from the removal of the signture from
ref-modifying functions/methods and the removal of the reflog string in
all but those directly related to moving references.
2015-03-15 01:49:32 +01:00
Carlos Martín Nieto a2878cf7b2 Merge branch 'v22' 2015-03-15 01:22:58 +01:00
Carlos Martín Nieto 1b44c0a234 Add a bit more on next vs master 2015-03-15 01:21:21 +01:00
Carlos Martín Nieto 050e6fbc49 Correct README on what master tracks
The second mention still said that master tracks master. Add a mention
of next which will become the branch to track upstream tip.
2015-03-15 01:19:32 +01:00
Carlos Martín Nieto 137c4fc3c8 Merge branch 'master' into v22 2015-03-15 01:09:11 +01:00
Carlos Martín Nieto 063bed33a9 Add a call to ldconfig in the travis script
This should help it find the library we just installed.
2015-03-15 01:03:06 +01:00
Carlos Martín Nieto 81d5cc0157 Make travis script install to /usr/local
This is the correct place for software not installed by the system's
package manager.
2015-03-15 00:53:02 +01:00
Carlos Martín Nieto 76d600f7b3 Correct README on what master tracks
The second mention still said that master tracks master. Add a mention
of next which will become the branch to track upstream tip.
2015-03-15 00:46:15 +01:00
Carlos Martín Nieto d300110b85 Merge pull request #178 from schani/master
Fixes and improvements
2015-03-11 17:05:16 +01:00
Mark Probst 9eae50f29a Add commit summary getter 2015-03-04 15:53:00 -08:00
Mark Probst 45d88ca5f4 go fmt 2015-03-04 15:52:59 -08:00
Mark Probst e439b931a6 Default signature 2015-03-04 15:52:57 -08:00
Mark Probst db5fa66b48 State cleanup 2015-03-04 15:52:55 -08:00
Mark Probst c78b4d665e Cherrypick 2015-03-04 15:52:54 -08:00
Mark Probst 56ed0b22d7 Repository state 2015-03-04 15:52:52 -08:00
Mark Probst eec61815fb Fix typos in constants 2015-03-04 15:52:50 -08:00
David Calavera 755721e684 Add BranchIterator#ForEach.
This abstracts the branch iteration from the user.
2015-02-19 11:44:56 +01:00
Matthew Donoughe 675b9b0df9 use v22 travis results 2015-02-13 22:13:57 -05:00
Matthew Donoughe 8bb707b827 return to correct directory for main build 2015-02-13 22:07:54 -05:00
Matthew Donoughe 0d3cc8be8a sudo make install? 2015-02-13 22:00:41 -05:00
Matthew Donoughe 5be2387aeb install libgit2 on travis ci 2015-02-13 21:43:16 -05:00
Matthew Donoughe 43f6a75066 remove static git support files 2015-02-13 21:43:16 -05:00
Matthew Donoughe 51e7cf0ad4 use git_signature_free 2015-02-13 20:44:29 -05:00
Carlos Martín Nieto 94b1f7d07d Merge pull request #173 from AaronO/add/graph-methods
Add bindings for git_graph_* methods
2015-02-13 08:45:14 +01:00
Aaron O'Mullan dddcbb71c4 Remove "Graph" prefix on method names 2015-02-12 18:49:54 +01:00
Aaron O'Mullan c10445cd67 Add bindings for git_graph_* methods
Add graph.go
2015-02-11 12:55:16 +01:00
Carlos Martín Nieto ce8bcbf966 Merge pull request #172 from motemen/fix-test-patch-prefix
Fix test to force diff prefixes.
2015-01-30 14:50:37 +01:00
motemen 3e05c10385 Fix test to force diff prefixes. 2015-01-30 13:44:29 +09:00
Carlos Martín Nieto 2bcbe6bd2d Merge commit '92a1f92d912cdb5f68da8c1e5e3a4d1ebfd282db' 2015-01-29 15:17:54 +01:00
joseferminj 92a1f92d91 Add TargetDirectory field to Checkout options.
TargetDirectory field indicates a alternative checkout path to workdir.
2015-01-29 15:17:46 +01:00
Carlos Martín Nieto 2b17dffc07 Merge pull request #171 from pks-t/submodule-foreach-fix
Submodule foreach fix
2015-01-29 15:15:28 +01:00
Patrick Steinhardt 2e481dbc79 Fix ForeachSubmodule panicing.
As the SubmoduleVisitor function is called from inside libgit2 we
cannot use Go types in its signature. Fix by using C types
instead.
2015-01-28 15:07:32 +01:00
Patrick Steinhardt 1107c6824f Add test triggering ForeachSubmodule panic. 2015-01-28 15:07:27 +01:00
Carlos Martín Nieto 4a17f8038d Merge pull request #169 from tarrant/master
Add Go functions for git_config_find_* functions
2015-01-15 11:13:21 +01:00
Tarrant Rollins 548bb9b5e9 Add Go functions for git_config_find_* functions
ConfigFindGlobal -> git_config_find_global
ConfigFindSystem -> git_config_find_system
ConfigFindXDG    -> git_config_find_xdg
2015-01-14 13:38:35 -08:00
Carlos Martín Nieto 57412d0293 Explain the difference between dynamic and static versions
With the release of libgit2 v0.22 we can link against a version of
library we've wrapped in more than a PoC sense.

Explain the difference and say how to use each version.
2015-01-14 14:11:19 +01:00
Carlos Martín Nieto e8da5affe7 Link dynamically to libgit2
With libgit2 v0.22 released, we can expect its API and ABI to remain
stable when installed on the system.

Linking dynamically allows us to use the go tool alone to build and
install the package.
2015-01-14 13:52:52 +01:00
Carlos Martín Nieto 62272c41c6 Update vendored libgit2 2015-01-12 18:01:43 +01:00
Carlos Martín Nieto 69be70646c Merge pull request #167 from calavera/fix_git_submodules
Fix inconsistent function call in Submodule.
2015-01-09 10:21:20 +01:00
Carlos Martín Nieto d260f21be3 Merge pull request #165 from calavera/remote_prune_refs
Add prune methods to Remote.
2015-01-09 10:17:55 +01:00
Carlos Martín Nieto d54ebd5f5b Merge pull request #166 from benburkert/master
Add git note support
2015-01-09 10:17:19 +01:00
David Calavera ea7f567756 Remove version from opts structure.
It's not necessary.
2015-01-08 15:06:35 -08:00
David Calavera 22f4a4edaa Do not double check if the signature is nil. 2015-01-08 14:21:29 -08:00
David Calavera 9b914e07cc Add Submodule.Update method.
Update libgit2 to a version that includes https://github.com/libgit2/libgit2/pull/2804.
2015-01-08 13:44:58 -08:00
Ben Burkert 04e3c7f6cd
define Note methods on pointers 2015-01-08 11:03:15 -08:00
Ben Burkert 4989fc5a15
Add git note support 2015-01-07 11:56:33 -08:00
David Calavera 8adbc08d70 Fix inconsistent function call in Submodule. 2015-01-06 14:02:44 -08:00
David Calavera dbddb88a8c Add prune methods to Remote. 2015-01-05 11:58:36 -08:00
Carlos Martín Nieto d57246fb74 Update to libgit2 master
This gets rid of the Push object. All network now goes through the
Remote object.
2015-01-04 17:05:11 +00:00
Carlos Martín Nieto dff9badc05 Merge commit '18aea4bfe89b83c5e2d6d55daa68efa6180655cc' 2015-01-04 12:14:12 +00:00
Henning Perl 18aea4bfe8 Add git_diff_get_stats()
This commit adds git_diff_get_stats() as well as functions to query the
stats for insertions, deletions, and changed files.
2015-01-04 12:13:46 +00:00
Carlos Martín Nieto dae3004ca3 Merge pull request #162 from libgit2/revert-159-cgo-directives
Revert "Make the cgo tool do more linking work"
2014-12-31 19:46:38 +00:00
Carlos Martín Nieto ef83908059 Revert "Make the cgo tool do more linking work" 2014-12-31 19:43:20 +00:00
Carlos Martín Nieto db3754ce8d Merge pull request #159 from libgit2/cgo-directives
Make the cgo tool do more linking work
2014-12-30 10:09:54 +00:00
Carlos Martín Nieto 42414248f9 Merge pull request #150 from sqs/DiffOptions_OldPrefix_and_NewPrefix
Diff: heed DiffOptions fields OldPrefix and NewPrefix
2014-12-30 10:09:20 +00:00
Quinn Slack d2a9d7768b heed DiffOptions fields OldPrefix and NewPrefix 2014-12-30 02:03:20 -08:00
Carlos Martín Nieto 8f6e13bd08 Make the cgo tool do more linking work
The cgo directives let us do a lot more than I previously thought, so we
can use this to make the building process of git2go go through the go
tool directly rather than via the script.

libgit2 still needs to be built manually, so we do still require make,
but only for building libgit2. Once that's built, any modifications to
git2go's own code can be built with

    go build
2014-12-27 10:59:19 +00:00
Carlos Martín Nieto 74957c2ae6 Add missing return type 2014-12-18 23:02:53 +00:00
David Calavera d69c771453 Update libgit2
Fix calls to C.git_treebuilder_create and C.git_treebuilder_write.
2014-12-18 22:59:08 +00:00
Carlos Martín Nieto 6f79e6e60b Merge pull request #154 from libgit2/cmn/update
Update to master
2014-12-14 21:57:06 +01:00
Carlos Martín Nieto 0202f152ac Add the new callbacks for Remote.Push()
This unifies the types with the Push struct, in preparation for its
deletion.
2014-12-13 01:23:40 +01:00
Carlos Martín Nieto 63116ea57e Update to master
This deprecates the Push struct in favour of Remote.Push()
2014-12-13 00:25:11 +01:00
Carlos Martín Nieto a9d993f3d1 Remove useless includes 2014-12-11 02:59:07 +01:00
Carlos Martín Nieto cb6201b633 Add missing thread-locking 2014-12-11 02:46:42 +01:00
Carlos Martín Nieto 1198f829b1 Merge pull request #148 from joseferminj/export-patch-from-branches
Export PatchFromBuffers function.
2014-12-11 02:42:29 +01:00
Carlos Martín Nieto 7762e1a3a9 Merge pull request #138 from sqs/check-MakeGitError-thread-lock
Add script for checking thread locks in funcs that call MakeGitError
2014-12-09 00:47:41 +01:00
Quinn Slack 57095bafe7 only check Go source files for non-thread-locked MakeGitError calls 2014-12-08 11:54:04 -08:00
Quinn Slack 3087e610fb add script for checking thread locks in funcs that call MakeGitError 2014-12-08 11:52:35 -08:00
Jose Alvarez db17135a30 Export PatchFromBuffers function.
This change also factor out diffOptionsToC function to remove
duplicated code.
2014-12-06 16:29:25 -05:00
Carlos Martín Nieto 520a0425c7 Add the newer missing thread-locking instances 2014-12-06 03:03:26 +01:00
Carlos Martín Nieto 8c631b0c25 Add missing thread locking 2014-12-06 02:45:26 +01:00
Carlos Martín Nieto 0ec2f46659 Merge pull request #136 from sqs/blame
Add BlameFile func and options
2014-12-06 02:33:56 +01:00
Carlos Martín Nieto 855d8790c4 Merge pull request #141 from jochil/git_diff_find_similar
Integrated git_diff_find_similar
2014-12-06 02:29:02 +01:00
Carlos Martín Nieto 27ce026f1c Merge pull request #140 from AaronO/patch-1
Add wrapper for git_remote_delete : Repository.DeleteRemote
2014-12-06 02:25:02 +01:00
Carlos Martín Nieto 752a4f0497 Merge pull request #146 from stevenwilkin/add-build-status
Add build status to README
2014-12-05 16:41:23 +01:00
Carlos Martín Nieto 1d0a688411 Merge pull request #144 from libgit2/update
Update to master
2014-12-05 11:45:39 +01:00
Steven Wilkin 9b0ba12c34 Add build status to README 2014-12-04 19:51:41 +00:00
Carlos Martín Nieto 1d759e3697 Update to master 2014-12-03 17:26:55 +01:00
Aaron O'Mullan 5b3bc2dd1f Add (*Repository).DeleteRemote 2014-11-26 22:05:21 +01:00
Jochen Hilgers a4ae68783d Integrated git_diff_find_similar 2014-11-26 17:22:15 +01:00
Quinn Slack 3268bdbeb2 add (*Blame).HunkByLine (git_blame_get_hunk_byline) and test 2014-11-18 05:06:03 -08:00
Quinn Slack ebb657ce2f free C string 2014-11-18 04:58:23 -08:00
Quinn Slack 8b39eb7953 lock OS thread when MakeGitError might be called 2014-11-18 04:57:26 -08:00
Quinn Slack 609a9a3cdf omit unnecessary #include 2014-11-18 04:55:10 -08:00
Quinn Slack 65ff78dead Add BlameFile func and options for git-blaming files 2014-11-17 19:44:21 -08:00
Carlos Martín Nieto 1796304374 Merge pull request #135 from joseferminj/diff-tree-workdir
Expose DiffTreeToWorkdir function
2014-11-17 14:42:00 +01:00
joseferminj 1ba38905d1 Expose DiffTreeToWorkdir function 2014-11-13 23:06:45 -05:00
Carlos Martín Nieto 61729f5c93 Merge pull request #134 from joseferminj/fix-merge-trees-nil-ancestor
Fix MergeTrees func to accept nil as ancestor parameter
2014-11-09 20:34:56 +01:00
joseferminj ff65faa082 Fix MergeTrees func to accept nil as ancestor parameter 2014-11-08 19:07:21 -05:00
Carlos Martín Nieto 98299e65fb README: add -d flag to 'go get'
To make sure it doesn't try to build it.
2014-10-31 23:20:57 +01:00
Carlos Martín Nieto 58f6de09f4 Merge pull request #132 from apsdehal/master
Updates README.md to include GoDoc badge.
2014-10-30 23:39:01 +01:00
Amanpreet Singh fd312d354f Updates README.md to include GoDoc badge. 2014-10-28 23:10:35 +05:30
Carlos Martín Nieto ccfce74e18 Merge pull request #129 from libgit2/cmn/const-type
Make the constants have types
2014-10-28 11:59:04 +01:00
Carlos Martín Nieto 668aa5dae1 Make the constants have types
While Go will assign the correct type to a const block when it
auto-creates the values, assigning makes the const be typeless and will
only gain it in each particular use.

Make each constant in the blocks have an assigned type.
2014-10-28 11:29:31 +01:00
Carlos Martín Nieto 9c6db70fc2 Merge pull request #127 from calavera/ls_remote
Add Remote#Ls.
2014-10-27 17:42:28 +01:00
David Calavera d722c11f7f Hide C.git_direction type. 2014-10-27 09:08:05 -07:00
David Calavera e969b33b33 Make filtering logic more simple. 2014-10-27 08:32:50 -07:00
David Calavera d1b87efd96 Add connect methods to Remote. 2014-10-27 08:29:42 -07:00
David Calavera 749d6149b3 Merge branch 'master' into ls_remote
* master:
  Update libgit2 submodule.
  Remove Config#Refresh
  implemented Index.AddAll, Index.RemoveAll, Index.UpdateAll
2014-10-27 07:25:57 -07:00
Carlos Martín Nieto 99d10775d6 Merge pull request #125 from lucas-clemente/master
implemented Index.AddAll as git_index_add_all wrapper
2014-10-25 18:11:52 +02:00
Carlos Martín Nieto a2fd47aad2 Merge pull request #128 from calavera/remote_git_config_refresh_call
Remove Config#Refresh
2014-10-25 18:07:33 +02:00
David Calavera ea54434f90 Update libgit2 submodule. 2014-10-25 06:59:40 -07:00
David Calavera f6fa1a38ab Add Remote#Ls.
Allow filtering heads by name.
2014-10-24 17:28:10 -07:00
David Calavera 5c72fe54b8 Remove Config#Refresh
Because it has been removed from libgit2:
55cb499972
2014-10-24 17:22:46 -07:00
Lucas Clemente 9d37f81764 implemented Index.AddAll, Index.RemoveAll, Index.UpdateAll 2014-10-24 21:55:08 +02:00
Carlos Martín Nieto 41008af54c Merge pull request #123 from ry/master
Use Filemode type in TreeEntry and IndexEntry
2014-10-23 19:17:23 +02:00
Carlos Martín Nieto 0af2a39b4e Update to latest master 2014-10-23 18:51:51 +02:00
Ryan Dahl 5539137e9a Use Filemode type in TreeEntry and IndexEntry
Fixes #121
2014-10-20 11:58:55 -04:00
Carlos Martín Nieto 3b7cc1e97e remote: use the library's certificate validity if no callback is set
We should not return 0, as in this case that means we let it through,
return an appropriate error instead.
2014-10-19 14:38:00 +02:00
Carlos Martín Nieto 68bd3bbdd7 Fix header name
It turns out that some systems have a strings.h which includes string.h,
which does not happen on GNU systems.
2014-10-15 17:10:35 +02:00
Carlos Martín Nieto c77c8f6fb9 Add a test for blobs
This has been in my worktree unadded for a while.
2014-10-15 16:59:19 +02:00
Carlos Martín Nieto d9dfc4bce8 Add support for hostkey certificates
While they're not exactly certificates, they belong in the same
category.
2014-10-15 16:57:32 +02:00
Carlos Martín Nieto a81abd10ca Merge branch 'fetchhead'
Conflicts:
	remote.go
2014-10-15 15:59:08 +02:00
Jess Sheneberger d917a13aca add plumbing for update fetch head on remotes 2014-10-15 15:58:33 +02:00
Carlos Martín Nieto 17a9214307 Update to libgit2 master
The option to ignore the server's certificate has been removed, replaced
witha  callback for the user to perform their own checking.

Remote.Fetch() now performs opportunistic updates and takes a list of
refspecs to use as the active set for a particular fetch.
2014-10-15 15:56:59 +02:00
Carlos Martín Nieto 5eda8d6935 Merge pull request #109 from roguePanda/git_status
Address issue #108
2014-09-11 09:16:39 +02:00
Carlos Martín Nieto c68241c3f0 Merge pull request #117 from piger/dev
add Index#RemoveByPath.
2014-09-09 10:51:44 +02:00
Daniel Kertesz c2f6381252 add Index#RemoveByPath. 2014-09-07 12:45:52 +02:00
Carlos Martín Nieto 756c8a7e8b Merge pull request #116 from calavera/sync_with_libgit2_master
Sync with libgit2 master.
2014-09-04 17:10:37 +02:00
David Calavera ced242954c Update vendored libgit2 version to master. 2014-09-04 07:57:10 -07:00
Ben Navetta d4734a41d5 remove unused status version field 2014-09-04 10:17:31 -04:00
Ben Navetta 1520978dcd give status option flags their own type 2014-09-04 10:15:13 -04:00
Carlos Martín Nieto db113288b3 Merge pull request #113 from CMGS/master
enhance checkout, add CheckoutTree method
2014-09-04 16:02:21 +02:00
Carlos Martín Nieto cea203d01c Merge pull request #115 from surma-dump/feature/empty_refname
Repository.CreateCommit: Allow empty refname for non-update commit
2014-09-04 14:38:36 +02:00
Alexander Surma d196da40ed Repository.CreateCommit: Only allocate CStr on non-empty refname 2014-09-04 09:57:54 +02:00
David Calavera 9bec36a0b0 Update clone options to be in sync with libgit2 master.
Remove missing constants.
2014-09-03 18:08:48 -07:00
Alexander Surma fe452620c6 Repository.CreateCommit: Allow empty refname for non-update commit 2014-09-01 19:27:44 +02:00
CMGS 2f93ce39cc embrace static types 2014-08-28 15:40:21 +08:00
CMGS e5e9636ce0 enhance checkout, add CheckoutTree method 2014-08-27 17:03:21 +08:00
Carlos Martín Nieto 84597241e0 Exit when pkg-config isn't found
We run pkg-config in a subshell, so our 'set -e' does not take effect
there. Explicitly error out if there was an error running pkg-config.
2014-08-26 23:32:27 +02:00
Carlos Martín Nieto 0f79d63fd5 Merge calvera/rev-parse 2014-08-26 12:35:17 +02:00
Carlos Martín Nieto 0d58580249 Remove unnecessary heap allocation 2014-08-26 12:35:05 +02:00
Ben Navetta 80997c6fa5 fix status list to handle null head_to_index in entries 2014-08-25 23:18:00 -04:00
Ben Navetta 0059b26255 add thread locking to status api 2014-08-25 18:20:54 -04:00
Ben Navetta 33ae83f4d9 remove status_foreach binding 2014-08-25 18:15:36 -04:00
Ben Navetta c8529e79da don't return anything from StatusList.Free 2014-08-25 17:41:35 -04:00
Carlos Martín Nieto 1377c8f3d2 Update vendored libgit2 version to v0.21.1 2014-08-25 15:45:40 +02:00
Carlos Martín Nieto 106ebe2fc4 Handle empty slices in CreateBlobFromBuffer()
Go won't let us access the zeroth alement of an empty slice, so we need
to figure out if the length is zero and take special action.

This fixes #106.
2014-08-25 15:44:01 +02:00
Carlos Martín Nieto 91fd186186 Merge pull request #105 from mattes/patch-1
Make sure install/lib dir exists. Fixes #104
2014-08-25 15:26:33 +02:00
David Calavera 29840d28ad Extract data into a go struct. 2014-08-22 22:36:18 -07:00
Ben Navetta 1cb654e4f2 add git_status_foreach binding 2014-08-19 08:51:18 -04:00
Ben Navetta fe1e6b83ed comment out issue with entry count 2014-08-19 08:08:46 -04:00
Ben Navetta 8fd7c2c609 add StatusFile function 2014-08-18 23:12:45 -04:00
Ben Navetta a093e20a88 add status option support 2014-08-18 22:58:53 -04:00
Ben Navetta f954871968 start on status tests; fix bug in Repository.StatusList() 2014-08-18 22:19:06 -04:00
Ben Navetta b831ae04aa add StatusList() to Repository 2014-08-18 20:34:57 -04:00
Ben Navetta 0513670745 add StatusList.EntryCount() 2014-08-18 20:11:14 -04:00
Ben Navetta 37ccc4c00d add ByIndex, reorder to match other files 2014-08-18 20:04:32 -04:00
Ben Navetta 39d825a2a8 status data types 2014-08-18 19:42:34 -04:00
Matthias Kadenbach c1ed1bc545 Make sure install/lib dir exists. Fixes #104 2014-08-06 04:07:15 +02:00
David Calavera 94d207e11c Add Repository#RevParseExt. 2014-07-24 21:03:50 -07:00
David Calavera 691d8f23ba Add Repository#RevParseSingle. 2014-07-24 17:41:10 -07:00
David Calavera 43b6e0b154 Make a new pointer to pass to git_revparse. 2014-07-24 17:20:22 -07:00
David Calavera 1e651aaa22 Add simple test for `rev-parse HEAD`. 2014-07-24 17:08:58 -07:00
David Calavera 8bbcdd65a6 Move RevSpec#From and RevSpec#To to struct methods. 2014-07-24 17:02:14 -07:00
David Calavera 3003e5e190 Add Repository#RevParse. 2014-07-24 15:37:59 -07:00
Carlos Martín Nieto 213a1bc876 Merge pull request #101 from bargez/patch-1
Fix a typo in git2go path
2014-07-12 21:20:23 +02:00
Bartosz Gęza f48c27e43c Fix a typo in git2go path 2014-07-12 20:49:50 +02:00
Carlos Martín Nieto ce7a12da68 travis: build with Go 1.3 2014-07-03 08:48:41 +02:00
Carlos Martín Nieto c7ac950815 Update README
Specify that we stick to the release and how to run the tests.
2014-07-03 08:47:38 +02:00
Carlos Martín Nieto d117fc9aa8 Merge commit 'refs/pull/95/head' of github.com:libgit2/git2go 2014-07-03 08:37:23 +02:00
Carlos Martín Nieto ae5efcda9b Update vendored libgit2 to v0.21.0 2014-07-03 08:36:04 +02:00
Frank Benkstein b3306bee41 fix indentation 2014-06-11 19:55:24 +02:00
Frank Benkstein bbdc7a825d add support for annotated tags 2014-06-09 23:19:17 +02:00
Carlos Martín Nieto bc5cfaffcb Remove left-over submodule configuration 2014-06-07 19:29:45 +02:00
Carlos Martín Nieto 10c058dcb9 Upate README with static instructions 2014-06-07 19:22:09 +02:00
Carlos Martín Nieto 4b681f9207 Don't build with Go 1.0
Its cgo linker doesn't support statically linking C libraries.
2014-06-07 19:14:11 +02:00
Carlos Martín Nieto a2b14b5be4 Tighten up the build
Build as release, and there is no need to install the library, we know
where the files are.
2014-06-07 19:12:33 +02:00
Carlos Martín Nieto 12a3a1e05c Move the settings into the main git2go 2014-06-07 18:57:46 +02:00
Carlos Martín Nieto aabeb7f585 Really build statically 2014-06-07 15:43:05 +02:00
Carlos Martín Nieto 6862c2c82d hack for travis 2014-06-07 14:07:15 +02:00
Carlos Martín Nieto 3da96c3dd7 Adjust travis script to static 2014-06-07 03:27:00 +02:00
Carlos Martín Nieto c734fc2a6b Use a submodule for libgit2
This makes building the static version easier as we know where the repo
is and that it's has a known-good version.
2014-06-07 03:03:04 +02:00
Carlos Martín Nieto 0646294282 Move some logic into the Makefile
This should provide a nice compromise between the scripts and comon
commands.
2014-06-03 18:35:09 +02:00
Carlos Martín Nieto 29a983b68f Switch default to static linking
Build in libgit2 statically into git2go by default, removing the need
for the right version to be available as a shared object.

We do still need to link dynamically against OpenSSL and LibSSH2.
2014-06-03 17:47:53 +02:00
Carlos Martín Nieto de4f42f476 Add scripts to build and use a static libgit2 2014-06-03 12:03:00 +02:00
Carlos Martín Nieto 3ca566e105 Update travis settings
Build on 1.2 and allow tip to fail. These failures sometimes are our
fault, sometimes go's.
2014-05-31 16:51:51 +02:00
Carlos Martín Nieto 7906bdbdce Merge: merge analysis now returns the user's preference 2014-05-31 16:47:35 +02:00
Carlos Martín Nieto 8a73c75f1a Keep a pointer to the repository in the objects and references
Otherwise, the garbage collector might decide it's a good idea to throw
away the repository instance while the C object still has a pointer to
it. Hilarity ensues.
2014-05-26 09:28:07 +02:00
Carlos Martín Nieto f953d4e5c7 Index: add functions to handle the data structure
Index is not just the index file
2014-05-25 18:12:50 +02:00
Carlos Martín Nieto 2942e18d05 Give Object and Reference an Onwer accessor
This reduces the need to carry around a pointer to the repository as
well as the objects.
2014-05-25 09:12:10 +02:00
Carlos Martín Nieto ec97cb4473 Merge branch 'repo-ext' 2014-05-23 16:02:36 +02:00
cloudson 591a67fef8 Add function to open repository from subpaths 2014-05-23 16:02:30 +02:00
Carlos Martín Nieto 22bcd7decf Reference: expose Peel()
Peel a reference to a particular type of object, returning an Object,
which you then perform a type assertion on.
2014-05-23 11:37:00 +02:00
Carlos Martín Nieto f5e1252d6e Merge pull request #90 from libgit2/unchanify
Remove usage of channels for `ForEach`
2014-05-07 15:12:00 +02:00
Carlos Martín Nieto 7e3c361ac4 Packbuilder: use a callback for ForEach instead of a channel
Channels provide no means to report an error. Closing a channel could
mean anything.

This is particularly important when dealing with IO, which we do quite
often in the pack builder. Use ForEach which returns an error instead.
2014-05-06 14:43:38 +02:00
Carlos Martín Nieto 2594f3f889 Odb: use a callback instead of a channel for ForEach
A channel provides no way to specify whether we stopped sending data
because of an error or because there is no more data.

Therefore, make Odb.ForEach() take a callback with which the user is free to
do whatever they need, letting us return en error.
2014-05-06 14:19:34 +02:00
Carlos Martín Nieto 9a8b80fc13 Fix typo 2014-05-04 15:15:37 +02:00
Carlos Martín Nieto 645a35dfb1 Merge pull request #89 from tchap/set-head
Implement git_repository_set_head(_detached)
2014-05-03 19:20:34 +02:00
Carlos Martín Nieto 9b4c865f18 test: adjust to safe commit append
When we create a commit and tell the library to update a ref, we need to
make sure to pass the current tip.

This is what we should have been doing in a function called
'upateReadme()' anyway. The existing code creates a new root commit,
which is not an update.
2014-05-03 19:13:32 +02:00
Carlos Martín Nieto 9c72700765 checkout: init_opts -> init_options 2014-05-03 18:56:44 +02:00
Ondrej Kupka 2cf19370bc Implement git_repository_set_head(_detached)
This closes #88

Signed-off-by: Ondrej Kupka <ondra.cap@gmail.com>
2014-05-03 13:55:25 +02:00
Carlos Martín Nieto 5809f03108 Merge commit 'refs/pull/72/head' of github.com:libgit2/git2go
Conflicts:
	git.go
	wrapper.c
2014-04-26 20:51:21 +02:00
Carlos Martín Nieto 4df7eb516c Merge pull request #63 from jezell/jezell/merge
Merge functions (in progress)
2014-04-26 20:42:18 +02:00
Carlos Martín Nieto 57f14a2591 Merge commit 'refs/pull/48/head' of github.com:libgit2/git2go 2014-04-26 20:30:57 +02:00
Carlos Martín Nieto 605f942e94 Merge pull request #66 from jezell/branch-iterator
Add branch iterator
2014-04-26 20:25:26 +02:00
Carlos Martín Nieto 3cf0b6db80 Merge pull request #80 from jezell/oid-parse-protection
make it possible to handle errors
2014-04-26 20:24:11 +02:00
Carlos Martín Nieto dca2192492 Merge pull request #81 from jezell/remote-set-callbacks
add remote functions to set callbacks / set cert check
2014-04-26 20:22:44 +02:00
Carlos Martín Nieto b3a160b0f8 Adjust to libgit2 dev changes
This fixes #87
2014-04-26 18:43:22 +02:00
cloudson 80ad996dc1 Recursive find respository 2014-04-17 08:33:22 -03:00
Jesse Ezell 7cf6b4f082 add set callbacks / set cert check 2014-04-04 09:51:46 -07:00
Jesse Ezell 63fd1f9b03 use cast_ptr instead of ptr for travis go tip build 2014-04-04 09:42:17 -07:00
Jesse Ezell a7d3c5955a merge with improved error handling logic 2014-04-04 00:56:58 -07:00
Jesse Ezell fc999289a2 Merge branch 'oid-parse-protection' into branch-iterator 2014-04-04 00:50:41 -07:00
Jesse Ezell 9d8cbe7547 use cast_ptr instead of ptr for travis go tip build 2014-04-04 00:47:21 -07:00
Jesse Ezell b00cb1a343 Merge remote-tracking branch 'libgit/master' into add-basic-diff-patch 2014-04-04 00:30:05 -07:00
Jesse Ezell 39f59d921b fix typo / return name iterator 2014-04-04 00:27:07 -07:00
Jesse Ezell 8319a792f3 Merge remote-tracking branch 'libgit/master' into branch-iterator 2014-04-04 00:26:22 -07:00
Jesse Ezell 864c57f554 merge latest, copy merge bytes to go array 2014-04-04 00:22:42 -07:00
Jesse Ezell 8982f4b3be adjust comments on error messages to standard godoc style 2014-04-04 00:14:31 -07:00
Aidan Nulman cbc81246af fix for travisci 2014-04-03 17:12:31 -04:00
Aidan Nulman fc70808cb7 update for upstream changes 2014-04-03 16:49:22 -04:00
Aidan Nulman d9f4adff6c Merge branch 'master' into custom_odb
Conflicts:
	odb.go
	wrapper.c
2014-04-03 16:41:43 -04:00
Aidan Nulman b5e60dc106 explicit returns 2014-04-03 16:39:21 -04:00
Jesse Ezell b660db0a4b make it possible to handle errors 2014-04-02 10:31:48 -07:00
Carlos Martín Nieto 9cd1d129bc Remote: The whole point of the anonymous change
Was that it would break and we'd remember that the order changed. Oh
well.
2014-04-01 20:10:20 +02:00
Carlos Martín Nieto c8ff10933b Merge pull request #77 from jezell/inmemory-to-anon
rename inmemory to anonymous remote
2014-04-01 20:07:58 +02:00
Jesse Ezell 9d0d814f19 rename inmemory to anonymous remote 2014-04-01 11:06:47 -07:00
Carlos Martín Nieto fa41ce8575 Merge pull request #73 from libgit2/cmn/settings
Add a settings package
2014-04-01 12:43:28 +02:00
Carlos Martín Nieto 1b4962cafe Merge pull request #75 from jezell/add-conflict-to-index
Add index conflict functions
2014-04-01 12:43:07 +02:00
Carlos Martín Nieto a06f4a030a Adjust to Go tip changes
It does not like breaking aliasing rules, so let's keep a casted pointer
for when libgit2 wants that.
2014-04-01 12:36:44 +02:00
Carlos Martín Nieto 286ff62b14 Merge pull request #76 from jezell/walk-enhancements
Add missing walk functions
2014-04-01 12:00:20 +02:00
Carlos Martín Nieto 5adb756c48 Merge pull request #74 from fd/master
Added force argument for (*Submodule).Reload() and (*Repository).ReloadAllSubmodules()
2014-03-31 17:08:00 +02:00
Jesse Ezell dcdf2c3555 add conflict functions 2014-03-30 19:53:07 -07:00
Jesse Ezell 552557ba51 add missing walk functions 2014-03-30 13:23:03 -07:00
Simon Menke 429408dbe5 Added force argument for (*Submodule).Reload() and (*Repository).ReloadAllSubmodules() 2014-03-27 11:32:22 +01:00
Jesse Ezell 0b48ba2224 Merge analysis and merge file 2014-03-26 14:42:01 -07:00
Jesse Ezell 4553b3ada6 fix go 1.0 compile error 2014-03-26 12:20:54 -07:00
Jesse Ezell eff3a8b4d0 support nil merge options 2014-03-26 12:17:23 -07:00
Jesse Ezell 50a3c4aa09 update to new merge API 2014-03-26 11:28:48 -07:00
Jesse Ezell 85420f2002 Merge branch 'master' of http://github.com/libgit2/git2go into merge 2014-03-26 11:18:21 -07:00
Carlos Martín Nieto b2a2a279d9 Add a settings package
This lets us modify the libgit2-wide options/settings.
2014-03-25 09:27:05 +01:00
Jesse Ezell 155f641683 don't expose 3 different diff foreach methods. use structures instead of pointers to structures for diff detail. add patch error code handling. trim excess data from diff structures. 2014-03-21 22:51:38 -07:00
Jesse Ezell f85c38ce22 Allow diff.ForEach to enumerate files, hunks, and lines with single call. Support use of closures for enumeration. 2014-03-21 17:20:48 -07:00
Jesse Ezell aea899e877 set ptr to nil after free 2014-03-20 23:19:22 -07:00
Jesse Ezell d78036fe24 refactor and cleanup code 2014-03-20 22:54:18 -07:00
Jesse Ezell 37964e878f merge with latest 2014-03-20 22:02:19 -07:00
Jesse Ezell d0b334b244 cleanup and refactor diff / patch 2014-03-20 21:56:41 -07:00
Carlos Martín Nieto 2811845a12 Merge pull request #71 from jezell/add-index-write-tree-to
Add index WriteTreeTo + test
2014-03-21 05:29:57 +01:00
Carlos Martín Nieto b869775665 Merge pull request #70 from jezell/add-remote-list
Add git_remote_list + test
2014-03-21 05:29:05 +01:00
Jesse Ezell 1f3f8adda8 Add index WriteTreeTo + test 2014-03-20 20:49:05 -07:00
Jesse Ezell 5d8db7f936 return nil instead of empty array on error 2014-03-20 20:32:23 -07:00
Jesse Ezell 99d7f66477 add remote list 2014-03-20 20:32:15 -07:00
Jesse Ezell 5590078e6f remove channel based iteration for branch / ref. Add ReferenceNameIterator. All iterators use Next(). Remove interfaces. 2014-03-20 20:28:41 -07:00
Jesse Ezell 37b950bc90 various improvements to interface 2014-03-19 20:24:19 -07:00
Carlos Martín Nieto 574f0dd12d Remote: remove Get prefix from refspecs
Idiomatic Go is to omit the Get from the getter methods.
2014-03-20 03:29:54 +01:00
Jesse Ezell 27bea93efe split out name iterator 2014-03-19 00:36:00 -07:00
Jesse Ezell 006286edb7 remove Branch struct, unify reference iterators 2014-03-19 00:19:02 -07:00
Carlos Martín Nieto 3ae9813fca Clean up after the tests 2014-03-19 08:15:19 +01:00
Carlos Martín Nieto ad128bdefb Remote: don't mix allocators
We cannot ask libgit2 to free the memory we have allocated ourselves, as
it cannot know how to do it. Let's free the strarray ourselves.
2014-03-19 08:15:18 +01:00
Carlos Martín Nieto 3274d477c9 Merge pull request #68 from libgit2/cmn/oid-revamp
Oid revamp
2014-03-19 07:20:45 +01:00
Carlos Martín Nieto b82a72a9ce Oid: fix IsZero()
We need to compare against the number zero, not its ASCII value.
2014-03-19 03:58:02 +01:00
Carlos Martín Nieto 0bb73e43a8 Oid: use Go's conversion functions
Go already has all the necessary pieces for encoding and decoding hex
strings. Using them let's us avoid going into C land.

Benchmarks show this takes about half the time as using libgit2's
functions.
2014-03-19 03:57:36 +01:00
Carlos Martín Nieto c243c31f7d Oid: remove Bytes()
This is not needed. We can do id[:] to get a slice.
2014-03-19 03:56:50 +01:00
Carlos Martín Nieto c9c7c1e779 Oid: make NewOid take a string
This is the most common way of having an id that's not in Oid form, so
let's make it the "default" and rename to NewOidFromBytes() the one that
takes []byte.
2014-03-19 03:56:50 +01:00
Carlos Martín Nieto b6703d4767 Oid: make the type directly [20]byte
There is no need for a struct with a single field. An Oid is 20 bytes
which hold the binary representation of the hash, so let's use that
directly. Go lets us have methods on this new type just the same.
2014-03-19 03:56:50 +01:00
Carlos Martín Nieto b5ce60925e Merge pull request #69 from jezell/clone_opts_init_fix
Clone opts init fix
2014-03-19 03:56:38 +01:00
Jesse Ezell f1f0fa7335 fix naming on test 2014-03-18 19:38:02 -07:00
Jesse Ezell 3d7f737481 add simple clone test 2014-03-18 18:24:31 -07:00
Jesse Ezell d1e7ee53d5 fix clone options init 2014-03-18 18:23:33 -07:00
Carlos Martín Nieto 5f35f13737 Merge pull request #61 from jezell/remotes-wip
Cleaned up remotes / clone / add push / fetch
2014-03-18 05:21:35 +01:00
Carlos Martín Nieto dbdbb4b0d1 Merge pull request #67 from jezell/chunk-create-fix
fix chunk create return values
2014-03-17 16:04:05 +01:00
Jesse Ezell 663c2a69c9 fix chunk create logic 2014-03-16 22:09:12 -07:00
Jesse Ezell 5f01bd7abd add branch iterator / remove useless repo from reference iterator 2014-03-12 15:49:11 -07:00
Jesse Ezell 51aa76d6f7 remove strarray wrappers 2014-03-11 16:25:22 -07:00
Jesse Ezell 0a172478dc fix return for old go versions / travis 2014-03-11 13:45:27 -07:00
Jesse Ezell 2f53196866 clean up clone code 2014-03-11 13:22:00 -07:00
Jesse Ezell d560b9e9bd cleanup clone code 2014-03-11 13:19:12 -07:00
Jesse Ezell 634acbe498 merge with latest 2014-03-11 12:55:57 -07:00
Jesse Ezell 8ad5cbc537 Merge branch 'merge' of https://github.com/jezell/git2go into merge 2014-03-11 12:49:29 -07:00
Jesse Ezell 6a068d5265 remove useless wrappers 2014-03-11 12:47:56 -07:00
Jesse Ezell 86efca0630 Merge branch 'jezell/merge' into merge 2014-03-11 12:42:18 -07:00
Jesse Ezell 7cbbeff7ac merge with latest 2014-03-11 12:42:08 -07:00
Jesse Ezell f1e889928a merge with latest, replace merge wrappers with go code 2014-03-11 12:29:40 -07:00
Carlos Martín Nieto 1cf8117814 Merge pull request #65 from jezell/blob_and_tree_updates
Minor API enhancements
2014-03-11 03:44:12 +01:00
Carlos Martín Nieto ea909d8518 Merge branch 'cmn/checkout-opts' 2014-03-11 03:36:28 +01:00
Carlos Martín Nieto 263884a908 CheckoutIndex: allow for index to be nil
Allow for the index to be nil and pass that to the library to use the
repository's index.
2014-03-11 03:30:56 +01:00
Carlos Martín Nieto b09c6d8bbe Move checkout functions options more in line with the library
Afjust Checkout -> CheckoutHead and pass nil if the options structure is
nil so as not to overide the library's decisions.
2014-03-11 03:27:35 +01:00
Carlos Martín Nieto b5b0f3f50e Remove custom checkout opts init function 2014-03-11 03:14:36 +01:00
Carlos Martín Nieto 42fce02197 Adjust to checkout_opts -> checkout_options 2014-03-11 03:09:48 +01:00
Carlos Martín Nieto 731f978b54 Merge pull request #58 from jezell/add-branch-lookup
Add branch functions
2014-03-11 02:59:33 +01:00
Jesse Ezell 5e163fa2e8 add blob chunk creation, creation of tree builders for specific trees, minor API cleanup 2014-03-07 16:43:20 -08:00
Jesse Ezell e5946d4009 Add defaults, add simple test, merge heads should be pointer array 2014-02-28 14:33:38 -08:00
Jesse Ezell 6af4d4a825 move finalizer above free 2014-02-28 11:15:39 -08:00
Jesse Ezell da64faf8bd switch from iota to explicit def 2014-02-28 11:13:51 -08:00
Jesse Ezell 127643eb54 move return outside of switch for go 1.0 / travis 2014-02-28 11:08:15 -08:00
Jesse Ezell 9fb7a746e0 fix handling of msg to treat empty str as nil 2014-02-28 10:58:53 -08:00
Jesse Ezell d6332f9526 fix msg handling to treat empty str as nil 2014-02-28 10:54:16 -08:00
Jesse Ezell b404c8b862 Remove unused consts 2014-02-28 10:47:56 -08:00
Jesse Ezell 2c56324ca5 fix bad git_buf handling 2014-02-28 10:46:57 -08:00
Carlos Martín Nieto f5f8e13744 Add a travis script
Add a build script and ask Travis to run it. It downloads the tip of
libgit2's dev branch and tests against that.
2014-02-28 15:15:32 +01:00
Carlos Martín Nieto 639b66345c Fix an old error function call that snuck in 2014-02-28 14:11:21 +01:00
Jesse Ezell 5f5906878e merge options / merge tree options 2014-02-28 01:31:01 -08:00
Jesse Ezell 9e7ba02708 wrap merge functions 2014-02-28 01:06:41 -08:00
Jesse Ezell 0497d2b114 fix bad merge (LastError -> MakeGitError) 2014-02-28 00:21:48 -08:00
Jesse Ezell 374e2112df add push, refine remotes 2014-02-27 16:36:44 -08:00
Carlos Martín Nieto fc6d0fbfff Merge pull request #59 from libgit2/cmn/refs
Add a few reference utility functions
2014-02-27 17:03:39 +01:00
Carlos Martín Nieto c431fe3c2c Merge pull request #60 from libgit2/cmn/reflogs-default
Allow for a default in reflog messages
2014-02-27 17:03:20 +01:00
Aidan Nulman 9299214e57 re-encapsulate repository 2014-02-26 23:44:32 -05:00
lye 9acd67e388 Actually type constants; unwrap DiffFile, DiffDelta, DiffHunk. 2014-02-26 16:02:26 -06:00
Jesse Ezell e2db9b16cd merge latest, cleanup error handling, add thread locks 2014-02-26 10:41:20 -08:00
Jesse Ezell a5df611100 LastError -> MakeGitError 2014-02-26 08:50:47 -08:00
Jesse Ezell baf4a84336 Merge branch 'master' of https://github.com/libgit2/git2go into add-branch-lookup 2014-02-26 08:45:44 -08:00
Jesse Ezell fe509411a5 Add thread locking 2014-02-26 08:45:38 -08:00
Jesse Ezell a728f70358 cleanup add-branch 2014-02-26 07:33:50 -08:00
Jesse Ezell 6a8e126cb9 Merge branch 'branch_functions' of http://github.com/JohannWeging/git2go into merge-add-branch-lookup 2014-02-26 07:32:16 -08:00
Carlos Martín Nieto 1c1f7bd1fa Merge branch 'more-errors' 2014-02-26 16:15:10 +01:00
Carlos Martín Nieto 00ea11691b Convert the rest of the errors 2014-02-26 16:14:31 +01:00
Jesper Hansen 499f52a354 Added git error code to the error object. 2014-02-26 16:10:00 +01:00
Carlos Martín Nieto 3e5586bd8d Remove 'oid' as id name
Following the cleanup from libgit2, let's not use 'oid' unless we mean
the name of the data type. In the other cases, we mean an identifier,
hence the name 'id'.
2014-02-26 15:30:16 +01:00
Carlos Martín Nieto 5f4283fac3 Merge branch 'index-entries' 2014-02-26 15:22:53 +01:00
Carlos Martín Nieto 14f902afed Adjust to oid -> id 2014-02-26 15:22:48 +01:00
Carlos Martín Nieto 1e01cae286 Remove pointer to git_index_entry
We have all the data
2014-02-26 15:19:07 +01:00
Carlos Martín Nieto 2c8de242ee Allow for a default in reflog messages
We don't have a way to represent a NULL string, so if the user passes an
empty string, let's pass NULL down so we tell libgit2 to use the default.
2014-02-26 15:01:23 +01:00
Carlos Martín Nieto ca2c3c6db2 Add a few reference utility functions 2014-02-26 14:51:04 +01:00
lye 45b0f17c04 Bundle consts more idiomatically; add GitLineType alias. 2014-02-26 05:58:45 -06:00
lye 2e5102b71a Unwrap DiffLine; add types for git_diff_flag_t and git_delta_t. 2014-02-26 04:36:04 -06:00
Carlos Martín Nieto 786393a380 Merge pull request #42 from libgit2/cmn/config
Wrap some more config functions
2014-02-26 10:37:35 +01:00
Carlos Martín Nieto fc0a2f56e8 Lock the thread so we can get the error message 2014-02-26 10:37:02 +01:00
Carlos Martín Nieto af2446b1da Add iterators and ConfigEntry 2014-02-26 10:37:01 +01:00
Carlos Martín Nieto 129105d410 Add a few more missing config functions 2014-02-26 10:36:35 +01:00
Carlos Martín Nieto fbd8698002 Add a few missing config setters and getters 2014-02-26 10:30:15 +01:00
Aidan Nulman 2656a72e82 Merge branch 'master' into custom_odb
Conflicts:
	git.go
	reference.go
	repository.go
	submodule.go
2014-02-24 03:05:44 -05:00
Aidan Nulman decaf064f9 add custom refdb backend support 2014-02-24 03:01:47 -05:00
lye 48a02566d6 Pre-emptively copy data when marshalling diff callback data.
When marshalling diff callback data to Go structs, any `char*` need to
be pre-emptively copied onto Go's heap as they're invalidated as soon as
our callback function returns. This patch adds this extra copy before
sending the value to the channel, which fixes a bug wherein
`DiffLine.Content`, `DiffFile.Path` and `DiffHunk.Header` would
previously return garbage data.
2014-02-23 18:51:56 -06:00
lye 375168abdc Export the constants and enumerations for diffs.
It is possible that the typed enums from libgit2 should be exported as
distinct types rather than numeric constants (e.g, `git_delta_t` and
`git_line_t` should be typed), but this has not been done in this patch.
2014-02-23 18:06:04 -06:00
Carlos Martín Nieto c6d1bde37c Return SubmoduleRecurse 2014-02-23 16:08:19 +01:00
Carlos Martín Nieto 0a1052b0e0 Merge pull request #52 from ursachec/master
Support for git_odb_hash.
2014-02-23 16:02:35 +01:00
Carlos Martín Nieto af80cc73ad Merge pull request #51 from Tobscher/master
added write method to index
2014-02-23 15:49:31 +01:00
Carlos Martín Nieto 8efcdd0e16 Merge pull request #55 from lye/add-is-x-reference-methods
Add Is$Type methods to Reference.
2014-02-23 15:43:02 +01:00
Carlos Martín Nieto 1b09b03c0e Merge commit 'refs/pull/53/head' of github.com:libgit2/git2go
On top: fix git_buf handling and rename signature

This fixes #57, #54.

Conflicts:
	git.go
	reference.go
	repository.go
	submodule.go
2014-02-23 15:31:22 +01:00
lye bc80beb843 Add partial diff/patch functionality.
This commit adds barebones capacity to generate diffs from two trees and
to emit those as git-style diffs (via `Patch.String`), or to enumerate
the files/hunks/lines in the diff to emit the data yourself.

The walk functions have been implemented in the same manner as the Odb
walking methods.

Note that not all of the functionality is implemented for either the
`git_diff_*` nor the `git_patch_*` functions, and there are unexposed
constants which would likely be useful to add.
2014-02-20 00:29:03 -06:00
lye 53b5ecacbe Add Is$Type methods to Reference.
This patch adds the following methods to Reference:

	IsBranch() bool
	IsRemote() bool
	IsTag() bool

which correspond to the `git_reference_is_$type` functions in libgit2.
2014-02-20 00:28:31 -06:00
Aidan Nulman 1c23e8ece3 add OdbBackend.Free() and the C it wraps; go fmt odb.go 2014-01-29 18:55:17 -05:00
Aidan Nulman d59f6d6d90 Merge branch 'catchupTo66af84' into custom_odb
Conflicts:
	git.go
	reference.go
	repository.go
	submodule.go
2014-01-29 18:10:38 -05:00
Aidan Nulman f66502aaf4 update git2go to support latest libgit2 development commit (id: 66af84) 2014-01-29 18:01:26 -05:00
Aidan Nulman f610cf25d7 Merge branch 'master' into custom_odb 2014-01-28 22:49:55 -05:00
Claudiu-Vlad Ursache bf209ca2ba Remove unnecessary cast. 2014-01-26 12:36:05 +01:00
Claudiu-Vlad Ursache 53f2ebce5f Test for Odb hash function. 2014-01-25 22:18:43 +01:00
Claudiu-Vlad Ursache 4ce2eb713b Add Odb hash function. 2014-01-25 22:18:32 +01:00
Tobias Haar 054268a634 added write method 2014-01-24 01:10:23 +00:00
Jason Toffaletti 32bf5f0a23 wip wrapping git_remote 2014-01-06 20:05:35 +00:00
Jason Toffaletti d124544668 minor 2014-01-06 16:55:29 +00:00
Jason Toffaletti 5d8a14d108 wrappers for git_cred, git_transfer_progress. don't call nil callbacks. 2014-01-05 20:55:32 +00:00
Jason Toffaletti e825d66fba work in progress wrapping git_clone 2014-01-04 00:40:21 +00:00
Jason Toffaletti 313e1126dd add git_submodule_recurse_t type 2014-01-02 23:33:08 +00:00
Aidan Nulman e686586149 remove unnecessary comment 2013-12-20 14:26:00 -05:00
Aidan Nulman 59c7bd5ce4 Add OdbBackend constructor 2013-12-20 14:11:24 -05:00
Aidan Nulman 507a204249 Rename constructor functions to New... 2013-12-19 17:24:44 -05:00
Aidan Nulman 19b241bd55 Refactor InitRepositoryWCustomOdbBackend() into component functions 2013-12-19 00:33:23 -05:00
Aidan Nulman dfe6d1ab7e Stop assuming ODB backend includes wrapping routine; wrap in git2go instead 2013-12-18 17:25:54 -05:00
Carlos Martín Nieto 66e1c47619 Merge pull request #41 from libgit2/cmn/threading
Lock the OS thread when acessing errors
2013-12-18 07:19:02 -08:00
Carlos Martín Nieto a40bdfd420 Lock the OS thread when acessing errors
The library stores error information in thread-local storage, which
means we need to make sure that the Go runtime doesn't switch OS
threads between the time we call a function and th time we attempt to
retrieve the error information.
2013-12-18 16:18:32 +01:00
Aidan Nulman 66dfbbf539 add function to init repos w/custom odb backends 2013-12-17 18:46:25 -05:00
Vicent Martí 625ffd022e Merge pull request #47 from kron4eg/tree_builder_mem_leak
Fix memleak, TreeBuilder, Config and Parent commit
2013-11-18 07:24:37 -08:00
Artiom Di 5e30c192e9 Fix memleak for Config and parent commit objects 2013-11-14 15:24:43 +02:00
Artiom Di 295ec8894c Fix memleak, free TreeBuilder 2013-11-14 13:08:34 +02:00
Carlos Martín Nieto d8c3772e35 ObjectType: capitalise Tag 2013-11-14 00:24:44 +01:00
Carlos Martín Nieto a5ad8de506 Don't repeat the pkg-config line
This is only needed once per package. Having it on every file makes the
build system ask about it n times, which is silly.
2013-11-14 00:23:04 +01:00
Carlos Martín Nieto b5b891a03d Packbuilder: adjust to changes in dev 2013-11-13 06:53:10 +01:00
Johann Weging 961db94aa2 branch: Deleted BranchForeach 2013-10-30 15:01:08 +01:00
Johann Weging ed86064871 branch:BranchForeach: Correct handling of the ListFlags 2013-10-10 10:39:49 +02:00
Johann Weging 4c4da3a846 branch: Renamed BranchLookup to LookupBrnach 2013-10-08 14:52:22 +02:00
Johann Weging 6372ec052f branch: Renamed BranchCreate to CreateBranch 2013-10-08 14:49:03 +02:00
Johann Weging 771e0c11bc branch: Variable names don't repeat its type name any longer 2013-10-08 14:44:11 +02:00
Johann Weging f03cec5375 branch: Changed BranchT to BranchType 2013-10-08 14:39:05 +02:00
Johann Weging ff5150e6c9 branch: Implemented branch functions. 2013-10-08 02:07:06 +02:00
Carlos Martín Nieto f583ad8abd Remove leftover reference to old const 2013-09-22 00:05:37 +02:00
Vicent Martí 6c97b9ac9d Merge pull request #36 from libgit2/ref-iter
reference: get references out of the iterator
2013-09-17 08:03:50 -07:00
Vicent Martí da5033fb98 Merge pull request #38 from libgit2/odb-stream
Wrap the odb streams
2013-09-17 08:03:34 -07:00
Vicent Martí b5d4197108 Merge pull request #39 from libgit2/goify-consts
Goify consts
2013-09-17 08:03:06 -07:00
Carlos Martín Nieto 621397026c Wrap the odb streams
The interface to these streams should be what you expect from Go, and
both have Write and Close functions so they implement
Reader/ReadCloser and Write/WriteCloser respectively.
2013-09-11 19:25:40 +02:00
Carlos Martín Nieto 4abda3a60b reference: get references out of the iterator
Allow getting references out of the iterator instead of just names.
2013-08-09 18:22:26 +02:00
Axel Wagner 179b69ce21 Support for index-entries 2013-05-22 14:41:42 +02:00
111 changed files with 19083 additions and 758 deletions

4
.gitignore vendored Normal file
View File

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

17
Build_bundled_static.go Normal file
View File

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

14
Build_system_dynamic.go Normal file
View File

@ -0,0 +1,14 @@
//go:build !static
// +build !static
package git
/*
#cgo pkg-config: libgit2
#cgo CFLAGS: -DLIBGIT2_DYNAMIC -I/opt/libgit2/include
#cgo LDFLAGS: -L/opt/libgit2 -lgit2
#include <git2.h>
*/
import "C"

15
Build_system_static.go Normal file
View File

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

View File

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

70
Makefile Normal file
View File

@ -0,0 +1,70 @@
TEST_ARGS ?= --count=1
PKG_CONFIG_PATH=/opt/libgit2/
default: goimports test
goimports:
goimports -w *.go
generate: static-build/install/lib/libgit2.a
go generate --tags "static" ./...
# System library
# ==============
# This uses whatever version of libgit2 can be found in the system.
test:
-go-mod-clean # go install go.wit.com/apps/go-mod-clean@latest
go run script/check-MakeGitError-thread-lock.go
LD_LIBRARY_PATH=/opt/libgit2 PKG_CONFIG_PATH=/opt/libgit2/ go test -v -x $(TEST_ARGS) ./...
add-remote:
git remote add git2go https://github.com/libgit2/git2go.git
install:
go install ./...
clean:
rm go.*
# Bundled dynamic library
# =======================
# In order to avoid having to manipulate `git_dynamic.go`, which would prevent
# the system-wide libgit2.so from being used in a sort of ergonomic way, this
# instead moves the complexity of overriding the paths so that the built
# libraries can be found by the build and tests.
.PHONY: build-libgit2-dynamic
build-libgit2-dynamic:
./script/build-libgit2-dynamic.sh
dynamic-build/install/lib/libgit2.so:
./script/build-libgit2-dynamic.sh
test-dynamic: dynamic-build/install/lib/libgit2.so
PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \
go run script/check-MakeGitError-thread-lock.go
PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \
LD_LIBRARY_PATH=dynamic-build/install/lib \
go test $(TEST_ARGS) ./...
install-dynamic: dynamic-build/install/lib/libgit2.so
PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \
go install ./...
# Bundled static library
# ======================
# This is mostly used in tests, but can also be used to provide a
# statically-linked library with the bundled version of libgit2.
.PHONY: build-libgit2-static
build-libgit2-static:
./script/build-libgit2-static.sh
static-build/install/lib/libgit2.a:
./script/build-libgit2-static.sh
test-static: static-build/install/lib/libgit2.a
go run script/check-MakeGitError-thread-lock.go
go test --tags "static" $(TEST_ARGS) ./...
install-static: static-build/install/lib/libgit2.a
go install --tags "static" ./...

View File

@ -1,21 +1,69 @@
git2go
GO libgit2
======
[![GoDoc](https://godoc.org/go.wit.com/lib/libgit2?status.svg)](http://godoc.org/go.wit.com/lib/libgit2) [![Build Status](https://travis-ci.org/libgit2/libgit2.svg?branch=main)](https://travis-ci.org/libgit2/libgit2)
Go bindings for [libgit2](http://libgit2.github.com/). These bindings are for top-of-the-branch libgit2, and they move fast, things may or may not work. Operator get me Beijing-jing-jing-jing!
Go bindings for [libgit2](http://libgit2.github.com/).
### Updated 2024/12/16
### Which Go version to use
* This package is updated to work against libgit2 version 1.8 on Debian sid
* There is one line commented out which needs to be fixed in remote.go
* some of the tests seem to run
```sh
go install go.wit.com/apps/go-clone@latest
go install go.wit.com/apps/go-mod-clean@latest
go-clone --recusive go.wit.com/lib/libgit2
```
### Which branch to send Pull requests to
TODO: not sure yet
Installing
----------
Just `go get github.com/libgit2/git2go`. You'll need to have top-of-the-branch libgit2 from development installed in your system and available via `pkg-config`. These bindings are in sync with the top of `development`.
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.
### 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
```go
goimports -w *.go
```
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.
Running the tests
-----------------
For the stable version, `go test` will work as usual. For the `main` branch, similarly to installing, running the tests requires building a local libgit2 library, so the Makefile provides a wrapper that makes sure it's built
make test-static
Alternatively, you can build the library manually first and then run the tests
make install-static
go test -v -tags static ./...
License
-------
M to the I to the T. See the LICENSE file if you've never seen a MIT license before.
M to the I to the T. See the LICENSE file if you've never seen an MIT license before.
Authors
-------
- Carlos Martín (@carlosmn)
- Vicent Martí (@vmg)
- Carlos Martín (github@carlosmn)
- Vicent Martí (github@vmg)

166
blame.go Normal file
View File

@ -0,0 +1,166 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
type BlameOptions struct {
Flags BlameOptionsFlag
MinMatchCharacters uint16
NewestCommit *Oid
OldestCommit *Oid
MinLine uint32
MaxLine uint32
}
func DefaultBlameOptions() (BlameOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
opts := C.git_blame_options{}
ecode := C.git_blame_options_init(&opts, C.GIT_BLAME_OPTIONS_VERSION)
if ecode < 0 {
return BlameOptions{}, MakeGitError(ecode)
}
return BlameOptions{
Flags: BlameOptionsFlag(opts.flags),
MinMatchCharacters: uint16(opts.min_match_characters),
NewestCommit: newOidFromC(&opts.newest_commit),
OldestCommit: newOidFromC(&opts.oldest_commit),
MinLine: uint32(opts.min_line),
MaxLine: uint32(opts.max_line),
}, nil
}
type BlameOptionsFlag uint32
const (
BlameNormal BlameOptionsFlag = C.GIT_BLAME_NORMAL
BlameTrackCopiesSameFile BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_SAME_FILE
BlameTrackCopiesSameCommitMoves BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES
BlameTrackCopiesSameCommitCopies BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES
BlameTrackCopiesAnyCommitCopies BlameOptionsFlag = C.GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES
BlameFirstParent BlameOptionsFlag = C.GIT_BLAME_FIRST_PARENT
BlameUseMailmap BlameOptionsFlag = C.GIT_BLAME_USE_MAILMAP
BlameIgnoreWhitespace BlameOptionsFlag = C.GIT_BLAME_IGNORE_WHITESPACE
)
func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error) {
var blamePtr *C.git_blame
var copts *C.git_blame_options
if opts != nil {
copts = &C.git_blame_options{
version: C.GIT_BLAME_OPTIONS_VERSION,
flags: C.uint32_t(opts.Flags),
min_match_characters: C.uint16_t(opts.MinMatchCharacters),
min_line: C.size_t(opts.MinLine),
max_line: C.size_t(opts.MaxLine),
}
if opts.NewestCommit != nil {
copts.newest_commit = *opts.NewestCommit.toC()
}
if opts.OldestCommit != nil {
copts.oldest_commit = *opts.OldestCommit.toC()
}
}
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_blame_file(&blamePtr, v.ptr, cpath, copts)
runtime.KeepAlive(v)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newBlameFromC(blamePtr), nil
}
type Blame struct {
doNotCompare
ptr *C.git_blame
}
func (blame *Blame) HunkCount() int {
ret := int(C.git_blame_get_hunk_count(blame.ptr))
runtime.KeepAlive(blame)
return ret
}
func (blame *Blame) HunkByIndex(index int) (BlameHunk, error) {
ptr := C.git_blame_get_hunk_byindex(blame.ptr, C.uint32_t(index))
runtime.KeepAlive(blame)
if ptr == nil {
return BlameHunk{}, ErrInvalid
}
return blameHunkFromC(ptr), nil
}
func (blame *Blame) HunkByLine(lineno int) (BlameHunk, error) {
ptr := C.git_blame_get_hunk_byline(blame.ptr, C.size_t(lineno))
runtime.KeepAlive(blame)
if ptr == nil {
return BlameHunk{}, ErrInvalid
}
return blameHunkFromC(ptr), nil
}
func newBlameFromC(ptr *C.git_blame) *Blame {
if ptr == nil {
return nil
}
blame := &Blame{
ptr: ptr,
}
runtime.SetFinalizer(blame, (*Blame).Free)
return blame
}
func (blame *Blame) Free() error {
if blame.ptr == nil {
return ErrInvalid
}
runtime.SetFinalizer(blame, nil)
C.git_blame_free(blame.ptr)
blame.ptr = nil
return nil
}
type BlameHunk struct {
LinesInHunk uint16
FinalCommitId *Oid
FinalStartLineNumber uint16
FinalSignature *Signature
OrigCommitId *Oid
OrigPath string
OrigStartLineNumber uint16
OrigSignature *Signature
Boundary bool
}
func blameHunkFromC(hunk *C.git_blame_hunk) BlameHunk {
return BlameHunk{
LinesInHunk: uint16(hunk.lines_in_hunk),
FinalCommitId: newOidFromC(&hunk.final_commit_id),
FinalStartLineNumber: uint16(hunk.final_start_line_number),
FinalSignature: newSignatureFromC(hunk.final_signature),
OrigCommitId: newOidFromC(&hunk.orig_commit_id),
OrigPath: C.GoString(hunk.orig_path),
OrigStartLineNumber: uint16(hunk.orig_start_line_number),
OrigSignature: newSignatureFromC(hunk.orig_signature),
Boundary: hunk.boundary == 1,
}
}

73
blame_test.go Normal file
View File

@ -0,0 +1,73 @@
package git
import (
"reflect"
"testing"
)
func TestBlame(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId1, _ := seedTestRepo(t, repo)
commitId2, _ := updateReadme(t, repo, "foo\nbar\nbaz\n")
opts := BlameOptions{
NewestCommit: commitId2,
OldestCommit: nil,
MinLine: 1,
MaxLine: 3,
}
blame, err := repo.BlameFile("README", &opts)
checkFatal(t, err)
defer blame.Free()
if blame.HunkCount() != 2 {
t.Errorf("got hunk count %d, want 2", blame.HunkCount())
}
wantHunk1 := BlameHunk{
LinesInHunk: 1,
FinalCommitId: commitId1,
FinalStartLineNumber: 1,
OrigCommitId: commitId1,
OrigPath: "README",
OrigStartLineNumber: 1,
Boundary: true,
}
wantHunk2 := BlameHunk{
LinesInHunk: 2,
FinalCommitId: commitId2,
FinalStartLineNumber: 2,
OrigCommitId: commitId2,
OrigPath: "README",
OrigStartLineNumber: 2,
Boundary: false,
}
hunk1, err := blame.HunkByIndex(0)
checkFatal(t, err)
checkHunk(t, "index 0", hunk1, wantHunk1)
hunk2, err := blame.HunkByIndex(1)
checkFatal(t, err)
checkHunk(t, "index 1", hunk2, wantHunk2)
hunkLine1, err := blame.HunkByLine(1)
checkFatal(t, err)
checkHunk(t, "line 1", hunkLine1, wantHunk1)
hunkLine2, err := blame.HunkByLine(3)
checkFatal(t, err)
checkHunk(t, "line 2", hunkLine2, wantHunk2)
}
func checkHunk(t *testing.T, label string, hunk, want BlameHunk) {
hunk.FinalSignature = nil
want.FinalSignature = nil
hunk.OrigSignature = nil
want.OrigSignature = nil
if !reflect.DeepEqual(hunk, want) {
t.Fatalf("%s: got hunk %+v, want %+v", label, hunk, want)
}
}

143
blob.go
View File

@ -1,26 +1,151 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
#include <string.h>
int _go_git_writestream_write(git_writestream *stream, const char *buffer, size_t len);
void _go_git_writestream_free(git_writestream *stream);
*/
import "C"
import (
"reflect"
"runtime"
"unsafe"
)
type Blob struct {
gitObject
doNotCompare
Object
cast_ptr *C.git_blob
}
func (v Blob) Size() int64 {
return int64(C.git_blob_rawsize(v.ptr))
func (b *Blob) AsObject() *Object {
return &b.Object
}
func (v Blob) Contents() []byte {
size := C.int(C.git_blob_rawsize(v.ptr))
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.ptr))
return C.GoBytes(buffer, size)
func (v *Blob) Size() int64 {
ret := int64(C.git_blob_rawsize(v.cast_ptr))
runtime.KeepAlive(v)
return ret
}
func (v *Blob) Contents() []byte {
size := C.int(C.git_blob_rawsize(v.cast_ptr))
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.cast_ptr))
goBytes := C.GoBytes(buffer, size)
runtime.KeepAlive(v)
return goBytes
}
func (v *Blob) IsBinary() bool {
ret := C.git_blob_is_binary(v.cast_ptr) == 1
runtime.KeepAlive(v)
return ret
}
func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var id C.git_oid
var size C.size_t
// Go 1.6 added some increased checking of passing pointer to
// C, but its check depends on its expectations of what we
// pass to the C function, so unless we take the address of
// its contents at the call site itself, it can fail when
// 'data' is a slice of a slice.
//
// When we're given an empty slice, create a dummy one where 0
// isn't out of bounds.
if len(data) > 0 {
size = C.size_t(len(data))
} else {
data = []byte{0}
size = C.size_t(0)
}
ecode := C.git_blob_create_from_buffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size)
runtime.KeepAlive(repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newOidFromC(&id), nil
}
func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, error) {
var chintPath *C.char = nil
var stream *C.git_writestream
if len(hintPath) > 0 {
chintPath = C.CString(hintPath)
defer C.free(unsafe.Pointer(chintPath))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_blob_create_from_stream(&stream, repo.ptr, chintPath)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newBlobWriteStreamFromC(stream, repo), nil
}
type BlobWriteStream struct {
doNotCompare
ptr *C.git_writestream
repo *Repository
}
func newBlobWriteStreamFromC(ptr *C.git_writestream, repo *Repository) *BlobWriteStream {
stream := &BlobWriteStream{
ptr: ptr,
repo: repo,
}
runtime.SetFinalizer(stream, (*BlobWriteStream).Free)
return stream
}
// Implement io.Writer
func (stream *BlobWriteStream) Write(p []byte) (int, error) {
header := (*reflect.SliceHeader)(unsafe.Pointer(&p))
ptr := (*C.char)(unsafe.Pointer(header.Data))
size := C.size_t(header.Len)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C._go_git_writestream_write(stream.ptr, ptr, size)
runtime.KeepAlive(stream)
if ecode < 0 {
return 0, MakeGitError(ecode)
}
return len(p), nil
}
func (stream *BlobWriteStream) Free() {
runtime.SetFinalizer(stream, nil)
C._go_git_writestream_free(stream.ptr)
}
func (stream *BlobWriteStream) Commit() (*Oid, error) {
oid := C.git_oid{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_blob_create_from_stream_commit(&oid, stream.ptr)
runtime.KeepAlive(stream)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newOidFromC(&oid), nil
}

60
blob_test.go Normal file
View File

@ -0,0 +1,60 @@
package git
import (
"bytes"
"testing"
)
type bufWrapper struct {
buf [64]byte
pointer []byte
}
func doublePointerBytes() []byte {
o := &bufWrapper{}
o.pointer = o.buf[0:10]
return o.pointer[0:1]
}
func TestCreateBlobFromBuffer(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
id, err := repo.CreateBlobFromBuffer(make([]byte, 0))
checkFatal(t, err)
if id.String() != "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" {
t.Fatal("Empty buffer did not deliver empty blob id")
}
tests := []struct {
data []byte
isBinary bool
}{
{
data: []byte("hello there"),
isBinary: false,
},
{
data: doublePointerBytes(),
isBinary: true,
},
}
for _, tt := range tests {
data := tt.data
id, err = repo.CreateBlobFromBuffer(data)
checkFatal(t, err)
blob, err := repo.LookupBlob(id)
checkFatal(t, err)
if !bytes.Equal(blob.Contents(), data) {
t.Fatal("Loaded bytes don't match original bytes:",
blob.Contents(), "!=", data)
}
want := tt.isBinary
if got := blob.IsBinary(); got != want {
t.Fatalf("IsBinary() = %v, want %v", got, want)
}
}
}

264
branch.go Normal file
View File

@ -0,0 +1,264 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
type BranchType uint
const (
BranchAll BranchType = C.GIT_BRANCH_ALL
BranchLocal BranchType = C.GIT_BRANCH_LOCAL
BranchRemote BranchType = C.GIT_BRANCH_REMOTE
)
type Branch struct {
doNotCompare
*Reference
}
func (r *Reference) Branch() *Branch {
return &Branch{Reference: r}
}
type BranchIterator struct {
doNotCompare
ptr *C.git_branch_iterator
repo *Repository
}
type BranchIteratorFunc func(*Branch, BranchType) error
func newBranchIteratorFromC(repo *Repository, ptr *C.git_branch_iterator) *BranchIterator {
i := &BranchIterator{repo: repo, ptr: ptr}
runtime.SetFinalizer(i, (*BranchIterator).Free)
return i
}
func (i *BranchIterator) Next() (*Branch, BranchType, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var refPtr *C.git_reference
var refType C.git_branch_t
ecode := C.git_branch_next(&refPtr, &refType, i.ptr)
if ecode < 0 {
return nil, BranchLocal, MakeGitError(ecode)
}
branch := newReferenceFromC(refPtr, i.repo).Branch()
return branch, BranchType(refType), nil
}
func (i *BranchIterator) Free() {
runtime.SetFinalizer(i, nil)
C.git_branch_iterator_free(i.ptr)
}
func (i *BranchIterator) ForEach(f BranchIteratorFunc) error {
b, t, err := i.Next()
for err == nil {
err = f(b, t)
if err == nil {
b, t, err = i.Next()
}
}
if err != nil && IsErrorCode(err, ErrorCodeIterOver) {
return nil
}
return err
}
func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, error) {
refType := C.git_branch_t(flags)
var ptr *C.git_branch_iterator
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_branch_iterator_new(&ptr, repo.ptr, refType)
runtime.KeepAlive(repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newBranchIteratorFromC(repo, ptr), nil
}
func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool) (*Branch, error) {
var ptr *C.git_reference
cBranchName := C.CString(branchName)
defer C.free(unsafe.Pointer(cBranchName))
cForce := cbool(force)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_create(&ptr, repo.ptr, cBranchName, target.cast_ptr, cForce)
runtime.KeepAlive(repo)
runtime.KeepAlive(target)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, repo).Branch(), nil
}
func (b *Branch) Delete() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_delete(b.Reference.ptr)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) {
var ptr *C.git_reference
cNewBranchName := C.CString(newBranchName)
defer C.free(unsafe.Pointer(cNewBranchName))
cForce := cbool(force)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_move(&ptr, b.Reference.ptr, cNewBranchName, cForce)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, b.repo).Branch(), nil
}
func (b *Branch) IsHead() (bool, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_is_head(b.Reference.ptr)
runtime.KeepAlive(b.Reference)
switch ret {
case 1:
return true, nil
case 0:
return false, nil
}
return false, MakeGitError(ret)
}
func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch, error) {
var ptr *C.git_reference
cName := C.CString(branchName)
defer C.free(unsafe.Pointer(cName))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_lookup(&ptr, repo.ptr, cName, C.git_branch_t(bt))
runtime.KeepAlive(repo)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, repo).Branch(), nil
}
func (b *Branch) Name() (string, error) {
var cName *C.char
defer C.free(unsafe.Pointer(cName))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_name(&cName, b.Reference.ptr)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(cName), nil
}
func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
cName := C.CString(canonicalBranchName)
defer C.free(unsafe.Pointer(cName))
nameBuf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_remote_name(&nameBuf, repo.ptr, cName)
runtime.KeepAlive(repo)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_dispose(&nameBuf)
return C.GoString(nameBuf.ptr), nil
}
func (b *Branch) SetUpstream(upstreamName string) error {
cName := C.CString(upstreamName)
defer C.free(unsafe.Pointer(cName))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_set_upstream(b.Reference.ptr, cName)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (b *Branch) Upstream() (*Reference, error) {
var ptr *C.git_reference
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_upstream(&ptr, b.Reference.ptr)
runtime.KeepAlive(b.Reference)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, b.repo), nil
}
func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) {
cName := C.CString(canonicalBranchName)
defer C.free(unsafe.Pointer(cName))
nameBuf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_upstream_name(&nameBuf, repo.ptr, cName)
runtime.KeepAlive(repo)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_dispose(&nameBuf)
return C.GoString(nameBuf.ptr), nil
}

63
branch_test.go Normal file
View File

@ -0,0 +1,63 @@
package git
import (
"testing"
)
func TestBranchIterator(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
i, err := repo.NewBranchIterator(BranchLocal)
checkFatal(t, err)
b, bt, err := i.Next()
checkFatal(t, err)
if name, _ := b.Name(); name != "master" {
t.Fatalf("expected master")
} else if bt != BranchLocal {
t.Fatalf("expected BranchLocal, not %v", t)
}
b, bt, err = i.Next()
if !IsErrorCode(err, ErrorCodeIterOver) {
t.Fatal("expected iterover")
}
}
func TestBranchIteratorEach(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
i, err := repo.NewBranchIterator(BranchLocal)
checkFatal(t, err)
var names []string
f := func(b *Branch, t BranchType) error {
name, err := b.Name()
if err != nil {
return err
}
names = append(names, name)
return nil
}
err = i.ForEach(f)
if err != nil && !IsErrorCode(err, ErrorCodeIterOver) {
t.Fatal(err)
}
if len(names) != 1 {
t.Fatalf("expect 1 branch, but it was %d\n", len(names))
}
if names[0] != "master" {
t.Fatalf("expect branch master, but it was %s\n", names[0])
}
}

View File

@ -1,81 +1,266 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
git_checkout_opts git_checkout_opts_init() {
git_checkout_opts ret = GIT_CHECKOUT_OPTS_INIT;
return ret;
}
extern void _go_git_populate_checkout_callbacks(git_checkout_options *opts);
*/
import "C"
import (
"os"
"runtime"
"unsafe"
)
type CheckoutNotifyType uint
type CheckoutStrategy uint
const (
CheckoutNotifyNone CheckoutNotifyType = C.GIT_CHECKOUT_NOTIFY_NONE
CheckoutNotifyConflict CheckoutNotifyType = C.GIT_CHECKOUT_NOTIFY_CONFLICT
CheckoutNotifyDirty CheckoutNotifyType = C.GIT_CHECKOUT_NOTIFY_DIRTY
CheckoutNotifyUpdated CheckoutNotifyType = C.GIT_CHECKOUT_NOTIFY_UPDATED
CheckoutNotifyUntracked CheckoutNotifyType = C.GIT_CHECKOUT_NOTIFY_UNTRACKED
CheckoutNotifyIgnored CheckoutNotifyType = C.GIT_CHECKOUT_NOTIFY_IGNORED
CheckoutNotifyAll CheckoutNotifyType = C.GIT_CHECKOUT_NOTIFY_ALL
CheckoutNone CheckoutStrategy = C.GIT_CHECKOUT_NONE // Dry run, no actual updates
CheckoutSafe = C.GIT_CHECKOUT_SAFE // Allow safe updates that cannot overwrite uncommitted data
CheckoutSafeCreate = C.GIT_CHECKOUT_SAFE_CREATE // Allow safe updates plus creation of missing files
CheckoutForce = C.GIT_CHECKOUT_FORCE // Allow all updates to force working directory to look like index
CheckoutAllowConflicts = C.GIT_CHECKOUT_ALLOW_CONFLICTS // Allow checkout to make safe updates even if conflicts are found
CheckoutRemoveUntracked = C.GIT_CHECKOUT_REMOVE_UNTRACKED // Remove untracked files not in index (that are not ignored)
CheckoutRemoveIgnored = C.GIT_CHECKOUT_REMOVE_IGNORED // Remove ignored files not in index
CheckotUpdateOnly = C.GIT_CHECKOUT_UPDATE_ONLY // Only update existing files, don't create new ones
CheckoutDontUpdateIndex = C.GIT_CHECKOUT_DONT_UPDATE_INDEX // Normally checkout updates index entries as it goes; this stops that
CheckoutNoRefresh = C.GIT_CHECKOUT_NO_REFRESH // Don't refresh index/config/etc before doing checkout
CheckooutDisablePathspecMatch = C.GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH // Treat pathspec as simple list of exact match file paths
CheckoutSkipUnmerged = C.GIT_CHECKOUT_SKIP_UNMERGED // Allow checkout to skip unmerged files (NOT IMPLEMENTED)
CheckoutUserOurs = C.GIT_CHECKOUT_USE_OURS // For unmerged files, checkout stage 2 from index (NOT IMPLEMENTED)
CheckoutUseTheirs = C.GIT_CHECKOUT_USE_THEIRS // For unmerged files, checkout stage 3 from index (NOT IMPLEMENTED)
CheckoutUpdateSubmodules = C.GIT_CHECKOUT_UPDATE_SUBMODULES // Recursively checkout submodules with same options (NOT IMPLEMENTED)
CheckoutUpdateSubmodulesIfChanged = C.GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED // Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED)
CheckoutSafe CheckoutStrategy = C.GIT_CHECKOUT_SAFE // Allow safe updates that cannot overwrite uncommitted data
CheckoutForce CheckoutStrategy = C.GIT_CHECKOUT_FORCE // Allow all updates to force working directory to look like index
CheckoutRecreateMissing CheckoutStrategy = C.GIT_CHECKOUT_RECREATE_MISSING // Allow checkout to recreate missing files
CheckoutAllowConflicts CheckoutStrategy = C.GIT_CHECKOUT_ALLOW_CONFLICTS // Allow checkout to make safe updates even if conflicts are found
CheckoutRemoveUntracked CheckoutStrategy = C.GIT_CHECKOUT_REMOVE_UNTRACKED // Remove untracked files not in index (that are not ignored)
CheckoutRemoveIgnored CheckoutStrategy = C.GIT_CHECKOUT_REMOVE_IGNORED // Remove ignored files not in index
CheckoutUpdateOnly CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_ONLY // Only update existing files, don't create new ones
CheckoutDontUpdateIndex CheckoutStrategy = C.GIT_CHECKOUT_DONT_UPDATE_INDEX // Normally checkout updates index entries as it goes; this stops that
CheckoutNoRefresh CheckoutStrategy = C.GIT_CHECKOUT_NO_REFRESH // Don't refresh index/config/etc before doing checkout
CheckoutSkipUnmerged CheckoutStrategy = C.GIT_CHECKOUT_SKIP_UNMERGED // Allow checkout to skip unmerged files
CheckoutUseOurs CheckoutStrategy = C.GIT_CHECKOUT_USE_OURS // For unmerged files, checkout stage 2 from index
CheckoutUseTheirs CheckoutStrategy = C.GIT_CHECKOUT_USE_THEIRS // For unmerged files, checkout stage 3 from index
CheckoutDisablePathspecMatch CheckoutStrategy = C.GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH // Treat pathspec as simple list of exact match file paths
CheckoutSkipLockedDirectories CheckoutStrategy = C.GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES // Ignore directories in use, they will be left empty
CheckoutDontOverwriteIgnored CheckoutStrategy = C.GIT_CHECKOUT_DONT_OVERWRITE_IGNORED // Don't overwrite ignored files that exist in the checkout target
CheckoutConflictStyleMerge CheckoutStrategy = C.GIT_CHECKOUT_CONFLICT_STYLE_MERGE // Write normal merge files for conflicts
CheckoutConflictStyleDiff3 CheckoutStrategy = C.GIT_CHECKOUT_CONFLICT_STYLE_DIFF3 // Include common ancestor data in diff3 format files for conflicts
CheckoutDontRemoveExisting CheckoutStrategy = C.GIT_CHECKOUT_DONT_REMOVE_EXISTING // Don't overwrite existing files or folders
CheckoutDontWriteIndex CheckoutStrategy = C.GIT_CHECKOUT_DONT_WRITE_INDEX // Normally checkout writes the index upon completion; this prevents that
CheckoutUpdateSubmodules CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_SUBMODULES // Recursively checkout submodules with same options (NOT IMPLEMENTED)
CheckoutUpdateSubmodulesIfChanged CheckoutStrategy = C.GIT_CHECKOUT_UPDATE_SUBMODULES_IF_CHANGED // Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED)
)
type CheckoutOpts struct {
Strategy CheckoutStrategy // Default will be a dry run
DisableFilters bool // Don't apply filters like CRLF conversion
DirMode os.FileMode // Default is 0755
FileMode os.FileMode // Default is 0644 or 0755 as dictated by blob
FileOpenFlags int // Default is O_CREAT | O_TRUNC | O_WRONLY
type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) error
type CheckoutProgressCallback func(path string, completed, total uint)
type CheckoutOptions struct {
Strategy CheckoutStrategy // Default will be a dry run
DisableFilters bool // Don't apply filters like CRLF conversion
DirMode os.FileMode // Default is 0755
FileMode os.FileMode // Default is 0644 or 0755 as dictated by blob
FileOpenFlags int // Default is O_CREAT | O_TRUNC | O_WRONLY
NotifyFlags CheckoutNotifyType // Default will be none
NotifyCallback CheckoutNotifyCallback
ProgressCallback CheckoutProgressCallback
TargetDirectory string // Alternative checkout path to workdir
Paths []string
Baseline *Tree
}
// Convert the CheckoutOpts struct to the corresponding C-struct
func populateCheckoutOpts(ptr *C.git_checkout_opts, opts *CheckoutOpts) {
*ptr = C.git_checkout_opts_init()
if opts == nil {
func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOptions {
opts := CheckoutOptions{
Strategy: CheckoutStrategy(c.checkout_strategy),
DisableFilters: c.disable_filters != 0,
DirMode: os.FileMode(c.dir_mode),
FileMode: os.FileMode(c.file_mode),
FileOpenFlags: int(c.file_open_flags),
NotifyFlags: CheckoutNotifyType(c.notify_flags),
}
if c.notify_payload != nil {
opts.NotifyCallback = pointerHandles.Get(c.notify_payload).(*checkoutCallbackData).options.NotifyCallback
}
if c.progress_payload != nil {
opts.ProgressCallback = pointerHandles.Get(c.progress_payload).(*checkoutCallbackData).options.ProgressCallback
}
if c.target_directory != nil {
opts.TargetDirectory = C.GoString(c.target_directory)
}
return opts
}
type checkoutCallbackData struct {
options *CheckoutOptions
errorTarget *error
}
//export checkoutNotifyCallback
func checkoutNotifyCallback(
why C.git_checkout_notify_t,
cpath *C.char,
cbaseline, ctarget, cworkdir, handle unsafe.Pointer,
) C.int {
if handle == nil {
return C.int(ErrorCodeOK)
}
path := C.GoString(cpath)
var baseline, target, workdir DiffFile
if cbaseline != nil {
baseline = diffFileFromC((*C.git_diff_file)(cbaseline))
}
if ctarget != nil {
target = diffFileFromC((*C.git_diff_file)(ctarget))
}
if cworkdir != nil {
workdir = diffFileFromC((*C.git_diff_file)(cworkdir))
}
data := pointerHandles.Get(handle).(*checkoutCallbackData)
if data.options.NotifyCallback == nil {
return C.int(ErrorCodeOK)
}
err := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir)
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
//export checkoutProgressCallback
func checkoutProgressCallback(
path *C.char,
completed_steps, total_steps C.size_t,
handle unsafe.Pointer,
) {
data := pointerHandles.Get(handle).(*checkoutCallbackData)
if data.options.ProgressCallback == nil {
return
}
ptr.checkout_strategy = C.uint(opts.Strategy)
ptr.disable_filters = cbool(opts.DisableFilters)
ptr.dir_mode = C.uint(opts.DirMode.Perm())
ptr.file_mode = C.uint(opts.FileMode.Perm())
data.options.ProgressCallback(C.GoString(path), uint(completed_steps), uint(total_steps))
}
// populateCheckoutOptions populates the provided C-struct with the contents of
// the provided CheckoutOptions struct. Returns copts, or nil if opts is nil,
// in order to help with what to pass.
func populateCheckoutOptions(copts *C.git_checkout_options, opts *CheckoutOptions, errorTarget *error) *C.git_checkout_options {
C.git_checkout_options_init(copts, C.GIT_CHECKOUT_OPTIONS_VERSION)
if opts == nil {
return nil
}
copts.checkout_strategy = C.uint(opts.Strategy)
copts.disable_filters = cbool(opts.DisableFilters)
copts.dir_mode = C.uint(opts.DirMode.Perm())
copts.file_mode = C.uint(opts.FileMode.Perm())
copts.notify_flags = C.uint(opts.NotifyFlags)
if opts.NotifyCallback != nil || opts.ProgressCallback != nil {
C._go_git_populate_checkout_callbacks(copts)
data := &checkoutCallbackData{
options: opts,
errorTarget: errorTarget,
}
payload := pointerHandles.Track(data)
if opts.NotifyCallback != nil {
copts.notify_payload = payload
}
if opts.ProgressCallback != nil {
copts.progress_payload = payload
}
}
if opts.TargetDirectory != "" {
copts.target_directory = C.CString(opts.TargetDirectory)
}
if len(opts.Paths) > 0 {
copts.paths.strings = makeCStringsFromStrings(opts.Paths)
copts.paths.count = C.size_t(len(opts.Paths))
}
if opts.Baseline != nil {
copts.baseline = opts.Baseline.cast_ptr
}
return copts
}
func freeCheckoutOptions(copts *C.git_checkout_options) {
if copts == nil {
return
}
C.free(unsafe.Pointer(copts.target_directory))
if copts.paths.count > 0 {
freeStrarray(&copts.paths)
}
if copts.notify_payload != nil {
pointerHandles.Untrack(copts.notify_payload)
} else if copts.progress_payload != nil {
pointerHandles.Untrack(copts.progress_payload)
}
}
// Updates files in the index and the working tree to match the content of
// the commit pointed at by HEAD.
func (v *Repository) Checkout(opts *CheckoutOpts) error {
var copts C.git_checkout_opts
populateCheckoutOpts(&copts, opts)
// the commit pointed at by HEAD. opts may be nil.
func (v *Repository) CheckoutHead(opts *CheckoutOptions) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_checkout_head(v.ptr, &copts)
var err error
cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err)
defer freeCheckoutOptions(cOpts)
ret := C.git_checkout_head(v.ptr, cOpts)
runtime.KeepAlive(v)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
// Updates files in the working tree to match the content of the index.
func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error {
var copts C.git_checkout_opts
populateCheckoutOpts(&copts, opts)
// Updates files in the working tree to match the content of the given
// index. If index is nil, the repository's index will be used. opts
// may be nil.
func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOptions) error {
var iptr *C.git_index = nil
if index != nil {
iptr = index.ptr
}
ret := C.git_checkout_index(v.ptr, index.ptr, &copts)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var err error
cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err)
defer freeCheckoutOptions(cOpts)
ret := C.git_checkout_index(v.ptr, iptr, cOpts)
runtime.KeepAlive(v)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOptions) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var err error
cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err)
defer freeCheckoutOptions(cOpts)
ret := C.git_checkout_tree(v.ptr, tree.ptr, cOpts)
runtime.KeepAlive(v)
runtime.KeepAlive(tree)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil

94
cherrypick.go Normal file
View File

@ -0,0 +1,94 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
)
type CherrypickOptions struct {
Mainline uint
MergeOptions MergeOptions
CheckoutOptions CheckoutOptions
}
func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions {
opts := CherrypickOptions{
Mainline: uint(c.mainline),
MergeOptions: mergeOptionsFromC(&c.merge_opts),
CheckoutOptions: checkoutOptionsFromC(&c.checkout_opts),
}
return opts
}
func populateCherrypickOptions(copts *C.git_cherrypick_options, opts *CherrypickOptions, errorTarget *error) *C.git_cherrypick_options {
C.git_cherrypick_options_init(copts, C.GIT_CHERRYPICK_OPTIONS_VERSION)
if opts == nil {
return nil
}
copts.mainline = C.uint(opts.Mainline)
populateMergeOptions(&copts.merge_opts, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
return copts
}
func freeCherrypickOpts(copts *C.git_cherrypick_options) {
if copts == nil {
return
}
freeMergeOptions(&copts.merge_opts)
freeCheckoutOptions(&copts.checkout_opts)
}
func DefaultCherrypickOptions() (CherrypickOptions, error) {
c := C.git_cherrypick_options{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_cherrypick_options_init(&c, C.GIT_CHERRYPICK_OPTIONS_VERSION)
if ecode < 0 {
return CherrypickOptions{}, MakeGitError(ecode)
}
defer freeCherrypickOpts(&c)
return cherrypickOptionsFromC(&c), nil
}
func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var err error
cOpts := populateCherrypickOptions(&C.git_cherrypick_options{}, &opts, &err)
defer freeCherrypickOpts(cOpts)
ret := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts)
runtime.KeepAlive(v)
runtime.KeepAlive(commit)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (r *Repository) CherrypickCommit(pick, our *Commit, opts CherrypickOptions) (*Index, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cOpts := populateMergeOptions(&C.git_merge_options{}, &opts.MergeOptions)
defer freeMergeOptions(cOpts)
var ptr *C.git_index
ret := C.git_cherrypick_commit(&ptr, r.ptr, pick.cast_ptr, our.cast_ptr, C.uint(opts.Mainline), cOpts)
runtime.KeepAlive(pick)
runtime.KeepAlive(our)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newIndexFromC(ptr, r), nil
}

140
cherrypick_test.go Normal file
View File

@ -0,0 +1,140 @@
package git
import (
"io/ioutil"
"testing"
)
func checkout(t *testing.T, repo *Repository, commit *Commit) {
tree, err := commit.Tree()
if err != nil {
t.Fatal(err)
}
err = repo.CheckoutTree(tree, &CheckoutOptions{Strategy: CheckoutSafe})
if err != nil {
t.Fatal(err)
}
err = repo.SetHeadDetached(commit.Id())
if err != nil {
t.Fatal(err)
}
}
const content = "Herro, Worrd!"
func readReadme(t *testing.T, repo *Repository) string {
bytes, err := ioutil.ReadFile(pathInRepo(repo, "README"))
if err != nil {
t.Fatal(err)
}
return string(bytes)
}
func TestCherrypick(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
c1, _ := seedTestRepo(t, repo)
c2, _ := updateReadme(t, repo, content)
commit1, err := repo.LookupCommit(c1)
if err != nil {
t.Fatal(err)
}
commit2, err := repo.LookupCommit(c2)
if err != nil {
t.Fatal(err)
}
checkout(t, repo, commit1)
if readReadme(t, repo) == content {
t.Fatalf("README has wrong content after checking out initial commit")
}
opts, err := DefaultCherrypickOptions()
if err != nil {
t.Fatal(err)
}
err = repo.Cherrypick(commit2, opts)
if err != nil {
t.Fatal(err)
}
if readReadme(t, repo) != content {
t.Fatalf("README has wrong contents after cherry-picking")
}
state := repo.State()
if state != RepositoryStateCherrypick {
t.Fatal("Incorrect repository state: ", state)
}
err = repo.StateCleanup()
if err != nil {
t.Fatal(err)
}
state = repo.State()
if state != RepositoryStateNone {
t.Fatal("Incorrect repository state: ", state)
}
}
func TestCherrypickCommit(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
c1, _ := seedTestRepo(t, repo)
c2, _ := updateReadme(t, repo, content)
commit1, err := repo.LookupCommit(c1)
if err != nil {
t.Fatal(err)
}
commit2, err := repo.LookupCommit(c2)
if err != nil {
t.Fatal(err)
}
checkout(t, repo, commit1)
if got := readReadme(t, repo); got == content {
t.Fatalf("README = %q, want %q", got, content)
}
opts, err := DefaultCherrypickOptions()
if err != nil {
t.Fatal(err)
}
idx, err := repo.CherrypickCommit(commit2, commit1, opts)
if err != nil {
t.Fatal(err)
}
defer idx.Free()
// The file is only updated in the index, not in the working directory.
if got := readReadme(t, repo); got == content {
t.Errorf("README = %q, want %q", got, content)
}
if got := repo.State(); got != RepositoryStateNone {
t.Errorf("repo.State() = %v, want %v", got, RepositoryStateCherrypick)
}
if got := idx.EntryCount(); got != 1 {
t.Fatalf("idx.EntryCount() = %v, want %v", got, 1)
}
entry, err := idx.EntryByIndex(0)
if err != nil {
t.Fatal(err)
}
if entry.Path != "README" {
t.Errorf("entry.Path = %v, want %v", entry.Path, "README")
}
}

133
clone.go Normal file
View File

@ -0,0 +1,133 @@
package git
/*
#include <git2.h>
extern void _go_git_populate_clone_callbacks(git_clone_options *opts);
*/
import "C"
import (
"runtime"
"unsafe"
)
type RemoteCreateCallback func(repo *Repository, name, url string) (*Remote, error)
type CloneOptions struct {
CheckoutOptions CheckoutOptions
FetchOptions FetchOptions
Bare bool
CheckoutBranch string
RemoteCreateCallback RemoteCreateCallback
}
func Clone(url string, path string, options *CloneOptions) (*Repository, error) {
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var err error
cOptions := populateCloneOptions(&C.git_clone_options{}, options, &err)
defer freeCloneOptions(cOptions)
if len(options.CheckoutBranch) != 0 {
cOptions.checkout_branch = C.CString(options.CheckoutBranch)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_repository
ret := C.git_clone(&ptr, curl, cpath, cOptions)
if ret == C.int(ErrorCodeUser) && err != nil {
return nil, err
}
if ret < 0 {
return nil, MakeGitError(ret)
}
return newRepositoryFromC(ptr), nil
}
//export remoteCreateCallback
func remoteCreateCallback(
out **C.git_remote,
crepo *C.git_repository,
cname, curl *C.char,
handle unsafe.Pointer,
) C.int {
name := C.GoString(cname)
url := C.GoString(curl)
repo := newRepositoryFromC(crepo)
repo.weak = true
defer repo.Free()
data, ok := pointerHandles.Get(handle).(*cloneCallbackData)
if !ok {
panic("invalid remote create callback")
}
remote, err := data.options.RemoteCreateCallback(repo, name, url)
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
if remote == nil {
panic("no remote created by callback")
}
*out = remote.ptr
// clear finalizer as the calling C function will
// free the remote itself
runtime.SetFinalizer(remote, nil)
remote.repo.Remotes.untrackRemote(remote)
return C.int(ErrorCodeOK)
}
type cloneCallbackData struct {
options *CloneOptions
errorTarget *error
}
func populateCloneOptions(copts *C.git_clone_options, opts *CloneOptions, errorTarget *error) *C.git_clone_options {
C.git_clone_options_init(copts, C.GIT_CLONE_OPTIONS_VERSION)
if opts == nil {
return nil
}
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
populateFetchOptions(&copts.fetch_opts, &opts.FetchOptions, errorTarget)
copts.bare = cbool(opts.Bare)
if opts.RemoteCreateCallback != nil {
data := &cloneCallbackData{
options: opts,
errorTarget: errorTarget,
}
// Go v1.1 does not allow to assign a C function pointer
C._go_git_populate_clone_callbacks(copts)
copts.remote_cb_payload = pointerHandles.Track(data)
}
return copts
}
func freeCloneOptions(copts *C.git_clone_options) {
if copts == nil {
return
}
freeCheckoutOptions(&copts.checkout_opts)
freeFetchOptions(&copts.fetch_opts)
if copts.remote_cb_payload != nil {
pointerHandles.Untrack(copts.remote_cb_payload)
}
C.free(unsafe.Pointer(copts.checkout_branch))
}

87
clone_test.go Normal file
View File

@ -0,0 +1,87 @@
package git
import (
"io/ioutil"
"os"
"testing"
)
const (
REMOTENAME = "testremote"
)
func TestClone(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
path, err := ioutil.TempDir("", "git2go")
checkFatal(t, err)
ref, err := repo.References.Lookup("refs/heads/master")
checkFatal(t, err)
repo2, err := Clone(repo.Path(), path, &CloneOptions{Bare: true})
defer cleanupTestRepo(t, repo2)
checkFatal(t, err)
ref2, err := repo2.References.Lookup("refs/heads/master")
checkFatal(t, err)
if ref.Cmp(ref2) != 0 {
t.Fatal("reference in clone does not match original ref")
}
}
func TestCloneWithCallback(t *testing.T) {
t.Parallel()
testPayload := 0
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
path, err := ioutil.TempDir("", "git2go")
checkFatal(t, err)
opts := CloneOptions{
Bare: true,
RemoteCreateCallback: func(r *Repository, name, url string) (*Remote, error) {
testPayload += 1
return r.Remotes.Create(REMOTENAME, url)
},
}
repo2, err := Clone(repo.Path(), path, &opts)
defer cleanupTestRepo(t, repo2)
checkFatal(t, err)
if testPayload != 1 {
t.Fatal("Payload's value has not been changed")
}
remote, err := repo2.Remotes.Lookup(REMOTENAME)
if err != nil || remote == nil {
t.Fatal("Remote was not created properly")
}
defer remote.Free()
}
// TestCloneWithExternalHTTPUrl
func TestCloneWithExternalHTTPUrl(t *testing.T) {
path, err := ioutil.TempDir("", "git2go")
defer os.RemoveAll(path)
// clone the repo
url := "https://github.com/libgit2/TestGitRepository"
_, err = Clone(url, path, &CloneOptions{})
if err != nil {
t.Fatal("cannot clone remote repo via https, error: ", err)
}
}

245
commit.go
View File

@ -2,105 +2,228 @@ package git
/*
#include <git2.h>
#include <git2/errors.h>
extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr);
*/
import "C"
import (
"runtime"
"unsafe"
"time"
)
// MessageEncoding is the encoding of commit messages.
type MessageEncoding string
const (
// MessageEncodingUTF8 is the default message encoding.
MessageEncodingUTF8 MessageEncoding = "UTF-8"
)
// Commit
type Commit struct {
gitObject
doNotCompare
Object
cast_ptr *C.git_commit
}
func (c Commit) Message() string {
return C.GoString(C.git_commit_message(c.ptr))
func (c *Commit) AsObject() *Object {
return &c.Object
}
func (c Commit) Tree() (*Tree, error) {
var ptr *C.git_object
func (c *Commit) Message() string {
ret := C.GoString(C.git_commit_message(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
err := C.git_commit_tree(&ptr, c.ptr)
if err < 0 {
return nil, LastError()
func (c *Commit) MessageEncoding() MessageEncoding {
ptr := C.git_commit_message_encoding(c.cast_ptr)
if ptr == nil {
return MessageEncodingUTF8
}
ret := C.GoString(ptr)
runtime.KeepAlive(c)
return MessageEncoding(ret)
}
func (c *Commit) RawMessage() string {
ret := C.GoString(C.git_commit_message_raw(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
// RawHeader gets the full raw text of the commit header.
func (c *Commit) RawHeader() string {
ret := C.GoString(C.git_commit_raw_header(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
// ContentToSign returns the content that will be passed to a signing function for this commit
func (c *Commit) ContentToSign() string {
return c.RawHeader() + "\n" + c.RawMessage()
}
// CommitSigningCallback defines a function type that takes some data to sign and returns (signature, signature_field, error)
type CommitSigningCallback func(string) (signature, signatureField string, err error)
// CommitCreateCallback defines a function type that is called when another
// function is going to create commits (for example, Rebase) to allow callers
// to override the commit creation behavior. For example, users may wish to
// sign commits by providing this information to Repository.CreateCommitBuffer,
// signing that buffer, then calling Repository.CreateCommitWithSignature.
type CommitCreateCallback func(
author, committer *Signature,
messageEncoding MessageEncoding,
message string,
tree *Tree,
parents ...*Commit,
) (oid *Oid, err error)
// WithSignatureUsing creates a new signed commit from this one using the given signing callback
func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) {
signature, signatureField, err := f(c.ContentToSign())
if err != nil {
return nil, err
}
return allocObject(ptr).(*Tree), nil
return c.WithSignature(signature, signatureField)
}
func (c Commit) TreeId() *Oid {
return newOidFromC(C.git_commit_tree_id(c.ptr))
// WithSignature creates a new signed commit from the given signature and signature field
func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, error) {
return c.Owner().CreateCommitWithSignature(
c.ContentToSign(),
signature,
signatureField,
)
}
func (c Commit) Author() *Signature {
ptr := C.git_commit_author(c.ptr)
return newSignatureFromC(ptr)
func (c *Commit) ExtractSignature() (string, string, error) {
var c_signed C.git_buf
defer C.git_buf_dispose(&c_signed)
var c_signature C.git_buf
defer C.git_buf_dispose(&c_signature)
oid := c.Id()
repo := C.git_commit_owner(c.cast_ptr)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_commit_extract_signature(&c_signature, &c_signed, repo, oid.toC(), nil)
runtime.KeepAlive(oid)
if ret < 0 {
return "", "", MakeGitError(ret)
} else {
return C.GoString(c_signature.ptr), C.GoString(c_signed.ptr), nil
}
}
func (c Commit) Committer() *Signature {
ptr := C.git_commit_committer(c.ptr)
return newSignatureFromC(ptr)
func (c *Commit) Summary() string {
ret := C.GoString(C.git_commit_summary(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
func (c *Commit) Tree() (*Tree, error) {
var ptr *C.git_tree
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_commit_tree(&ptr, c.cast_ptr)
runtime.KeepAlive(c)
if err < 0 {
return nil, MakeGitError(err)
}
return allocTree(ptr, c.repo), nil
}
func (c *Commit) TreeId() *Oid {
ret := newOidFromC(C.git_commit_tree_id(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
func (c *Commit) Author() *Signature {
cast_ptr := C.git_commit_author(c.cast_ptr)
ret := newSignatureFromC(cast_ptr)
runtime.KeepAlive(c)
return ret
}
func (c *Commit) Committer() *Signature {
cast_ptr := C.git_commit_committer(c.cast_ptr)
ret := newSignatureFromC(cast_ptr)
runtime.KeepAlive(c)
return ret
}
func (c *Commit) Parent(n uint) *Commit {
par := &Commit{}
ret := C.git_commit_parent(&par.ptr, c.ptr, C.uint(n))
var cobj *C.git_commit
ret := C.git_commit_parent(&cobj, c.cast_ptr, C.uint(n))
if ret != 0 {
return nil
}
return par
parent := allocCommit(cobj, c.repo)
runtime.KeepAlive(c)
return parent
}
func (c *Commit) ParentId(n uint) *Oid {
return newOidFromC(C.git_commit_parent_id(c.ptr, C.uint(n)))
ret := newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
runtime.KeepAlive(c)
return ret
}
func (c *Commit) ParentCount() uint {
return uint(C.git_commit_parentcount(c.ptr))
ret := uint(C.git_commit_parentcount(c.cast_ptr))
runtime.KeepAlive(c)
return ret
}
// Signature
type Signature struct {
Name string
Email string
When time.Time
}
func newSignatureFromC(sig *C.git_signature) *Signature {
// git stores minutes, go wants seconds
loc := time.FixedZone("", int(sig.when.offset)*60)
return &Signature{
C.GoString(sig.name),
C.GoString(sig.email),
time.Unix(int64(sig.when.time), 0).In(loc),
}
}
// the offset in mintes, which is what git wants
func (v *Signature) Offset() int {
_, offset := v.When.Zone()
return offset/60
}
func (sig *Signature) toC() (*C.git_signature) {
var out *C.git_signature
name := C.CString(sig.Name)
defer C.free(unsafe.Pointer(name))
email := C.CString(sig.Email)
defer C.free(unsafe.Pointer(email))
ret := C.git_signature_new(&out, name, email, C.git_time_t(sig.When.Unix()), C.int(sig.Offset()))
if ret < 0 {
return nil
func (c *Commit) Amend(refname string, author, committer *Signature, message string, tree *Tree) (*Oid, error) {
var cref *C.char
if refname == "" {
cref = nil
} else {
cref = C.CString(refname)
defer C.free(unsafe.Pointer(cref))
}
return out
cmsg := C.CString(message)
defer C.free(unsafe.Pointer(cmsg))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
authorSig, err := author.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(committerSig)
oid := new(Oid)
cerr := C.git_commit_amend(oid.toC(), c.cast_ptr, cref, authorSig, committerSig, nil, cmsg, tree.cast_ptr)
runtime.KeepAlive(oid)
runtime.KeepAlive(c)
runtime.KeepAlive(tree)
if cerr < 0 {
return nil, MakeGitError(cerr)
}
return oid, nil
}

429
config.go
View File

@ -1,69 +1,468 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
type ConfigLevel int
const (
// System-wide on Windows, for compatibility with portable git
ConfigLevelProgramdata ConfigLevel = C.GIT_CONFIG_LEVEL_PROGRAMDATA
// System-wide configuration file; /etc/gitconfig on Linux systems
ConfigLevelSystem ConfigLevel = C.GIT_CONFIG_LEVEL_SYSTEM
// XDG compatible configuration file; typically ~/.config/git/config
ConfigLevelXDG ConfigLevel = C.GIT_CONFIG_LEVEL_XDG
// User-specific configuration file (also called Global configuration
// file); typically ~/.gitconfig
ConfigLevelGlobal ConfigLevel = C.GIT_CONFIG_LEVEL_GLOBAL
// Repository specific configuration file; $WORK_DIR/.git/config on
// non-bare repos
ConfigLevelLocal ConfigLevel = C.GIT_CONFIG_LEVEL_LOCAL
// Application specific configuration file; freely defined by applications
ConfigLevelApp ConfigLevel = C.GIT_CONFIG_LEVEL_APP
// Represents the highest level available config file (i.e. the most
// specific config file available that actually is loaded)
ConfigLevelHighest ConfigLevel = C.GIT_CONFIG_HIGHEST_LEVEL
)
type ConfigEntry struct {
Name string
Value string
Level ConfigLevel
}
func newConfigEntryFromC(centry *C.git_config_entry) *ConfigEntry {
return &ConfigEntry{
Name: C.GoString(centry.name),
Value: C.GoString(centry.value),
Level: ConfigLevel(centry.level),
}
}
type Config struct {
doNotCompare
ptr *C.git_config
}
func (c *Config) LookupInt32(name string) (v int32, err error) {
// NewConfig creates a new empty configuration object
func NewConfig() (*Config, error) {
config := new(Config)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if ret := C.git_config_new(&config.ptr); ret < 0 {
return nil, MakeGitError(ret)
}
return config, nil
}
// AddFile adds a file-backed backend to the config object at the specified level.
func (c *Config) AddFile(path string, level ConfigLevel, force bool) error {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_add_file_ondisk(c.ptr, cpath, C.git_config_level_t(level), nil, cbool(force))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) LookupInt32(name string) (int32, error) {
var out C.int32_t
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_get_int32(&out, c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return 0, LastError()
return 0, MakeGitError(ret)
}
return int32(out), nil
}
func (c *Config) LookupInt64(name string) (v int64, err error) {
func (c *Config) LookupInt64(name string) (int64, error) {
var out C.int64_t
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_get_int64(&out, c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return 0, LastError()
return 0, MakeGitError(ret)
}
return int64(out), nil
}
func (c *Config) LookupString(name string) (v string, err error) {
var ptr *C.char
func (c *Config) LookupString(name string) (string, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ret := C.git_config_get_string(&ptr, c.ptr, cname)
if ret < 0 {
return "", LastError()
}
valBuf := C.git_buf{}
return C.GoString(ptr), nil
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_get_string_buf(&valBuf, c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_dispose(&valBuf)
return C.GoString(valBuf.ptr), nil
}
func (c *Config) Set(name, value string) (err error) {
func (c *Config) LookupBool(name string) (bool, error) {
var out C.int
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_get_bool(&out, c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return false, MakeGitError(ret)
}
return out != 0, nil
}
func (c *Config) NewMultivarIterator(name, regexp string) (*ConfigIterator, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var cregexp *C.char
if regexp == "" {
cregexp = nil
} else {
cregexp = C.CString(regexp)
defer C.free(unsafe.Pointer(cregexp))
}
iter := &ConfigIterator{cfg: c}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_multivar_iterator_new(&iter.ptr, c.ptr, cname, cregexp)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(iter, (*ConfigIterator).Free)
return iter, nil
}
// NewIterator creates an iterator over each entry in the
// configuration
func (c *Config) NewIterator() (*ConfigIterator, error) {
iter := &ConfigIterator{cfg: c}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_iterator_new(&iter.ptr, c.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
return iter, nil
}
// NewIteratorGlob creates an iterator over each entry in the
// configuration whose name matches the given regular expression
func (c *Config) NewIteratorGlob(regexp string) (*ConfigIterator, error) {
iter := &ConfigIterator{cfg: c}
cregexp := C.CString(regexp)
defer C.free(unsafe.Pointer(cregexp))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_iterator_glob_new(&iter.ptr, c.ptr, cregexp)
if ret < 0 {
return nil, MakeGitError(ret)
}
return iter, nil
}
func (c *Config) SetString(name, value string) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
cvalue := C.CString(value)
defer C.free(unsafe.Pointer(cvalue))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_string(c.ptr, cname, cvalue)
runtime.KeepAlive(c)
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (c *Config) Free() {
runtime.SetFinalizer(c, nil)
C.git_config_free(c.ptr)
}
func (c *Config) SetInt32(name string, value int32) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_int32(c.ptr, cname, C.int32_t(value))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) SetInt64(name string, value int64) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_int64(c.ptr, cname, C.int64_t(value))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) SetBool(name string, value bool) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_bool(c.ptr, cname, cbool(value))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) SetMultivar(name, regexp, value string) (err error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
cregexp := C.CString(regexp)
defer C.free(unsafe.Pointer(cregexp))
cvalue := C.CString(value)
defer C.free(unsafe.Pointer(cvalue))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_set_multivar(c.ptr, cname, cregexp, cvalue)
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (c *Config) Delete(name string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_delete_entry(c.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// OpenLevel creates a single-level focused config object from a multi-level one
func (c *Config) OpenLevel(parent *Config, level ConfigLevel) (*Config, error) {
config := new(Config)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_open_level(&config.ptr, parent.ptr, C.git_config_level_t(level))
runtime.KeepAlive(c)
runtime.KeepAlive(parent)
if ret < 0 {
return nil, MakeGitError(ret)
}
return config, nil
}
// OpenOndisk creates a new config instance containing a single on-disk file
func OpenOndisk(path string) (*Config, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
config := new(Config)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if ret := C.git_config_open_ondisk(&config.ptr, cpath); ret < 0 {
return nil, MakeGitError(ret)
}
return config, nil
}
type ConfigIterator struct {
doNotCompare
ptr *C.git_config_iterator
cfg *Config
}
// Next returns the next entry for this iterator
func (iter *ConfigIterator) Next() (*ConfigEntry, error) {
var centry *C.git_config_entry
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_next(&centry, iter.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
entry := newConfigEntryFromC(centry)
runtime.KeepAlive(iter)
return entry, nil
}
func (iter *ConfigIterator) Free() {
runtime.SetFinalizer(iter, nil)
C.free(unsafe.Pointer(iter.ptr))
}
func ConfigFindGlobal() (string, error) {
var buf C.git_buf
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_find_global(&buf)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(buf.ptr), nil
}
func ConfigFindSystem() (string, error) {
var buf C.git_buf
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_find_system(&buf)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(buf.ptr), nil
}
func ConfigFindXDG() (string, error) {
var buf C.git_buf
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_find_xdg(&buf)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(buf.ptr), nil
}
// ConfigFindProgramdata locate the path to the configuration file in ProgramData.
//
// Look for the file in %PROGRAMDATA%\Git\config used by portable git.
func ConfigFindProgramdata() (string, error) {
var buf C.git_buf
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_config_find_programdata(&buf)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(buf.ptr), nil
}
// OpenDefault opens the default config according to git rules
func OpenDefault() (*Config, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
config := new(Config)
if ret := C.git_config_open_default(&config.ptr); ret < 0 {
return nil, MakeGitError(ret)
}
return config, nil
}

119
config_test.go Normal file
View File

@ -0,0 +1,119 @@
package git
import (
"os"
"testing"
)
var tempConfig = "./temp.gitconfig"
func setupConfig() (*Config, error) {
var (
c *Config
err error
)
c, err = OpenOndisk(tempConfig)
if err != nil {
return nil, err
}
err = c.SetString("foo.bar", "baz")
if err != nil {
return nil, err
}
err = c.SetBool("foo.bool", true)
if err != nil {
return nil, err
}
err = c.SetInt32("foo.int32", 32)
if err != nil {
return nil, err
}
err = c.SetInt64("foo.int64", 64)
if err != nil {
return nil, err
}
return c, err
}
func cleanupConfig() {
os.Remove(tempConfig)
}
type TestRunner func(*Config, *testing.T)
var tests = []TestRunner{
// LookupString
func(c *Config, t *testing.T) {
val, err := c.LookupString("foo.bar")
if err != nil {
t.Errorf("Got LookupString error: '%v', expected none\n", err)
}
if val != "baz" {
t.Errorf("Got '%s' from LookupString, expected 'bar'\n", val)
}
},
// LookupBool
func(c *Config, t *testing.T) {
val, err := c.LookupBool("foo.bool")
if err != nil {
t.Errorf("Got LookupBool error: '%v', expected none\n", err)
}
if !val {
t.Errorf("Got %t from LookupBool, expected 'false'\n", val)
}
},
// LookupInt32
func(c *Config, t *testing.T) {
val, err := c.LookupInt32("foo.int32")
if err != nil {
t.Errorf("Got LookupInt32 error: '%v', expected none\n", err)
}
if val != 32 {
t.Errorf("Got %v, expected 32\n", val)
}
},
// LookupInt64
func(c *Config, t *testing.T) {
val, err := c.LookupInt64("foo.int64")
if err != nil {
t.Errorf("Got LookupInt64 error: '%v', expected none\n", err)
}
if val != 64 {
t.Errorf("Got %v, expected 64\n", val)
}
},
}
func TestConfigLookups(t *testing.T) {
t.Parallel()
var (
err error
c *Config
)
c, err = setupConfig()
defer cleanupConfig()
if err != nil {
t.Errorf("Setup error: '%v'. Expected none\n", err)
return
}
defer c.Free()
for _, test := range tests {
test(c, t)
}
}
func TestOpenDefault(t *testing.T) {
c, err := OpenDefault()
if err != nil {
t.Errorf("OpenDefault error: '%v'. Expected none\n", err)
return
}
defer c.Free()
}

298
credentials.go Normal file
View File

@ -0,0 +1,298 @@
package git
/*
#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_credential_ssh_custom *cred);
*/
import "C"
import (
"crypto/rand"
"errors"
"fmt"
"runtime"
"strings"
"unsafe"
"golang.org/x/crypto/ssh"
)
// CredentialType is a bitmask of supported credential types.
//
// This represents the various types of authentication methods supported by the
// library.
type CredentialType uint
const (
CredentialTypeUserpassPlaintext CredentialType = C.GIT_CREDENTIAL_USERPASS_PLAINTEXT
CredentialTypeSSHKey CredentialType = C.GIT_CREDENTIAL_SSH_KEY
CredentialTypeSSHCustom CredentialType = C.GIT_CREDENTIAL_SSH_CUSTOM
CredentialTypeDefault CredentialType = C.GIT_CREDENTIAL_DEFAULT
CredentialTypeSSHInteractive CredentialType = C.GIT_CREDENTIAL_SSH_INTERACTIVE
CredentialTypeUsername CredentialType = C.GIT_CREDENTIAL_USERNAME
CredentialTypeSSHMemory CredentialType = C.GIT_CREDENTIAL_SSH_MEMORY
)
func (t CredentialType) String() string {
if t == 0 {
return "CredentialType(0)"
}
var parts []string
if (t & CredentialTypeUserpassPlaintext) != 0 {
parts = append(parts, "UserpassPlaintext")
t &= ^CredentialTypeUserpassPlaintext
}
if (t & CredentialTypeSSHKey) != 0 {
parts = append(parts, "SSHKey")
t &= ^CredentialTypeSSHKey
}
if (t & CredentialTypeSSHCustom) != 0 {
parts = append(parts, "SSHCustom")
t &= ^CredentialTypeSSHCustom
}
if (t & CredentialTypeDefault) != 0 {
parts = append(parts, "Default")
t &= ^CredentialTypeDefault
}
if (t & CredentialTypeSSHInteractive) != 0 {
parts = append(parts, "SSHInteractive")
t &= ^CredentialTypeSSHInteractive
}
if (t & CredentialTypeUsername) != 0 {
parts = append(parts, "Username")
t &= ^CredentialTypeUsername
}
if (t & CredentialTypeSSHMemory) != 0 {
parts = append(parts, "SSHMemory")
t &= ^CredentialTypeSSHMemory
}
if t != 0 {
parts = append(parts, fmt.Sprintf("CredentialType(%#x)", t))
}
return strings.Join(parts, "|")
}
type Credential struct {
doNotCompare
ptr *C.git_credential
}
func newCredential() *Credential {
cred := &Credential{}
runtime.SetFinalizer(cred, (*Credential).Free)
return cred
}
func (o *Credential) HasUsername() bool {
if C.git_credential_has_username(o.ptr) == 1 {
return true
}
return false
}
func (o *Credential) Type() CredentialType {
return (CredentialType)(C._go_git_credential_credtype(o.ptr))
}
func (o *Credential) Free() {
C.git_credential_free(o.ptr)
runtime.SetFinalizer(o, nil)
o.ptr = nil
}
// GetUserpassPlaintext returns the plaintext username/password combination stored in the Cred.
func (o *Credential) GetUserpassPlaintext() (username, password string, err error) {
if o.Type() != CredentialTypeUserpassPlaintext {
err = errors.New("credential is not userpass plaintext")
return
}
plaintextCredPtr := (*C.git_cred_userpass_plaintext)(unsafe.Pointer(o.ptr))
username = C.GoString(plaintextCredPtr.username)
password = C.GoString(plaintextCredPtr.password)
return
}
// GetSSHKey returns the SSH-specific key information from the Cred object.
func (o *Credential) GetSSHKey() (username, publickey, privatekey, passphrase string, err error) {
if o.Type() != CredentialTypeSSHKey && o.Type() != CredentialTypeSSHMemory {
err = fmt.Errorf("credential is not an SSH key: %v", o.Type())
return
}
sshKeyCredPtr := (*C.git_cred_ssh_key)(unsafe.Pointer(o.ptr))
username = C.GoString(sshKeyCredPtr.username)
publickey = C.GoString(sshKeyCredPtr.publickey)
privatekey = C.GoString(sshKeyCredPtr.privatekey)
passphrase = C.GoString(sshKeyCredPtr.passphrase)
return
}
func NewCredentialUsername(username string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
ret := C.git_credential_username_new(&cred.ptr, cusername)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
func NewCredentialUserpassPlaintext(username string, password string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
cpassword := C.CString(password)
defer C.free(unsafe.Pointer(cpassword))
ret := C.git_credential_userpass_plaintext_new(&cred.ptr, cusername, cpassword)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
// NewCredentialSSHKey creates new ssh credentials reading the public and private keys
// from the file system.
func NewCredentialSSHKey(username string, publicKeyPath string, privateKeyPath string, passphrase string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
cpublickey := C.CString(publicKeyPath)
defer C.free(unsafe.Pointer(cpublickey))
cprivatekey := C.CString(privateKeyPath)
defer C.free(unsafe.Pointer(cprivatekey))
cpassphrase := C.CString(passphrase)
defer C.free(unsafe.Pointer(cpassphrase))
ret := C.git_credential_ssh_key_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
// NewCredentialSSHKeyFromMemory creates new ssh credentials using the publicKey and privateKey
// arguments as the values for the public and private keys.
func NewCredentialSSHKeyFromMemory(username string, publicKey string, privateKey string, passphrase string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
cpublickey := C.CString(publicKey)
defer C.free(unsafe.Pointer(cpublickey))
cprivatekey := C.CString(privateKey)
defer C.free(unsafe.Pointer(cprivatekey))
cpassphrase := C.CString(passphrase)
defer C.free(unsafe.Pointer(cpassphrase))
ret := C.git_credential_ssh_key_memory_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
func NewCredentialSSHKeyFromAgent(username string) (*Credential, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cred := newCredential()
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
ret := C.git_credential_ssh_key_from_agent(&cred.ptr, cusername)
if ret != 0 {
cred.Free()
return nil, MakeGitError(ret)
}
return cred, nil
}
type credentialSSHCustomData struct {
signer ssh.Signer
}
//export credentialSSHCustomFree
func credentialSSHCustomFree(cred *C.git_credential_ssh_custom) {
if cred == nil {
return
}
C.free(unsafe.Pointer(cred.username))
C.free(unsafe.Pointer(cred.publickey))
pointerHandles.Untrack(cred.payload)
C.free(unsafe.Pointer(cred))
}
//export credentialSSHSignCallback
func credentialSSHSignCallback(
errorMessage **C.char,
sig **C.uchar,
sig_len *C.size_t,
data *C.uchar,
data_len C.size_t,
handle unsafe.Pointer,
) C.int {
signer := pointerHandles.Get(handle).(*credentialSSHCustomData).signer
signature, err := signer.Sign(rand.Reader, C.GoBytes(unsafe.Pointer(data), C.int(data_len)))
if err != nil {
return setCallbackError(errorMessage, err)
}
*sig = (*C.uchar)(C.CBytes(signature.Blob))
*sig_len = C.size_t(len(signature.Blob))
return C.int(ErrorCodeOK)
}
// NewCredentialSSHKeyFromSigner creates new SSH credentials using the provided signer.
func NewCredentialSSHKeyFromSigner(username string, signer ssh.Signer) (*Credential, error) {
publicKey := signer.PublicKey().Marshal()
ccred := (*C.git_credential_ssh_custom)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_credential_ssh_custom{}))))
ccred.parent.credtype = C.GIT_CREDENTIAL_SSH_CUSTOM
ccred.username = C.CString(username)
ccred.publickey = (*C.char)(C.CBytes(publicKey))
ccred.publickey_len = C.size_t(len(publicKey))
C._go_git_populate_credential_ssh_custom(ccred)
data := credentialSSHCustomData{
signer: signer,
}
ccred.payload = pointerHandles.Track(&data)
cred := newCredential()
cred.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
}

33
delta_string.go Normal file
View File

@ -0,0 +1,33 @@
// Code generated by "stringer -type Delta -trimprefix Delta -tags static"; DO NOT EDIT.
package git
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[DeltaUnmodified-0]
_ = x[DeltaAdded-1]
_ = x[DeltaDeleted-2]
_ = x[DeltaModified-3]
_ = x[DeltaRenamed-4]
_ = x[DeltaCopied-5]
_ = x[DeltaIgnored-6]
_ = x[DeltaUntracked-7]
_ = x[DeltaTypeChange-8]
_ = x[DeltaUnreadable-9]
_ = x[DeltaConflicted-10]
}
const _Delta_name = "UnmodifiedAddedDeletedModifiedRenamedCopiedIgnoredUntrackedTypeChangeUnreadableConflicted"
var _Delta_index = [...]uint8{0, 10, 15, 22, 30, 37, 43, 50, 59, 69, 79, 89}
func (i Delta) String() string {
if i < 0 || i >= Delta(len(_Delta_index)-1) {
return "Delta(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _Delta_name[_Delta_index[i]:_Delta_index[i+1]]
}

271
deprecated.go Normal file
View File

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

226
describe.go Normal file
View File

@ -0,0 +1,226 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
// DescribeOptions represents the describe operation configuration.
//
// You can use DefaultDescribeOptions() to get default options.
type DescribeOptions struct {
// How many tags as candidates to consider to describe the input commit-ish.
// Increasing it above 10 will take slightly longer but may produce a more
// accurate result. 0 will cause only exact matches to be output.
MaxCandidatesTags uint // default: 10
// By default describe only shows annotated tags. Change this in order
// to show all refs from refs/tags or refs/.
Strategy DescribeOptionsStrategy // default: DescribeDefault
// Only consider tags matching the given glob(7) pattern, excluding
// the "refs/tags/" prefix. Can be used to avoid leaking private
// tags from the repo.
Pattern string
// When calculating the distance from the matching tag or
// reference, only walk down the first-parent ancestry.
OnlyFollowFirstParent bool
// If no matching tag or reference is found, the describe
// operation would normally fail. If this option is set, it
// will instead fall back to showing the full id of the commit.
ShowCommitOidAsFallback bool
}
// DefaultDescribeOptions returns default options for the describe operation.
func DefaultDescribeOptions() (DescribeOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
opts := C.git_describe_options{}
ecode := C.git_describe_options_init(&opts, C.GIT_DESCRIBE_OPTIONS_VERSION)
if ecode < 0 {
return DescribeOptions{}, MakeGitError(ecode)
}
return DescribeOptions{
MaxCandidatesTags: uint(opts.max_candidates_tags),
Strategy: DescribeOptionsStrategy(opts.describe_strategy),
}, nil
}
// DescribeFormatOptions can be used for formatting the describe string.
//
// You can use DefaultDescribeFormatOptions() to get default options.
type DescribeFormatOptions struct {
// Size of the abbreviated commit id to use. This value is the
// lower bound for the length of the abbreviated string.
AbbreviatedSize uint // default: 7
// Set to use the long format even when a shorter name could be used.
AlwaysUseLongFormat bool
// If the workdir is dirty and this is set, this string will be
// appended to the description string.
DirtySuffix string
}
// DefaultDescribeFormatOptions returns default options for formatting
// the output.
func DefaultDescribeFormatOptions() (DescribeFormatOptions, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
opts := C.git_describe_format_options{}
ecode := C.git_describe_format_options_init(&opts, C.GIT_DESCRIBE_FORMAT_OPTIONS_VERSION)
if ecode < 0 {
return DescribeFormatOptions{}, MakeGitError(ecode)
}
return DescribeFormatOptions{
AbbreviatedSize: uint(opts.abbreviated_size),
AlwaysUseLongFormat: opts.always_use_long_format == 1,
}, nil
}
// DescribeOptionsStrategy behaves like the --tags and --all options
// to git-describe, namely they say to look for any reference in
// either refs/tags/ or refs/ respectively.
//
// By default it only shows annotated tags.
type DescribeOptionsStrategy uint
// Describe strategy options.
const (
DescribeDefault DescribeOptionsStrategy = C.GIT_DESCRIBE_DEFAULT
DescribeTags DescribeOptionsStrategy = C.GIT_DESCRIBE_TAGS
DescribeAll DescribeOptionsStrategy = C.GIT_DESCRIBE_ALL
)
// Describe performs the describe operation on the commit.
func (c *Commit) Describe(opts *DescribeOptions) (*DescribeResult, error) {
var resultPtr *C.git_describe_result
var cDescribeOpts *C.git_describe_options
if opts != nil {
var cpattern *C.char
if len(opts.Pattern) > 0 {
cpattern = C.CString(opts.Pattern)
defer C.free(unsafe.Pointer(cpattern))
}
cDescribeOpts = &C.git_describe_options{
version: C.GIT_DESCRIBE_OPTIONS_VERSION,
max_candidates_tags: C.uint(opts.MaxCandidatesTags),
describe_strategy: C.uint(opts.Strategy),
pattern: cpattern,
only_follow_first_parent: cbool(opts.OnlyFollowFirstParent),
show_commit_oid_as_fallback: cbool(opts.ShowCommitOidAsFallback),
}
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_describe_commit(&resultPtr, c.ptr, cDescribeOpts)
runtime.KeepAlive(c)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newDescribeResultFromC(resultPtr), nil
}
// DescribeWorkdir describes the working tree. It means describe HEAD
// and appends <mark> (-dirty by default) if the working tree is dirty.
func (repo *Repository) DescribeWorkdir(opts *DescribeOptions) (*DescribeResult, error) {
var resultPtr *C.git_describe_result
var cDescribeOpts *C.git_describe_options
if opts != nil {
var cpattern *C.char
if len(opts.Pattern) > 0 {
cpattern = C.CString(opts.Pattern)
defer C.free(unsafe.Pointer(cpattern))
}
cDescribeOpts = &C.git_describe_options{
version: C.GIT_DESCRIBE_OPTIONS_VERSION,
max_candidates_tags: C.uint(opts.MaxCandidatesTags),
describe_strategy: C.uint(opts.Strategy),
pattern: cpattern,
only_follow_first_parent: cbool(opts.OnlyFollowFirstParent),
show_commit_oid_as_fallback: cbool(opts.ShowCommitOidAsFallback),
}
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_describe_workdir(&resultPtr, repo.ptr, cDescribeOpts)
runtime.KeepAlive(repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newDescribeResultFromC(resultPtr), nil
}
// DescribeResult represents the output from the 'git_describe_commit'
// and 'git_describe_workdir' functions in libgit2.
//
// Use Format() to get a string out of it.
type DescribeResult struct {
doNotCompare
ptr *C.git_describe_result
}
func newDescribeResultFromC(ptr *C.git_describe_result) *DescribeResult {
result := &DescribeResult{
ptr: ptr,
}
runtime.SetFinalizer(result, (*DescribeResult).Free)
return result
}
// Format prints the DescribeResult as a string.
func (result *DescribeResult) Format(opts *DescribeFormatOptions) (string, error) {
resultBuf := C.git_buf{}
var cFormatOpts *C.git_describe_format_options
if opts != nil {
cDirtySuffix := C.CString(opts.DirtySuffix)
defer C.free(unsafe.Pointer(cDirtySuffix))
cFormatOpts = &C.git_describe_format_options{
version: C.GIT_DESCRIBE_FORMAT_OPTIONS_VERSION,
abbreviated_size: C.uint(opts.AbbreviatedSize),
always_use_long_format: cbool(opts.AlwaysUseLongFormat),
dirty_suffix: cDirtySuffix,
}
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_describe_format(&resultBuf, result.ptr, cFormatOpts)
runtime.KeepAlive(result)
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_dispose(&resultBuf)
return C.GoString(resultBuf.ptr), nil
}
// Free cleans up the C reference.
func (result *DescribeResult) Free() {
runtime.SetFinalizer(result, nil)
C.git_describe_result_free(result.ptr)
result.ptr = nil
}

109
describe_test.go Normal file
View File

@ -0,0 +1,109 @@
package git
import (
"path"
"runtime"
"strings"
"testing"
)
func TestDescribeCommit(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
describeOpts, err := DefaultDescribeOptions()
checkFatal(t, err)
formatOpts, err := DefaultDescribeFormatOptions()
checkFatal(t, err)
commitID, _ := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
// No annotated tags can be used to describe master
_, err = commit.Describe(&describeOpts)
checkDescribeNoRefsFound(t, err)
// Fallback
fallback := describeOpts
fallback.ShowCommitOidAsFallback = true
result, err := commit.Describe(&fallback)
checkFatal(t, err)
resultStr, err := result.Format(&formatOpts)
checkFatal(t, err)
compareStrings(t, "473bf77", resultStr)
// Abbreviated
abbreviated := formatOpts
abbreviated.AbbreviatedSize = 2
result, err = commit.Describe(&fallback)
checkFatal(t, err)
resultStr, err = result.Format(&abbreviated)
checkFatal(t, err)
compareStrings(t, "473b", resultStr)
createTestTag(t, repo, commit)
// Exact tag
patternOpts := describeOpts
patternOpts.Pattern = "v[0-9]*"
result, err = commit.Describe(&patternOpts)
checkFatal(t, err)
resultStr, err = result.Format(&formatOpts)
checkFatal(t, err)
compareStrings(t, "v0.0.0", resultStr)
// Pattern no match
patternOpts.Pattern = "v[1-9]*"
result, err = commit.Describe(&patternOpts)
checkDescribeNoRefsFound(t, err)
commitID, _ = updateReadme(t, repo, "update1")
commit, err = repo.LookupCommit(commitID)
checkFatal(t, err)
// Tag-1
result, err = commit.Describe(&describeOpts)
checkFatal(t, err)
resultStr, err = result.Format(&formatOpts)
checkFatal(t, err)
compareStrings(t, "v0.0.0-1-gd88ef8d", resultStr)
// Strategy: All
describeOpts.Strategy = DescribeAll
result, err = commit.Describe(&describeOpts)
checkFatal(t, err)
resultStr, err = result.Format(&formatOpts)
checkFatal(t, err)
compareStrings(t, "heads/master", resultStr)
repo.CreateBranch("hotfix", commit, false)
// Workdir (branch)
result, err = repo.DescribeWorkdir(&describeOpts)
checkFatal(t, err)
resultStr, err = result.Format(&formatOpts)
checkFatal(t, err)
compareStrings(t, "heads/hotfix", resultStr)
}
func checkDescribeNoRefsFound(t *testing.T, err error) {
// The failure happens at wherever we were called, not here
_, file, line, ok := runtime.Caller(1)
expectedString := "no reference found, cannot describe anything"
if !ok {
t.Fatalf("Unable to get caller")
}
if err == nil || !strings.Contains(err.Error(), expectedString) {
t.Fatalf(
"%s:%v: was expecting error %v, got %v",
path.Base(file),
line,
expectedString,
err,
)
}
}

1103
diff.go Normal file

File diff suppressed because it is too large Load Diff

685
diff_test.go Normal file
View File

@ -0,0 +1,685 @@
package git
import (
"errors"
"fmt"
"io/ioutil"
"path"
"reflect"
"strings"
"testing"
)
func TestFindSimilar(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
originalTree, newTree := createTestTrees(t, repo)
diffOpt, _ := DefaultDiffOptions()
diff, err := repo.DiffTreeToTree(originalTree, newTree, &diffOpt)
checkFatal(t, err)
if diff == nil {
t.Fatal("no diff returned")
}
findOpts, err := DefaultDiffFindOptions()
checkFatal(t, err)
findOpts.Flags = DiffFindBreakRewrites
err = diff.FindSimilar(&findOpts)
checkFatal(t, err)
numDiffs := 0
numAdded := 0
numDeleted := 0
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
numDiffs++
switch file.Status {
case DeltaAdded:
numAdded++
case DeltaDeleted:
numDeleted++
}
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
return func(line DiffLine) error {
return nil
}, nil
}, nil
}, DiffDetailLines)
if numDiffs != 2 {
t.Fatal("Incorrect number of files in diff")
}
if numAdded != 1 {
t.Fatal("Incorrect number of new files in diff")
}
if numDeleted != 1 {
t.Fatal("Incorrect number of deleted files in diff")
}
}
func TestDiffTreeToTree(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
originalTree, newTree := createTestTrees(t, repo)
callbackInvoked := false
opts := DiffOptions{
NotifyCallback: func(diffSoFar *Diff, delta DiffDelta, matchedPathSpec string) error {
callbackInvoked = true
return nil
},
OldPrefix: "x1/",
NewPrefix: "y1/",
}
diff, err := repo.DiffTreeToTree(originalTree, newTree, &opts)
checkFatal(t, err)
if !callbackInvoked {
t.Fatal("callback not invoked")
}
if diff == nil {
t.Fatal("no diff returned")
}
files := make([]string, 0)
hunks := make([]DiffHunk, 0)
lines := make([]DiffLine, 0)
patches := make([]string, 0)
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
patch, err := diff.Patch(len(patches))
if err != nil {
return nil, err
}
defer patch.Free()
patchStr, err := patch.String()
if err != nil {
return nil, err
}
patches = append(patches, patchStr)
files = append(files, file.OldFile.Path)
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
hunks = append(hunks, hunk)
return func(line DiffLine) error {
lines = append(lines, line)
return nil
}, nil
}, nil
}, DiffDetailLines)
checkFatal(t, err)
if len(files) != 1 {
t.Fatal("Incorrect number of files in diff")
}
if files[0] != "README" {
t.Fatal("File in diff was expected to be README")
}
if len(hunks) != 1 {
t.Fatal("Incorrect number of hunks in diff")
}
if hunks[0].OldStart != 1 || hunks[0].NewStart != 1 {
t.Fatal("Incorrect hunk")
}
if len(lines) != 2 {
t.Fatal("Incorrect number of lines in diff")
}
if lines[0].Content != "foo\n" {
t.Fatal("Incorrect lines in diff")
}
if lines[1].Content != "file changed\n" {
t.Fatal("Incorrect lines in diff")
}
if want1, want2 := "x1/README", "y1/README"; !strings.Contains(patches[0], want1) || !strings.Contains(patches[0], want2) {
t.Errorf("Diff patch doesn't contain %q or %q\n\n%s", want1, want2, patches[0])
}
stats, err := diff.Stats()
checkFatal(t, err)
if stats.Insertions() != 1 {
t.Fatal("Incorrect number of insertions in diff")
}
if stats.Deletions() != 1 {
t.Fatal("Incorrect number of deletions in diff")
}
if stats.FilesChanged() != 1 {
t.Fatal("Incorrect number of changed files in diff")
}
errTest := errors.New("test error")
err = diff.ForEach(func(file DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
return nil, errTest
}, DiffDetailLines)
if err != errTest {
t.Fatalf("Expected custom error to be returned, got %v, want %v", err, errTest)
}
}
func createTestTrees(t *testing.T, repo *Repository) (originalTree *Tree, newTree *Tree) {
var err error
_, originalTreeId := seedTestRepo(t, repo)
originalTree, err = repo.LookupTree(originalTreeId)
checkFatal(t, err)
_, newTreeId := updateReadme(t, repo, "file changed\n")
newTree, err = repo.LookupTree(newTreeId)
checkFatal(t, err)
return originalTree, newTree
}
func TestDiffBlobs(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
id1, err := odb.Write([]byte("hello\nhello\n"), ObjectBlob)
checkFatal(t, err)
id2, err := odb.Write([]byte("hallo\nhallo\n"), ObjectBlob)
checkFatal(t, err)
blob1, err := repo.LookupBlob(id1)
checkFatal(t, err)
blob2, err := repo.LookupBlob(id2)
checkFatal(t, err)
var files, hunks, lines int
err = DiffBlobs(blob1, "hi", blob2, "hi", nil,
func(delta DiffDelta, progress float64) (DiffForEachHunkCallback, error) {
files++
return func(hunk DiffHunk) (DiffForEachLineCallback, error) {
hunks++
return func(line DiffLine) error {
lines++
return nil
}, nil
}, nil
},
DiffDetailLines)
if files != 1 {
t.Fatal("Bad number of files iterated")
}
if hunks != 1 {
t.Fatal("Bad number of hunks iterated")
}
// two removals, two additions
if lines != 4 {
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
}

56
difflinetype_string.go Normal file
View File

@ -0,0 +1,56 @@
// Code generated by "stringer -type DiffLineType -trimprefix DiffLine -tags static"; DO NOT EDIT.
package git
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[DiffLineContext-32]
_ = x[DiffLineAddition-43]
_ = x[DiffLineDeletion-45]
_ = x[DiffLineContextEOFNL-61]
_ = x[DiffLineAddEOFNL-62]
_ = x[DiffLineDelEOFNL-60]
_ = x[DiffLineFileHdr-70]
_ = x[DiffLineHunkHdr-72]
_ = x[DiffLineBinary-66]
}
const (
_DiffLineType_name_0 = "Context"
_DiffLineType_name_1 = "Addition"
_DiffLineType_name_2 = "Deletion"
_DiffLineType_name_3 = "DelEOFNLContextEOFNLAddEOFNL"
_DiffLineType_name_4 = "Binary"
_DiffLineType_name_5 = "FileHdr"
_DiffLineType_name_6 = "HunkHdr"
)
var (
_DiffLineType_index_3 = [...]uint8{0, 8, 20, 28}
)
func (i DiffLineType) String() string {
switch {
case i == 32:
return _DiffLineType_name_0
case i == 43:
return _DiffLineType_name_1
case i == 45:
return _DiffLineType_name_2
case 60 <= i && i <= 62:
i -= 60
return _DiffLineType_name_3[_DiffLineType_index_3[i]:_DiffLineType_index_3[i+1]]
case i == 66:
return _DiffLineType_name_4
case i == 70:
return _DiffLineType_name_5
case i == 72:
return _DiffLineType_name_6
default:
return "DiffLineType(" + strconv.FormatInt(int64(i), 10) + ")"
}
}

63
errorclass_string.go Normal file
View File

@ -0,0 +1,63 @@
// Code generated by "stringer -type ErrorClass -trimprefix ErrorClass -tags static"; DO NOT EDIT.
package git
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[ErrorClassNone-0]
_ = x[ErrorClassNoMemory-1]
_ = x[ErrorClassOS-2]
_ = x[ErrorClassInvalid-3]
_ = x[ErrorClassReference-4]
_ = x[ErrorClassZlib-5]
_ = x[ErrorClassRepository-6]
_ = x[ErrorClassConfig-7]
_ = x[ErrorClassRegex-8]
_ = x[ErrorClassOdb-9]
_ = x[ErrorClassIndex-10]
_ = x[ErrorClassObject-11]
_ = x[ErrorClassNet-12]
_ = x[ErrorClassTag-13]
_ = x[ErrorClassTree-14]
_ = x[ErrorClassIndexer-15]
_ = x[ErrorClassSSL-16]
_ = x[ErrorClassSubmodule-17]
_ = x[ErrorClassThread-18]
_ = x[ErrorClassStash-19]
_ = x[ErrorClassCheckout-20]
_ = x[ErrorClassFetchHead-21]
_ = x[ErrorClassMerge-22]
_ = x[ErrorClassSSH-23]
_ = x[ErrorClassFilter-24]
_ = x[ErrorClassRevert-25]
_ = x[ErrorClassCallback-26]
_ = x[ErrorClassRebase-29]
_ = x[ErrorClassPatch-31]
}
const (
_ErrorClass_name_0 = "NoneNoMemoryOSInvalidReferenceZlibRepositoryConfigRegexOdbIndexObjectNetTagTreeIndexerSSLSubmoduleThreadStashCheckoutFetchHeadMergeSSHFilterRevertCallback"
_ErrorClass_name_1 = "Rebase"
_ErrorClass_name_2 = "Patch"
)
var (
_ErrorClass_index_0 = [...]uint8{0, 4, 12, 14, 21, 30, 34, 44, 50, 55, 58, 63, 69, 72, 75, 79, 86, 89, 98, 104, 109, 117, 126, 131, 134, 140, 146, 154}
)
func (i ErrorClass) String() string {
switch {
case 0 <= i && i <= 26:
return _ErrorClass_name_0[_ErrorClass_index_0[i]:_ErrorClass_index_0[i+1]]
case i == 29:
return _ErrorClass_name_1
case i == 31:
return _ErrorClass_name_2
default:
return "ErrorClass(" + strconv.FormatInt(int64(i), 10) + ")"
}
}

69
errorcode_string.go Normal file
View File

@ -0,0 +1,69 @@
// Code generated by "stringer -type ErrorCode -trimprefix ErrorCode -tags static"; DO NOT EDIT.
package git
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[ErrorCodeOK-0]
_ = x[ErrorCodeGeneric - -1]
_ = x[ErrorCodeNotFound - -3]
_ = x[ErrorCodeExists - -4]
_ = x[ErrorCodeAmbiguous - -5]
_ = x[ErrorCodeBuffs - -6]
_ = x[ErrorCodeUser - -7]
_ = x[ErrorCodeBareRepo - -8]
_ = x[ErrorCodeUnbornBranch - -9]
_ = x[ErrorCodeUnmerged - -10]
_ = x[ErrorCodeNonFastForward - -11]
_ = x[ErrorCodeInvalidSpec - -12]
_ = x[ErrorCodeConflict - -13]
_ = x[ErrorCodeLocked - -14]
_ = x[ErrorCodeModified - -15]
_ = x[ErrorCodeAuth - -16]
_ = x[ErrorCodeCertificate - -17]
_ = x[ErrorCodeApplied - -18]
_ = x[ErrorCodePeel - -19]
_ = x[ErrorCodeEOF - -20]
_ = x[ErrorCodeInvalid - -21]
_ = x[ErrorCodeUncommitted - -22]
_ = x[ErrorCodeDirectory - -23]
_ = x[ErrorCodeMergeConflict - -24]
_ = x[ErrorCodePassthrough - -30]
_ = x[ErrorCodeIterOver - -31]
_ = x[ErrorCodeRetry - -32]
_ = x[ErrorCodeMismatch - -33]
_ = x[ErrorCodeIndexDirty - -34]
_ = x[ErrorCodeApplyFail - -35]
}
const (
_ErrorCode_name_0 = "ApplyFailIndexDirtyMismatchRetryIterOverPassthrough"
_ErrorCode_name_1 = "MergeConflictDirectoryUncommittedInvalidEOFPeelAppliedCertificateAuthModifiedLockedConflictInvalidSpecNonFastForwardUnmergedUnbornBranchBareRepoUserBuffsAmbiguousExistsNotFound"
_ErrorCode_name_2 = "GenericOK"
)
var (
_ErrorCode_index_0 = [...]uint8{0, 9, 19, 27, 32, 40, 51}
_ErrorCode_index_1 = [...]uint8{0, 13, 22, 33, 40, 43, 47, 54, 65, 69, 77, 83, 91, 102, 116, 124, 136, 144, 148, 153, 162, 168, 176}
_ErrorCode_index_2 = [...]uint8{0, 7, 9}
)
func (i ErrorCode) String() string {
switch {
case -35 <= i && i <= -30:
i -= -35
return _ErrorCode_name_0[_ErrorCode_index_0[i]:_ErrorCode_index_0[i+1]]
case -24 <= i && i <= -3:
i -= -24
return _ErrorCode_name_1[_ErrorCode_index_1[i]:_ErrorCode_index_1[i+1]]
case -1 <= i && i <= 0:
i -= -1
return _ErrorCode_name_2[_ErrorCode_index_2[i]:_ErrorCode_index_2[i+1]]
default:
return "ErrorCode(" + strconv.FormatInt(int64(i), 10) + ")"
}
}

30
features.go Normal file
View File

@ -0,0 +1,30 @@
package git
/*
#include <git2.h>
*/
import "C"
type Feature int
const (
// libgit2 was built with threading support
FeatureThreads Feature = C.GIT_FEATURE_THREADS
// libgit2 was built with HTTPS support built-in
FeatureHTTPS Feature = C.GIT_FEATURE_HTTPS
// libgit2 was build with SSH support built-in
FeatureSSH Feature = C.GIT_FEATURE_SSH
// libgit2 was built with nanosecond support for files
FeatureNSec Feature = C.GIT_FEATURE_NSEC
)
// Features returns a bit-flag of Feature values indicating which features the
// loaded libgit2 library has.
func Features() Feature {
features := C.git_libgit2_features()
return Feature(features)
}

326
git.go
View File

@ -1,104 +1,264 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
#include <git2/sys/openssl.h>
*/
import "C"
import (
"bytes"
"encoding/hex"
"errors"
"unsafe"
"runtime"
"strings"
"unsafe"
)
//go:generate stringer -type ErrorClass -trimprefix ErrorClass -tags static
type ErrorClass int
const (
ITEROVER = C.GIT_ITEROVER
EEXISTS = C.GIT_EEXISTS
ENOTFOUND = C.GIT_ENOTFOUND
ErrorClassNone ErrorClass = C.GIT_ERROR_NONE
ErrorClassNoMemory ErrorClass = C.GIT_ERROR_NOMEMORY
ErrorClassOS ErrorClass = C.GIT_ERROR_OS
ErrorClassInvalid ErrorClass = C.GIT_ERROR_INVALID
ErrorClassReference ErrorClass = C.GIT_ERROR_REFERENCE
ErrorClassZlib ErrorClass = C.GIT_ERROR_ZLIB
ErrorClassRepository ErrorClass = C.GIT_ERROR_REPOSITORY
ErrorClassConfig ErrorClass = C.GIT_ERROR_CONFIG
ErrorClassRegex ErrorClass = C.GIT_ERROR_REGEX
ErrorClassOdb ErrorClass = C.GIT_ERROR_ODB
ErrorClassIndex ErrorClass = C.GIT_ERROR_INDEX
ErrorClassObject ErrorClass = C.GIT_ERROR_OBJECT
ErrorClassNet ErrorClass = C.GIT_ERROR_NET
ErrorClassTag ErrorClass = C.GIT_ERROR_TAG
ErrorClassTree ErrorClass = C.GIT_ERROR_TREE
ErrorClassIndexer ErrorClass = C.GIT_ERROR_INDEXER
ErrorClassSSL ErrorClass = C.GIT_ERROR_SSL
ErrorClassSubmodule ErrorClass = C.GIT_ERROR_SUBMODULE
ErrorClassThread ErrorClass = C.GIT_ERROR_THREAD
ErrorClassStash ErrorClass = C.GIT_ERROR_STASH
ErrorClassCheckout ErrorClass = C.GIT_ERROR_CHECKOUT
ErrorClassFetchHead ErrorClass = C.GIT_ERROR_FETCHHEAD
ErrorClassMerge ErrorClass = C.GIT_ERROR_MERGE
ErrorClassSSH ErrorClass = C.GIT_ERROR_SSH
ErrorClassFilter ErrorClass = C.GIT_ERROR_FILTER
ErrorClassRevert ErrorClass = C.GIT_ERROR_REVERT
ErrorClassCallback ErrorClass = C.GIT_ERROR_CALLBACK
ErrorClassRebase ErrorClass = C.GIT_ERROR_REBASE
ErrorClassPatch ErrorClass = C.GIT_ERROR_PATCH
)
//go:generate stringer -type ErrorCode -trimprefix ErrorCode -tags static
type ErrorCode int
const (
// ErrorCodeOK indicates that the operation completed successfully.
ErrorCodeOK ErrorCode = C.GIT_OK
// ErrorCodeGeneric represents a generic error.
ErrorCodeGeneric ErrorCode = C.GIT_ERROR
// ErrorCodeNotFound represents that the requested object could not be found
ErrorCodeNotFound ErrorCode = C.GIT_ENOTFOUND
// ErrorCodeExists represents that the object exists preventing operation.
ErrorCodeExists ErrorCode = C.GIT_EEXISTS
// ErrorCodeAmbiguous represents that more than one object matches.
ErrorCodeAmbiguous ErrorCode = C.GIT_EAMBIGUOUS
// ErrorCodeBuffs represents that the output buffer is too short to hold data.
ErrorCodeBuffs ErrorCode = C.GIT_EBUFS
// ErrorCodeUser is a special error that is never generated by libgit2
// code. You can return it from a callback (e.g to stop an iteration)
// to know that it was generated by the callback and not by libgit2.
ErrorCodeUser ErrorCode = C.GIT_EUSER
// ErrorCodeBareRepo represents that the operation not allowed on bare repository
ErrorCodeBareRepo ErrorCode = C.GIT_EBAREREPO
// ErrorCodeUnbornBranch represents that HEAD refers to branch with no commits.
ErrorCodeUnbornBranch ErrorCode = C.GIT_EUNBORNBRANCH
// ErrorCodeUnmerged represents that a merge in progress prevented operation.
ErrorCodeUnmerged ErrorCode = C.GIT_EUNMERGED
// ErrorCodeNonFastForward represents that the reference was not fast-forwardable.
ErrorCodeNonFastForward ErrorCode = C.GIT_ENONFASTFORWARD
// ErrorCodeInvalidSpec represents that the name/ref spec was not in a valid format.
ErrorCodeInvalidSpec ErrorCode = C.GIT_EINVALIDSPEC
// ErrorCodeConflict represents that checkout conflicts prevented operation.
ErrorCodeConflict ErrorCode = C.GIT_ECONFLICT
// ErrorCodeLocked represents that lock file prevented operation.
ErrorCodeLocked ErrorCode = C.GIT_ELOCKED
// ErrorCodeModified represents that the reference value does not match expected.
ErrorCodeModified ErrorCode = C.GIT_EMODIFIED
// ErrorCodeAuth represents that the authentication failed.
ErrorCodeAuth ErrorCode = C.GIT_EAUTH
// ErrorCodeCertificate represents that the server certificate is invalid.
ErrorCodeCertificate ErrorCode = C.GIT_ECERTIFICATE
// ErrorCodeApplied represents that the patch/merge has already been applied.
ErrorCodeApplied ErrorCode = C.GIT_EAPPLIED
// ErrorCodePeel represents that the requested peel operation is not possible.
ErrorCodePeel ErrorCode = C.GIT_EPEEL
// ErrorCodeEOF represents an unexpected EOF.
ErrorCodeEOF ErrorCode = C.GIT_EEOF
// ErrorCodeInvalid represents an invalid operation or input.
ErrorCodeInvalid ErrorCode = C.GIT_EINVALID
// ErrorCodeUIncommitted represents that uncommitted changes in index prevented operation.
ErrorCodeUncommitted ErrorCode = C.GIT_EUNCOMMITTED
// ErrorCodeDirectory represents that the operation is not valid for a directory.
ErrorCodeDirectory ErrorCode = C.GIT_EDIRECTORY
// ErrorCodeMergeConflict represents that a merge conflict exists and cannot continue.
ErrorCodeMergeConflict ErrorCode = C.GIT_EMERGECONFLICT
// ErrorCodePassthrough represents that a user-configured callback refused to act.
ErrorCodePassthrough ErrorCode = C.GIT_PASSTHROUGH
// ErrorCodeIterOver signals end of iteration with iterator.
ErrorCodeIterOver ErrorCode = C.GIT_ITEROVER
// ErrorCodeRetry is an internal-only error code.
ErrorCodeRetry ErrorCode = C.GIT_RETRY
// ErrorCodeMismatch represents a hashsum mismatch in object.
ErrorCodeMismatch ErrorCode = C.GIT_EMISMATCH
// ErrorCodeIndexDirty represents that unsaved changes in the index would be overwritten.
ErrorCodeIndexDirty ErrorCode = C.GIT_EINDEXDIRTY
// ErrorCodeApplyFail represents that a patch application failed.
ErrorCodeApplyFail ErrorCode = C.GIT_EAPPLYFAIL
)
var (
ErrIterOver = errors.New("Iteration is over")
ErrInvalid = errors.New("Invalid state for operation")
)
// doNotCompare is an idiomatic way of making structs non-comparable to avoid
// future field additions to make them non-comparable.
type doNotCompare [0]func()
var pointerHandles *HandleList
var remotePointers *remotePointerList
func init() {
C.git_threads_init()
initLibGit2()
}
// Oid
type Oid struct {
bytes [20]byte
func initLibGit2() {
pointerHandles = NewHandleList()
remotePointers = newRemotePointerList()
C.git_libgit2_init()
features := Features()
// Due to the multithreaded nature of Go and its interaction with
// calling C functions, we cannot work with a library that was not built
// with multi-threading support. The most likely outcome is a segfault
// or panic at an incomprehensible time, so let's make it easy by
// panicking right here.
if features&FeatureThreads == 0 {
panic("libgit2 was not built with threading support")
}
if features&FeatureHTTPS == 0 {
if err := registerManagedHTTP(); err != nil {
panic(err)
}
} else {
// This is not something we should be doing, as we may be stomping all over
// someone else's setup. The user should do this themselves or use some
// binding/wrapper which does it in such a way that they can be sure
// they're the only ones setting it up.
C.git_openssl_set_locking()
}
if features&FeatureSSH == 0 {
if err := registerManagedSSH(); err != nil {
panic(err)
}
}
}
// Shutdown frees all the resources acquired by libgit2. Make sure no
// references to any libgit2 go objects are live before calling this.
// After this is called, invoking any function from this library will result in
// undefined behavior, so make sure this is called carefully.
func Shutdown() {
if err := unregisterManagedTransports(); err != nil {
panic(err)
}
pointerHandles.Clear()
remotePointers.clear()
C.git_libgit2_shutdown()
}
// ReInit reinitializes the global state, this is useful if the effective user
// id has changed and you want to update the stored search paths for gitconfig
// files. This function frees any references to objects, so it should be called
// before any other functions are called.
func ReInit() {
Shutdown()
initLibGit2()
}
// Oid represents the id for a Git object.
type Oid [20]byte
func newOidFromC(coid *C.git_oid) *Oid {
if coid == nil {
return nil
}
oid := new(Oid)
copy(oid.bytes[0:20], C.GoBytes(unsafe.Pointer(coid), 20))
copy(oid[0:20], C.GoBytes(unsafe.Pointer(coid), 20))
return oid
}
func NewOid(b []byte) *Oid {
func NewOidFromBytes(b []byte) *Oid {
oid := new(Oid)
copy(oid.bytes[0:20], b[0:20])
copy(oid[0:20], b[0:20])
return oid
}
func (oid *Oid) toC() *C.git_oid {
return (*C.git_oid)(unsafe.Pointer(&oid.bytes))
return (*C.git_oid)(unsafe.Pointer(oid))
}
func NewOidFromString(s string) (*Oid, error) {
o := new(Oid)
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
if C.git_oid_fromstr(o.toC(), cs) < 0 {
return nil, LastError()
func NewOid(s string) (*Oid, error) {
if len(s) > C.GIT_OID_HEXSZ {
return nil, errors.New("string is too long for oid")
}
o := new(Oid)
slice, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
if len(slice) != 20 {
return nil, &GitError{"invalid oid", ErrorClassNone, ErrorCodeGeneric}
}
copy(o[:], slice[:20])
return o, nil
}
func (oid *Oid) String() string {
buf := make([]byte, 40)
C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), oid.toC())
return string(buf)
}
func (oid *Oid) Bytes() []byte {
return oid.bytes[0:]
return hex.EncodeToString(oid[:])
}
func (oid *Oid) Cmp(oid2 *Oid) int {
return bytes.Compare(oid.bytes[:], oid2.bytes[:])
return bytes.Compare(oid[:], oid2[:])
}
func (oid *Oid) Copy() *Oid {
ret := new(Oid)
copy(ret.bytes[:], oid.bytes[:])
return ret
ret := *oid
return &ret
}
func (oid *Oid) Equal(oid2 *Oid) bool {
return bytes.Equal(oid.bytes[:], oid2.bytes[:])
return *oid == *oid2
}
func (oid *Oid) IsZero() bool {
for _, a := range(oid.bytes) {
if a != '0' {
return false
}
}
return true
return *oid == Oid{}
}
func (oid *Oid) NCmp(oid2 *Oid, n uint) int {
return bytes.Compare(oid.bytes[:n], oid2.bytes[:n])
return bytes.Compare(oid[:n], oid2[:n])
}
func ShortenOids(ids []*Oid, minlen int) (int, error) {
@ -109,49 +269,101 @@ func ShortenOids(ids []*Oid, minlen int) (int, error) {
defer C.git_oid_shorten_free(shorten)
var ret C.int
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for _, id := range ids {
buf := make([]byte, 41)
C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), id.toC())
buf[40] = 0
ret = C.git_oid_shorten_add(shorten, (*C.char)(unsafe.Pointer(&buf[0])))
if ret < 0 {
return int(ret), LastError()
return int(ret), MakeGitError(ret)
}
}
runtime.KeepAlive(ids)
return int(ret), nil
}
type GitError struct {
Message string
Code int
Class ErrorClass
Code ErrorCode
}
func (e GitError) Error() string{
func (e GitError) Error() string {
return e.Message
}
func LastError() error {
err := C.giterr_last()
func IsErrorClass(err error, c ErrorClass) bool {
if err == nil {
return &GitError{"No message", 0}
return false
}
return &GitError{C.GoString(err.message), int(err.klass)}
if gitError, ok := err.(*GitError); ok {
return gitError.Class == c
}
return false
}
func IsErrorCode(err error, c ErrorCode) bool {
if err == nil {
return false
}
if gitError, ok := err.(*GitError); ok {
return gitError.Code == c
}
return false
}
func MakeGitError(c C.int) error {
var errMessage string
var errClass ErrorClass
errorCode := ErrorCode(c)
if errorCode != ErrorCodeIterOver {
err := C.git_error_last()
if err != nil {
errMessage = C.GoString(err.message)
errClass = ErrorClass(err.klass)
} else {
errClass = ErrorClassInvalid
}
}
if errMessage == "" {
errMessage = errorCode.String()
}
return &GitError{errMessage, errClass, errorCode}
}
func MakeGitError2(err int) error {
return MakeGitError(C.int(err))
}
func cbool(b bool) C.int {
if (b) {
if b {
return C.int(1)
}
return C.int(0)
}
func ucbool(b bool) C.uint {
if (b) {
if b {
return C.uint(1)
}
return C.uint(0)
}
func setCallbackError(errorMessage **C.char, err error) C.int {
if err != nil {
*errorMessage = C.CString(err.Error())
if gitError, ok := err.(*GitError); ok {
return C.int(gitError.Code)
}
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
func Discover(start string, across_fs bool, ceiling_dirs []string) (string, error) {
ceildirs := C.CString(strings.Join(ceiling_dirs, string(C.GIT_PATH_LIST_SEPARATOR)))
defer C.free(unsafe.Pointer(ceildirs))
@ -159,14 +371,16 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro
cstart := C.CString(start)
defer C.free(unsafe.Pointer(cstart))
retpath := (*C.char)(C.malloc(C.GIT_PATH_MAX))
defer C.free(unsafe.Pointer(retpath))
var buf C.git_buf
defer C.git_buf_dispose(&buf)
r := C.git_repository_discover(retpath, C.GIT_PATH_MAX, cstart, cbool(across_fs), ceildirs)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if r == 0 {
return C.GoString(retpath), nil
ret := C.git_repository_discover(&buf, cstart, cbool(across_fs), ceildirs)
if ret < 0 {
return "", MakeGitError(ret)
}
return "", LastError()
return C.GoString(buf.ptr), nil
}

View File

@ -1,11 +1,63 @@
package git
import (
"testing"
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"testing"
"time"
)
func TestMain(m *testing.M) {
if err := registerManagedHTTP(); err != nil {
panic(err)
}
ret := m.Run()
if err := unregisterManagedTransports(); err != nil {
panic(err)
}
// Ensure that we are not leaking any pointer handles.
pointerHandles.Lock()
if len(pointerHandles.handles) > 0 {
for h, ptr := range pointerHandles.handles {
fmt.Printf("%016p: %v %+v\n", h, reflect.TypeOf(ptr), ptr)
}
panic("pointer handle list not empty")
}
pointerHandles.Unlock()
// Or remote pointers.
remotePointers.Lock()
if len(remotePointers.pointers) > 0 {
for ptr, remote := range remotePointers.pointers {
fmt.Printf("%016p: %+v\n", ptr, remote)
}
panic("remote pointer list not empty")
}
remotePointers.Unlock()
Shutdown()
os.Exit(ret)
}
func cleanupTestRepo(t *testing.T, r *Repository) {
var err error
if r.IsBare() {
err = os.RemoveAll(r.Path())
} else {
err = os.RemoveAll(r.Workdir())
}
checkFatal(t, err)
r.Free()
}
func createTestRepo(t *testing.T) *Repository {
// figure out where we can create the test repo
path, err := ioutil.TempDir("", "git2go")
@ -14,13 +66,33 @@ func createTestRepo(t *testing.T) *Repository {
checkFatal(t, err)
tmpfile := "README"
err = ioutil.WriteFile(path + "/" + tmpfile, []byte("foo\n"), 0644)
err = ioutil.WriteFile(path+"/"+tmpfile, []byte("foo\n"), 0644)
checkFatal(t, err)
return repo
}
func createBareTestRepo(t *testing.T) *Repository {
// figure out where we can create the test repo
path, err := ioutil.TempDir("", "git2go")
checkFatal(t, err)
repo, err := InitRepository(path, true)
checkFatal(t, err)
return repo
}
// commitOptions contains any extra options for creating commits in the seed repo
type commitOptions struct {
CommitSigningCallback
}
func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) {
return seedTestRepoOpt(t, repo, commitOptions{})
}
func seedTestRepoOpt(t *testing.T, repo *Repository, opts commitOptions) (*Oid, *Oid) {
loc, err := time.LoadLocation("Europe/Berlin")
checkFatal(t, err)
sig := &Signature{
@ -33,6 +105,8 @@ func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) {
checkFatal(t, err)
err = idx.AddByPath("README")
checkFatal(t, err)
err = idx.Write()
checkFatal(t, err)
treeId, err := idx.WriteTree()
checkFatal(t, err)
@ -42,6 +116,84 @@ func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) {
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree)
checkFatal(t, err)
if opts.CommitSigningCallback != nil {
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
signature, signatureField, err := opts.CommitSigningCallback(commit.ContentToSign())
checkFatal(t, err)
oid, err := commit.WithSignature(signature, signatureField)
checkFatal(t, err)
newCommit, err := repo.LookupCommit(oid)
checkFatal(t, err)
head, err := repo.Head()
checkFatal(t, err)
_, err = repo.References.Create(
head.Name(),
newCommit.Id(),
true,
"repoint to signed commit",
)
checkFatal(t, err)
}
return commitId, treeId
}
func pathInRepo(repo *Repository, name string) string {
return path.Join(path.Dir(path.Dir(repo.Path())), name)
}
func updateReadme(t *testing.T, repo *Repository, content string) (*Oid, *Oid) {
loc, err := time.LoadLocation("Europe/Berlin")
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc),
}
tmpfile := "README"
err = ioutil.WriteFile(pathInRepo(repo, tmpfile), []byte(content), 0644)
checkFatal(t, err)
idx, err := repo.Index()
checkFatal(t, err)
err = idx.AddByPath("README")
checkFatal(t, err)
err = idx.Write()
checkFatal(t, err)
treeId, err := idx.WriteTree()
checkFatal(t, err)
currentBranch, err := repo.Head()
checkFatal(t, err)
currentTip, err := repo.LookupCommit(currentBranch.Target())
checkFatal(t, err)
message := "This is a commit\n"
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree, currentTip)
checkFatal(t, err)
return commitId, treeId
}
func TestOidZero(t *testing.T) {
t.Parallel()
var zeroId Oid
if !zeroId.IsZero() {
t.Error("Zero Oid is not zero")
}
}
func TestEmptyOid(t *testing.T) {
t.Parallel()
_, err := NewOid("")
if err == nil || !IsErrorCode(err, ErrorCodeGeneric) {
t.Fatal("Should have returned invalid error")
}
}

69
graph.go Normal file
View File

@ -0,0 +1,69 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
)
func (repo *Repository) DescendantOf(commit, ancestor *Oid) (bool, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_graph_descendant_of(repo.ptr, commit.toC(), ancestor.toC())
runtime.KeepAlive(repo)
runtime.KeepAlive(commit)
runtime.KeepAlive(ancestor)
if ret < 0 {
return false, MakeGitError(ret)
}
return (ret > 0), nil
}
func (repo *Repository) AheadBehind(local, upstream *Oid) (ahead, behind int, err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var aheadT C.size_t
var behindT C.size_t
ret := C.git_graph_ahead_behind(&aheadT, &behindT, repo.ptr, local.toC(), upstream.toC())
runtime.KeepAlive(repo)
runtime.KeepAlive(local)
runtime.KeepAlive(upstream)
if ret < 0 {
return 0, 0, MakeGitError(ret)
}
return int(aheadT), int(behindT), nil
}
// ReachableFromAny returns whether a commit is reachable from any of a list of
// commits by following parent edges.
func (repo *Repository) ReachableFromAny(commit *Oid, descendants []*Oid) (bool, error) {
if len(descendants) == 0 {
return false, nil
}
coids := make([]C.git_oid, len(descendants))
for i := 0; i < len(descendants); i++ {
coids[i] = *descendants[i].toC()
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_graph_reachable_from_any(repo.ptr, commit.toC(), &coids[0], C.size_t(len(descendants)))
runtime.KeepAlive(repo)
runtime.KeepAlive(commit)
runtime.KeepAlive(coids)
runtime.KeepAlive(descendants)
if ret < 0 {
return false, MakeGitError(ret)
}
return (ret > 0), nil
}

75
graph_test.go Normal file
View File

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

68
handles.go Normal file
View File

@ -0,0 +1,68 @@
package git
/*
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"sync"
"unsafe"
)
type HandleList struct {
doNotCompare
sync.RWMutex
// stores the Go pointers
handles map[unsafe.Pointer]interface{}
}
func NewHandleList() *HandleList {
return &HandleList{
handles: make(map[unsafe.Pointer]interface{}),
}
}
// Track adds the given pointer to the list of pointers to track and
// returns a pointer value which can be passed to C as an opaque
// pointer.
func (v *HandleList) Track(pointer interface{}) unsafe.Pointer {
handle := C.malloc(1)
v.Lock()
v.handles[handle] = pointer
v.Unlock()
return handle
}
// Untrack stops tracking the pointer given by the handle
func (v *HandleList) Untrack(handle unsafe.Pointer) {
v.Lock()
delete(v.handles, handle)
C.free(handle)
v.Unlock()
}
// Clear stops tracking all the managed pointers.
func (v *HandleList) Clear() {
v.Lock()
for handle := range v.handles {
delete(v.handles, handle)
C.free(handle)
}
v.Unlock()
}
// Get retrieves the pointer from the given handle
func (v *HandleList) Get(handle unsafe.Pointer) interface{} {
v.RLock()
defer v.RUnlock()
ptr, ok := v.handles[handle]
if !ok {
panic(fmt.Sprintf("invalid pointer handle: %p", handle))
}
return ptr
}

243
http.go Normal file
View File

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

54
ignore.go Normal file
View File

@ -0,0 +1,54 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
func (v *Repository) AddIgnoreRule(rules string) error {
crules := C.CString(rules)
defer C.free(unsafe.Pointer(crules))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_ignore_add_rule(v.ptr, crules)
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (v *Repository) ClearInternalIgnoreRules() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_ignore_clear_internal_rules(v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (v *Repository) IsPathIgnored(path string) (bool, error) {
var ignored C.int
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_ignore_path_is_ignored(&ignored, v.ptr, cpath)
runtime.KeepAlive(v)
if ret < 0 {
return false, MakeGitError(ret)
}
return ignored == 1, nil
}

638
index.go
View File

@ -1,33 +1,416 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
extern int _go_git_index_add_all(git_index*, const git_strarray*, unsigned int, void*);
extern int _go_git_index_update_all(git_index*, const git_strarray*, void*);
extern int _go_git_index_remove_all(git_index*, const git_strarray*, void*);
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
type Index struct {
ptr *C.git_index
type IndexMatchedPathCallback func(string, string) error
type indexMatchedPathCallbackData struct {
callback IndexMatchedPathCallback
errorTarget *error
}
func newIndexFromC(ptr *C.git_index) *Index {
idx := &Index{ptr}
// IndexAddOption is a set of flags for APIs that add files matching pathspec.
type IndexAddOption uint
const (
IndexAddDefault IndexAddOption = C.GIT_INDEX_ADD_DEFAULT
IndexAddForce IndexAddOption = C.GIT_INDEX_ADD_FORCE
IndexAddDisablePathspecMatch IndexAddOption = C.GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH
IndexAddCheckPathspec IndexAddOption = C.GIT_INDEX_ADD_CHECK_PATHSPEC
)
// IndexStageState indicates the state of the git index.
type IndexStageState int
const (
// IndexStageAny matches any index stage.
//
// Some index APIs take a stage to match; pass this value to match
// any entry matching the path regardless of stage.
IndexStageAny IndexStageState = C.GIT_INDEX_STAGE_ANY
// IndexStageNormal is a normal staged file in the index.
IndexStageNormal IndexStageState = C.GIT_INDEX_STAGE_NORMAL
// IndexStageAncestor is the ancestor side of a conflict.
IndexStageAncestor IndexStageState = C.GIT_INDEX_STAGE_ANCESTOR
// IndexStageOurs is the "ours" side of a conflict.
IndexStageOurs IndexStageState = C.GIT_INDEX_STAGE_OURS
// IndexStageTheirs is the "theirs" side of a conflict.
IndexStageTheirs IndexStageState = C.GIT_INDEX_STAGE_THEIRS
)
type Index struct {
doNotCompare
ptr *C.git_index
repo *Repository
}
type IndexTime struct {
Seconds int32
Nanoseconds uint32
}
type IndexEntry struct {
Ctime IndexTime
Mtime IndexTime
Mode Filemode
Uid uint32
Gid uint32
Size uint32
Id *Oid
Path string
}
func newIndexEntryFromC(entry *C.git_index_entry) *IndexEntry {
if entry == nil {
return nil
}
return &IndexEntry{
IndexTime{int32(entry.ctime.seconds), uint32(entry.ctime.nanoseconds)},
IndexTime{int32(entry.mtime.seconds), uint32(entry.mtime.nanoseconds)},
Filemode(entry.mode),
uint32(entry.uid),
uint32(entry.gid),
uint32(entry.file_size),
newOidFromC(&entry.id),
C.GoString(entry.path),
}
}
func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) {
dest.ctime.seconds = C.int32_t(source.Ctime.Seconds)
dest.ctime.nanoseconds = C.uint32_t(source.Ctime.Nanoseconds)
dest.mtime.seconds = C.int32_t(source.Mtime.Seconds)
dest.mtime.nanoseconds = C.uint32_t(source.Mtime.Nanoseconds)
dest.mode = C.uint32_t(source.Mode)
dest.uid = C.uint32_t(source.Uid)
dest.gid = C.uint32_t(source.Gid)
dest.file_size = C.uint32_t(source.Size)
if source.Id != nil {
dest.id = *source.Id.toC()
}
dest.path = C.CString(source.Path)
}
func freeCIndexEntry(entry *C.git_index_entry) {
C.free(unsafe.Pointer(entry.path))
}
func newIndexFromC(ptr *C.git_index, repo *Repository) *Index {
idx := &Index{ptr: ptr, repo: repo}
runtime.SetFinalizer(idx, (*Index).Free)
return idx
}
// NewIndex allocates a new index. It won't be associated with any
// file on the filesystem or repository
func NewIndex() (*Index, error) {
var ptr *C.git_index
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := C.git_index_new(&ptr); err < 0 {
return nil, MakeGitError(err)
}
return newIndexFromC(ptr, nil), nil
}
// OpenIndex creates a new index at the given path. If the file does
// not exist it will be created when Write() is called.
func OpenIndex(path string) (*Index, error) {
var ptr *C.git_index
var cpath = C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := C.git_index_open(&ptr, cpath); err < 0 {
return nil, MakeGitError(err)
}
return newIndexFromC(ptr, nil), nil
}
// Path returns the index' path on disk or an empty string if it
// exists only in memory.
func (v *Index) Path() string {
ret := C.GoString(C.git_index_path(v.ptr))
runtime.KeepAlive(v)
return ret
}
// Clear clears the index object in memory; changes must be explicitly
// written to disk for them to take effect persistently
func (v *Index) Clear() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_index_clear(v.ptr)
runtime.KeepAlive(v)
if err < 0 {
return MakeGitError(err)
}
return nil
}
// Add adds or replaces the given entry to the index, making a copy of
// the data
func (v *Index) Add(entry *IndexEntry) error {
var centry C.git_index_entry
populateCIndexEntry(entry, &centry)
defer freeCIndexEntry(&centry)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_index_add(v.ptr, &centry)
runtime.KeepAlive(v)
if err < 0 {
return MakeGitError(err)
}
return nil
}
func (v *Index) AddByPath(path string) error {
cstr := C.CString(path)
defer C.free(unsafe.Pointer(cstr))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_index_add_bypath(v.ptr, cstr)
runtime.KeepAlive(v)
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
// AddFromBuffer adds or replaces an index entry from a buffer in memory
func (v *Index) AddFromBuffer(entry *IndexEntry, buffer []byte) error {
var centry C.git_index_entry
populateCIndexEntry(entry, &centry)
defer freeCIndexEntry(&centry)
var cbuffer unsafe.Pointer
if len(buffer) > 0 {
cbuffer = unsafe.Pointer(&buffer[0])
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := C.git_index_add_from_buffer(v.ptr, &centry, cbuffer, C.size_t(len(buffer))); err < 0 {
return MakeGitError(err)
}
return nil
}
func (v *Index) AddAll(pathspecs []string, flags IndexAddOption, callback IndexMatchedPathCallback) error {
cpathspecs := C.git_strarray{}
cpathspecs.count = C.size_t(len(pathspecs))
cpathspecs.strings = makeCStringsFromStrings(pathspecs)
defer freeStrarray(&cpathspecs)
var err error
data := indexMatchedPathCallbackData{
callback: callback,
errorTarget: &err,
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var handle unsafe.Pointer
if callback != nil {
handle = pointerHandles.Track(&data)
defer pointerHandles.Untrack(handle)
}
ret := C._go_git_index_add_all(
v.ptr,
&cpathspecs,
C.uint(flags),
handle,
)
runtime.KeepAlive(v)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (v *Index) UpdateAll(pathspecs []string, callback IndexMatchedPathCallback) error {
cpathspecs := C.git_strarray{}
cpathspecs.count = C.size_t(len(pathspecs))
cpathspecs.strings = makeCStringsFromStrings(pathspecs)
defer freeStrarray(&cpathspecs)
var err error
data := indexMatchedPathCallbackData{
callback: callback,
errorTarget: &err,
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var handle unsafe.Pointer
if callback != nil {
handle = pointerHandles.Track(&data)
defer pointerHandles.Untrack(handle)
}
ret := C._go_git_index_update_all(
v.ptr,
&cpathspecs,
handle,
)
runtime.KeepAlive(v)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (v *Index) RemoveAll(pathspecs []string, callback IndexMatchedPathCallback) error {
cpathspecs := C.git_strarray{}
cpathspecs.count = C.size_t(len(pathspecs))
cpathspecs.strings = makeCStringsFromStrings(pathspecs)
defer freeStrarray(&cpathspecs)
var err error
data := indexMatchedPathCallbackData{
callback: callback,
errorTarget: &err,
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var handle unsafe.Pointer
if callback != nil {
handle = pointerHandles.Track(&data)
defer pointerHandles.Untrack(handle)
}
ret := C._go_git_index_remove_all(
v.ptr,
&cpathspecs,
handle,
)
runtime.KeepAlive(v)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
//export indexMatchedPathCallback
func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Pointer) C.int {
data, ok := pointerHandles.Get(payload).(*indexMatchedPathCallbackData)
if !ok {
panic("invalid matched path callback")
}
err := data.callback(C.GoString(cPath), C.GoString(cMatchedPathspec))
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
func (v *Index) RemoveByPath(path string) error {
cstr := C.CString(path)
defer C.free(unsafe.Pointer(cstr))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_index_remove_bypath(v.ptr, cstr)
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// RemoveDirectory removes all entries from the index under a given directory.
func (v *Index) RemoveDirectory(dir string, stage int) error {
cstr := C.CString(dir)
defer C.free(unsafe.Pointer(cstr))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_index_remove_directory(v.ptr, cstr, C.int(stage))
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (v *Index) WriteTreeTo(repo *Repository) (*Oid, error) {
oid := new(Oid)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_index_write_tree_to(oid.toC(), v.ptr, repo.ptr)
runtime.KeepAlive(v)
runtime.KeepAlive(repo)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}
// ReadTree replaces the contents of the index with those of the given
// tree
func (v *Index) ReadTree(tree *Tree) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_index_read_tree(v.ptr, tree.cast_ptr)
runtime.KeepAlive(v)
runtime.KeepAlive(tree)
if ret < 0 {
return MakeGitError(ret)
}
return nil
@ -35,15 +418,254 @@ func (v *Index) AddByPath(path string) error {
func (v *Index) WriteTree() (*Oid, error) {
oid := new(Oid)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_index_write_tree(oid.toC(), v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
return oid, nil
}
func (v *Index) Write() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_index_write(v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (v *Index) Free() {
runtime.SetFinalizer(v, nil)
C.git_index_free(v.ptr)
}
func (v *Index) EntryCount() uint {
ret := uint(C.git_index_entrycount(v.ptr))
runtime.KeepAlive(v)
return ret
}
func (v *Index) EntryByIndex(index uint) (*IndexEntry, error) {
centry := C.git_index_get_byindex(v.ptr, C.size_t(index))
if centry == nil {
return nil, fmt.Errorf("Index out of Bounds")
}
ret := newIndexEntryFromC(centry)
runtime.KeepAlive(v)
return ret, nil
}
func (v *Index) EntryByPath(path string, stage int) (*IndexEntry, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
centry := C.git_index_get_bypath(v.ptr, cpath, C.int(stage))
if centry == nil {
return nil, MakeGitError(C.int(ErrorCodeNotFound))
}
ret := newIndexEntryFromC(centry)
runtime.KeepAlive(v)
return ret, nil
}
func (v *Index) Find(path string) (uint, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var pos C.size_t
ret := C.git_index_find(&pos, v.ptr, cpath)
runtime.KeepAlive(v)
if ret < 0 {
return uint(0), MakeGitError(ret)
}
return uint(pos), nil
}
func (v *Index) FindPrefix(prefix string) (uint, error) {
cprefix := C.CString(prefix)
defer C.free(unsafe.Pointer(cprefix))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var pos C.size_t
ret := C.git_index_find_prefix(&pos, v.ptr, cprefix)
runtime.KeepAlive(v)
if ret < 0 {
return uint(0), MakeGitError(ret)
}
return uint(pos), nil
}
func (v *Index) HasConflicts() bool {
ret := C.git_index_has_conflicts(v.ptr) != 0
runtime.KeepAlive(v)
return ret
}
// FIXME: this might return an error
func (v *Index) CleanupConflicts() {
C.git_index_conflict_cleanup(v.ptr)
runtime.KeepAlive(v)
}
func (v *Index) AddConflict(ancestor *IndexEntry, our *IndexEntry, their *IndexEntry) error {
var cancestor *C.git_index_entry
var cour *C.git_index_entry
var ctheir *C.git_index_entry
if ancestor != nil {
cancestor = &C.git_index_entry{}
populateCIndexEntry(ancestor, cancestor)
defer freeCIndexEntry(cancestor)
}
if our != nil {
cour = &C.git_index_entry{}
populateCIndexEntry(our, cour)
defer freeCIndexEntry(cour)
}
if their != nil {
ctheir = &C.git_index_entry{}
populateCIndexEntry(their, ctheir)
defer freeCIndexEntry(ctheir)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_index_conflict_add(v.ptr, cancestor, cour, ctheir)
runtime.KeepAlive(v)
runtime.KeepAlive(ancestor)
runtime.KeepAlive(our)
runtime.KeepAlive(their)
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
type IndexConflict struct {
Ancestor *IndexEntry
Our *IndexEntry
Their *IndexEntry
}
func (v *Index) Conflict(path string) (IndexConflict, error) {
var cancestor *C.git_index_entry
var cour *C.git_index_entry
var ctheir *C.git_index_entry
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_index_conflict_get(&cancestor, &cour, &ctheir, v.ptr, cpath)
if ecode < 0 {
return IndexConflict{}, MakeGitError(ecode)
}
ret := IndexConflict{
Ancestor: newIndexEntryFromC(cancestor),
Our: newIndexEntryFromC(cour),
Their: newIndexEntryFromC(ctheir),
}
runtime.KeepAlive(v)
return ret, nil
}
// deprecated: You should use `Index.Conflict()` instead.
func (v *Index) GetConflict(path string) (IndexConflict, error) {
return v.Conflict(path)
}
func (v *Index) RemoveConflict(path string) error {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_index_conflict_remove(v.ptr, cpath)
runtime.KeepAlive(v)
if ecode < 0 {
return MakeGitError(ecode)
}
return nil
}
type IndexConflictIterator struct {
doNotCompare
ptr *C.git_index_conflict_iterator
index *Index
}
func newIndexConflictIteratorFromC(index *Index, ptr *C.git_index_conflict_iterator) *IndexConflictIterator {
i := &IndexConflictIterator{ptr: ptr, index: index}
runtime.SetFinalizer(i, (*IndexConflictIterator).Free)
return i
}
func (v *IndexConflictIterator) Index() *Index {
return v.index
}
func (v *IndexConflictIterator) Free() {
runtime.SetFinalizer(v, nil)
C.git_index_conflict_iterator_free(v.ptr)
}
func (v *Index) ConflictIterator() (*IndexConflictIterator, error) {
var i *C.git_index_conflict_iterator
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_index_conflict_iterator_new(&i, v.ptr)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newIndexConflictIteratorFromC(v, i), nil
}
func (v *IndexConflictIterator) Next() (IndexConflict, error) {
var cancestor *C.git_index_entry
var cour *C.git_index_entry
var ctheir *C.git_index_entry
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_index_conflict_next(&cancestor, &cour, &ctheir, v.ptr)
if ecode < 0 {
return IndexConflict{}, MakeGitError(ecode)
}
ret := IndexConflict{
Ancestor: newIndexEntryFromC(cancestor),
Our: newIndexEntryFromC(cour),
Their: newIndexEntryFromC(ctheir),
}
runtime.KeepAlive(v)
return ret, nil
}

View File

@ -1,14 +1,17 @@
package git
import (
"io/ioutil"
"os"
"path"
"runtime"
"testing"
)
func TestCreateRepoAndStage(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer os.RemoveAll(repo.Workdir())
defer cleanupTestRepo(t, repo)
idx, err := repo.Index()
checkFatal(t, err)
@ -22,6 +25,249 @@ func TestCreateRepoAndStage(t *testing.T) {
}
}
func TestIndexReadTree(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
ref, err := repo.Head()
checkFatal(t, err)
obj, err := ref.Peel(ObjectTree)
checkFatal(t, err)
tree, err := obj.AsTree()
checkFatal(t, err)
idx, err := NewIndex()
checkFatal(t, err)
err = idx.ReadTree(tree)
checkFatal(t, err)
id, err := idx.WriteTreeTo(repo)
checkFatal(t, err)
if tree.Id().Cmp(id) != 0 {
t.Fatalf("Read and written trees are not the same")
}
}
func TestIndexWriteTreeTo(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
idx, err := NewIndex()
checkFatal(t, err)
odb, err := repo.Odb()
checkFatal(t, err)
content, err := ioutil.ReadFile(path.Join(repo.Workdir(), "README"))
checkFatal(t, err)
id, err := odb.Write(content, ObjectBlob)
checkFatal(t, err)
err = idx.Add(&IndexEntry{
Mode: FilemodeBlob,
Uid: 0,
Gid: 0,
Size: uint32(len(content)),
Id: id,
Path: "README",
})
checkFatal(t, err)
treeId, err := idx.WriteTreeTo(repo)
checkFatal(t, err)
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
t.Fatalf("%v", treeId.String())
}
}
func TestIndexAddAndWriteTreeTo(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
blobID, err := odb.Write([]byte("foo\n"), ObjectBlob)
checkFatal(t, err)
idx, err := NewIndex()
checkFatal(t, err)
if idx.Path() != "" {
t.Fatal("in-memory repo has a path")
}
entry := IndexEntry{
Path: "README",
Id: blobID,
Mode: FilemodeBlob,
}
err = idx.Add(&entry)
checkFatal(t, err)
treeId, err := idx.WriteTreeTo(repo)
checkFatal(t, err)
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
t.Fatalf("%v", treeId.String())
}
}
func TestIndexRemoveDirectory(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
blobID, err := odb.Write([]byte("fou\n"), ObjectBlob)
checkFatal(t, err)
idx, err := NewIndex()
checkFatal(t, err)
entryCount := idx.EntryCount()
if entryCount != 0 {
t.Fatal("Index should count 0 entry")
}
entry := IndexEntry{
Path: "path/to/LISEZ_MOI",
Id: blobID,
Mode: FilemodeBlob,
}
err = idx.Add(&entry)
checkFatal(t, err)
entryCount = idx.EntryCount()
if entryCount != 1 {
t.Fatal("Index should count 1 entry")
}
err = idx.RemoveDirectory("path", 0)
entryCount = idx.EntryCount()
if entryCount != 0 {
t.Fatal("Index should count 0 entry")
}
}
func TestIndexAddFromBuffer(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
idx, err := repo.Index()
checkFatal(t, err)
entry := IndexEntry{
Path: "README",
Mode: FilemodeBlob,
}
err = idx.AddFromBuffer(&entry, []byte("foo\n"))
checkFatal(t, err)
treeId, err := idx.WriteTreeTo(repo)
checkFatal(t, err)
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
t.Fatalf("%v", treeId.String())
}
}
func TestIndexAddAllNoCallback(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
err := ioutil.WriteFile(repo.Workdir()+"/README", []byte("foo\n"), 0644)
checkFatal(t, err)
idx, err := repo.Index()
checkFatal(t, err)
err = idx.AddAll([]string{}, IndexAddDefault, nil)
checkFatal(t, err)
treeId, err := idx.WriteTreeTo(repo)
checkFatal(t, err)
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
t.Fatalf("%v", treeId.String())
}
}
func TestIndexAddAllCallback(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
err := ioutil.WriteFile(repo.Workdir()+"/README", []byte("foo\n"), 0644)
checkFatal(t, err)
idx, err := repo.Index()
checkFatal(t, err)
cbPath := ""
err = idx.AddAll([]string{}, IndexAddDefault, func(p, mP string) error {
cbPath = p
return nil
})
checkFatal(t, err)
if cbPath != "README" {
t.Fatalf("%v", cbPath)
}
treeId, err := idx.WriteTreeTo(repo)
checkFatal(t, err)
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
t.Fatalf("%v", treeId.String())
}
}
func TestIndexOpen(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
path := repo.Workdir() + "/heyindex"
_, err := os.Stat(path)
if !os.IsNotExist(err) {
t.Fatal("new index file already exists")
}
idx, err := OpenIndex(path)
checkFatal(t, err)
if path != idx.Path() {
t.Fatalf("mismatched index paths, expected %v, got %v", path, idx.Path())
}
err = idx.Write()
checkFatal(t, err)
_, err = os.Stat(path)
if os.IsNotExist(err) {
t.Fatal("new index file did not get written")
}
}
func checkFatal(t *testing.T, err error) {
if err == nil {
return
@ -30,8 +276,7 @@ func checkFatal(t *testing.T, err error) {
// The failure happens at wherever we were called, not here
_, file, line, ok := runtime.Caller(1)
if !ok {
t.Fatal()
t.Fatalf("Unable to get caller")
}
t.Fatalf("Fail at %v:%v; %v", file, line, err)
}

97
indexer.go Normal file
View File

@ -0,0 +1,97 @@
package git
/*
#include <git2.h>
extern const git_oid * git_indexer_hash(const git_indexer *idx);
extern int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_transfer_progress *stats);
extern int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats);
extern int _go_git_indexer_new(git_indexer **out, const char *path, unsigned int mode, git_odb *odb, void *progress_cb_payload);
extern void git_indexer_free(git_indexer *idx);
*/
import "C"
import (
"reflect"
"runtime"
"unsafe"
)
// Indexer can post-process packfiles and create an .idx file for efficient
// lookup.
type Indexer struct {
doNotCompare
ptr *C.git_indexer
stats C.git_transfer_progress
ccallbacks C.git_remote_callbacks
}
// NewIndexer creates a new indexer instance.
func NewIndexer(packfilePath string, odb *Odb, callback TransferProgressCallback) (indexer *Indexer, err error) {
var odbPtr *C.git_odb = nil
if odb != nil {
odbPtr = odb.ptr
}
indexer = new(Indexer)
populateRemoteCallbacks(&indexer.ccallbacks, &RemoteCallbacks{TransferProgressCallback: callback}, nil)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(packfilePath)
defer C.free(unsafe.Pointer(cstr))
ret := C._go_git_indexer_new(&indexer.ptr, cstr, 0, odbPtr, indexer.ccallbacks.payload)
runtime.KeepAlive(odb)
if ret < 0 {
untrackCallbacksPayload(&indexer.ccallbacks)
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(indexer, (*Indexer).Free)
return indexer, nil
}
// Write adds data to the indexer.
func (indexer *Indexer) Write(data []byte) (int, error) {
header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
ptr := unsafe.Pointer(header.Data)
size := C.size_t(header.Len)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_indexer_append(indexer.ptr, ptr, size, &indexer.stats)
runtime.KeepAlive(indexer)
if ret < 0 {
return 0, MakeGitError(ret)
}
return len(data), nil
}
// Commit finalizes the pack and index. It resolves any pending deltas and
// writes out the index file.
//
// It also returns the packfile's hash. A packfile's name is derived from the
// sorted hashing of all object names.
func (indexer *Indexer) Commit() (*Oid, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_indexer_commit(indexer.ptr, &indexer.stats)
if ret < 0 {
return nil, MakeGitError(ret)
}
id := newOidFromC(C.git_indexer_hash(indexer.ptr))
runtime.KeepAlive(indexer)
return id, nil
}
// Free frees the indexer and its resources.
func (indexer *Indexer) Free() {
untrackCallbacksPayload(&indexer.ccallbacks)
runtime.SetFinalizer(indexer, nil)
C.git_indexer_free(indexer.ptr)
}

93
indexer_test.go Normal file
View File

@ -0,0 +1,93 @@
package git
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
)
var (
// This is a packfile with three objects. The second is a delta which
// depends on the third, which is also a delta.
outOfOrderPack = []byte{
0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76,
0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10,
0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62,
0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x75, 0x01,
0xd7, 0x71, 0x36, 0x66, 0xf4, 0xde, 0x82, 0x27, 0x76, 0xc7, 0x62, 0x2c,
0x10, 0xf1, 0xb0, 0x7d, 0xe2, 0x80, 0xdc, 0x78, 0x9c, 0x63, 0x62, 0x62,
0x62, 0xb7, 0x03, 0x00, 0x00, 0x69, 0x00, 0x4c, 0xde, 0x7d, 0xaa, 0xe4,
0x19, 0x87, 0x58, 0x80, 0x61, 0x09, 0x9a, 0x33, 0xca, 0x7a, 0x31, 0x92,
0x6f, 0xae, 0x66, 0x75,
}
)
func TestIndexerOutOfOrder(t *testing.T) {
t.Parallel()
tmpPath, err := ioutil.TempDir("", "git2go")
checkFatal(t, err)
defer os.RemoveAll(tmpPath)
var finalStats TransferProgress
idx, err := NewIndexer(tmpPath, nil, func(stats TransferProgress) error {
finalStats = stats
return nil
})
checkFatal(t, err)
defer idx.Free()
_, err = idx.Write(outOfOrderPack)
checkFatal(t, err)
oid, err := idx.Commit()
checkFatal(t, err)
// The packfile contains the hash as the last 20 bytes.
expectedOid := NewOidFromBytes(outOfOrderPack[len(outOfOrderPack)-20:])
if !expectedOid.Equal(oid) {
t.Errorf("mismatched packfile hash, expected %v, got %v", expectedOid, oid)
}
if finalStats.TotalObjects != 3 {
t.Errorf("mismatched transferred objects, expected 3, got %v", finalStats.TotalObjects)
}
if finalStats.ReceivedObjects != 3 {
t.Errorf("mismatched received objects, expected 3, got %v", finalStats.ReceivedObjects)
}
if finalStats.IndexedObjects != 3 {
t.Errorf("mismatched indexed objects, expected 3, got %v", finalStats.IndexedObjects)
}
odb, err := NewOdb()
checkFatal(t, err)
defer odb.Free()
backend, err := NewOdbBackendOnePack(path.Join(tmpPath, fmt.Sprintf("pack-%s.idx", oid.String())))
checkFatal(t, err)
// Transfer the ownership of the backend to the odb, no freeing needed.
err = odb.AddBackend(backend, 1)
checkFatal(t, err)
packfileObjects := 0
err = odb.ForEach(func(id *Oid) error {
packfileObjects += 1
return nil
})
checkFatal(t, err)
if packfileObjects != 3 {
t.Errorf("mismatched packfile objects, expected 3, got %v", packfileObjects)
}
// Inspect one of the well-known objects in the packfile.
obj, err := odb.Read(NewOidFromBytes([]byte{
0x19, 0x10, 0x28, 0x15, 0x66, 0x3d, 0x23, 0xf8, 0xb7, 0x5a, 0x47, 0xe7,
0xa0, 0x19, 0x65, 0xdc, 0xdc, 0x96, 0x46, 0x8c,
}))
checkFatal(t, err)
defer obj.Free()
if "foo" != string(obj.Data()) {
t.Errorf("mismatched packfile object contents, expected foo, got %q", string(obj.Data()))
}
}

92
mempack.go Normal file
View File

@ -0,0 +1,92 @@
package git
/*
#include <git2.h>
#include <git2/sys/mempack.h>
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_reset(git_odb_backend *backend);
extern void _go_git_odb_backend_free(git_odb_backend *backend);
*/
import "C"
import (
"runtime"
"unsafe"
)
// Mempack is a custom ODB backend that permits packing object in-memory.
type Mempack struct {
doNotCompare
ptr *C.git_odb_backend
}
// NewMempack creates a new mempack instance and registers it to the ODB.
func NewMempack(odb *Odb) (mempack *Mempack, err error) {
mempack = new(Mempack)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_mempack_new(&mempack.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
ret = C.git_odb_add_backend(odb.ptr, mempack.ptr, C.int(999))
runtime.KeepAlive(odb)
if ret < 0 {
// Since git_odb_add_alternate() takes ownership of the ODB backend, the
// only case in which we free the mempack's memory is if it fails to be
// added to the ODB.
C._go_git_odb_backend_free(mempack.ptr)
return nil, MakeGitError(ret)
}
return mempack, nil
}
// Dump dumps all the queued in-memory writes to a packfile.
//
// It is the caller's responsibility to ensure that the generated packfile is
// available to the repository (e.g. by writing it to disk, or doing something
// crazy like distributing it across several copies of the repository over a
// network).
//
// Once the generated packfile is available to the repository, call
// Mempack.Reset to cleanup the memory store.
//
// Calling Mempack.Reset before the packfile has been written to disk will
// result in an inconsistent repository (the objects in the memory store won't
// be accessible).
func (mempack *Mempack) Dump(repository *Repository) ([]byte, error) {
buf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_mempack_dump(&buf, repository.ptr, mempack.ptr)
runtime.KeepAlive(repository)
if ret < 0 {
return nil, MakeGitError(ret)
}
defer C.git_buf_dispose(&buf)
return C.GoBytes(unsafe.Pointer(buf.ptr), C.int(buf.size)), nil
}
// Reset resets the memory packer by clearing all the queued objects.
//
// This assumes that Mempack.Dump has been called before to store all the
// queued objects into a single packfile.
func (mempack *Mempack) Reset() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_mempack_reset(mempack.ptr)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}

60
mempack_test.go Normal file
View File

@ -0,0 +1,60 @@
package git
import (
"bytes"
"testing"
)
func TestMempack(t *testing.T) {
t.Parallel()
odb, err := NewOdb()
checkFatal(t, err)
repo, err := NewRepositoryWrapOdb(odb)
checkFatal(t, err)
mempack, err := NewMempack(odb)
checkFatal(t, err)
id, err := odb.Write([]byte("hello, world!"), ObjectBlob)
checkFatal(t, err)
expectedId, err := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
checkFatal(t, err)
if !expectedId.Equal(id) {
t.Errorf("mismatched id. expected %v, got %v", expectedId.String(), id.String())
}
// The object should be available from the odb.
{
obj, err := odb.Read(expectedId)
checkFatal(t, err)
defer obj.Free()
}
data, err := mempack.Dump(repo)
checkFatal(t, err)
expectedData := []byte{
0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
0x02, 0x9d, 0x08, 0x82, 0x3b, 0xd8, 0xa8, 0xea, 0xb5, 0x10, 0xad, 0x6a,
0xc7, 0x5c, 0x82, 0x3c, 0xfd, 0x3e, 0xd3, 0x1e,
}
if !bytes.Equal(expectedData, data) {
t.Errorf("mismatched mempack data. expected %v, got %v", expectedData, data)
}
mempack.Reset()
// After the reset, the object should now be unavailable.
{
obj, err := odb.Read(expectedId)
if err == nil {
t.Errorf("object %s unexpectedly found", obj.Id().String())
obj.Free()
} else if !IsErrorCode(err, ErrorCodeNotFound) {
t.Errorf("unexpected error %v", err)
}
}
}

596
merge.go Normal file
View File

@ -0,0 +1,596 @@
package git
/*
#include <git2.h>
extern git_annotated_commit** _go_git_make_merge_head_array(size_t len);
extern void _go_git_annotated_commit_array_set(git_annotated_commit** array, git_annotated_commit* ptr, size_t n);
extern git_annotated_commit* _go_git_annotated_commit_array_get(git_annotated_commit** array, size_t n);
extern int _go_git_merge_file(git_merge_file_result*, char*, size_t, char*, unsigned int, char*, size_t, char*, unsigned int, char*, size_t, char*, unsigned int, git_merge_file_options*);
*/
import "C"
import (
"reflect"
"runtime"
"unsafe"
)
type AnnotatedCommit struct {
doNotCompare
ptr *C.git_annotated_commit
r *Repository
}
func newAnnotatedCommitFromC(ptr *C.git_annotated_commit, r *Repository) *AnnotatedCommit {
mh := &AnnotatedCommit{ptr: ptr, r: r}
runtime.SetFinalizer(mh, (*AnnotatedCommit).Free)
return mh
}
func (mh *AnnotatedCommit) Id() *Oid {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := newOidFromC(C.git_annotated_commit_id(mh.ptr))
runtime.KeepAlive(mh)
return ret
}
func (mh *AnnotatedCommit) Free() {
runtime.SetFinalizer(mh, nil)
C.git_annotated_commit_free(mh.ptr)
}
func (r *Repository) AnnotatedCommitFromFetchHead(branchName string, remoteURL string, oid *Oid) (*AnnotatedCommit, error) {
cbranchName := C.CString(branchName)
defer C.free(unsafe.Pointer(cbranchName))
cremoteURL := C.CString(remoteURL)
defer C.free(unsafe.Pointer(cremoteURL))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_from_fetchhead(&ptr, r.ptr, cbranchName, cremoteURL, oid.toC())
runtime.KeepAlive(oid)
if ret < 0 {
return nil, MakeGitError(ret)
}
annotatedCommit := newAnnotatedCommitFromC(ptr, r)
runtime.KeepAlive(r)
return annotatedCommit, nil
}
func (r *Repository) LookupAnnotatedCommit(oid *Oid) (*AnnotatedCommit, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_lookup(&ptr, r.ptr, oid.toC())
runtime.KeepAlive(oid)
if ret < 0 {
return nil, MakeGitError(ret)
}
annotatedCommit := newAnnotatedCommitFromC(ptr, r)
runtime.KeepAlive(r)
return annotatedCommit, nil
}
func (r *Repository) AnnotatedCommitFromRef(ref *Reference) (*AnnotatedCommit, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_from_ref(&ptr, r.ptr, ref.ptr)
runtime.KeepAlive(r)
runtime.KeepAlive(ref)
if ret < 0 {
return nil, MakeGitError(ret)
}
annotatedCommit := newAnnotatedCommitFromC(ptr, r)
runtime.KeepAlive(r)
return annotatedCommit, nil
}
func (r *Repository) AnnotatedCommitFromRevspec(spec string) (*AnnotatedCommit, error) {
crevspec := C.CString(spec)
defer C.free(unsafe.Pointer(crevspec))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_annotated_commit
ret := C.git_annotated_commit_from_revspec(&ptr, r.ptr, crevspec)
runtime.KeepAlive(r)
if ret < 0 {
return nil, MakeGitError(ret)
}
annotatedCommit := newAnnotatedCommitFromC(ptr, r)
runtime.KeepAlive(r)
return annotatedCommit, nil
}
type MergeTreeFlag int
const (
// Detect renames that occur between the common ancestor and the "ours"
// side or the common ancestor and the "theirs" side. This will enable
// the ability to merge between a modified and renamed file.
MergeTreeFindRenames MergeTreeFlag = C.GIT_MERGE_FIND_RENAMES
// If a conflict occurs, exit immediately instead of attempting to
// continue resolving conflicts. The merge operation will fail with
// GIT_EMERGECONFLICT and no index will be returned.
MergeTreeFailOnConflict MergeTreeFlag = C.GIT_MERGE_FAIL_ON_CONFLICT
// MergeTreeSkipREUC specifies not to write the REUC extension on the
// generated index.
MergeTreeSkipREUC MergeTreeFlag = C.GIT_MERGE_SKIP_REUC
// MergeTreeNoRecursive specifies not to build a recursive merge base (by
// merging the multiple merge bases) if the commits being merged have
// multiple merge bases. Instead, the first base is used.
// This flag provides a similar merge base to `git-merge-resolve`.
MergeTreeNoRecursive MergeTreeFlag = C.GIT_MERGE_NO_RECURSIVE
)
type MergeOptions struct {
TreeFlags MergeTreeFlag
RenameThreshold uint
TargetLimit uint
RecursionLimit uint
FileFavor MergeFileFavor
//TODO: Diff similarity metric
}
func mergeOptionsFromC(opts *C.git_merge_options) MergeOptions {
return MergeOptions{
TreeFlags: MergeTreeFlag(opts.flags),
RenameThreshold: uint(opts.rename_threshold),
TargetLimit: uint(opts.target_limit),
RecursionLimit: uint(opts.recursion_limit),
FileFavor: MergeFileFavor(opts.file_favor),
}
}
func DefaultMergeOptions() (MergeOptions, error) {
opts := C.git_merge_options{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_merge_options_init(&opts, C.GIT_MERGE_OPTIONS_VERSION)
if ecode < 0 {
return MergeOptions{}, MakeGitError(ecode)
}
return mergeOptionsFromC(&opts), nil
}
func populateMergeOptions(copts *C.git_merge_options, opts *MergeOptions) *C.git_merge_options {
C.git_merge_options_init(copts, C.GIT_MERGE_OPTIONS_VERSION)
if opts == nil {
return nil
}
copts.flags = C.uint32_t(opts.TreeFlags)
copts.rename_threshold = C.uint(opts.RenameThreshold)
copts.target_limit = C.uint(opts.TargetLimit)
copts.recursion_limit = C.uint(opts.RecursionLimit)
copts.file_favor = C.git_merge_file_favor_t(opts.FileFavor)
return copts
}
func freeMergeOptions(copts *C.git_merge_options) {
}
type MergeFileFavor int
const (
MergeFileFavorNormal MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_NORMAL
MergeFileFavorOurs MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_OURS
MergeFileFavorTheirs MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_THEIRS
MergeFileFavorUnion MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_UNION
)
func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOptions, checkoutOptions *CheckoutOptions) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var err error
cMergeOpts := populateMergeOptions(&C.git_merge_options{}, mergeOptions)
defer freeMergeOptions(cMergeOpts)
cCheckoutOptions := populateCheckoutOptions(&C.git_checkout_options{}, checkoutOptions, &err)
defer freeCheckoutOptions(cCheckoutOptions)
gmerge_head_array := make([]*C.git_annotated_commit, len(theirHeads))
for i := 0; i < len(theirHeads); i++ {
gmerge_head_array[i] = theirHeads[i].ptr
}
ptr := unsafe.Pointer(&gmerge_head_array[0])
ret := C.git_merge(r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)), cMergeOpts, cCheckoutOptions)
runtime.KeepAlive(theirHeads)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
type MergeAnalysis int
const (
MergeAnalysisNone MergeAnalysis = C.GIT_MERGE_ANALYSIS_NONE
MergeAnalysisNormal MergeAnalysis = C.GIT_MERGE_ANALYSIS_NORMAL
MergeAnalysisUpToDate MergeAnalysis = C.GIT_MERGE_ANALYSIS_UP_TO_DATE
MergeAnalysisFastForward MergeAnalysis = C.GIT_MERGE_ANALYSIS_FASTFORWARD
MergeAnalysisUnborn MergeAnalysis = C.GIT_MERGE_ANALYSIS_UNBORN
)
type MergePreference int
const (
MergePreferenceNone MergePreference = C.GIT_MERGE_PREFERENCE_NONE
MergePreferenceNoFastForward MergePreference = C.GIT_MERGE_PREFERENCE_NO_FASTFORWARD
MergePreferenceFastForwardOnly MergePreference = C.GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
)
// MergeAnalysis returns the possible actions which could be taken by
// a 'git-merge' command. There may be multiple answers, so the first
// return value is a bitmask of MergeAnalysis values.
func (r *Repository) MergeAnalysis(theirHeads []*AnnotatedCommit) (MergeAnalysis, MergePreference, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
gmerge_head_array := make([]*C.git_annotated_commit, len(theirHeads))
for i := 0; i < len(theirHeads); i++ {
gmerge_head_array[i] = theirHeads[i].ptr
}
ptr := unsafe.Pointer(&gmerge_head_array[0])
var analysis C.git_merge_analysis_t
var preference C.git_merge_preference_t
err := C.git_merge_analysis(&analysis, &preference, r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)))
runtime.KeepAlive(theirHeads)
if err < 0 {
return MergeAnalysisNone, MergePreferenceNone, MakeGitError(err)
}
return MergeAnalysis(analysis), MergePreference(preference), nil
}
func (r *Repository) MergeCommits(ours *Commit, theirs *Commit, options *MergeOptions) (*Index, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
copts := populateMergeOptions(&C.git_merge_options{}, options)
defer freeMergeOptions(copts)
var ptr *C.git_index
ret := C.git_merge_commits(&ptr, r.ptr, ours.cast_ptr, theirs.cast_ptr, copts)
runtime.KeepAlive(ours)
runtime.KeepAlive(theirs)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newIndexFromC(ptr, r), nil
}
func (r *Repository) MergeTrees(ancestor *Tree, ours *Tree, theirs *Tree, options *MergeOptions) (*Index, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
copts := populateMergeOptions(&C.git_merge_options{}, options)
defer freeMergeOptions(copts)
var ancestor_ptr *C.git_tree
if ancestor != nil {
ancestor_ptr = ancestor.cast_ptr
}
var ptr *C.git_index
ret := C.git_merge_trees(&ptr, r.ptr, ancestor_ptr, ours.cast_ptr, theirs.cast_ptr, copts)
runtime.KeepAlive(ancestor)
runtime.KeepAlive(ours)
runtime.KeepAlive(theirs)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newIndexFromC(ptr, r), nil
}
func (r *Repository) MergeBase(one *Oid, two *Oid) (*Oid, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var oid C.git_oid
ret := C.git_merge_base(&oid, r.ptr, one.toC(), two.toC())
runtime.KeepAlive(one)
runtime.KeepAlive(two)
runtime.KeepAlive(r)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newOidFromC(&oid), nil
}
// MergeBases retrieves the list of merge bases between two commits.
//
// If none are found, an empty slice is returned and the error is set
// approprately
func (r *Repository) MergeBases(one, two *Oid) ([]*Oid, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var coids C.git_oidarray
ret := C.git_merge_bases(&coids, r.ptr, one.toC(), two.toC())
runtime.KeepAlive(one)
runtime.KeepAlive(two)
if ret < 0 {
return nil, MakeGitError(ret)
}
oids := make([]*Oid, coids.count)
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(coids.ids)),
Len: int(coids.count),
Cap: int(coids.count),
}
goSlice := *(*[]C.git_oid)(unsafe.Pointer(&hdr))
for i, cid := range goSlice {
oids[i] = newOidFromC(&cid)
}
return oids, nil
}
// MergeBaseMany finds a merge base given a list of commits.
func (r *Repository) MergeBaseMany(oids []*Oid) (*Oid, error) {
coids := make([]C.git_oid, len(oids))
for i := 0; i < len(oids); i++ {
coids[i] = *oids[i].toC()
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var oid C.git_oid
ret := C.git_merge_base_many(&oid, r.ptr, C.size_t(len(oids)), &coids[0])
runtime.KeepAlive(r)
runtime.KeepAlive(coids)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newOidFromC(&oid), nil
}
// MergeBasesMany finds all merge bases given a list of commits.
func (r *Repository) MergeBasesMany(oids []*Oid) ([]*Oid, error) {
inCoids := make([]C.git_oid, len(oids))
for i := 0; i < len(oids); i++ {
inCoids[i] = *oids[i].toC()
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var outCoids C.git_oidarray
ret := C.git_merge_bases_many(&outCoids, r.ptr, C.size_t(len(oids)), &inCoids[0])
runtime.KeepAlive(r)
runtime.KeepAlive(inCoids)
if ret < 0 {
return nil, MakeGitError(ret)
}
outOids := make([]*Oid, outCoids.count)
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(outCoids.ids)),
Len: int(outCoids.count),
Cap: int(outCoids.count),
}
goSlice := *(*[]C.git_oid)(unsafe.Pointer(&hdr))
for i, cid := range goSlice {
outOids[i] = newOidFromC(&cid)
}
return outOids, nil
}
// MergeBaseOctopus finds a merge base in preparation for an octopus merge.
func (r *Repository) MergeBaseOctopus(oids []*Oid) (*Oid, error) {
coids := make([]C.git_oid, len(oids))
for i := 0; i < len(oids); i++ {
coids[i] = *oids[i].toC()
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var oid C.git_oid
ret := C.git_merge_base_octopus(&oid, r.ptr, C.size_t(len(oids)), &coids[0])
runtime.KeepAlive(r)
runtime.KeepAlive(coids)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newOidFromC(&oid), nil
}
type MergeFileResult struct {
doNotCompare
ptr *C.git_merge_file_result
Automergeable bool
Path string
Mode uint
Contents []byte
}
func newMergeFileResultFromC(c *C.git_merge_file_result) *MergeFileResult {
var path string
if c.path != nil {
path = C.GoString(c.path)
}
originalBytes := C.GoBytes(unsafe.Pointer(c.ptr), C.int(c.len))
gobytes := make([]byte, len(originalBytes))
copy(gobytes, originalBytes)
r := &MergeFileResult{
Automergeable: c.automergeable != 0,
Path: path,
Mode: uint(c.mode),
Contents: gobytes,
ptr: c,
}
runtime.SetFinalizer(r, (*MergeFileResult).Free)
return r
}
func (r *MergeFileResult) Free() {
runtime.SetFinalizer(r, nil)
C.git_merge_file_result_free(r.ptr)
}
type MergeFileInput struct {
Path string
Mode uint
Contents []byte
}
type MergeFileFlags int
const (
MergeFileDefault MergeFileFlags = C.GIT_MERGE_FILE_DEFAULT
// Create standard conflicted merge files
MergeFileStyleMerge MergeFileFlags = C.GIT_MERGE_FILE_STYLE_MERGE
// Create diff3-style files
MergeFileStyleDiff MergeFileFlags = C.GIT_MERGE_FILE_STYLE_DIFF3
// Condense non-alphanumeric regions for simplified diff file
MergeFileStyleSimplifyAlnum MergeFileFlags = C.GIT_MERGE_FILE_SIMPLIFY_ALNUM
// Ignore all whitespace
MergeFileIgnoreWhitespace MergeFileFlags = C.GIT_MERGE_FILE_IGNORE_WHITESPACE
// Ignore changes in amount of whitespace
MergeFileIgnoreWhitespaceChange MergeFileFlags = C.GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE
// Ignore whitespace at end of line
MergeFileIgnoreWhitespaceEOL MergeFileFlags = C.GIT_MERGE_FILE_IGNORE_WHITESPACE_EOL
// Use the "patience diff" algorithm
MergeFileDiffPatience MergeFileFlags = C.GIT_MERGE_FILE_DIFF_PATIENCE
// Take extra time to find minimal diff
MergeFileDiffMinimal MergeFileFlags = C.GIT_MERGE_FILE_DIFF_MINIMAL
)
type MergeFileOptions struct {
AncestorLabel string
OurLabel string
TheirLabel string
Favor MergeFileFavor
Flags MergeFileFlags
MarkerSize uint16
}
func mergeFileOptionsFromC(c C.git_merge_file_options) MergeFileOptions {
return MergeFileOptions{
AncestorLabel: C.GoString(c.ancestor_label),
OurLabel: C.GoString(c.our_label),
TheirLabel: C.GoString(c.their_label),
Favor: MergeFileFavor(c.favor),
Flags: MergeFileFlags(c.flags),
MarkerSize: uint16(c.marker_size),
}
}
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)
if opts == nil {
return nil
}
copts.ancestor_label = C.CString(opts.AncestorLabel)
copts.our_label = C.CString(opts.OurLabel)
copts.their_label = C.CString(opts.TheirLabel)
copts.favor = C.git_merge_file_favor_t(opts.Favor)
copts.flags = C.uint32_t(opts.Flags)
copts.marker_size = C.ushort(opts.MarkerSize)
return copts
}
func freeMergeFileOptions(copts *C.git_merge_file_options) {
if copts == nil {
return
}
C.free(unsafe.Pointer(copts.ancestor_label))
C.free(unsafe.Pointer(copts.our_label))
C.free(unsafe.Pointer(copts.their_label))
}
func MergeFile(ancestor MergeFileInput, ours MergeFileInput, theirs MergeFileInput, options *MergeFileOptions) (*MergeFileResult, error) {
ancestorPath := C.CString(ancestor.Path)
defer C.free(unsafe.Pointer(ancestorPath))
var ancestorContents *byte
if len(ancestor.Contents) > 0 {
ancestorContents = &ancestor.Contents[0]
}
oursPath := C.CString(ours.Path)
defer C.free(unsafe.Pointer(oursPath))
var oursContents *byte
if len(ours.Contents) > 0 {
oursContents = &ours.Contents[0]
}
theirsPath := C.CString(theirs.Path)
defer C.free(unsafe.Pointer(theirsPath))
var theirsContents *byte
if len(theirs.Contents) > 0 {
theirsContents = &theirs.Contents[0]
}
var copts *C.git_merge_file_options
if options != nil {
copts = &C.git_merge_file_options{}
ecode := C.git_merge_file_options_init(copts, C.GIT_MERGE_FILE_OPTIONS_VERSION)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
populateMergeFileOptions(copts, options)
defer freeMergeFileOptions(copts)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var result C.git_merge_file_result
ecode := C._go_git_merge_file(&result,
(*C.char)(unsafe.Pointer(ancestorContents)), C.size_t(len(ancestor.Contents)), ancestorPath, C.uint(ancestor.Mode),
(*C.char)(unsafe.Pointer(oursContents)), C.size_t(len(ours.Contents)), oursPath, C.uint(ours.Mode),
(*C.char)(unsafe.Pointer(theirsContents)), C.size_t(len(theirs.Contents)), theirsPath, C.uint(theirs.Mode),
copts)
runtime.KeepAlive(ancestor)
runtime.KeepAlive(ours)
runtime.KeepAlive(theirs)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newMergeFileResultFromC(&result), nil
}
// TODO: GIT_EXTERN(int) git_merge_file_from_index(git_merge_file_result *out,git_repository *repo,const git_index_entry *ancestor, const git_index_entry *ours, const git_index_entry *theirs, const git_merge_file_options *opts);

254
merge_test.go Normal file
View File

@ -0,0 +1,254 @@
package git
import (
"testing"
"time"
)
func TestAnnotatedCommitFromRevspec(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
mergeHead, err := repo.AnnotatedCommitFromRevspec("refs/heads/master")
checkFatal(t, err)
expectedId := "473bf778b67b6d53e2ab289e0f1a2e8addef2fc2"
if mergeHead.Id().String() != expectedId {
t.Errorf("mergeHead.Id() = %v, want %v", mergeHead.Id(), expectedId)
}
}
func TestMergeWithSelf(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
master, err := repo.References.Lookup("refs/heads/master")
checkFatal(t, err)
mergeHead, err := repo.AnnotatedCommitFromRef(master)
checkFatal(t, err)
expectedId := "473bf778b67b6d53e2ab289e0f1a2e8addef2fc2"
if mergeHead.Id().String() != expectedId {
t.Errorf("mergeHead.Id() = %v, want %v", mergeHead.Id(), expectedId)
}
mergeHeads := make([]*AnnotatedCommit, 1)
mergeHeads[0] = mergeHead
err = repo.Merge(mergeHeads, nil, nil)
checkFatal(t, err)
mergeMessage, err := repo.Message()
checkFatal(t, err)
expectedMessage := "Merge branch 'master'\n"
if mergeMessage != expectedMessage {
t.Errorf("merge Message = %v, want %v", mergeMessage, expectedMessage)
}
}
func TestMergeAnalysisWithSelf(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
master, err := repo.References.Lookup("refs/heads/master")
checkFatal(t, err)
mergeHead, err := repo.AnnotatedCommitFromRef(master)
checkFatal(t, err)
mergeHeads := make([]*AnnotatedCommit, 1)
mergeHeads[0] = mergeHead
a, _, err := repo.MergeAnalysis(mergeHeads)
checkFatal(t, err)
if a != MergeAnalysisUpToDate {
t.Fatalf("Expected up to date merge, not %v", a)
}
}
func TestMergeSameFile(t *testing.T) {
t.Parallel()
file := MergeFileInput{
Path: "test",
Mode: 33188,
Contents: []byte("hello world"),
}
result, err := MergeFile(file, file, file, nil)
checkFatal(t, err)
if !result.Automergeable {
t.Fatal("expected automergeable")
}
if result.Path != file.Path {
t.Fatal("path was incorrect")
}
if result.Mode != file.Mode {
t.Fatal("mode was incorrect")
}
compareBytes(t, file.Contents, result.Contents)
}
func TestMergeTreesWithoutAncestor(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, originalTreeId := seedTestRepo(t, repo)
originalTree, err := repo.LookupTree(originalTreeId)
checkFatal(t, err)
_, newTreeId := updateReadme(t, repo, "file changed\n")
newTree, err := repo.LookupTree(newTreeId)
checkFatal(t, err)
index, err := repo.MergeTrees(nil, originalTree, newTree, nil)
if !index.HasConflicts() {
t.Fatal("expected conflicts in the index")
}
_, err = index.Conflict("README")
checkFatal(t, err)
}
func appendCommit(t *testing.T, repo *Repository) (*Oid, *Oid) {
loc, err := time.LoadLocation("Europe/Berlin")
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc),
}
idx, err := repo.Index()
checkFatal(t, err)
err = idx.AddByPath("README")
checkFatal(t, err)
treeId, err := idx.WriteTree()
checkFatal(t, err)
message := "This is another commit\n"
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
ref, err := repo.References.Lookup("HEAD")
checkFatal(t, err)
parent, err := ref.Peel(ObjectCommit)
checkFatal(t, err)
parentCommit, err := parent.AsCommit()
checkFatal(t, err)
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree, parentCommit)
checkFatal(t, err)
return commitId, treeId
}
func TestMergeBase(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitAId, _ := seedTestRepo(t, repo)
commitBId, _ := appendCommit(t, repo)
mergeBase, err := repo.MergeBase(commitAId, commitBId)
checkFatal(t, err)
if mergeBase.Cmp(commitAId) != 0 {
t.Fatalf("unexpected merge base")
}
}
func TestMergeBases(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitAId, _ := seedTestRepo(t, repo)
commitBId, _ := appendCommit(t, repo)
mergeBases, err := repo.MergeBases(commitAId, commitBId)
checkFatal(t, err)
if len(mergeBases) != 1 {
t.Fatalf("expected merge bases len to be 1, got %v", len(mergeBases))
}
if mergeBases[0].Cmp(commitAId) != 0 {
t.Fatalf("unexpected merge base")
}
}
func TestMergeBaseMany(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitAId, _ := seedTestRepo(t, repo)
commitBId, _ := appendCommit(t, repo)
mergeBase, err := repo.MergeBaseMany([]*Oid{commitAId, commitBId})
checkFatal(t, err)
if mergeBase.Cmp(commitAId) != 0 {
t.Fatalf("unexpected merge base")
}
}
func TestMergeBasesMany(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitAId, _ := seedTestRepo(t, repo)
commitBId, _ := appendCommit(t, repo)
mergeBases, err := repo.MergeBasesMany([]*Oid{commitAId, commitBId})
checkFatal(t, err)
if len(mergeBases) != 1 {
t.Fatalf("expected merge bases len to be 1, got %v", len(mergeBases))
}
if mergeBases[0].Cmp(commitAId) != 0 {
t.Fatalf("unexpected merge base")
}
}
func TestMergeBaseOctopus(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitAId, _ := seedTestRepo(t, repo)
commitBId, _ := appendCommit(t, repo)
mergeBase, err := repo.MergeBaseOctopus([]*Oid{commitAId, commitBId})
checkFatal(t, err)
if mergeBase.Cmp(commitAId) != 0 {
t.Fatalf("unexpected merge base")
}
}
func compareBytes(t *testing.T, expected, actual []byte) {
for i, v := range expected {
if actual[i] != v {
t.Fatalf("Bad bytes")
}
}
}

42
message.go Normal file
View File

@ -0,0 +1,42 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
// Trailer represents a single git message trailer.
type Trailer struct {
Key string
Value string
}
// MessageTrailers parses trailers out of a message, returning a slice of
// Trailer structs. Trailers are key/value pairs in the last paragraph of a
// message, not including any patches or conflicts that may be present.
func MessageTrailers(message string) ([]Trailer, error) {
var trailersC C.git_message_trailer_array
messageC := C.CString(message)
defer C.free(unsafe.Pointer(messageC))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_message_trailers(&trailersC, messageC)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
defer C.git_message_trailer_array_free(&trailersC)
trailers := make([]Trailer, trailersC.count)
var trailer *C.git_message_trailer
for i, p := 0, uintptr(unsafe.Pointer(trailersC.trailers)); i < int(trailersC.count); i, p = i+1, p+unsafe.Sizeof(C.git_message_trailer{}) {
trailer = (*C.git_message_trailer)(unsafe.Pointer(p))
trailers[i] = Trailer{Key: C.GoString(trailer.key), Value: C.GoString(trailer.value)}
}
return trailers, nil
}

42
message_test.go Normal file
View File

@ -0,0 +1,42 @@
package git
import (
"fmt"
"reflect"
"testing"
)
func TestTrailers(t *testing.T) {
t.Parallel()
tests := []struct {
input string
expected []Trailer
}{
{
"commit with zero trailers\n",
[]Trailer{},
},
{
"commit with one trailer\n\nCo-authored-by: Alice <alice@example.com>\n",
[]Trailer{
Trailer{Key: "Co-authored-by", Value: "Alice <alice@example.com>"},
},
},
{
"commit with two trailers\n\nCo-authored-by: Alice <alice@example.com>\nSigned-off-by: Bob <bob@example.com>\n",
[]Trailer{
Trailer{Key: "Co-authored-by", Value: "Alice <alice@example.com>"},
Trailer{Key: "Signed-off-by", Value: "Bob <bob@example.com>"}},
},
}
for _, test := range tests {
fmt.Printf("%s", test.input)
actual, err := MessageTrailers(test.input)
if err != nil {
t.Errorf("Trailers returned an unexpected error: %v", err)
}
if !reflect.DeepEqual(test.expected, actual) {
t.Errorf("expecting %#v\ngot %#v", test.expected, actual)
}
}
}

247
note.go Normal file
View File

@ -0,0 +1,247 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
// This object represents the possible operations which can be
// performed on the collection of notes for a repository.
type NoteCollection struct {
doNotCompare
repo *Repository
}
// Create adds a note for an object
func (c *NoteCollection) Create(
ref string, author, committer *Signature, id *Oid,
note string, force bool) (*Oid, error) {
oid := new(Oid)
var cref *C.char
if ref == "" {
cref = nil
} else {
cref = C.CString(ref)
defer C.free(unsafe.Pointer(cref))
}
authorSig, err := author.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(committerSig)
cnote := C.CString(note)
defer C.free(unsafe.Pointer(cnote))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_note_create(
oid.toC(), c.repo.ptr, cref, authorSig,
committerSig, id.toC(), cnote, cbool(force))
runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}
// Read reads the note for an object
func (c *NoteCollection) Read(ref string, id *Oid) (*Note, error) {
var cref *C.char
if ref == "" {
cref = nil
} else {
cref = C.CString(ref)
defer C.free(unsafe.Pointer(cref))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_note
ret := C.git_note_read(&ptr, c.repo.ptr, cref, id.toC())
runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newNoteFromC(ptr, c.repo), nil
}
// Remove removes the note for an object
func (c *NoteCollection) Remove(ref string, author, committer *Signature, id *Oid) error {
var cref *C.char
if ref == "" {
cref = nil
} else {
cref = C.CString(ref)
defer C.free(unsafe.Pointer(cref))
}
authorSig, err := author.toC()
if err != nil {
return err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return err
}
defer C.git_signature_free(committerSig)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_note_remove(c.repo.ptr, cref, authorSig, committerSig, id.toC())
runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// DefaultRef returns the default notes reference for a repository
func (c *NoteCollection) DefaultRef() (string, error) {
buf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_note_default_ref(&buf, c.repo.ptr)
runtime.KeepAlive(c)
if ecode < 0 {
return "", MakeGitError(ecode)
}
ret := C.GoString(buf.ptr)
C.git_buf_dispose(&buf)
return ret, nil
}
// Note
type Note struct {
doNotCompare
ptr *C.git_note
r *Repository
}
func newNoteFromC(ptr *C.git_note, r *Repository) *Note {
note := &Note{ptr: ptr, r: r}
runtime.SetFinalizer(note, (*Note).Free)
return note
}
// Free frees a git_note object
func (n *Note) Free() error {
if n.ptr == nil {
return ErrInvalid
}
runtime.SetFinalizer(n, nil)
C.git_note_free(n.ptr)
n.ptr = nil
return nil
}
// Author returns the signature of the note author
func (n *Note) Author() *Signature {
ptr := C.git_note_author(n.ptr)
return newSignatureFromC(ptr)
}
// Id returns the note object's id
func (n *Note) Id() *Oid {
ptr := C.git_note_id(n.ptr)
runtime.KeepAlive(n)
return newOidFromC(ptr)
}
// Committer returns the signature of the note committer
func (n *Note) Committer() *Signature {
ptr := C.git_note_committer(n.ptr)
runtime.KeepAlive(n)
return newSignatureFromC(ptr)
}
// Message returns the note message
func (n *Note) Message() string {
ret := C.GoString(C.git_note_message(n.ptr))
runtime.KeepAlive(n)
return ret
}
// NoteIterator
type NoteIterator struct {
doNotCompare
ptr *C.git_note_iterator
r *Repository
}
// NewNoteIterator creates a new iterator for notes
func (repo *Repository) NewNoteIterator(ref string) (*NoteIterator, error) {
var cref *C.char
if ref == "" {
cref = nil
} else {
cref = C.CString(ref)
defer C.free(unsafe.Pointer(cref))
}
var ptr *C.git_note_iterator
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_note_iterator_new(&ptr, repo.ptr, cref)
runtime.KeepAlive(repo)
if ret < 0 {
return nil, MakeGitError(ret)
}
iter := &NoteIterator{ptr: ptr, r: repo}
runtime.SetFinalizer(iter, (*NoteIterator).Free)
return iter, nil
}
// Free frees the note interator
func (v *NoteIterator) Free() {
runtime.SetFinalizer(v, nil)
C.git_note_iterator_free(v.ptr)
}
// Next returns the current item (note id & annotated id) and advances the
// iterator internally to the next item
func (it *NoteIterator) Next() (noteId, annotatedId *Oid, err error) {
noteId, annotatedId = new(Oid), new(Oid)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_note_next(noteId.toC(), annotatedId.toC(), it.ptr)
runtime.KeepAlive(noteId)
runtime.KeepAlive(annotatedId)
runtime.KeepAlive(it)
if ret < 0 {
err = MakeGitError(ret)
}
return
}

117
note_test.go Normal file
View File

@ -0,0 +1,117 @@
package git
import (
"fmt"
"reflect"
"testing"
"time"
)
func TestCreateNote(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, _ := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
note, noteId := createTestNote(t, repo, commit)
compareStrings(t, "I am a note\n", note.Message())
compareStrings(t, noteId.String(), note.Id().String())
compareStrings(t, "alice", note.Author().Name)
compareStrings(t, "alice@example.com", note.Author().Email)
compareStrings(t, "alice", note.Committer().Name)
compareStrings(t, "alice@example.com", note.Committer().Email)
}
func TestNoteIterator(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
notes := make([]*Note, 5)
for i := range notes {
commitId, _ := updateReadme(t, repo, fmt.Sprintf("README v%d\n", i+1))
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
note, _ := createTestNote(t, repo, commit)
notes[i] = note
}
iter, err := repo.NewNoteIterator("")
checkFatal(t, err)
for {
noteId, commitId, err := iter.Next()
if err != nil {
if !IsErrorCode(err, ErrorCodeIterOver) {
checkFatal(t, err)
}
break
}
note, err := repo.Notes.Read("", commitId)
checkFatal(t, err)
if !reflect.DeepEqual(note.Id(), noteId) {
t.Errorf("expected note oid '%v', actual '%v'", note.Id(), noteId)
}
}
}
func TestRemoveNote(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, _ := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
note, _ := createTestNote(t, repo, commit)
_, err = repo.Notes.Read("", commit.Id())
checkFatal(t, err)
err = repo.Notes.Remove("", note.Author(), note.Committer(), commitId)
checkFatal(t, err)
_, err = repo.Notes.Read("", commit.Id())
if err == nil {
t.Fatal("note remove failed")
}
}
func TestDefaultNoteRef(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
ref, err := repo.Notes.DefaultRef()
checkFatal(t, err)
compareStrings(t, "refs/notes/commits", ref)
}
func createTestNote(t *testing.T, repo *Repository, commit *Commit) (*Note, *Oid) {
loc, err := time.LoadLocation("Europe/Berlin")
sig := &Signature{
Name: "alice",
Email: "alice@example.com",
When: time.Date(2015, 01, 05, 13, 0, 0, 0, loc),
}
noteId, err := repo.Notes.Create("", sig, sig, commit.Id(), "I am a note\n", false)
checkFatal(t, err)
note, err := repo.Notes.Read("", commit.Id())
checkFatal(t, err)
return note, noteId
}

242
object.go
View File

@ -1,40 +1,43 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
*/
import "C"
import "runtime"
import (
"errors"
"fmt"
"runtime"
)
type ObjectType int
const (
ObjectAny ObjectType = C.GIT_OBJ_ANY
ObjectBad = C.GIT_OBJ_BAD
ObjectCommit = C.GIT_OBJ_COMMIT
ObjectTree = C.GIT_OBJ_TREE
ObjectBlob = C.GIT_OBJ_BLOB
ObjectTag = C.GIT_OBJ_TAG
ObjectAny ObjectType = C.GIT_OBJECT_ANY
ObjectInvalid ObjectType = C.GIT_OBJECT_INVALID
ObjectCommit ObjectType = C.GIT_OBJECT_COMMIT
ObjectTree ObjectType = C.GIT_OBJECT_TREE
ObjectBlob ObjectType = C.GIT_OBJECT_BLOB
ObjectTag ObjectType = C.GIT_OBJECT_TAG
)
type Object interface {
Free()
Id() *Oid
Type() ObjectType
type Object struct {
doNotCompare
ptr *C.git_object
repo *Repository
}
type gitObject struct {
ptr *C.git_object
// Objecter lets us accept any kind of Git object in functions.
type Objecter interface {
AsObject() *Object
}
func (t ObjectType) String() (string) {
switch (t) {
func (t ObjectType) String() string {
switch t {
case ObjectAny:
return "Any"
case ObjectBad:
return "Bad"
case ObjectInvalid:
return "Invalid"
case ObjectCommit:
return "Commit"
case ObjectTree:
@ -42,43 +45,194 @@ func (t ObjectType) String() (string) {
case ObjectBlob:
return "Blob"
case ObjectTag:
return "tag"
return "Tag"
}
// Never reached
return ""
}
func (o gitObject) Id() *Oid {
return newOidFromC(C.git_commit_id(o.ptr))
func (o *Object) Id() *Oid {
ret := newOidFromC(C.git_object_id(o.ptr))
runtime.KeepAlive(o)
return ret
}
func (o gitObject) Type() ObjectType {
return ObjectType(C.git_object_type(o.ptr))
func (o *Object) ShortId() (string, error) {
resultBuf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_object_short_id(&resultBuf, o.ptr)
runtime.KeepAlive(o)
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_dispose(&resultBuf)
return C.GoString(resultBuf.ptr), nil
}
func (o *gitObject) Free() {
runtime.SetFinalizer(o, nil)
C.git_commit_free(o.ptr)
func (o *Object) Type() ObjectType {
ret := ObjectType(C.git_object_type(o.ptr))
runtime.KeepAlive(o)
return ret
}
func allocObject(cobj *C.git_object) Object {
// Owner returns a weak reference to the repository which owns this object.
// This won't keep the underlying repository alive, but it should still be
// Freed.
func (o *Object) Owner() *Repository {
repo := newRepositoryFromC(C.git_object_owner(o.ptr))
runtime.KeepAlive(o)
repo.weak = true
return repo
}
switch ObjectType(C.git_object_type(cobj)) {
case ObjectCommit:
commit := &Commit{gitObject{cobj}}
runtime.SetFinalizer(commit, (*Commit).Free)
return commit
case ObjectTree:
tree := &Tree{gitObject{cobj}}
runtime.SetFinalizer(tree, (*Tree).Free)
return tree
case ObjectBlob:
blob := &Blob{gitObject{cobj}}
runtime.SetFinalizer(blob, (*Blob).Free)
return blob
func dupObject(obj *Object, kind ObjectType) (*C.git_object, error) {
if obj.Type() != kind {
return nil, errors.New(fmt.Sprintf("object is not a %v", kind))
}
return nil
var cobj *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_object_dup(&cobj, obj.ptr)
runtime.KeepAlive(obj)
if err < 0 {
return nil, MakeGitError(err)
}
return cobj, nil
}
func allocTree(ptr *C.git_tree, repo *Repository) *Tree {
tree := &Tree{
Object: Object{
ptr: (*C.git_object)(ptr),
repo: repo,
},
cast_ptr: ptr,
}
runtime.SetFinalizer(tree, (*Tree).Free)
return tree
}
func (o *Object) AsTree() (*Tree, error) {
cobj, err := dupObject(o, ObjectTree)
if err != nil {
return nil, err
}
return allocTree((*C.git_tree)(cobj), o.repo), nil
}
func allocCommit(ptr *C.git_commit, repo *Repository) *Commit {
commit := &Commit{
Object: Object{
ptr: (*C.git_object)(ptr),
repo: repo,
},
cast_ptr: ptr,
}
runtime.SetFinalizer(commit, (*Commit).Free)
return commit
}
func (o *Object) AsCommit() (*Commit, error) {
cobj, err := dupObject(o, ObjectCommit)
if err != nil {
return nil, err
}
return allocCommit((*C.git_commit)(cobj), o.repo), nil
}
func allocBlob(ptr *C.git_blob, repo *Repository) *Blob {
blob := &Blob{
Object: Object{
ptr: (*C.git_object)(ptr),
repo: repo,
},
cast_ptr: ptr,
}
runtime.SetFinalizer(blob, (*Blob).Free)
return blob
}
func (o *Object) AsBlob() (*Blob, error) {
cobj, err := dupObject(o, ObjectBlob)
if err != nil {
return nil, err
}
return allocBlob((*C.git_blob)(cobj), o.repo), nil
}
func allocTag(ptr *C.git_tag, repo *Repository) *Tag {
tag := &Tag{
Object: Object{
ptr: (*C.git_object)(ptr),
repo: repo,
},
cast_ptr: ptr,
}
runtime.SetFinalizer(tag, (*Tag).Free)
return tag
}
func (o *Object) AsTag() (*Tag, error) {
cobj, err := dupObject(o, ObjectTag)
if err != nil {
return nil, err
}
return allocTag((*C.git_tag)(cobj), o.repo), nil
}
func (o *Object) Free() {
runtime.SetFinalizer(o, nil)
C.git_object_free(o.ptr)
}
// Peel recursively peels an object until an object of the specified type is met.
//
// If the query cannot be satisfied due to the object model, ErrorCodeInvalidSpec
// will be returned (e.g. trying to peel a blob to a tree).
//
// If you pass ObjectAny as the target type, then the object will be peeled
// until the type changes. A tag will be peeled until the referenced object
// is no longer a tag, and a commit will be peeled to a tree. Any other object
// type will return ErrorCodeInvalidSpec.
//
// If peeling a tag we discover an object which cannot be peeled to the target
// type due to the object model, an error will be returned.
func (o *Object) Peel(t ObjectType) (*Object, error) {
var cobj *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_object_peel(&cobj, o.ptr, C.git_object_t(t))
runtime.KeepAlive(o)
if err < 0 {
return nil, MakeGitError(err)
}
return allocObject(cobj, o.repo), nil
}
func allocObject(cobj *C.git_object, repo *Repository) *Object {
obj := &Object{
ptr: cobj,
repo: repo,
}
runtime.SetFinalizer(obj, (*Object).Free)
return obj
}

View File

@ -1,21 +1,23 @@
package git
import (
"os"
"strings"
"testing"
)
func TestObjectPoymorphism(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer os.RemoveAll(repo.Workdir())
defer cleanupTestRepo(t, repo)
commitId, treeId := seedTestRepo(t, repo)
var obj Object
var obj *Object
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
obj = commit
obj = &commit.Object
if obj.Type() != ObjectCommit {
t.Fatalf("Wrong object type, expected commit, have %v", obj.Type())
}
@ -27,13 +29,13 @@ func TestObjectPoymorphism(t *testing.T) {
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
obj = tree
obj = &tree.Object
if obj.Type() != ObjectTree {
t.Fatalf("Wrong object type, expected tree, have %v", obj.Type())
}
tree2, ok := obj.(*Tree)
if !ok {
tree2, err := obj.AsTree()
if err != nil {
t.Fatalf("Converting back to *Tree is not ok")
}
@ -46,16 +48,16 @@ func TestObjectPoymorphism(t *testing.T) {
t.Fatal("Wrong filemode for \"README\"")
}
_, ok = obj.(*Commit)
if ok {
_, err = obj.AsCommit()
if err == nil {
t.Fatalf("*Tree is somehow the same as *Commit")
}
obj, err = repo.Lookup(tree.Id())
checkFatal(t, err)
_, ok = obj.(*Tree)
if !ok {
_, err = obj.AsTree()
if err != nil {
t.Fatalf("Lookup creates the wrong type")
}
@ -75,3 +77,118 @@ func TestObjectPoymorphism(t *testing.T) {
t.Fatalf("Failed to parse the right revision")
}
}
func checkOwner(t *testing.T, repo *Repository, obj Object) {
owner := obj.Owner()
if owner == nil {
t.Fatal("bad owner")
}
if owner.ptr != repo.ptr {
t.Fatalf("bad owner, got %v expected %v\n", owner.ptr, repo.ptr)
}
}
func TestObjectOwner(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, treeId := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
checkOwner(t, repo, commit.Object)
checkOwner(t, repo, tree.Object)
}
func checkShortId(t *testing.T, Id, shortId string) {
if len(shortId) < 7 || len(shortId) >= len(Id) {
t.Fatalf("bad shortId length %d", len(shortId))
}
if !strings.HasPrefix(Id, shortId) {
t.Fatalf("bad shortId, should be prefix of %s, but is %s\n", Id, shortId)
}
}
func TestObjectShortId(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, _ := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
shortId, err := commit.ShortId()
checkFatal(t, err)
checkShortId(t, commitId.String(), shortId)
}
func TestObjectPeel(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitID, treeID := seedTestRepo(t, repo)
var obj *Object
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
obj, err = commit.Peel(ObjectAny)
checkFatal(t, err)
if obj.Type() != ObjectTree {
t.Fatalf("Wrong object type when peeling a commit, expected tree, have %v", obj.Type())
}
obj, err = commit.Peel(ObjectTag)
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
t.Fatalf("Wrong error when peeling a commit to a tag, expected ErrorCodeInvalidSpec, have %v", err)
}
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
obj, err = tree.Peel(ObjectAny)
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
t.Fatalf("Wrong error when peeling a tree, expected ErrorCodeInvalidSpec, have %v", err)
}
entry := tree.EntryByName("README")
blob, err := repo.LookupBlob(entry.Id)
checkFatal(t, err)
obj, err = blob.Peel(ObjectAny)
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
t.Fatalf("Wrong error when peeling a blob, expected ErrorCodeInvalidSpec, have %v", err)
}
tagID := createTestTag(t, repo, commit)
tag, err := repo.LookupTag(tagID)
checkFatal(t, err)
obj, err = tag.Peel(ObjectAny)
checkFatal(t, err)
if obj.Type() != ObjectCommit {
t.Fatalf("Wrong object type when peeling a tag, expected commit, have %v", obj.Type())
}
// TODO: Should test a tag that annotates a different object than a commit
// but it's impossible at the moment to tag such an object.
}

472
odb.go
View File

@ -1,78 +1,347 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
extern int git_odb_backend_one_pack(git_odb_backend **out, const char *index_file);
extern int git_odb_backend_loose(git_odb_backend **out, const char *objects_dir, int compression_level, int do_fsync, unsigned int dir_mode, unsigned int file_mode);
extern int _go_git_odb_foreach(git_odb *db, void *payload);
extern void _go_git_odb_backend_free(git_odb_backend *backend);
extern int _go_git_odb_write_pack(git_odb_writepack **out, git_odb *db, void *progress_payload);
extern int _go_git_odb_writepack_append(git_odb_writepack *writepack, const void *, size_t, git_transfer_progress *);
extern int _go_git_odb_writepack_commit(git_odb_writepack *writepack, git_transfer_progress *);
extern void _go_git_odb_writepack_free(git_odb_writepack *writepack);
*/
import "C"
import (
"unsafe"
"io"
"os"
"reflect"
"runtime"
"unsafe"
)
type Odb struct {
doNotCompare
ptr *C.git_odb
}
type OdbBackend struct {
doNotCompare
ptr *C.git_odb_backend
}
func NewOdb() (odb *Odb, err error) {
odb = new(Odb)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_new(&odb.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(odb, (*Odb).Free)
return odb, nil
}
func NewOdbBackendFromC(ptr unsafe.Pointer) (backend *OdbBackend) {
backend = &OdbBackend{ptr: (*C.git_odb_backend)(ptr)}
return backend
}
func (v *Odb) AddAlternate(backend *OdbBackend, priority int) (err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_add_alternate(v.ptr, backend.ptr, C.int(priority))
runtime.KeepAlive(v)
if ret < 0 {
backend.Free()
return MakeGitError(ret)
}
return nil
}
func (v *Odb) AddBackend(backend *OdbBackend, priority int) (err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_add_backend(v.ptr, backend.ptr, C.int(priority))
runtime.KeepAlive(v)
if ret < 0 {
backend.Free()
return MakeGitError(ret)
}
return nil
}
func NewOdbBackendOnePack(packfileIndexPath string) (backend *OdbBackend, err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cstr := C.CString(packfileIndexPath)
defer C.free(unsafe.Pointer(cstr))
var odbOnePack *C.git_odb_backend = nil
ret := C.git_odb_backend_one_pack(&odbOnePack, cstr)
if ret < 0 {
return nil, MakeGitError(ret)
}
return NewOdbBackendFromC(unsafe.Pointer(odbOnePack)), nil
}
// NewOdbBackendLoose creates a backend for loose objects.
func NewOdbBackendLoose(objectsDir string, compressionLevel int, doFsync bool, dirMode os.FileMode, fileMode os.FileMode) (backend *OdbBackend, err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var odbLoose *C.git_odb_backend = nil
var doFsyncInt C.int
if doFsync {
doFsyncInt = C.int(1)
}
cstr := C.CString(objectsDir)
defer C.free(unsafe.Pointer(cstr))
ret := C.git_odb_backend_loose(&odbLoose, cstr, C.int(compressionLevel), doFsyncInt, C.uint(dirMode), C.uint(fileMode))
if ret < 0 {
return nil, MakeGitError(ret)
}
return NewOdbBackendFromC(unsafe.Pointer(odbLoose)), nil
}
func (v *Odb) ReadHeader(oid *Oid) (uint64, ObjectType, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var sz C.size_t
var cotype C.git_object_t
ret := C.git_odb_read_header(&sz, &cotype, v.ptr, oid.toC())
runtime.KeepAlive(v)
if ret < 0 {
return 0, ObjectInvalid, MakeGitError(ret)
}
return uint64(sz), ObjectType(cotype), nil
}
func (v *Odb) Exists(oid *Oid) bool {
ret := C.git_odb_exists(v.ptr, oid.toC())
runtime.KeepAlive(v)
runtime.KeepAlive(oid)
return ret != 0
}
func (v *Odb) Write(data []byte, otype ObjectType) (oid *Oid, err error) {
oid = new(Oid)
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&data))
ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(hdr.Data), C.size_t(hdr.Len), C.git_otype(otype))
if ret < 0 {
err = LastError()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var size C.size_t
if len(data) > 0 {
size = C.size_t(len(data))
} else {
data = []byte{0}
size = C.size_t(0)
}
return
ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(&data[0]), size, C.git_object_t(otype))
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}
func (v *Odb) Read(oid *Oid) (obj *OdbObject, err error) {
obj = new(OdbObject)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_read(&obj.ptr, v.ptr, oid.toC())
runtime.KeepAlive(v)
runtime.KeepAlive(oid)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(obj, (*OdbObject).Free)
return
return obj, nil
}
//export odbForEachCb
func odbForEachCb(id *C.git_oid, payload unsafe.Pointer) int {
ch := *(*chan *Oid)(payload)
oid := newOidFromC(id)
// Because the channel is unbuffered, we never read our own data. If ch is
// readable, the user has sent something on it, which means we should
// abort.
select {
case ch <- oid:
case <-ch:
return -1
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 0;
return nil
}
func (v *Odb) forEachWrap(ch chan *Oid) {
C._go_git_odb_foreach(v.ptr, unsafe.Pointer(&ch))
close(ch)
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
}
func (v *Odb) ForEach() chan *Oid {
ch := make(chan *Oid, 0)
go v.forEachWrap(ch)
return ch
type OdbForEachCallback func(id *Oid) error
type odbForEachCallbackData struct {
callback OdbForEachCallback
errorTarget *error
}
//export odbForEachCallback
func odbForEachCallback(id *C.git_oid, handle unsafe.Pointer) C.int {
data, ok := pointerHandles.Get(handle).(*odbForEachCallbackData)
if !ok {
panic("could not retrieve handle")
}
err := data.callback(newOidFromC(id))
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
func (v *Odb) ForEach(callback OdbForEachCallback) error {
var err error
data := odbForEachCallbackData{
callback: callback,
errorTarget: &err,
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
handle := pointerHandles.Track(&data)
defer pointerHandles.Untrack(handle)
ret := C._go_git_odb_foreach(v.ptr, handle)
runtime.KeepAlive(v)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// Hash determines the object-ID (sha1) of a data buffer.
func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) {
oid = new(Oid)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var size C.size_t
if len(data) > 0 {
size = C.size_t(len(data))
} else {
data = []byte{0}
size = C.size_t(0)
}
ret := C.git_odb_hash(oid.toC(), unsafe.Pointer(&data[0]), size, C.git_object_t(otype))
runtime.KeepAlive(data)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}
// NewReadStream opens a read stream from the ODB. Reading from it will give you the
// contents of the object.
func (v *Odb) NewReadStream(id *Oid) (*OdbReadStream, error) {
stream := new(OdbReadStream)
var ctype C.git_object_t
var csize C.size_t
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_open_rstream(&stream.ptr, &csize, &ctype, v.ptr, id.toC())
runtime.KeepAlive(v)
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)
}
stream.Size = uint64(csize)
stream.Type = ObjectType(ctype)
runtime.SetFinalizer(stream, (*OdbReadStream).Free)
return stream, nil
}
// NewWriteStream opens a write stream to the ODB, which allows you to
// create a new object in the database. The size and type must be
// known in advance
func (v *Odb) NewWriteStream(size int64, otype ObjectType) (*OdbWriteStream, error) {
stream := new(OdbWriteStream)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_open_wstream(&stream.ptr, v.ptr, C.git_object_size_t(size), C.git_object_t(otype))
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(stream, (*OdbWriteStream).Free)
return stream, nil
}
// NewWritePack opens a stream for writing a pack file to the ODB. If the ODB
// layer understands pack files, then the given packfile will likely be
// streamed directly to disk (and a corresponding index created). If the ODB
// layer does not understand pack files, the objects will be stored in whatever
// format the ODB layer uses.
func (v *Odb) NewWritePack(callback TransferProgressCallback) (*OdbWritepack, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
writepack := new(OdbWritepack)
populateRemoteCallbacks(&writepack.ccallbacks, &RemoteCallbacks{TransferProgressCallback: callback}, nil)
ret := C._go_git_odb_write_pack(&writepack.ptr, v.ptr, writepack.ccallbacks.payload)
runtime.KeepAlive(v)
if ret < 0 {
untrackCallbacksPayload(&writepack.ccallbacks)
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(writepack, (*OdbWritepack).Free)
return writepack, nil
}
func (v *OdbBackend) Free() {
C._go_git_odb_backend_free(v.ptr)
}
type OdbObject struct {
doNotCompare
ptr *C.git_odb_object
}
@ -82,13 +351,25 @@ func (v *OdbObject) Free() {
}
func (object *OdbObject) Id() (oid *Oid) {
return newOidFromC(C.git_odb_object_id(object.ptr))
ret := newOidFromC(C.git_odb_object_id(object.ptr))
runtime.KeepAlive(object)
return ret
}
func (object *OdbObject) Len() (len uint64) {
return uint64(C.git_odb_object_size(object.ptr))
ret := uint64(C.git_odb_object_size(object.ptr))
runtime.KeepAlive(object)
return ret
}
func (object *OdbObject) Type() ObjectType {
ret := ObjectType(C.git_odb_object_type(object.ptr))
runtime.KeepAlive(object)
return ret
}
// Data returns a slice pointing to the unmanaged object memory. You must make
// sure the object is referenced for at least as long as the slice is used.
func (object *OdbObject) Data() (data []byte) {
var c_blob unsafe.Pointer = C.git_odb_object_data(object.ptr)
var blob []byte
@ -102,3 +383,132 @@ func (object *OdbObject) Data() (data []byte) {
return blob
}
type OdbReadStream struct {
doNotCompare
ptr *C.git_odb_stream
Size uint64
Type ObjectType
}
// Read reads from the stream
func (stream *OdbReadStream) Read(data []byte) (int, error) {
header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
ptr := (*C.char)(unsafe.Pointer(header.Data))
size := C.size_t(header.Cap)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_stream_read(stream.ptr, ptr, size)
runtime.KeepAlive(stream)
if ret < 0 {
return 0, MakeGitError(ret)
}
if ret == 0 {
return 0, io.EOF
}
header.Len = int(ret)
return len(data), nil
}
// Close is a dummy function in order to implement the Closer and
// ReadCloser interfaces
func (stream *OdbReadStream) Close() error {
return nil
}
func (stream *OdbReadStream) Free() {
runtime.SetFinalizer(stream, nil)
C.git_odb_stream_free(stream.ptr)
}
type OdbWriteStream struct {
doNotCompare
ptr *C.git_odb_stream
Id Oid
}
// Write writes to the stream
func (stream *OdbWriteStream) Write(data []byte) (int, error) {
header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
ptr := (*C.char)(unsafe.Pointer(header.Data))
size := C.size_t(header.Len)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_stream_write(stream.ptr, ptr, size)
runtime.KeepAlive(stream)
if ret < 0 {
return 0, MakeGitError(ret)
}
return len(data), nil
}
// Close signals that all the data has been written and stores the
// resulting object id in the stream's Id field.
func (stream *OdbWriteStream) Close() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_odb_stream_finalize_write(stream.Id.toC(), stream.ptr)
runtime.KeepAlive(stream)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (stream *OdbWriteStream) Free() {
runtime.SetFinalizer(stream, nil)
C.git_odb_stream_free(stream.ptr)
}
// OdbWritepack is a stream to write a packfile to the ODB.
type OdbWritepack struct {
doNotCompare
ptr *C.git_odb_writepack
stats C.git_transfer_progress
ccallbacks C.git_remote_callbacks
}
func (writepack *OdbWritepack) Write(data []byte) (int, error) {
header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
ptr := unsafe.Pointer(header.Data)
size := C.size_t(header.Len)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C._go_git_odb_writepack_append(writepack.ptr, ptr, size, &writepack.stats)
runtime.KeepAlive(writepack)
if ret < 0 {
return 0, MakeGitError(ret)
}
return len(data), nil
}
func (writepack *OdbWritepack) Commit() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C._go_git_odb_writepack_commit(writepack.ptr, &writepack.stats)
runtime.KeepAlive(writepack)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (writepack *OdbWritepack) Free() {
untrackCallbacksPayload(&writepack.ccallbacks)
runtime.SetFinalizer(writepack, nil)
C._go_git_odb_writepack_free(writepack.ptr)
}

235
odb_test.go Normal file
View File

@ -0,0 +1,235 @@
package git
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"testing"
)
func TestOdbRead(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, err := repo.Odb()
if err != nil {
t.Fatalf("Odb: %v", err)
}
data := []byte("hello")
id, err := odb.Write(data, ObjectBlob)
if err != nil {
t.Fatalf("odb.Write: %v", err)
}
sz, typ, err := odb.ReadHeader(id)
if err != nil {
t.Fatalf("ReadHeader: %v", err)
}
if sz != uint64(len(data)) {
t.Errorf("ReadHeader got size %d, want %d", sz, len(data))
}
if typ != ObjectBlob {
t.Errorf("ReadHeader got object type %s", typ)
}
obj, err := odb.Read(id)
if err != nil {
t.Fatalf("Read: %v", err)
}
if !bytes.Equal(obj.Data(), data) {
t.Errorf("Read got wrong data")
}
if sz := obj.Len(); sz != uint64(len(data)) {
t.Errorf("Read got size %d, want %d", sz, len(data))
}
if typ := obj.Type(); typ != ObjectBlob {
t.Errorf("Read got object type %s", typ)
}
}
func TestOdbStream(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
str := "hello, world!"
writeStream, err := odb.NewWriteStream(int64(len(str)), ObjectBlob)
checkFatal(t, err)
n, err := io.WriteString(writeStream, str)
checkFatal(t, err)
if n != len(str) {
t.Fatalf("Bad write length %v != %v", n, len(str))
}
err = writeStream.Close()
checkFatal(t, err)
expectedId, err := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
checkFatal(t, err)
if writeStream.Id.Cmp(expectedId) != 0 {
t.Fatal("Wrong data written")
}
readStream, err := odb.NewReadStream(&writeStream.Id)
checkFatal(t, err)
data, err := ioutil.ReadAll(readStream)
if str != string(data) {
t.Fatalf("Wrong data read %v != %v", str, string(data))
}
}
func TestOdbHash(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
str := `tree 115fcae49287c82eb55bb275cbbd4556fbed72b7
parent 66e1c476199ebcd3e304659992233132c5a52c6c
author John Doe <john@doe.com> 1390682018 +0000
committer John Doe <john@doe.com> 1390682018 +0000
Initial commit.`
for _, data := range [][]byte{[]byte(str), doublePointerBytes()} {
oid, err := odb.Hash(data, ObjectCommit)
checkFatal(t, err)
coid, err := odb.Write(data, ObjectCommit)
checkFatal(t, err)
if oid.Cmp(coid) != 0 {
t.Fatal("Hash and write Oids are different")
}
}
}
func TestOdbForeach(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
expect := 3
count := 0
err = odb.ForEach(func(id *Oid) error {
count++
return nil
})
checkFatal(t, err)
if count != expect {
t.Fatalf("Expected %v objects, got %v", expect, count)
}
expect = 1
count = 0
to_return := errors.New("not really an error")
err = odb.ForEach(func(id *Oid) error {
count++
return to_return
})
if err != to_return {
t.Fatalf("Odb.ForEach() did not return the expected error, got %v", err)
}
}
func TestOdbWritepack(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
var finalStats TransferProgress
writepack, err := odb.NewWritePack(func(stats TransferProgress) error {
finalStats = stats
return nil
})
checkFatal(t, err)
defer writepack.Free()
_, err = writepack.Write(outOfOrderPack)
checkFatal(t, err)
err = writepack.Commit()
checkFatal(t, err)
if finalStats.TotalObjects != 3 {
t.Errorf("mismatched transferred objects, expected 3, got %v", finalStats.TotalObjects)
}
if finalStats.ReceivedObjects != 3 {
t.Errorf("mismatched received objects, expected 3, got %v", finalStats.ReceivedObjects)
}
if finalStats.IndexedObjects != 3 {
t.Errorf("mismatched indexed objects, expected 3, got %v", finalStats.IndexedObjects)
}
}
func TestOdbBackendLoose(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, _ = seedTestRepo(t, repo)
odb, err := repo.Odb()
checkFatal(t, err)
looseObjectsDir, err := ioutil.TempDir("", fmt.Sprintf("loose_objects_%s", path.Base(repo.Path())))
checkFatal(t, err)
defer os.RemoveAll(looseObjectsDir)
looseObjectsBackend, err := NewOdbBackendLoose(looseObjectsDir, -1, false, 0, 0)
checkFatal(t, err)
if err := odb.AddBackend(looseObjectsBackend, 999); err != nil {
looseObjectsBackend.Free()
checkFatal(t, err)
}
str := "hello, world!"
writeStream, err := odb.NewWriteStream(int64(len(str)), ObjectBlob)
checkFatal(t, err)
n, err := io.WriteString(writeStream, str)
checkFatal(t, err)
if n != len(str) {
t.Fatalf("Bad write length %v != %v", n, len(str))
}
err = writeStream.Close()
checkFatal(t, err)
expectedId, err := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
checkFatal(t, err)
if !writeStream.Id.Equal(expectedId) {
t.Fatalf("writeStream.id = %v; want %v", writeStream.Id, expectedId)
}
_, err = os.Stat(path.Join(looseObjectsDir, expectedId.String()[:2], expectedId.String()[2:]))
checkFatal(t, err)
}

View File

@ -1,9 +1,7 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
#include <git2/pack.h>
#include <stdlib.h>
@ -12,22 +10,33 @@ extern int _go_git_packbuilder_foreach(git_packbuilder *pb, void *payload);
import "C"
import (
"io"
"os"
"runtime"
"unsafe"
)
type Packbuilder struct {
doNotCompare
ptr *C.git_packbuilder
r *Repository
}
func (repo *Repository) NewPackbuilder() (*Packbuilder, error) {
builder := &Packbuilder{}
ret := C.git_packbuilder_new(&builder.ptr, repo.ptr)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_packbuilder
ret := C.git_packbuilder_new(&ptr, repo.ptr)
if ret != 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(builder, (*Packbuilder).Free)
return builder, nil
return newPackbuilderFromC(ptr, repo), nil
}
func newPackbuilderFromC(ptr *C.git_packbuilder, r *Repository) *Packbuilder {
pb := &Packbuilder{ptr: ptr, r: r}
runtime.SetFinalizer(pb, (*Packbuilder).Free)
return pb
}
func (pb *Packbuilder) Free() {
@ -38,93 +47,139 @@ func (pb *Packbuilder) Free() {
func (pb *Packbuilder) Insert(id *Oid, name string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_insert(pb.ptr, id.toC(), cname)
runtime.KeepAlive(pb)
runtime.KeepAlive(id)
if ret != 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) InsertCommit(id *Oid) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_insert_commit(pb.ptr, id.toC())
runtime.KeepAlive(pb)
runtime.KeepAlive(id)
if ret != 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) InsertTree(id *Oid) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_insert_tree(pb.ptr, id.toC())
runtime.KeepAlive(pb)
runtime.KeepAlive(id)
if ret != 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) InsertWalk(walk *RevWalk) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_insert_walk(pb.ptr, walk.ptr)
runtime.KeepAlive(pb)
runtime.KeepAlive(walk)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) ObjectCount() uint32 {
return uint32(C.git_packbuilder_object_count(pb.ptr))
ret := uint32(C.git_packbuilder_object_count(pb.ptr))
runtime.KeepAlive(pb)
return ret
}
func (pb *Packbuilder) WriteToFile(name string) error {
func (pb *Packbuilder) WriteToFile(name string, mode os.FileMode) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ret := C.git_packbuilder_write(pb.ptr, cname, nil, nil)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_packbuilder_write(pb.ptr, cname, C.uint(mode.Perm()), nil, nil)
runtime.KeepAlive(pb)
if ret != 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (pb *Packbuilder) Write(w io.Writer) error {
ch, stop := pb.ForEach()
for slice := range ch {
return pb.ForEach(func(slice []byte) error {
_, err := w.Write(slice)
if err != nil {
close(stop)
return err
}
}
return nil
return err
})
}
func (pb *Packbuilder) Written() uint32 {
return uint32(C.git_packbuilder_written(pb.ptr))
ret := uint32(C.git_packbuilder_written(pb.ptr))
runtime.KeepAlive(pb)
return ret
}
type packbuilderCbData struct {
ch chan<- []byte
stop <-chan bool
type PackbuilderForeachCallback func([]byte) error
type packbuilderCallbackData struct {
callback PackbuilderForeachCallback
errorTarget *error
}
//export packbuilderForEachCb
func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, payload unsafe.Pointer) int {
data := (*packbuilderCbData)(payload)
ch := data.ch
stop := data.stop
slice := C.GoBytes(buf, C.int(size))
select {
case <- stop:
return -1
case ch <- slice:
//export packbuilderForEachCallback
func packbuilderForEachCallback(buf unsafe.Pointer, size C.size_t, handle unsafe.Pointer) C.int {
payload := pointerHandles.Get(handle)
data, ok := payload.(*packbuilderCallbackData)
if !ok {
panic("could not get packbuilder CB data")
}
return 0
slice := C.GoBytes(buf, C.int(size))
err := data.callback(slice)
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
func (pb *Packbuilder) forEachWrap(data *packbuilderCbData) {
C._go_git_packbuilder_foreach(pb.ptr, unsafe.Pointer(data))
close(data.ch)
}
// ForEach repeatedly calls the callback with new packfile data until
// there is no more data or the callback returns an error
func (pb *Packbuilder) ForEach(callback PackbuilderForeachCallback) error {
var err error
data := packbuilderCallbackData{
callback: callback,
errorTarget: &err,
}
handle := pointerHandles.Track(&data)
defer pointerHandles.Untrack(handle)
// Foreach sends the packfile as slices through the "data" channel. If
// you want to stop the pack-building process (e.g. there's an error
// writing to the output), close or write a value into the "stop"
// channel.
func (pb *Packbuilder) ForEach() (<-chan []byte, chan<- bool) {
ch := make(chan []byte)
stop := make(chan bool)
data := packbuilderCbData{ch, stop}
go pb.forEachWrap(&data)
return ch, stop
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C._go_git_packbuilder_foreach(pb.ptr, handle)
runtime.KeepAlive(pb)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}

99
patch.go Normal file
View File

@ -0,0 +1,99 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
type Patch struct {
doNotCompare
ptr *C.git_patch
}
func newPatchFromC(ptr *C.git_patch) *Patch {
if ptr == nil {
return nil
}
patch := &Patch{
ptr: ptr,
}
runtime.SetFinalizer(patch, (*Patch).Free)
return patch
}
func (patch *Patch) Free() error {
if patch.ptr == nil {
return ErrInvalid
}
runtime.SetFinalizer(patch, nil)
C.git_patch_free(patch.ptr)
patch.ptr = nil
return nil
}
func (patch *Patch) String() (string, error) {
if patch.ptr == nil {
return "", ErrInvalid
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var buf C.git_buf
ecode := C.git_patch_to_buf(&buf, patch.ptr)
runtime.KeepAlive(patch)
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_dispose(&buf)
return C.GoString(buf.ptr), nil
}
func toPointer(data []byte) (ptr unsafe.Pointer) {
if len(data) > 0 {
ptr = unsafe.Pointer(&data[0])
} else {
ptr = unsafe.Pointer(nil)
}
return
}
func (v *Repository) PatchFromBuffers(oldPath, newPath string, oldBuf, newBuf []byte, opts *DiffOptions) (*Patch, error) {
var patchPtr *C.git_patch
oldPtr := toPointer(oldBuf)
newPtr := toPointer(newBuf)
cOldPath := C.CString(oldPath)
defer C.free(unsafe.Pointer(cOldPath))
cNewPath := C.CString(newPath)
defer C.free(unsafe.Pointer(cNewPath))
var err error
copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err)
defer freeDiffOptions(copts)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_patch_from_buffers(&patchPtr, oldPtr, C.size_t(len(oldBuf)), cOldPath, newPtr, C.size_t(len(newBuf)), cNewPath, copts)
runtime.KeepAlive(oldBuf)
runtime.KeepAlive(newBuf)
if ret == C.int(ErrorCodeUser) && err != nil {
return nil, err
}
if ret < 0 {
return nil, MakeGitError(ret)
}
return newPatchFromC(patchPtr), nil
}

38
patch_test.go Normal file
View File

@ -0,0 +1,38 @@
package git
import (
"strings"
"testing"
)
func TestPatch(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, originalTreeId := seedTestRepo(t, repo)
originalTree, err := repo.LookupTree(originalTreeId)
checkFatal(t, err)
_, newTreeId := updateReadme(t, repo, "file changed\n")
newTree, err := repo.LookupTree(newTreeId)
checkFatal(t, err)
opts := &DiffOptions{
OldPrefix: "a",
NewPrefix: "b",
}
diff, err := repo.DiffTreeToTree(originalTree, newTree, opts)
checkFatal(t, err)
patch, err := diff.Patch(0)
checkFatal(t, err)
patchStr, err := patch.String()
checkFatal(t, err)
if strings.Index(patchStr, "diff --git a/README b/README\nindex 257cc56..820734a 100644\n--- a/README\n+++ b/README\n@@ -1 +1 @@\n-foo\n+file changed") == -1 {
t.Fatalf("patch was bad")
}
}

31
push_test.go Normal file
View File

@ -0,0 +1,31 @@
package git
import (
"testing"
)
func TestRemotePush(t *testing.T) {
t.Parallel()
repo := createBareTestRepo(t)
defer cleanupTestRepo(t, repo)
localRepo := createTestRepo(t)
defer cleanupTestRepo(t, localRepo)
remote, err := localRepo.Remotes.Create("test_push", repo.Path())
checkFatal(t, err)
defer remote.Free()
seedTestRepo(t, localRepo)
err = remote.Push([]string{"refs/heads/master"}, nil)
checkFatal(t, err)
ref, err := localRepo.References.Lookup("refs/remotes/test_push/master")
checkFatal(t, err)
defer ref.Free()
ref, err = repo.References.Lookup("refs/heads/master")
checkFatal(t, err)
defer ref.Free()
}

485
rebase.go Normal file
View File

@ -0,0 +1,485 @@
package git
/*
#include <git2.h>
extern void _go_git_populate_rebase_callbacks(git_rebase_options *opts);
*/
import "C"
import (
"errors"
"fmt"
"reflect"
"runtime"
"unsafe"
)
// RebaseOperationType is the type of rebase operation
type RebaseOperationType uint
const (
// RebaseOperationPick The given commit is to be cherry-picked. The client should commit the changes and continue if there are no conflicts.
RebaseOperationPick RebaseOperationType = C.GIT_REBASE_OPERATION_PICK
// RebaseOperationReword The given commit is to be cherry-picked, but the client should prompt the user to provide an updated commit message.
RebaseOperationReword RebaseOperationType = C.GIT_REBASE_OPERATION_REWORD
// RebaseOperationEdit The given commit is to be cherry-picked, but the client should stop to allow the user to edit the changes before committing them.
RebaseOperationEdit RebaseOperationType = C.GIT_REBASE_OPERATION_EDIT
// RebaseOperationSquash The given commit is to be squashed into the previous commit. The commit message will be merged with the previous message.
RebaseOperationSquash RebaseOperationType = C.GIT_REBASE_OPERATION_SQUASH
// RebaseOperationFixup No commit will be cherry-picked. The client should run the given command and (if successful) continue.
RebaseOperationFixup RebaseOperationType = C.GIT_REBASE_OPERATION_FIXUP
// RebaseOperationExec No commit will be cherry-picked. The client should run the given command and (if successful) continue.
RebaseOperationExec RebaseOperationType = C.GIT_REBASE_OPERATION_EXEC
)
func (t RebaseOperationType) String() string {
switch t {
case RebaseOperationPick:
return "pick"
case RebaseOperationReword:
return "reword"
case RebaseOperationEdit:
return "edit"
case RebaseOperationSquash:
return "squash"
case RebaseOperationFixup:
return "fixup"
case RebaseOperationExec:
return "exec"
}
return fmt.Sprintf("RebaseOperationType(%d)", t)
}
// Special value indicating that there is no currently active operation
var RebaseNoOperation uint = ^uint(0)
// Error returned if there is no current rebase operation
var ErrRebaseNoOperation = errors.New("no current rebase operation")
// RebaseOperation describes a single instruction/operation to be performed during the rebase.
type RebaseOperation struct {
Type RebaseOperationType
Id *Oid
Exec string
}
func newRebaseOperationFromC(c *C.git_rebase_operation) *RebaseOperation {
operation := &RebaseOperation{}
operation.Type = RebaseOperationType(c._type)
operation.Id = newOidFromC(&c.id)
operation.Exec = C.GoString(c.exec)
return operation
}
//export commitCreateCallback
func commitCreateCallback(
errorMessage **C.char,
_out *C.git_oid,
_author, _committer *C.git_signature,
_message_encoding, _message *C.char,
_tree *C.git_tree,
_parent_count C.size_t,
_parents **C.git_commit,
handle unsafe.Pointer,
) C.int {
data, ok := pointerHandles.Get(handle).(*rebaseOptionsData)
if !ok {
panic("invalid sign payload")
}
if data.options.CommitCreateCallback == nil && data.options.CommitSigningCallback == nil {
return C.int(ErrorCodePassthrough)
}
messageEncoding := MessageEncodingUTF8
if _message_encoding != nil {
messageEncoding = MessageEncoding(C.GoString(_message_encoding))
}
tree := &Tree{
Object: Object{
ptr: (*C.git_object)(_tree),
repo: data.repo,
},
cast_ptr: _tree,
}
var goParents []*C.git_commit
if _parent_count > 0 {
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(_parents)),
Len: int(_parent_count),
Cap: int(_parent_count),
}
goParents = *(*[]*C.git_commit)(unsafe.Pointer(&hdr))
}
parents := make([]*Commit, int(_parent_count))
for i, p := range goParents {
parents[i] = &Commit{
Object: Object{
ptr: (*C.git_object)(p),
repo: data.repo,
},
cast_ptr: p,
}
}
if data.options.CommitCreateCallback != nil {
oid, err := data.options.CommitCreateCallback(
newSignatureFromC(_author),
newSignatureFromC(_committer),
messageEncoding,
C.GoString(_message),
tree,
parents...,
)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
if oid == nil {
return C.int(ErrorCodePassthrough)
}
*_out = *oid.toC()
} else if data.options.CommitSigningCallback != nil {
commitContent, err := data.repo.CreateCommitBuffer(
newSignatureFromC(_author),
newSignatureFromC(_committer),
messageEncoding,
C.GoString(_message),
tree,
parents...,
)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
signature, signatureField, err := data.options.CommitSigningCallback(string(commitContent))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
oid, err := data.repo.CreateCommitWithSignature(string(commitContent), signature, signatureField)
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
*_out = *oid.toC()
}
return C.int(ErrorCodeOK)
}
// RebaseOptions are used to tell the rebase machinery how to operate.
type RebaseOptions struct {
Quiet int
InMemory int
RewriteNotesRef string
MergeOptions MergeOptions
CheckoutOptions CheckoutOptions
// CommitCreateCallback is an optional callback that allows users to override
// commit creation when rebasing. If specified, users can create
// their own commit and provide the commit ID, which may be useful for
// signing commits or otherwise customizing the commit creation. If this
// callback returns a nil Oid, then the rebase will continue to create the
// commit.
CommitCreateCallback CommitCreateCallback
// Deprecated: CommitSigningCallback is an optional callback that will be
// called with the commit content, allowing a signature to be added to the
// rebase commit. This field is only used when rebasing. This callback is
// not invoked if a CommitCreateCallback is specified. CommitCreateCallback
// should be used instead of this.
CommitSigningCallback CommitSigningCallback
}
type rebaseOptionsData struct {
options *RebaseOptions
repo *Repository
errorTarget *error
}
// DefaultRebaseOptions returns a RebaseOptions with default values.
func DefaultRebaseOptions() (RebaseOptions, error) {
opts := C.git_rebase_options{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_rebase_options_init(&opts, C.GIT_REBASE_OPTIONS_VERSION)
if ecode < 0 {
return RebaseOptions{}, MakeGitError(ecode)
}
return rebaseOptionsFromC(&opts), nil
}
func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions {
return RebaseOptions{
Quiet: int(opts.quiet),
InMemory: int(opts.inmemory),
RewriteNotesRef: C.GoString(opts.rewrite_notes_ref),
MergeOptions: mergeOptionsFromC(&opts.merge_options),
CheckoutOptions: checkoutOptionsFromC(&opts.checkout_options),
}
}
func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, repo *Repository, errorTarget *error) *C.git_rebase_options {
C.git_rebase_options_init(copts, C.GIT_REBASE_OPTIONS_VERSION)
if opts == nil {
return nil
}
copts.quiet = C.int(opts.Quiet)
copts.inmemory = C.int(opts.InMemory)
copts.rewrite_notes_ref = mapEmptyStringToNull(opts.RewriteNotesRef)
populateMergeOptions(&copts.merge_options, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget)
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
}
func freeRebaseOptions(copts *C.git_rebase_options) {
if copts == nil {
return
}
C.free(unsafe.Pointer(copts.rewrite_notes_ref))
freeMergeOptions(&copts.merge_options)
freeCheckoutOptions(&copts.checkout_options)
if copts.payload != nil {
pointerHandles.Untrack(copts.payload)
}
}
func mapEmptyStringToNull(ref string) *C.char {
if ref == "" {
return nil
}
return C.CString(ref)
}
// Rebase is the struct representing a Rebase object.
type Rebase struct {
doNotCompare
ptr *C.git_rebase
r *Repository
options *C.git_rebase_options
}
// InitRebase initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch.
func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedCommit, onto *AnnotatedCommit, opts *RebaseOptions) (*Rebase, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if branch == nil {
branch = &AnnotatedCommit{ptr: nil}
}
if upstream == nil {
upstream = &AnnotatedCommit{ptr: nil}
}
if onto == nil {
onto = &AnnotatedCommit{ptr: nil}
}
var ptr *C.git_rebase
var err error
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, r, &err)
ret := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, cOpts)
runtime.KeepAlive(branch)
runtime.KeepAlive(upstream)
runtime.KeepAlive(onto)
if ret == C.int(ErrorCodeUser) && err != nil {
freeRebaseOptions(cOpts)
return nil, err
}
if ret < 0 {
freeRebaseOptions(cOpts)
return nil, MakeGitError(ret)
}
return newRebaseFromC(ptr, r, cOpts), nil
}
// OpenRebase opens an existing rebase that was previously started by either an invocation of InitRebase or by another client.
func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_rebase
var err error
cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, r, &err)
ret := C.git_rebase_open(&ptr, r.ptr, cOpts)
runtime.KeepAlive(r)
if ret == C.int(ErrorCodeUser) && err != nil {
freeRebaseOptions(cOpts)
return nil, err
}
if ret < 0 {
freeRebaseOptions(cOpts)
return nil, MakeGitError(ret)
}
return newRebaseFromC(ptr, r, cOpts), nil
}
// OperationAt gets the rebase operation specified by the given index.
func (rebase *Rebase) OperationAt(index uint) *RebaseOperation {
operation := C.git_rebase_operation_byindex(rebase.ptr, C.size_t(index))
return newRebaseOperationFromC(operation)
}
// CurrentOperationIndex gets the index of the rebase operation that is
// currently being applied. There is also an error returned for API
// compatibility.
func (rebase *Rebase) CurrentOperationIndex() (uint, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var err error
operationIndex := uint(C.git_rebase_operation_current(rebase.ptr))
runtime.KeepAlive(rebase)
if operationIndex == RebaseNoOperation {
err = ErrRebaseNoOperation
}
return uint(operationIndex), err
}
// OperationCount gets the count of rebase operations that are to be applied.
func (rebase *Rebase) OperationCount() uint {
ret := uint(C.git_rebase_operation_entrycount(rebase.ptr))
runtime.KeepAlive(rebase)
return ret
}
// Next performs the next rebase operation and returns the information about it.
// If the operation is one that applies a patch (which is any operation except RebaseOperationExec)
// then the patch will be applied and the index and working directory will be updated with the changes.
// If there are conflicts, you will need to address those before committing the changes.
func (rebase *Rebase) Next() (*RebaseOperation, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_rebase_operation
err := C.git_rebase_next(&ptr, rebase.ptr)
runtime.KeepAlive(rebase)
if err < 0 {
return nil, MakeGitError(err)
}
return newRebaseOperationFromC(ptr), nil
}
// InmemoryIndex gets the index produced by the last operation, which is the
// result of `Next()` and which will be committed by the next invocation of
// `Commit()`. This is useful for resolving conflicts in an in-memory rebase
// before committing them.
//
// This is only applicable for in-memory rebases; for rebases within a working
// directory, the changes were applied to the repository's index.
func (rebase *Rebase) InmemoryIndex() (*Index, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_index
err := C.git_rebase_inmemory_index(&ptr, rebase.ptr)
runtime.KeepAlive(rebase)
if err < 0 {
return nil, MakeGitError(err)
}
return newIndexFromC(ptr, rebase.r), nil
}
// Commit commits the current patch.
// You must have resolved any conflicts that were introduced during the patch application from the Next() invocation.
func (rebase *Rebase) Commit(ID *Oid, author, committer *Signature, message string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
authorSig, err := author.toC()
if err != nil {
return err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return err
}
defer C.git_signature_free(committerSig)
cmsg := C.CString(message)
defer C.free(unsafe.Pointer(cmsg))
cerr := C.git_rebase_commit(ID.toC(), rebase.ptr, authorSig, committerSig, nil, cmsg)
runtime.KeepAlive(ID)
runtime.KeepAlive(rebase)
if cerr < 0 {
return MakeGitError(cerr)
}
return nil
}
// Finish finishes a rebase that is currently in progress once all patches have been applied.
func (rebase *Rebase) Finish() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_rebase_finish(rebase.ptr, nil)
runtime.KeepAlive(rebase)
if err < 0 {
return MakeGitError(err)
}
return nil
}
// Abort aborts a rebase that is currently in progress, resetting the repository and working directory to their state before rebase began.
func (rebase *Rebase) Abort() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_rebase_abort(rebase.ptr)
runtime.KeepAlive(rebase)
if err < 0 {
return MakeGitError(err)
}
return nil
}
// Free frees the Rebase object.
func (r *Rebase) Free() {
runtime.SetFinalizer(r, nil)
C.git_rebase_free(r.ptr)
freeRebaseOptions(r.options)
}
func newRebaseFromC(ptr *C.git_rebase, repo *Repository, opts *C.git_rebase_options) *Rebase {
rebase := &Rebase{ptr: ptr, r: repo, options: opts}
runtime.SetFinalizer(rebase, (*Rebase).Free)
return rebase
}

602
rebase_test.go Normal file
View File

@ -0,0 +1,602 @@
package git
import (
"bytes"
"errors"
"strconv"
"strings"
"testing"
"time"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
)
// Tests
func TestRebaseInMemoryWithConflict(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
// Create two branches with common history, where both modify "common-file"
// in a conflicting way.
_, err := commitSomething(repo, "common-file", "a\nb\nc\n", commitOptions{})
checkFatal(t, err)
checkFatal(t, createBranch(repo, "branch-a"))
checkFatal(t, createBranch(repo, "branch-b"))
checkFatal(t, repo.SetHead("refs/heads/branch-a"))
_, err = commitSomething(repo, "common-file", "1\nb\nc\n", commitOptions{})
checkFatal(t, err)
checkFatal(t, repo.SetHead("refs/heads/branch-b"))
_, err = commitSomething(repo, "common-file", "x\nb\nc\n", commitOptions{})
checkFatal(t, err)
branchA, err := repo.LookupBranch("branch-a", BranchLocal)
checkFatal(t, err)
onto, err := repo.AnnotatedCommitFromRef(branchA.Reference)
checkFatal(t, err)
// We then rebase "branch-b" onto "branch-a" in-memory, which should result
// in a conflict.
rebase, err := repo.InitRebase(nil, nil, onto, &RebaseOptions{InMemory: 1})
checkFatal(t, err)
_, err = rebase.Next()
checkFatal(t, err)
index, err := rebase.InmemoryIndex()
checkFatal(t, err)
// We simply resolve the conflict and commit the rebase.
if !index.HasConflicts() {
t.Fatal("expected index to have conflicts")
}
conflict, err := index.Conflict("common-file")
checkFatal(t, err)
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
checkFatal(t, err)
resolvedEntry := *conflict.Our
resolvedEntry.Id = resolvedBlobID
checkFatal(t, index.Add(&resolvedEntry))
checkFatal(t, index.RemoveConflict("common-file"))
var commitID Oid
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
checkFatal(t, rebase.Finish())
// And then assert that we can look up the new merge commit, and that the
// "common-file" has the expected contents.
commit, err := repo.LookupCommit(&commitID)
checkFatal(t, err)
if commit.Message() != "rebased message" {
t.Fatalf("unexpected commit message %q", commit.Message())
}
tree, err := commit.Tree()
checkFatal(t, err)
blob, err := repo.LookupBlob(tree.EntryByName("common-file").Id)
checkFatal(t, err)
if string(blob.Contents()) != "resolved contents" {
t.Fatalf("unexpected resolved blob contents %q", string(blob.Contents()))
}
}
func TestRebaseAbort(t *testing.T) {
// TEST DATA
// Inputs
branchName := "emile"
masterCommit := "something"
emileCommits := []string{
"fou",
"barre",
}
// Outputs
expectedHistory := []string{
"Test rebase, Baby! " + emileCommits[1],
"Test rebase, Baby! " + emileCommits[0],
"This is a commit\n",
}
// TEST
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
// Setup a repo with 2 branches and a different tree
err := setupRepoForRebase(repo, masterCommit, branchName, commitOptions{})
checkFatal(t, err)
// Create several commits in emile
for _, commit := range emileCommits {
_, err = commitSomething(repo, commit, commit, commitOptions{})
checkFatal(t, err)
}
// Check history
actualHistory, err := commitMsgsList(repo)
checkFatal(t, err)
assertStringList(t, expectedHistory, actualHistory)
// Rebase onto master
rebase, err := performRebaseOnto(repo, "master", nil)
checkFatal(t, err)
defer rebase.Free()
// Abort rebase
rebase.Abort()
// Check history is still the same
actualHistory, err = commitMsgsList(repo)
checkFatal(t, err)
assertStringList(t, expectedHistory, actualHistory)
}
func TestRebaseNoConflicts(t *testing.T) {
// TEST DATA
// 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)
seedTestRepo(t, repo)
// Try to open existing rebase
oRebase, 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, commitOptions{})
checkFatal(t, err)
// Create several commits in emile
for _, commit := range emileCommits {
_, err = commitSomething(repo, commit, commit, commitOptions{})
checkFatal(t, err)
}
// Rebase onto master
rebase, err := performRebaseOnto(repo, "master", nil)
checkFatal(t, err)
defer rebase.Free()
// Open existing rebase
oRebase, err = repo.OpenRebase(nil)
checkFatal(t, err)
defer oRebase.Free()
if oRebase == nil {
t.Fatal("Expected to find an existing rebase in progress")
}
// Finish the rebase properly
err = rebase.Finish()
checkFatal(t, err)
// Check no more rebase is in progress
oRebase, err = repo.OpenRebase(nil)
if err == nil {
t.Fatal("Did not expect to find a rebase in progress")
}
// Check history is in correct order
actualHistory, err := commitMsgsList(repo)
checkFatal(t, err)
assertStringList(t, expectedHistory, actualHistory)
}
func TestRebaseGpgSigned(t *testing.T) {
// TEST DATA
entity, err := openpgp.NewEntity("Namey mcnameface", "test comment", "test@example.com", nil)
checkFatal(t, err)
rebaseOpts, err := DefaultRebaseOptions()
checkFatal(t, err)
signCommitContent := func(commitContent string) (string, string, error) {
cipherText := new(bytes.Buffer)
err := openpgp.ArmoredDetachSignText(cipherText, entity, strings.NewReader(commitContent), &packet.Config{})
if err != nil {
return "", "", errors.New("error signing payload")
}
return cipherText.String(), "", nil
}
rebaseOpts.CommitSigningCallback = signCommitContent
commitOpts := commitOptions{
CommitSigningCallback: signCommitContent,
}
// Inputs
branchName := "emile"
masterCommit := "something"
emileCommits := []string{
"fou",
"barre",
"ouich",
}
// Outputs
expectedHistory := []string{
"Test rebase, Baby! " + emileCommits[2],
"Test rebase, Baby! " + emileCommits[1],
"Test rebase, Baby! " + emileCommits[0],
"Test rebase, Baby! " + masterCommit,
"This is a commit\n",
}
// TEST
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepoOpt(t, repo, commitOpts)
// Try to open existing rebase
_, err = repo.OpenRebase(nil)
if err == nil {
t.Fatal("Did not expect to find a rebase in progress")
}
// Setup a repo with 2 branches and a different tree
err = setupRepoForRebase(repo, masterCommit, branchName, commitOpts)
checkFatal(t, err)
// Create several commits in emile
for _, commit := range emileCommits {
_, err = commitSomething(repo, commit, commit, commitOpts)
checkFatal(t, err)
}
// Rebase onto master
rebase, err := performRebaseOnto(repo, "master", &rebaseOpts)
checkFatal(t, err)
defer rebase.Free()
// Finish the rebase properly
err = rebase.Finish()
checkFatal(t, err)
// Check history is in correct order
actualHistory, err := commitMsgsList(repo)
checkFatal(t, err)
assertStringList(t, expectedHistory, actualHistory)
checkAllCommitsSigned(t, entity, repo)
}
func checkAllCommitsSigned(t *testing.T, entity *openpgp.Entity, repo *Repository) {
head, err := headCommit(repo)
checkFatal(t, err)
defer head.Free()
parent := head
err = checkCommitSigned(t, entity, parent)
checkFatal(t, err)
for parent.ParentCount() != 0 {
parent = parent.Parent(0)
defer parent.Free()
err = checkCommitSigned(t, entity, parent)
checkFatal(t, err)
}
}
func checkCommitSigned(t *testing.T, entity *openpgp.Entity, commit *Commit) error {
t.Helper()
signature, signedData, err := commit.ExtractSignature()
if err != nil {
t.Logf("No signature on commit\n%s", commit.ContentToSign())
return err
}
_, err = openpgp.CheckArmoredDetachedSignature(openpgp.EntityList{entity}, strings.NewReader(signedData), bytes.NewBufferString(signature), nil)
if err != nil {
t.Logf("Commit is not signed correctly\n%s", commit.ContentToSign())
return err
}
return nil
}
// Utils
func setupRepoForRebase(repo *Repository, masterCommit, branchName string, commitOpts commitOptions) error {
// Create a new branch from master
err := createBranch(repo, branchName)
if err != nil {
return err
}
// Create a commit in master
_, err = commitSomething(repo, masterCommit, masterCommit, commitOpts)
if err != nil {
return err
}
// Switch to emile
err = repo.SetHead("refs/heads/" + branchName)
if err != nil {
return err
}
// Check master commit is not in emile branch
if entryExists(repo, masterCommit) {
return errors.New(masterCommit + " entry should not exist in " + branchName + " branch.")
}
return nil
}
func performRebaseOnto(repo *Repository, branch string, rebaseOpts *RebaseOptions) (*Rebase, error) {
master, err := repo.LookupBranch(branch, BranchLocal)
if err != nil {
return nil, err
}
defer master.Free()
onto, err := repo.AnnotatedCommitFromRef(master.Reference)
if err != nil {
return nil, err
}
defer onto.Free()
// Init rebase
rebase, err := repo.InitRebase(nil, nil, onto, rebaseOpts)
if err != nil {
return nil, err
}
// Check no operation has been started yet
rebaseOperationIndex, err := rebase.CurrentOperationIndex()
if rebaseOperationIndex != RebaseNoOperation && err != ErrRebaseNoOperation {
return nil, errors.New("No operation should have been started yet")
}
// Iterate in rebase operations regarding operation count
opCount := int(rebase.OperationCount())
for op := 0; op < opCount; op++ {
operation, err := rebase.Next()
if err != nil {
return nil, err
}
// Check operation index is correct
rebaseOperationIndex, err = rebase.CurrentOperationIndex()
if int(rebaseOperationIndex) != op {
return nil, errors.New("Bad operation index")
}
if !operationsAreEqual(rebase.OperationAt(uint(op)), operation) {
return nil, errors.New("Rebase operations should be equal")
}
// Get current rebase operation created commit
commit, err := repo.LookupCommit(operation.Id)
if err != nil {
return nil, err
}
defer commit.Free()
// Apply commit
err = rebase.Commit(operation.Id, signature(), signature(), commit.Message())
if err != nil {
return nil, err
}
}
return rebase, nil
}
func operationsAreEqual(l, r *RebaseOperation) bool {
return l.Exec == r.Exec && l.Type == r.Type && l.Id.String() == r.Id.String()
}
func createBranch(repo *Repository, branch string) error {
commit, err := headCommit(repo)
if err != nil {
return err
}
defer commit.Free()
_, err = repo.CreateBranch(branch, commit, false)
if err != nil {
return err
}
return nil
}
func signature() *Signature {
return &Signature{
Name: "Emile",
Email: "emile@emile.com",
When: time.Now(),
}
}
func headCommit(repo *Repository) (*Commit, error) {
head, err := repo.Head()
if err != nil {
return nil, err
}
defer head.Free()
commit, err := repo.LookupCommit(head.Target())
if err != nil {
return nil, err
}
return commit, nil
}
func headTree(repo *Repository) (*Tree, error) {
headCommit, err := headCommit(repo)
if err != nil {
return nil, err
}
defer headCommit.Free()
tree, err := headCommit.Tree()
if err != nil {
return nil, err
}
return tree, nil
}
func commitSomething(repo *Repository, something, content string, commitOpts commitOptions) (*Oid, error) {
headCommit, err := headCommit(repo)
if err != nil {
return nil, err
}
defer headCommit.Free()
index, err := NewIndex()
if err != nil {
return nil, err
}
defer index.Free()
blobOID, err := repo.CreateBlobFromBuffer([]byte(content))
if err != nil {
return nil, err
}
entry := &IndexEntry{
Mode: FilemodeBlob,
Id: blobOID,
Path: something,
}
if err := index.Add(entry); err != nil {
return nil, err
}
newTreeOID, err := index.WriteTreeTo(repo)
if err != nil {
return nil, err
}
newTree, err := repo.LookupTree(newTreeOID)
if err != nil {
return nil, err
}
defer newTree.Free()
commit, err := repo.CreateCommit("HEAD", signature(), signature(), "Test rebase, Baby! "+something, newTree, headCommit)
if err != nil {
return nil, err
}
if commitOpts.CommitSigningCallback != nil {
commit, err := repo.LookupCommit(commit)
if err != nil {
return nil, err
}
oid, err := commit.WithSignatureUsing(commitOpts.CommitSigningCallback)
if err != nil {
return nil, err
}
newCommit, err := repo.LookupCommit(oid)
if err != nil {
return nil, err
}
head, err := repo.Head()
if err != nil {
return nil, err
}
_, err = repo.References.Create(
head.Name(),
newCommit.Id(),
true,
"repoint to signed commit",
)
if err != nil {
return nil, err
}
}
checkoutOpts := &CheckoutOptions{
Strategy: CheckoutRemoveUntracked | CheckoutForce,
}
err = repo.CheckoutIndex(index, checkoutOpts)
if err != nil {
return nil, err
}
return commit, nil
}
func entryExists(repo *Repository, file string) bool {
headTree, err := headTree(repo)
if err != nil {
return false
}
defer headTree.Free()
_, err = headTree.EntryByPath(file)
return err == nil
}
func commitMsgsList(repo *Repository) ([]string, error) {
head, err := headCommit(repo)
if err != nil {
return nil, err
}
defer head.Free()
var commits []string
parent := head.Parent(0)
defer parent.Free()
commits = append(commits, head.Message(), parent.Message())
for parent.ParentCount() != 0 {
parent = parent.Parent(0)
defer parent.Free()
commits = append(commits, parent.Message())
}
return commits, nil
}
func assertStringList(t *testing.T, expected, actual []string) {
if len(expected) != len(actual) {
t.Fatal("Lists are not the same size, expected " + strconv.Itoa(len(expected)) +
", got " + strconv.Itoa(len(actual)))
}
for index, element := range expected {
if element != actual[index] {
t.Error("Expected element " + strconv.Itoa(index) + " to be " + element + ", got " + actual[index])
}
}
}

64
refdb.go Normal file
View File

@ -0,0 +1,64 @@
package git
/*
#include <git2.h>
#include <git2/sys/refdb_backend.h>
extern void _go_git_refdb_backend_free(git_refdb_backend *backend);
*/
import "C"
import (
"runtime"
"unsafe"
)
type Refdb struct {
doNotCompare
ptr *C.git_refdb
r *Repository
}
type RefdbBackend struct {
doNotCompare
ptr *C.git_refdb_backend
}
func (v *Repository) NewRefdb() (refdb *Refdb, err error) {
var ptr *C.git_refdb
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_refdb_new(&ptr, v.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
refdb = &Refdb{ptr: ptr, r: v}
runtime.SetFinalizer(refdb, (*Refdb).Free)
return refdb, nil
}
func NewRefdbBackendFromC(ptr unsafe.Pointer) (backend *RefdbBackend) {
backend = &RefdbBackend{ptr: (*C.git_refdb_backend)(ptr)}
return backend
}
func (v *Refdb) SetBackend(backend *RefdbBackend) (err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_refdb_set_backend(v.ptr, backend.ptr)
runtime.KeepAlive(v)
runtime.KeepAlive(backend)
if ret < 0 {
backend.Free()
return MakeGitError(ret)
}
return nil
}
func (v *RefdbBackend) Free() {
runtime.SetFinalizer(v, nil)
C._go_git_refdb_backend_free(v.ptr)
}

View File

@ -1,9 +1,7 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
*/
import "C"
import (
@ -12,100 +10,353 @@ import (
)
type ReferenceType int
const (
ReferenceSymbolic ReferenceType = C.GIT_REF_SYMBOLIC
ReferenceOid = C.GIT_REF_OID
ReferenceOid ReferenceType = C.GIT_REF_OID
)
type Reference struct {
ptr *C.git_reference
doNotCompare
ptr *C.git_reference
repo *Repository
}
func newReferenceFromC(ptr *C.git_reference) *Reference {
ref := &Reference{ptr}
runtime.SetFinalizer(ref, (*Reference).Free)
return ref
type ReferenceCollection struct {
doNotCompare
repo *Repository
}
func (v *Reference) SetSymbolicTarget(target string) (*Reference, error) {
func (c *ReferenceCollection) Lookup(name string) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var ptr *C.git_reference
ctarget := C.CString(target)
defer C.free(unsafe.Pointer(ctarget))
ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget)
if ret < 0 {
return nil, LastError()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_reference_lookup(&ptr, c.repo.ptr, cname)
runtime.KeepAlive(c)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newReferenceFromC(ptr), nil
return newReferenceFromC(ptr, c.repo), nil
}
func (v *Reference) SetTarget(target *Oid) (*Reference, error) {
var ptr *C.git_reference
ret := C.git_reference_set_target(&ptr, v.ptr, target.toC())
if ret < 0 {
return nil, LastError()
}
return newReferenceFromC(ptr), nil
}
func (v *Reference) Resolve() (*Reference, error) {
var ptr *C.git_reference
ret := C.git_reference_resolve(&ptr, v.ptr)
if ret < 0 {
return nil, LastError()
}
return newReferenceFromC(ptr), nil
}
func (v *Reference) Rename(name string, force bool) (*Reference, error) {
var ptr *C.git_reference
func (c *ReferenceCollection) Create(name string, id *Oid, force bool, msg string) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ret := C.git_reference_rename(&ptr, v.ptr, cname, cbool(force))
if ret < 0 {
return nil, LastError()
var cmsg *C.char
if msg == "" {
cmsg = nil
} else {
cmsg = C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
}
return newReferenceFromC(ptr), nil
}
var ptr *C.git_reference
func (v *Reference) Target() *Oid {
return newOidFromC(C.git_reference_target(v.ptr))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
func (v *Reference) SymbolicTarget() string {
cstr := C.git_reference_symbolic_target(v.ptr)
if cstr == nil {
return ""
ecode := C.git_reference_create(&ptr, c.repo.ptr, cname, id.toC(), cbool(force), cmsg)
runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return C.GoString(cstr)
return newReferenceFromC(ptr, c.repo), nil
}
func (v *Reference) Delete() error {
ret := C.git_reference_delete(v.ptr)
func (c *ReferenceCollection) CreateSymbolic(name, target string, force bool, msg string) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ctarget := C.CString(target)
defer C.free(unsafe.Pointer(ctarget))
var cmsg *C.char
if msg == "" {
cmsg = nil
} else {
cmsg = C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
}
var ptr *C.git_reference
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_reference_symbolic_create(&ptr, c.repo.ptr, cname, ctarget, cbool(force), cmsg)
runtime.KeepAlive(c)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newReferenceFromC(ptr, c.repo), nil
}
// EnsureLog ensures that there is a reflog for the given reference
// name and creates an empty one if necessary.
func (c *ReferenceCollection) EnsureLog(name string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_ensure_log(c.repo.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
// HasLog returns whether there is a reflog for the given reference
// name
func (c *ReferenceCollection) HasLog(name string) (bool, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_has_log(c.repo.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return false, MakeGitError(ret)
}
return ret == 1, nil
}
// Dwim looks up a reference by DWIMing its short name
func (c *ReferenceCollection) Dwim(name string) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_reference
ret := C.git_reference_dwim(&ptr, c.repo.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, c.repo), nil
}
func newReferenceFromC(ptr *C.git_reference, repo *Repository) *Reference {
ref := &Reference{ptr: ptr, repo: repo}
runtime.SetFinalizer(ref, (*Reference).Free)
return ref
}
func (v *Reference) SetSymbolicTarget(target string, msg string) (*Reference, error) {
var ptr *C.git_reference
ctarget := C.CString(target)
defer C.free(unsafe.Pointer(ctarget))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var cmsg *C.char
if msg == "" {
cmsg = nil
} else {
cmsg = C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
}
ret := C.git_reference_symbolic_set_target(&ptr, v.ptr, ctarget, cmsg)
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, v.repo), nil
}
func (v *Reference) SetTarget(target *Oid, msg string) (*Reference, error) {
var ptr *C.git_reference
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var cmsg *C.char
if msg == "" {
cmsg = nil
} else {
cmsg = C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
}
ret := C.git_reference_set_target(&ptr, v.ptr, target.toC(), cmsg)
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, v.repo), nil
}
func (v *Reference) Resolve() (*Reference, error) {
var ptr *C.git_reference
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_resolve(&ptr, v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, v.repo), nil
}
func (v *Reference) Rename(name string, force bool, msg string) (*Reference, error) {
var ptr *C.git_reference
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var cmsg *C.char
if msg == "" {
cmsg = nil
} else {
cmsg = C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_rename(&ptr, v.ptr, cname, cbool(force), cmsg)
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, v.repo), nil
}
func (v *Reference) Target() *Oid {
ret := newOidFromC(C.git_reference_target(v.ptr))
runtime.KeepAlive(v)
return ret
}
func (v *Reference) SymbolicTarget() string {
var ret string
cstr := C.git_reference_symbolic_target(v.ptr)
if cstr != nil {
return C.GoString(cstr)
}
runtime.KeepAlive(v)
return ret
}
func (v *Reference) Delete() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_delete(v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (v *Reference) Peel(t ObjectType) (*Object, error) {
var cobj *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_reference_peel(&cobj, v.ptr, C.git_object_t(t))
runtime.KeepAlive(v)
if err < 0 {
return nil, MakeGitError(err)
}
return allocObject(cobj, v.repo), nil
}
// Owner returns a weak reference to the repository which owns this reference.
// This won't keep the underlying repository alive, but it should still be
// Freed.
func (v *Reference) Owner() *Repository {
repo := newRepositoryFromC(C.git_reference_owner(v.ptr))
runtime.KeepAlive(v)
repo.weak = true
return repo
}
// Cmp compares v to ref2. It returns 0 on equality, otherwise a
// stable sorting.
func (v *Reference) Cmp(ref2 *Reference) int {
ret := int(C.git_reference_cmp(v.ptr, ref2.ptr))
runtime.KeepAlive(v)
runtime.KeepAlive(ref2)
return ret
}
// Shorthand returns a "human-readable" short reference name.
func (v *Reference) Shorthand() string {
ret := C.GoString(C.git_reference_shorthand(v.ptr))
runtime.KeepAlive(v)
return ret
}
// Name returns the full name of v.
func (v *Reference) Name() string {
return C.GoString(C.git_reference_name(v.ptr))
ret := C.GoString(C.git_reference_name(v.ptr))
runtime.KeepAlive(v)
return ret
}
func (v *Reference) Type() ReferenceType {
return ReferenceType(C.git_reference_type(v.ptr))
ret := ReferenceType(C.git_reference_type(v.ptr))
runtime.KeepAlive(v)
return ret
}
func (v *Reference) IsBranch() bool {
ret := C.git_reference_is_branch(v.ptr) == 1
runtime.KeepAlive(v)
return ret
}
func (v *Reference) IsRemote() bool {
ret := C.git_reference_is_remote(v.ptr) == 1
runtime.KeepAlive(v)
return ret
}
func (v *Reference) IsTag() bool {
ret := C.git_reference_is_tag(v.ptr) == 1
runtime.KeepAlive(v)
return ret
}
// IsNote checks if the reference is a note.
func (v *Reference) IsNote() bool {
ret := C.git_reference_is_note(v.ptr) == 1
runtime.KeepAlive(v)
return ret
}
func (v *Reference) Free() {
@ -114,21 +365,45 @@ func (v *Reference) Free() {
}
type ReferenceIterator struct {
doNotCompare
ptr *C.git_reference_iterator
repo *Repository
}
type ReferenceNameIterator struct {
doNotCompare
*ReferenceIterator
}
// NewReferenceIterator creates a new iterator over reference names
func (repo *Repository) NewReferenceIterator() (*ReferenceIterator, error) {
var ptr *C.git_reference_iterator
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_iterator_new(&ptr, repo.ptr)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
iter := &ReferenceIterator{repo: repo, ptr: ptr}
runtime.SetFinalizer(iter, (*ReferenceIterator).Free)
return iter, nil
return newReferenceIteratorFromC(ptr, repo), nil
}
// NewReferenceIterator creates a new branch iterator over reference names
func (repo *Repository) NewReferenceNameIterator() (*ReferenceNameIterator, error) {
var ptr *C.git_reference_iterator
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_iterator_new(&ptr, repo.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
iter := newReferenceIteratorFromC(ptr, repo)
return iter.Names(), nil
}
// NewReferenceIteratorGlob creates an iterator over reference names
@ -138,46 +413,61 @@ func (repo *Repository) NewReferenceIteratorGlob(glob string) (*ReferenceIterato
cstr := C.CString(glob)
defer C.free(unsafe.Pointer(cstr))
var ptr *C.git_reference_iterator
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_iterator_glob_new(&ptr, repo.ptr, cstr)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
iter := &ReferenceIterator{repo: repo, ptr: ptr}
runtime.SetFinalizer(iter, (*ReferenceIterator).Free)
return iter, nil
return newReferenceIteratorFromC(ptr, repo), nil
}
// Next retrieves the next reference name. If the iteration is over,
// the returned error is git.ErrIterOver
func (v *ReferenceIterator) NextName() (string, error) {
func (i *ReferenceIterator) Names() *ReferenceNameIterator {
return &ReferenceNameIterator{ReferenceIterator: i}
}
// NextName retrieves the next reference name. If the iteration is over,
// the returned error code is git.ErrorCodeIterOver
func (v *ReferenceNameIterator) Next() (string, error) {
var ptr *C.char
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_next_name(&ptr, v.ptr)
if ret == ITEROVER {
return "", ErrIterOver
}
if ret < 0 {
return "", LastError()
return "", MakeGitError(ret)
}
return C.GoString(ptr), nil
}
// Create a channel from the iterator. You can use range on the
// returned channel to iterate over all the references names. The channel
// will be closed in case any error is found.
func (v *ReferenceIterator) NameIter() <-chan string {
ch := make(chan string)
go func() {
defer close(ch)
name, err := v.NextName()
for err == nil {
ch <- name
name, err = v.NextName()
}
}()
// Next retrieves the next reference. If the iterationis over, the
// returned error code is git.ErrorCodeIterOver
func (v *ReferenceIterator) Next() (*Reference, error) {
var ptr *C.git_reference
return ch
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reference_next(&ptr, v.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newReferenceFromC(ptr, v.repo), nil
}
func newReferenceIteratorFromC(ptr *C.git_reference_iterator, r *Repository) *ReferenceIterator {
iter := &ReferenceIterator{
ptr: ptr,
repo: r,
}
runtime.SetFinalizer(iter, (*ReferenceIterator).Free)
return iter
}
// Free the reference iterator
@ -185,3 +475,67 @@ func (v *ReferenceIterator) Free() {
runtime.SetFinalizer(v, nil)
C.git_reference_iterator_free(v.ptr)
}
// ReferenceNameIsValid returns whether the reference name is well-formed.
//
// Valid reference names must follow one of two patterns:
//
// 1. Top-level names must contain only capital letters and underscores,
// and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD").
//
// 2. Names prefixed with "refs/" can be almost anything. You must avoid
// the characters '~', '^', ':', ' \ ', '?', '[', and '*', and the sequences
// ".." and " @ {" which have special meaning to revparse.
func ReferenceNameIsValid(name string) (bool, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var valid C.int
ret := C.git_reference_name_is_valid(&valid, cname)
if ret < 0 {
return false, MakeGitError(ret)
}
return valid == 1, nil
}
const (
// This should match GIT_REFNAME_MAX in src/refs.h
_refnameMaxLength = C.size_t(1024)
)
type ReferenceFormat uint
const (
ReferenceFormatNormal ReferenceFormat = C.GIT_REFERENCE_FORMAT_NORMAL
ReferenceFormatAllowOnelevel ReferenceFormat = C.GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL
ReferenceFormatRefspecPattern ReferenceFormat = C.GIT_REFERENCE_FORMAT_REFSPEC_PATTERN
ReferenceFormatRefspecShorthand ReferenceFormat = C.GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND
)
// ReferenceNormalizeName normalizes the reference name and checks validity.
//
// This will normalize the reference name by removing any leading slash '/'
// characters and collapsing runs of adjacent slashes between name components
// into a single slash.
//
// See git_reference_symbolic_create() for rules about valid names.
func ReferenceNormalizeName(name string, flags ReferenceFormat) (string, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
buf := (*C.char)(C.malloc(_refnameMaxLength))
defer C.free(unsafe.Pointer(buf))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_reference_normalize_name(buf, _refnameMaxLength, cname, C.uint(flags))
if ecode < 0 {
return "", MakeGitError(ecode)
}
return C.GoString(buf), nil
}

View File

@ -1,7 +1,6 @@
package git
import (
"os"
"runtime"
"sort"
"testing"
@ -9,19 +8,20 @@ import (
)
func TestRefModification(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer os.RemoveAll(repo.Workdir())
defer cleanupTestRepo(t, repo)
commitId, treeId := seedTestRepo(t, repo)
_, err := repo.CreateReference("refs/tags/tree", treeId, true)
_, err := repo.References.Create("refs/tags/tree", treeId, true, "testTreeTag")
checkFatal(t, err)
tag, err := repo.LookupReference("refs/tags/tree")
tag, err := repo.References.Lookup("refs/tags/tree")
checkFatal(t, err)
checkRefType(t, tag, ReferenceOid)
ref, err := repo.LookupReference("HEAD")
ref, err := repo.References.Lookup("HEAD")
checkFatal(t, err)
checkRefType(t, ref, ReferenceSymbolic)
@ -45,17 +45,18 @@ func TestRefModification(t *testing.T) {
t.Fatalf("Wrong ref target")
}
_, err = tag.Rename("refs/tags/renamed", false)
_, err = tag.Rename("refs/tags/renamed", false, "")
checkFatal(t, err)
tag, err = repo.LookupReference("refs/tags/renamed")
tag, err = repo.References.Lookup("refs/tags/renamed")
checkFatal(t, err)
checkRefType(t, ref, ReferenceOid)
}
func TestIterator(t *testing.T) {
func TestReferenceIterator(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer os.RemoveAll(repo.Workdir())
defer cleanupTestRepo(t, repo)
loc, err := time.LoadLocation("Europe/Berlin")
checkFatal(t, err)
@ -78,13 +79,13 @@ func TestIterator(t *testing.T) {
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree)
checkFatal(t, err)
_, err = repo.CreateReference("refs/heads/one", commitId, true)
_, err = repo.References.Create("refs/heads/one", commitId, true, "headOne")
checkFatal(t, err)
_, err = repo.CreateReference("refs/heads/two", commitId, true)
_, err = repo.References.Create("refs/heads/two", commitId, true, "headTwo")
checkFatal(t, err)
_, err = repo.CreateReference("refs/heads/three", commitId, true)
_, err = repo.References.Create("refs/heads/three", commitId, true, "headThree")
checkFatal(t, err)
iter, err := repo.NewReferenceIterator()
@ -99,41 +100,155 @@ func TestIterator(t *testing.T) {
}
// test some manual iteration
name, err := iter.NextName()
nameIter := iter.Names()
name, err := nameIter.Next()
for err == nil {
list = append(list, name)
name, err = iter.NextName()
name, err = nameIter.Next()
}
if err != ErrIterOver {
if !IsErrorCode(err, ErrorCodeIterOver) {
t.Fatal("Iteration not over")
}
sort.Strings(list)
compareStringList(t, expected, list)
// test the channel iteration
list = []string{}
// test the iterator for full refs, rather than just names
iter, err = repo.NewReferenceIterator()
for name := range iter.NameIter() {
list = append(list, name)
checkFatal(t, err)
count := 0
_, err = iter.Next()
for err == nil {
count++
_, err = iter.Next()
}
if !IsErrorCode(err, ErrorCodeIterOver) {
t.Fatal("Iteration not over")
}
sort.Strings(list)
compareStringList(t, expected, list)
iter, err = repo.NewReferenceIteratorGlob("refs/heads/t*")
expected = []string{
"refs/heads/three",
"refs/heads/two",
if count != 4 {
t.Fatalf("Wrong number of references returned %v", count)
}
list = []string{}
for name := range iter.NameIter() {
list = append(list, name)
}
func TestReferenceOwner(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, _ := seedTestRepo(t, repo)
ref, err := repo.References.Create("refs/heads/foo", commitId, true, "")
checkFatal(t, err)
owner := ref.Owner()
if owner == nil {
t.Fatal("nil owner")
}
compareStringList(t, expected, list)
if owner.ptr != repo.ptr {
t.Fatalf("bad ptr, expected %v have %v\n", repo.ptr, owner.ptr)
}
}
func TestUtil(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, _ := seedTestRepo(t, repo)
ref, err := repo.References.Create("refs/heads/foo", commitId, true, "")
checkFatal(t, err)
ref2, err := repo.References.Dwim("foo")
checkFatal(t, err)
if ref.Cmp(ref2) != 0 {
t.Fatalf("foo didn't dwim to the right thing")
}
if ref.Shorthand() != "foo" {
t.Fatalf("refs/heads/foo has no foo shorthand")
}
hasLog, err := repo.References.HasLog("refs/heads/foo")
checkFatal(t, err)
if !hasLog {
t.Fatalf("branches have logs by default")
}
}
func TestIsNote(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitID, _ := seedTestRepo(t, repo)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Now(),
}
refname, err := repo.Notes.DefaultRef()
checkFatal(t, err)
_, err = repo.Notes.Create(refname, sig, sig, commitID, "This is a note", false)
checkFatal(t, err)
ref, err := repo.References.Lookup(refname)
checkFatal(t, err)
if !ref.IsNote() {
t.Fatalf("%s should be a note", ref.Name())
}
ref, err = repo.References.Create("refs/heads/foo", commitID, true, "")
checkFatal(t, err)
if ref.IsNote() {
t.Fatalf("%s should not be a note", ref.Name())
}
}
func TestReferenceNameIsValid(t *testing.T) {
t.Parallel()
valid, err := ReferenceNameIsValid("HEAD")
checkFatal(t, err)
if !valid {
t.Errorf("HEAD should be a valid reference name")
}
valid, err = ReferenceNameIsValid("HEAD1")
checkFatal(t, err)
if valid {
t.Errorf("HEAD1 should not be a valid reference name")
}
}
func TestReferenceNormalizeName(t *testing.T) {
t.Parallel()
ref, err := ReferenceNormalizeName("refs/heads//master", ReferenceFormatNormal)
checkFatal(t, err)
if ref != "refs/heads/master" {
t.Errorf("ReferenceNormalizeName(%q) = %q; want %q", "refs/heads//master", ref, "refs/heads/master")
}
ref, err = ReferenceNormalizeName("master", ReferenceFormatAllowOnelevel|ReferenceFormatRefspecShorthand)
checkFatal(t, err)
if ref != "master" {
t.Errorf("ReferenceNormalizeName(%q) = %q; want %q", "master", ref, "master")
}
ref, err = ReferenceNormalizeName("foo^", ReferenceFormatNormal)
if !IsErrorCode(err, ErrorCodeInvalidSpec) {
t.Errorf("foo^ should be invalid")
}
}
func compareStringList(t *testing.T, expected, actual []string) {
@ -152,8 +267,7 @@ func checkRefType(t *testing.T, ref *Reference, kind ReferenceType) {
// The failure happens at wherever we were called, not here
_, file, line, ok := runtime.Caller(1)
if !ok {
t.Fatal()
t.Fatalf("Unable to get caller")
}
t.Fatalf("Wrong ref type at %v:%v; have %v, expected %v", file, line, ref.Type(), kind)
}

149
refspec.go Normal file
View File

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

75
refspec_test.go Normal file
View File

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

1267
remote.go Normal file

File diff suppressed because it is too large Load Diff

521
remote_test.go Normal file
View File

@ -0,0 +1,521 @@
package git
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
"github.com/google/shlex"
"golang.org/x/crypto/ssh"
)
func TestListRemotes(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
remote, err := repo.Remotes.Create("test", "git://foo/bar")
checkFatal(t, err)
defer remote.Free()
expected := []string{
"test",
}
actual, err := repo.Remotes.List()
checkFatal(t, err)
compareStringList(t, expected, actual)
}
func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T) error {
if hostname != "github.com" {
t.Fatal("hostname does not match")
return errors.New("hostname does not match")
}
return nil
}
func TestCertificateCheck(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err)
defer remote.Free()
options := FetchOptions{
RemoteCallbacks: RemoteCallbacks{
CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) error {
return assertHostname(cert, valid, hostname, t)
},
},
}
err = remote.Fetch([]string{}, &options, "")
checkFatal(t, err)
}
func TestRemoteConnect(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err)
defer remote.Free()
err = remote.ConnectFetch(nil, nil, nil)
checkFatal(t, err)
}
func TestRemoteConnectOption(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
config, err := repo.Config()
checkFatal(t, err)
err = config.SetString("url.git@github.com:.insteadof", "https://github.com/")
checkFatal(t, err)
option, err := DefaultRemoteCreateOptions()
checkFatal(t, err)
option.Name = "origin"
option.Flags = RemoteCreateSkipInsteadof
remote, err := repo.Remotes.CreateWithOptions("https://github.com/libgit2/TestGitRepository", option)
checkFatal(t, err)
defer remote.Free()
err = remote.ConnectFetch(nil, nil, nil)
checkFatal(t, err)
}
func TestRemoteLs(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err)
defer remote.Free()
err = remote.ConnectFetch(nil, nil, nil)
checkFatal(t, err)
heads, err := remote.Ls()
checkFatal(t, err)
if len(heads) == 0 {
t.Error("Expected remote heads")
}
}
func TestRemoteLsFiltering(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err)
defer remote.Free()
err = remote.ConnectFetch(nil, nil, nil)
checkFatal(t, err)
heads, err := remote.Ls("master")
checkFatal(t, err)
if len(heads) != 1 {
t.Fatalf("Expected one head for master but I got %d", len(heads))
}
if heads[0].Id == nil {
t.Fatalf("Expected head to have an Id, but it's nil")
}
if heads[0].Name == "" {
t.Fatalf("Expected head to have a name, but it's empty")
}
}
func TestRemotePruneRefs(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
config, err := repo.Config()
checkFatal(t, err)
defer config.Free()
err = config.SetBool("remote.origin.prune", true)
checkFatal(t, err)
remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err)
defer remote.Free()
remote, err = repo.Remotes.Lookup("origin")
checkFatal(t, err)
defer remote.Free()
if !remote.PruneRefs() {
t.Fatal("Expected remote to be configured to prune references")
}
}
func TestRemotePrune(t *testing.T) {
t.Parallel()
remoteRepo := createTestRepo(t)
defer cleanupTestRepo(t, remoteRepo)
head, _ := seedTestRepo(t, remoteRepo)
commit, err := remoteRepo.LookupCommit(head)
checkFatal(t, err)
defer commit.Free()
remoteRef, err := remoteRepo.CreateBranch("test-prune", commit, true)
checkFatal(t, err)
defer remoteRef.Free()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
config, err := repo.Config()
checkFatal(t, err)
defer config.Free()
remoteUrl := fmt.Sprintf("file://%s", remoteRepo.Workdir())
remote, err := repo.Remotes.Create("origin", remoteUrl)
checkFatal(t, err)
defer remote.Free()
err = remote.Fetch([]string{"test-prune"}, nil, "")
checkFatal(t, err)
ref, err := repo.References.Create("refs/remotes/origin/test-prune", head, true, "remote reference")
checkFatal(t, err)
defer ref.Free()
err = remoteRef.Delete()
checkFatal(t, err)
err = config.SetBool("remote.origin.prune", true)
checkFatal(t, err)
rr, err := repo.Remotes.Lookup("origin")
checkFatal(t, err)
defer rr.Free()
err = rr.ConnectFetch(nil, nil, nil)
checkFatal(t, err)
err = rr.Prune(nil)
checkFatal(t, err)
ref, err = repo.References.Lookup("refs/remotes/origin/test-prune")
if err == nil {
ref.Free()
t.Fatal("Expected error getting a pruned reference")
}
}
func TestRemoteCredentialsCalled(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
remote, err := repo.Remotes.CreateAnonymous("https://github.com/libgit2/non-existent")
checkFatal(t, err)
defer remote.Free()
errNonExistent := errors.New("non-existent repository")
fetchOpts := FetchOptions{
RemoteCallbacks: RemoteCallbacks{
CredentialsCallback: func(url, username string, allowedTypes CredentialType) (*Credential, error) {
return nil, errNonExistent
},
},
}
err = remote.Fetch(nil, &fetchOpts, "fetch")
if err != errNonExistent {
t.Fatalf("remote.Fetch() = %v, want %v", err, errNonExistent)
}
}
func newChannelPipe(t *testing.T, w io.Writer, wg *sync.WaitGroup) (*os.File, error) {
pr, pw, err := os.Pipe()
if err != nil {
return nil, err
}
wg.Add(1)
go func() {
_, err := io.Copy(w, pr)
if err != nil && err != io.EOF {
t.Logf("Failed to copy: %v", err)
}
wg.Done()
}()
return pw, nil
}
func startSSHServer(t *testing.T, hostKey ssh.Signer, authorizedKeys []ssh.PublicKey) net.Listener {
t.Helper()
marshaledAuthorizedKeys := make([][]byte, len(authorizedKeys))
for i, authorizedKey := range authorizedKeys {
marshaledAuthorizedKeys[i] = authorizedKey.Marshal()
}
config := &ssh.ServerConfig{
PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
marshaledPubKey := pubKey.Marshal()
for _, marshaledAuthorizedKey := range marshaledAuthorizedKeys {
if bytes.Equal(marshaledPubKey, marshaledAuthorizedKey) {
return &ssh.Permissions{
// Record the public key used for authentication.
Extensions: map[string]string{
"pubkey-fp": ssh.FingerprintSHA256(pubKey),
},
}, nil
}
}
t.Logf("unknown public key for %q:\n\t%+v\n\t%+v\n", c.User(), pubKey.Marshal(), authorizedKeys)
return nil, fmt.Errorf("unknown public key for %q", c.User())
},
}
config.AddHostKey(hostKey)
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Failed to listen for connection: %v", err)
}
go func() {
nConn, err := listener.Accept()
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
return
}
t.Logf("Failed to accept incoming connection: %v", err)
return
}
defer nConn.Close()
conn, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil {
t.Logf("failed to handshake: %+v, %+v", conn, err)
return
}
// The incoming Request channel must be serviced.
go func() {
for newRequest := range reqs {
t.Logf("new request %v", newRequest)
}
}()
// Service only the first channel request
newChannel := <-chans
defer func() {
for newChannel := range chans {
t.Logf("new channel %v", newChannel)
newChannel.Reject(ssh.UnknownChannelType, "server closing")
}
}()
// Channels have a type, depending on the application level
// protocol intended. In the case of a shell, the type is
// "session" and ServerShell may be used to present a simple
// terminal interface.
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
return
}
channel, requests, err := newChannel.Accept()
if err != nil {
t.Logf("Could not accept channel: %v", err)
return
}
defer channel.Close()
// Sessions have out-of-band requests such as "shell",
// "pty-req" and "env". Here we handle only the
// "exec" request.
req := <-requests
if req.Type != "exec" {
req.Reply(false, nil)
return
}
// RFC 4254 Section 6.5.
var payload struct {
Command string
}
if err := ssh.Unmarshal(req.Payload, &payload); err != nil {
t.Logf("invalid payload on channel %v: %v", channel, err)
req.Reply(false, nil)
return
}
args, err := shlex.Split(payload.Command)
if err != nil {
t.Logf("invalid command on channel %v: %v", channel, err)
req.Reply(false, nil)
return
}
if len(args) < 2 || (args[0] != "git-upload-pack" && args[0] != "git-receive-pack") {
t.Logf("invalid command (%v) on channel %v: %v", args, channel, err)
req.Reply(false, nil)
return
}
req.Reply(true, nil)
go func(in <-chan *ssh.Request) {
for req := range in {
t.Logf("draining request %v", req)
}
}(requests)
// The first parameter is the (absolute) path of the repository.
args[1] = "./testdata" + args[1]
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = channel
var wg sync.WaitGroup
stdoutPipe, err := newChannelPipe(t, channel, &wg)
if err != nil {
t.Logf("Failed to create stdout pipe: %v", err)
return
}
cmd.Stdout = stdoutPipe
stderrPipe, err := newChannelPipe(t, channel.Stderr(), &wg)
if err != nil {
t.Logf("Failed to create stderr pipe: %v", err)
return
}
cmd.Stderr = stderrPipe
go func() {
wg.Wait()
channel.CloseWrite()
}()
err = cmd.Start()
if err != nil {
t.Logf("Failed to start %v: %v", args, err)
return
}
// Once the process has started, we need to close the write end of the
// pipes from this process so that we can know when the child has done
// writing to it.
stdoutPipe.Close()
stderrPipe.Close()
timer := time.AfterFunc(5*time.Second, func() {
t.Log("process timed out, terminating")
cmd.Process.Kill()
})
defer timer.Stop()
err = cmd.Wait()
if err != nil {
t.Logf("Failed to run %v: %v", args, err)
return
}
}()
return listener
}
func TestRemoteSSH(t *testing.T) {
t.Parallel()
pubKeyUsername := "testuser"
hostPrivKey, err := rsa.GenerateKey(rand.Reader, 512)
if err != nil {
t.Fatalf("Failed to generate the host RSA private key: %v", err)
}
hostSigner, err := ssh.NewSignerFromKey(hostPrivKey)
if err != nil {
t.Fatalf("Failed to generate SSH hostSigner: %v", err)
}
privKey, err := rsa.GenerateKey(rand.Reader, 512)
if err != nil {
t.Fatalf("Failed to generate the user RSA private key: %v", err)
}
signer, err := ssh.NewSignerFromKey(privKey)
if err != nil {
t.Fatalf("Failed to generate SSH signer: %v", err)
}
// This is in the format "xx:xx:xx:...", so we remove the colons so that it
// matches the fmt.Sprintf() below.
// Note that not all libssh2 implementations support the SHA256 fingerprint,
// so we use MD5 here for testing.
publicKeyFingerprint := strings.Replace(ssh.FingerprintLegacyMD5(hostSigner.PublicKey()), ":", "", -1)
listener := startSSHServer(t, hostSigner, []ssh.PublicKey{signer.PublicKey()})
defer listener.Close()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
certificateCheckCallbackCalled := false
fetchOpts := FetchOptions{
RemoteCallbacks: RemoteCallbacks{
CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) error {
hostkeyFingerprint := fmt.Sprintf("%x", cert.Hostkey.HashMD5[:])
if hostkeyFingerprint != publicKeyFingerprint {
return fmt.Errorf("server hostkey %q, want %q", hostkeyFingerprint, publicKeyFingerprint)
}
certificateCheckCallbackCalled = true
return nil
},
CredentialsCallback: func(url, username string, allowedTypes CredentialType) (*Credential, error) {
if allowedTypes&(CredentialTypeSSHKey|CredentialTypeSSHCustom|CredentialTypeSSHMemory) != 0 {
return NewCredentialSSHKeyFromSigner(pubKeyUsername, signer)
}
if (allowedTypes & CredentialTypeUsername) != 0 {
return NewCredentialUsername(pubKeyUsername)
}
return nil, fmt.Errorf("unknown credential type %+v", allowedTypes)
},
},
}
remote, err := repo.Remotes.Create(
"origin",
fmt.Sprintf("ssh://%s/TestGitRepository", listener.Addr().String()),
)
checkFatal(t, err)
defer remote.Free()
err = remote.Fetch(nil, &fetchOpts, "")
checkFatal(t, err)
if !certificateCheckCallbackCalled {
t.Fatalf("CertificateCheckCallback was not called")
}
heads, err := remote.Ls()
checkFatal(t, err)
if len(heads) == 0 {
t.Error("Expected remote heads")
}
}

View File

@ -1,169 +1,432 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
#include <git2/sys/repository.h>
#include <git2/sys/commit.h>
#include <string.h>
*/
import "C"
import (
"unsafe"
"runtime"
"unsafe"
)
// Repository
type Repository struct {
doNotCompare
ptr *C.git_repository
// Remotes represents the collection of remotes and can be
// used to add, remove and configure remotes for this
// repository.
Remotes RemoteCollection
// Submodules represents the collection of submodules and can
// be used to add, remove and configure submodules in this
// repository.
Submodules SubmoduleCollection
// References represents the collection of references and can
// be used to create, remove or update references for this repository.
References ReferenceCollection
// Notes represents the collection of notes and can be used to
// read, write and delete notes from this repository.
Notes NoteCollection
// Tags represents the collection of tags and can be used to create,
// list, iterate and remove tags in this repository.
Tags TagsCollection
// Stashes represents the collection of stashes and can be used to
// save, apply and iterate over stash states in this repository.
Stashes StashCollection
// weak indicates that a repository is a weak pointer and should not be
// freed.
weak bool
}
func newRepositoryFromC(ptr *C.git_repository) *Repository {
repo := &Repository{ptr: ptr}
repo.Remotes.repo = repo
repo.Remotes.remotes = make(map[*C.git_remote]*Remote)
repo.Submodules.repo = repo
repo.References.repo = repo
repo.Notes.repo = repo
repo.Tags.repo = repo
repo.Stashes.repo = repo
runtime.SetFinalizer(repo, (*Repository).Free)
return repo
}
func OpenRepository(path string) (*Repository, error) {
repo := new(Repository)
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.git_repository_open(&repo.ptr, cpath)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_repository
ret := C.git_repository_open(&ptr, cpath)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(repo, (*Repository).Free)
return repo, nil
return newRepositoryFromC(ptr), nil
}
type RepositoryOpenFlag int
const (
RepositoryOpenNoSearch RepositoryOpenFlag = C.GIT_REPOSITORY_OPEN_NO_SEARCH
RepositoryOpenCrossFs RepositoryOpenFlag = C.GIT_REPOSITORY_OPEN_CROSS_FS
RepositoryOpenBare RepositoryOpenFlag = C.GIT_REPOSITORY_OPEN_BARE
RepositoryOpenFromEnv RepositoryOpenFlag = C.GIT_REPOSITORY_OPEN_FROM_ENV
RepositoryOpenNoDotGit RepositoryOpenFlag = C.GIT_REPOSITORY_OPEN_NO_DOTGIT
)
func OpenRepositoryExtended(path string, flags RepositoryOpenFlag, ceiling string) (*Repository, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var cceiling *C.char = nil
if len(ceiling) > 0 {
cceiling = C.CString(ceiling)
defer C.free(unsafe.Pointer(cceiling))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_repository
ret := C.git_repository_open_ext(&ptr, cpath, C.uint(flags), cceiling)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newRepositoryFromC(ptr), nil
}
func InitRepository(path string, isbare bool) (*Repository, error) {
repo := new(Repository)
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.git_repository_init(&repo.ptr, cpath, ucbool(isbare))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_repository
ret := C.git_repository_init(&ptr, cpath, ucbool(isbare))
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(repo, (*Repository).Free)
return repo, nil
return newRepositoryFromC(ptr), nil
}
func NewRepositoryWrapOdb(odb *Odb) (repo *Repository, err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_repository
ret := C.git_repository_wrap_odb(&ptr, odb.ptr)
runtime.KeepAlive(odb)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newRepositoryFromC(ptr), nil
}
func (v *Repository) SetRefdb(refdb *Refdb) {
C.git_repository_set_refdb(v.ptr, refdb.ptr)
runtime.KeepAlive(v)
}
func (v *Repository) Free() {
ptr := v.ptr
v.ptr = nil
runtime.SetFinalizer(v, nil)
C.git_repository_free(v.ptr)
v.Remotes.Free()
if v.weak {
return
}
C.git_repository_free(ptr)
}
func (v *Repository) Config() (*Config, error) {
config := new(Config)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_config(&config.ptr, v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(config, (*Config).Free)
return config, nil
}
// SetConfig sets the configuration file for this repository.
//
// This configuration file will be used for all configuration queries involving
// this repository.
func (v *Repository) SetConfig(c *Config) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_set_config(v.ptr, c.ptr)
runtime.KeepAlive(v)
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (v *Repository) Index() (*Index, error) {
var ptr *C.git_index
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_index(&ptr, v.ptr)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
return newIndexFromC(ptr), nil
return newIndexFromC(ptr, v), nil
}
func (v *Repository) lookupType(oid *Oid, t ObjectType) (Object, error) {
func (v *Repository) lookupType(id *Oid, t ObjectType) (*Object, error) {
var ptr *C.git_object
ret := C.git_object_lookup(&ptr, v.ptr, oid.toC(), C.git_otype(t))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_object_lookup(&ptr, v.ptr, id.toC(), C.git_object_t(t))
runtime.KeepAlive(id)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
return allocObject(ptr), nil
return allocObject(ptr, v), nil
}
func (v *Repository) Lookup(oid *Oid) (Object, error) {
return v.lookupType(oid, ObjectAny)
func (v *Repository) lookupPrefixType(id *Oid, prefix uint, t ObjectType) (*Object, error) {
var ptr *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_object_lookup_prefix(&ptr, v.ptr, id.toC(), C.size_t(prefix), C.git_object_t(t))
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)
}
return allocObject(ptr, v), nil
}
func (v *Repository) LookupTree(oid *Oid) (*Tree, error) {
obj, err := v.lookupType(oid, ObjectTree)
func (v *Repository) Lookup(id *Oid) (*Object, error) {
return v.lookupType(id, ObjectAny)
}
// LookupPrefix looks up an object by its OID given a prefix of its identifier.
func (v *Repository) LookupPrefix(id *Oid, prefix uint) (*Object, error) {
return v.lookupPrefixType(id, prefix, ObjectAny)
}
func (v *Repository) LookupTree(id *Oid) (*Tree, error) {
obj, err := v.lookupType(id, ObjectTree)
if err != nil {
return nil, err
}
defer obj.Free()
return obj.(*Tree), nil
return obj.AsTree()
}
func (v *Repository) LookupCommit(oid *Oid) (*Commit, error) {
obj, err := v.lookupType(oid, ObjectCommit)
// LookupPrefixTree looks up a tree by its OID given a prefix of its identifier.
func (v *Repository) LookupPrefixTree(id *Oid, prefix uint) (*Tree, error) {
obj, err := v.lookupPrefixType(id, prefix, ObjectTree)
if err != nil {
return nil, err
}
defer obj.Free()
return obj.(*Commit), nil
return obj.AsTree()
}
func (v *Repository) LookupBlob(oid *Oid) (*Blob, error) {
obj, err := v.lookupType(oid, ObjectBlob)
func (v *Repository) LookupCommit(id *Oid) (*Commit, error) {
obj, err := v.lookupType(id, ObjectCommit)
if err != nil {
return nil, err
}
defer obj.Free()
return obj.(*Blob), nil
return obj.AsCommit()
}
func (v *Repository) LookupReference(name string) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var ptr *C.git_reference
ecode := C.git_reference_lookup(&ptr, v.ptr, cname)
if ecode < 0 {
return nil, LastError()
// LookupPrefixCommit looks up a commit by its OID given a prefix of its identifier.
func (v *Repository) LookupPrefixCommit(id *Oid, prefix uint) (*Commit, error) {
obj, err := v.lookupPrefixType(id, prefix, ObjectCommit)
if err != nil {
return nil, err
}
defer obj.Free()
return newReferenceFromC(ptr), nil
return obj.AsCommit()
}
func (v *Repository) CreateReference(name string, oid *Oid, force bool) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var ptr *C.git_reference
ecode := C.git_reference_create(&ptr, v.ptr, cname, oid.toC(), cbool(force))
if ecode < 0 {
return nil, LastError()
func (v *Repository) LookupBlob(id *Oid) (*Blob, error) {
obj, err := v.lookupType(id, ObjectBlob)
if err != nil {
return nil, err
}
defer obj.Free()
return newReferenceFromC(ptr), nil
return obj.AsBlob()
}
func (v *Repository) CreateSymbolicReference(name, target string, force bool) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ctarget := C.CString(target)
defer C.free(unsafe.Pointer(ctarget))
// LookupPrefixBlob looks up a blob by its OID given a prefix of its identifier.
func (v *Repository) LookupPrefixBlob(id *Oid, prefix uint) (*Blob, error) {
obj, err := v.lookupPrefixType(id, prefix, ObjectBlob)
if err != nil {
return nil, err
}
defer obj.Free()
return obj.AsBlob()
}
func (v *Repository) LookupTag(id *Oid) (*Tag, error) {
obj, err := v.lookupType(id, ObjectTag)
if err != nil {
return nil, err
}
defer obj.Free()
return obj.AsTag()
}
// LookupPrefixTag looks up a tag by its OID given a prefix of its identifier.
func (v *Repository) LookupPrefixTag(id *Oid, prefix uint) (*Tag, error) {
obj, err := v.lookupPrefixType(id, prefix, ObjectTag)
if err != nil {
return nil, err
}
defer obj.Free()
return obj.AsTag()
}
func (v *Repository) Head() (*Reference, error) {
var ptr *C.git_reference
ecode := C.git_reference_symbolic_create(&ptr, v.ptr, cname, ctarget, cbool(force))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_repository_head(&ptr, v.ptr)
if ecode < 0 {
return nil, LastError()
return nil, MakeGitError(ecode)
}
return newReferenceFromC(ptr), nil
return newReferenceFromC(ptr, v), nil
}
func (v *Repository) SetHead(refname string) error {
cname := C.CString(refname)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_repository_set_head(v.ptr, cname)
runtime.KeepAlive(v)
if ecode != 0 {
return MakeGitError(ecode)
}
return nil
}
func (v *Repository) SetHeadDetached(id *Oid) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_repository_set_head_detached(v.ptr, id.toC())
runtime.KeepAlive(v)
runtime.KeepAlive(id)
if ecode != 0 {
return MakeGitError(ecode)
}
return nil
}
func (v *Repository) IsHeadDetached() (bool, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_head_detached(v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return false, MakeGitError(ret)
}
return ret != 0, nil
}
func (v *Repository) IsHeadUnborn() (bool, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_head_unborn(v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return false, MakeGitError(ret)
}
return ret != 0, nil
}
func (v *Repository) IsEmpty() (bool, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_is_empty(v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return false, MakeGitError(ret)
}
return ret != 0, nil
}
func (v *Repository) IsShallow() (bool, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_is_shallow(v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return false, MakeGitError(ret)
}
return ret != 0, nil
}
func (v *Repository) Walk() (*RevWalk, error) {
walk := new(RevWalk)
ecode := C.git_revwalk_new(&walk.ptr, v.ptr)
var walkPtr *C.git_revwalk
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revwalk_new(&walkPtr, v.ptr)
if ecode < 0 {
return nil, LastError()
return nil, MakeGitError(ecode)
}
walk.repo = v
runtime.SetFinalizer(walk, freeRevWalk)
return walk, nil
return revWalkFromC(v, walkPtr), nil
}
func (v *Repository) CreateCommit(
@ -172,8 +435,13 @@ func (v *Repository) CreateCommit(
oid := new(Oid)
cref := C.CString(refname)
defer C.free(unsafe.Pointer(cref))
var cref *C.char
if refname == "" {
cref = nil
} else {
cref = C.CString(refname)
defer C.free(unsafe.Pointer(cref))
}
cmsg := C.CString(message)
defer C.free(unsafe.Pointer(cmsg))
@ -181,28 +449,204 @@ func (v *Repository) CreateCommit(
var cparents []*C.git_commit = nil
var parentsarg **C.git_commit = nil
nparents:= len(parents)
nparents := len(parents)
if nparents > 0 {
cparents = make([]*C.git_commit, nparents)
for i, v := range parents {
cparents[i] = v.ptr
cparents[i] = v.cast_ptr
}
parentsarg = &cparents[0]
}
authorSig := author.toC()
authorSig, err := author.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(authorSig)
committerSig := committer.toC()
committerSig, err := committer.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(committerSig)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_commit_create(
oid.toC(), v.ptr, cref,
authorSig, committerSig,
nil, cmsg, tree.ptr, C.int(nparents), parentsarg)
nil, cmsg, tree.cast_ptr, C.size_t(nparents), parentsarg)
runtime.KeepAlive(v)
runtime.KeepAlive(oid)
runtime.KeepAlive(parents)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
return oid, nil
}
// CreateCommitWithSignature creates a commit object from the given contents and
// signature.
func (v *Repository) CreateCommitWithSignature(
commitContent, signature, signatureField string,
) (*Oid, error) {
cCommitContent := C.CString(commitContent)
defer C.free(unsafe.Pointer(cCommitContent))
var cSignature *C.char
if signature != "" {
cSignature = C.CString(string(signature))
defer C.free(unsafe.Pointer(cSignature))
}
var cSignatureField *C.char
if signatureField != "" {
cSignatureField = C.CString(string(signatureField))
defer C.free(unsafe.Pointer(cSignatureField))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
oid := new(Oid)
ret := C.git_commit_create_with_signature(oid.toC(), v.ptr, cCommitContent, cSignature, cSignatureField)
runtime.KeepAlive(v)
runtime.KeepAlive(oid)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}
// CreateCommitBuffer creates a commit and write it into a buffer.
func (v *Repository) CreateCommitBuffer(
author, committer *Signature,
messageEncoding MessageEncoding,
message string,
tree *Tree,
parents ...*Commit,
) ([]byte, error) {
cmsg := C.CString(message)
defer C.free(unsafe.Pointer(cmsg))
var cencoding *C.char
// Since the UTF-8 encoding is the default, pass in nil whenever UTF-8 is
// provided. That will cause the commit to not have an explicit header for
// it.
if messageEncoding != MessageEncodingUTF8 && messageEncoding != MessageEncoding("") {
cencoding = C.CString(string(messageEncoding))
defer C.free(unsafe.Pointer(cencoding))
}
var cparents []*C.git_commit = nil
var parentsarg **C.git_commit = nil
nparents := len(parents)
if nparents > 0 {
cparents = make([]*C.git_commit, nparents)
for i, v := range parents {
cparents[i] = v.cast_ptr
}
parentsarg = &cparents[0]
}
authorSig, err := author.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(committerSig)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var buf C.git_buf
defer C.git_buf_dispose(&buf)
ret := C.git_commit_create_buffer(
&buf, v.ptr,
authorSig, committerSig,
cencoding, cmsg, tree.cast_ptr, C.size_t(nparents), parentsarg)
runtime.KeepAlive(v)
runtime.KeepAlive(buf)
runtime.KeepAlive(parents)
if ret < 0 {
return nil, MakeGitError(ret)
}
return C.GoBytes(unsafe.Pointer(buf.ptr), C.int(buf.size)), nil
}
func (v *Repository) CreateCommitFromIds(
refname string, author, committer *Signature,
message string, tree *Oid, parents ...*Oid) (*Oid, error) {
oid := new(Oid)
var cref *C.char
if refname == "" {
cref = nil
} else {
cref = C.CString(refname)
defer C.free(unsafe.Pointer(cref))
}
cmsg := C.CString(message)
defer C.free(unsafe.Pointer(cmsg))
var parentsarg **C.git_oid = nil
nparents := len(parents)
if nparents > 0 {
// All this awful pointer arithmetic is needed to avoid passing a Go
// pointer to Go pointer into C. Other methods (like CreateCommits) are
// fine without this workaround because they are just passing Go pointers
// to C pointers, but arrays-of-pointers-to-git_oid are a bit special since
// both the array and the objects are allocated from Go.
var emptyOidPtr *C.git_oid
sizeofOidPtr := unsafe.Sizeof(emptyOidPtr)
parentsarg = (**C.git_oid)(C.calloc(C.size_t(uintptr(nparents)), C.size_t(sizeofOidPtr)))
defer C.free(unsafe.Pointer(parentsarg))
parentsptr := uintptr(unsafe.Pointer(parentsarg))
for _, v := range parents {
*(**C.git_oid)(unsafe.Pointer(parentsptr)) = v.toC()
parentsptr += sizeofOidPtr
}
}
authorSig, err := author.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(committerSig)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_commit_create_from_ids(
oid.toC(), v.ptr, cref,
authorSig, committerSig,
nil, cmsg, tree.toC(), C.size_t(nparents), parentsarg)
runtime.KeepAlive(v)
runtime.KeepAlive(oid)
runtime.KeepAlive(tree)
runtime.KeepAlive(parents)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
@ -213,57 +657,220 @@ func (v *Odb) Free() {
C.git_odb_free(v.ptr)
}
func (v *Refdb) Free() {
runtime.SetFinalizer(v, nil)
C.git_refdb_free(v.ptr)
}
func (v *Repository) Odb() (odb *Odb, err error) {
odb = new(Odb)
if ret := C.git_repository_odb(&odb.ptr, v.ptr); ret < 0 {
return nil, LastError()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_odb(&odb.ptr, v.ptr)
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(odb, (*Odb).Free)
return
return odb, nil
}
func (repo *Repository) Path() string {
return C.GoString(C.git_repository_path(repo.ptr))
s := C.GoString(C.git_repository_path(repo.ptr))
runtime.KeepAlive(repo)
return s
}
func (repo *Repository) IsBare() (bool) {
return C.git_repository_is_bare(repo.ptr) != 0
func (repo *Repository) IsBare() bool {
ret := C.git_repository_is_bare(repo.ptr) != 0
runtime.KeepAlive(repo)
return ret
}
func (repo *Repository) Workdir() string {
return C.GoString(C.git_repository_workdir(repo.ptr))
s := C.GoString(C.git_repository_workdir(repo.ptr))
runtime.KeepAlive(repo)
return s
}
func (repo *Repository) SetWorkdir(workdir string, updateGitlink bool) error {
cstr := C.CString(workdir)
defer C.free(unsafe.Pointer(cstr))
if C.git_repository_set_workdir(repo.ptr, cstr, cbool(updateGitlink)) < 0 {
return LastError()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if ret := C.git_repository_set_workdir(repo.ptr, cstr, cbool(updateGitlink)); ret < 0 {
return MakeGitError(ret)
}
runtime.KeepAlive(repo)
return nil
}
func (v *Repository) TreeBuilder() (*TreeBuilder, error) {
bld := new(TreeBuilder)
if ret := C.git_treebuilder_create(&bld.ptr, nil); ret < 0 {
return nil, LastError()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if ret := C.git_treebuilder_new(&bld.ptr, v.ptr, nil); ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(bld, (*TreeBuilder).Free)
bld.repo = v
return bld, nil
}
func (v *Repository) RevparseSingle(spec string) (Object, error) {
cspec := C.CString(spec)
defer C.free(unsafe.Pointer(cspec))
func (v *Repository) TreeBuilderFromTree(tree *Tree) (*TreeBuilder, error) {
bld := new(TreeBuilder)
var ptr *C.git_object
ecode := C.git_revparse_single(&ptr, v.ptr, cspec)
if ecode < 0 {
return nil, LastError()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if ret := C.git_treebuilder_new(&bld.ptr, v.ptr, tree.cast_ptr); ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(bld, (*TreeBuilder).Free)
return allocObject(ptr), nil
bld.repo = v
return bld, nil
}
type RepositoryState int
const (
RepositoryStateNone RepositoryState = C.GIT_REPOSITORY_STATE_NONE
RepositoryStateMerge RepositoryState = C.GIT_REPOSITORY_STATE_MERGE
RepositoryStateRevert RepositoryState = C.GIT_REPOSITORY_STATE_REVERT
RepositoryStateCherrypick RepositoryState = C.GIT_REPOSITORY_STATE_CHERRYPICK
RepositoryStateBisect RepositoryState = C.GIT_REPOSITORY_STATE_BISECT
RepositoryStateRebase RepositoryState = C.GIT_REPOSITORY_STATE_REBASE
RepositoryStateRebaseInteractive RepositoryState = C.GIT_REPOSITORY_STATE_REBASE_INTERACTIVE
RepositoryStateRebaseMerge RepositoryState = C.GIT_REPOSITORY_STATE_REBASE_MERGE
RepositoryStateApplyMailbox RepositoryState = C.GIT_REPOSITORY_STATE_APPLY_MAILBOX
RepositoryStateApplyMailboxOrRebase RepositoryState = C.GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE
)
func (r *Repository) State() RepositoryState {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := RepositoryState(C.git_repository_state(r.ptr))
runtime.KeepAlive(r)
return ret
}
func (r *Repository) StateCleanup() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cErr := C.git_repository_state_cleanup(r.ptr)
runtime.KeepAlive(r)
if cErr < 0 {
return MakeGitError(cErr)
}
return nil
}
func (r *Repository) AddGitIgnoreRules(rules string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
crules := C.CString(rules)
defer C.free(unsafe.Pointer(crules))
ret := C.git_ignore_add_rule(r.ptr, crules)
runtime.KeepAlive(r)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (r *Repository) ClearGitIgnoreRules() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_ignore_clear_internal_rules(r.ptr)
runtime.KeepAlive(r)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// Message retrieves git's prepared message.
// Operations such as git revert/cherry-pick/merge with the -n option stop just
// short of creating a commit with the changes and save their prepared message
// in .git/MERGE_MSG so the next git-commit execution can present it to the
// user for them to amend if they wish.
//
// Use this function to get the contents of this file. Don't forget to remove
// the file after you create the commit.
func (r *Repository) Message() (string, error) {
buf := C.git_buf{}
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cErr := C.git_repository_message(&buf, r.ptr)
runtime.KeepAlive(r)
if cErr < 0 {
return "", MakeGitError(cErr)
}
return C.GoString(buf.ptr), nil
}
// RemoveMessage removes git's prepared message.
func (r *Repository) RemoveMessage() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cErr := C.git_repository_message_remove(r.ptr)
runtime.KeepAlive(r)
if cErr < 0 {
return MakeGitError(cErr)
}
return nil
}
type RepositoryItem int
const (
RepositoryItemGitDir RepositoryItem = C.GIT_REPOSITORY_ITEM_GITDIR
RepositoryItemWorkDir RepositoryItem = C.GIT_REPOSITORY_ITEM_WORKDIR
RepositoryItemCommonDir RepositoryItem = C.GIT_REPOSITORY_ITEM_COMMONDIR
RepositoryItemIndex RepositoryItem = C.GIT_REPOSITORY_ITEM_INDEX
RepositoryItemObjects RepositoryItem = C.GIT_REPOSITORY_ITEM_OBJECTS
RepositoryItemRefs RepositoryItem = C.GIT_REPOSITORY_ITEM_REFS
RepositoryItemPackedRefs RepositoryItem = C.GIT_REPOSITORY_ITEM_PACKED_REFS
RepositoryItemRemotes RepositoryItem = C.GIT_REPOSITORY_ITEM_REMOTES
RepositoryItemConfig RepositoryItem = C.GIT_REPOSITORY_ITEM_CONFIG
RepositoryItemInfo RepositoryItem = C.GIT_REPOSITORY_ITEM_INFO
RepositoryItemHooks RepositoryItem = C.GIT_REPOSITORY_ITEM_HOOKS
RepositoryItemLogs RepositoryItem = C.GIT_REPOSITORY_ITEM_LOGS
RepositoryItemModules RepositoryItem = C.GIT_REPOSITORY_ITEM_MODULES
RepositoryItemWorkTrees RepositoryItem = C.GIT_REPOSITORY_ITEM_WORKTREES
)
func (r *Repository) ItemPath(item RepositoryItem) (string, error) {
var c_buf C.git_buf
defer C.git_buf_dispose(&c_buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_item_path(&c_buf, r.ptr, C.git_repository_item_t(item))
runtime.KeepAlive(r)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(c_buf.ptr), nil
}

159
repository_test.go Normal file
View File

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

49
reset.go Normal file
View File

@ -0,0 +1,49 @@
package git
/*
#include <git2.h>
*/
import "C"
import "runtime"
type ResetType int
const (
ResetSoft ResetType = C.GIT_RESET_SOFT
ResetMixed ResetType = C.GIT_RESET_MIXED
ResetHard ResetType = C.GIT_RESET_HARD
)
func (r *Repository) ResetToCommit(commit *Commit, resetType ResetType, opts *CheckoutOptions) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var err error
cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err)
defer freeCheckoutOptions(cOpts)
ret := C.git_reset(r.ptr, commit.ptr, C.git_reset_t(resetType), cOpts)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (r *Repository) ResetDefaultToCommit(commit *Commit, pathspecs []string) error {
cpathspecs := C.git_strarray{}
cpathspecs.count = C.size_t(len(pathspecs))
cpathspecs.strings = makeCStringsFromStrings(pathspecs)
defer freeStrarray(&cpathspecs)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_reset_default(r.ptr, commit.ptr, &cpathspecs)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}

48
reset_test.go Normal file
View File

@ -0,0 +1,48 @@
package git
import (
"io/ioutil"
"testing"
)
func TestResetToCommit(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
// create commit to reset to
commitId, _ := updateReadme(t, repo, "testing reset")
// create commit to reset from
nextCommitId, _ := updateReadme(t, repo, "will be reset")
// confirm that we wrote "will be reset" to the readme
newBytes, err := ioutil.ReadFile(pathInRepo(repo, "README"))
checkFatal(t, err)
if string(newBytes) != "will be reset" {
t.Fatalf("expected %s to equal 'will be reset'", string(newBytes))
}
// confirm that the head of the repo is the next commit id
head, err := repo.Head()
checkFatal(t, err)
if head.Target().String() != nextCommitId.String() {
t.Fatalf(
"expected to be at latest commit %s, but was %s",
nextCommitId.String(),
head.Target().String(),
)
}
commitToResetTo, err := repo.LookupCommit(commitId)
checkFatal(t, err)
repo.ResetToCommit(commitToResetTo, ResetHard, &CheckoutOptions{})
// check that the file now reads "testing reset" like it did before
bytes, err := ioutil.ReadFile(pathInRepo(repo, "README"))
checkFatal(t, err)
if string(bytes) != "testing reset" {
t.Fatalf("expected %s to equal 'testing reset'", string(bytes))
}
}

104
revert.go Normal file
View File

@ -0,0 +1,104 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
)
// RevertOptions contains options for performing a revert
type RevertOptions struct {
Mainline uint
MergeOptions MergeOptions
CheckoutOptions CheckoutOptions
}
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)
if opts == nil {
return nil
}
copts.mainline = C.uint(opts.Mainline)
populateMergeOptions(&copts.merge_opts, &opts.MergeOptions)
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
return copts
}
func revertOptionsFromC(copts *C.git_revert_options) RevertOptions {
return RevertOptions{
Mainline: uint(copts.mainline),
MergeOptions: mergeOptionsFromC(&copts.merge_opts),
CheckoutOptions: checkoutOptionsFromC(&copts.checkout_opts),
}
}
func freeRevertOptions(copts *C.git_revert_options) {
if copts != nil {
return
}
freeMergeOptions(&copts.merge_opts)
freeCheckoutOptions(&copts.checkout_opts)
}
// DefaultRevertOptions initialises a RevertOptions struct with default values
func DefaultRevertOptions() (RevertOptions, error) {
copts := C.git_revert_options{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revert_options_init(&copts, C.GIT_REVERT_OPTIONS_VERSION)
if ecode < 0 {
return RevertOptions{}, MakeGitError(ecode)
}
defer freeRevertOptions(&copts)
return revertOptionsFromC(&copts), nil
}
// Revert the provided commit leaving the index updated with the results of the revert
func (r *Repository) Revert(commit *Commit, revertOptions *RevertOptions) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var err error
cOpts := populateRevertOptions(&C.git_revert_options{}, revertOptions, &err)
defer freeRevertOptions(cOpts)
ret := C.git_revert(r.ptr, commit.cast_ptr, cOpts)
runtime.KeepAlive(r)
runtime.KeepAlive(commit)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// RevertCommit reverts the provided commit against "ourCommit"
// The returned index contains the result of the revert and should be freed
func (r *Repository) RevertCommit(revertCommit *Commit, ourCommit *Commit, mainline uint, mergeOptions *MergeOptions) (*Index, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cOpts := populateMergeOptions(&C.git_merge_options{}, mergeOptions)
defer freeMergeOptions(cOpts)
var index *C.git_index
ecode := C.git_revert_commit(&index, r.ptr, revertCommit.cast_ptr, ourCommit.cast_ptr, C.uint(mainline), cOpts)
runtime.KeepAlive(revertCommit)
runtime.KeepAlive(ourCommit)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return newIndexFromC(index, r), nil
}

76
revert_test.go Normal file
View File

@ -0,0 +1,76 @@
package git
import (
"testing"
)
const (
expectedRevertedReadmeContents = "foo\n"
)
func TestRevert(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
commitID, _ := updateReadme(t, repo, content)
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
revertOptions, err := DefaultRevertOptions()
checkFatal(t, err)
err = repo.Revert(commit, &revertOptions)
checkFatal(t, err)
actualReadmeContents := readReadme(t, repo)
if actualReadmeContents != expectedRevertedReadmeContents {
t.Fatalf(`README has incorrect contents after revert. Expected: "%v", Actual: "%v"`,
expectedRevertedReadmeContents, actualReadmeContents)
}
state := repo.State()
if state != RepositoryStateRevert {
t.Fatalf("Incorrect repository state. Expected: %v, Actual: %v", RepositoryStateRevert, state)
}
err = repo.StateCleanup()
checkFatal(t, err)
state = repo.State()
if state != RepositoryStateNone {
t.Fatalf("Incorrect repository state. Expected: %v, Actual: %v", RepositoryStateNone, state)
}
}
func TestRevertCommit(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
commitID, _ := updateReadme(t, repo, content)
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
revertOptions, err := DefaultRevertOptions()
checkFatal(t, err)
index, err := repo.RevertCommit(commit, commit, 0, &revertOptions.MergeOptions)
checkFatal(t, err)
defer index.Free()
err = repo.CheckoutIndex(index, &revertOptions.CheckoutOptions)
checkFatal(t, err)
actualReadmeContents := readReadme(t, repo)
if actualReadmeContents != expectedRevertedReadmeContents {
t.Fatalf(`README has incorrect contents after revert. Expected: "%v", Actual: "%v"`,
expectedRevertedReadmeContents, actualReadmeContents)
}
}

114
revparse.go Normal file
View File

@ -0,0 +1,114 @@
package git
/*
#include <git2.h>
extern void _go_git_revspec_free(git_revspec *revspec);
*/
import "C"
import (
"runtime"
"unsafe"
)
type RevparseFlag int
const (
RevparseSingle RevparseFlag = C.GIT_REVPARSE_SINGLE
RevparseRange RevparseFlag = C.GIT_REVPARSE_RANGE
RevparseMergeBase RevparseFlag = C.GIT_REVPARSE_MERGE_BASE
)
type Revspec struct {
doNotCompare
to *Object
from *Object
flags RevparseFlag
}
func (rs *Revspec) To() *Object {
return rs.to
}
func (rs *Revspec) From() *Object {
return rs.from
}
func (rs *Revspec) Flags() RevparseFlag {
return rs.flags
}
func newRevspecFromC(ptr *C.git_revspec, repo *Repository) *Revspec {
var to *Object
var from *Object
if ptr.to != nil {
to = allocObject(ptr.to, repo)
}
if ptr.from != nil {
from = allocObject(ptr.from, repo)
}
return &Revspec{
to: to,
from: from,
flags: RevparseFlag(ptr.flags),
}
}
func (r *Repository) Revparse(spec string) (*Revspec, error) {
cspec := C.CString(spec)
defer C.free(unsafe.Pointer(cspec))
var crevspec C.git_revspec
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revparse(&crevspec, r.ptr, cspec)
if ecode != 0 {
return nil, MakeGitError(ecode)
}
return newRevspecFromC(&crevspec, r), nil
}
func (v *Repository) RevparseSingle(spec string) (*Object, error) {
cspec := C.CString(spec)
defer C.free(unsafe.Pointer(cspec))
var ptr *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revparse_single(&ptr, v.ptr, cspec)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return allocObject(ptr, v), nil
}
func (r *Repository) RevparseExt(spec string) (*Object, *Reference, error) {
cspec := C.CString(spec)
defer C.free(unsafe.Pointer(cspec))
var obj *C.git_object
var ref *C.git_reference
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revparse_ext(&obj, &ref, r.ptr, cspec)
if ecode != 0 {
return nil, nil, MakeGitError(ecode)
}
if ref == nil {
return allocObject(obj, r), nil, nil
}
return allocObject(obj, r), newReferenceFromC(ref, r), nil
}

60
revparse_test.go Normal file
View File

@ -0,0 +1,60 @@
package git
import (
"testing"
)
func TestRevparse(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, _ := seedTestRepo(t, repo)
revSpec, err := repo.Revparse("HEAD")
checkFatal(t, err)
checkObject(t, revSpec.From(), commitId)
}
func TestRevparseSingle(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, _ := seedTestRepo(t, repo)
obj, err := repo.RevparseSingle("HEAD")
checkFatal(t, err)
checkObject(t, obj, commitId)
}
func TestRevparseExt(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
_, treeId := seedTestRepo(t, repo)
ref, err := repo.References.Create("refs/heads/master", treeId, true, "")
checkFatal(t, err)
obj, ref, err := repo.RevparseExt("master")
checkFatal(t, err)
checkObject(t, obj, treeId)
if ref == nil {
t.Fatalf("bad reference")
}
}
func checkObject(t *testing.T, obj *Object, id *Oid) {
if obj == nil {
t.Fatalf("bad object")
}
if !obj.Id().Equal(id) {
t.Fatalf("bad object, expected %s, got %s", id.String(), obj.Id().String())
}
}

View File

@ -0,0 +1,5 @@
#!/bin/sh
set -e
exec "$(dirname "$0")/build-libgit2.sh" --dynamic

5
script/build-libgit2-static.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
set -e
exec "$(dirname "$0")/build-libgit2.sh" --static

90
script/build-libgit2.sh Executable file
View File

@ -0,0 +1,90 @@
#!/bin/sh
# Since CMake cannot build the static and dynamic libraries in the same
# directory, this script helps build both static and dynamic versions of it and
# have the common flags in one place instead of split between two places.
set -e
usage() {
echo "Usage: $0 <--dynamic|--static> [--system]">&2
exit 1
}
if [ "$#" -eq "0" ]; then
usage
fi
ROOT=${ROOT-"$(cd "$(dirname "$0")/.." && echo "${PWD}")"}
VENDORED_PATH=${VENDORED_PATH-"${ROOT}/vendor/libgit2"}
BUILD_SYSTEM=OFF
while [ $# -gt 0 ]; do
case "$1" in
--static)
BUILD_PATH="${ROOT}/static-build"
BUILD_SHARED_LIBS=OFF
;;
--dynamic)
BUILD_PATH="${ROOT}/dynamic-build"
BUILD_SHARED_LIBS=ON
;;
--system)
BUILD_SYSTEM=ON
;;
*)
usage
;;
esac
shift
done
if [ -z "${BUILD_SHARED_LIBS}" ]; then
usage
fi
if [ -n "${BUILD_LIBGIT_REF}" ]; then
git -C "${VENDORED_PATH}" checkout "${BUILD_LIBGIT_REF}"
trap "git submodule update --init" EXIT
fi
BUILD_DEPRECATED_HARD="ON"
if [ "${BUILD_SYSTEM}" = "ON" ]; then
BUILD_INSTALL_PREFIX=${SYSTEM_INSTALL_PREFIX-"/usr"}
# Most system-wide installations won't intentionally omit deprecated symbols.
BUILD_DEPRECATED_HARD="OFF"
else
BUILD_INSTALL_PREFIX="${BUILD_PATH}/install"
mkdir -p "${BUILD_PATH}/install/lib"
fi
USE_BUNDLED_ZLIB="ON"
if [ "${USE_CHROMIUM_ZLIB}" = "ON" ]; then
USE_BUNDLED_ZLIB="Chromium"
fi
mkdir -p "${BUILD_PATH}/build" &&
cd "${BUILD_PATH}/build" &&
cmake -DTHREADSAFE=ON \
-DBUILD_CLAR=OFF \
-DBUILD_SHARED_LIBS"=${BUILD_SHARED_LIBS}" \
-DREGEX_BACKEND=builtin \
-DUSE_BUNDLED_ZLIB="${USE_BUNDLED_ZLIB}" \
-DUSE_HTTPS=OFF \
-DUSE_SSH=OFF \
-DCMAKE_C_FLAGS=-fPIC \
-DCMAKE_BUILD_TYPE="RelWithDebInfo" \
-DCMAKE_INSTALL_PREFIX="${BUILD_INSTALL_PREFIX}" \
-DCMAKE_INSTALL_LIBDIR="lib" \
-DDEPRECATE_HARD="${BUILD_DEPRECATE_HARD}" \
"${VENDORED_PATH}"
if which make nproc >/dev/null && [ -f Makefile ]; then
# Make the build parallel if make is available and cmake used Makefiles.
exec make "-j$(nproc --all)" install
else
exec cmake --build . --target install
fi

View File

@ -0,0 +1,73 @@
// +build ignore
package main
import (
"bytes"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/printer"
"go/token"
"log"
"os"
"path/filepath"
"strings"
)
var (
fset = token.NewFileSet()
)
func main() {
log.SetFlags(0)
bpkg, err := build.ImportDir(".", 0)
if err != nil {
log.Fatal(err)
}
pkgs, err := parser.ParseDir(fset, bpkg.Dir, func(fi os.FileInfo) bool { return filepath.Ext(fi.Name()) == ".go" }, 0)
if err != nil {
log.Fatal(err)
}
for _, pkg := range pkgs {
if err := checkPkg(pkg); err != nil {
log.Fatal(err)
}
}
if len(pkgs) == 0 {
log.Fatal("No packages to check.")
}
}
var ignoreViolationsInFunc = map[string]bool{
"MakeGitError": true,
"MakeGitError2": true,
}
func checkPkg(pkg *ast.Package) error {
var violations []string
ast.Inspect(pkg, func(node ast.Node) bool {
switch node := node.(type) {
case *ast.FuncDecl:
var b bytes.Buffer
if err := printer.Fprint(&b, fset, node); err != nil {
log.Fatal(err)
}
src := b.String()
if strings.Contains(src, "MakeGitError") && !strings.Contains(src, "runtime.LockOSThread()") && !strings.Contains(src, "defer runtime.UnlockOSThread()") && !ignoreViolationsInFunc[node.Name.Name] {
pos := fset.Position(node.Pos())
violations = append(violations, fmt.Sprintf("%s at %s:%d", node.Name.Name, pos.Filename, pos.Line))
}
}
return true
})
if len(violations) > 0 {
return fmt.Errorf("%d non-thread-locked calls to MakeGitError found. To fix, add the following to each func below that calls MakeGitError, before the cgo call that might produce the error:\n\n\truntime.LockOSThread()\n\tdefer runtime.UnlockOSThread()\n\n%s", len(violations), strings.Join(violations, "\n"))
}
return nil
}

22
script/install-libgit2.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/sh
#
# Install libgit2 to go libgit2 in dynamic mode on Travis
#
set -ex
# We don't want to build libgit2 on the next branch, as we carry a
# submodule with the exact version we support
if [ "x$TRAVIS_BRANCH" = "xnext" ]; then
exit 0
fi
cd "${HOME}"
LG2VER="0.24.0"
wget -O libgit2-${LG2VER}.tar.gz https://github.com/libgit2/libgit2/archive/v${LG2VER}.tar.gz
tar -xzvf libgit2-${LG2VER}.tar.gz
cd libgit2-${LG2VER} && mkdir build && cd build
cmake -DTHREADSAFE=ON -DBUILD_CLAR=OFF -DCMAKE_BUILD_TYPE="RelWithDebInfo" .. && make && sudo make install
sudo ldconfig
cd "${TRAVIS_BUILD_DIR}"

174
settings.go Normal file
View File

@ -0,0 +1,174 @@
package git
/*
#include <git2.h>
int _go_git_opts_get_search_path(int level, git_buf *buf)
{
return git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, level, buf);
}
int _go_git_opts_set_search_path(int level, const char *path)
{
return git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, level, path);
}
int _go_git_opts_set_size_t(int opt, size_t val)
{
return git_libgit2_opts(opt, val);
}
int _go_git_opts_set_cache_object_limit(git_object_t type, size_t size)
{
return git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, type, size);
}
int _go_git_opts_get_size_t(int opt, size_t *val)
{
return git_libgit2_opts(opt, val);
}
int _go_git_opts_get_size_t_size_t(int opt, size_t *val1, size_t *val2)
{
return git_libgit2_opts(opt, val1, val2);
}
*/
import "C"
import (
"runtime"
"unsafe"
)
func SearchPath(level ConfigLevel) (string, error) {
var buf C.git_buf
defer C.git_buf_dispose(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C._go_git_opts_get_search_path(C.int(level), &buf)
if err < 0 {
return "", MakeGitError(err)
}
return C.GoString(buf.ptr), nil
}
func SetSearchPath(level ConfigLevel, path string) error {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C._go_git_opts_set_search_path(C.int(level), cpath)
if err < 0 {
return MakeGitError(err)
}
return nil
}
func MwindowSize() (int, error) {
return getSizet(C.GIT_OPT_GET_MWINDOW_SIZE)
}
func SetMwindowSize(size int) error {
return setSizet(C.GIT_OPT_SET_MWINDOW_SIZE, size)
}
func MwindowMappedLimit() (int, error) {
return getSizet(C.GIT_OPT_GET_MWINDOW_MAPPED_LIMIT)
}
func SetMwindowMappedLimit(size int) error {
return setSizet(C.GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, size)
}
func EnableCaching(enabled bool) error {
if enabled {
return setSizet(C.GIT_OPT_ENABLE_CACHING, 1)
} else {
return setSizet(C.GIT_OPT_ENABLE_CACHING, 0)
}
}
func EnableStrictHashVerification(enabled bool) error {
if enabled {
return setSizet(C.GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1)
} else {
return setSizet(C.GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0)
}
}
func EnableFsyncGitDir(enabled bool) error {
if enabled {
return setSizet(C.GIT_OPT_ENABLE_FSYNC_GITDIR, 1)
} else {
return setSizet(C.GIT_OPT_ENABLE_FSYNC_GITDIR, 0)
}
}
func CachedMemory() (current int, allowed int, err error) {
return getSizetSizet(C.GIT_OPT_GET_CACHED_MEMORY)
}
// deprecated: You should use `CachedMemory()` instead.
func GetCachedMemory() (current int, allowed int, err error) {
return CachedMemory()
}
func SetCacheMaxSize(maxSize int) error {
return setSizet(C.GIT_OPT_SET_CACHE_MAX_SIZE, maxSize)
}
func SetCacheObjectLimit(objectType ObjectType, size int) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C._go_git_opts_set_cache_object_limit(C.git_object_t(objectType), C.size_t(size))
if err < 0 {
return MakeGitError(err)
}
return nil
}
func getSizet(opt C.int) (int, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var val C.size_t
err := C._go_git_opts_get_size_t(opt, &val)
if err < 0 {
return 0, MakeGitError(err)
}
return int(val), nil
}
func getSizetSizet(opt C.int) (int, int, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var val1, val2 C.size_t
err := C._go_git_opts_get_size_t_size_t(opt, &val1, &val2)
if err < 0 {
return 0, 0, MakeGitError(err)
}
return int(val1), int(val2), nil
}
func setSizet(opt C.int, val int) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cval := C.size_t(val)
err := C._go_git_opts_set_size_t(opt, cval)
if err < 0 {
return MakeGitError(err)
}
return nil
}

99
settings_test.go Normal file
View File

@ -0,0 +1,99 @@
package git
import (
"testing"
)
type pathPair struct {
Level ConfigLevel
Path string
}
func TestSearchPath(t *testing.T) {
paths := []pathPair{
pathPair{ConfigLevelSystem, "/tmp/system"},
pathPair{ConfigLevelGlobal, "/tmp/global"},
pathPair{ConfigLevelXDG, "/tmp/xdg"},
}
for _, pair := range paths {
err := SetSearchPath(pair.Level, pair.Path)
checkFatal(t, err)
actual, err := SearchPath(pair.Level)
checkFatal(t, err)
if pair.Path != actual {
t.Fatal("Search paths don't match")
}
}
}
func TestMmapSizes(t *testing.T) {
size := 42 * 1024
err := SetMwindowSize(size)
checkFatal(t, err)
actual, err := MwindowSize()
if size != actual {
t.Fatal("Sizes don't match")
}
err = SetMwindowMappedLimit(size)
checkFatal(t, err)
actual, err = MwindowMappedLimit()
if size != actual {
t.Fatal("Sizes don't match")
}
}
func TestEnableCaching(t *testing.T) {
err := EnableCaching(false)
checkFatal(t, err)
err = EnableCaching(true)
checkFatal(t, err)
}
func TestEnableStrictHashVerification(t *testing.T) {
err := EnableStrictHashVerification(false)
checkFatal(t, err)
err = EnableStrictHashVerification(true)
checkFatal(t, err)
}
func TestEnableFsyncGitDir(t *testing.T) {
err := EnableFsyncGitDir(false)
checkFatal(t, err)
err = EnableFsyncGitDir(true)
checkFatal(t, err)
}
func TestCachedMemory(t *testing.T) {
current, allowed, err := CachedMemory()
checkFatal(t, err)
if current < 0 {
t.Fatal("current < 0")
}
if allowed < 0 {
t.Fatal("allowed < 0")
}
}
func TestSetCacheMaxSize(t *testing.T) {
err := SetCacheMaxSize(0)
checkFatal(t, err)
err = SetCacheMaxSize(1024 * 1024)
checkFatal(t, err)
// revert to default 256MB
err = SetCacheMaxSize(256 * 1024 * 1024)
checkFatal(t, err)
}

78
signature.go Normal file
View File

@ -0,0 +1,78 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"time"
"unsafe"
)
type Signature struct {
Name string
Email string
When time.Time
}
func newSignatureFromC(sig *C.git_signature) *Signature {
if sig == nil {
return nil
}
// git stores minutes, go wants seconds
loc := time.FixedZone("", int(sig.when.offset)*60)
return &Signature{
C.GoString(sig.name),
C.GoString(sig.email),
time.Unix(int64(sig.when.time), 0).In(loc),
}
}
// Offset returns the time zone offset of v.When in minutes, which is what git wants.
func (v *Signature) Offset() int {
_, offset := v.When.Zone()
return offset / 60
}
func (sig *Signature) toC() (*C.git_signature, error) {
if sig == nil {
return nil, nil
}
var out *C.git_signature
runtime.LockOSThread()
defer runtime.UnlockOSThread()
name := C.CString(sig.Name)
defer C.free(unsafe.Pointer(name))
email := C.CString(sig.Email)
defer C.free(unsafe.Pointer(email))
ret := C.git_signature_new(&out, name, email, C.git_time_t(sig.When.Unix()), C.int(sig.Offset()))
if ret < 0 {
return nil, MakeGitError(ret)
}
return out, nil
}
func (repo *Repository) DefaultSignature() (*Signature, error) {
var out *C.git_signature
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cErr := C.git_signature_default(&out, repo.ptr)
runtime.KeepAlive(repo)
if cErr < 0 {
return nil, MakeGitError(cErr)
}
defer C.git_signature_free(out)
return newSignatureFromC(out), nil
}

250
ssh.go Normal file
View File

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

337
stash.go Normal file
View File

@ -0,0 +1,337 @@
package git
/*
#include <git2.h>
extern void _go_git_populate_stash_apply_callbacks(git_stash_apply_options *opts);
extern int _go_git_stash_foreach(git_repository *repo, void *payload);
*/
import "C"
import (
"runtime"
"unsafe"
)
// StashFlag are flags that affect the stash save operation.
type StashFlag int
const (
// StashDefault represents no option, default.
StashDefault StashFlag = C.GIT_STASH_DEFAULT
// StashKeepIndex leaves all changes already added to the
// index intact in the working directory.
StashKeepIndex StashFlag = C.GIT_STASH_KEEP_INDEX
// StashIncludeUntracked means all untracked files are also
// stashed and then cleaned up from the working directory.
StashIncludeUntracked StashFlag = C.GIT_STASH_INCLUDE_UNTRACKED
// StashIncludeIgnored means all ignored files are also
// stashed and then cleaned up from the working directory.
StashIncludeIgnored StashFlag = C.GIT_STASH_INCLUDE_IGNORED
)
// StashCollection represents the possible operations that can be
// performed on the collection of stashes for a repository.
type StashCollection struct {
doNotCompare
repo *Repository
}
// Save saves the local modifications to a new stash.
//
// Stasher is the identity of the person performing the stashing.
// Message is the optional description along with the stashed state.
// Flags control the stashing process and are given as bitwise OR.
func (c *StashCollection) Save(
stasher *Signature, message string, flags StashFlag) (*Oid, error) {
oid := new(Oid)
stasherC, err := stasher.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(stasherC)
messageC := C.CString(message)
defer C.free(unsafe.Pointer(messageC))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_stash_save(
oid.toC(), c.repo.ptr,
stasherC, messageC, C.uint32_t(flags))
runtime.KeepAlive(c)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}
// StashApplyFlag are flags that affect the stash apply operation.
type StashApplyFlag int
const (
// StashApplyDefault is the default.
StashApplyDefault StashApplyFlag = C.GIT_STASH_APPLY_DEFAULT
// StashApplyReinstateIndex will try to reinstate not only the
// working tree's changes, but also the index's changes.
StashApplyReinstateIndex StashApplyFlag = C.GIT_STASH_APPLY_REINSTATE_INDEX
)
// StashApplyProgress are flags describing the progress of the apply operation.
type StashApplyProgress int
const (
// StashApplyProgressNone means loading the stashed data from the object store.
StashApplyProgressNone StashApplyProgress = C.GIT_STASH_APPLY_PROGRESS_NONE
// StashApplyProgressLoadingStash means the stored index is being analyzed.
StashApplyProgressLoadingStash StashApplyProgress = C.GIT_STASH_APPLY_PROGRESS_LOADING_STASH
// StashApplyProgressAnalyzeIndex means the stored index is being analyzed.
StashApplyProgressAnalyzeIndex StashApplyProgress = C.GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX
// StashApplyProgressAnalyzeModified means the modified files are being analyzed.
StashApplyProgressAnalyzeModified StashApplyProgress = C.GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED
// StashApplyProgressAnalyzeUntracked means the untracked and ignored files are being analyzed.
StashApplyProgressAnalyzeUntracked StashApplyProgress = C.GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED
// StashApplyProgressCheckoutUntracked means the untracked files are being written to disk.
StashApplyProgressCheckoutUntracked StashApplyProgress = C.GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED
// StashApplyProgressCheckoutModified means the modified files are being written to disk.
StashApplyProgressCheckoutModified StashApplyProgress = C.GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED
// StashApplyProgressDone means the stash was applied successfully.
StashApplyProgressDone StashApplyProgress = C.GIT_STASH_APPLY_PROGRESS_DONE
)
// StashApplyProgressCallback is the apply operation notification callback.
type StashApplyProgressCallback func(progress StashApplyProgress) error
type stashApplyProgressCallbackData struct {
callback StashApplyProgressCallback
errorTarget *error
}
//export stashApplyProgressCallback
func stashApplyProgressCallback(progress C.git_stash_apply_progress_t, handle unsafe.Pointer) C.int {
payload := pointerHandles.Get(handle)
data, ok := payload.(*stashApplyProgressCallbackData)
if !ok {
panic("could not retrieve data for handle")
}
if data == nil || data.callback == nil {
return C.int(ErrorCodeOK)
}
err := data.callback(StashApplyProgress(progress))
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
// StashApplyOptions represents options to control the apply operation.
type StashApplyOptions struct {
Flags StashApplyFlag
CheckoutOptions CheckoutOptions // options to use when writing files to the working directory
ProgressCallback StashApplyProgressCallback // optional callback to notify the consumer of application progress
}
// DefaultStashApplyOptions initializes the structure with default values.
func DefaultStashApplyOptions() (StashApplyOptions, error) {
optsC := C.git_stash_apply_options{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_stash_apply_options_init(&optsC, C.GIT_STASH_APPLY_OPTIONS_VERSION)
if ecode < 0 {
return StashApplyOptions{}, MakeGitError(ecode)
}
return StashApplyOptions{
Flags: StashApplyFlag(optsC.flags),
CheckoutOptions: checkoutOptionsFromC(&optsC.checkout_options),
}, nil
}
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)
if opts == nil {
return nil
}
copts.flags = C.uint32_t(opts.Flags)
populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget)
if opts.ProgressCallback != nil {
progressData := &stashApplyProgressCallbackData{
callback: opts.ProgressCallback,
errorTarget: errorTarget,
}
C._go_git_populate_stash_apply_callbacks(copts)
copts.progress_payload = pointerHandles.Track(progressData)
}
return copts
}
func freeStashApplyOptions(copts *C.git_stash_apply_options) {
if copts == nil {
return
}
if copts.progress_payload != nil {
pointerHandles.Untrack(copts.progress_payload)
}
freeCheckoutOptions(&copts.checkout_options)
}
// Apply applies a single stashed state from the stash list.
//
// If local changes in the working directory conflict with changes in the
// stash then ErrorCodeConflict will be returned. In this case, the index
// will always remain unmodified and all files in the working directory will
// remain unmodified. However, if you are restoring untracked files or
// ignored files and there is a conflict when applying the modified files,
// then those files will remain in the working directory.
//
// If passing the StashApplyReinstateIndex flag and there would be conflicts
// when reinstating the index, the function will return ErrorCodeConflict
// and both the working directory and index will be left unmodified.
//
// Note that a minimum checkout strategy of 'CheckoutSafe' is implied.
//
// 'index' is the position within the stash list. 0 points to the most
// recent stashed state.
//
// Returns error code ErrorCodeNotFound if there's no stashed state for the given
// index, error code ErrorCodeConflict if local changes in the working directory
// conflict with changes in the stash, the user returned error from the
// StashApplyProgressCallback, if any, or other error code.
//
// Error codes can be interogated with IsErrorCode(err, ErrorCodeNotFound).
func (c *StashCollection) Apply(index int, opts StashApplyOptions) error {
var err error
optsC := populateStashApplyOptions(&C.git_stash_apply_options{}, &opts, &err)
defer freeStashApplyOptions(optsC)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_stash_apply(c.repo.ptr, C.size_t(index), optsC)
runtime.KeepAlive(c)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// StashCallback is called per entry when interating over all
// the stashed states.
//
// 'index' is the position of the current stash in the stash list,
// 'message' is the message used when creating the stash and 'id'
// is the commit id of the stash.
type StashCallback func(index int, message string, id *Oid) error
type stashCallbackData struct {
callback StashCallback
errorTarget *error
}
//export stashForeachCallback
func stashForeachCallback(index C.size_t, message *C.char, id *C.git_oid, handle unsafe.Pointer) C.int {
payload := pointerHandles.Get(handle)
data, ok := payload.(*stashCallbackData)
if !ok {
panic("could not retrieve data for handle")
}
err := data.callback(int(index), C.GoString(message), newOidFromC(id))
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
// Foreach loops over all the stashed states and calls the callback
// for each one.
//
// If callback returns an error, this will stop looping.
func (c *StashCollection) Foreach(callback StashCallback) error {
var err error
data := stashCallbackData{
callback: callback,
errorTarget: &err,
}
handle := pointerHandles.Track(&data)
defer pointerHandles.Untrack(handle)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C._go_git_stash_foreach(c.repo.ptr, handle)
runtime.KeepAlive(c)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// Drop removes a single stashed state from the stash list.
//
// 'index' is the position within the stash list. 0 points
// to the most recent stashed state.
//
// Returns error code ErrorCodeNotFound if there's no stashed
// state for the given index.
func (c *StashCollection) Drop(index int) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_stash_drop(c.repo.ptr, C.size_t(index))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// Pop applies a single stashed state from the stash list
// and removes it from the list if successful.
//
// 'index' is the position within the stash list. 0 points
// to the most recent stashed state.
//
// 'opts' controls how stashes are applied.
//
// Returns error code ErrorCodeNotFound if there's no stashed
// state for the given index.
func (c *StashCollection) Pop(index int, opts StashApplyOptions) error {
var err error
optsC := populateStashApplyOptions(&C.git_stash_apply_options{}, &opts, &err)
defer freeStashApplyOptions(optsC)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_stash_pop(c.repo.ptr, C.size_t(index), optsC)
runtime.KeepAlive(c)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}

198
stash_test.go Normal file
View File

@ -0,0 +1,198 @@
package git
import (
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"runtime"
"testing"
"time"
)
func TestStash(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
prepareStashRepo(t, repo)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Now(),
}
stash1, err := repo.Stashes.Save(sig, "First stash", StashDefault)
checkFatal(t, err)
_, err = repo.LookupCommit(stash1)
checkFatal(t, err)
b, err := ioutil.ReadFile(pathInRepo(repo, "README"))
checkFatal(t, err)
if string(b) == "Update README goes to stash\n" {
t.Errorf("README still contains the uncommitted changes")
}
if !fileExistsInRepo(repo, "untracked.txt") {
t.Errorf("untracked.txt doesn't exist in the repo; should be untracked")
}
// Apply: default
opts, err := DefaultStashApplyOptions()
checkFatal(t, err)
err = repo.Stashes.Apply(0, opts)
checkFatal(t, err)
b, err = ioutil.ReadFile(pathInRepo(repo, "README"))
checkFatal(t, err)
if string(b) != "Update README goes to stash\n" {
t.Errorf("README changes aren't here")
}
// Apply: no stash for the given index
err = repo.Stashes.Apply(1, opts)
if !IsErrorCode(err, ErrorCodeNotFound) {
t.Errorf("expecting GIT_ENOTFOUND error code %d, got %v", ErrorCodeNotFound, err)
}
// Apply: callback stopped
opts.ProgressCallback = func(progress StashApplyProgress) error {
if progress == StashApplyProgressCheckoutModified {
return fmt.Errorf("Stop")
}
return nil
}
err = repo.Stashes.Apply(0, opts)
if err.Error() != "Stop" {
t.Errorf("expecting error 'Stop', got %v", err)
}
// Create second stash with ignored files
os.MkdirAll(pathInRepo(repo, "tmp"), os.ModeDir|os.ModePerm)
err = ioutil.WriteFile(pathInRepo(repo, "tmp/ignored.txt"), []byte("Ignore me\n"), 0644)
checkFatal(t, err)
stash2, err := repo.Stashes.Save(sig, "Second stash", StashIncludeIgnored)
checkFatal(t, err)
if fileExistsInRepo(repo, "tmp/ignored.txt") {
t.Errorf("tmp/ignored.txt should not exist anymore in the work dir")
}
// Stash foreach
expected := []stash{
{0, "On master: Second stash", stash2.String()},
{1, "On master: First stash", stash1.String()},
}
checkStashes(t, repo, expected)
// Stash pop
opts, _ = DefaultStashApplyOptions()
err = repo.Stashes.Pop(1, opts)
checkFatal(t, err)
b, err = ioutil.ReadFile(pathInRepo(repo, "README"))
checkFatal(t, err)
if string(b) != "Update README goes to stash\n" {
t.Errorf("README changes aren't here")
}
expected = []stash{
{0, "On master: Second stash", stash2.String()},
}
checkStashes(t, repo, expected)
// Stash drop
err = repo.Stashes.Drop(0)
checkFatal(t, err)
expected = []stash{}
checkStashes(t, repo, expected)
}
type stash struct {
index int
msg string
id string
}
func checkStashes(t *testing.T, repo *Repository, expected []stash) {
var actual []stash
repo.Stashes.Foreach(func(index int, msg string, id *Oid) error {
stash := stash{index, msg, id.String()}
if len(expected) > len(actual) {
if s := expected[len(actual)]; s.id == "" {
stash.id = "" // don't check id
}
}
actual = append(actual, stash)
return nil
})
if len(expected) > 0 && !reflect.DeepEqual(expected, actual) {
// The failure happens at wherever we were called, not here
_, file, line, ok := runtime.Caller(1)
if !ok {
t.Fatalf("Unable to get caller")
}
t.Errorf("%v:%v: expecting %#v\ngot %#v", path.Base(file), line, expected, actual)
}
}
func prepareStashRepo(t *testing.T, repo *Repository) {
seedTestRepo(t, repo)
err := ioutil.WriteFile(pathInRepo(repo, ".gitignore"), []byte("tmp\n"), 0644)
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Now(),
}
idx, err := repo.Index()
checkFatal(t, err)
err = idx.AddByPath(".gitignore")
checkFatal(t, err)
treeID, err := idx.WriteTree()
checkFatal(t, err)
err = idx.Write()
checkFatal(t, err)
currentBranch, err := repo.Head()
checkFatal(t, err)
currentTip, err := repo.LookupCommit(currentBranch.Target())
checkFatal(t, err)
message := "Add .gitignore\n"
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
_, err = repo.CreateCommit("HEAD", sig, sig, message, tree, currentTip)
checkFatal(t, err)
err = ioutil.WriteFile(pathInRepo(repo, "README"), []byte("Update README goes to stash\n"), 0644)
checkFatal(t, err)
err = ioutil.WriteFile(pathInRepo(repo, "untracked.txt"), []byte("Hello, World\n"), 0644)
checkFatal(t, err)
}
func fileExistsInRepo(repo *Repository, name string) bool {
if _, err := os.Stat(pathInRepo(repo, name)); err != nil {
return false
}
return true
}

194
status.go Normal file
View File

@ -0,0 +1,194 @@
package git
/*
#include <git2.h>
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
)
type Status int
const (
StatusCurrent Status = C.GIT_STATUS_CURRENT
StatusIndexNew Status = C.GIT_STATUS_INDEX_NEW
StatusIndexModified Status = C.GIT_STATUS_INDEX_MODIFIED
StatusIndexDeleted Status = C.GIT_STATUS_INDEX_DELETED
StatusIndexRenamed Status = C.GIT_STATUS_INDEX_RENAMED
StatusIndexTypeChange Status = C.GIT_STATUS_INDEX_TYPECHANGE
StatusWtNew Status = C.GIT_STATUS_WT_NEW
StatusWtModified Status = C.GIT_STATUS_WT_MODIFIED
StatusWtDeleted Status = C.GIT_STATUS_WT_DELETED
StatusWtTypeChange Status = C.GIT_STATUS_WT_TYPECHANGE
StatusWtRenamed Status = C.GIT_STATUS_WT_RENAMED
StatusIgnored Status = C.GIT_STATUS_IGNORED
StatusConflicted Status = C.GIT_STATUS_CONFLICTED
)
type StatusEntry struct {
Status Status
HeadToIndex DiffDelta
IndexToWorkdir DiffDelta
}
func statusEntryFromC(statusEntry *C.git_status_entry) StatusEntry {
var headToIndex DiffDelta = DiffDelta{}
var indexToWorkdir DiffDelta = DiffDelta{}
// Based on the libgit2 status example, head_to_index can be null in some cases
if statusEntry.head_to_index != nil {
headToIndex = diffDeltaFromC(statusEntry.head_to_index)
}
if statusEntry.index_to_workdir != nil {
indexToWorkdir = diffDeltaFromC(statusEntry.index_to_workdir)
}
return StatusEntry{
Status: Status(statusEntry.status),
HeadToIndex: headToIndex,
IndexToWorkdir: indexToWorkdir,
}
}
type StatusList struct {
doNotCompare
ptr *C.git_status_list
r *Repository
}
func newStatusListFromC(ptr *C.git_status_list, r *Repository) *StatusList {
if ptr == nil {
return nil
}
statusList := &StatusList{
ptr: ptr,
r: r,
}
runtime.SetFinalizer(statusList, (*StatusList).Free)
return statusList
}
func (statusList *StatusList) Free() {
if statusList.ptr == nil {
return
}
runtime.SetFinalizer(statusList, nil)
C.git_status_list_free(statusList.ptr)
statusList.ptr = nil
}
func (statusList *StatusList) ByIndex(index int) (StatusEntry, error) {
if statusList.ptr == nil {
return StatusEntry{}, ErrInvalid
}
ptr := C.git_status_byindex(statusList.ptr, C.size_t(index))
if ptr == nil {
return StatusEntry{}, errors.New("index out of Bounds")
}
entry := statusEntryFromC(ptr)
runtime.KeepAlive(statusList)
return entry, nil
}
func (statusList *StatusList) EntryCount() (int, error) {
if statusList.ptr == nil {
return -1, ErrInvalid
}
ret := int(C.git_status_list_entrycount(statusList.ptr))
runtime.KeepAlive(statusList)
return ret, nil
}
type StatusOpt int
const (
StatusOptIncludeUntracked StatusOpt = C.GIT_STATUS_OPT_INCLUDE_UNTRACKED
StatusOptIncludeIgnored StatusOpt = C.GIT_STATUS_OPT_INCLUDE_IGNORED
StatusOptIncludeUnmodified StatusOpt = C.GIT_STATUS_OPT_INCLUDE_UNMODIFIED
StatusOptExcludeSubmodules StatusOpt = C.GIT_STATUS_OPT_EXCLUDE_SUBMODULES
StatusOptRecurseUntrackedDirs StatusOpt = C.GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS
StatusOptDisablePathspecMatch StatusOpt = C.GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH
StatusOptRecurseIgnoredDirs StatusOpt = C.GIT_STATUS_OPT_RECURSE_IGNORED_DIRS
StatusOptRenamesHeadToIndex StatusOpt = C.GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX
StatusOptRenamesIndexToWorkdir StatusOpt = C.GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR
StatusOptSortCaseSensitively StatusOpt = C.GIT_STATUS_OPT_SORT_CASE_SENSITIVELY
StatusOptSortCaseInsensitively StatusOpt = C.GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY
StatusOptRenamesFromRewrites StatusOpt = C.GIT_STATUS_OPT_RENAMES_FROM_REWRITES
StatusOptNoRefresh StatusOpt = C.GIT_STATUS_OPT_NO_REFRESH
StatusOptUpdateIndex StatusOpt = C.GIT_STATUS_OPT_UPDATE_INDEX
)
type StatusShow int
const (
StatusShowIndexAndWorkdir StatusShow = C.GIT_STATUS_SHOW_INDEX_AND_WORKDIR
StatusShowIndexOnly StatusShow = C.GIT_STATUS_SHOW_INDEX_ONLY
StatusShowWorkdirOnly StatusShow = C.GIT_STATUS_SHOW_WORKDIR_ONLY
)
type StatusOptions struct {
Show StatusShow
Flags StatusOpt
Pathspec []string
}
func (v *Repository) StatusList(opts *StatusOptions) (*StatusList, error) {
var ptr *C.git_status_list
var copts *C.git_status_options
if opts != nil {
cpathspec := C.git_strarray{}
if opts.Pathspec != nil {
cpathspec.count = C.size_t(len(opts.Pathspec))
cpathspec.strings = makeCStringsFromStrings(opts.Pathspec)
defer freeStrarray(&cpathspec)
}
copts = &C.git_status_options{
version: C.GIT_STATUS_OPTIONS_VERSION,
show: C.git_status_show_t(opts.Show),
flags: C.uint(opts.Flags),
pathspec: cpathspec,
}
} else {
copts = &C.git_status_options{}
ret := C.git_status_options_init(copts, C.GIT_STATUS_OPTIONS_VERSION)
if ret < 0 {
return nil, MakeGitError(ret)
}
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_status_list_new(&ptr, v.ptr, copts)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newStatusListFromC(ptr, v), nil
}
func (v *Repository) StatusFile(path string) (Status, error) {
var statusFlags C.uint
cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_status_file(&statusFlags, v.ptr, cPath)
runtime.KeepAlive(v)
if ret < 0 {
return 0, MakeGitError(ret)
}
return Status(statusFlags), nil
}

91
status_test.go Normal file
View File

@ -0,0 +1,91 @@
package git
import (
"io/ioutil"
"path"
"testing"
)
func TestStatusFile(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
state := repo.State()
if state != RepositoryStateNone {
t.Fatal("Incorrect repository state: ", state)
}
err := ioutil.WriteFile(path.Join(path.Dir(repo.Workdir()), "hello.txt"), []byte("Hello, World"), 0644)
checkFatal(t, err)
status, err := repo.StatusFile("hello.txt")
checkFatal(t, err)
if status != StatusWtNew {
t.Fatal("Incorrect status flags: ", status)
}
}
func TestStatusList(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
// This commits the test repo README, so it doesn't show up in the status list and there's a head to compare to
seedTestRepo(t, repo)
err := ioutil.WriteFile(path.Join(path.Dir(repo.Workdir()), "hello.txt"), []byte("Hello, World"), 0644)
checkFatal(t, err)
opts := &StatusOptions{}
opts.Show = StatusShowIndexAndWorkdir
opts.Flags = StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively
statusList, err := repo.StatusList(opts)
checkFatal(t, err)
entryCount, err := statusList.EntryCount()
checkFatal(t, err)
if entryCount != 1 {
t.Fatal("Incorrect number of status entries: ", entryCount)
}
entry, err := statusList.ByIndex(0)
checkFatal(t, err)
if entry.Status != StatusWtNew {
t.Fatal("Incorrect status flags: ", entry.Status)
}
if entry.IndexToWorkdir.NewFile.Path != "hello.txt" {
t.Fatal("Incorrect entry path: ", entry.IndexToWorkdir.NewFile.Path)
}
}
func TestStatusNothing(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
opts := &StatusOptions{
Show: StatusShowIndexAndWorkdir,
Flags: StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively,
}
statusList, err := repo.StatusList(opts)
checkFatal(t, err)
entryCount, err := statusList.EntryCount()
checkFatal(t, err)
if entryCount != 0 {
t.Fatal("expected no statuses in empty repo")
}
_, err = statusList.ByIndex(0)
if err == nil {
t.Error("expected error getting status by index")
}
}

View File

@ -1,263 +1,405 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
extern int _go_git_visit_submodule(git_repository *repo, void *fct);
*/
import "C"
import (
"runtime"
"unsafe"
)
// SubmoduleUpdateOptions
type SubmoduleUpdateOptions struct {
CheckoutOptions CheckoutOptions
FetchOptions FetchOptions
}
// Submodule
type Submodule struct {
doNotCompare
ptr *C.git_submodule
r *Repository
}
func newSubmoduleFromC(ptr *C.git_submodule, r *Repository) *Submodule {
s := &Submodule{ptr: ptr, r: r}
runtime.SetFinalizer(s, (*Submodule).Free)
return s
}
func (sub *Submodule) Free() {
runtime.SetFinalizer(sub, nil)
C.git_submodule_free(sub.ptr)
}
type SubmoduleUpdate int
const (
SubmoduleUpdateReset SubmoduleUpdate = C.GIT_SUBMODULE_UPDATE_RESET
SubmoduleUpdateCheckout = C.GIT_SUBMODULE_UPDATE_CHECKOUT
SubmoduleUpdateRebase = C.GIT_SUBMODULE_UPDATE_REBASE
SubmoduleUpdateMerge = C.GIT_SUBMODULE_UPDATE_MERGE
SubmoduleUpdateNone = C.GIT_SUBMODULE_UPDATE_NONE
SubmoduleUpdateCheckout SubmoduleUpdate = C.GIT_SUBMODULE_UPDATE_CHECKOUT
SubmoduleUpdateRebase SubmoduleUpdate = C.GIT_SUBMODULE_UPDATE_REBASE
SubmoduleUpdateMerge SubmoduleUpdate = C.GIT_SUBMODULE_UPDATE_MERGE
SubmoduleUpdateNone SubmoduleUpdate = C.GIT_SUBMODULE_UPDATE_NONE
)
type SubmoduleIgnore int
const (
SubmoduleIgnoreReset SubmoduleIgnore = C.GIT_SUBMODULE_IGNORE_RESET
SubmoduleIgnoreNone = C.GIT_SUBMODULE_IGNORE_NONE
SubmoduleIgnoreUntracked = C.GIT_SUBMODULE_IGNORE_UNTRACKED
SubmoduleIgnoreDirty = C.GIT_SUBMODULE_IGNORE_DIRTY
SubmoduleIgnoreAll = C.GIT_SUBMODULE_IGNORE_ALL
SubmoduleIgnoreNone SubmoduleIgnore = C.GIT_SUBMODULE_IGNORE_NONE
SubmoduleIgnoreUntracked SubmoduleIgnore = C.GIT_SUBMODULE_IGNORE_UNTRACKED
SubmoduleIgnoreDirty SubmoduleIgnore = C.GIT_SUBMODULE_IGNORE_DIRTY
SubmoduleIgnoreAll SubmoduleIgnore = C.GIT_SUBMODULE_IGNORE_ALL
)
type SubmoduleStatus int
const (
SubmoduleStatusInHead SubmoduleStatus = C.GIT_SUBMODULE_STATUS_IN_HEAD
SubmoduleStatusInIndex = C.GIT_SUBMODULE_STATUS_IN_INDEX
SubmoduleStatusInConfig = C.GIT_SUBMODULE_STATUS_IN_CONFIG
SubmoduleStatusInWd = C.GIT_SUBMODULE_STATUS_IN_WD
SubmoduleStatusIndexAdded = C.GIT_SUBMODULE_STATUS_INDEX_ADDED
SubmoduleStatusIndexDeleted = C.GIT_SUBMODULE_STATUS_INDEX_DELETED
SubmoduleStatusIndexModified = C.GIT_SUBMODULE_STATUS_INDEX_MODIFIED
SubmoduleStatusWdUninitialized = C.GIT_SUBMODULE_STATUS_WD_UNINITIALIZED
SubmoduleStatusWdAdded = C.GIT_SUBMODULE_STATUS_WD_ADDED
SubmoduleStatusWdDeleted = C.GIT_SUBMODULE_STATUS_WD_DELETED
SubmoduleStatusWdModified = C.GIT_SUBMODULE_STATUS_WD_MODIFIED
SubmoduleStatusWdIndexModified = C.GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED
SubmoduleStatusWdWdModified = C.GIT_SUBMODULE_STATUS_WD_WD_MODIFIED
SubmoduleStatusWdUntracked = C.GIT_SUBMODULE_STATUS_WD_UNTRACKED
SubmoduleStatusInIndex SubmoduleStatus = C.GIT_SUBMODULE_STATUS_IN_INDEX
SubmoduleStatusInConfig SubmoduleStatus = C.GIT_SUBMODULE_STATUS_IN_CONFIG
SubmoduleStatusInWd SubmoduleStatus = C.GIT_SUBMODULE_STATUS_IN_WD
SubmoduleStatusIndexAdded SubmoduleStatus = C.GIT_SUBMODULE_STATUS_INDEX_ADDED
SubmoduleStatusIndexDeleted SubmoduleStatus = C.GIT_SUBMODULE_STATUS_INDEX_DELETED
SubmoduleStatusIndexModified SubmoduleStatus = C.GIT_SUBMODULE_STATUS_INDEX_MODIFIED
SubmoduleStatusWdUninitialized SubmoduleStatus = C.GIT_SUBMODULE_STATUS_WD_UNINITIALIZED
SubmoduleStatusWdAdded SubmoduleStatus = C.GIT_SUBMODULE_STATUS_WD_ADDED
SubmoduleStatusWdDeleted SubmoduleStatus = C.GIT_SUBMODULE_STATUS_WD_DELETED
SubmoduleStatusWdModified SubmoduleStatus = C.GIT_SUBMODULE_STATUS_WD_MODIFIED
SubmoduleStatusWdIndexModified SubmoduleStatus = C.GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED
SubmoduleStatusWdWdModified SubmoduleStatus = C.GIT_SUBMODULE_STATUS_WD_WD_MODIFIED
SubmoduleStatusWdUntracked SubmoduleStatus = C.GIT_SUBMODULE_STATUS_WD_UNTRACKED
)
type SubmoduleRecurse int
const (
SubmoduleRecurseNo SubmoduleRecurse = C.GIT_SUBMODULE_RECURSE_NO
SubmoduleRecurseYes SubmoduleRecurse = C.GIT_SUBMODULE_RECURSE_YES
SubmoduleRecurseOndemand SubmoduleRecurse = C.GIT_SUBMODULE_RECURSE_ONDEMAND
)
type SubmoduleCollection struct {
doNotCompare
repo *Repository
}
func SubmoduleStatusIsUnmodified(status int) bool {
o := SubmoduleStatus(status) & ^(SubmoduleStatusInHead | SubmoduleStatusInIndex |
SubmoduleStatusInConfig | SubmoduleStatusInWd)
return o == 0
}
func (repo *Repository) LookupSubmodule(name string) (*Submodule, error) {
func (c *SubmoduleCollection) Lookup(name string) (*Submodule, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
sub := new(Submodule)
ret := C.git_submodule_lookup(&sub.ptr, repo.ptr, cname)
var ptr *C.git_submodule
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_submodule_lookup(&ptr, c.repo.ptr, cname)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
return sub, nil
return newSubmoduleFromC(ptr, c.repo), nil
}
type SubmoduleCbk func(sub *Submodule, name string) int
//export SubmoduleVisitor
func SubmoduleVisitor(csub unsafe.Pointer, name string, cfct unsafe.Pointer) int {
sub := &Submodule{(*C.git_submodule)(csub)}
fct := *(*SubmoduleCbk)(cfct)
return fct(sub, name)
// SubmoduleCallback is a function that is called for every submodule found in SubmoduleCollection.Foreach.
type SubmoduleCallback func(sub *Submodule, name string) error
type submoduleCallbackData struct {
callback SubmoduleCallback
errorTarget *error
}
func (repo *Repository) ForeachSubmodule(cbk SubmoduleCbk) error {
ret := C._go_git_visit_submodule(repo.ptr, unsafe.Pointer(&cbk))
if ret < 0 {
return LastError()
//export submoduleCallback
func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int {
sub := &Submodule{ptr: (*C.git_submodule)(csub)}
data, ok := pointerHandles.Get(handle).(submoduleCallbackData)
if !ok {
panic("invalid submodule visitor callback")
}
err := data.callback(sub, C.GoString(name))
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
func (c *SubmoduleCollection) Foreach(callback SubmoduleCallback) error {
var err error
data := submoduleCallbackData{
callback: callback,
errorTarget: &err,
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
handle := pointerHandles.Track(data)
defer pointerHandles.Untrack(handle)
ret := C._go_git_visit_submodule(c.repo.ptr, handle)
runtime.KeepAlive(c)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (repo *Repository) AddSubmodule(url, path string, use_git_link bool) (*Submodule, error) {
func (c *SubmoduleCollection) Add(url, path string, use_git_link bool) (*Submodule, error) {
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
sub := new(Submodule)
ret := C.git_submodule_add_setup(&sub.ptr, repo.ptr, curl, cpath, cbool(use_git_link))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_submodule
ret := C.git_submodule_add_setup(&ptr, c.repo.ptr, curl, cpath, cbool(use_git_link))
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
return sub, nil
return newSubmoduleFromC(ptr, c.repo), nil
}
func (sub *Submodule) FinalizeAdd() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_submodule_add_finalize(sub.ptr)
runtime.KeepAlive(sub)
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (sub *Submodule) AddToIndex(write_index bool) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_submodule_add_to_index(sub.ptr, cbool(write_index))
runtime.KeepAlive(sub)
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (sub *Submodule) Save() error {
ret := C.git_submodule_save(sub.ptr)
if ret < 0 {
return LastError()
}
return nil
}
func (sub *Submodule) Owner() *Repository {
repo := C.git_submodule_owner(sub.ptr)
//FIXME: how to handle dangling references ?
return &Repository{repo}
}
func (sub *Submodule) Name() string {
n := C.git_submodule_name(sub.ptr)
return C.GoString(n)
n := C.GoString(C.git_submodule_name(sub.ptr))
runtime.KeepAlive(sub)
return n
}
func (sub *Submodule) Path() string {
n := C.git_submodule_path(sub.ptr)
return C.GoString(n)
n := C.GoString(C.git_submodule_path(sub.ptr))
runtime.KeepAlive(sub)
return n
}
func (sub *Submodule) Url() string {
n := C.git_submodule_url(sub.ptr)
return C.GoString(n)
n := C.GoString(C.git_submodule_url(sub.ptr))
runtime.KeepAlive(sub)
return n
}
func (sub *Submodule) SetUrl(url string) error {
func (c *SubmoduleCollection) SetUrl(submodule, url string) error {
csubmodule := C.CString(submodule)
defer C.free(unsafe.Pointer(csubmodule))
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
ret := C.git_submodule_set_url(sub.ptr, curl)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_submodule_set_url(c.repo.ptr, csubmodule, curl)
runtime.KeepAlive(c)
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (sub *Submodule) IndexId() *Oid {
var id *Oid
idx := C.git_submodule_index_id(sub.ptr)
if idx == nil {
return nil
if idx != nil {
id = newOidFromC(idx)
}
return newOidFromC(idx)
runtime.KeepAlive(sub)
return id
}
func (sub *Submodule) HeadId() *Oid {
var id *Oid
idx := C.git_submodule_head_id(sub.ptr)
if idx == nil {
return nil
if idx != nil {
id = newOidFromC(idx)
}
return newOidFromC(idx)
runtime.KeepAlive(sub)
return id
}
func (sub *Submodule) WdId() *Oid {
var id *Oid
idx := C.git_submodule_wd_id(sub.ptr)
if idx == nil {
return nil
if idx != nil {
id = newOidFromC(idx)
}
return newOidFromC(idx)
runtime.KeepAlive(sub)
return id
}
func (sub *Submodule) Ignore() SubmoduleIgnore {
o := C.git_submodule_ignore(sub.ptr)
runtime.KeepAlive(sub)
return SubmoduleIgnore(o)
}
func (sub *Submodule) SetIgnore(ignore SubmoduleIgnore) SubmoduleIgnore {
o := C.git_submodule_set_ignore(sub.ptr, C.git_submodule_ignore_t(ignore))
return SubmoduleIgnore(o)
}
func (c *SubmoduleCollection) SetIgnore(submodule string, ignore SubmoduleIgnore) error {
csubmodule := C.CString(submodule)
defer C.free(unsafe.Pointer(csubmodule))
func (sub *Submodule) Update() SubmoduleUpdate {
o := C.git_submodule_update(sub.ptr)
return SubmoduleUpdate(o)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
func (sub *Submodule) SetUpdate(update SubmoduleUpdate) SubmoduleUpdate {
o := C.git_submodule_set_update(sub.ptr, C.git_submodule_update_t(update))
return SubmoduleUpdate(o)
}
func (sub *Submodule) FetchRecurseSubmodules() bool {
if 0 == C.git_submodule_fetch_recurse_submodules(sub.ptr) {
return false
}
return true
}
func (sub *Submodule) SetFetchRecurseSubmodules(v bool) error {
ret := C.git_submodule_set_fetch_recurse_submodules(sub.ptr, cbool(v))
ret := C.git_submodule_set_ignore(c.repo.ptr, csubmodule, C.git_submodule_ignore_t(ignore))
runtime.KeepAlive(c)
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (sub *Submodule) UpdateStrategy() SubmoduleUpdate {
o := C.git_submodule_update_strategy(sub.ptr)
runtime.KeepAlive(sub)
return SubmoduleUpdate(o)
}
func (c *SubmoduleCollection) SetUpdate(submodule string, update SubmoduleUpdate) error {
csubmodule := C.CString(submodule)
defer C.free(unsafe.Pointer(csubmodule))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_submodule_set_update(c.repo.ptr, csubmodule, C.git_submodule_update_t(update))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (sub *Submodule) FetchRecurseSubmodules() SubmoduleRecurse {
return SubmoduleRecurse(C.git_submodule_fetch_recurse_submodules(sub.ptr))
}
func (c *SubmoduleCollection) SetFetchRecurseSubmodules(submodule string, recurse SubmoduleRecurse) error {
csubmodule := C.CString(submodule)
defer C.free(unsafe.Pointer(csubmodule))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_submodule_set_fetch_recurse_submodules(c.repo.ptr, csubmodule, C.git_submodule_recurse_t(recurse))
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(C.int(ret))
}
return nil
}
func (sub *Submodule) Init(overwrite bool) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_submodule_init(sub.ptr, cbool(overwrite))
runtime.KeepAlive(sub)
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (sub *Submodule) Sync() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_submodule_sync(sub.ptr)
runtime.KeepAlive(sub)
if ret < 0 {
return LastError()
return MakeGitError(ret)
}
return nil
}
func (sub *Submodule) Open() (*Repository, error) {
repo := new(Repository)
ret := C.git_submodule_open(&repo.ptr, sub.ptr)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_repository
ret := C.git_submodule_open(&ptr, sub.ptr)
runtime.KeepAlive(sub)
if ret < 0 {
return nil, LastError()
return nil, MakeGitError(ret)
}
return repo, nil
return newRepositoryFromC(ptr), nil
}
func (sub *Submodule) Reload() error {
ret := C.git_submodule_reload(sub.ptr)
if ret < 0 {
return LastError()
func (sub *Submodule) Update(init bool, opts *SubmoduleUpdateOptions) error {
var err error
cOpts := populateSubmoduleUpdateOptions(&C.git_submodule_update_options{}, opts, &err)
defer freeSubmoduleUpdateOptions(cOpts)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_submodule_update(sub.ptr, cbool(init), cOpts)
runtime.KeepAlive(sub)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (repo *Repository) ReloadAllSubmodules() error {
ret := C.git_submodule_reload_all(repo.ptr)
if ret < 0 {
return LastError()
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)
if opts == nil {
return nil
}
return nil
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
populateFetchOptions(&copts.fetch_opts, &opts.FetchOptions, errorTarget)
return copts
}
func freeSubmoduleUpdateOptions(copts *C.git_submodule_update_options) {
if copts == nil {
return
}
freeCheckoutOptions(&copts.checkout_opts)
freeFetchOptions(&copts.fetch_opts)
}

27
submodule_test.go Normal file
View File

@ -0,0 +1,27 @@
package git
import (
"testing"
)
func TestSubmoduleForeach(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
_, err := repo.Submodules.Add("http://example.org/submodule", "submodule", true)
checkFatal(t, err)
i := 0
err = repo.Submodules.Foreach(func(sub *Submodule, name string) error {
i++
return nil
})
checkFatal(t, err)
if i != 1 {
t.Fatalf("expected one submodule found but got %d", i)
}
}

247
tag.go Normal file
View File

@ -0,0 +1,247 @@
package git
/*
#include <git2.h>
extern int _go_git_tag_foreach(git_repository *repo, void *payload);
*/
import "C"
import (
"runtime"
"unsafe"
)
// Tag
type Tag struct {
doNotCompare
Object
cast_ptr *C.git_tag
}
func (t *Tag) AsObject() *Object {
return &t.Object
}
func (t *Tag) Message() string {
ret := C.GoString(C.git_tag_message(t.cast_ptr))
runtime.KeepAlive(t)
return ret
}
func (t *Tag) Name() string {
ret := C.GoString(C.git_tag_name(t.cast_ptr))
runtime.KeepAlive(t)
return ret
}
func (t *Tag) Tagger() *Signature {
cast_ptr := C.git_tag_tagger(t.cast_ptr)
ret := newSignatureFromC(cast_ptr)
runtime.KeepAlive(t)
return ret
}
func (t *Tag) Target() *Object {
var ptr *C.git_object
ret := C.git_tag_target(&ptr, t.cast_ptr)
runtime.KeepAlive(t)
if ret != 0 {
return nil
}
return allocObject(ptr, t.repo)
}
func (t *Tag) TargetId() *Oid {
ret := newOidFromC(C.git_tag_target_id(t.cast_ptr))
runtime.KeepAlive(t)
return ret
}
func (t *Tag) TargetType() ObjectType {
ret := ObjectType(C.git_tag_target_type(t.cast_ptr))
runtime.KeepAlive(t)
return ret
}
type TagsCollection struct {
doNotCompare
repo *Repository
}
func (c *TagsCollection) Create(name string, obj Objecter, tagger *Signature, message string) (*Oid, error) {
oid := new(Oid)
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
cmessage := C.CString(message)
defer C.free(unsafe.Pointer(cmessage))
taggerSig, err := tagger.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(taggerSig)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
o := obj.AsObject()
ret := C.git_tag_create(oid.toC(), c.repo.ptr, cname, o.ptr, taggerSig, cmessage, 0)
runtime.KeepAlive(c)
runtime.KeepAlive(obj)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}
func (c *TagsCollection) Remove(name string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ret := C.git_tag_delete(c.repo.ptr, cname)
runtime.KeepAlive(c)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// CreateLightweight creates a new lightweight tag pointing to an object
// and returns the id of the target object.
//
// The name of the tag is validated for consistency (see git_tag_create() for the rules
// https://libgit2.github.com/libgit2/#HEAD/group/tag/git_tag_create) and should
// not conflict with an already existing tag name.
//
// If force is true and a reference already exists with the given name, it'll be replaced.
//
// The created tag is a simple reference and can be queried using
// repo.References.Lookup("refs/tags/<name>"). The name of the tag (eg "v1.0.0")
// is queried with ref.Shorthand().
func (c *TagsCollection) CreateLightweight(name string, obj Objecter, force bool) (*Oid, error) {
oid := new(Oid)
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
o := obj.AsObject()
err := C.git_tag_create_lightweight(oid.toC(), c.repo.ptr, cname, o.ptr, cbool(force))
runtime.KeepAlive(c)
runtime.KeepAlive(obj)
if err < 0 {
return nil, MakeGitError(err)
}
return oid, nil
}
// List returns the names of all the tags in the repository,
// eg: ["v1.0.1", "v2.0.0"].
func (c *TagsCollection) List() ([]string, error) {
var strC C.git_strarray
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_tag_list(&strC, c.repo.ptr)
runtime.KeepAlive(c)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
defer C.git_strarray_dispose(&strC)
tags := makeStringsFromCStrings(strC.strings, int(strC.count))
return tags, nil
}
// ListWithMatch returns the names of all the tags in the repository
// that match a given pattern.
//
// The pattern is a standard fnmatch(3) pattern http://man7.org/linux/man-pages/man3/fnmatch.3.html
func (c *TagsCollection) ListWithMatch(pattern string) ([]string, error) {
var strC C.git_strarray
patternC := C.CString(pattern)
defer C.free(unsafe.Pointer(patternC))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_tag_list_match(&strC, patternC, c.repo.ptr)
runtime.KeepAlive(c)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
defer C.git_strarray_dispose(&strC)
tags := makeStringsFromCStrings(strC.strings, int(strC.count))
return tags, nil
}
// TagForeachCallback is called for each tag in the repository.
//
// The name is the full ref name eg: "refs/tags/v1.0.0".
//
// Note that the callback is called for lightweight tags as well,
// so repo.LookupTag() will return an error for these tags. Use
// repo.References.Lookup() instead.
type TagForeachCallback func(name string, id *Oid) error
type tagForeachCallbackData struct {
callback TagForeachCallback
errorTarget *error
}
//export tagForeachCallback
func tagForeachCallback(name *C.char, id *C.git_oid, handle unsafe.Pointer) C.int {
payload := pointerHandles.Get(handle)
data, ok := payload.(*tagForeachCallbackData)
if !ok {
panic("could not retrieve tag foreach CB handle")
}
err := data.callback(C.GoString(name), newOidFromC(id))
if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
return C.int(ErrorCodeOK)
}
// Foreach calls the callback for each tag in the repository.
func (c *TagsCollection) Foreach(callback TagForeachCallback) error {
var err error
data := tagForeachCallbackData{
callback: callback,
errorTarget: &err,
}
handle := pointerHandles.Track(&data)
defer pointerHandles.Untrack(handle)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C._go_git_tag_foreach(c.repo.ptr, handle)
runtime.KeepAlive(c)
if ret == C.int(ErrorCodeUser) && err != nil {
return err
}
if ret < 0 {
return MakeGitError(ret)
}
return nil
}

205
tag_test.go Normal file
View File

@ -0,0 +1,205 @@
package git
import (
"errors"
"testing"
"time"
)
func TestCreateTag(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitId, _ := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitId)
checkFatal(t, err)
tagId := createTestTag(t, repo, commit)
tag, err := repo.LookupTag(tagId)
checkFatal(t, err)
compareStrings(t, "v0.0.0", tag.Name())
compareStrings(t, "This is a tag", tag.Message())
compareStrings(t, commitId.String(), tag.TargetId().String())
}
func TestCreateTagLightweight(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitID, _ := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
tagID, err := repo.Tags.CreateLightweight("v0.1.0", commit, false)
checkFatal(t, err)
_, err = repo.Tags.CreateLightweight("v0.1.0", commit, true)
checkFatal(t, err)
ref, err := repo.References.Lookup("refs/tags/v0.1.0")
checkFatal(t, err)
compareStrings(t, "refs/tags/v0.1.0", ref.Name())
compareStrings(t, "v0.1.0", ref.Shorthand())
compareStrings(t, tagID.String(), commitID.String())
compareStrings(t, commitID.String(), ref.Target().String())
}
func TestListTags(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitID, _ := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
createTag(t, repo, commit, "v1.0.1", "Release v1.0.1")
commitID, _ = updateReadme(t, repo, "Release version 2")
commit, err = repo.LookupCommit(commitID)
checkFatal(t, err)
createTag(t, repo, commit, "v2.0.0", "Release v2.0.0")
expected := []string{
"v1.0.1",
"v2.0.0",
}
actual, err := repo.Tags.List()
checkFatal(t, err)
compareStringList(t, expected, actual)
}
func TestListTagsWithMatch(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitID, _ := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
createTag(t, repo, commit, "v1.0.1", "Release v1.0.1")
commitID, _ = updateReadme(t, repo, "Release version 2")
commit, err = repo.LookupCommit(commitID)
checkFatal(t, err)
createTag(t, repo, commit, "v2.0.0", "Release v2.0.0")
expected := []string{
"v2.0.0",
}
actual, err := repo.Tags.ListWithMatch("v2*")
checkFatal(t, err)
compareStringList(t, expected, actual)
expected = []string{
"v1.0.1",
}
actual, err = repo.Tags.ListWithMatch("v1*")
checkFatal(t, err)
compareStringList(t, expected, actual)
}
func TestTagForeach(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
commitID, _ := seedTestRepo(t, repo)
commit, err := repo.LookupCommit(commitID)
checkFatal(t, err)
tag1 := createTag(t, repo, commit, "v1.0.1", "Release v1.0.1")
commitID, _ = updateReadme(t, repo, "Release version 2")
commit, err = repo.LookupCommit(commitID)
checkFatal(t, err)
tag2 := createTag(t, repo, commit, "v2.0.0", "Release v2.0.0")
expectedNames := []string{
"refs/tags/v1.0.1",
"refs/tags/v2.0.0",
}
actualNames := []string{}
expectedOids := []string{
tag1.String(),
tag2.String(),
}
actualOids := []string{}
err = repo.Tags.Foreach(func(name string, id *Oid) error {
actualNames = append(actualNames, name)
actualOids = append(actualOids, id.String())
return nil
})
checkFatal(t, err)
compareStringList(t, expectedNames, actualNames)
compareStringList(t, expectedOids, actualOids)
fakeErr := errors.New("fake error")
err = repo.Tags.Foreach(func(name string, id *Oid) error {
return fakeErr
})
if err != fakeErr {
t.Fatalf("Tags.Foreach() did not return the expected error, got %v", err)
}
}
func compareStrings(t *testing.T, expected, value string) {
if value != expected {
t.Fatalf("expected '%v', actual '%v'", expected, value)
}
}
func createTestTag(t *testing.T, repo *Repository, commit *Commit) *Oid {
loc, err := time.LoadLocation("Europe/Berlin")
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc),
}
tagId, err := repo.Tags.Create("v0.0.0", commit, sig, "This is a tag")
checkFatal(t, err)
return tagId
}
func createTag(t *testing.T, repo *Repository, commit *Commit, name, message string) *Oid {
loc, err := time.LoadLocation("Europe/Bucharest")
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "random@hacker.com",
When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc),
}
tagId, err := repo.Tags.Create(name, commit, sig, message)
checkFatal(t, err)
return tagId
}

1
testdata/TestGitRepository.git/HEAD vendored Normal file
View File

@ -0,0 +1 @@
ref: refs/heads/master

6
testdata/TestGitRepository.git/config vendored Normal file
View File

@ -0,0 +1,6 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
[remote "origin"]
url = https://github.com/libgit2/TestGitRepository

View File

@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

View File

@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~

View File

@ -0,0 +1,8 @@
0966a434eb1a025db6b71485ab63a3bfbea520b6 refs/heads/first-merge
49322bb17d3acc9146f98c97d078513228bbf3c0 refs/heads/master
42e4e7c5e507e113ebbb7801b16b52cf867b7ce1 refs/heads/no-parent
d96c4e80345534eccee5ac7b07fc7603b56124cb refs/tags/annotated_tag
c070ad8c08840c8116da865b2d65593a6bb9cd2a refs/tags/annotated_tag^{}
55a1a760df4b86a02094a904dfa511deb5655905 refs/tags/blob
8f50ba15d49353813cc6e20298002c0d17b0a9ee refs/tags/commit_tree
6e0c7bdb9b4ed93212491ee778ca1c65047cab4e refs/tags/nearly-dangling

Binary file not shown.

View File

@ -0,0 +1,2 @@
P pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.pack

Some files were not shown because too many files have changed in this diff Show More