From ef798090666344d15138d4e20d3042f65e3da527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 29 May 2018 20:21:28 +0200 Subject: [PATCH 001/103] vendor: update libgit2 to v0.27.1 --- vendor/libgit2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/libgit2 b/vendor/libgit2 index 6311e886d..b0d9952c3 160000 --- a/vendor/libgit2 +++ b/vendor/libgit2 @@ -1 +1 @@ -Subproject commit 6311e886d8b5377c6037cd9937ccf66a71f3361d +Subproject commit b0d9952c318a3d1b8917e06ad46b9110c0c28831 From 7696f18fed7fbb493ec740de1c839345232f8f8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 8 Aug 2018 11:04:32 +0200 Subject: [PATCH 002/103] Bump vendored libgit2 to v0.27.4 --- vendor/libgit2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/libgit2 b/vendor/libgit2 index b0d9952c3..8b89f362a 160000 --- a/vendor/libgit2 +++ b/vendor/libgit2 @@ -1 +1 @@ -Subproject commit b0d9952c318a3d1b8917e06ad46b9110c0c28831 +Subproject commit 8b89f362a34fcccdf1c6c5f3445895b71d9c6d56 From bde7731d51198fe2437fa8a911e4badc99dec5a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sun, 7 Oct 2018 18:48:41 +0200 Subject: [PATCH 003/103] Update vendored libgit2 to v0.27.5 --- vendor/libgit2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/libgit2 b/vendor/libgit2 index 8b89f362a..8e0b17298 160000 --- a/vendor/libgit2 +++ b/vendor/libgit2 @@ -1 +1 @@ -Subproject commit 8b89f362a34fcccdf1c6c5f3445895b71d9c6d56 +Subproject commit 8e0b172981a046d19f1d9efa5acd6186bccbd3ce From e209475ae0259d6b02ed021bcaa219cb9f10a847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Fri, 4 Jan 2019 13:25:56 +0000 Subject: [PATCH 004/103] Update vendored libgit2 to v0.27.7 --- vendor/libgit2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/libgit2 b/vendor/libgit2 index 8e0b17298..f23dc5b29 160000 --- a/vendor/libgit2 +++ b/vendor/libgit2 @@ -1 +1 @@ -Subproject commit 8e0b172981a046d19f1d9efa5acd6186bccbd3ce +Subproject commit f23dc5b29f1394928a940d7ec447f4bfd53dad1f From 43550d0b7f012f4ebab6851de64558a9374905bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 13 Aug 2019 19:24:57 +0200 Subject: [PATCH 005/103] Update vendored libgit2 to v0.27.9 --- vendor/libgit2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/libgit2 b/vendor/libgit2 index f23dc5b29..3828d7afd 160000 --- a/vendor/libgit2 +++ b/vendor/libgit2 @@ -1 +1 @@ -Subproject commit f23dc5b29f1394928a940d7ec447f4bfd53dad1f +Subproject commit 3828d7afdd08b595584048e8e4dab6ddd4506ed1 From 75a20e5aeb9dc813338c7d2b35668eeebc0433e1 Mon Sep 17 00:00:00 2001 From: Dinesh Bolkensteyn Date: Sun, 17 Nov 2019 09:19:36 +0100 Subject: [PATCH 006/103] Fixes #513 - Segfault during tree walk --- tree.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tree.go b/tree.go index 02507d2c9..b309193cf 100644 --- a/tree.go +++ b/tree.go @@ -47,7 +47,7 @@ func newTreeEntry(entry *C.git_tree_entry) *TreeEntry { } } -func (t Tree) EntryByName(filename string) *TreeEntry { +func (t *Tree) EntryByName(filename string) *TreeEntry { cname := C.CString(filename) defer C.free(unsafe.Pointer(cname)) @@ -67,7 +67,7 @@ func (t Tree) EntryByName(filename string) *TreeEntry { // free it, but you must not use it after the Tree is freed. // // Warning: this must examine every entry in the tree, so it is not fast. -func (t Tree) EntryById(id *Oid) *TreeEntry { +func (t *Tree) EntryById(id *Oid) *TreeEntry { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -84,7 +84,7 @@ func (t Tree) EntryById(id *Oid) *TreeEntry { // EntryByPath looks up an entry by its full path, recursing into // deeper trees if necessary (i.e. if there are slashes in the path) -func (t Tree) EntryByPath(path string) (*TreeEntry, error) { +func (t *Tree) EntryByPath(path string) (*TreeEntry, error) { cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) var entry *C.git_tree_entry @@ -102,7 +102,7 @@ func (t Tree) EntryByPath(path string) (*TreeEntry, error) { return newTreeEntry(entry), nil } -func (t Tree) EntryByIndex(index uint64) *TreeEntry { +func (t *Tree) EntryByIndex(index uint64) *TreeEntry { entry := C.git_tree_entry_byindex(t.cast_ptr, C.size_t(index)) if entry == nil { return nil @@ -113,7 +113,7 @@ func (t Tree) EntryByIndex(index uint64) *TreeEntry { return goEntry } -func (t Tree) EntryCount() uint64 { +func (t *Tree) EntryCount() uint64 { num := C.git_tree_entrycount(t.cast_ptr) runtime.KeepAlive(t) return uint64(num) @@ -122,9 +122,8 @@ func (t Tree) EntryCount() uint64 { type TreeWalkCallback func(string, *TreeEntry) int //export CallbackGitTreeWalk -func CallbackGitTreeWalk(_root *C.char, _entry unsafe.Pointer, ptr unsafe.Pointer) C.int { +func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int { root := C.GoString(_root) - entry := (*C.git_tree_entry)(_entry) if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok { return C.int(callback(root, newTreeEntry(entry))) @@ -133,7 +132,7 @@ func CallbackGitTreeWalk(_root *C.char, _entry unsafe.Pointer, ptr unsafe.Pointe } } -func (t Tree) Walk(callback TreeWalkCallback) error { +func (t *Tree) Walk(callback TreeWalkCallback) error { runtime.LockOSThread() defer runtime.UnlockOSThread() From 53ee1f6e9ab29d6aa4acc7306c037948835e69f6 Mon Sep 17 00:00:00 2001 From: Dinesh Bolkensteyn Date: Sun, 17 Nov 2019 17:41:43 +0100 Subject: [PATCH 007/103] Similar to #513 Fix potential segfault on Tag objects --- tag.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tag.go b/tag.go index 4debdb7a2..1bea2b7bf 100644 --- a/tag.go +++ b/tag.go @@ -21,26 +21,26 @@ func (t *Tag) AsObject() *Object { return &t.Object } -func (t Tag) Message() string { +func (t *Tag) Message() string { ret := C.GoString(C.git_tag_message(t.cast_ptr)) runtime.KeepAlive(t) return ret } -func (t Tag) Name() string { +func (t *Tag) Name() string { ret := C.GoString(C.git_tag_name(t.cast_ptr)) runtime.KeepAlive(t) return ret } -func (t Tag) Tagger() *Signature { +func (t *Tag) Tagger() *Signature { cast_ptr := C.git_tag_tagger(t.cast_ptr) ret := newSignatureFromC(cast_ptr) runtime.KeepAlive(t) return ret } -func (t Tag) Target() *Object { +func (t *Tag) Target() *Object { var ptr *C.git_object ret := C.git_tag_target(&ptr, t.cast_ptr) runtime.KeepAlive(t) @@ -51,13 +51,13 @@ func (t Tag) Target() *Object { return allocObject(ptr, t.repo) } -func (t Tag) TargetId() *Oid { +func (t *Tag) TargetId() *Oid { ret := newOidFromC(C.git_tag_target_id(t.cast_ptr)) runtime.KeepAlive(t) return ret } -func (t Tag) TargetType() ObjectType { +func (t *Tag) TargetType() ObjectType { ret := ObjectType(C.git_tag_target_type(t.cast_ptr)) runtime.KeepAlive(t) return ret From f0ad5b44b9dea37dd1abaeb47fba8af469fb9d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 10 Dec 2019 21:36:08 +0000 Subject: [PATCH 008/103] Update libgit2 to v0.27.10 --- vendor/libgit2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/libgit2 b/vendor/libgit2 index 3828d7afd..45c6187cb 160000 --- a/vendor/libgit2 +++ b/vendor/libgit2 @@ -1 +1 @@ -Subproject commit 3828d7afdd08b595584048e8e4dab6ddd4506ed1 +Subproject commit 45c6187cbfdcfee2bcf59a1ed3a853065142f203 From ca9f5b6523c56e1330924e5aa3fd6e2dc2b72366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 31 May 2018 07:26:17 +0200 Subject: [PATCH 009/103] Merge pull request #443 from walkenzoy/master git2go: fix reference iterator leak (cherry picked from commit 14280de4da0f392935854a7cbdd67b2a5505c3a8) --- reference.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/reference.go b/reference.go index 294c2f32e..12ecb74b4 100644 --- a/reference.go +++ b/reference.go @@ -456,10 +456,12 @@ func (v *ReferenceIterator) Next() (*Reference, error) { } func newReferenceIteratorFromC(ptr *C.git_reference_iterator, r *Repository) *ReferenceIterator { - return &ReferenceIterator{ + iter := &ReferenceIterator{ ptr: ptr, repo: r, } + runtime.SetFinalizer(iter, (*ReferenceIterator).Free) + return iter } // Free the reference iterator From 13d27a4f625b01412bc7716adc9bc39d16b08a57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 17 Dec 2018 10:52:34 +0000 Subject: [PATCH 010/103] Merge pull request #468 from wmedlar/patch-1 Fix typo in constant name (cherry picked from commit 8b368063e958f421ca973b49fb71a092b696cf92) --- checkout.go | 1 + 1 file changed, 1 insertion(+) diff --git a/checkout.go b/checkout.go index db3118f15..76d6d6975 100644 --- a/checkout.go +++ b/checkout.go @@ -36,6 +36,7 @@ const ( CheckoutNoRefresh CheckoutStrategy = C.GIT_CHECKOUT_NO_REFRESH // Don't refresh index/config/etc before doing checkout CheckoutSkipUnmerged CheckoutStrategy = C.GIT_CHECKOUT_SKIP_UNMERGED // Allow checkout to skip unmerged files CheckoutUserOurs CheckoutStrategy = C.GIT_CHECKOUT_USE_OURS // For unmerged files, checkout stage 2 from index + CheckoutUseOurs CheckoutStrategy = C.GIT_CHECKOUT_USE_OURS // For unmerged files, checkout stage 2 from index CheckoutUseTheirs CheckoutStrategy = C.GIT_CHECKOUT_USE_THEIRS // For unmerged files, checkout stage 3 from index CheckoutDisablePathspecMatch CheckoutStrategy = C.GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH // Treat pathspec as simple list of exact match file paths CheckoutSkipLockedDirectories CheckoutStrategy = C.GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES // Ignore directories in use, they will be left empty From c74ce46055419fde18eb5af317a1b6cd25a11b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 2 Jan 2019 23:03:07 +0100 Subject: [PATCH 011/103] Merge pull request #470 from lhchavez/fix-odbreadstream-read Return io.EOF on OdbReadStream.Read() (cherry picked from commit 7e9128bd581d2766672bb14ca6f13976a02764ac) --- odb.go | 4 ++++ odb_test.go | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/odb.go b/odb.go index f236fc4dc..fd27363e4 100644 --- a/odb.go +++ b/odb.go @@ -8,6 +8,7 @@ extern void _go_git_odb_backend_free(git_odb_backend *backend); */ import "C" import ( + "io" "reflect" "runtime" "unsafe" @@ -287,6 +288,9 @@ func (stream *OdbReadStream) Read(data []byte) (int, error) { if ret < 0 { return 0, MakeGitError(ret) } + if ret == 0 { + return 0, io.EOF + } header.Len = int(ret) diff --git a/odb_test.go b/odb_test.go index 3d22fc98e..090bdf641 100644 --- a/odb_test.go +++ b/odb_test.go @@ -3,6 +3,7 @@ package git import ( "errors" "io" + "io/ioutil" "testing" ) @@ -26,7 +27,7 @@ func TestOdbReadHeader(t *testing.T) { if err != nil { t.Fatalf("ReadHeader: %v", err) } - + if sz != uint64(len(data)) { t.Errorf("ReadHeader got size %d, want %d", sz, len(data)) } @@ -47,22 +48,29 @@ func TestOdbStream(t *testing.T) { str := "hello, world!" - stream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob) + writeStream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob) checkFatal(t, error) - n, error := io.WriteString(stream, str) + n, error := io.WriteString(writeStream, str) checkFatal(t, error) if n != len(str) { t.Fatalf("Bad write length %v != %v", n, len(str)) } - error = stream.Close() + error = writeStream.Close() checkFatal(t, error) expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f") checkFatal(t, error) - if stream.Id.Cmp(expectedId) != 0 { + if writeStream.Id.Cmp(expectedId) != 0 { t.Fatal("Wrong data written") } + + readStream, error := odb.NewReadStream(&writeStream.Id) + checkFatal(t, error) + data, error := ioutil.ReadAll(readStream) + if str != string(data) { + t.Fatalf("Wrong data read %v != %v", str, string(data)) + } } func TestOdbHash(t *testing.T) { From da10fee49e2be3c5be44750f56da7db64ff55365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 2 Jan 2019 23:13:50 +0100 Subject: [PATCH 012/103] Merge pull request #424 from josharian/sigdoc signature: improve Signature.Offset docs (cherry picked from commit 7197faee7995ed147c09649fb932dce31fab5a64) --- signature.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signature.go b/signature.go index 16964d231..220fe5701 100644 --- a/signature.go +++ b/signature.go @@ -26,7 +26,7 @@ func newSignatureFromC(sig *C.git_signature) *Signature { } } -// the offset in mintes, which is what git wants +// 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 From 10ce50558897d56e86db656b2cd69ed82ddd902a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 2 Jan 2019 23:58:19 +0100 Subject: [PATCH 013/103] Merge pull request #425 from josharian/more-merge-file-flags merge: add missing MergeFileFlag constants (cherry picked from commit e319b9427fec099fcc8986d49a61f66ae1133e0f) --- merge.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/merge.go b/merge.go index adc521a2b..bc672ce12 100644 --- a/merge.go +++ b/merge.go @@ -344,9 +344,29 @@ type MergeFileFlags int const ( MergeFileDefault MergeFileFlags = C.GIT_MERGE_FILE_DEFAULT - MergeFileStyleMerge MergeFileFlags = C.GIT_MERGE_FILE_STYLE_MERGE - MergeFileStyleDiff MergeFileFlags = C.GIT_MERGE_FILE_STYLE_DIFF3 + // 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 { From 13d16e7ac59c538cb58bbf5d657079e4cc4c9e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 3 Jan 2019 16:22:15 +0100 Subject: [PATCH 014/103] Merge pull request #447 from walkenzoy/master git2go: small fixes to odb module (cherry picked from commit fc1230ba16d8f2af3c5e74c4c5b84970f33f1b19) --- odb.go | 26 ++++++++++++++++++-------- odb_test.go | 31 ++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/odb.go b/odb.go index fd27363e4..7ae108a89 100644 --- a/odb.go +++ b/odb.go @@ -81,15 +81,19 @@ func (v *Odb) Exists(oid *Oid) bool { func (v *Odb) Write(data []byte, otype ObjectType) (oid *Oid, err error) { oid = new(Oid) - var cptr unsafe.Pointer - if len(data) > 0 { - cptr = unsafe.Pointer(&data[0]) - } runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_odb_write(oid.toC(), v.ptr, cptr, C.size_t(len(data)), C.git_otype(otype)) + 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_write(oid.toC(), v.ptr, unsafe.Pointer(&data[0]), size, C.git_otype(otype)) runtime.KeepAlive(v) if ret < 0 { return nil, MakeGitError(ret) @@ -165,13 +169,19 @@ func (v *Odb) ForEach(callback OdbForEachCallback) error { // 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) - header := (*reflect.SliceHeader)(unsafe.Pointer(&data)) - ptr := unsafe.Pointer(header.Data) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_odb_hash(oid.toC(), ptr, C.size_t(header.Len), C.git_otype(otype)) + 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_otype(otype)) runtime.KeepAlive(data) if ret < 0 { return nil, MakeGitError(ret) diff --git a/odb_test.go b/odb_test.go index 090bdf641..46acdbaf2 100644 --- a/odb_test.go +++ b/odb_test.go @@ -1,13 +1,14 @@ package git import ( + "bytes" "errors" "io" "io/ioutil" "testing" ) -func TestOdbReadHeader(t *testing.T) { +func TestOdbRead(t *testing.T) { t.Parallel() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -34,6 +35,20 @@ func TestOdbReadHeader(t *testing.T) { 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) { @@ -90,14 +105,16 @@ committer John Doe 1390682018 +0000 Initial commit.` - oid, error := odb.Hash([]byte(str), ObjectCommit) - checkFatal(t, error) + for _, data := range [][]byte{[]byte(str), doublePointerBytes()} { + oid, error := odb.Hash(data, ObjectCommit) + checkFatal(t, error) - coid, error := odb.Write([]byte(str), ObjectCommit) - checkFatal(t, error) + coid, error := odb.Write(data, ObjectCommit) + checkFatal(t, error) - if oid.Cmp(coid) != 0 { - t.Fatal("Hash and write Oids are different") + if oid.Cmp(coid) != 0 { + t.Fatal("Hash and write Oids are different") + } } } From 97d05a1e4c1202f3acd579773f7c7ca457413661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Thu, 3 Jan 2019 23:53:13 +0000 Subject: [PATCH 015/103] Merge pull request #432 from josharian/simplify-oid git: simplify some Oid methods (cherry picked from commit c740e1d83df805f55eda80c76bd1f1c562b3051d) --- git.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/git.go b/git.go index 0925e45d8..897d261f2 100644 --- a/git.go +++ b/git.go @@ -189,22 +189,16 @@ func (oid *Oid) Cmp(oid2 *Oid) int { } func (oid *Oid) Copy() *Oid { - ret := new(Oid) - copy(ret[:], oid[:]) - return ret + ret := *oid + return &ret } func (oid *Oid) Equal(oid2 *Oid) bool { - return bytes.Equal(oid[:], oid2[:]) + return *oid == *oid2 } func (oid *Oid) IsZero() bool { - for _, a := range oid { - if a != 0 { - return false - } - } - return true + return *oid == Oid{} } func (oid *Oid) NCmp(oid2 *Oid, n uint) int { From eb38aaaeeeeeddbd59ef8bc895ad098fe69b428f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Fri, 4 Jan 2019 00:43:48 +0000 Subject: [PATCH 016/103] Merge pull request #463 from Nivl/patch-1 Add index.Clear() to clear the index object (cherry picked from commit c27981c283b5e02fcbbf17cce0a3e6da9339986c) --- index.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.go b/index.go index 5106516ce..dd1346033 100644 --- a/index.go +++ b/index.go @@ -145,6 +145,20 @@ func (v *Index) Path() string { 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 { From d314b459b8de6f41069d8bb825c825650426ca6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Fri, 4 Jan 2019 12:11:05 +0000 Subject: [PATCH 017/103] Merge pull request #465 from lhchavez/packbuilder-insert_from_walk Add support for Packbuilder.InsertFromWalk() (cherry picked from commit b51a90c13329109db0c9ce2303ddf5ad08b3fe5b) --- packbuilder.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packbuilder.go b/packbuilder.go index 0e04bbf5f..576e5ca81 100644 --- a/packbuilder.go +++ b/packbuilder.go @@ -85,6 +85,19 @@ func (pb *Packbuilder) InsertTree(id *Oid) error { 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 { ret := uint32(C.git_packbuilder_object_count(pb.ptr)) runtime.KeepAlive(pb) From a07879739fd7912c8914f06daed60c99de2686a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Fri, 4 Jan 2019 12:18:20 +0000 Subject: [PATCH 018/103] Merge pull request #445 from rmg/exclusive-pkg-config static: use pkg-config exclusively when using it (cherry picked from commit bcf325244c4bfba5a722d1c9269836a3e83ecc73) --- git_static.go | 5 ++--- script/build-libgit2-static.sh | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/git_static.go b/git_static.go index 63037348a..243669ff1 100644 --- a/git_static.go +++ b/git_static.go @@ -3,9 +3,8 @@ package git /* -#cgo CFLAGS: -I${SRCDIR}/vendor/libgit2/include -#cgo LDFLAGS: -L${SRCDIR}/vendor/libgit2/build/ -lgit2 -#cgo windows LDFLAGS: -lwinhttp +#cgo windows CFLAGS: -I${SRCDIR}/vendor/libgit2/include +#cgo windows LDFLAGS: -L${SRCDIR}/vendor/libgit2/build/ -lgit2 -lwinhttp #cgo !windows pkg-config: --static ${SRCDIR}/vendor/libgit2/build/libgit2.pc #include diff --git a/script/build-libgit2-static.sh b/script/build-libgit2-static.sh index 572372195..1568ece1a 100755 --- a/script/build-libgit2-static.sh +++ b/script/build-libgit2-static.sh @@ -16,4 +16,4 @@ cmake -DTHREADSAFE=ON \ -DCMAKE_INSTALL_PREFIX=../install \ .. && -cmake --build . +cmake --build . --target install From 7b1c424572ea6e83b2358ae6aa5a2bf7bf61ac3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 5 Jan 2019 11:05:44 +0000 Subject: [PATCH 019/103] Merge pull request #420 from josharian/rebase-operation-type-stringer Add RebaseOperationReword, and make RebaseOperationType a stringer (cherry picked from commit 7ae106611c9cabe9c3c30343139efbb5d7d6fc27) --- rebase.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rebase.go b/rebase.go index 5206fcae7..d29e18334 100644 --- a/rebase.go +++ b/rebase.go @@ -6,6 +6,7 @@ package git import "C" import ( "errors" + "fmt" "runtime" "unsafe" ) @@ -16,6 +17,8 @@ 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. @@ -26,6 +29,24 @@ const ( 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) From fedb5b8e68c63bd5ecca8c5561ef8e4c9c70ce34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 7 Jan 2019 12:33:43 +0000 Subject: [PATCH 020/103] Merge pull request #476 from lhchavez/clean-up-leaked-dir Clean up one leaked temporary directory (cherry picked from commit e93f34cf186c46cd105b0f58ac2a390cc87fb698) --- reset_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/reset_test.go b/reset_test.go index 45777e468..89ebc49e0 100644 --- a/reset_test.go +++ b/reset_test.go @@ -8,6 +8,8 @@ import ( 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") From a13d27e9c0e1b9eb7ef1e0f5fae73a2129e65b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 7 Jan 2019 14:02:51 +0000 Subject: [PATCH 021/103] Merge pull request #475 from lhchavez/self-contained-build Improve the static build script (cherry picked from commit b30b050c9c8a531c9c58b8b52c83a13b8d149caf) --- .gitignore | 1 + git_static.go | 6 +++--- script/build-libgit2-static.sh | 15 ++++++++------- 3 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..edc18d530 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/static-build/ diff --git a/git_static.go b/git_static.go index 243669ff1..547ae8a33 100644 --- a/git_static.go +++ b/git_static.go @@ -3,9 +3,9 @@ package git /* -#cgo windows CFLAGS: -I${SRCDIR}/vendor/libgit2/include -#cgo windows LDFLAGS: -L${SRCDIR}/vendor/libgit2/build/ -lgit2 -lwinhttp -#cgo !windows pkg-config: --static ${SRCDIR}/vendor/libgit2/build/libgit2.pc +#cgo windows CFLAGS: -I${SRCDIR}/static-build/install/include/ +#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp +#cgo !windows pkg-config: --static ${SRCDIR}/static-build/install/lib/pkgconfig/libgit2.pc #include #if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 27 diff --git a/script/build-libgit2-static.sh b/script/build-libgit2-static.sh index 1568ece1a..680dd93d9 100755 --- a/script/build-libgit2-static.sh +++ b/script/build-libgit2-static.sh @@ -2,18 +2,19 @@ set -ex -VENDORED_PATH=vendor/libgit2 +ROOT="$(cd "$0/../.." && echo "${PWD}")" +BUILD_PATH="${ROOT}/static-build" +VENDORED_PATH="${ROOT}/vendor/libgit2" -cd $VENDORED_PATH && -mkdir -p install/lib && -mkdir -p build && -cd build && +mkdir -p "${BUILD_PATH}/build" "${BUILD_PATH}/install/lib" + +cd "${BUILD_PATH}/build" && cmake -DTHREADSAFE=ON \ -DBUILD_CLAR=OFF \ -DBUILD_SHARED_LIBS=OFF \ -DCMAKE_C_FLAGS=-fPIC \ -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ - -DCMAKE_INSTALL_PREFIX=../install \ - .. && + -DCMAKE_INSTALL_PREFIX="${BUILD_PATH}/install" \ + "${VENDORED_PATH}" && cmake --build . --target install From 9fd0d987ae173eb84cd05780b6dcc7a58961f118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 7 Jan 2019 16:00:59 +0000 Subject: [PATCH 022/103] Merge pull request #477 from lhchavez/patch-1 Add support for Go 1.11 modules (cherry picked from commit 2609f4c6f25a7da56e2e4960c250ea3dfb53e82b) --- go.mod | 1 + 1 file changed, 1 insertion(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 000000000..688b8e3bd --- /dev/null +++ b/go.mod @@ -0,0 +1 @@ +module github.com/libgit2/git2go From c1c2b5a7307c34e3608ae546547d874e2381e0e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 8 Jan 2019 09:30:36 +0000 Subject: [PATCH 023/103] Merge pull request #466 from lhchavez/repository-create_commit_from_ids Add support for CreateCommitFromIds (cherry picked from commit 8766f9f36cfb30934b7133365aa3d1e91086bafa) --- repository.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ repository_test.go | 42 ++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 repository_test.go diff --git a/repository.go b/repository.go index d8de97a6d..2a4e9c815 100644 --- a/repository.go +++ b/repository.go @@ -3,6 +3,8 @@ package git /* #include #include +#include +#include */ import "C" import ( @@ -389,6 +391,74 @@ func (v *Repository) CreateCommit( return oid, 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 +} + func (v *Odb) Free() { runtime.SetFinalizer(v, nil) C.git_odb_free(v.ptr) diff --git a/repository_test.go b/repository_test.go new file mode 100644 index 000000000..1950c6977 --- /dev/null +++ b/repository_test.go @@ -0,0 +1,42 @@ +package git + +import ( + "testing" + "time" +) + +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()) + } +} From 544af2b39a344118656b36e20820dd75b34f8961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 15 Jan 2019 20:46:54 +0100 Subject: [PATCH 024/103] Merge pull request #448 ftrom lhchavez/mempack Add support for mempack (cherry picked from commit 2f91268f7464b21051b4800975a04a1ecde3f559) --- mempack.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ mempack_test.go | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 mempack.go create mode 100644 mempack_test.go diff --git a/mempack.go b/mempack.go new file mode 100644 index 000000000..39d706a1f --- /dev/null +++ b/mempack.go @@ -0,0 +1,84 @@ +package git + +/* +#include +#include + +extern int git_mempack_new(git_odb_backend **out); +extern int git_mempack_dump(git_buf *pack, git_repository *repo, git_odb_backend *backend); +extern void git_mempack_reset(git_odb_backend *backend); +extern 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 { + 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_free(&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() { + C.git_mempack_reset(mempack.ptr) +} diff --git a/mempack_test.go b/mempack_test.go new file mode 100644 index 000000000..3e31dcf9c --- /dev/null +++ b/mempack_test.go @@ -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, ErrNotFound) { + t.Errorf("unexpected error %v", err) + } + } +} From bee1068871f6f9bcf66c5559166290ce5eb8c311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 18 Jun 2019 11:33:28 +0200 Subject: [PATCH 025/103] Merge pull request #512 from codeocean/diff-to-buf Add Diff.ToBuf wrapping git_diff_to_buf (cherry picked from commit 4fa93499429f056b0d7c6e2634b9f17d29df96b3) --- diff.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/diff.go b/diff.go index 308832074..eb893ce3b 100644 --- a/diff.go +++ b/diff.go @@ -405,6 +405,36 @@ func (diff *Diff) Patch(deltaIndex int) (*Patch, error) { return newPatchFromC(patchPtr), nil } +type DiffFormat int + +const ( + DiffFormatPatch DiffFormat = C.GIT_DIFF_FORMAT_PATCH + DiffFormatPatchHeader DiffFormat = C.GIT_DIFF_FORMAT_PATCH_HEADER + DiffFormatRaw DiffFormat = C.GIT_DIFF_FORMAT_RAW + DiffFormatNameOnly DiffFormat = C.GIT_DIFF_FORMAT_NAME_ONLY + DiffFormatNameStatus DiffFormat = C.GIT_DIFF_FORMAT_NAME_STATUS +) + +func (diff *Diff) ToBuf(format DiffFormat) ([]byte, error) { + if diff.ptr == nil { + return nil, ErrInvalid + } + + diffBuf := C.git_buf{} + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_diff_to_buf(&diffBuf, diff.ptr, C.git_diff_format_t(format)) + runtime.KeepAlive(diff) + if ecode < 0 { + return nil, MakeGitError(ecode) + } + defer C.git_buf_free(&diffBuf) + + return C.GoBytes(unsafe.Pointer(diffBuf.ptr), C.int(diffBuf.size)), nil +} + type DiffOptionsFlag int const ( From e888805c2b51662a7119e8069e1d8317617bafb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 18 Jun 2019 11:39:25 +0200 Subject: [PATCH 026/103] Merge pull request #506 from takuji/git_commit_message_encoding Add git_commit_message_encoding support (cherry picked from commit b2e2b2f71bb47ae3d4cfde07b39f524f13d0df93) --- commit.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/commit.go b/commit.go index 223b093c6..6fdfacf7d 100644 --- a/commit.go +++ b/commit.go @@ -28,6 +28,12 @@ func (c *Commit) Message() string { return ret } +func (c *Commit) MessageEncoding() string { + ret := C.GoString(C.git_commit_message_encoding(c.cast_ptr)) + runtime.KeepAlive(c) + return ret +} + func (c *Commit) RawMessage() string { ret := C.GoString(C.git_commit_message_raw(c.cast_ptr)) runtime.KeepAlive(c) From 61fdd76c769e44b410f30144f5ddeb6ec2e33f03 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Wed, 12 Feb 2020 16:57:53 -0800 Subject: [PATCH 027/103] Merge pull request #503 from jonEbird/static-build-script-cleanup script/build-libgit2-static.sh: correctly set ROOT (cherry picked from commit aa802a90db35ddbecd8323910595de5bac040ba0) --- script/build-libgit2-static.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/build-libgit2-static.sh b/script/build-libgit2-static.sh index 680dd93d9..4d89fba9b 100755 --- a/script/build-libgit2-static.sh +++ b/script/build-libgit2-static.sh @@ -2,7 +2,7 @@ set -ex -ROOT="$(cd "$0/../.." && echo "${PWD}")" +ROOT="$(cd "$(dirname "$0")/.." && echo "${PWD}")" BUILD_PATH="${ROOT}/static-build" VENDORED_PATH="${ROOT}/vendor/libgit2" From 2ce0cec363e80d56a732acf1b5a5f657d5802270 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Wed, 12 Feb 2020 17:00:02 -0800 Subject: [PATCH 028/103] Merge pull request #524 from josharian/doc-params provide param names in DiffForEachFileCallback (cherry picked from commit 917d8dcb9efc5dc506123a7ba4f862340d69ce84) --- diff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff.go b/diff.go index eb893ce3b..1046f8d39 100644 --- a/diff.go +++ b/diff.go @@ -284,7 +284,7 @@ type diffForEachData struct { Error error } -type DiffForEachFileCallback func(DiffDelta, float64) (DiffForEachHunkCallback, error) +type DiffForEachFileCallback func(delta DiffDelta, progress float64) (DiffForEachHunkCallback, error) type DiffDetail int From 5bfa93a8ddfc48b66a8a86de4f3015189b32dff1 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Wed, 12 Feb 2020 17:03:52 -0800 Subject: [PATCH 029/103] Merge pull request #523 from josharian/diff-stringers make Delta and DiffLineType stringers (cherry picked from commit 11506ab07032a7956e7e120ca4095b60b77a75b3) --- delta_string.go | 33 +++++++++++++++++++++++++ diff.go | 4 +++ difflinetype_string.go | 56 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 delta_string.go create mode 100644 difflinetype_string.go diff --git a/delta_string.go b/delta_string.go new file mode 100644 index 000000000..53e02bd2d --- /dev/null +++ b/delta_string.go @@ -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]] +} diff --git a/diff.go b/diff.go index 1046f8d39..6be8f5ba8 100644 --- a/diff.go +++ b/diff.go @@ -39,6 +39,8 @@ const ( DeltaConflicted Delta = C.GIT_DELTA_CONFLICTED ) +//go:generate stringer -type Delta -trimprefix Delta -tags static + type DiffLineType int const ( @@ -54,6 +56,8 @@ const ( DiffLineBinary DiffLineType = C.GIT_DIFF_LINE_BINARY ) +//go:generate stringer -type DiffLineType -trimprefix DiffLine -tags static + type DiffFile struct { Path string Oid *Oid diff --git a/difflinetype_string.go b/difflinetype_string.go new file mode 100644 index 000000000..3c1ad585d --- /dev/null +++ b/difflinetype_string.go @@ -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) + ")" + } +} From 15641667bdee714cddeb0d83f040bcbcb95422a7 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Wed, 12 Feb 2020 18:58:58 -0800 Subject: [PATCH 030/103] Merge pull request #520 from libgit2/actions Setup CI via Actions (cherry picked from commit f21ecd9e74314585b26c08ddb6ea674d7b0bcca4) --- .github/workflows/ci.yml | 31 +++++++++++++++++++++++++++++++ remote.go | 3 ++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..d058ec6fc --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,31 @@ +name: git2go CI +on: + pull_request: + push: + branches: + - master + - v* + +jobs: + + build: + strategy: + fail-fast: false + matrix: + go: [ '1.9', '1.10', '1.11', '1.12' , '1.13'] + name: Go ${{ matrix.go }} + + runs-on: ubuntu-18.04 + + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: ${{ matrix.go }} + id: go + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + - name: Build + run: | + git submodule update --init + make test-static diff --git a/remote.go b/remote.go index b4b1dd727..6765e45b8 100644 --- a/remote.go +++ b/remote.go @@ -1,9 +1,10 @@ package git /* -#include #include +#include + extern void _go_git_setup_callbacks(git_remote_callbacks *callbacks); */ From b6aa16143be4bcd5d5e7d6f310c9c6daa3d9ec62 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 22 Feb 2020 23:07:08 +0000 Subject: [PATCH 031/103] Update CI configuration This change: * Updates the GitHub actions so that they run different commands for the dynamic and static flavors of libgit2. * Updates the .travis.yml file so that it does roughly the same as the GitHub actions. * Adds the release-* branches to the CI configurations. (cherry picked from commit 26edffd5f57618d2927926fde4c4ac1fcba5d84a) --- .github/workflows/ci.yml | 64 +++++++++++++++++++++++++++++++-- .gitignore | 1 + .travis.yml | 23 +++++++----- Makefile | 51 +++++++++++++++++++++----- script/build-libgit2-dynamic.sh | 5 +++ script/build-libgit2-static.sh | 19 ++-------- script/build-libgit2.sh | 45 +++++++++++++++++++++++ 7 files changed, 171 insertions(+), 37 deletions(-) create mode 100755 script/build-libgit2-dynamic.sh create mode 100755 script/build-libgit2.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d058ec6fc..931cc09c1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,15 +4,16 @@ on: push: branches: - master + - release-* - v* jobs: - build: + build-legacy: strategy: fail-fast: false matrix: - go: [ '1.9', '1.10', '1.11', '1.12' , '1.13'] + go: [ '1.9', '1.10' ] name: Go ${{ matrix.go }} runs-on: ubuntu-18.04 @@ -23,9 +24,66 @@ jobs: with: go-version: ${{ matrix.go }} id: go + - name: Check out code into the GOPATH + uses: actions/checkout@v1 + with: + fetch-depth: 1 + path: src/github.com/${{ github.repository }} + - name: Build + env: + GOPATH: /home/runner/work/git2go + run: | + git submodule update --init + make build-libgit2-static + go get --tags "static" github.com/${{ github.repository }}/... + go build --tags "static" github.com/${{ github.repository }}/... + - name: Test + env: + GOPATH: /home/runner/work/git2go + run: make test-static + + build-static: + strategy: + fail-fast: false + matrix: + go: [ '1.11', '1.12', '1.13' ] + name: Go ${{ matrix.go }} + + runs-on: ubuntu-18.04 + + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: ${{ matrix.go }} + id: go + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + - name: Build + run: | + git submodule update --init + make build-libgit2-static + - name: Test + run: make test-static + + build-dynamic: + strategy: + fail-fast: false + name: Go (dynamic) + + runs-on: ubuntu-18.04 + + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: '1.13' + id: go - name: Check out code into the Go module directory uses: actions/checkout@v1 - name: Build run: | git submodule update --init - make test-static + make build-libgit2-dynamic + - name: Test + run: make test-dynamic diff --git a/.gitignore b/.gitignore index edc18d530..713781b00 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /static-build/ +/dynamic-build/ diff --git a/.travis.yml b/.travis.yml index f611cc3e6..c89cb5ce6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,26 +1,31 @@ language: go go: - - 1.7 - - 1.8 - - 1.9 + - "1.7" + - "1.8" + - "1.9" - "1.10" + - "1.11" + - "1.12" + - "1.13" - tip -script: make test-static +install: + - make build-libgit2-static + - go get --tags "static" ./... + +script: + - make test-static matrix: allow_failures: - go: tip git: - submodules: false - -before_install: - - git submodule update --init + submodules: true branches: only: - master - /v\d+/ - - next + - /release-.*/ diff --git a/Makefile b/Makefile index cf00ceff1..182c53e33 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,53 @@ default: test -test: build-libgit2 +# System library +# ============== +# This uses whatever version of libgit2 can be found in the system. +test: go run script/check-MakeGitError-thread-lock.go - go test ./... + go test --count=1 ./... -install: build-libgit2 +install: go install ./... -build-libgit2: +# Bundled dynamic library +# ======================= +# In order to avoid having to manipulate `git_dynamic.go`, which would prevent +# the system-wide libgit2.so from being used in a sort of ergonomic way, this +# instead moves the complexity of overriding the paths so that the built +# libraries can be found by the build and tests. +.PHONY: build-libgit2-dynamic +build-libgit2-dynamic: + ./script/build-libgit2-dynamic.sh + +dynamic-build/install/lib/libgit2.so: + ./script/build-libgit2-dynamic.sh + +test-dynamic: dynamic-build/install/lib/libgit2.so + PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \ + go run script/check-MakeGitError-thread-lock.go + PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \ + LD_LIBRARY_PATH=dynamic-build/install/lib \ + go test --count=1 ./... + +install-dynamic: dynamic-build/install/lib/libgit2.so + PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \ + go install ./... + +# Bundled static library +# ====================== +# This is mostly used in tests, but can also be used to provide a +# statically-linked library with the bundled version of libgit2. +.PHONY: build-libgit2-static +build-libgit2-static: ./script/build-libgit2-static.sh -install-static: build-libgit2 - go install --tags "static" ./... +static-build/install/lib/libgit2.a: + ./script/build-libgit2-static.sh -test-static: build-libgit2 +test-static: static-build/install/lib/libgit2.a go run script/check-MakeGitError-thread-lock.go - go test --tags "static" ./... + go test --count=1 --tags "static" ./... + +install-static: static-build/install/lib/libgit2.a + go install --tags "static" ./... diff --git a/script/build-libgit2-dynamic.sh b/script/build-libgit2-dynamic.sh new file mode 100755 index 000000000..af037f3b2 --- /dev/null +++ b/script/build-libgit2-dynamic.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -e + +exec "$(dirname "$0")/build-libgit2.sh" --dynamic diff --git a/script/build-libgit2-static.sh b/script/build-libgit2-static.sh index 4d89fba9b..1d288988d 100755 --- a/script/build-libgit2-static.sh +++ b/script/build-libgit2-static.sh @@ -1,20 +1,5 @@ #!/bin/sh -set -ex +set -e -ROOT="$(cd "$(dirname "$0")/.." && echo "${PWD}")" -BUILD_PATH="${ROOT}/static-build" -VENDORED_PATH="${ROOT}/vendor/libgit2" - -mkdir -p "${BUILD_PATH}/build" "${BUILD_PATH}/install/lib" - -cd "${BUILD_PATH}/build" && -cmake -DTHREADSAFE=ON \ - -DBUILD_CLAR=OFF \ - -DBUILD_SHARED_LIBS=OFF \ - -DCMAKE_C_FLAGS=-fPIC \ - -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ - -DCMAKE_INSTALL_PREFIX="${BUILD_PATH}/install" \ - "${VENDORED_PATH}" && - -cmake --build . --target install +exec "$(dirname "$0")/build-libgit2.sh" --static diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh new file mode 100755 index 000000000..fbb05abda --- /dev/null +++ b/script/build-libgit2.sh @@ -0,0 +1,45 @@ +#!/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 + +if [ "$#" -eq "0" ]; then + echo "Usage: $0 <--dynamic|--static>">&2 + exit 1 +fi + +ROOT="$(cd "$(dirname "$0")/.." && echo "${PWD}")" +VENDORED_PATH="${ROOT}/vendor/libgit2" + +case "$1" in + --static) + BUILD_PATH="${ROOT}/static-build" + BUILD_SHARED_LIBS=OFF + ;; + + --dynamic) + BUILD_PATH="${ROOT}/dynamic-build" + BUILD_SHARED_LIBS=ON + ;; + + *) + echo "Usage: $0 <--dynamic|--static>">&2 + exit 1 + ;; +esac + +mkdir -p "${BUILD_PATH}/build" "${BUILD_PATH}/install/lib" + +cd "${BUILD_PATH}/build" && +cmake -DTHREADSAFE=ON \ + -DBUILD_CLAR=OFF \ + -DBUILD_SHARED_LIBS"=${BUILD_SHARED_LIBS}" \ + -DCMAKE_C_FLAGS=-fPIC \ + -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ + -DCMAKE_INSTALL_PREFIX="${BUILD_PATH}/install" \ + "${VENDORED_PATH}" && + +exec cmake --build . --target install From 37230d2697a10b82de358a11392cdace5a115cb1 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 16 Feb 2019 17:14:39 +0000 Subject: [PATCH 032/103] Free() the copies of repository.LookupXxx() `repository.LookupXxx()` allocate new go `Object`s that have a reference to a `C.git_object`. Those are then duplicated with `git_object_dup()`, so the original `Object`s linger unnecessarily until the Go GC kicks in. This change explicitly calls `Free()` on the originals to avoid unnecessary accumulation of garbage. (cherry picked from commit 2bb5930733a50b441c4a591dee931af00cf293f2) --- repository.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/repository.go b/repository.go index 2a4e9c815..b645051f8 100644 --- a/repository.go +++ b/repository.go @@ -194,6 +194,7 @@ func (v *Repository) LookupTree(id *Oid) (*Tree, error) { if err != nil { return nil, err } + defer obj.Free() return obj.AsTree() } @@ -203,6 +204,7 @@ func (v *Repository) LookupCommit(id *Oid) (*Commit, error) { if err != nil { return nil, err } + defer obj.Free() return obj.AsCommit() } @@ -212,6 +214,7 @@ func (v *Repository) LookupBlob(id *Oid) (*Blob, error) { if err != nil { return nil, err } + defer obj.Free() return obj.AsBlob() } @@ -221,6 +224,7 @@ func (v *Repository) LookupTag(id *Oid) (*Tag, error) { if err != nil { return nil, err } + defer obj.Free() return obj.AsTag() } From 0b5abad959dd5b001bb4f387baa6a6b9a165517f Mon Sep 17 00:00:00 2001 From: Richard Burke Date: Tue, 3 Jul 2018 22:05:05 +0100 Subject: [PATCH 033/103] Add revert functionality Closes #436 (cherry picked from commit 30c3d0ffe2118376ccbf3ff5ea1676bd7442440d) --- revert.go | 103 +++++++++++++++++++++++++++++++++++++++++++++++++ revert_test.go | 77 ++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 revert.go create mode 100644 revert_test.go diff --git a/revert.go b/revert.go new file mode 100644 index 000000000..8e8bb296f --- /dev/null +++ b/revert.go @@ -0,0 +1,103 @@ +package git + +/* +#include +*/ +import "C" +import ( + "runtime" +) + +// RevertOptions contains options for performing a revert +type RevertOptions struct { + Version uint + Mainline uint + MergeOpts MergeOptions + CheckoutOpts CheckoutOpts +} + +func (opts *RevertOptions) toC() *C.git_revert_options { + return &C.git_revert_options{ + version: C.uint(opts.Version), + mainline: C.uint(opts.Mainline), + merge_opts: *opts.MergeOpts.toC(), + checkout_opts: *opts.CheckoutOpts.toC(), + } +} + +func revertOptionsFromC(opts *C.git_revert_options) RevertOptions { + return RevertOptions{ + Version: uint(opts.version), + Mainline: uint(opts.mainline), + MergeOpts: mergeOptionsFromC(&opts.merge_opts), + CheckoutOpts: checkoutOptionsFromC(&opts.checkout_opts), + } +} + +func freeRevertOptions(opts *C.git_revert_options) { + freeCheckoutOpts(&opts.checkout_opts) +} + +// DefaultRevertOptions initialises a RevertOptions struct with default values +func DefaultRevertOptions() (RevertOptions, error) { + opts := C.git_revert_options{} + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_revert_init_options(&opts, C.GIT_REVERT_OPTIONS_VERSION) + if ecode < 0 { + return RevertOptions{}, MakeGitError(ecode) + } + + defer freeRevertOptions(&opts) + return revertOptionsFromC(&opts), 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 cOpts *C.git_revert_options + + if revertOptions != nil { + cOpts = revertOptions.toC() + defer freeRevertOptions(cOpts) + } + + ecode := C.git_revert(r.ptr, commit.cast_ptr, cOpts) + runtime.KeepAlive(r) + runtime.KeepAlive(commit) + + if ecode < 0 { + return MakeGitError(ecode) + } + + 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() + + var cOpts *C.git_merge_options + + if mergeOptions != nil { + cOpts = mergeOptions.toC() + } + + 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 +} diff --git a/revert_test.go b/revert_test.go new file mode 100644 index 000000000..46ed3347e --- /dev/null +++ b/revert_test.go @@ -0,0 +1,77 @@ +package git + +import ( + "testing" +) + +const ( + expectedRevertedReadmeContents = "foo\n" +) + +func TestRevert(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + seedTestRepo(t, repo) + commitID, _ := updateReadme(t, repo, content) + + commit, err := repo.LookupCommit(commitID) + checkFatal(t, err) + + revertOptions, err := DefaultRevertOptions() + checkFatal(t, err) + + err = repo.Revert(commit, &revertOptions) + checkFatal(t, err) + + actualReadmeContents := readReadme(t, repo) + + if actualReadmeContents != expectedRevertedReadmeContents { + t.Fatalf(`README has incorrect contents after revert. Expected: "%v", Actual: "%v"`, + expectedRevertedReadmeContents, actualReadmeContents) + } + + state := repo.State() + if state != RepositoryStateRevert { + t.Fatalf("Incorrect repository state. Expected: %v, Actual: %v", RepositoryStateRevert, state) + } + + err = repo.StateCleanup() + checkFatal(t, err) + + state = repo.State() + if state != RepositoryStateNone { + t.Fatalf("Incorrect repository state. Expected: %v, Actual: %v", RepositoryStateNone, state) + } +} + +func TestRevertCommit(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + seedTestRepo(t, repo) + commitID, _ := updateReadme(t, repo, content) + + commit, err := repo.LookupCommit(commitID) + checkFatal(t, err) + + revertOptions, err := DefaultRevertOptions() + checkFatal(t, err) + + index, err := repo.RevertCommit(commit, commit, 0, &revertOptions.MergeOpts) + checkFatal(t, err) + defer index.Free() + + revertOptions.CheckoutOpts.Strategy = CheckoutSafe + err = repo.CheckoutIndex(index, &revertOptions.CheckoutOpts) + checkFatal(t, err) + + actualReadmeContents := readReadme(t, repo) + + if actualReadmeContents != expectedRevertedReadmeContents { + t.Fatalf(`README has incorrect contents after revert. Expected: "%v", Actual: "%v"`, + expectedRevertedReadmeContents, actualReadmeContents) + } +} From 5acdcfaf865dd8c0022f51b48c2cfb747ef494d4 Mon Sep 17 00:00:00 2001 From: Richard Burke Date: Tue, 15 Jan 2019 21:29:45 +0000 Subject: [PATCH 034/103] Remove Version from RevertOptions Version is defaulted to GIT_REVERT_OPTIONS_VERSION (cherry picked from commit 4bca045e5aa98b0b791fb467705de0692fe3514f) --- revert.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/revert.go b/revert.go index 8e8bb296f..e745f11ab 100644 --- a/revert.go +++ b/revert.go @@ -10,7 +10,6 @@ import ( // RevertOptions contains options for performing a revert type RevertOptions struct { - Version uint Mainline uint MergeOpts MergeOptions CheckoutOpts CheckoutOpts @@ -18,7 +17,7 @@ type RevertOptions struct { func (opts *RevertOptions) toC() *C.git_revert_options { return &C.git_revert_options{ - version: C.uint(opts.Version), + version: C.GIT_REVERT_OPTIONS_VERSION, mainline: C.uint(opts.Mainline), merge_opts: *opts.MergeOpts.toC(), checkout_opts: *opts.CheckoutOpts.toC(), @@ -27,7 +26,6 @@ func (opts *RevertOptions) toC() *C.git_revert_options { func revertOptionsFromC(opts *C.git_revert_options) RevertOptions { return RevertOptions{ - Version: uint(opts.version), Mainline: uint(opts.mainline), MergeOpts: mergeOptionsFromC(&opts.merge_opts), CheckoutOpts: checkoutOptionsFromC(&opts.checkout_opts), From 23e13acf733f98ef7c5c648933a2e7307bd8f63d Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 23 Feb 2020 06:47:18 -0800 Subject: [PATCH 035/103] Merge pull request #400 from ramanenka/git_index_add_frombuffer Add binding for `git_index_add_frombuffer` (cherry picked from commit 06764f48dce903bf95701c6ef75ad0fe46c0dedf) --- index.go | 26 +++++++++++++++++++++++++- index_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/index.go b/index.go index dd1346033..32acd143c 100644 --- a/index.go +++ b/index.go @@ -90,7 +90,9 @@ func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) { dest.uid = C.uint32_t(source.Uid) dest.gid = C.uint32_t(source.Gid) dest.file_size = C.uint32_t(source.Size) - dest.id = *source.Id.toC() + if source.Id != nil { + dest.id = *source.Id.toC() + } dest.path = C.CString(source.Path) } @@ -195,6 +197,28 @@ func (v *Index) AddByPath(path string) error { 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, ¢ry) + defer freeCIndexEntry(¢ry) + + var cbuffer unsafe.Pointer + if len(buffer) > 0 { + cbuffer = unsafe.Pointer(&buffer[0]) + } + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if err := C.git_index_add_frombuffer(v.ptr, ¢ry, cbuffer, C.size_t(len(buffer))); err < 0 { + return MakeGitError(err) + } + + return nil +} + func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMatchedPathCallback) error { cpathspecs := C.git_strarray{} cpathspecs.count = C.size_t(len(pathspecs)) diff --git a/index_test.go b/index_test.go index f47dace00..d88280930 100644 --- a/index_test.go +++ b/index_test.go @@ -149,6 +149,30 @@ func TestIndexRemoveDirectory(t *testing.T) { } } +func TestIndexAddFromBuffer(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + idx, err := repo.Index() + checkFatal(t, err) + + entry := IndexEntry{ + Path: "README", + Mode: FilemodeBlob, + } + + err = idx.AddFromBuffer(&entry, []byte("foo\n")) + checkFatal(t, err) + + treeId, err := idx.WriteTreeTo(repo) + checkFatal(t, err) + + if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" { + t.Fatalf("%v", treeId.String()) + } +} + func TestIndexAddAllNoCallback(t *testing.T) { t.Parallel() repo := createTestRepo(t) From 75daa3227d590736222fe822e9325d036069ecb2 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 23 Feb 2020 07:05:25 -0800 Subject: [PATCH 036/103] Merge pull request #423 from josharian/more-annotated-commit merge: add two missing AnnotatedCommit methods (cherry picked from commit 21d618136f415486d95965e75af80c0e6688a0d5) --- merge.go | 42 +++++++++++++++++++++++++++++++++++++++--- merge_test.go | 21 +++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/merge.go b/merge.go index bc672ce12..36dd936fa 100644 --- a/merge.go +++ b/merge.go @@ -27,6 +27,15 @@ func newAnnotatedCommitFromC(ptr *C.git_annotated_commit, r *Repository) *Annota return mh } +func (mh *AnnotatedCommit) Id() *Oid { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := newOidFromC(C.git_annotated_commit_id(mh.ptr)) + runtime.KeepAlive(mh) + return ret +} + func (mh *AnnotatedCommit) Free() { runtime.SetFinalizer(mh, nil) C.git_annotated_commit_free(mh.ptr) @@ -49,7 +58,9 @@ func (r *Repository) AnnotatedCommitFromFetchHead(branchName string, remoteURL s return nil, MakeGitError(ret) } - return newAnnotatedCommitFromC(ptr, r), nil + annotatedCommit := newAnnotatedCommitFromC(ptr, r) + runtime.KeepAlive(r) + return annotatedCommit, nil } func (r *Repository) LookupAnnotatedCommit(oid *Oid) (*AnnotatedCommit, error) { @@ -62,7 +73,10 @@ func (r *Repository) LookupAnnotatedCommit(oid *Oid) (*AnnotatedCommit, error) { if ret < 0 { return nil, MakeGitError(ret) } - return newAnnotatedCommitFromC(ptr, r), nil + + annotatedCommit := newAnnotatedCommitFromC(ptr, r) + runtime.KeepAlive(r) + return annotatedCommit, nil } func (r *Repository) AnnotatedCommitFromRef(ref *Reference) (*AnnotatedCommit, error) { @@ -76,7 +90,29 @@ func (r *Repository) AnnotatedCommitFromRef(ref *Reference) (*AnnotatedCommit, e if ret < 0 { return nil, MakeGitError(ret) } - return newAnnotatedCommitFromC(ptr, r), nil + + annotatedCommit := newAnnotatedCommitFromC(ptr, r) + runtime.KeepAlive(r) + return annotatedCommit, nil +} + +func (r *Repository) AnnotatedCommitFromRevspec(spec string) (*AnnotatedCommit, error) { + crevspec := C.CString(spec) + defer C.free(unsafe.Pointer(crevspec)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var ptr *C.git_annotated_commit + ret := C.git_annotated_commit_from_revspec(&ptr, r.ptr, crevspec) + runtime.KeepAlive(r) + if ret < 0 { + return nil, MakeGitError(ret) + } + + annotatedCommit := newAnnotatedCommitFromC(ptr, r) + runtime.KeepAlive(r) + return annotatedCommit, nil } type MergeTreeFlag int diff --git a/merge_test.go b/merge_test.go index f2c84bcb3..7cf034f6b 100644 --- a/merge_test.go +++ b/merge_test.go @@ -5,6 +5,22 @@ import ( "time" ) +func TestAnnotatedCommitFromRevspec(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + seedTestRepo(t, repo) + + mergeHead, err := repo.AnnotatedCommitFromRevspec("refs/heads/master") + checkFatal(t, err) + + expectedId := "473bf778b67b6d53e2ab289e0f1a2e8addef2fc2" + if mergeHead.Id().String() != expectedId { + t.Errorf("mergeHead.Id() = %v, want %v", mergeHead.Id(), expectedId) + } +} + func TestMergeWithSelf(t *testing.T) { t.Parallel() repo := createTestRepo(t) @@ -18,6 +34,11 @@ func TestMergeWithSelf(t *testing.T) { mergeHead, err := repo.AnnotatedCommitFromRef(master) checkFatal(t, err) + expectedId := "473bf778b67b6d53e2ab289e0f1a2e8addef2fc2" + if mergeHead.Id().String() != expectedId { + t.Errorf("mergeHead.Id() = %v, want %v", mergeHead.Id(), expectedId) + } + mergeHeads := make([]*AnnotatedCommit, 1) mergeHeads[0] = mergeHead err = repo.Merge(mergeHeads, nil, nil) From 36b0f8ba75f045d2f5dc034d9e7a869f8fbe7182 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 23 Feb 2020 08:08:30 -0800 Subject: [PATCH 037/103] Merge pull request #429 from josharian/cherrypick-commit cherrypick: wrap git_cherrypick_commit (cherry picked from commit 45097a857c3df900111d49b9c07f2ad4645c1450) --- cherrypick.go | 16 ++++++++++++++ cherrypick_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/cherrypick.go b/cherrypick.go index 8983a7a52..e86e94077 100644 --- a/cherrypick.go +++ b/cherrypick.go @@ -73,3 +73,19 @@ func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error { } return nil } + +func (r *Repository) CherrypickCommit(pick, our *Commit, opts CherrypickOptions) (*Index, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cOpts := opts.MergeOpts.toC() + + 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 +} diff --git a/cherrypick_test.go b/cherrypick_test.go index bfa5ca8f8..19a97360e 100644 --- a/cherrypick_test.go +++ b/cherrypick_test.go @@ -84,3 +84,57 @@ func TestCherrypick(t *testing.T) { t.Fatal("Incorrect repository state: ", state) } } + +func TestCherrypickCommit(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + c1, _ := seedTestRepo(t, repo) + c2, _ := updateReadme(t, repo, content) + + commit1, err := repo.LookupCommit(c1) + if err != nil { + t.Fatal(err) + } + commit2, err := repo.LookupCommit(c2) + if err != nil { + t.Fatal(err) + } + + checkout(t, repo, commit1) + + if got := readReadme(t, repo); got == content { + t.Fatalf("README = %q, want %q", got, content) + } + + opts, err := DefaultCherrypickOptions() + if err != nil { + t.Fatal(err) + } + + idx, err := repo.CherrypickCommit(commit2, commit1, opts) + if err != nil { + t.Fatal(err) + } + defer idx.Free() + + // The file is only updated in the index, not in the working directory. + if got := readReadme(t, repo); got == content { + t.Errorf("README = %q, want %q", got, content) + } + if got := repo.State(); got != RepositoryStateNone { + t.Errorf("repo.State() = %v, want %v", got, RepositoryStateCherrypick) + } + + if got := idx.EntryCount(); got != 1 { + t.Fatalf("idx.EntryCount() = %v, want %v", got, 1) + } + entry, err := idx.EntryByIndex(0) + if err != nil { + t.Fatal(err) + } + if entry.Path != "README" { + t.Errorf("entry.Path = %v, want %v", entry.Path, "README") + } +} From 5acb58e83dcd488bf77f6e5f1d4cdc3001dd23a0 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Thu, 22 Dec 2016 06:46:13 -0800 Subject: [PATCH 038/103] Add support for indexers and alternate odb packfiles This allows for implementations of git servers written in Go. (cherry picked from commit 05bc5e36ff93eb2195480c5cad91e6c5c44cd128) --- indexer.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++ indexer_test.go | 93 +++++++++++++++++++++++++++++++++++++++++++ odb.go | 103 +++++++++++++++++++++++++++++++++++++++++++++++- odb_test.go | 34 ++++++++++++++++ wrapper.c | 25 ++++++++++++ 5 files changed, 353 insertions(+), 1 deletion(-) create mode 100644 indexer.go create mode 100644 indexer_test.go diff --git a/indexer.go b/indexer.go new file mode 100644 index 000000000..d1b93722e --- /dev/null +++ b/indexer.go @@ -0,0 +1,99 @@ +package git + +/* +#include + +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 { + ptr *C.git_indexer + stats C.git_transfer_progress + callbacks RemoteCallbacks + callbacksHandle unsafe.Pointer +} + +// NewIndexer creates a new indexer instance. +func NewIndexer(packfilePath string, odb *Odb, callback TransferProgressCallback) (indexer *Indexer, err error) { + indexer = new(Indexer) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var odbPtr *C.git_odb = nil + if odb != nil { + odbPtr = odb.ptr + } + + indexer.callbacks.TransferProgressCallback = callback + indexer.callbacksHandle = pointerHandles.Track(&indexer.callbacks) + + cstr := C.CString(packfilePath) + defer C.free(unsafe.Pointer(cstr)) + + ret := C._go_git_indexer_new(&indexer.ptr, cstr, 0, odbPtr, indexer.callbacksHandle) + runtime.KeepAlive(odb) + if ret < 0 { + pointerHandles.Untrack(indexer.callbacksHandle) + 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() { + pointerHandles.Untrack(indexer.callbacksHandle) + runtime.SetFinalizer(indexer, nil) + C.git_indexer_free(indexer.ptr) +} diff --git a/indexer_test.go b/indexer_test.go new file mode 100644 index 000000000..1b65c9555 --- /dev/null +++ b/indexer_test.go @@ -0,0 +1,93 @@ +package git + +import ( + "fmt" + "io/ioutil" + "os" + "path" + "testing" +) + +var ( + // This is a packfile with three objects. The second is a delta which + // depends on the third, which is also a delta. + outOfOrderPack = []byte{ + 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, + 0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76, + 0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10, + 0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62, + 0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x75, 0x01, + 0xd7, 0x71, 0x36, 0x66, 0xf4, 0xde, 0x82, 0x27, 0x76, 0xc7, 0x62, 0x2c, + 0x10, 0xf1, 0xb0, 0x7d, 0xe2, 0x80, 0xdc, 0x78, 0x9c, 0x63, 0x62, 0x62, + 0x62, 0xb7, 0x03, 0x00, 0x00, 0x69, 0x00, 0x4c, 0xde, 0x7d, 0xaa, 0xe4, + 0x19, 0x87, 0x58, 0x80, 0x61, 0x09, 0x9a, 0x33, 0xca, 0x7a, 0x31, 0x92, + 0x6f, 0xae, 0x66, 0x75, + } +) + +func TestIndexerOutOfOrder(t *testing.T) { + t.Parallel() + + tmpPath, err := ioutil.TempDir("", "git2go") + checkFatal(t, err) + defer os.RemoveAll(tmpPath) + + var finalStats TransferProgress + idx, err := NewIndexer(tmpPath, nil, func(stats TransferProgress) ErrorCode { + finalStats = stats + return ErrOk + }) + 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())) + } +} diff --git a/odb.go b/odb.go index 7ae108a89..303065ba9 100644 --- a/odb.go +++ b/odb.go @@ -3,8 +3,13 @@ package git /* #include +extern int git_odb_backend_one_pack(git_odb_backend **out, const char *index_file); 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 ( @@ -42,8 +47,20 @@ func NewOdbBackendFromC(ptr unsafe.Pointer) (backend *OdbBackend) { return backend } -func (v *Odb) AddBackend(backend *OdbBackend, priority int) (err error) { +func (v *Odb) AddAlternate(backend *OdbBackend, priority int) (err error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_odb_add_alternate(v.ptr, backend.ptr, C.int(priority)) + runtime.KeepAlive(v) + if ret < 0 { + backend.Free() + return MakeGitError(ret) + } + return nil +} + +func (v *Odb) AddBackend(backend *OdbBackend, priority int) (err error) { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -56,6 +73,21 @@ func (v *Odb) AddBackend(backend *OdbBackend, priority int) (err error) { return nil } +func NewOdbBackendOnePack(packfileIndexPath string) (backend *OdbBackend, err error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cstr := C.CString(packfileIndexPath) + defer C.free(unsafe.Pointer(cstr)) + + var odbOnePack *C.git_odb_backend = nil + ret := C.git_odb_backend_one_pack(&odbOnePack, cstr) + if ret < 0 { + return nil, MakeGitError(ret) + } + return NewOdbBackendFromC(unsafe.Pointer(odbOnePack)), nil +} + func (v *Odb) ReadHeader(oid *Oid) (uint64, ObjectType, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -231,6 +263,31 @@ func (v *Odb) NewWriteStream(size int64, otype ObjectType) (*OdbWriteStream, err return stream, nil } +// NewWritePack opens a stream for writing a pack file to the ODB. If the ODB +// layer understands pack files, then the given packfile will likely be +// streamed directly to disk (and a corresponding index created). If the ODB +// layer does not understand pack files, the objects will be stored in whatever +// format the ODB layer uses. +func (v *Odb) NewWritePack(callback TransferProgressCallback) (*OdbWritepack, error) { + writepack := new(OdbWritepack) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + writepack.callbacks.TransferProgressCallback = callback + writepack.callbacksHandle = pointerHandles.Track(&writepack.callbacks) + + ret := C._go_git_odb_write_pack(&writepack.ptr, v.ptr, writepack.callbacksHandle) + runtime.KeepAlive(v) + if ret < 0 { + pointerHandles.Untrack(writepack.callbacksHandle) + return nil, MakeGitError(ret) + } + + runtime.SetFinalizer(writepack, (*OdbWritepack).Free) + return writepack, nil +} + func (v *OdbBackend) Free() { C._go_git_odb_backend_free(v.ptr) } @@ -360,3 +417,47 @@ func (stream *OdbWriteStream) Free() { runtime.SetFinalizer(stream, nil) C.git_odb_stream_free(stream.ptr) } + +// OdbWritepack is a stream to write a packfile to the ODB. +type OdbWritepack struct { + ptr *C.git_odb_writepack + stats C.git_transfer_progress + callbacks RemoteCallbacks + callbacksHandle unsafe.Pointer +} + +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() { + pointerHandles.Untrack(writepack.callbacksHandle) + runtime.SetFinalizer(writepack, nil) + C._go_git_odb_writepack_free(writepack.ptr) +} diff --git a/odb_test.go b/odb_test.go index 46acdbaf2..e44c9271e 100644 --- a/odb_test.go +++ b/odb_test.go @@ -152,3 +152,37 @@ func TestOdbForeach(t *testing.T) { t.Fatalf("Odb.ForEach() did not return the expected error, got %v", err) } } + +func TestOdbWritepack(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + _, _ = seedTestRepo(t, repo) + + odb, err := repo.Odb() + checkFatal(t, err) + + var finalStats TransferProgress + writepack, err := odb.NewWritePack(func(stats TransferProgress) ErrorCode { + finalStats = stats + return ErrOk + }) + 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) + } +} diff --git a/wrapper.c b/wrapper.c index 11c2f3242..df767fb49 100644 --- a/wrapper.c +++ b/wrapper.c @@ -180,4 +180,29 @@ void _go_git_writestream_free(git_writestream *stream) stream->free(stream); } +int _go_git_odb_write_pack(git_odb_writepack **out, git_odb *db, void *progress_payload) +{ + return git_odb_write_pack(out, db, (git_transfer_progress_cb)transferProgressCallback, progress_payload); +} + +int _go_git_odb_writepack_append(git_odb_writepack *writepack, const void *data, size_t size, git_transfer_progress *stats) +{ + return writepack->append(writepack, data, size, stats); +} + +int _go_git_odb_writepack_commit(git_odb_writepack *writepack, git_transfer_progress *stats) +{ + return writepack->commit(writepack, stats); +} + +void _go_git_odb_writepack_free(git_odb_writepack *writepack) +{ + writepack->free(writepack); +} + +int _go_git_indexer_new(git_indexer **out, const char *path, unsigned int mode, git_odb *odb, void *progress_cb_payload) +{ + return git_indexer_new(out, path, mode, odb, (git_transfer_progress_cb)transferProgressCallback, progress_cb_payload); +} + /* EOF */ From 395b2a90a469ca8a904e90c7a3c573c4ec0cc9f1 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 23 Dec 2018 03:25:52 +0000 Subject: [PATCH 039/103] Add odb.NewOdbBackendLoose() This change adds support for odb.NewOdbBackendLoose(). This, together with the git.Packbuilder, can do what Mempack does with a lot less memory. (cherry picked from commit 91946a570500bc441be0aea4603a5c83f81e22a4) --- odb.go | 23 +++++++++++++++++++++++ odb_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/odb.go b/odb.go index 303065ba9..1f215ec64 100644 --- a/odb.go +++ b/odb.go @@ -4,6 +4,7 @@ package git #include 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); @@ -14,6 +15,7 @@ extern void _go_git_odb_writepack_free(git_odb_writepack *writepack); import "C" import ( "io" + "os" "reflect" "runtime" "unsafe" @@ -88,6 +90,27 @@ func NewOdbBackendOnePack(packfileIndexPath string) (backend *OdbBackend, err er 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() diff --git a/odb_test.go b/odb_test.go index e44c9271e..d79afa1ac 100644 --- a/odb_test.go +++ b/odb_test.go @@ -3,8 +3,11 @@ package git import ( "bytes" "errors" + "fmt" "io" "io/ioutil" + "os" + "path" "testing" ) @@ -186,3 +189,47 @@ func TestOdbWritepack(t *testing.T) { 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) +} From 26397cdaeae152bebe55b48c1f2440b98566f664 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 23 Feb 2020 23:24:27 +0000 Subject: [PATCH 040/103] Fix the DiffFlag type This change makes the underlying type of DiffFlag be uint32 instead of int. That makes it possible to build on 32-bit systems. Fixes: #487 (cherry picked from commit 93c4c5b30a73a587efd2f5bc1f33d0ef8cec6950) --- diff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff.go b/diff.go index 6be8f5ba8..9b3b27874 100644 --- a/diff.go +++ b/diff.go @@ -14,7 +14,7 @@ import ( "unsafe" ) -type DiffFlag int +type DiffFlag uint32 const ( DiffFlagBinary DiffFlag = C.GIT_DIFF_FLAG_BINARY From 231f89164b85744ae3d327d104d702c6d6ccb301 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Thu, 27 Feb 2020 21:16:46 -0800 Subject: [PATCH 041/103] Merge pull request #542 from slyphon/fix-error-name Resolves issue #541 - typo in error code 'ErrAmbigious' (cherry picked from commit 30de4b2e26a2eb7742445962e3038052c5376bfa) --- git.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/git.go b/git.go index 897d261f2..f953ebc8d 100644 --- a/git.go +++ b/git.go @@ -61,6 +61,8 @@ const ( // Object exists preventing operation ErrExists ErrorCode = C.GIT_EEXISTS // More than one object matches + ErrAmbiguous ErrorCode = C.GIT_EAMBIGUOUS + // (backwards compatibility misspelling) ErrAmbigious ErrorCode = C.GIT_EAMBIGUOUS // Output buffer too short to hold data ErrBuffs ErrorCode = C.GIT_EBUFS From 891d857c3acf1731746ecaaa50d1948cc0fe7ad2 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Thu, 19 Mar 2020 07:45:38 -0700 Subject: [PATCH 042/103] Add the version number to go.mod This is the second take on trying to tag the current release with a Go version. (cherry picked from commit a32375a86063768507a01b32cc3d868f4c2ffa9c) --- go.mod | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 688b8e3bd..0228d0290 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ -module github.com/libgit2/git2go +module github.com/libgit2/git2go/v27 + +go 1.13 From 59a177dd02f61f78e529368cb79bd2631b7a7c45 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Thu, 19 Mar 2020 08:50:28 -0700 Subject: [PATCH 043/103] Update README.md Clarifying the versions since we're using Go 1.11 module version rules now. (cherry picked from commit 2b66c0f9e75261ac678d08d45e86615fece82dad) --- README.md | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4bd2e7ec5..21d31a600 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,30 @@ git2go Go bindings for [libgit2](http://libgit2.github.com/). -### Which branch to use +### Which Go version to use -The numbered branches work against the version of libgit2 as specified by their number. You can import them in your project via gopkg.in, e.g. if you have libgit2 v0.25 installed you'd import with +Due to the fact that Go 1.11 module versions have semantic meaning and don't necessarily align with libgit2's release schedule, please consult the following table for a mapping between libgit2 and git2go module versions: - import "gopkg.in/libgit2/git2go.v25" +| libgit2 | git2go | +|---------|---------------| +| master | (will be v30) | +| 0.99 | v29 | +| 0.28 | v28 | +| 0.27 | v27 | + +You can import them in your project via `go get` or a regular `import` with the version number as a suffix. For example, if you have libgit2 v0.27 installed, you'd import with + +```go +import "github.com/libgit2/git2go/v27" +``` which will ensure there are no sudden changes to the API. -The `master` branch follows the tip of libgit2 itself (with some lag) and as such has no guarantees on its own API nor does it have expectations the stability of libgit2's. Thus this only supports statically linking against libgit2. +The `master` branch follows the tip of libgit2 itself (with some lag) and as such has no guarantees on the stability of libgit2's API. Thus this only supports statically linking against libgit2. + +### Which branch to send Pull requests to + +If there's something version-specific that you'd want to contribute to, you can send them to the `release-${MAJOR}-${MINOR}` branches, which follow libgit2's releases. Installing ---------- @@ -24,9 +39,11 @@ This project wraps the functionality provided by libgit2. If you're using a vers ### Versioned branch, dynamic linking -When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via gopkg.in, e.g. to work against libgit2 v0.25 +When linking dynamically against a released version of libgit2, install it via your system's package manager. CGo will take care of finding its pkg-config file and set up the linking. Import via Go modules, e.g. to work against libgit2 v0.27 - import "gopkg.in/libgit2/git2go.v25" +```go +import "github.com/libgit2/git2go/v27" +``` ### Master branch, or static linking From 0dfadb0dd05e20c4bc27cad8c8d49bb113a90637 Mon Sep 17 00:00:00 2001 From: Suhaib Mujahid Date: Mon, 23 Mar 2020 21:05:30 -0400 Subject: [PATCH 044/103] Update README.md (cherry picked from commit 3a2102638d64cd76e50f51577e8bb2b8f9c7035f) --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 21d31a600..f37640bb4 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,11 @@ Due to the fact that Go 1.11 module versions have semantic meaning and don't nec | 0.28 | v28 | | 0.27 | v27 | -You can import them in your project via `go get` or a regular `import` with the version number as a suffix. For example, if you have libgit2 v0.27 installed, you'd import with +You can import them in your project with the version's major number as a suffix. For example, if you have libgit2 v0.27 installed, you'd import git2go v27 with +```sh +go get github.com/libgit2/git2go/v27 +``` ```go import "github.com/libgit2/git2go/v27" ``` From 447d43c57d26a84b5f943a56275870b0b55e3ced Mon Sep 17 00:00:00 2001 From: lhchavez Date: Thu, 26 Mar 2020 17:50:12 -0700 Subject: [PATCH 045/103] Fix SIGSEGV on double free for Cred object This change removes the Go finalizer when passing ownership to libgit2. Fixes: #553 (cherry picked from commit 0843b826d219b16d55d3bb7fffbb5fb3fbbf82f8) --- remote.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/remote.go b/remote.go index 6765e45b8..59a60143a 100644 --- a/remote.go +++ b/remote.go @@ -248,6 +248,10 @@ func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types)) if cred != nil { *_cred = cred.ptr + + // have transferred ownership to libgit, 'forget' the native pointer + cred.ptr = nil + runtime.SetFinalizer(cred, nil) } return int(ret) } From 8e87fa7d72000af43cd48c00c254677c6690c392 Mon Sep 17 00:00:00 2001 From: Suhaib Mujahid Date: Thu, 23 Apr 2020 19:07:58 -0400 Subject: [PATCH 046/103] Check nil signature (cherry picked from commit 91d08450b68efc8ef5bd5bfee29e813ca5829229) --- signature.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/signature.go b/signature.go index 220fe5701..dfd97189c 100644 --- a/signature.go +++ b/signature.go @@ -17,6 +17,10 @@ type Signature struct { } func newSignatureFromC(sig *C.git_signature) *Signature { + if sig == nil { + return nil + } + // git stores minutes, go wants seconds loc := time.FixedZone("", int(sig.when.offset)*60) return &Signature{ From 8334650b1ca27be0a8187e3ff3899610ed15f4e1 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Mon, 4 May 2020 17:44:13 -0700 Subject: [PATCH 047/103] expose options related to caching (cherry picked from commit 8b51d0db8e40e97283b771a5a51b13bea4651f81) --- settings.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ settings_test.go | 33 +++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/settings.go b/settings.go index c7f1850ef..72fffc56a 100644 --- a/settings.go +++ b/settings.go @@ -18,10 +18,20 @@ int _go_git_opts_set_size_t(int opt, size_t val) return git_libgit2_opts(opt, val); } +int _go_git_opts_set_cache_object_limit(git_otype type, size_t size) +{ + return git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, type, size); +} + int _go_git_opts_get_size_t(int opt, size_t *val) { return git_libgit2_opts(opt, val); } + +int _go_git_opts_get_size_t_size_t(int opt, size_t *val1, size_t *val2) +{ + return git_libgit2_opts(opt, val1, val2); +} */ import "C" import ( @@ -75,6 +85,34 @@ 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 GetCachedMemory() (current int, allowed int, err error) { + return getSizetSizet(C.GIT_OPT_GET_CACHED_MEMORY) +} + +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_otype(objectType), C.size_t(size)) + if err < 0 { + return MakeGitError(err) + } + + return nil +} + func getSizet(opt C.int) (int, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -88,6 +126,19 @@ func getSizet(opt C.int) (int, error) { return int(val), nil } +func getSizetSizet(opt C.int) (int, int, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var val1, val2 C.size_t + err := C._go_git_opts_get_size_t_size_t(opt, &val1, &val2) + if err < 0 { + return 0, 0, MakeGitError(err) + } + + return int(val1), int(val2), nil +} + func setSizet(opt C.int, val int) error { runtime.LockOSThread() defer runtime.UnlockOSThread() diff --git a/settings_test.go b/settings_test.go index 3a4ce0a95..4e45567be 100644 --- a/settings_test.go +++ b/settings_test.go @@ -48,3 +48,36 @@ func TestMmapSizes(t *testing.T) { t.Fatal("Sizes don't match") } } + +func TestEnableCaching(t *testing.T) { + err := EnableCaching(false) + checkFatal(t, err) + + err = EnableCaching(true) + checkFatal(t, err) +} + +func TestGetCachedMemory(t *testing.T) { + current, allowed, err := GetCachedMemory() + 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) +} \ No newline at end of file From d6ab8729d88501913c757cc8ebd619905d976b1d Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 9 May 2020 20:39:51 -0700 Subject: [PATCH 048/103] Merge pull request #582 from suhaibmujahid/method-rename It is not Go idiomatic to put Get into the getter's name (cherry picked from commit 31f877e249e28c29cc4fcd512381a5a5b26e59d9) --- diff.go | 7 ++++++- index.go | 7 ++++++- merge_test.go | 2 +- settings.go | 7 ++++++- settings_test.go | 4 ++-- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/diff.go b/diff.go index 9b3b27874..7a41f861a 100644 --- a/diff.go +++ b/diff.go @@ -144,7 +144,7 @@ func (diff *Diff) NumDeltas() (int, error) { return ret, nil } -func (diff *Diff) GetDelta(index int) (DiffDelta, error) { +func (diff *Diff) Delta(index int) (DiffDelta, error) { if diff.ptr == nil { return DiffDelta{}, ErrInvalid } @@ -154,6 +154,11 @@ func (diff *Diff) GetDelta(index int) (DiffDelta, error) { return ret, nil } +// deprecated: You should use `Diff.Delta()` instead. +func (diff *Diff) GetDelta(index int) (DiffDelta, error) { + return diff.Delta(index) +} + func newDiffFromC(ptr *C.git_diff, repo *Repository) *Diff { if ptr == nil { return nil diff --git a/index.go b/index.go index 32acd143c..626e64921 100644 --- a/index.go +++ b/index.go @@ -528,7 +528,7 @@ type IndexConflict struct { Their *IndexEntry } -func (v *Index) GetConflict(path string) (IndexConflict, error) { +func (v *Index) Conflict(path string) (IndexConflict, error) { var cancestor *C.git_index_entry var cour *C.git_index_entry @@ -553,6 +553,11 @@ func (v *Index) GetConflict(path string) (IndexConflict, error) { return ret, nil } +// deprecated: You should use `Index.Conflict()` instead. +func (v *Index) GetConflict(path string) (IndexConflict, error) { + return v.Conflict(path) +} + func (v *Index) RemoveConflict(path string) error { cpath := C.CString(path) diff --git a/merge_test.go b/merge_test.go index 7cf034f6b..319bef332 100644 --- a/merge_test.go +++ b/merge_test.go @@ -109,7 +109,7 @@ func TestMergeTreesWithoutAncestor(t *testing.T) { if !index.HasConflicts() { t.Fatal("expected conflicts in the index") } - _, err = index.GetConflict("README") + _, err = index.Conflict("README") checkFatal(t, err) } diff --git a/settings.go b/settings.go index 72fffc56a..247290801 100644 --- a/settings.go +++ b/settings.go @@ -93,10 +93,15 @@ func EnableCaching(enabled bool) error { } } -func GetCachedMemory() (current int, allowed int, err error) { +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) } diff --git a/settings_test.go b/settings_test.go index 4e45567be..150ee7c5c 100644 --- a/settings_test.go +++ b/settings_test.go @@ -57,8 +57,8 @@ func TestEnableCaching(t *testing.T) { checkFatal(t, err) } -func TestGetCachedMemory(t *testing.T) { - current, allowed, err := GetCachedMemory() +func TestCachedMemory(t *testing.T) { + current, allowed, err := CachedMemory() checkFatal(t, err) if current < 0 { From 250a674787fe218261af2116f32c04141d7c13ad Mon Sep 17 00:00:00 2001 From: Jesse Hathaway Date: Tue, 2 Jun 2020 12:30:42 -0500 Subject: [PATCH 049/103] Add support for parsing git trailers (#614) Adds a wrapper for git_message_trailers which returns a slice of trailer structs. (cherry picked from commit 5241c72e6ebd21085e56a1c6d284c06154a202b5) --- message.go | 42 ++++++++++++++++++++++++++++++++++++++++++ message_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 message.go create mode 100644 message_test.go diff --git a/message.go b/message.go new file mode 100644 index 000000000..255e8d7a2 --- /dev/null +++ b/message.go @@ -0,0 +1,42 @@ +package git + +/* +#include +*/ +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 +} diff --git a/message_test.go b/message_test.go new file mode 100644 index 000000000..f33ccb722 --- /dev/null +++ b/message_test.go @@ -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 \n", + []Trailer{ + Trailer{Key: "Co-authored-by", Value: "Alice "}, + }, + }, + { + "commit with two trailers\n\nCo-authored-by: Alice \nSigned-off-by: Bob \n", + []Trailer{ + Trailer{Key: "Co-authored-by", Value: "Alice "}, + Trailer{Key: "Signed-off-by", Value: "Bob "}}, + }, + } + 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) + } + } +} From aea6cff7ac91d88d7a9ec394489c8f24e9c4fbaf Mon Sep 17 00:00:00 2001 From: Takuji Shimokawa Date: Fri, 5 Jun 2020 12:34:37 +0900 Subject: [PATCH 050/103] Provide missing merge flags (#615) This change adds two missing merge flags MergeTreeSkipREUC and MergeTreeNoRecursive. (cherry picked from commit 33dac3d46077ef71ed97ba232a0a53b793f1ca7e) --- merge.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/merge.go b/merge.go index 36dd936fa..4f097b7d3 100644 --- a/merge.go +++ b/merge.go @@ -126,6 +126,14 @@ const ( // continue resolving conflicts. The merge operation will fail with // GIT_EMERGECONFLICT and no index will be returned. MergeTreeFailOnConflict MergeTreeFlag = C.GIT_MERGE_FAIL_ON_CONFLICT + // MergeTreeSkipREUC specifies not to write the REUC extension on the + // generated index. + MergeTreeSkipREUC MergeTreeFlag = C.GIT_MERGE_SKIP_REUC + // MergeTreeNoRecursive specifies not to build a recursive merge base (by + // merging the multiple merge bases) if the commits being merged have + // multiple merge bases. Instead, the first base is used. + // This flag provides a similar merge base to `git-merge-resolve`. + MergeTreeNoRecursive MergeTreeFlag = C.GIT_MERGE_NO_RECURSIVE ) type MergeOptions struct { From 1233fdebf5b20f023295476473a6b6f9801e1f1c Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 20 Jun 2020 16:24:46 -0700 Subject: [PATCH 051/103] Update the `README.md` to clarify some aspects of static libgit2 (#620) This change improves the documentation surrounding libgit2 static builds and modules. Fixes: #618 (cherry picked from commit 9eaf4fed5f4f2361898f9da8345b34886076bfc2) --- .github/workflows/ci.yml | 4 ++-- README.md | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 931cc09c1..5472f22bd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,8 +35,8 @@ jobs: run: | git submodule update --init make build-libgit2-static - go get --tags "static" github.com/${{ github.repository }}/... - go build --tags "static" github.com/${{ github.repository }}/... + go get -tags static github.com/${{ github.repository }}/... + go build -tags static github.com/${{ github.repository }}/... - name: Test env: GOPATH: /home/runner/work/git2go diff --git a/README.md b/README.md index f37640bb4..8908fc06c 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,16 @@ Run `go get -d github.com/libgit2/git2go` to download the code and go to your `$ will compile libgit2, link it into git2go and install it. The `master` branch is set up to follow the specific libgit2 version that is vendored, so trying dynamic linking may or may not work depending on the exact versions involved. +In order to let Go pass the correct flags to `pkg-config`, `-tags static` needs to be passed to all `go` commands that build any binaries. For instance: + + go build -tags static github.com/my/project/... + go test -tags static github.com/my/project/... + go install -tags static github.com/my/project/... + +One thing to take into account is that since Go expects the `pkg-config` file to be within the same directory where `make install-static` was called, so the `go.mod` file may need to have a [`replace` directive](https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive) so that the correct setup is achieved. So if `git2go` is checked out at `$GOPATH/src/github.com/libgit2/git2go` and your project at `$GOPATH/src/github.com/my/project`, the `go.mod` file of `github.com/my/project` might need to have a line like + + replace github.com/libgit2/git2go/v27 ../../libgit2/git2go + Parallelism and network operations ---------------------------------- @@ -74,7 +84,7 @@ For the stable version, `go test` will work as usual. For the `master` branch, s Alternatively, you can build the library manually first and then run the tests ./script/build-libgit2-static.sh - go test -v --tags "static" ./... + go test -v -tags static ./... License ------- From 2635b45d9035fc157c337b9c8e3300baaf135ebb Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 21 Jun 2020 06:44:06 -0700 Subject: [PATCH 052/103] Add a way to cleanly shut down the library (#578) This change adds the Shutdown() method, so that the library can be cleanly shut down. This helps significanly reduce the amount of noise in the leak detector. (cherry picked from commit 619a9c236bf79c63d955490c0803833004a47154) --- git.go | 10 ++++++++++ handles.go | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/git.go b/git.go index f953ebc8d..ff9b64314 100644 --- a/git.go +++ b/git.go @@ -139,6 +139,16 @@ func init() { C.git_openssl_set_locking() } +// Shutdown frees all the resources acquired by libgit2. Make sure no +// references to any git2go objects are live before calling this. +// After this is called, invoking any function from this library will result in +// undefined behavior, so make sure this is called carefully. +func Shutdown() { + pointerHandles.Clear() + + C.git_libgit2_shutdown() +} + // Oid represents the id for a Git object. type Oid [20]byte diff --git a/handles.go b/handles.go index d27d3c353..c0d4b3cbb 100644 --- a/handles.go +++ b/handles.go @@ -43,6 +43,16 @@ func (v *HandleList) Untrack(handle unsafe.Pointer) { v.Unlock() } +// Clear stops tracking all the managed pointers. +func (v *HandleList) Clear() { + v.Lock() + for handle := range v.handles { + delete(v.handles, handle) + C.free(handle) + } + v.Unlock() +} + // Get retrieves the pointer from the given handle func (v *HandleList) Get(handle unsafe.Pointer) interface{} { v.RLock() From 14e29a3390cf38a6c596abfa71caa6765d36b86f Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 21 Jun 2020 06:45:39 -0700 Subject: [PATCH 053/103] Fix a potential use-after-free in DiffNotifyCallback (#579) This change makes the DiffNotifyCallback always use an "unowned" *git.Diff that does _not_ run the finalizer. Since the underlying git_diff object is still owned by libgit2, we shouldn't be calling Diff.Free() on it, even by accident, since that would cause a whole lot of undefined behavior. Now instead of storing a reference to a *git.Diff in the intermediate state while the diff operation is being done, create a brand new *git.Diff for every callback invocation, and only create a fully-owned *git.Diff when the diff operation is done and the ownership is transfered to Go. (cherry picked from commit c78ae57de63857d53f05ed088f308699622360fe) --- diff.go | 78 ++++++++++++++++++++++++++++---------------------------- patch.go | 2 +- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/diff.go b/diff.go index 7a41f861a..7f824fa6f 100644 --- a/diff.go +++ b/diff.go @@ -131,8 +131,9 @@ func diffLineFromC(line *C.git_diff_line) DiffLine { } type Diff struct { - ptr *C.git_diff - repo *Repository + ptr *C.git_diff + repo *Repository + runFinalizer bool } func (diff *Diff) NumDeltas() (int, error) { @@ -165,8 +166,9 @@ func newDiffFromC(ptr *C.git_diff, repo *Repository) *Diff { } diff := &Diff{ - ptr: ptr, - repo: repo, + ptr: ptr, + repo: repo, + runFinalizer: true, } runtime.SetFinalizer(diff, (*Diff).Free) @@ -177,6 +179,11 @@ func (diff *Diff) Free() error { if diff.ptr == nil { return ErrInvalid } + if !diff.runFinalizer { + // This is the case with the Diff objects that are involved in the DiffNotifyCallback. + diff.ptr = nil + return nil + } runtime.SetFinalizer(diff, nil) C.git_diff_free(diff.ptr) diff.ptr = nil @@ -579,9 +586,9 @@ var ( ) type diffNotifyData struct { - Callback DiffNotifyCallback - Diff *Diff - Error error + Callback DiffNotifyCallback + Repository *Repository + Error error } //export diffNotifyCb @@ -595,11 +602,20 @@ func diffNotifyCb(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, m } if data != nil { - if data.Diff == nil { - data.Diff = newDiffFromC(diff_so_far, nil) + // We are not taking ownership of this diff pointer, so no finalizer is set. + diff := &Diff{ + ptr: diff_so_far, + repo: data.Repository, + runFinalizer: false, } - err := data.Callback(data.Diff, diffDeltaFromC(delta_to_add), C.GoString(matched_pathspec)) + err := data.Callback(diff, diffDeltaFromC(delta_to_add), C.GoString(matched_pathspec)) + + // Since the callback could theoretically keep a reference to the diff + // (which could be freed by libgit2 if an error occurs later during the + // diffing process), this converts a use-after-free (terrible!) into a nil + // dereference ("just" pretty bad). + diff.ptr = nil if err == ErrDeltaSkip { return 1 @@ -613,11 +629,12 @@ func diffNotifyCb(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, m return 0 } -func diffOptionsToC(opts *DiffOptions) (copts *C.git_diff_options, notifyData *diffNotifyData) { +func diffOptionsToC(opts *DiffOptions, repo *Repository) (copts *C.git_diff_options) { cpathspec := C.git_strarray{} if opts != nil { - notifyData = &diffNotifyData{ - Callback: opts.NotifyCallback, + notifyData := &diffNotifyData{ + Callback: opts.NotifyCallback, + Repository: repo, } if opts.Pathspec != nil { @@ -670,7 +687,7 @@ func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) ( newPtr = newTree.cast_ptr } - copts, notifyData := diffOptionsToC(opts) + copts := diffOptionsToC(opts, v) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -682,10 +699,6 @@ func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) ( if ecode < 0 { return nil, MakeGitError(ecode) } - - if notifyData != nil && notifyData.Diff != nil { - return notifyData.Diff, nil - } return newDiffFromC(diffPtr, v), nil } @@ -697,7 +710,7 @@ func (v *Repository) DiffTreeToWorkdir(oldTree *Tree, opts *DiffOptions) (*Diff, oldPtr = oldTree.cast_ptr } - copts, notifyData := diffOptionsToC(opts) + copts := diffOptionsToC(opts, v) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -708,10 +721,6 @@ func (v *Repository) DiffTreeToWorkdir(oldTree *Tree, opts *DiffOptions) (*Diff, if ecode < 0 { return nil, MakeGitError(ecode) } - - if notifyData != nil && notifyData.Diff != nil { - return notifyData.Diff, nil - } return newDiffFromC(diffPtr, v), nil } @@ -728,7 +737,7 @@ func (v *Repository) DiffTreeToIndex(oldTree *Tree, index *Index, opts *DiffOpti indexPtr = index.ptr } - copts, notifyData := diffOptionsToC(opts) + copts := diffOptionsToC(opts, v) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -740,10 +749,6 @@ func (v *Repository) DiffTreeToIndex(oldTree *Tree, index *Index, opts *DiffOpti if ecode < 0 { return nil, MakeGitError(ecode) } - - if notifyData != nil && notifyData.Diff != nil { - return notifyData.Diff, nil - } return newDiffFromC(diffPtr, v), nil } @@ -755,7 +760,7 @@ func (v *Repository) DiffTreeToWorkdirWithIndex(oldTree *Tree, opts *DiffOptions oldPtr = oldTree.cast_ptr } - copts, notifyData := diffOptionsToC(opts) + copts := diffOptionsToC(opts, v) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -766,10 +771,6 @@ func (v *Repository) DiffTreeToWorkdirWithIndex(oldTree *Tree, opts *DiffOptions if ecode < 0 { return nil, MakeGitError(ecode) } - - if notifyData != nil && notifyData.Diff != nil { - return notifyData.Diff, nil - } return newDiffFromC(diffPtr, v), nil } @@ -781,7 +782,7 @@ func (v *Repository) DiffIndexToWorkdir(index *Index, opts *DiffOptions) (*Diff, indexPtr = index.ptr } - copts, notifyData := diffOptionsToC(opts) + copts := diffOptionsToC(opts, v) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -792,10 +793,6 @@ func (v *Repository) DiffIndexToWorkdir(index *Index, opts *DiffOptions) (*Diff, if ecode < 0 { return nil, MakeGitError(ecode) } - - if notifyData != nil && notifyData.Diff != nil { - return notifyData.Diff, nil - } return newDiffFromC(diffPtr, v), nil } @@ -819,12 +816,15 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, handle := pointerHandles.Track(data) defer pointerHandles.Untrack(handle) + var repo *Repository var oldBlobPtr, newBlobPtr *C.git_blob if oldBlob != nil { oldBlobPtr = oldBlob.cast_ptr + repo = oldBlob.repo } if newBlob != nil { newBlobPtr = newBlob.cast_ptr + repo = newBlob.repo } oldBlobPath := C.CString(oldAsPath) @@ -832,7 +832,7 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, newBlobPath := C.CString(newAsPath) defer C.free(unsafe.Pointer(newBlobPath)) - copts, _ := diffOptionsToC(opts) + copts := diffOptionsToC(opts, repo) defer freeDiffOptions(copts) runtime.LockOSThread() diff --git a/patch.go b/patch.go index 7e6f71da3..1c4d6334e 100644 --- a/patch.go +++ b/patch.go @@ -77,7 +77,7 @@ func (v *Repository) PatchFromBuffers(oldPath, newPath string, oldBuf, newBuf [] cNewPath := C.CString(newPath) defer C.free(unsafe.Pointer(cNewPath)) - copts, _ := diffOptionsToC(opts) + copts := diffOptionsToC(opts, v) defer freeDiffOptions(copts) runtime.LockOSThread() From 26287a926a02c36e60813d99bfe142c1231862e2 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 21 Jun 2020 15:40:52 -0700 Subject: [PATCH 054/103] Revamp the ways in which the library can be built (#621) This change allows to link the system version of libgit2 statically. Since `-tags static` is already used for the bundled version of the library and to avoid breaking old workflows, `-tags static,system_libgit2` is now used to select that. This means that the valid combinations are: | Flag | Effect | |-------------------------------|-----------------------------------------------| | _No flags_ | Dynamically-linked against the system libgit2 | | `-tags static,system_libgit2` | Statically-linked against the system libgit2 | | `-tags static` | Statically-linked against the bundled libgit2 | Note that there is no way to express dynamically linking against the bundled libgit2 because that makes very little sense, since the binaries wouldn't be able to be distributed. If that's still desired, the `PKG_CONFIG_PATH` environment variable can set before building the code. [`Makefile`](https://github.com/libgit2/git2go/blob/master/Makefile) has an example of how it is used in the CI. (cherry picked from commit 20a55cdf92f4ad6af4110861811a7076056cdf36) --- README.md | 14 +++++++++++--- git_static.go => git_bundled_static.go | 4 ++-- git_dynamic.go => git_system_dynamic.go | 4 ++-- git_system_static.go | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) rename git_static.go => git_bundled_static.go (86%) rename git_dynamic.go => git_system_dynamic.go (87%) create mode 100644 git_system_static.go diff --git a/README.md b/README.md index 8908fc06c..771c24872 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,17 @@ When linking dynamically against a released version of libgit2, install it via y import "github.com/libgit2/git2go/v27" ``` -### Master branch, or static linking +### Versioned branch, static linking -If using `master` or building a branch statically, we need to build libgit2 first. In order to build it, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL (outside of Windows or macOS) and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. Note that even if libgit2 is included in the resulting binary, its dependencies will not be. +Follow the instructions for [Versioned branch, dynamic linking](#versioned-branch-dynamic-linking), but pass the `-tags static,system_libgit2` flag to all `go` commands that build any binaries. For instance: + + go build -tags static,system_libgit2 github.com/my/project/... + go test -tags static,system_libgit2 github.com/my/project/... + go install -tags static,system_libgit2 github.com/my/project/... + +### Master branch, or vendored static linking + +If using `master` or building a branch with the vendored libgit2 statically, we need to build libgit2 first. In order to build it, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL (outside of Windows or macOS) and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. Note that even if libgit2 is included in the resulting binary, its dependencies will not be. Run `go get -d github.com/libgit2/git2go` to download the code and go to your `$GOPATH/src/github.com/libgit2/git2go` directory. From there, we need to build the C code and put it into the resulting go binary. @@ -83,7 +91,7 @@ For the stable version, `go test` will work as usual. For the `master` branch, s Alternatively, you can build the library manually first and then run the tests - ./script/build-libgit2-static.sh + make install-static go test -v -tags static ./... License diff --git a/git_static.go b/git_bundled_static.go similarity index 86% rename from git_static.go rename to git_bundled_static.go index 547ae8a33..e66f425d7 100644 --- a/git_static.go +++ b/git_bundled_static.go @@ -1,4 +1,4 @@ -// +build static +// +build static,!system_libgit2 package git @@ -6,11 +6,11 @@ package git #cgo windows CFLAGS: -I${SRCDIR}/static-build/install/include/ #cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp #cgo !windows pkg-config: --static ${SRCDIR}/static-build/install/lib/pkgconfig/libgit2.pc +#cgo CFLAGS: -DLIBGIT2_STATIC #include #if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 27 # error "Invalid libgit2 version; this git2go supports libgit2 v0.27" #endif - */ import "C" diff --git a/git_dynamic.go b/git_system_dynamic.go similarity index 87% rename from git_dynamic.go rename to git_system_dynamic.go index 0a977e876..eda95e536 100644 --- a/git_dynamic.go +++ b/git_system_dynamic.go @@ -3,12 +3,12 @@ package git /* -#include #cgo pkg-config: libgit2 +#cgo CFLAGS: -DLIBGIT2_DYNAMIC +#include #if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 27 # error "Invalid libgit2 version; this git2go supports libgit2 v0.27" #endif - */ import "C" diff --git a/git_system_static.go b/git_system_static.go new file mode 100644 index 000000000..cc754b485 --- /dev/null +++ b/git_system_static.go @@ -0,0 +1,14 @@ +// +build static,system_libgit2 + +package git + +/* +#cgo pkg-config: libgit2 --static +#cgo CFLAGS: -DLIBGIT2_STATIC +#include + +#if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 27 +# error "Invalid libgit2 version; this git2go supports libgit2 v0.27" +#endif +*/ +import "C" From 5c0c2356cc0c5146d5ff88db4b54b7c27757559a Mon Sep 17 00:00:00 2001 From: Jesse Hathaway Date: Fri, 10 Jul 2020 15:00:52 -0500 Subject: [PATCH 055/103] FetchOptions: add ability to specify ProxyOptions (#623) Prior to this change you could not specifiy proxy options on the FetchOptions struct, which made it impossible to specify a proxy for an initial clone. This change adds the ProxyOptions to the FetchOptions struct so you can go through a proxy when cloning. (cherry picked from commit b1cad11555ac966c1575375eb69ebb6c6463b27f) --- remote.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/remote.go b/remote.go index 59a60143a..9d1705460 100644 --- a/remote.go +++ b/remote.go @@ -116,6 +116,9 @@ type FetchOptions struct { // Headers are extra headers for the fetch operation. Headers []string + + // Proxy options to use for this fetch operation + ProxyOptions ProxyOptions } type ProxyType uint @@ -687,6 +690,7 @@ func populateFetchOptions(options *C.git_fetch_options, opts *FetchOptions) { options.custom_headers = C.git_strarray{} options.custom_headers.count = C.size_t(len(opts.Headers)) options.custom_headers.strings = makeCStringsFromStrings(opts.Headers) + populateProxyOptions(&options.proxy_opts, &opts.ProxyOptions) } func populatePushOptions(options *C.git_push_options, opts *PushOptions) { From 7a89ced8fc7cc334a19e72ac0324e89ea64639da Mon Sep 17 00:00:00 2001 From: Yuichi Watanabe Date: Thu, 30 Jul 2020 21:07:05 +0900 Subject: [PATCH 056/103] Add support for git_blob_is_binary (#625) This adds IsBinary() func to Blob struct, which simply returns the result of git_blob_is_binary function. Refs: https://libgit2.org/libgit2/#HEAD/group/blob/git_blob_is_binary Issue: Add support for git_blob_is_binary #426 Fixes: #426 (cherry picked from commit 462ebd83e0ccba9cd93c05ec12dc3d98064e3d5c) --- blob.go | 6 ++++++ blob_test.go | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/blob.go b/blob.go index d895449c2..e8296bba5 100644 --- a/blob.go +++ b/blob.go @@ -40,6 +40,12 @@ func (v *Blob) Contents() []byte { return goBytes } +func (v *Blob) IsBinary() bool { + ret := C.git_blob_is_binary(v.cast_ptr) == 1 + runtime.KeepAlive(v) + return ret +} + func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() diff --git a/blob_test.go b/blob_test.go index 815ab3d2a..2ab12912a 100644 --- a/blob_test.go +++ b/blob_test.go @@ -28,7 +28,21 @@ func TestCreateBlobFromBuffer(t *testing.T) { t.Fatal("Empty buffer did not deliver empty blob id") } - for _, data := range []([]byte){[]byte("hello there"), doublePointerBytes()} { + tests := []struct { + data []byte + isBinary bool + }{ + { + data: []byte("hello there"), + isBinary: false, + }, + { + data: doublePointerBytes(), + isBinary: true, + }, + } + for _, tt := range tests { + data := tt.data id, err = repo.CreateBlobFromBuffer(data) checkFatal(t, err) @@ -38,5 +52,9 @@ func TestCreateBlobFromBuffer(t *testing.T) { t.Fatal("Loaded bytes don't match original bytes:", blob.Contents(), "!=", data) } + want := tt.isBinary + if got := blob.IsBinary(); got != want { + t.Fatalf("IsBinary() = %v, want %v", got, want) + } } } From ff0fed0c323384fb5f1c54e1819a9171e9438882 Mon Sep 17 00:00:00 2001 From: michael boulton <61595820+mbfr@users.noreply.github.com> Date: Fri, 14 Aug 2020 19:19:21 +0100 Subject: [PATCH 057/103] Fix null pointer dereference in status.ByIndex (#628) `git_status_byindex` can return a null pointer if there is no statuses. (cherry picked from commit fc6eaf36388841b16ff004e1d48e887d3f9613dc) --- status.go | 4 ++++ status_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/status.go b/status.go index e37a96d53..3923e1a45 100644 --- a/status.go +++ b/status.go @@ -6,6 +6,7 @@ package git import "C" import ( + "errors" "runtime" "unsafe" ) @@ -86,6 +87,9 @@ func (statusList *StatusList) ByIndex(index int) (StatusEntry, error) { return StatusEntry{}, ErrInvalid } ptr := C.git_status_byindex(statusList.ptr, C.size_t(index)) + if ptr == nil { + return StatusEntry{}, errors.New("index out of Bounds") + } entry := statusEntryFromC(ptr) runtime.KeepAlive(statusList) diff --git a/status_test.go b/status_test.go index 17ed94f41..d5cd530b2 100644 --- a/status_test.go +++ b/status_test.go @@ -61,3 +61,31 @@ func TestStatusList(t *testing.T) { t.Fatal("Incorrect entry path: ", entry.IndexToWorkdir.NewFile.Path) } } + +func TestStatusNothing(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + seedTestRepo(t, repo) + + opts := &StatusOptions{ + Show: StatusShowIndexAndWorkdir, + Flags: StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively, + } + + statusList, err := repo.StatusList(opts) + checkFatal(t, err) + + entryCount, err := statusList.EntryCount() + checkFatal(t, err) + + if entryCount != 0 { + t.Fatal("expected no statuses in empty repo") + } + + _, err = statusList.ByIndex(0) + if err == nil { + t.Error("expected error getting status by index") + } +} From 6453cf9f8ada2ca65ff48b515d2160cab2c8c987 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 15 Aug 2020 17:19:53 -0700 Subject: [PATCH 058/103] Refresh the GitHub Actions CI (#632) This change: * Builds the library with Go 1.14, too. * Builds the non-legacy tests with Ubuntu Focal (20.04). * Adds testing for system-wide libraries, both static and dynamic versions. * Fixes a typo in the README. (cherry picked from commit 53149517592d613935e6bcbcea18f8ddb6bb546e) --- .github/workflows/ci.yml | 52 +++++++++++++++++++++++++++++++++--- README.md | 2 +- script/build-libgit2.sh | 57 +++++++++++++++++++++++++++------------- 3 files changed, 88 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5472f22bd..88e6c57b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,10 +46,10 @@ jobs: strategy: fail-fast: false matrix: - go: [ '1.11', '1.12', '1.13' ] + go: [ '1.11', '1.12', '1.13', '1.14' ] name: Go ${{ matrix.go }} - runs-on: ubuntu-18.04 + runs-on: ubuntu-20.04 steps: - name: Set up Go @@ -71,13 +71,13 @@ jobs: fail-fast: false name: Go (dynamic) - runs-on: ubuntu-18.04 + runs-on: ubuntu-20.04 steps: - name: Set up Go uses: actions/setup-go@v1 with: - go-version: '1.13' + go-version: '1.14' id: go - name: Check out code into the Go module directory uses: actions/checkout@v1 @@ -87,3 +87,47 @@ jobs: make build-libgit2-dynamic - name: Test run: make test-dynamic + + build-system-dynamic: + strategy: + fail-fast: false + name: Go (system-wide, dynamic) + + runs-on: ubuntu-20.04 + + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: '1.14' + id: go + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + - name: Build libgit2 + run: | + git submodule update --init + sudo ./script/build-libgit2.sh --dynamic --system + - name: Test + run: make test + + build-system-static: + strategy: + fail-fast: false + name: Go (system-wide, static) + + runs-on: ubuntu-20.04 + + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: '1.14' + id: go + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + - name: Build libgit2 + run: | + git submodule update --init + sudo ./script/build-libgit2.sh --static --system + - name: Test + run: go test --count=1 --tags "static,system_libgit2" ./... diff --git a/README.md b/README.md index 771c24872..20c11e08a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ The `master` branch follows the tip of libgit2 itself (with some lag) and as suc ### Which branch to send Pull requests to -If there's something version-specific that you'd want to contribute to, you can send them to the `release-${MAJOR}-${MINOR}` branches, which follow libgit2's releases. +If there's something version-specific that you'd want to contribute to, you can send them to the `release-${MAJOR}.${MINOR}` branches, which follow libgit2's releases. Installing ---------- diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh index fbb05abda..3cbfa4ef1 100755 --- a/script/build-libgit2.sh +++ b/script/build-libgit2.sh @@ -6,40 +6,61 @@ set -e -if [ "$#" -eq "0" ]; then - echo "Usage: $0 <--dynamic|--static>">&2 +usage() { + echo "Usage: $0 <--dynamic|--static> [--system]">&2 exit 1 +} + +if [ "$#" -eq "0" ]; then + usage fi ROOT="$(cd "$(dirname "$0")/.." && echo "${PWD}")" 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 + ;; -case "$1" in - --static) - BUILD_PATH="${ROOT}/static-build" - BUILD_SHARED_LIBS=OFF - ;; + --dynamic) + BUILD_PATH="${ROOT}/dynamic-build" + BUILD_SHARED_LIBS=ON + ;; - --dynamic) - BUILD_PATH="${ROOT}/dynamic-build" - BUILD_SHARED_LIBS=ON - ;; + --system) + BUILD_SYSTEM=ON + ;; - *) - echo "Usage: $0 <--dynamic|--static>">&2 - exit 1 - ;; -esac + *) + usage + ;; + esac + shift +done -mkdir -p "${BUILD_PATH}/build" "${BUILD_PATH}/install/lib" +if [ -z "${BUILD_SHARED_LIBS}" ]; then + usage +fi + +if [ "${BUILD_SYSTEM}" = "ON" ]; then + BUILD_INSTALL_PREFIX="/usr" +else + BUILD_INSTALL_PREFIX="${BUILD_PATH}/install" + mkdir -p "${BUILD_PATH}/install/lib" +fi +mkdir -p "${BUILD_PATH}/build" && cd "${BUILD_PATH}/build" && cmake -DTHREADSAFE=ON \ -DBUILD_CLAR=OFF \ -DBUILD_SHARED_LIBS"=${BUILD_SHARED_LIBS}" \ -DCMAKE_C_FLAGS=-fPIC \ -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ - -DCMAKE_INSTALL_PREFIX="${BUILD_PATH}/install" \ + -DCMAKE_INSTALL_PREFIX="${BUILD_INSTALL_PREFIX}" \ "${VENDORED_PATH}" && exec cmake --build . --target install From 0430fd700c88953686644dd27c0b39a8a5ef9f41 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 16 Aug 2020 07:28:35 -0700 Subject: [PATCH 059/103] Add two more GitHub Actions workflows (#633) (#635) This change adds: * `tag.yml`: Creates a new tag every time the master or a release branch is pushed. * `backport.yml`: Creates a cherry-pick in older release branches to keep them up to date with little cost. (cherry picked from commit 2ac9f4e69bd57a686d15176d199a3c9cc4a6bb91) --- .github/workflows/backport.yml | 53 ++++++++++++++++++++++++++++++++++ .github/workflows/tag.yml | 28 ++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 .github/workflows/backport.yml create mode 100644 .github/workflows/tag.yml diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml new file mode 100644 index 000000000..4337fb1b4 --- /dev/null +++ b/.github/workflows/backport.yml @@ -0,0 +1,53 @@ +name: Backport to older releases +on: + push: + branches: + - master + +jobs: + + backport: + strategy: + fail-fast: false + matrix: + branch: [ 'release-0.28', 'release-0.27' ] + name: Backport change to branch ${{ matrix.branch }} + + runs-on: ubuntu-20.04 + + steps: + - name: Check out code + uses: actions/checkout@v1 + with: + fetch-depth: 0 + - name: Create a cherry-pick PR + run: | + if ! git diff --quiet HEAD^ HEAD -- vendor/libgit2; then + echo '::warning::Skipping cherry-pick since it is a vendored libgit2 bump' + exit 0 + fi + + BRANCH_NAME="cherry-pick-${{ github.run_id }}-${{ matrix.branch }}" + + # Setup usernames and authentication + git config --global user.name "${{ github.actor }}" + git config --global user.email "${{ github.actor }}@users.noreply.github.com" + cat <<- EOF > $HOME/.netrc + machine github.com + login ${{ github.actor }} + password ${{ secrets.GITHUB_TOKEN }} + machine api.github.com + login ${{ github.actor }} + password ${{ secrets.GITHUB_TOKEN }} + EOF + chmod 600 $HOME/.netrc + + # Create the cherry-pick commit and create the PR for it. + git checkout "${{ matrix.branch }}" + git switch -c "${BRANCH_NAME}" + git cherry-pick -x "${{ github.sha }}" + git push --set-upstream origin "${BRANCH_NAME}" + GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}" gh pr create \ + --base "${{ matrix.branch }}" \ + --title "$(git --no-pager show --format="%s" --no-patch HEAD)" \ + --body "$(git --no-pager show --format="%b" --no-patch HEAD)" diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml new file mode 100644 index 000000000..b29327450 --- /dev/null +++ b/.github/workflows/tag.yml @@ -0,0 +1,28 @@ +name: Tag new releases +on: + push: + branches: + - master + - release-* + +jobs: + + tag-release: + name: Bump tag in ${{ github.ref }} + + runs-on: ubuntu-20.04 + + steps: + - name: Check out code + uses: actions/checkout@v1 + with: + fetch-depth: 0 + - name: Bump version and push tag + id: bump-version + uses: anothrNick/github-tag-action@9aaabdb5e989894e95288328d8b17a6347217ae3 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + WITH_V: true + DEFAULT_BUMP: patch + TAG_CONTEXT: branch + RELEASE_BRANCHES: .* From 4d690277874bce93e81a1f1a12c9ec9ba3daca0f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 18 Aug 2020 06:21:19 -0700 Subject: [PATCH 060/103] More diff functionality (#629) (#636) This PR adds - The ability to apply a Diff object to the repo - Support for git_apply_hunk_cb and git_apply_delta_cb callbacks in options for applying the diffs - The ability to import a diff from a raw buffer (for example, one exported by ToBuf) into a Diff object associated with the repo - Tests for the above (cherry picked from commit 7883ec85de56ee55667481228282fd690fce6246) --- diff.go | 174 ++++++++++++++++++++++++++++- diff_test.go | 307 +++++++++++++++++++++++++++++++++++++++++++++++++++ wrapper.c | 6 + 3 files changed, 486 insertions(+), 1 deletion(-) diff --git a/diff.go b/diff.go index 7f824fa6f..cd8793b6e 100644 --- a/diff.go +++ b/diff.go @@ -3,6 +3,7 @@ package git /* #include +extern void _go_git_populate_apply_cb(git_apply_options *options); extern int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload); extern void _go_git_setup_diff_notify_callbacks(git_diff_options* opts); extern int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload); @@ -550,7 +551,7 @@ const ( DiffFindRemoveUnmodified DiffFindOptionsFlag = C.GIT_DIFF_FIND_REMOVE_UNMODIFIED ) -//TODO implement git_diff_similarity_metric +// TODO implement git_diff_similarity_metric type DiffFindOptions struct { Flags DiffFindOptionsFlag RenameThreshold uint16 @@ -847,3 +848,174 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, return nil } + +// ApplyHunkCallback is a callback that will be made per delta (file) when applying a patch. +type ApplyHunkCallback func(*DiffHunk) (apply bool, err error) + +// ApplyDeltaCallback is a callback that will be made per hunk when applying a patch. +type ApplyDeltaCallback func(*DiffDelta) (apply bool, err error) + +// ApplyOptions has 2 callbacks that are called for hunks or deltas +// If these functions return an error, abort the apply process immediately. +// If the first return value is true, the delta/hunk will be applied. If it is false, the delta/hunk will not be applied. In either case, the rest of the apply process will continue. +type ApplyOptions struct { + ApplyHunkCallback ApplyHunkCallback + ApplyDeltaCallback ApplyDeltaCallback + Flags uint +} + +//export hunkApplyCallback +func hunkApplyCallback(_hunk *C.git_diff_hunk, _payload unsafe.Pointer) C.int { + opts, ok := pointerHandles.Get(_payload).(*ApplyOptions) + if !ok { + panic("invalid apply options payload") + } + + if opts.ApplyHunkCallback == nil { + return 0 + } + + hunk := diffHunkFromC(_hunk) + + apply, err := opts.ApplyHunkCallback(&hunk) + if err != nil { + if gitError, ok := err.(*GitError); ok { + return C.int(gitError.Code) + } + return -1 + } else if apply { + return 0 + } else { + return 1 + } +} + +//export deltaApplyCallback +func deltaApplyCallback(_delta *C.git_diff_delta, _payload unsafe.Pointer) C.int { + opts, ok := pointerHandles.Get(_payload).(*ApplyOptions) + if !ok { + panic("invalid apply options payload") + } + + if opts.ApplyDeltaCallback == nil { + return 0 + } + + delta := diffDeltaFromC(_delta) + + apply, err := opts.ApplyDeltaCallback(&delta) + if err != nil { + if gitError, ok := err.(*GitError); ok { + return C.int(gitError.Code) + } + return -1 + } else if apply { + return 0 + } else { + return 1 + } +} + +// DefaultApplyOptions returns default options for applying diffs or patches. +func DefaultApplyOptions() (*ApplyOptions, error) { + opts := C.git_apply_options{} + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_apply_options_init(&opts, C.GIT_APPLY_OPTIONS_VERSION) + if int(ecode) != 0 { + + return nil, MakeGitError(ecode) + } + + return applyOptionsFromC(&opts), nil +} + +func (a *ApplyOptions) toC() *C.git_apply_options { + if a == nil { + return nil + } + + opts := &C.git_apply_options{ + version: C.GIT_APPLY_OPTIONS_VERSION, + flags: C.uint(a.Flags), + } + + if a.ApplyDeltaCallback != nil || a.ApplyHunkCallback != nil { + C._go_git_populate_apply_cb(opts) + opts.payload = pointerHandles.Track(a) + } + + return opts +} + +func applyOptionsFromC(opts *C.git_apply_options) *ApplyOptions { + return &ApplyOptions{ + Flags: uint(opts.flags), + } +} + +// ApplyLocation represents the possible application locations for applying +// diffs. +type ApplyLocation int + +const ( + // ApplyLocationWorkdir applies the patch to the workdir, leaving the + // index untouched. This is the equivalent of `git apply` with no location + // argument. + ApplyLocationWorkdir ApplyLocation = C.GIT_APPLY_LOCATION_WORKDIR + // ApplyLocationIndex applies the patch to the index, leaving the working + // directory untouched. This is the equivalent of `git apply --cached`. + ApplyLocationIndex ApplyLocation = C.GIT_APPLY_LOCATION_INDEX + // ApplyLocationBoth applies the patch to both the working directory and + // the index. This is the equivalent of `git apply --index`. + ApplyLocationBoth ApplyLocation = C.GIT_APPLY_LOCATION_BOTH +) + +// ApplyDiff appllies a Diff to the given repository, making changes directly +// in the working directory, the index, or both. +func (v *Repository) ApplyDiff(diff *Diff, location ApplyLocation, opts *ApplyOptions) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cOpts := opts.toC() + ecode := C.git_apply(v.ptr, diff.ptr, C.git_apply_location_t(location), cOpts) + runtime.KeepAlive(v) + runtime.KeepAlive(diff) + runtime.KeepAlive(cOpts) + if ecode < 0 { + return MakeGitError(ecode) + } + + return nil +} + +// DiffFromBuffer reads the contents of a git patch file into a Diff object. +// +// The diff object produced is similar to the one that would be produced if you +// actually produced it computationally by comparing two trees, however there +// may be subtle differences. For example, a patch file likely contains +// abbreviated object IDs, so the object IDs in a git_diff_delta produced by +// this function will also be abbreviated. +// +// This function will only read patch files created by a git implementation, it +// will not read unified diffs produced by the diff program, nor any other +// types of patch files. +func DiffFromBuffer(buffer []byte, repo *Repository) (*Diff, error) { + var diff *C.git_diff + + cBuffer := C.CBytes(buffer) + defer C.free(unsafe.Pointer(cBuffer)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_diff_from_buffer(&diff, (*C.char)(cBuffer), C.size_t(len(buffer))) + if ecode < 0 { + return nil, MakeGitError(ecode) + } + runtime.KeepAlive(diff) + + return newDiffFromC(diff, repo), nil +} diff --git a/diff_test.go b/diff_test.go index 6fbad512e..394a4c154 100644 --- a/diff_test.go +++ b/diff_test.go @@ -2,6 +2,9 @@ package git import ( "errors" + "fmt" + "io/ioutil" + "path" "strings" "testing" ) @@ -236,3 +239,307 @@ func TestDiffBlobs(t *testing.T) { t.Fatalf("Bad number of lines iterated") } } + +func TestApplyDiffAddfile(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + seedTestRepo(t, repo) + + addFirstFileCommit, addFileTree := addAndGetTree(t, repo, "file1", `hello`) + addSecondFileCommit, addSecondFileTree := addAndGetTree(t, repo, "file2", `hello2`) + + diff, err := repo.DiffTreeToTree(addFileTree, addSecondFileTree, nil) + checkFatal(t, err) + + t.Run("check does not apply to current tree because file exists", func(t *testing.T) { + err = repo.ResetToCommit(addSecondFileCommit, ResetHard, &CheckoutOpts{}) + 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, &CheckoutOpts{}) + 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) + + tree, err := commit.Tree() + checkFatal(t, err) + + newDiff, err := repo.DiffTreeToTree(addFileTree, tree, nil) + checkFatal(t, err) + + 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, &CheckoutOpts{}) + 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) + + 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, &CheckoutOpts{}) + 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) + }) + }) +} + +// 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 +} diff --git a/wrapper.c b/wrapper.c index df767fb49..1093cf4e2 100644 --- a/wrapper.c +++ b/wrapper.c @@ -5,6 +5,12 @@ typedef int (*gogit_submodule_cbk)(git_submodule *sm, const char *name, void *payload); +void _go_git_populate_apply_cb(git_apply_options *options) +{ + options->delta_cb = (git_apply_delta_cb)deltaApplyCallback; + options->hunk_cb = (git_apply_hunk_cb)hunkApplyCallback; +} + void _go_git_populate_remote_cb(git_clone_options *opts) { opts->remote_cb = (git_remote_create_cb)remoteCreateCallback; From 9912ed9742b906725971b140d5939077c36dc384 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Tue, 18 Aug 2020 06:52:38 -0700 Subject: [PATCH 061/103] Revert "More diff functionality (#629) (#636)" (#639) This reverts commit 4d690277874bce93e81a1f1a12c9ec9ba3daca0f. v0.27 does not support this API. --- diff.go | 174 +---------------------------- diff_test.go | 307 --------------------------------------------------- wrapper.c | 6 - 3 files changed, 1 insertion(+), 486 deletions(-) diff --git a/diff.go b/diff.go index cd8793b6e..7f824fa6f 100644 --- a/diff.go +++ b/diff.go @@ -3,7 +3,6 @@ package git /* #include -extern void _go_git_populate_apply_cb(git_apply_options *options); extern int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload); extern void _go_git_setup_diff_notify_callbacks(git_diff_options* opts); extern int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload); @@ -551,7 +550,7 @@ const ( DiffFindRemoveUnmodified DiffFindOptionsFlag = C.GIT_DIFF_FIND_REMOVE_UNMODIFIED ) -// TODO implement git_diff_similarity_metric +//TODO implement git_diff_similarity_metric type DiffFindOptions struct { Flags DiffFindOptionsFlag RenameThreshold uint16 @@ -848,174 +847,3 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, return nil } - -// ApplyHunkCallback is a callback that will be made per delta (file) when applying a patch. -type ApplyHunkCallback func(*DiffHunk) (apply bool, err error) - -// ApplyDeltaCallback is a callback that will be made per hunk when applying a patch. -type ApplyDeltaCallback func(*DiffDelta) (apply bool, err error) - -// ApplyOptions has 2 callbacks that are called for hunks or deltas -// If these functions return an error, abort the apply process immediately. -// If the first return value is true, the delta/hunk will be applied. If it is false, the delta/hunk will not be applied. In either case, the rest of the apply process will continue. -type ApplyOptions struct { - ApplyHunkCallback ApplyHunkCallback - ApplyDeltaCallback ApplyDeltaCallback - Flags uint -} - -//export hunkApplyCallback -func hunkApplyCallback(_hunk *C.git_diff_hunk, _payload unsafe.Pointer) C.int { - opts, ok := pointerHandles.Get(_payload).(*ApplyOptions) - if !ok { - panic("invalid apply options payload") - } - - if opts.ApplyHunkCallback == nil { - return 0 - } - - hunk := diffHunkFromC(_hunk) - - apply, err := opts.ApplyHunkCallback(&hunk) - if err != nil { - if gitError, ok := err.(*GitError); ok { - return C.int(gitError.Code) - } - return -1 - } else if apply { - return 0 - } else { - return 1 - } -} - -//export deltaApplyCallback -func deltaApplyCallback(_delta *C.git_diff_delta, _payload unsafe.Pointer) C.int { - opts, ok := pointerHandles.Get(_payload).(*ApplyOptions) - if !ok { - panic("invalid apply options payload") - } - - if opts.ApplyDeltaCallback == nil { - return 0 - } - - delta := diffDeltaFromC(_delta) - - apply, err := opts.ApplyDeltaCallback(&delta) - if err != nil { - if gitError, ok := err.(*GitError); ok { - return C.int(gitError.Code) - } - return -1 - } else if apply { - return 0 - } else { - return 1 - } -} - -// DefaultApplyOptions returns default options for applying diffs or patches. -func DefaultApplyOptions() (*ApplyOptions, error) { - opts := C.git_apply_options{} - - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - ecode := C.git_apply_options_init(&opts, C.GIT_APPLY_OPTIONS_VERSION) - if int(ecode) != 0 { - - return nil, MakeGitError(ecode) - } - - return applyOptionsFromC(&opts), nil -} - -func (a *ApplyOptions) toC() *C.git_apply_options { - if a == nil { - return nil - } - - opts := &C.git_apply_options{ - version: C.GIT_APPLY_OPTIONS_VERSION, - flags: C.uint(a.Flags), - } - - if a.ApplyDeltaCallback != nil || a.ApplyHunkCallback != nil { - C._go_git_populate_apply_cb(opts) - opts.payload = pointerHandles.Track(a) - } - - return opts -} - -func applyOptionsFromC(opts *C.git_apply_options) *ApplyOptions { - return &ApplyOptions{ - Flags: uint(opts.flags), - } -} - -// ApplyLocation represents the possible application locations for applying -// diffs. -type ApplyLocation int - -const ( - // ApplyLocationWorkdir applies the patch to the workdir, leaving the - // index untouched. This is the equivalent of `git apply` with no location - // argument. - ApplyLocationWorkdir ApplyLocation = C.GIT_APPLY_LOCATION_WORKDIR - // ApplyLocationIndex applies the patch to the index, leaving the working - // directory untouched. This is the equivalent of `git apply --cached`. - ApplyLocationIndex ApplyLocation = C.GIT_APPLY_LOCATION_INDEX - // ApplyLocationBoth applies the patch to both the working directory and - // the index. This is the equivalent of `git apply --index`. - ApplyLocationBoth ApplyLocation = C.GIT_APPLY_LOCATION_BOTH -) - -// ApplyDiff appllies a Diff to the given repository, making changes directly -// in the working directory, the index, or both. -func (v *Repository) ApplyDiff(diff *Diff, location ApplyLocation, opts *ApplyOptions) error { - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - cOpts := opts.toC() - ecode := C.git_apply(v.ptr, diff.ptr, C.git_apply_location_t(location), cOpts) - runtime.KeepAlive(v) - runtime.KeepAlive(diff) - runtime.KeepAlive(cOpts) - if ecode < 0 { - return MakeGitError(ecode) - } - - return nil -} - -// DiffFromBuffer reads the contents of a git patch file into a Diff object. -// -// The diff object produced is similar to the one that would be produced if you -// actually produced it computationally by comparing two trees, however there -// may be subtle differences. For example, a patch file likely contains -// abbreviated object IDs, so the object IDs in a git_diff_delta produced by -// this function will also be abbreviated. -// -// This function will only read patch files created by a git implementation, it -// will not read unified diffs produced by the diff program, nor any other -// types of patch files. -func DiffFromBuffer(buffer []byte, repo *Repository) (*Diff, error) { - var diff *C.git_diff - - cBuffer := C.CBytes(buffer) - defer C.free(unsafe.Pointer(cBuffer)) - - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - ecode := C.git_diff_from_buffer(&diff, (*C.char)(cBuffer), C.size_t(len(buffer))) - if ecode < 0 { - return nil, MakeGitError(ecode) - } - runtime.KeepAlive(diff) - - return newDiffFromC(diff, repo), nil -} diff --git a/diff_test.go b/diff_test.go index 394a4c154..6fbad512e 100644 --- a/diff_test.go +++ b/diff_test.go @@ -2,9 +2,6 @@ package git import ( "errors" - "fmt" - "io/ioutil" - "path" "strings" "testing" ) @@ -239,307 +236,3 @@ func TestDiffBlobs(t *testing.T) { t.Fatalf("Bad number of lines iterated") } } - -func TestApplyDiffAddfile(t *testing.T) { - repo := createTestRepo(t) - defer cleanupTestRepo(t, repo) - - seedTestRepo(t, repo) - - addFirstFileCommit, addFileTree := addAndGetTree(t, repo, "file1", `hello`) - addSecondFileCommit, addSecondFileTree := addAndGetTree(t, repo, "file2", `hello2`) - - diff, err := repo.DiffTreeToTree(addFileTree, addSecondFileTree, nil) - checkFatal(t, err) - - t.Run("check does not apply to current tree because file exists", func(t *testing.T) { - err = repo.ResetToCommit(addSecondFileCommit, ResetHard, &CheckoutOpts{}) - 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, &CheckoutOpts{}) - 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) - - tree, err := commit.Tree() - checkFatal(t, err) - - newDiff, err := repo.DiffTreeToTree(addFileTree, tree, nil) - checkFatal(t, err) - - 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, &CheckoutOpts{}) - 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) - - 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, &CheckoutOpts{}) - 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) - }) - }) -} - -// 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 -} diff --git a/wrapper.c b/wrapper.c index 1093cf4e2..df767fb49 100644 --- a/wrapper.c +++ b/wrapper.c @@ -5,12 +5,6 @@ typedef int (*gogit_submodule_cbk)(git_submodule *sm, const char *name, void *payload); -void _go_git_populate_apply_cb(git_apply_options *options) -{ - options->delta_cb = (git_apply_delta_cb)deltaApplyCallback; - options->hunk_cb = (git_apply_hunk_cb)hunkApplyCallback; -} - void _go_git_populate_remote_cb(git_clone_options *opts) { opts->remote_cb = (git_remote_create_cb)remoteCreateCallback; From 3d80bd22ad0f884d94733d06c446bb5e3411fa31 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Tue, 18 Aug 2020 09:52:46 -0700 Subject: [PATCH 062/103] Add support for creating signed commits (#626) (#641) (cherry picked from commit 7d4453198b55ecc2d9e09b64352edecb5db8b6ef) This is only a partial cherry-pick, since signing commits during a rebase is not supported in v0.27. --- .github/workflows/ci.yml | 2 +- commit.go | 63 ++++++++++++++++++++++++++++++++++++++++ git_test.go | 31 ++++++++++++++++++++ rebase_test.go | 55 +++++++++++++++++++++++++---------- 4 files changed, 135 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88e6c57b2..42636bc15 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,7 +35,7 @@ jobs: run: | git submodule update --init make build-libgit2-static - go get -tags static github.com/${{ github.repository }}/... + go get -tags static -t github.com/${{ github.repository }}/... go build -tags static github.com/${{ github.repository }}/... - name: Test env: diff --git a/commit.go b/commit.go index 6fdfacf7d..10bf96a80 100644 --- a/commit.go +++ b/commit.go @@ -40,6 +40,69 @@ func (c *Commit) RawMessage() string { return ret } +// RawHeader gets the full raw text of the commit header. +func (c *Commit) RawHeader() string { + ret := C.GoString(C.git_commit_raw_header(c.cast_ptr)) + runtime.KeepAlive(c) + return ret +} + +// ContentToSign returns the content that will be passed to a signing function for this commit +func (c *Commit) ContentToSign() string { + return c.RawHeader() + "\n" + c.RawMessage() +} + +// CommitSigningCallback defines a function type that takes some data to sign and returns (signature, signature_field, error) +type CommitSigningCallback func(string) (signature, signatureField string, err error) + +// WithSignatureUsing creates a new signed commit from this one using the given signing callback +func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) { + signature, signatureField, err := f(c.ContentToSign()) + if err != nil { + return nil, err + } + + return c.WithSignature(signature, signatureField) +} + +// WithSignature creates a new signed commit from the given signature and signature field +func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, error) { + totalCommit := c.ContentToSign() + + oid := new(Oid) + + var csf *C.char = nil + if signatureField != "" { + csf = C.CString(signatureField) + defer C.free(unsafe.Pointer(csf)) + } + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cTotalCommit := C.CString(totalCommit) + cSignature := C.CString(signature) + defer C.free(unsafe.Pointer(cTotalCommit)) + defer C.free(unsafe.Pointer(cSignature)) + + ret := C.git_commit_create_with_signature( + oid.toC(), + c.Owner().ptr, + cTotalCommit, + cSignature, + csf, + ) + + runtime.KeepAlive(c) + runtime.KeepAlive(oid) + + if ret < 0 { + return nil, MakeGitError(ret) + } + + return oid, nil +} + func (c *Commit) ExtractSignature() (string, string, error) { var c_signed C.git_buf diff --git a/git_test.go b/git_test.go index 807dcc222..91ade73b4 100644 --- a/git_test.go +++ b/git_test.go @@ -45,7 +45,16 @@ func createBareTestRepo(t *testing.T) *Repository { return repo } +// commitOpts contains any extra options for creating commits in the seed repo +type commitOpts struct { + CommitSigningCallback +} + func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) { + return seedTestRepoOpt(t, repo, commitOpts{}) +} + +func seedTestRepoOpt(t *testing.T, repo *Repository, opts commitOpts) (*Oid, *Oid) { loc, err := time.LoadLocation("Europe/Berlin") checkFatal(t, err) sig := &Signature{ @@ -69,6 +78,28 @@ func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) { commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree) checkFatal(t, err) + if opts.CommitSigningCallback != nil { + commit, err := repo.LookupCommit(commitId) + checkFatal(t, err) + + signature, signatureField, err := opts.CommitSigningCallback(commit.ContentToSign()) + checkFatal(t, err) + + oid, err := commit.WithSignature(signature, signatureField) + checkFatal(t, err) + newCommit, err := repo.LookupCommit(oid) + checkFatal(t, err) + head, err := repo.Head() + checkFatal(t, err) + _, err = repo.References.Create( + head.Name(), + newCommit.Id(), + true, + "repoint to signed commit", + ) + checkFatal(t, err) + } + return commitId, treeId } diff --git a/rebase_test.go b/rebase_test.go index ef4f92028..c7deef6d9 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -33,12 +33,12 @@ func TestRebaseAbort(t *testing.T) { seedTestRepo(t, repo) // Setup a repo with 2 branches and a different tree - err := setupRepoForRebase(repo, masterCommit, branchName) + err := setupRepoForRebase(repo, masterCommit, branchName, commitOpts{}) checkFatal(t, err) // Create several commits in emile for _, commit := range emileCommits { - _, err = commitSomething(repo, commit, commit) + _, err = commitSomething(repo, commit, commit, commitOpts{}) checkFatal(t, err) } @@ -48,7 +48,7 @@ func TestRebaseAbort(t *testing.T) { assertStringList(t, expectedHistory, actualHistory) // Rebase onto master - rebase, err := performRebaseOnto(repo, "master") + rebase, err := performRebaseOnto(repo, "master", nil) checkFatal(t, err) defer rebase.Free() @@ -94,17 +94,17 @@ func TestRebaseNoConflicts(t *testing.T) { } // Setup a repo with 2 branches and a different tree - err = setupRepoForRebase(repo, masterCommit, branchName) + err = setupRepoForRebase(repo, masterCommit, branchName, commitOpts{}) checkFatal(t, err) // Create several commits in emile for _, commit := range emileCommits { - _, err = commitSomething(repo, commit, commit) + _, err = commitSomething(repo, commit, commit, commitOpts{}) checkFatal(t, err) } // Rebase onto master - rebase, err := performRebaseOnto(repo, "master") + rebase, err := performRebaseOnto(repo, "master", nil) checkFatal(t, err) defer rebase.Free() @@ -130,11 +130,10 @@ func TestRebaseNoConflicts(t *testing.T) { actualHistory, err := commitMsgsList(repo) checkFatal(t, err) assertStringList(t, expectedHistory, actualHistory) - } // Utils -func setupRepoForRebase(repo *Repository, masterCommit, branchName string) error { +func setupRepoForRebase(repo *Repository, masterCommit, branchName string, opts commitOpts) error { // Create a new branch from master err := createBranch(repo, branchName) if err != nil { @@ -142,7 +141,7 @@ func setupRepoForRebase(repo *Repository, masterCommit, branchName string) error } // Create a commit in master - _, err = commitSomething(repo, masterCommit, masterCommit) + _, err = commitSomething(repo, masterCommit, masterCommit, opts) if err != nil { return err } @@ -161,7 +160,7 @@ func setupRepoForRebase(repo *Repository, masterCommit, branchName string) error return nil } -func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { +func performRebaseOnto(repo *Repository, branch string, opts *RebaseOptions) (*Rebase, error) { master, err := repo.LookupBranch(branch, BranchLocal) if err != nil { return nil, err @@ -175,7 +174,7 @@ func performRebaseOnto(repo *Repository, branch string) (*Rebase, error) { defer onto.Free() // Init rebase - rebase, err := repo.InitRebase(nil, nil, onto, nil) + rebase, err := repo.InitRebase(nil, nil, onto, opts) if err != nil { return nil, err } @@ -276,7 +275,7 @@ func headTree(repo *Repository) (*Tree, error) { return tree, nil } -func commitSomething(repo *Repository, something, content string) (*Oid, error) { +func commitSomething(repo *Repository, something, content string, commitOpts commitOpts) (*Oid, error) { headCommit, err := headCommit(repo) if err != nil { return nil, err @@ -315,14 +314,40 @@ func commitSomething(repo *Repository, something, content string) (*Oid, error) } defer newTree.Free() - if err != nil { - return nil, err - } commit, err := repo.CreateCommit("HEAD", signature(), signature(), "Test rebase, Baby! "+something, newTree, headCommit) if err != nil { return nil, err } + 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 + } + } + opts := &CheckoutOpts{ Strategy: CheckoutRemoveUntracked | CheckoutForce, } From 627f58d4038cdbc20a1ea96dc43ee06709528d2f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 18 Sep 2020 06:23:21 -0700 Subject: [PATCH 063/103] merge: Expose recursion limit merge option (#642) (#644) The `recursion_limit` merge option provided by libgit2 is currently not exposed and thus inaccessible to Git2Go users. Let's expose it to let users control creation of recursive merge bases. (cherry picked from commit 7e726fda6ec2b5d773e5a8b54ed06378a53c0f7f) Co-authored-by: Patrick Steinhardt --- merge.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/merge.go b/merge.go index 4f097b7d3..da1c3d612 100644 --- a/merge.go +++ b/merge.go @@ -142,6 +142,7 @@ type MergeOptions struct { RenameThreshold uint TargetLimit uint + RecursionLimit uint FileFavor MergeFileFavor //TODO: Diff similarity metric @@ -153,6 +154,7 @@ func mergeOptionsFromC(opts *C.git_merge_options) MergeOptions { TreeFlags: MergeTreeFlag(opts.flags), RenameThreshold: uint(opts.rename_threshold), TargetLimit: uint(opts.target_limit), + RecursionLimit: uint(opts.recursion_limit), FileFavor: MergeFileFavor(opts.file_favor), } } @@ -179,6 +181,7 @@ func (mo *MergeOptions) toC() *C.git_merge_options { flags: C.git_merge_flag_t(mo.TreeFlags), rename_threshold: C.uint(mo.RenameThreshold), target_limit: C.uint(mo.TargetLimit), + recursion_limit: C.uint(mo.RecursionLimit), file_favor: C.git_merge_file_favor_t(mo.FileFavor), } } From a81a08606fb54ba61c0179739b793ec8fbc3558b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 29 Sep 2020 15:51:32 -0700 Subject: [PATCH 064/103] Add a ReInit function (#647) (#649) libgit2 stores the lookup paths for gitconfig files in its global state. This means that after a program changes its effective uid libgit2 will still look for gitconfig files in the home directory of the original uid. Expose a way to call C.git_libgit2_init so a user can reinitialize the libgit2 global state. (cherry picked from commit 3c5c580d78831d10e082743f3783424b72ac9e09) Co-authored-by: Jesse Hathaway --- git.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/git.go b/git.go index ff9b64314..d83350595 100644 --- a/git.go +++ b/git.go @@ -118,6 +118,10 @@ var ( var pointerHandles *HandleList func init() { + initLibGit2() +} + +func initLibGit2() { pointerHandles = NewHandleList() C.git_libgit2_init() @@ -149,6 +153,15 @@ func Shutdown() { 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 From 6cb9c7cf4136c14b59768c81a4e623b89f5eaeff Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 30 Sep 2020 17:16:28 -0700 Subject: [PATCH 065/103] Enable set the VENDORED_PATH for libgit2 (#650) (#652) ### What this change is doing? This change aims to enable us to set the `VENDORED_PATH` as an environment variable and failback to the default value if the environment variable is not set. Thus, this change should not break current behavior. ### Why we need this? This will enable using the script to build libgit2 form source code downloaded manually. Example: ```sh LIBGIT2_VER="1.0.1" DOWNLOAD_URL="https://codeload.github.com/libgit2/libgit2/tar.gz/v${LIBGIT2_VER}"" LIBGIT2_PATH="${HOME}/libgit2-${LIBGIT2_VER}" wget -O "${LIBGIT2_PATH}.tar.gz" "$DOWNLOAD_URL" tar -xzvf "${LIBGIT2_PATH}.tar.gz" VENDORED_PATH=$LIBGIT2_PATH sh ./script/build-libgit2.sh --static ``` (cherry picked from commit f3a746d7b6a27a9d6f98143641466f68ef1f3dee) Co-authored-by: Suhaib Mujahid --- script/build-libgit2.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh index 3cbfa4ef1..a541849cd 100755 --- a/script/build-libgit2.sh +++ b/script/build-libgit2.sh @@ -15,8 +15,8 @@ if [ "$#" -eq "0" ]; then usage fi -ROOT="$(cd "$(dirname "$0")/.." && echo "${PWD}")" -VENDORED_PATH="${ROOT}/vendor/libgit2" +ROOT=${ROOT-"$(cd "$(dirname "$0")/.." && echo "${PWD}")"} +VENDORED_PATH=${VENDORED_PATH-"${ROOT}/vendor/libgit2"} BUILD_SYSTEM=OFF while [ $# -gt 0 ]; do From 27f87bd821239fda40db72790d2eca4bde9e3ac0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 30 Sep 2020 17:31:20 -0700 Subject: [PATCH 066/103] feat: Enable change the system install path (#653) (#654) #### Problem: The current `CMAKE_INSTALL_PREFIX` value for the `system` build mode is `/usr`. However, in`macOS` as an example, you cannot write to `/usr` without `sudo` permission. #### Proposed solution: Enable changing the value to `/usr/local` (or any other path). This change makes the script use the value of the environment variable `SYSTEM_INSTALL_PREFIX` to select the installation path. If the variable is not set, it fallback to the path `/usr`. (cherry picked from commit 111185838cebe3415e47c75e67fb81295952ce68) Co-authored-by: Suhaib Mujahid --- script/build-libgit2.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh index a541849cd..8888ec913 100755 --- a/script/build-libgit2.sh +++ b/script/build-libgit2.sh @@ -47,7 +47,7 @@ if [ -z "${BUILD_SHARED_LIBS}" ]; then fi if [ "${BUILD_SYSTEM}" = "ON" ]; then - BUILD_INSTALL_PREFIX="/usr" + BUILD_INSTALL_PREFIX=${SYSTEM_INSTALL_PREFIX-"/usr"} else BUILD_INSTALL_PREFIX="${BUILD_PATH}/install" mkdir -p "${BUILD_PATH}/install/lib" From f58d71b8a9228a1d1a1966c0dcd4a3a63e8a7439 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 22 Oct 2020 06:05:23 -0700 Subject: [PATCH 067/103] repository: Implement wrappers for `git_object_lookup_prefix` (#658) (#659) While we already have wrappers for `git_object_lookup`, there are none yet for the prefixed variant where only the first n bytes of the OID are used for the lookup. This commit adds them. (cherry picked from commit 37b81b61f16f4bdf891a54dc8311bd5d2236e329) Co-authored-by: Patrick Steinhardt --- repository.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/repository.go b/repository.go index b645051f8..fdd38f6bc 100644 --- a/repository.go +++ b/repository.go @@ -185,10 +185,30 @@ func (v *Repository) lookupType(id *Oid, t ObjectType) (*Object, error) { return allocObject(ptr, v), nil } +func (v *Repository) lookupPrefixType(id *Oid, prefix uint, t ObjectType) (*Object, error) { + var ptr *C.git_object + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C.git_object_lookup_prefix(&ptr, v.ptr, id.toC(), C.size_t(prefix), C.git_otype(t)) + runtime.KeepAlive(id) + if ret < 0 { + return nil, MakeGitError(ret) + } + + return allocObject(ptr, v), nil +} + func (v *Repository) Lookup(id *Oid) (*Object, error) { return v.lookupType(id, ObjectAny) } +// LookupPrefix looks up an object by its OID given a prefix of its identifier. +func (v *Repository) LookupPrefix(id *Oid, prefix uint) (*Object, error) { + return v.lookupPrefixType(id, prefix, ObjectAny) +} + func (v *Repository) LookupTree(id *Oid) (*Tree, error) { obj, err := v.lookupType(id, ObjectTree) if err != nil { @@ -199,6 +219,17 @@ func (v *Repository) LookupTree(id *Oid) (*Tree, error) { return obj.AsTree() } +// LookupPrefixTree looks up a tree by its OID given a prefix of its identifier. +func (v *Repository) LookupPrefixTree(id *Oid, prefix uint) (*Tree, error) { + obj, err := v.lookupPrefixType(id, prefix, ObjectTree) + if err != nil { + return nil, err + } + defer obj.Free() + + return obj.AsTree() +} + func (v *Repository) LookupCommit(id *Oid) (*Commit, error) { obj, err := v.lookupType(id, ObjectCommit) if err != nil { @@ -209,6 +240,17 @@ func (v *Repository) LookupCommit(id *Oid) (*Commit, error) { return obj.AsCommit() } +// LookupPrefixCommit looks up a commit by its OID given a prefix of its identifier. +func (v *Repository) LookupPrefixCommit(id *Oid, prefix uint) (*Commit, error) { + obj, err := v.lookupPrefixType(id, prefix, ObjectCommit) + if err != nil { + return nil, err + } + defer obj.Free() + + return obj.AsCommit() +} + func (v *Repository) LookupBlob(id *Oid) (*Blob, error) { obj, err := v.lookupType(id, ObjectBlob) if err != nil { @@ -219,6 +261,17 @@ func (v *Repository) LookupBlob(id *Oid) (*Blob, error) { return obj.AsBlob() } +// LookupPrefixBlob looks up a blob by its OID given a prefix of its identifier. +func (v *Repository) LookupPrefixBlob(id *Oid, prefix uint) (*Blob, error) { + obj, err := v.lookupPrefixType(id, prefix, ObjectBlob) + if err != nil { + return nil, err + } + defer obj.Free() + + return obj.AsBlob() +} + func (v *Repository) LookupTag(id *Oid) (*Tag, error) { obj, err := v.lookupType(id, ObjectTag) if err != nil { @@ -229,6 +282,17 @@ func (v *Repository) LookupTag(id *Oid) (*Tag, error) { 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 From 7b9a768b08984dd81922560c995e567944ffddbb Mon Sep 17 00:00:00 2001 From: lhchavez Date: Mon, 26 Oct 2020 17:56:51 -0700 Subject: [PATCH 068/103] CI refresh (#666) (#667) This change: * Makes the Travis tests only run tip, since the rest of the Go versions are better served by GitHub Actions. * Use Go 1.15 in the CI. This has been released for a while. (cherry picked from commit f83530b18dc46867ed06fc261b309b8b545a3b6f) --- .github/workflows/ci.yml | 8 ++++---- .travis.yml | 11 ----------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42636bc15..caa52a453 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,7 +46,7 @@ jobs: strategy: fail-fast: false matrix: - go: [ '1.11', '1.12', '1.13', '1.14' ] + go: [ '1.11', '1.12', '1.13', '1.14', '1.15' ] name: Go ${{ matrix.go }} runs-on: ubuntu-20.04 @@ -77,7 +77,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v1 with: - go-version: '1.14' + go-version: '1.15' id: go - name: Check out code into the Go module directory uses: actions/checkout@v1 @@ -99,7 +99,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v1 with: - go-version: '1.14' + go-version: '1.15' id: go - name: Check out code into the Go module directory uses: actions/checkout@v1 @@ -121,7 +121,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v1 with: - go-version: '1.14' + go-version: '1.15' id: go - name: Check out code into the Go module directory uses: actions/checkout@v1 diff --git a/.travis.yml b/.travis.yml index c89cb5ce6..984e1c682 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,6 @@ language: go go: - - "1.7" - - "1.8" - - "1.9" - - "1.10" - - "1.11" - - "1.12" - - "1.13" - tip install: @@ -17,10 +10,6 @@ install: script: - make test-static -matrix: - allow_failures: - - go: tip - git: submodules: true From 800edc61bf1616f10c5182ddea7c752dee07a6d4 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 2 Nov 2020 18:44:37 -0800 Subject: [PATCH 069/103] feat: Implement an option to control hash verification (#671) (#673) Add a binding to enable/disable hash verification using the `GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION` option. Change type: #minor (cherry picked from commit c3664193f3c05bd6ae48f153c6c41cd7d7a3d98b) Co-authored-by: Suhaib Mujahid --- settings.go | 8 ++++++++ settings_test.go | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/settings.go b/settings.go index 247290801..f278e7be2 100644 --- a/settings.go +++ b/settings.go @@ -93,6 +93,14 @@ func EnableCaching(enabled bool) error { } } +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 CachedMemory() (current int, allowed int, err error) { return getSizetSizet(C.GIT_OPT_GET_CACHED_MEMORY) } diff --git a/settings_test.go b/settings_test.go index 150ee7c5c..47eb7116e 100644 --- a/settings_test.go +++ b/settings_test.go @@ -57,6 +57,14 @@ func TestEnableCaching(t *testing.T) { checkFatal(t, err) } +func TestEnableStrictHashVerification(t *testing.T) { + err := EnableStrictHashVerification(false) + checkFatal(t, err) + + err = EnableStrictHashVerification(true) + checkFatal(t, err) +} + func TestCachedMemory(t *testing.T) { current, allowed, err := CachedMemory() checkFatal(t, err) @@ -80,4 +88,4 @@ func TestSetCacheMaxSize(t *testing.T) { // revert to default 256MB err = SetCacheMaxSize(256 * 1024 * 1024) checkFatal(t, err) -} \ No newline at end of file +} From 81c9eb4670c729d445051e437d693be16779c597 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 13 Nov 2020 06:31:46 -0800 Subject: [PATCH 070/103] Travis-ci: added support for ppc64le (#682) (#684) Added power support for the travis.yml file with ppc64le. This is part of the Ubuntu distribution for ppc64le. This helps us simplify testing later when distributions are re-building and re-releasing. (cherry picked from commit 2d639d8e49470be188108eeae8e8f722d68aa910) Co-authored-by: Devendra --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 984e1c682..1717c8ef0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,9 @@ language: go +arch: + - AMD64 + - ppc64le + go: - tip From 3159d21503e773520ebb64dd4b43e314a06cac2f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 13 Nov 2020 19:01:23 -0800 Subject: [PATCH 071/103] Add ReferenceNormalizeName (#681) (#688) (cherry picked from commit 2bd574b6bd75a044f526cf882161db08ac3cb633) Co-authored-by: Segev Finer --- reference.go | 39 +++++++++++++++++++++++++++++++++++++++ reference_test.go | 23 +++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/reference.go b/reference.go index 12ecb74b4..3288c2757 100644 --- a/reference.go +++ b/reference.go @@ -488,3 +488,42 @@ func ReferenceIsValidName(name string) bool { } return false } + +const ( + // This should match GIT_REFNAME_MAX in src/refs.h + _refnameMaxLength = C.size_t(1024) +) + +type ReferenceFormat uint + +const ( + ReferenceFormatNormal ReferenceFormat = C.GIT_REF_FORMAT_NORMAL + ReferenceFormatAllowOnelevel ReferenceFormat = C.GIT_REF_FORMAT_ALLOW_ONELEVEL + ReferenceFormatRefspecPattern ReferenceFormat = C.GIT_REF_FORMAT_REFSPEC_PATTERN + ReferenceFormatRefspecShorthand ReferenceFormat = C.GIT_REF_FORMAT_REFSPEC_SHORTHAND +) + +// 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 +} diff --git a/reference_test.go b/reference_test.go index b6721e159..e42db41da 100644 --- a/reference_test.go +++ b/reference_test.go @@ -224,6 +224,29 @@ func TestReferenceIsValidName(t *testing.T) { } } +func TestReferenceNormalizeName(t *testing.T) { + t.Parallel() + + ref, err := ReferenceNormalizeName("refs/heads//master", ReferenceFormatNormal) + checkFatal(t, err) + + if ref != "refs/heads/master" { + t.Errorf("ReferenceNormalizeName(%q) = %q; want %q", "refs/heads//master", ref, "refs/heads/master") + } + + ref, err = ReferenceNormalizeName("master", ReferenceFormatAllowOnelevel|ReferenceFormatRefspecShorthand) + checkFatal(t, err) + + if ref != "master" { + t.Errorf("ReferenceNormalizeName(%q) = %q; want %q", "master", ref, "master") + } + + ref, err = ReferenceNormalizeName("foo^", ReferenceFormatNormal) + if !IsErrorCode(err, ErrInvalidSpec) { + t.Errorf("foo^ should be invalid") + } +} + func compareStringList(t *testing.T, expected, actual []string) { for i, v := range expected { if actual[i] != v { From 5f0640baed87f722ee0fe733bc29c94a9b28afb7 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 5 Dec 2020 13:12:04 -0800 Subject: [PATCH 072/103] Mark some symbols to be deprecated #minor (#698) (#702) This change introduces the file deprecated.go, which contains any constants, functions, and types that are slated to be deprecated in the next major release. These symbols are deprecated because they refer to old spellings in pre-1.0 libgit2. This also makes the build be done with the `-DDEPRECATE_HARD` flag to avoid regressions. This, together with [gorelease](https://godoc.org/golang.org/x/exp/cmd/gorelease)[1] should make releases safer going forward. 1: More information about how that works at https://go.googlesource.com/exp/+/refs/heads/master/apidiff/README.md (cherry picked from commit 137c05e802d5e11a5ab54809bc8be8f61ccece21) --- blob.go | 27 ------ branch.go | 2 +- branch_test.go | 4 +- checkout.go | 49 ++++++----- cherrypick.go | 4 +- cherrypick_test.go | 2 +- clone.go | 12 +-- clone_test.go | 4 +- deprecated.go | 138 +++++++++++++++++++++++++++++ errorclass_string.go | 63 +++++++++++++ errorcode_string.go | 67 ++++++++++++++ features.go | 4 +- git.go | 190 +++++++++++++++++++++------------------- git_test.go | 10 +-- index.go | 26 +++--- indexer_test.go | 2 +- mempack_test.go | 2 +- merge.go | 8 +- note_test.go | 2 +- object.go | 4 +- object_test.go | 12 +-- odb_test.go | 2 +- rebase.go | 2 +- rebase_test.go | 22 ++--- reference.go | 12 ++- reference_test.go | 6 +- remote.go | 9 +- remote_test.go | 2 +- reset.go | 2 +- reset_test.go | 2 +- revert.go | 4 +- script/build-libgit2.sh | 1 + stash.go | 20 ++--- stash_test.go | 4 +- submodule.go | 13 +-- tree.go | 4 +- walk.go | 2 +- wrapper.c | 4 +- 38 files changed, 496 insertions(+), 247 deletions(-) create mode 100644 deprecated.go create mode 100644 errorclass_string.go create mode 100644 errorcode_string.go diff --git a/blob.go b/blob.go index e8296bba5..652b729aa 100644 --- a/blob.go +++ b/blob.go @@ -9,7 +9,6 @@ void _go_git_writestream_free(git_writestream *stream); */ import "C" import ( - "io" "reflect" "runtime" "unsafe" @@ -76,32 +75,6 @@ func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) { return newOidFromC(&id), nil } -type BlobChunkCallback func(maxLen int) ([]byte, error) - -type BlobCallbackData struct { - Callback BlobChunkCallback - Error error -} - -//export blobChunkCb -func blobChunkCb(buffer *C.char, maxLen C.size_t, handle unsafe.Pointer) int { - payload := pointerHandles.Get(handle) - data, ok := payload.(*BlobCallbackData) - if !ok { - panic("could not retrieve blob callback data") - } - - goBuf, err := data.Callback(int(maxLen)) - if err == io.EOF { - return 0 - } else if err != nil { - data.Error = err - return -1 - } - C.memcpy(unsafe.Pointer(buffer), unsafe.Pointer(&goBuf[0]), C.size_t(len(goBuf))) - return len(goBuf) -} - func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, error) { var chintPath *C.char = nil var stream *C.git_writestream diff --git a/branch.go b/branch.go index d6e7a5323..3985ad21e 100644 --- a/branch.go +++ b/branch.go @@ -73,7 +73,7 @@ func (i *BranchIterator) ForEach(f BranchIteratorFunc) error { } } - if err != nil && IsErrorCode(err, ErrIterOver) { + if err != nil && IsErrorCode(err, ErrorCodeIterOver) { return nil } diff --git a/branch_test.go b/branch_test.go index 01a2e28e6..56795a4f5 100644 --- a/branch_test.go +++ b/branch_test.go @@ -22,7 +22,7 @@ func TestBranchIterator(t *testing.T) { t.Fatalf("expected BranchLocal, not %v", t) } b, bt, err = i.Next() - if !IsErrorCode(err, ErrIterOver) { + if !IsErrorCode(err, ErrorCodeIterOver) { t.Fatal("expected iterover") } } @@ -49,7 +49,7 @@ func TestBranchIteratorEach(t *testing.T) { } err = i.ForEach(f) - if err != nil && !IsErrorCode(err, ErrIterOver) { + if err != nil && !IsErrorCode(err, ErrorCodeIterOver) { t.Fatal(err) } diff --git a/checkout.go b/checkout.go index 76d6d6975..71ccac495 100644 --- a/checkout.go +++ b/checkout.go @@ -52,7 +52,7 @@ const ( type CheckoutNotifyCallback func(why CheckoutNotifyType, path string, baseline, target, workdir DiffFile) ErrorCode type CheckoutProgressCallback func(path string, completed, total uint) ErrorCode -type CheckoutOpts struct { +type CheckoutOptions struct { Strategy CheckoutStrategy // Default will be a dry run DisableFilters bool // Don't apply filters like CRLF conversion DirMode os.FileMode // Default is 0755 @@ -66,19 +66,20 @@ type CheckoutOpts struct { Baseline *Tree } -func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOpts { - opts := CheckoutOpts{} - opts.Strategy = CheckoutStrategy(c.checkout_strategy) - opts.DisableFilters = c.disable_filters != 0 - opts.DirMode = os.FileMode(c.dir_mode) - opts.FileMode = os.FileMode(c.file_mode) - opts.FileOpenFlags = int(c.file_open_flags) - opts.NotifyFlags = CheckoutNotifyType(c.notify_flags) +func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOptions { + opts := CheckoutOptions{ + Strategy: CheckoutStrategy(c.checkout_strategy), + DisableFilters: c.disable_filters != 0, + DirMode: os.FileMode(c.dir_mode), + FileMode: os.FileMode(c.file_mode), + FileOpenFlags: int(c.file_open_flags), + NotifyFlags: CheckoutNotifyType(c.notify_flags), + } if c.notify_payload != nil { - opts.NotifyCallback = pointerHandles.Get(c.notify_payload).(*CheckoutOpts).NotifyCallback + opts.NotifyCallback = pointerHandles.Get(c.notify_payload).(*CheckoutOptions).NotifyCallback } if c.progress_payload != nil { - opts.ProgressCallback = pointerHandles.Get(c.progress_payload).(*CheckoutOpts).ProgressCallback + opts.ProgressCallback = pointerHandles.Get(c.progress_payload).(*CheckoutOptions).ProgressCallback } if c.target_directory != nil { opts.TargetDirectory = C.GoString(c.target_directory) @@ -86,12 +87,12 @@ func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOpts { return opts } -func (opts *CheckoutOpts) toC() *C.git_checkout_options { +func (opts *CheckoutOptions) toC() *C.git_checkout_options { if opts == nil { return nil } c := C.git_checkout_options{} - populateCheckoutOpts(&c, opts) + populateCheckoutOptions(&c, opts) return &c } @@ -111,7 +112,7 @@ func checkoutNotifyCallback(why C.git_checkout_notify_t, cpath *C.char, cbaselin if cworkdir != nil { workdir = diffFileFromC((*C.git_diff_file)(cworkdir)) } - opts := pointerHandles.Get(data).(*CheckoutOpts) + opts := pointerHandles.Get(data).(*CheckoutOptions) if opts.NotifyCallback == nil { return 0 } @@ -120,17 +121,17 @@ func checkoutNotifyCallback(why C.git_checkout_notify_t, cpath *C.char, cbaselin //export checkoutProgressCallback func checkoutProgressCallback(path *C.char, completed_steps, total_steps C.size_t, data unsafe.Pointer) int { - opts := pointerHandles.Get(data).(*CheckoutOpts) + opts := pointerHandles.Get(data).(*CheckoutOptions) if opts.ProgressCallback == nil { return 0 } return int(opts.ProgressCallback(C.GoString(path), uint(completed_steps), uint(total_steps))) } -// Convert the CheckoutOpts struct to the corresponding +// Convert the CheckoutOptions struct to the corresponding // C-struct. Returns a pointer to ptr, or nil if opts is nil, in order // to help with what to pass. -func populateCheckoutOpts(ptr *C.git_checkout_options, opts *CheckoutOpts) *C.git_checkout_options { +func populateCheckoutOptions(ptr *C.git_checkout_options, opts *CheckoutOptions) *C.git_checkout_options { if opts == nil { return nil } @@ -166,7 +167,7 @@ func populateCheckoutOpts(ptr *C.git_checkout_options, opts *CheckoutOpts) *C.gi return ptr } -func freeCheckoutOpts(ptr *C.git_checkout_options) { +func freeCheckoutOptions(ptr *C.git_checkout_options) { if ptr == nil { return } @@ -181,12 +182,12 @@ func freeCheckoutOpts(ptr *C.git_checkout_options) { // Updates files in the index and the working tree to match the content of // the commit pointed at by HEAD. opts may be nil. -func (v *Repository) CheckoutHead(opts *CheckoutOpts) error { +func (v *Repository) CheckoutHead(opts *CheckoutOptions) error { runtime.LockOSThread() defer runtime.UnlockOSThread() cOpts := opts.toC() - defer freeCheckoutOpts(cOpts) + defer freeCheckoutOptions(cOpts) ret := C.git_checkout_head(v.ptr, cOpts) runtime.KeepAlive(v) @@ -200,7 +201,7 @@ func (v *Repository) CheckoutHead(opts *CheckoutOpts) error { // Updates files in the working tree to match the content of the given // index. If index is nil, the repository's index will be used. opts // may be nil. -func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error { +func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOptions) error { var iptr *C.git_index = nil if index != nil { iptr = index.ptr @@ -210,7 +211,7 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error { defer runtime.UnlockOSThread() cOpts := opts.toC() - defer freeCheckoutOpts(cOpts) + defer freeCheckoutOptions(cOpts) ret := C.git_checkout_index(v.ptr, iptr, cOpts) runtime.KeepAlive(v) @@ -221,12 +222,12 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error { return nil } -func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOpts) error { +func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOptions) error { runtime.LockOSThread() defer runtime.UnlockOSThread() cOpts := opts.toC() - defer freeCheckoutOpts(cOpts) + defer freeCheckoutOptions(cOpts) ret := C.git_checkout_tree(v.ptr, tree.ptr, cOpts) runtime.KeepAlive(v) diff --git a/cherrypick.go b/cherrypick.go index e86e94077..b80852b5c 100644 --- a/cherrypick.go +++ b/cherrypick.go @@ -12,7 +12,7 @@ type CherrypickOptions struct { Version uint Mainline uint MergeOpts MergeOptions - CheckoutOpts CheckoutOpts + CheckoutOpts CheckoutOptions } func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions { @@ -41,7 +41,7 @@ func freeCherrypickOpts(ptr *C.git_cherrypick_options) { if ptr == nil { return } - freeCheckoutOpts(&ptr.checkout_opts) + freeCheckoutOptions(&ptr.checkout_opts) } func DefaultCherrypickOptions() (CherrypickOptions, error) { diff --git a/cherrypick_test.go b/cherrypick_test.go index 19a97360e..06f65853e 100644 --- a/cherrypick_test.go +++ b/cherrypick_test.go @@ -11,7 +11,7 @@ func checkout(t *testing.T, repo *Repository, commit *Commit) { t.Fatal(err) } - err = repo.CheckoutTree(tree, &CheckoutOpts{Strategy: CheckoutSafe}) + err = repo.CheckoutTree(tree, &CheckoutOptions{Strategy: CheckoutSafe}) if err != nil { t.Fatal(err) } diff --git a/clone.go b/clone.go index 1ff51245b..b329a25d1 100644 --- a/clone.go +++ b/clone.go @@ -58,19 +58,19 @@ func remoteCreateCallback(cremote unsafe.Pointer, crepo unsafe.Pointer, cname, c runtime.SetFinalizer(repo, nil) if opts, ok := pointerHandles.Get(payload).(CloneOptions); ok { - remote, err := opts.RemoteCreateCallback(repo, name, url) + remote, errorCode := opts.RemoteCreateCallback(repo, name, url) // clear finalizer as the calling C function will // free the remote itself runtime.SetFinalizer(remote, nil) - if err == ErrOk && remote != nil { + if errorCode == ErrorCodeOK && remote != nil { cptr := (**C.git_remote)(cremote) *cptr = remote.ptr - } else if err == ErrOk && remote == nil { + } else if errorCode == ErrorCodeOK && remote == nil { panic("no remote created by callback") } - return C.int(err) + return C.int(errorCode) } else { panic("invalid remote create callback") } @@ -82,7 +82,7 @@ func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions) { if opts == nil { return } - populateCheckoutOpts(&ptr.checkout_opts, opts.CheckoutOpts) + populateCheckoutOptions(&ptr.checkout_opts, opts.CheckoutOpts) populateFetchOptions(&ptr.fetch_opts, opts.FetchOptions) ptr.bare = cbool(opts.Bare) @@ -98,7 +98,7 @@ func freeCloneOptions(ptr *C.git_clone_options) { return } - freeCheckoutOpts(&ptr.checkout_opts) + freeCheckoutOptions(&ptr.checkout_opts) if ptr.remote_cb_payload != nil { pointerHandles.Untrack(ptr.remote_cb_payload) diff --git a/clone_test.go b/clone_test.go index 24c3a09d5..ded9847db 100644 --- a/clone_test.go +++ b/clone_test.go @@ -54,10 +54,10 @@ func TestCloneWithCallback(t *testing.T) { remote, err := r.Remotes.Create(REMOTENAME, url) if err != nil { - return nil, ErrGeneric + return nil, ErrorCodeGeneric } - return remote, ErrOk + return remote, ErrorCodeOK }, } diff --git a/deprecated.go b/deprecated.go new file mode 100644 index 000000000..1e24c10ca --- /dev/null +++ b/deprecated.go @@ -0,0 +1,138 @@ +package git + +/* +#include +*/ +import "C" +import ( + "unsafe" +) + +// The constants, functions, and types in this files are slated for deprecation +// in the next major version. + +// blob.go + +// BlobChunkCallback is not used. +type BlobChunkCallback func(maxLen int) ([]byte, error) + +// BlobCallbackData is not used. +type BlobCallbackData struct { + Callback BlobChunkCallback + Error error +} + +// checkout.go + +// CheckoutOpts is a deprecated alias of CheckoutOptions. +type CheckoutOpts = CheckoutOptions + +// features.go + +const ( + // FeatureHttps is a deprecated alias of FeatureHTTPS. + FeatureHttps = FeatureHTTPS + + // FeatureSsh is a deprecated alias of FeatureSSH. + FeatureSsh = FeatureSSH +) + +// git.go + +const ( + ErrClassNone = ErrorClassNone + ErrClassNoMemory = ErrorClassNoMemory + ErrClassOs = ErrorClassOS + ErrClassInvalid = ErrorClassInvalid + ErrClassReference = ErrorClassReference + ErrClassZlib = ErrorClassZlib + ErrClassRepository = ErrorClassRepository + ErrClassConfig = ErrorClassConfig + ErrClassRegex = ErrorClassRegex + ErrClassOdb = ErrorClassOdb + ErrClassIndex = ErrorClassIndex + ErrClassObject = ErrorClassObject + ErrClassNet = ErrorClassNet + ErrClassTag = ErrorClassTag + ErrClassTree = ErrorClassTree + ErrClassIndexer = ErrorClassIndexer + ErrClassSSL = ErrorClassSSL + ErrClassSubmodule = ErrorClassSubmodule + ErrClassThread = ErrorClassThread + ErrClassStash = ErrorClassStash + ErrClassCheckout = ErrorClassCheckout + ErrClassFetchHead = ErrorClassFetchHead + ErrClassMerge = ErrorClassMerge + ErrClassSsh = ErrorClassSSH + ErrClassFilter = ErrorClassFilter + ErrClassRevert = ErrorClassRevert + ErrClassCallback = ErrorClassCallback + ErrClassRebase = ErrorClassRebase + ErrClassPatch = ErrorClassPatch +) + +const ( + ErrOk = ErrorCodeOK + ErrGeneric = ErrorCodeGeneric + ErrNotFound = ErrorCodeNotFound + ErrExists = ErrorCodeExists + ErrAmbiguous = ErrorCodeAmbiguous + ErrAmbigious = ErrorCodeAmbiguous + ErrBuffs = ErrorCodeBuffs + ErrUser = ErrorCodeUser + ErrBareRepo = ErrorCodeBareRepo + ErrUnbornBranch = ErrorCodeUnbornBranch + ErrUnmerged = ErrorCodeUnmerged + ErrNonFastForward = ErrorCodeNonFastForward + ErrInvalidSpec = ErrorCodeInvalidSpec + ErrConflict = ErrorCodeConflict + ErrLocked = ErrorCodeLocked + ErrModified = ErrorCodeModified + ErrAuth = ErrorCodeAuth + ErrCertificate = ErrorCodeCertificate + ErrApplied = ErrorCodeApplied + ErrPeel = ErrorCodePeel + ErrEOF = ErrorCodeEOF + ErrUncommitted = ErrorCodeUncommitted + ErrDirectory = ErrorCodeDirectory + ErrMergeConflict = ErrorCodeMergeConflict + ErrPassthrough = ErrorCodePassthrough + ErrIterOver = ErrorCodeIterOver +) + +// index.go + +// IndexAddOpts is a deprecated alias of IndexAddOption. +type IndexAddOpts = IndexAddOption + +// IndexStageOpts is a deprecated alias of IndexStageState. +type IndexStageOpts = IndexStageState + +// submodule.go + +// SubmoduleCbk is a deprecated alias of SubmoduleCallback. +type SubmoduleCbk = SubmoduleCallback + +// SubmoduleVisitor is not used. +func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { + sub := &Submodule{(*C.git_submodule)(csub), nil} + + callback, ok := pointerHandles.Get(handle).(SubmoduleCallback) + if !ok { + panic("invalid submodule visitor callback") + } + return (C.int)(callback(sub, C.GoString(name))) +} + +// tree.go + +// CallbackGitTreeWalk is not used. +func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int { + root := C.GoString(_root) + + if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok { + return C.int(callback(root, newTreeEntry(entry))) + } else { + panic("invalid treewalk callback") + } +} diff --git a/errorclass_string.go b/errorclass_string.go new file mode 100644 index 000000000..122344247 --- /dev/null +++ b/errorclass_string.go @@ -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) + ")" + } +} diff --git a/errorcode_string.go b/errorcode_string.go new file mode 100644 index 000000000..b15214ee1 --- /dev/null +++ b/errorcode_string.go @@ -0,0 +1,67 @@ +// 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] +} + +const ( + _ErrorCode_name_0 = "MismatchRetryIterOverPassthrough" + _ErrorCode_name_1 = "MergeConflictDirectoryUncommittedInvalidEOFPeelAppliedCertificateAuthModifiedLockedConflictInvalidSpecNonFastForwardUnmergedUnbornBranchBareRepoUserBuffsAmbiguousExistsNotFound" + _ErrorCode_name_2 = "GenericOK" +) + +var ( + _ErrorCode_index_0 = [...]uint8{0, 8, 13, 21, 32} + _ErrorCode_index_1 = [...]uint8{0, 13, 22, 33, 40, 43, 47, 54, 65, 69, 77, 83, 91, 102, 116, 124, 136, 144, 148, 153, 162, 168, 176} + _ErrorCode_index_2 = [...]uint8{0, 7, 9} +) + +func (i ErrorCode) String() string { + switch { + case -33 <= i && i <= -30: + i -= -33 + 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) + ")" + } +} diff --git a/features.go b/features.go index f6474a061..a9f81a12b 100644 --- a/features.go +++ b/features.go @@ -12,10 +12,10 @@ const ( FeatureThreads Feature = C.GIT_FEATURE_THREADS // libgit2 was built with HTTPS support built-in - FeatureHttps Feature = C.GIT_FEATURE_HTTPS + FeatureHTTPS Feature = C.GIT_FEATURE_HTTPS // libgit2 was build with SSH support built-in - FeatureSsh Feature = C.GIT_FEATURE_SSH + FeatureSSH Feature = C.GIT_FEATURE_SSH // libgit2 was built with nanosecond support for files FeatureNSec Feature = C.GIT_FEATURE_NSEC diff --git a/git.go b/git.go index d83350595..34086a9a5 100644 --- a/git.go +++ b/git.go @@ -17,98 +17,102 @@ import ( type ErrorClass int const ( - ErrClassNone ErrorClass = C.GITERR_NONE - ErrClassNoMemory ErrorClass = C.GITERR_NOMEMORY - ErrClassOs ErrorClass = C.GITERR_OS - ErrClassInvalid ErrorClass = C.GITERR_INVALID - ErrClassReference ErrorClass = C.GITERR_REFERENCE - ErrClassZlib ErrorClass = C.GITERR_ZLIB - ErrClassRepository ErrorClass = C.GITERR_REPOSITORY - ErrClassConfig ErrorClass = C.GITERR_CONFIG - ErrClassRegex ErrorClass = C.GITERR_REGEX - ErrClassOdb ErrorClass = C.GITERR_ODB - ErrClassIndex ErrorClass = C.GITERR_INDEX - ErrClassObject ErrorClass = C.GITERR_OBJECT - ErrClassNet ErrorClass = C.GITERR_NET - ErrClassTag ErrorClass = C.GITERR_TAG - ErrClassTree ErrorClass = C.GITERR_TREE - ErrClassIndexer ErrorClass = C.GITERR_INDEXER - ErrClassSSL ErrorClass = C.GITERR_SSL - ErrClassSubmodule ErrorClass = C.GITERR_SUBMODULE - ErrClassThread ErrorClass = C.GITERR_THREAD - ErrClassStash ErrorClass = C.GITERR_STASH - ErrClassCheckout ErrorClass = C.GITERR_CHECKOUT - ErrClassFetchHead ErrorClass = C.GITERR_FETCHHEAD - ErrClassMerge ErrorClass = C.GITERR_MERGE - ErrClassSsh ErrorClass = C.GITERR_SSH - ErrClassFilter ErrorClass = C.GITERR_FILTER - ErrClassRevert ErrorClass = C.GITERR_REVERT - ErrClassCallback ErrorClass = C.GITERR_CALLBACK - ErrClassRebase ErrorClass = C.GITERR_REBASE + ErrorClassNone ErrorClass = C.GITERR_NONE + ErrorClassNoMemory ErrorClass = C.GITERR_NOMEMORY + ErrorClassOS ErrorClass = C.GITERR_OS + ErrorClassInvalid ErrorClass = C.GITERR_INVALID + ErrorClassReference ErrorClass = C.GITERR_REFERENCE + ErrorClassZlib ErrorClass = C.GITERR_ZLIB + ErrorClassRepository ErrorClass = C.GITERR_REPOSITORY + ErrorClassConfig ErrorClass = C.GITERR_CONFIG + ErrorClassRegex ErrorClass = C.GITERR_REGEX + ErrorClassOdb ErrorClass = C.GITERR_ODB + ErrorClassIndex ErrorClass = C.GITERR_INDEX + ErrorClassObject ErrorClass = C.GITERR_OBJECT + ErrorClassNet ErrorClass = C.GITERR_NET + ErrorClassTag ErrorClass = C.GITERR_TAG + ErrorClassTree ErrorClass = C.GITERR_TREE + ErrorClassIndexer ErrorClass = C.GITERR_INDEXER + ErrorClassSSL ErrorClass = C.GITERR_SSL + ErrorClassSubmodule ErrorClass = C.GITERR_SUBMODULE + ErrorClassThread ErrorClass = C.GITERR_THREAD + ErrorClassStash ErrorClass = C.GITERR_STASH + ErrorClassCheckout ErrorClass = C.GITERR_CHECKOUT + ErrorClassFetchHead ErrorClass = C.GITERR_FETCHHEAD + ErrorClassMerge ErrorClass = C.GITERR_MERGE + ErrorClassSSH ErrorClass = C.GITERR_SSH + ErrorClassFilter ErrorClass = C.GITERR_FILTER + ErrorClassRevert ErrorClass = C.GITERR_REVERT + ErrorClassCallback ErrorClass = C.GITERR_CALLBACK + ErrorClassRebase ErrorClass = C.GITERR_REBASE + ErrorClassPatch ErrorClass = C.GITERR_PATCH ) type ErrorCode int const ( - - // No error - ErrOk ErrorCode = C.GIT_OK - - // Generic error - ErrGeneric ErrorCode = C.GIT_ERROR - // Requested object could not be found - ErrNotFound ErrorCode = C.GIT_ENOTFOUND - // Object exists preventing operation - ErrExists ErrorCode = C.GIT_EEXISTS - // More than one object matches - ErrAmbiguous ErrorCode = C.GIT_EAMBIGUOUS - // (backwards compatibility misspelling) - ErrAmbigious ErrorCode = C.GIT_EAMBIGUOUS - // Output buffer too short to hold data - ErrBuffs ErrorCode = C.GIT_EBUFS - - // GIT_EUSER is a special error that is never generated by libgit2 + // 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. - ErrUser ErrorCode = C.GIT_EUSER - - // Operation not allowed on bare repository - ErrBareRepo ErrorCode = C.GIT_EBAREREPO - // HEAD refers to branch with no commits - ErrUnbornBranch ErrorCode = C.GIT_EUNBORNBRANCH - // Merge in progress prevented operation - ErrUnmerged ErrorCode = C.GIT_EUNMERGED - // Reference was not fast-forwardable - ErrNonFastForward ErrorCode = C.GIT_ENONFASTFORWARD - // Name/ref spec was not in a valid format - ErrInvalidSpec ErrorCode = C.GIT_EINVALIDSPEC - // Checkout conflicts prevented operation - ErrConflict ErrorCode = C.GIT_ECONFLICT - // Lock file prevented operation - ErrLocked ErrorCode = C.GIT_ELOCKED - // Reference value does not match expected - ErrModified ErrorCode = C.GIT_EMODIFIED - // Authentication failed - ErrAuth ErrorCode = C.GIT_EAUTH - // Server certificate is invalid - ErrCertificate ErrorCode = C.GIT_ECERTIFICATE - // Patch/merge has already been applied - ErrApplied ErrorCode = C.GIT_EAPPLIED - // The requested peel operation is not possible - ErrPeel ErrorCode = C.GIT_EPEEL - // Unexpected EOF - ErrEOF ErrorCode = C.GIT_EEOF - // Uncommitted changes in index prevented operation - ErrUncommitted ErrorCode = C.GIT_EUNCOMMITTED - // The operation is not valid for a directory - ErrDirectory ErrorCode = C.GIT_EDIRECTORY - // A merge conflict exists and cannot continue - ErrMergeConflict ErrorCode = C.GIT_EMERGECONFLICT - - // Internal only - ErrPassthrough ErrorCode = C.GIT_PASSTHROUGH - // Signals end of iteration with iterator - ErrIterOver ErrorCode = C.GIT_ITEROVER + 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 ) var ( @@ -198,7 +202,7 @@ func NewOid(s string) (*Oid, error) { } if len(slice) != 20 { - return nil, &GitError{"Invalid Oid", ErrClassNone, ErrGeneric} + return nil, &GitError{"Invalid Oid", ErrorClassNone, ErrGeneric} } copy(o[:], slice[:20]) @@ -266,7 +270,6 @@ func (e GitError) Error() string { } func IsErrorClass(err error, c ErrorClass) bool { - if err == nil { return false } @@ -286,20 +289,23 @@ func IsErrorCode(err error, c ErrorCode) bool { return false } -func MakeGitError(errorCode C.int) error { - +func MakeGitError(c C.int) error { var errMessage string var errClass ErrorClass - if errorCode != C.GIT_ITEROVER { + errorCode := ErrorCode(c) + if errorCode != ErrorCodeIterOver { err := C.giterr_last() if err != nil { errMessage = C.GoString(err.message) errClass = ErrorClass(err.klass) } else { - errClass = ErrClassInvalid + errClass = ErrorClassInvalid } } - return &GitError{errMessage, errClass, ErrorCode(errorCode)} + if errMessage == "" { + errMessage = errorCode.String() + } + return &GitError{errMessage, errClass, errorCode} } func MakeGitError2(err int) error { diff --git a/git_test.go b/git_test.go index 91ade73b4..1bd311af3 100644 --- a/git_test.go +++ b/git_test.go @@ -45,16 +45,16 @@ func createBareTestRepo(t *testing.T) *Repository { return repo } -// commitOpts contains any extra options for creating commits in the seed repo -type commitOpts struct { +// 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, commitOpts{}) + return seedTestRepoOpt(t, repo, commitOptions{}) } -func seedTestRepoOpt(t *testing.T, repo *Repository, opts commitOpts) (*Oid, *Oid) { +func seedTestRepoOpt(t *testing.T, repo *Repository, opts commitOptions) (*Oid, *Oid) { loc, err := time.LoadLocation("Europe/Berlin") checkFatal(t, err) sig := &Signature{ @@ -155,7 +155,7 @@ func TestOidZero(t *testing.T) { func TestEmptyOid(t *testing.T) { t.Parallel() _, err := NewOid("") - if err == nil || !IsErrorCode(err, ErrGeneric) { + if err == nil || !IsErrorCode(err, ErrorCodeGeneric) { t.Fatal("Should have returned invalid error") } } diff --git a/index.go b/index.go index 626e64921..7173ae015 100644 --- a/index.go +++ b/index.go @@ -17,31 +17,33 @@ import ( type IndexMatchedPathCallback func(string, string) int -type IndexAddOpts uint +// IndexAddOption is a set of flags for APIs that add files matching pathspec. +type IndexAddOption uint const ( - IndexAddDefault IndexAddOpts = C.GIT_INDEX_ADD_DEFAULT - IndexAddForce IndexAddOpts = C.GIT_INDEX_ADD_FORCE - IndexAddDisablePathspecMatch IndexAddOpts = C.GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH - IndexAddCheckPathspec IndexAddOpts = C.GIT_INDEX_ADD_CHECK_PATHSPEC + IndexAddDefault IndexAddOption = C.GIT_INDEX_ADD_DEFAULT + IndexAddForce IndexAddOption = C.GIT_INDEX_ADD_FORCE + IndexAddDisablePathspecMatch IndexAddOption = C.GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH + IndexAddCheckPathspec IndexAddOption = C.GIT_INDEX_ADD_CHECK_PATHSPEC ) -type IndexStageOpts int +// IndexStageState indicates the state of the git index. +type IndexStageState int const ( // IndexStageAny matches any index stage. // // Some index APIs take a stage to match; pass this value to match // any entry matching the path regardless of stage. - IndexStageAny IndexStageOpts = C.GIT_INDEX_STAGE_ANY + IndexStageAny IndexStageState = C.GIT_INDEX_STAGE_ANY // IndexStageNormal is a normal staged file in the index. - IndexStageNormal IndexStageOpts = C.GIT_INDEX_STAGE_NORMAL + IndexStageNormal IndexStageState = C.GIT_INDEX_STAGE_NORMAL // IndexStageAncestor is the ancestor side of a conflict. - IndexStageAncestor IndexStageOpts = C.GIT_INDEX_STAGE_ANCESTOR + IndexStageAncestor IndexStageState = C.GIT_INDEX_STAGE_ANCESTOR // IndexStageOurs is the "ours" side of a conflict. - IndexStageOurs IndexStageOpts = C.GIT_INDEX_STAGE_OURS + IndexStageOurs IndexStageState = C.GIT_INDEX_STAGE_OURS // IndexStageTheirs is the "theirs" side of a conflict. - IndexStageTheirs IndexStageOpts = C.GIT_INDEX_STAGE_THEIRS + IndexStageTheirs IndexStageState = C.GIT_INDEX_STAGE_THEIRS ) type Index struct { @@ -219,7 +221,7 @@ func (v *Index) AddFromBuffer(entry *IndexEntry, buffer []byte) error { return nil } -func (v *Index) AddAll(pathspecs []string, flags IndexAddOpts, callback IndexMatchedPathCallback) error { +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) diff --git a/indexer_test.go b/indexer_test.go index 1b65c9555..70b9f761b 100644 --- a/indexer_test.go +++ b/indexer_test.go @@ -35,7 +35,7 @@ func TestIndexerOutOfOrder(t *testing.T) { var finalStats TransferProgress idx, err := NewIndexer(tmpPath, nil, func(stats TransferProgress) ErrorCode { finalStats = stats - return ErrOk + return ErrorCodeOK }) checkFatal(t, err) defer idx.Free() diff --git a/mempack_test.go b/mempack_test.go index 3e31dcf9c..9bbfea248 100644 --- a/mempack_test.go +++ b/mempack_test.go @@ -53,7 +53,7 @@ func TestMempack(t *testing.T) { if err == nil { t.Errorf("object %s unexpectedly found", obj.Id().String()) obj.Free() - } else if !IsErrorCode(err, ErrNotFound) { + } else if !IsErrorCode(err, ErrorCodeNotFound) { t.Errorf("unexpected error %v", err) } } diff --git a/merge.go b/merge.go index da1c3d612..064ca59dc 100644 --- a/merge.go +++ b/merge.go @@ -195,20 +195,20 @@ const ( MergeFileFavorUnion MergeFileFavor = C.GIT_MERGE_FILE_FAVOR_UNION ) -func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOptions, checkoutOptions *CheckoutOpts) error { +func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOptions, checkoutOptions *CheckoutOptions) error { runtime.LockOSThread() defer runtime.UnlockOSThread() cMergeOpts := mergeOptions.toC() - cCheckoutOpts := checkoutOptions.toC() - defer freeCheckoutOpts(cCheckoutOpts) + cCheckoutOptions := checkoutOptions.toC() + defer freeCheckoutOptions(cCheckoutOptions) gmerge_head_array := make([]*C.git_annotated_commit, len(theirHeads)) for i := 0; i < len(theirHeads); i++ { gmerge_head_array[i] = theirHeads[i].ptr } ptr := unsafe.Pointer(&gmerge_head_array[0]) - err := C.git_merge(r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)), cMergeOpts, cCheckoutOpts) + err := C.git_merge(r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)), cMergeOpts, cCheckoutOptions) runtime.KeepAlive(theirHeads) if err < 0 { return MakeGitError(err) diff --git a/note_test.go b/note_test.go index 9f64eb8c3..f0c9c53d4 100644 --- a/note_test.go +++ b/note_test.go @@ -49,7 +49,7 @@ func TestNoteIterator(t *testing.T) { for { noteId, commitId, err := iter.Next() if err != nil { - if !IsErrorCode(err, ErrIterOver) { + if !IsErrorCode(err, ErrorCodeIterOver) { checkFatal(t, err) } break diff --git a/object.go b/object.go index 5505e356e..e7d38908e 100644 --- a/object.go +++ b/object.go @@ -201,13 +201,13 @@ func (o *Object) Free() { // Peel recursively peels an object until an object of the specified type is met. // -// If the query cannot be satisfied due to the object model, ErrInvalidSpec +// If the query cannot be satisfied due to the object model, ErrorCodeInvalidSpec // will be returned (e.g. trying to peel a blob to a tree). // // If you pass ObjectAny as the target type, then the object will be peeled // until the type changes. A tag will be peeled until the referenced object // is no longer a tag, and a commit will be peeled to a tree. Any other object -// type will return ErrInvalidSpec. +// type will return ErrorCodeInvalidSpec. // // If peeling a tag we discover an object which cannot be peeled to the target // type due to the object model, an error will be returned. diff --git a/object_test.go b/object_test.go index 4932dd22b..3b602b876 100644 --- a/object_test.go +++ b/object_test.go @@ -153,8 +153,8 @@ func TestObjectPeel(t *testing.T) { obj, err = commit.Peel(ObjectTag) - if !IsErrorCode(err, ErrInvalidSpec) { - t.Fatalf("Wrong error when peeling a commit to a tag, expected ErrInvalidSpec, have %v", err) + if !IsErrorCode(err, ErrorCodeInvalidSpec) { + t.Fatalf("Wrong error when peeling a commit to a tag, expected ErrorCodeInvalidSpec, have %v", err) } tree, err := repo.LookupTree(treeID) @@ -162,8 +162,8 @@ func TestObjectPeel(t *testing.T) { obj, err = tree.Peel(ObjectAny) - if !IsErrorCode(err, ErrInvalidSpec) { - t.Fatalf("Wrong error when peeling a tree, expected ErrInvalidSpec, have %v", err) + if !IsErrorCode(err, ErrorCodeInvalidSpec) { + t.Fatalf("Wrong error when peeling a tree, expected ErrorCodeInvalidSpec, have %v", err) } entry := tree.EntryByName("README") @@ -173,8 +173,8 @@ func TestObjectPeel(t *testing.T) { obj, err = blob.Peel(ObjectAny) - if !IsErrorCode(err, ErrInvalidSpec) { - t.Fatalf("Wrong error when peeling a blob, expected ErrInvalidSpec, have %v", err) + if !IsErrorCode(err, ErrorCodeInvalidSpec) { + t.Fatalf("Wrong error when peeling a blob, expected ErrorCodeInvalidSpec, have %v", err) } tagID := createTestTag(t, repo, commit) diff --git a/odb_test.go b/odb_test.go index d79afa1ac..cb3ab5548 100644 --- a/odb_test.go +++ b/odb_test.go @@ -169,7 +169,7 @@ func TestOdbWritepack(t *testing.T) { var finalStats TransferProgress writepack, err := odb.NewWritePack(func(stats TransferProgress) ErrorCode { finalStats = stats - return ErrOk + return ErrorCodeOK }) checkFatal(t, err) defer writepack.Free() diff --git a/rebase.go b/rebase.go index d29e18334..2c13fad11 100644 --- a/rebase.go +++ b/rebase.go @@ -76,7 +76,7 @@ type RebaseOptions struct { InMemory int RewriteNotesRef string MergeOptions MergeOptions - CheckoutOptions CheckoutOpts + CheckoutOptions CheckoutOptions } // DefaultRebaseOptions returns a RebaseOptions with default values. diff --git a/rebase_test.go b/rebase_test.go index c7deef6d9..bd035d2ab 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -33,12 +33,12 @@ func TestRebaseAbort(t *testing.T) { seedTestRepo(t, repo) // Setup a repo with 2 branches and a different tree - err := setupRepoForRebase(repo, masterCommit, branchName, commitOpts{}) + err := setupRepoForRebase(repo, masterCommit, branchName, commitOptions{}) checkFatal(t, err) // Create several commits in emile for _, commit := range emileCommits { - _, err = commitSomething(repo, commit, commit, commitOpts{}) + _, err = commitSomething(repo, commit, commit, commitOptions{}) checkFatal(t, err) } @@ -94,12 +94,12 @@ func TestRebaseNoConflicts(t *testing.T) { } // Setup a repo with 2 branches and a different tree - err = setupRepoForRebase(repo, masterCommit, branchName, commitOpts{}) + err = setupRepoForRebase(repo, masterCommit, branchName, commitOptions{}) checkFatal(t, err) // Create several commits in emile for _, commit := range emileCommits { - _, err = commitSomething(repo, commit, commit, commitOpts{}) + _, err = commitSomething(repo, commit, commit, commitOptions{}) checkFatal(t, err) } @@ -133,7 +133,7 @@ func TestRebaseNoConflicts(t *testing.T) { } // Utils -func setupRepoForRebase(repo *Repository, masterCommit, branchName string, opts commitOpts) error { +func setupRepoForRebase(repo *Repository, masterCommit, branchName string, commitOpts commitOptions) error { // Create a new branch from master err := createBranch(repo, branchName) if err != nil { @@ -141,7 +141,7 @@ func setupRepoForRebase(repo *Repository, masterCommit, branchName string, opts } // Create a commit in master - _, err = commitSomething(repo, masterCommit, masterCommit, opts) + _, err = commitSomething(repo, masterCommit, masterCommit, commitOpts) if err != nil { return err } @@ -160,7 +160,7 @@ func setupRepoForRebase(repo *Repository, masterCommit, branchName string, opts return nil } -func performRebaseOnto(repo *Repository, branch string, opts *RebaseOptions) (*Rebase, error) { +func performRebaseOnto(repo *Repository, branch string, rebaseOpts *RebaseOptions) (*Rebase, error) { master, err := repo.LookupBranch(branch, BranchLocal) if err != nil { return nil, err @@ -174,7 +174,7 @@ func performRebaseOnto(repo *Repository, branch string, opts *RebaseOptions) (*R defer onto.Free() // Init rebase - rebase, err := repo.InitRebase(nil, nil, onto, opts) + rebase, err := repo.InitRebase(nil, nil, onto, rebaseOpts) if err != nil { return nil, err } @@ -275,7 +275,7 @@ func headTree(repo *Repository) (*Tree, error) { return tree, nil } -func commitSomething(repo *Repository, something, content string, commitOpts commitOpts) (*Oid, error) { +func commitSomething(repo *Repository, something, content string, commitOpts commitOptions) (*Oid, error) { headCommit, err := headCommit(repo) if err != nil { return nil, err @@ -348,10 +348,10 @@ func commitSomething(repo *Repository, something, content string, commitOpts com } } - opts := &CheckoutOpts{ + checkoutOpts := &CheckoutOptions{ Strategy: CheckoutRemoveUntracked | CheckoutForce, } - err = repo.CheckoutIndex(index, opts) + err = repo.CheckoutIndex(index, checkoutOpts) if err != nil { return nil, err } diff --git a/reference.go b/reference.go index 3288c2757..02022a4f2 100644 --- a/reference.go +++ b/reference.go @@ -424,7 +424,7 @@ func (i *ReferenceIterator) Names() *ReferenceNameIterator { } // NextName retrieves the next reference name. If the iteration is over, -// the returned error is git.ErrIterOver +// the returned error code is git.ErrorCodeIterOver func (v *ReferenceNameIterator) Next() (string, error) { var ptr *C.char @@ -440,7 +440,7 @@ func (v *ReferenceNameIterator) Next() (string, error) { } // Next retrieves the next reference. If the iterationis over, the -// returned error is git.ErrIterOver +// returned error code is git.ErrorCodeIterOver func (v *ReferenceIterator) Next() (*Reference, error) { var ptr *C.git_reference @@ -470,7 +470,7 @@ func (v *ReferenceIterator) Free() { C.git_reference_iterator_free(v.ptr) } -// ReferenceIsValidName ensures the reference name is well-formed. +// ReferenceIsValidName returns whether the reference name is well-formed. // // Valid reference names must follow one of two patterns: // @@ -483,10 +483,8 @@ func (v *ReferenceIterator) Free() { func ReferenceIsValidName(name string) bool { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) - if C.git_reference_is_valid_name(cname) == 1 { - return true - } - return false + + return C.git_reference_is_valid_name(cname) == 1 } const ( diff --git a/reference_test.go b/reference_test.go index e42db41da..285adb59a 100644 --- a/reference_test.go +++ b/reference_test.go @@ -106,7 +106,7 @@ func TestReferenceIterator(t *testing.T) { list = append(list, name) name, err = nameIter.Next() } - if !IsErrorCode(err, ErrIterOver) { + if !IsErrorCode(err, ErrorCodeIterOver) { t.Fatal("Iteration not over") } @@ -122,7 +122,7 @@ func TestReferenceIterator(t *testing.T) { count++ _, err = iter.Next() } - if !IsErrorCode(err, ErrIterOver) { + if !IsErrorCode(err, ErrorCodeIterOver) { t.Fatal("Iteration not over") } @@ -242,7 +242,7 @@ func TestReferenceNormalizeName(t *testing.T) { } ref, err = ReferenceNormalizeName("foo^", ReferenceFormatNormal) - if !IsErrorCode(err, ErrInvalidSpec) { + if !IsErrorCode(err, ErrorCodeInvalidSpec) { t.Errorf("foo^ should be invalid") } } diff --git a/remote.go b/remote.go index 9d1705460..a4b23b5ce 100644 --- a/remote.go +++ b/remote.go @@ -313,7 +313,7 @@ func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, da C.memcpy(unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), unsafe.Pointer(&ccert.hash_sha1[0]), C.size_t(len(cert.Hostkey.HashSHA1))) } else { cstr := C.CString("Unsupported certificate type") - C.giterr_set_str(C.GITERR_NET, cstr) + C.giterr_set_str(C.int(ErrorClassNet), cstr) C.free(unsafe.Pointer(cstr)) return -1 // we don't support anything else atm } @@ -367,13 +367,12 @@ func freeProxyOptions(ptr *C.git_proxy_options) { C.free(unsafe.Pointer(ptr.url)) } +// RemoteIsValidName returns whether the remote name is well-formed. func RemoteIsValidName(name string) bool { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) - if C.git_remote_is_valid_name(cname) == 1 { - return true - } - return false + + return C.git_remote_is_valid_name(cname) == 1 } func (r *Remote) Free() { diff --git a/remote_test.go b/remote_test.go index 8b20fd24c..7e1685627 100644 --- a/remote_test.go +++ b/remote_test.go @@ -27,7 +27,7 @@ func TestListRemotes(t *testing.T) { func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T) ErrorCode { if hostname != "github.com" { t.Fatal("Hostname does not match") - return ErrUser + return ErrorCodeUser } return 0 diff --git a/reset.go b/reset.go index 031f5bd12..b3ecff4a1 100644 --- a/reset.go +++ b/reset.go @@ -14,7 +14,7 @@ const ( ResetHard ResetType = C.GIT_RESET_HARD ) -func (r *Repository) ResetToCommit(commit *Commit, resetType ResetType, opts *CheckoutOpts) error { +func (r *Repository) ResetToCommit(commit *Commit, resetType ResetType, opts *CheckoutOptions) error { runtime.LockOSThread() defer runtime.UnlockOSThread() ret := C.git_reset(r.ptr, commit.ptr, C.git_reset_t(resetType), opts.toC()) diff --git a/reset_test.go b/reset_test.go index 89ebc49e0..ff37bfcc9 100644 --- a/reset_test.go +++ b/reset_test.go @@ -37,7 +37,7 @@ func TestResetToCommit(t *testing.T) { commitToResetTo, err := repo.LookupCommit(commitId) checkFatal(t, err) - repo.ResetToCommit(commitToResetTo, ResetHard, &CheckoutOpts{}) + repo.ResetToCommit(commitToResetTo, ResetHard, &CheckoutOptions{}) // check that the file now reads "testing reset" like it did before bytes, err := ioutil.ReadFile(pathInRepo(repo, "README")) diff --git a/revert.go b/revert.go index e745f11ab..cabc30386 100644 --- a/revert.go +++ b/revert.go @@ -12,7 +12,7 @@ import ( type RevertOptions struct { Mainline uint MergeOpts MergeOptions - CheckoutOpts CheckoutOpts + CheckoutOpts CheckoutOptions } func (opts *RevertOptions) toC() *C.git_revert_options { @@ -33,7 +33,7 @@ func revertOptionsFromC(opts *C.git_revert_options) RevertOptions { } func freeRevertOptions(opts *C.git_revert_options) { - freeCheckoutOpts(&opts.checkout_opts) + freeCheckoutOptions(&opts.checkout_opts) } // DefaultRevertOptions initialises a RevertOptions struct with default values diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh index 8888ec913..c6498366e 100755 --- a/script/build-libgit2.sh +++ b/script/build-libgit2.sh @@ -61,6 +61,7 @@ cmake -DTHREADSAFE=ON \ -DCMAKE_C_FLAGS=-fPIC \ -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ -DCMAKE_INSTALL_PREFIX="${BUILD_INSTALL_PREFIX}" \ + -DDEPRECATE_HARD=ON \ "${VENDORED_PATH}" && exec cmake --build . --target install diff --git a/stash.go b/stash.go index 8743da89e..2b3ea8ce0 100644 --- a/stash.go +++ b/stash.go @@ -140,7 +140,7 @@ func stashApplyProgressCb(progress C.git_stash_apply_progress_t, handle unsafe.P // StashApplyOptions represents options to control the apply operation. type StashApplyOptions struct { Flags StashApplyFlag - CheckoutOptions CheckoutOpts // options to use when writing files to the working directory + CheckoutOptions CheckoutOptions // options to use when writing files to the working directory ProgressCallback StashApplyProgressCallback // optional callback to notify the consumer of application progress } @@ -173,7 +173,7 @@ func (opts *StashApplyOptions) toC() ( version: C.GIT_STASH_APPLY_OPTIONS_VERSION, flags: C.git_stash_apply_flags(opts.Flags), } - populateCheckoutOpts(&optsC.checkout_options, &opts.CheckoutOptions) + populateCheckoutOptions(&optsC.checkout_options, &opts.CheckoutOptions) if opts.ProgressCallback != nil { C._go_git_setup_stash_apply_progress_callbacks(optsC) optsC.progress_payload = pointerHandles.Track(progressData) @@ -191,21 +191,21 @@ func untrackStashApplyOptionsCallback(optsC *C.git_stash_apply_options) { func freeStashApplyOptions(optsC *C.git_stash_apply_options) { if optsC != nil { - freeCheckoutOpts(&optsC.checkout_options) + freeCheckoutOptions(&optsC.checkout_options) } } // Apply applies a single stashed state from the stash list. // // If local changes in the working directory conflict with changes in the -// stash then ErrConflict will be returned. In this case, the index +// stash then ErrorCodeConflict will be returned. In this case, the index // will always remain unmodified and all files in the working directory will // remain unmodified. However, if you are restoring untracked files or // ignored files and there is a conflict when applying the modified files, // then those files will remain in the working directory. // // If passing the StashApplyReinstateIndex flag and there would be conflicts -// when reinstating the index, the function will return ErrConflict +// when reinstating the index, the function will return ErrorCodeConflict // and both the working directory and index will be left unmodified. // // Note that a minimum checkout strategy of 'CheckoutSafe' is implied. @@ -213,12 +213,12 @@ func freeStashApplyOptions(optsC *C.git_stash_apply_options) { // 'index' is the position within the stash list. 0 points to the most // recent stashed state. // -// Returns error code ErrNotFound if there's no stashed state for the given -// index, error code ErrConflict if local changes in the working directory +// Returns error code ErrorCodeNotFound if there's no stashed state for the given +// index, error code ErrorCodeConflict if local changes in the working directory // conflict with changes in the stash, the user returned error from the // StashApplyProgressCallback, if any, or other error code. // -// Error codes can be interogated with IsErrorCode(err, ErrNotFound). +// Error codes can be interogated with IsErrorCode(err, ErrorCodeNotFound). func (c *StashCollection) Apply(index int, opts StashApplyOptions) error { optsC, progressData := opts.toC() defer untrackStashApplyOptionsCallback(optsC) @@ -298,7 +298,7 @@ func (c *StashCollection) Foreach(callback StashCallback) error { // 'index' is the position within the stash list. 0 points // to the most recent stashed state. // -// Returns error code ErrNotFound if there's no stashed +// Returns error code ErrorCodeNotFound if there's no stashed // state for the given index. func (c *StashCollection) Drop(index int) error { runtime.LockOSThread() @@ -320,7 +320,7 @@ func (c *StashCollection) Drop(index int) error { // // 'opts' controls how stashes are applied. // -// Returns error code ErrNotFound if there's no stashed +// Returns error code ErrorCodeNotFound if there's no stashed // state for the given index. func (c *StashCollection) Pop(index int, opts StashApplyOptions) error { optsC, progressData := opts.toC() diff --git a/stash_test.go b/stash_test.go index 180a16b0e..62990b139 100644 --- a/stash_test.go +++ b/stash_test.go @@ -56,8 +56,8 @@ func TestStash(t *testing.T) { // Apply: no stash for the given index err = repo.Stashes.Apply(1, opts) - if !IsErrorCode(err, ErrNotFound) { - t.Errorf("expecting GIT_ENOTFOUND error code %d, got %v", ErrNotFound, err) + if !IsErrorCode(err, ErrorCodeNotFound) { + t.Errorf("expecting GIT_ENOTFOUND error code %d, got %v", ErrorCodeNotFound, err) } // Apply: callback stopped diff --git a/submodule.go b/submodule.go index f4b4f15e3..ea65bfe98 100644 --- a/submodule.go +++ b/submodule.go @@ -106,20 +106,21 @@ func (c *SubmoduleCollection) Lookup(name string) (*Submodule, error) { return newSubmoduleFromC(ptr, c.repo), nil } -type SubmoduleCbk func(sub *Submodule, name string) int +// SubmoduleCallback is a function that is called for every submodule found in SubmoduleCollection.Foreach. +type SubmoduleCallback func(sub *Submodule, name string) int -//export SubmoduleVisitor -func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { +//export submoduleCallback +func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { sub := &Submodule{(*C.git_submodule)(csub), nil} - if callback, ok := pointerHandles.Get(handle).(SubmoduleCbk); ok { + if callback, ok := pointerHandles.Get(handle).(SubmoduleCallback); ok { return (C.int)(callback(sub, C.GoString(name))) } else { panic("invalid submodule visitor callback") } } -func (c *SubmoduleCollection) Foreach(cbk SubmoduleCbk) error { +func (c *SubmoduleCollection) Foreach(cbk SubmoduleCallback) error { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -366,7 +367,7 @@ func populateSubmoduleUpdateOptions(ptr *C.git_submodule_update_options, opts *S return nil } - populateCheckoutOpts(&ptr.checkout_opts, opts.CheckoutOpts) + populateCheckoutOptions(&ptr.checkout_opts, opts.CheckoutOpts) populateFetchOptions(&ptr.fetch_opts, opts.FetchOptions) return nil diff --git a/tree.go b/tree.go index b309193cf..cf85f2eac 100644 --- a/tree.go +++ b/tree.go @@ -121,8 +121,8 @@ func (t *Tree) EntryCount() uint64 { type TreeWalkCallback func(string, *TreeEntry) int -//export CallbackGitTreeWalk -func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int { +//export treeWalkCallback +func treeWalkCallback(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int { root := C.GoString(_root) if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok { diff --git a/walk.go b/walk.go index 287edb631..6020c97c3 100644 --- a/walk.go +++ b/walk.go @@ -182,7 +182,7 @@ func (v *RevWalk) Iterate(fun RevWalkIterator) (err error) { oid := new(Oid) for { err = v.Next(oid) - if IsErrorCode(err, ErrIterOver) { + if IsErrorCode(err, ErrorCodeIterOver) { return nil } if err != nil { diff --git a/wrapper.c b/wrapper.c index df767fb49..7ca2d5903 100644 --- a/wrapper.c +++ b/wrapper.c @@ -18,12 +18,12 @@ void _go_git_populate_checkout_cb(git_checkout_options *opts) int _go_git_visit_submodule(git_repository *repo, void *fct) { - return git_submodule_foreach(repo, (gogit_submodule_cbk)&SubmoduleVisitor, fct); + return git_submodule_foreach(repo, (gogit_submodule_cbk)&submoduleCallback, fct); } int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr) { - return git_tree_walk(tree, mode, (git_treewalk_cb)&CallbackGitTreeWalk, ptr); + return git_tree_walk(tree, mode, (git_treewalk_cb)&treeWalkCallback, ptr); } int _go_git_packbuilder_foreach(git_packbuilder *pb, void *payload) From 37d110dbf082a7a6b23401a09a49e639bbd46baa Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 5 Dec 2020 17:10:18 -0800 Subject: [PATCH 073/103] Refactor all callbacks (#700) (#705) This change is a preparation for another change that makes all callback types return a Go error instead of an error code / an integer. That is going to make make things a lot more idiomatic. The reason this change is split is threefold: a) This change is mostly mechanical and should contain no semantic changes. b) This change is backwards-compatible (in the Go API compatibility sense of the word), and thus can be backported to all other releases. c) It makes the other change a bit smaller and more focused on just one thing. Concretely, this change makes all callbacks populate a Go error when they fail. If the callback is invoked from the same stack as the function to which it was passed (e.g. for `Tree.Walk`), it will preserve the error object directly into a struct that also holds the callback function. Otherwise if the callback is pased to one func and will be invoked when run from another one (e.g. for `Repository.InitRebase`), the error string is saved into the libgit2 thread-local storage and then re-created as a `GitError`. (cherry picked from commit 5d8eaf7e65c404a0d10d3705697dd99369630dda) --- checkout.go | 99 +++++++++---- cherrypick.go | 18 ++- clone.go | 81 +++++++---- diff.go | 330 ++++++++++++++++++++++++++----------------- diff_test.go | 3 +- git.go | 11 ++ index.go | 55 ++++++-- merge.go | 21 ++- odb.go | 35 +++-- packbuilder.go | 35 ++--- patch.go | 13 +- rebase.go | 59 +++++--- remote.go | 135 ++++++++++++------ remote_test.go | 2 +- reset.go | 9 +- revert.go | 36 ++--- stash.go | 120 ++++++++-------- submodule.go | 58 ++++++-- tag.go | 36 ++--- tree.go | 42 ++++-- wrapper.c | 374 +++++++++++++++++++++++++++++++++++++++---------- 21 files changed, 1071 insertions(+), 501 deletions(-) diff --git a/checkout.go b/checkout.go index 71ccac495..30f606fac 100644 --- a/checkout.go +++ b/checkout.go @@ -3,10 +3,11 @@ package git /* #include -extern void _go_git_populate_checkout_cb(git_checkout_options *opts); +extern void _go_git_populate_checkout_callbacks(git_checkout_options *opts); */ import "C" import ( + "errors" "os" "runtime" "unsafe" @@ -76,10 +77,10 @@ func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOptions { NotifyFlags: CheckoutNotifyType(c.notify_flags), } if c.notify_payload != nil { - opts.NotifyCallback = pointerHandles.Get(c.notify_payload).(*CheckoutOptions).NotifyCallback + opts.NotifyCallback = pointerHandles.Get(c.notify_payload).(*checkoutCallbackData).options.NotifyCallback } if c.progress_payload != nil { - opts.ProgressCallback = pointerHandles.Get(c.progress_payload).(*CheckoutOptions).ProgressCallback + opts.ProgressCallback = pointerHandles.Get(c.progress_payload).(*checkoutCallbackData).options.ProgressCallback } if c.target_directory != nil { opts.TargetDirectory = C.GoString(c.target_directory) @@ -87,19 +88,26 @@ func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOptions { return opts } -func (opts *CheckoutOptions) toC() *C.git_checkout_options { +func (opts *CheckoutOptions) toC(errorTarget *error) *C.git_checkout_options { if opts == nil { return nil } - c := C.git_checkout_options{} - populateCheckoutOptions(&c, opts) - return &c + return populateCheckoutOptions(&C.git_checkout_options{}, opts, errorTarget) +} + +type checkoutCallbackData struct { + options *CheckoutOptions + errorTarget *error } //export checkoutNotifyCallback -func checkoutNotifyCallback(why C.git_checkout_notify_t, cpath *C.char, cbaseline, ctarget, cworkdir, data unsafe.Pointer) int { - if data == nil { - return 0 +func checkoutNotifyCallback( + why C.git_checkout_notify_t, + cpath *C.char, + cbaseline, ctarget, cworkdir, handle unsafe.Pointer, +) C.int { + if handle == nil { + return C.int(ErrorCodeOK) } path := C.GoString(cpath) var baseline, target, workdir DiffFile @@ -112,26 +120,35 @@ func checkoutNotifyCallback(why C.git_checkout_notify_t, cpath *C.char, cbaselin if cworkdir != nil { workdir = diffFileFromC((*C.git_diff_file)(cworkdir)) } - opts := pointerHandles.Get(data).(*CheckoutOptions) - if opts.NotifyCallback == nil { - return 0 + data := pointerHandles.Get(handle).(*checkoutCallbackData) + if data.options.NotifyCallback == nil { + return C.int(ErrorCodeOK) + } + ret := data.options.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir) + if ret < 0 { + *data.errorTarget = errors.New(ErrorCode(ret).String()) + return C.int(ErrorCodeUser) } - return int(opts.NotifyCallback(CheckoutNotifyType(why), path, baseline, target, workdir)) + return C.int(ErrorCodeOK) } //export checkoutProgressCallback -func checkoutProgressCallback(path *C.char, completed_steps, total_steps C.size_t, data unsafe.Pointer) int { - opts := pointerHandles.Get(data).(*CheckoutOptions) - if opts.ProgressCallback == nil { - return 0 +func checkoutProgressCallback( + path *C.char, + completed_steps, total_steps C.size_t, + handle unsafe.Pointer, +) { + data := pointerHandles.Get(handle).(*checkoutCallbackData) + if data.options.ProgressCallback == nil { + return } - return int(opts.ProgressCallback(C.GoString(path), uint(completed_steps), uint(total_steps))) + data.options.ProgressCallback(C.GoString(path), uint(completed_steps), uint(total_steps)) } // Convert the CheckoutOptions struct to the corresponding // C-struct. Returns a pointer to ptr, or nil if opts is nil, in order // to help with what to pass. -func populateCheckoutOptions(ptr *C.git_checkout_options, opts *CheckoutOptions) *C.git_checkout_options { +func populateCheckoutOptions(ptr *C.git_checkout_options, opts *CheckoutOptions, errorTarget *error) *C.git_checkout_options { if opts == nil { return nil } @@ -143,14 +160,18 @@ func populateCheckoutOptions(ptr *C.git_checkout_options, opts *CheckoutOptions) ptr.file_mode = C.uint(opts.FileMode.Perm()) ptr.notify_flags = C.uint(opts.NotifyFlags) if opts.NotifyCallback != nil || opts.ProgressCallback != nil { - C._go_git_populate_checkout_cb(ptr) - } - payload := pointerHandles.Track(opts) - if opts.NotifyCallback != nil { - ptr.notify_payload = payload - } - if opts.ProgressCallback != nil { - ptr.progress_payload = payload + C._go_git_populate_checkout_callbacks(ptr) + data := &checkoutCallbackData{ + options: opts, + errorTarget: errorTarget, + } + payload := pointerHandles.Track(data) + if opts.NotifyCallback != nil { + ptr.notify_payload = payload + } + if opts.ProgressCallback != nil { + ptr.progress_payload = payload + } } if opts.TargetDirectory != "" { ptr.target_directory = C.CString(opts.TargetDirectory) @@ -177,6 +198,8 @@ func freeCheckoutOptions(ptr *C.git_checkout_options) { } if ptr.notify_payload != nil { pointerHandles.Untrack(ptr.notify_payload) + } else if ptr.progress_payload != nil { + pointerHandles.Untrack(ptr.progress_payload) } } @@ -186,11 +209,16 @@ func (v *Repository) CheckoutHead(opts *CheckoutOptions) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - cOpts := opts.toC() + var err error + cOpts := opts.toC(&err) defer freeCheckoutOptions(cOpts) ret := C.git_checkout_head(v.ptr, cOpts) runtime.KeepAlive(v) + + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } @@ -210,11 +238,15 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOptions) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - cOpts := opts.toC() + var err error + cOpts := opts.toC(&err) defer freeCheckoutOptions(cOpts) ret := C.git_checkout_index(v.ptr, iptr, cOpts) runtime.KeepAlive(v) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } @@ -226,11 +258,16 @@ func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOptions) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - cOpts := opts.toC() + var err error + cOpts := opts.toC(&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) } diff --git a/cherrypick.go b/cherrypick.go index b80852b5c..142852cdb 100644 --- a/cherrypick.go +++ b/cherrypick.go @@ -25,7 +25,7 @@ func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions { return opts } -func (opts *CherrypickOptions) toC() *C.git_cherrypick_options { +func (opts *CherrypickOptions) toC(errorTarget *error) *C.git_cherrypick_options { if opts == nil { return nil } @@ -33,7 +33,7 @@ func (opts *CherrypickOptions) toC() *C.git_cherrypick_options { c.version = C.uint(opts.Version) c.mainline = C.uint(opts.Mainline) c.merge_opts = *opts.MergeOpts.toC() - c.checkout_opts = *opts.CheckoutOpts.toC() + c.checkout_opts = *opts.CheckoutOpts.toC(errorTarget) return &c } @@ -41,6 +41,7 @@ func freeCherrypickOpts(ptr *C.git_cherrypick_options) { if ptr == nil { return } + freeMergeOptions(&ptr.merge_opts) freeCheckoutOptions(&ptr.checkout_opts) } @@ -62,14 +63,18 @@ func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - cOpts := opts.toC() + var err error + cOpts := opts.toC(&err) defer freeCherrypickOpts(cOpts) - ecode := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts) + ret := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts) runtime.KeepAlive(v) runtime.KeepAlive(commit) - if ecode < 0 { - return MakeGitError(ecode) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } + if ret < 0 { + return MakeGitError(ret) } return nil } @@ -79,6 +84,7 @@ func (r *Repository) CherrypickCommit(pick, our *Commit, opts CherrypickOptions) defer runtime.UnlockOSThread() cOpts := opts.MergeOpts.toC() + 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) diff --git a/clone.go b/clone.go index b329a25d1..4a3c3d13c 100644 --- a/clone.go +++ b/clone.go @@ -3,10 +3,11 @@ package git /* #include -extern void _go_git_populate_remote_cb(git_clone_options *opts); +extern void _go_git_populate_clone_callbacks(git_clone_options *opts); */ import "C" import ( + "errors" "runtime" "unsafe" ) @@ -28,20 +29,23 @@ func Clone(url string, path string, options *CloneOptions) (*Repository, error) cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) - copts := (*C.git_clone_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_clone_options{})))) - populateCloneOptions(copts, options) - defer freeCloneOptions(copts) + var err error + cOptions := populateCloneOptions(&C.git_clone_options{}, options, &err) + defer freeCloneOptions(cOptions) if len(options.CheckoutBranch) != 0 { - copts.checkout_branch = C.CString(options.CheckoutBranch) + cOptions.checkout_branch = C.CString(options.CheckoutBranch) } runtime.LockOSThread() defer runtime.UnlockOSThread() var ptr *C.git_repository - ret := C.git_clone(&ptr, curl, cpath, copts) + ret := C.git_clone(&ptr, curl, cpath, cOptions) + if ret == C.int(ErrorCodeUser) && err != nil { + return nil, err + } if ret < 0 { return nil, MakeGitError(ret) } @@ -50,47 +54,69 @@ func Clone(url string, path string, options *CloneOptions) (*Repository, error) } //export remoteCreateCallback -func remoteCreateCallback(cremote unsafe.Pointer, crepo unsafe.Pointer, cname, curl *C.char, payload unsafe.Pointer) C.int { +func remoteCreateCallback( + cremote unsafe.Pointer, + crepo unsafe.Pointer, + cname, curl *C.char, + payload unsafe.Pointer, +) C.int { name := C.GoString(cname) url := C.GoString(curl) repo := newRepositoryFromC((*C.git_repository)(crepo)) // We don't own this repository, so make sure we don't try to free it runtime.SetFinalizer(repo, nil) - if opts, ok := pointerHandles.Get(payload).(CloneOptions); ok { - remote, errorCode := opts.RemoteCreateCallback(repo, name, url) - // clear finalizer as the calling C function will - // free the remote itself - runtime.SetFinalizer(remote, nil) - - if errorCode == ErrorCodeOK && remote != nil { - cptr := (**C.git_remote)(cremote) - *cptr = remote.ptr - } else if errorCode == ErrorCodeOK && remote == nil { - panic("no remote created by callback") - } - - return C.int(errorCode) - } else { + data, ok := pointerHandles.Get(payload).(*cloneCallbackData) + if !ok { panic("invalid remote create callback") } + + remote, ret := data.options.RemoteCreateCallback(repo, name, url) + // clear finalizer as the calling C function will + // free the remote itself + runtime.SetFinalizer(remote, nil) + + if ret < 0 { + *data.errorTarget = errors.New(ErrorCode(ret).String()) + return C.int(ErrorCodeUser) + } + + if remote == nil { + panic("no remote created by callback") + } + + cptr := (**C.git_remote)(cremote) + *cptr = remote.ptr + + return C.int(ErrorCodeOK) +} + +type cloneCallbackData struct { + options *CloneOptions + errorTarget *error } -func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions) { +func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions, errorTarget *error) *C.git_clone_options { C.git_clone_init_options(ptr, C.GIT_CLONE_OPTIONS_VERSION) if opts == nil { - return + return nil } - populateCheckoutOptions(&ptr.checkout_opts, opts.CheckoutOpts) + populateCheckoutOptions(&ptr.checkout_opts, opts.CheckoutOpts, errorTarget) populateFetchOptions(&ptr.fetch_opts, opts.FetchOptions) ptr.bare = cbool(opts.Bare) if opts.RemoteCreateCallback != nil { + data := &cloneCallbackData{ + options: opts, + errorTarget: errorTarget, + } // Go v1.1 does not allow to assign a C function pointer - C._go_git_populate_remote_cb(ptr) - ptr.remote_cb_payload = pointerHandles.Track(*opts) + C._go_git_populate_clone_callbacks(ptr) + ptr.remote_cb_payload = pointerHandles.Track(data) } + + return ptr } func freeCloneOptions(ptr *C.git_clone_options) { @@ -105,5 +131,4 @@ func freeCloneOptions(ptr *C.git_clone_options) { } C.free(unsafe.Pointer(ptr.checkout_branch)) - C.free(unsafe.Pointer(ptr)) } diff --git a/diff.go b/diff.go index 7f824fa6f..1a67d5d85 100644 --- a/diff.go +++ b/diff.go @@ -293,11 +293,11 @@ func (diff *Diff) Stats() (*DiffStats, error) { return stats, nil } -type diffForEachData struct { - FileCallback DiffForEachFileCallback - HunkCallback DiffForEachHunkCallback - LineCallback DiffForEachLineCallback - Error error +type diffForEachCallbackData struct { + fileCallback DiffForEachFileCallback + hunkCallback DiffForEachHunkCallback + lineCallback DiffForEachLineCallback + errorTarget *error } type DiffForEachFileCallback func(delta DiffDelta, progress float64) (DiffForEachHunkCallback, error) @@ -325,82 +325,91 @@ func (diff *Diff) ForEach(cbFile DiffForEachFileCallback, detail DiffDetail) err intLines = C.int(1) } - data := &diffForEachData{ - FileCallback: cbFile, + var err error + data := &diffForEachCallbackData{ + fileCallback: cbFile, + errorTarget: &err, } handle := pointerHandles.Track(data) defer pointerHandles.Untrack(handle) - ecode := C._go_git_diff_foreach(diff.ptr, 1, intHunks, intLines, handle) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C._go_git_diff_foreach(diff.ptr, 1, intHunks, intLines, handle) runtime.KeepAlive(diff) - if ecode < 0 { - return data.Error + if ret == C.int(ErrorCodeUser) && err != nil { + return err } + if ret < 0 { + return MakeGitError(ret) + } + return nil } -//export diffForEachFileCb -func diffForEachFileCb(delta *C.git_diff_delta, progress C.float, handle unsafe.Pointer) int { +//export diffForEachFileCallback +func diffForEachFileCallback(delta *C.git_diff_delta, progress C.float, handle unsafe.Pointer) C.int { payload := pointerHandles.Get(handle) - data, ok := payload.(*diffForEachData) + data, ok := payload.(*diffForEachCallbackData) if !ok { panic("could not retrieve data for handle") } - data.HunkCallback = nil - if data.FileCallback != nil { - cb, err := data.FileCallback(diffDeltaFromC(delta), float64(progress)) + data.hunkCallback = nil + if data.fileCallback != nil { + cb, err := data.fileCallback(diffDeltaFromC(delta), float64(progress)) if err != nil { - data.Error = err - return -1 + *data.errorTarget = err + return C.int(ErrorCodeUser) } - data.HunkCallback = cb + data.hunkCallback = cb } - return 0 + return C.int(ErrorCodeOK) } type DiffForEachHunkCallback func(DiffHunk) (DiffForEachLineCallback, error) -//export diffForEachHunkCb -func diffForEachHunkCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, handle unsafe.Pointer) int { +//export diffForEachHunkCallback +func diffForEachHunkCallback(delta *C.git_diff_delta, hunk *C.git_diff_hunk, handle unsafe.Pointer) C.int { payload := pointerHandles.Get(handle) - data, ok := payload.(*diffForEachData) + data, ok := payload.(*diffForEachCallbackData) if !ok { panic("could not retrieve data for handle") } - data.LineCallback = nil - if data.HunkCallback != nil { - cb, err := data.HunkCallback(diffHunkFromC(hunk)) + data.lineCallback = nil + if data.hunkCallback != nil { + cb, err := data.hunkCallback(diffHunkFromC(hunk)) if err != nil { - data.Error = err - return -1 + *data.errorTarget = err + return C.int(ErrorCodeUser) } - data.LineCallback = cb + data.lineCallback = cb } - return 0 + return C.int(ErrorCodeOK) } type DiffForEachLineCallback func(DiffLine) error -//export diffForEachLineCb -func diffForEachLineCb(delta *C.git_diff_delta, hunk *C.git_diff_hunk, line *C.git_diff_line, handle unsafe.Pointer) int { +//export diffForEachLineCallback +func diffForEachLineCallback(delta *C.git_diff_delta, hunk *C.git_diff_hunk, line *C.git_diff_line, handle unsafe.Pointer) C.int { payload := pointerHandles.Get(handle) - data, ok := payload.(*diffForEachData) + data, ok := payload.(*diffForEachCallbackData) if !ok { panic("could not retrieve data for handle") } - err := data.LineCallback(diffLineFromC(line)) + err := data.lineCallback(diffLineFromC(line)) if err != nil { - data.Error = err - return -1 + *data.errorTarget = err + return C.int(ErrorCodeUser) } - return 0 + return C.int(ErrorCodeOK) } func (diff *Diff) Patch(deltaIndex int) (*Patch, error) { @@ -585,93 +594,98 @@ var ( ErrDeltaSkip = errors.New("Skip delta") ) -type diffNotifyData struct { - Callback DiffNotifyCallback - Repository *Repository - Error error +type diffNotifyCallbackData struct { + callback DiffNotifyCallback + repository *Repository + errorTarget *error } -//export diffNotifyCb -func diffNotifyCb(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, matched_pathspec *C.char, handle unsafe.Pointer) int { +//export diffNotifyCallback +func diffNotifyCallback(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_delta, matched_pathspec *C.char, handle unsafe.Pointer) C.int { diff_so_far := (*C.git_diff)(_diff_so_far) payload := pointerHandles.Get(handle) - data, ok := payload.(*diffNotifyData) + data, ok := payload.(*diffNotifyCallbackData) if !ok { panic("could not retrieve data for handle") } - if data != nil { - // We are not taking ownership of this diff pointer, so no finalizer is set. - diff := &Diff{ - ptr: diff_so_far, - repo: data.Repository, - runFinalizer: false, - } + if data == nil { + return C.int(ErrorCodeOK) + } - err := data.Callback(diff, diffDeltaFromC(delta_to_add), C.GoString(matched_pathspec)) + // We are not taking ownership of this diff pointer, so no finalizer is set. + diff := &Diff{ + ptr: diff_so_far, + repo: data.repository, + runFinalizer: false, + } - // Since the callback could theoretically keep a reference to the diff - // (which could be freed by libgit2 if an error occurs later during the - // diffing process), this converts a use-after-free (terrible!) into a nil - // dereference ("just" pretty bad). - diff.ptr = nil + err := data.callback(diff, diffDeltaFromC(delta_to_add), C.GoString(matched_pathspec)) - if err == ErrDeltaSkip { - return 1 - } else if err != nil { - data.Error = err - return -1 - } else { - return 0 - } - } - return 0 -} + // Since the callback could theoretically keep a reference to the diff + // (which could be freed by libgit2 if an error occurs later during the + // diffing process), this converts a use-after-free (terrible!) into a nil + // dereference ("just" pretty bad). + diff.ptr = nil -func diffOptionsToC(opts *DiffOptions, repo *Repository) (copts *C.git_diff_options) { - cpathspec := C.git_strarray{} - if opts != nil { - notifyData := &diffNotifyData{ - Callback: opts.NotifyCallback, - Repository: repo, - } + if err == ErrDeltaSkip { + return 1 + } + if err != nil { + *data.errorTarget = err + return C.int(ErrorCodeUser) + } - if opts.Pathspec != nil { - cpathspec.count = C.size_t(len(opts.Pathspec)) - cpathspec.strings = makeCStringsFromStrings(opts.Pathspec) - } + return C.int(ErrorCodeOK) +} - copts = &C.git_diff_options{ - version: C.GIT_DIFF_OPTIONS_VERSION, - flags: C.uint32_t(opts.Flags), - ignore_submodules: C.git_submodule_ignore_t(opts.IgnoreSubmodules), - pathspec: cpathspec, - context_lines: C.uint32_t(opts.ContextLines), - interhunk_lines: C.uint32_t(opts.InterhunkLines), - id_abbrev: C.uint16_t(opts.IdAbbrev), - max_size: C.git_off_t(opts.MaxSize), - old_prefix: C.CString(opts.OldPrefix), - new_prefix: C.CString(opts.NewPrefix), - } +func (opts *DiffOptions) toC(repo *Repository, errorTarget *error) *C.git_diff_options { + if opts == nil { + return nil + } - if opts.NotifyCallback != nil { - C._go_git_setup_diff_notify_callbacks(copts) - copts.payload = pointerHandles.Track(notifyData) + cpathspec := C.git_strarray{} + if opts.Pathspec != nil { + cpathspec.count = C.size_t(len(opts.Pathspec)) + cpathspec.strings = makeCStringsFromStrings(opts.Pathspec) + } + + copts := &C.git_diff_options{ + version: C.GIT_DIFF_OPTIONS_VERSION, + flags: C.uint32_t(opts.Flags), + ignore_submodules: C.git_submodule_ignore_t(opts.IgnoreSubmodules), + pathspec: cpathspec, + context_lines: C.uint32_t(opts.ContextLines), + interhunk_lines: C.uint32_t(opts.InterhunkLines), + id_abbrev: C.uint16_t(opts.IdAbbrev), + max_size: C.git_off_t(opts.MaxSize), + old_prefix: C.CString(opts.OldPrefix), + new_prefix: C.CString(opts.NewPrefix), + } + + if opts.NotifyCallback != nil { + notifyData := &diffNotifyCallbackData{ + callback: opts.NotifyCallback, + repository: repo, + errorTarget: errorTarget, } + C._go_git_setup_diff_notify_callbacks(copts) + copts.payload = pointerHandles.Track(notifyData) } - return + return copts } func freeDiffOptions(copts *C.git_diff_options) { - if copts != nil { - cpathspec := copts.pathspec - freeStrarray(&cpathspec) - C.free(unsafe.Pointer(copts.old_prefix)) - C.free(unsafe.Pointer(copts.new_prefix)) - if copts.payload != nil { - pointerHandles.Untrack(copts.payload) - } + if copts == nil { + return + } + cpathspec := copts.pathspec + freeStrarray(&cpathspec) + C.free(unsafe.Pointer(copts.old_prefix)) + C.free(unsafe.Pointer(copts.new_prefix)) + if copts.payload != nil { + pointerHandles.Untrack(copts.payload) } } @@ -687,17 +701,21 @@ func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) ( newPtr = newTree.cast_ptr } - copts := diffOptionsToC(opts, v) + var err error + copts := opts.toC(v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C.git_diff_tree_to_tree(&diffPtr, v.ptr, oldPtr, newPtr, copts) + ret := C.git_diff_tree_to_tree(&diffPtr, v.ptr, oldPtr, newPtr, copts) runtime.KeepAlive(oldTree) runtime.KeepAlive(newTree) - if ecode < 0 { - return nil, MakeGitError(ecode) + if ret == C.int(ErrorCodeUser) && err != nil { + return nil, err + } + if ret < 0 { + return nil, MakeGitError(ret) } return newDiffFromC(diffPtr, v), nil } @@ -710,17 +728,22 @@ func (v *Repository) DiffTreeToWorkdir(oldTree *Tree, opts *DiffOptions) (*Diff, oldPtr = oldTree.cast_ptr } - copts := diffOptionsToC(opts, v) + var err error + copts := opts.toC(v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C.git_diff_tree_to_workdir(&diffPtr, v.ptr, oldPtr, copts) + ret := C.git_diff_tree_to_workdir(&diffPtr, v.ptr, oldPtr, copts) runtime.KeepAlive(oldTree) - if ecode < 0 { - return nil, MakeGitError(ecode) + if ret == C.int(ErrorCodeUser) && err != nil { + return nil, err + } + if ret < 0 { + return nil, MakeGitError(ret) } + return newDiffFromC(diffPtr, v), nil } @@ -737,18 +760,23 @@ func (v *Repository) DiffTreeToIndex(oldTree *Tree, index *Index, opts *DiffOpti indexPtr = index.ptr } - copts := diffOptionsToC(opts, v) + var err error + copts := opts.toC(v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C.git_diff_tree_to_index(&diffPtr, v.ptr, oldPtr, indexPtr, copts) + ret := C.git_diff_tree_to_index(&diffPtr, v.ptr, oldPtr, indexPtr, copts) runtime.KeepAlive(oldTree) runtime.KeepAlive(index) - if ecode < 0 { - return nil, MakeGitError(ecode) + if ret == C.int(ErrorCodeUser) && err != nil { + return nil, err + } + if ret < 0 { + return nil, MakeGitError(ret) } + return newDiffFromC(diffPtr, v), nil } @@ -760,17 +788,22 @@ func (v *Repository) DiffTreeToWorkdirWithIndex(oldTree *Tree, opts *DiffOptions oldPtr = oldTree.cast_ptr } - copts := diffOptionsToC(opts, v) + var err error + copts := opts.toC(v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C.git_diff_tree_to_workdir_with_index(&diffPtr, v.ptr, oldPtr, copts) + ret := C.git_diff_tree_to_workdir_with_index(&diffPtr, v.ptr, oldPtr, copts) runtime.KeepAlive(oldTree) - if ecode < 0 { - return nil, MakeGitError(ecode) + if ret == C.int(ErrorCodeUser) && err != nil { + return nil, err } + if ret < 0 { + return nil, MakeGitError(ret) + } + return newDiffFromC(diffPtr, v), nil } @@ -782,25 +815,32 @@ func (v *Repository) DiffIndexToWorkdir(index *Index, opts *DiffOptions) (*Diff, indexPtr = index.ptr } - copts := diffOptionsToC(opts, v) + var err error + copts := opts.toC(v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C.git_diff_index_to_workdir(&diffPtr, v.ptr, indexPtr, copts) + ret := C.git_diff_index_to_workdir(&diffPtr, v.ptr, indexPtr, copts) runtime.KeepAlive(index) - if ecode < 0 { - return nil, MakeGitError(ecode) + if ret == C.int(ErrorCodeUser) && err != nil { + return nil, err + } + if ret < 0 { + return nil, MakeGitError(ret) } + return newDiffFromC(diffPtr, v), nil } // DiffBlobs performs a diff between two arbitrary blobs. You can pass // whatever file names you'd like for them to appear as in the diff. func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, opts *DiffOptions, fileCallback DiffForEachFileCallback, detail DiffDetail) error { - data := &diffForEachData{ - FileCallback: fileCallback, + var err error + data := &diffForEachCallbackData{ + fileCallback: fileCallback, + errorTarget: &err, } intHunks := C.int(0) @@ -832,18 +872,50 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, newBlobPath := C.CString(newAsPath) defer C.free(unsafe.Pointer(newBlobPath)) - copts := diffOptionsToC(opts, repo) + copts := opts.toC(repo, &err) defer freeDiffOptions(copts) runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C._go_git_diff_blobs(oldBlobPtr, oldBlobPath, newBlobPtr, newBlobPath, copts, 1, intHunks, intLines, handle) + ret := C._go_git_diff_blobs(oldBlobPtr, oldBlobPath, newBlobPtr, newBlobPath, copts, 1, intHunks, intLines, handle) runtime.KeepAlive(oldBlob) runtime.KeepAlive(newBlob) - if ecode < 0 { - return MakeGitError(ecode) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } + if ret < 0 { + return MakeGitError(ret) } return nil } + +// DiffFromBuffer reads the contents of a git patch file into a Diff object. +// +// The diff object produced is similar to the one that would be produced if you +// actually produced it computationally by comparing two trees, however there +// may be subtle differences. For example, a patch file likely contains +// abbreviated object IDs, so the object IDs in a git_diff_delta produced by +// this function will also be abbreviated. +// +// This function will only read patch files created by a git implementation, it +// will not read unified diffs produced by the diff program, nor any other +// types of patch files. +func DiffFromBuffer(buffer []byte, repo *Repository) (*Diff, error) { + var diff *C.git_diff + + cBuffer := C.CBytes(buffer) + defer C.free(unsafe.Pointer(cBuffer)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ecode := C.git_diff_from_buffer(&diff, (*C.char)(cBuffer), C.size_t(len(buffer))) + if ecode < 0 { + return nil, MakeGitError(ecode) + } + runtime.KeepAlive(diff) + + return newDiffFromC(diff, repo), nil +} diff --git a/diff_test.go b/diff_test.go index 6fbad512e..f486e8a45 100644 --- a/diff_test.go +++ b/diff_test.go @@ -169,9 +169,8 @@ func TestDiffTreeToTree(t *testing.T) { }, DiffDetailLines) if err != errTest { - t.Fatal("Expected custom error to be returned") + t.Fatalf("Expected custom error to be returned, got %v, want %v", err, errTest) } - } func createTestTrees(t *testing.T, repo *Repository) (originalTree *Tree, newTree *Tree) { diff --git a/git.go b/git.go index 34086a9a5..1b01ec122 100644 --- a/git.go +++ b/git.go @@ -326,6 +326,17 @@ func ucbool(b bool) C.uint { return C.uint(0) } +func setCallbackError(errorMessage **C.char, err error) C.int { + if err != nil { + *errorMessage = C.CString(err.Error()) + if gitError, ok := err.(*GitError); ok { + return C.int(gitError.Code) + } + return C.int(ErrorCodeUser) + } + return C.int(ErrorCodeOK) +} + func Discover(start string, across_fs bool, ceiling_dirs []string) (string, error) { ceildirs := C.CString(strings.Join(ceiling_dirs, string(C.GIT_PATH_LIST_SEPARATOR))) defer C.free(unsafe.Pointer(ceildirs)) diff --git a/index.go b/index.go index 7173ae015..4960a73db 100644 --- a/index.go +++ b/index.go @@ -10,12 +10,17 @@ extern int _go_git_index_remove_all(git_index*, const git_strarray*, void*); */ import "C" import ( + "errors" "fmt" "runtime" "unsafe" ) type IndexMatchedPathCallback func(string, string) int +type indexMatchedPathCallbackData struct { + callback IndexMatchedPathCallback + errorTarget *error +} // IndexAddOption is a set of flags for APIs that add files matching pathspec. type IndexAddOption uint @@ -227,12 +232,17 @@ func (v *Index) AddAll(pathspecs []string, flags IndexAddOption, callback IndexM cpathspecs.strings = makeCStringsFromStrings(pathspecs) defer freeStrarray(&cpathspecs) + var err error + data := indexMatchedPathCallbackData{ + callback: callback, + errorTarget: &err, + } runtime.LockOSThread() defer runtime.UnlockOSThread() var handle unsafe.Pointer if callback != nil { - handle = pointerHandles.Track(callback) + handle = pointerHandles.Track(&data) defer pointerHandles.Untrack(handle) } @@ -243,9 +253,13 @@ func (v *Index) AddAll(pathspecs []string, flags IndexAddOption, callback IndexM handle, ) runtime.KeepAlive(v) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } + return nil } @@ -255,12 +269,17 @@ func (v *Index) UpdateAll(pathspecs []string, callback IndexMatchedPathCallback) cpathspecs.strings = makeCStringsFromStrings(pathspecs) defer freeStrarray(&cpathspecs) + var err error + data := indexMatchedPathCallbackData{ + callback: callback, + errorTarget: &err, + } runtime.LockOSThread() defer runtime.UnlockOSThread() var handle unsafe.Pointer if callback != nil { - handle = pointerHandles.Track(callback) + handle = pointerHandles.Track(&data) defer pointerHandles.Untrack(handle) } @@ -270,9 +289,13 @@ func (v *Index) UpdateAll(pathspecs []string, callback IndexMatchedPathCallback) handle, ) runtime.KeepAlive(v) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } + return nil } @@ -282,12 +305,17 @@ func (v *Index) RemoveAll(pathspecs []string, callback IndexMatchedPathCallback) cpathspecs.strings = makeCStringsFromStrings(pathspecs) defer freeStrarray(&cpathspecs) + var err error + data := indexMatchedPathCallbackData{ + callback: callback, + errorTarget: &err, + } runtime.LockOSThread() defer runtime.UnlockOSThread() var handle unsafe.Pointer if callback != nil { - handle = pointerHandles.Track(callback) + handle = pointerHandles.Track(&data) defer pointerHandles.Untrack(handle) } @@ -297,19 +325,30 @@ func (v *Index) RemoveAll(pathspecs []string, callback IndexMatchedPathCallback) handle, ) runtime.KeepAlive(v) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } + return nil } //export indexMatchedPathCallback -func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Pointer) int { - if callback, ok := pointerHandles.Get(payload).(IndexMatchedPathCallback); ok { - return callback(C.GoString(cPath), C.GoString(cMatchedPathspec)) - } else { +func indexMatchedPathCallback(cPath, cMatchedPathspec *C.char, payload unsafe.Pointer) C.int { + data, ok := pointerHandles.Get(payload).(*indexMatchedPathCallbackData) + if !ok { panic("invalid matched path callback") } + + ret := data.callback(C.GoString(cPath), C.GoString(cMatchedPathspec)) + if ret < 0 { + *data.errorTarget = errors.New(ErrorCode(ret).String()) + return C.int(ErrorCodeUser) + } + + return C.int(ErrorCodeOK) } func (v *Index) RemoveByPath(path string) error { @@ -435,7 +474,7 @@ func (v *Index) EntryByPath(path string, stage int) (*IndexEntry, error) { centry := C.git_index_get_bypath(v.ptr, cpath, C.int(stage)) if centry == nil { - return nil, MakeGitError(C.GIT_ENOTFOUND) + return nil, MakeGitError(C.int(ErrorCodeNotFound)) } ret := newIndexEntryFromC(centry) runtime.KeepAlive(v) diff --git a/merge.go b/merge.go index 064ca59dc..19bfd8760 100644 --- a/merge.go +++ b/merge.go @@ -186,6 +186,9 @@ func (mo *MergeOptions) toC() *C.git_merge_options { } } +func freeMergeOptions(opts *C.git_merge_options) { +} + type MergeFileFavor int const ( @@ -199,8 +202,10 @@ func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOpt runtime.LockOSThread() defer runtime.UnlockOSThread() + var err error cMergeOpts := mergeOptions.toC() - cCheckoutOptions := checkoutOptions.toC() + defer freeMergeOptions(cMergeOpts) + cCheckoutOptions := checkoutOptions.toC(&err) defer freeCheckoutOptions(cCheckoutOptions) gmerge_head_array := make([]*C.git_annotated_commit, len(theirHeads)) @@ -208,10 +213,13 @@ func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOpt gmerge_head_array[i] = theirHeads[i].ptr } ptr := unsafe.Pointer(&gmerge_head_array[0]) - err := C.git_merge(r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)), cMergeOpts, cCheckoutOptions) + ret := C.git_merge(r.ptr, (**C.git_annotated_commit)(ptr), C.size_t(len(theirHeads)), cMergeOpts, cCheckoutOptions) runtime.KeepAlive(theirHeads) - if err < 0 { - return MakeGitError(err) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } + if ret < 0 { + return MakeGitError(ret) } return nil } @@ -262,6 +270,7 @@ func (r *Repository) MergeCommits(ours *Commit, theirs *Commit, options *MergeOp defer runtime.UnlockOSThread() copts := options.toC() + defer freeMergeOptions(copts) var ptr *C.git_index ret := C.git_merge_commits(&ptr, r.ptr, ours.cast_ptr, theirs.cast_ptr, copts) @@ -279,6 +288,7 @@ func (r *Repository) MergeTrees(ancestor *Tree, ours *Tree, theirs *Tree, option defer runtime.UnlockOSThread() copts := options.toC() + defer freeMergeOptions(copts) var ancestor_ptr *C.git_tree if ancestor != nil { @@ -446,6 +456,9 @@ func populateCMergeFileOptions(c *C.git_merge_file_options, options MergeFileOpt } func freeCMergeFileOptions(c *C.git_merge_file_options) { + if c == nil { + return + } C.free(unsafe.Pointer(c.ancestor_label)) C.free(unsafe.Pointer(c.our_label)) C.free(unsafe.Pointer(c.their_label)) diff --git a/odb.go b/odb.go index 1f215ec64..415ff1c8d 100644 --- a/odb.go +++ b/odb.go @@ -175,35 +175,33 @@ func (v *Odb) Read(oid *Oid) (obj *OdbObject, err error) { } type OdbForEachCallback func(id *Oid) error - -type foreachData struct { - callback OdbForEachCallback - err error +type odbForEachCallbackData struct { + callback OdbForEachCallback + errorTarget *error } -//export odbForEachCb -func odbForEachCb(id *C.git_oid, handle unsafe.Pointer) int { - data, ok := pointerHandles.Get(handle).(*foreachData) - +//export odbForEachCallback +func odbForEachCallback(id *C.git_oid, handle unsafe.Pointer) C.int { + data, ok := pointerHandles.Get(handle).(*odbForEachCallbackData) if !ok { panic("could not retrieve handle") } err := data.callback(newOidFromC(id)) if err != nil { - data.err = err - return C.GIT_EUSER + *data.errorTarget = err + return C.int(ErrorCodeUser) } - return 0 + return C.int(ErrorCodeOK) } func (v *Odb) ForEach(callback OdbForEachCallback) error { - data := foreachData{ - callback: callback, - err: nil, + var err error + data := odbForEachCallbackData{ + callback: callback, + errorTarget: &err, } - runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -212,9 +210,10 @@ func (v *Odb) ForEach(callback OdbForEachCallback) error { ret := C._go_git_odb_foreach(v.ptr, handle) runtime.KeepAlive(v) - if ret == C.GIT_EUSER { - return data.err - } else if ret < 0 { + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } + if ret < 0 { return MakeGitError(ret) } diff --git a/packbuilder.go b/packbuilder.go index 576e5ca81..5d3a93375 100644 --- a/packbuilder.go +++ b/packbuilder.go @@ -133,15 +133,15 @@ func (pb *Packbuilder) Written() uint32 { } type PackbuilderForeachCallback func([]byte) error -type packbuilderCbData struct { - callback PackbuilderForeachCallback - err error +type packbuilderCallbackData struct { + callback PackbuilderForeachCallback + errorTarget *error } -//export packbuilderForEachCb -func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, handle unsafe.Pointer) int { +//export packbuilderForEachCallback +func packbuilderForEachCallback(buf unsafe.Pointer, size C.size_t, handle unsafe.Pointer) C.int { payload := pointerHandles.Get(handle) - data, ok := payload.(*packbuilderCbData) + data, ok := payload.(*packbuilderCallbackData) if !ok { panic("could not get packbuilder CB data") } @@ -150,19 +150,20 @@ func packbuilderForEachCb(buf unsafe.Pointer, size C.size_t, handle unsafe.Point err := data.callback(slice) if err != nil { - data.err = err - return C.GIT_EUSER + *data.errorTarget = err + return C.int(ErrorCodeUser) } - return 0 + return C.int(ErrorCodeOK) } // ForEach repeatedly calls the callback with new packfile data until // there is no more data or the callback returns an error func (pb *Packbuilder) ForEach(callback PackbuilderForeachCallback) error { - data := packbuilderCbData{ - callback: callback, - err: nil, + var err error + data := packbuilderCallbackData{ + callback: callback, + errorTarget: &err, } handle := pointerHandles.Track(&data) defer pointerHandles.Untrack(handle) @@ -170,13 +171,13 @@ func (pb *Packbuilder) ForEach(callback PackbuilderForeachCallback) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C._go_git_packbuilder_foreach(pb.ptr, handle) + ret := C._go_git_packbuilder_foreach(pb.ptr, handle) runtime.KeepAlive(pb) - if err == C.GIT_EUSER { - return data.err + if ret == C.int(ErrorCodeUser) && err != nil { + return err } - if err < 0 { - return MakeGitError(err) + if ret < 0 { + return MakeGitError(ret) } return nil diff --git a/patch.go b/patch.go index 1c4d6334e..f53040754 100644 --- a/patch.go +++ b/patch.go @@ -77,17 +77,22 @@ func (v *Repository) PatchFromBuffers(oldPath, newPath string, oldBuf, newBuf [] cNewPath := C.CString(newPath) defer C.free(unsafe.Pointer(cNewPath)) - copts := diffOptionsToC(opts, v) + var err error + copts := opts.toC(v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C.git_patch_from_buffers(&patchPtr, oldPtr, C.size_t(len(oldBuf)), cOldPath, newPtr, C.size_t(len(newBuf)), cNewPath, copts) + ret := C.git_patch_from_buffers(&patchPtr, oldPtr, C.size_t(len(oldBuf)), cOldPath, newPtr, C.size_t(len(newBuf)), cNewPath, copts) runtime.KeepAlive(oldBuf) runtime.KeepAlive(newBuf) - if ecode < 0 { - return nil, MakeGitError(ecode) + if ret == C.int(ErrorCodeUser) && err != nil { + return nil, err + } + if ret < 0 { + return nil, MakeGitError(ret) } + return newPatchFromC(patchPtr), nil } diff --git a/rebase.go b/rebase.go index 2c13fad11..2d7785088 100644 --- a/rebase.go +++ b/rebase.go @@ -104,7 +104,7 @@ func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions { } } -func (ro *RebaseOptions) toC() *C.git_rebase_options { +func (ro *RebaseOptions) toC(errorTarget *error) *C.git_rebase_options { if ro == nil { return nil } @@ -114,10 +114,19 @@ func (ro *RebaseOptions) toC() *C.git_rebase_options { inmemory: C.int(ro.InMemory), rewrite_notes_ref: mapEmptyStringToNull(ro.RewriteNotesRef), merge_options: *ro.MergeOptions.toC(), - checkout_options: *ro.CheckoutOptions.toC(), + checkout_options: *ro.CheckoutOptions.toC(errorTarget), } } +func freeRebaseOptions(opts *C.git_rebase_options) { + if opts == nil { + return + } + C.free(unsafe.Pointer(opts.rewrite_notes_ref)) + freeMergeOptions(&opts.merge_options) + freeCheckoutOptions(&opts.checkout_options) +} + func mapEmptyStringToNull(ref string) *C.char { if ref == "" { return nil @@ -127,8 +136,9 @@ func mapEmptyStringToNull(ref string) *C.char { // Rebase is the struct representing a Rebase object. type Rebase struct { - ptr *C.git_rebase - r *Repository + ptr *C.git_rebase + r *Repository + options *C.git_rebase_options } // InitRebase initializes a rebase operation to rebase the changes in branch relative to upstream onto another branch. @@ -149,15 +159,22 @@ func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedComm } var ptr *C.git_rebase - err := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, opts.toC()) + var err error + cOpts := opts.toC(&err) + ret := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, cOpts) runtime.KeepAlive(branch) runtime.KeepAlive(upstream) runtime.KeepAlive(onto) - if err < 0 { - return nil, MakeGitError(err) + if ret == C.int(ErrorCodeUser) && err != nil { + freeRebaseOptions(cOpts) + return nil, err + } + if ret < 0 { + freeRebaseOptions(cOpts) + return nil, MakeGitError(ret) } - return newRebaseFromC(ptr), nil + return newRebaseFromC(ptr, cOpts), nil } // OpenRebase opens an existing rebase that was previously started by either an invocation of InitRebase or by another client. @@ -166,13 +183,20 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) { defer runtime.UnlockOSThread() var ptr *C.git_rebase - err := C.git_rebase_open(&ptr, r.ptr, opts.toC()) + var err error + cOpts := opts.toC(&err) + ret := C.git_rebase_open(&ptr, r.ptr, cOpts) runtime.KeepAlive(r) - if err < 0 { - return nil, MakeGitError(err) + if ret == C.int(ErrorCodeUser) && err != nil { + freeRebaseOptions(cOpts) + return nil, err + } + if ret < 0 { + freeRebaseOptions(cOpts) + return nil, MakeGitError(ret) } - return newRebaseFromC(ptr), nil + return newRebaseFromC(ptr, cOpts), nil } // OperationAt gets the rebase operation specified by the given index. @@ -283,13 +307,14 @@ func (rebase *Rebase) Abort() error { } // Free frees the Rebase object. -func (rebase *Rebase) Free() { - runtime.SetFinalizer(rebase, nil) - C.git_rebase_free(rebase.ptr) +func (r *Rebase) Free() { + runtime.SetFinalizer(r, nil) + C.git_rebase_free(r.ptr) + freeRebaseOptions(r.options) } -func newRebaseFromC(ptr *C.git_rebase) *Rebase { - rebase := &Rebase{ptr: ptr} +func newRebaseFromC(ptr *C.git_rebase, opts *C.git_rebase_options) *Rebase { + rebase := &Rebase{ptr: ptr, options: opts} runtime.SetFinalizer(rebase, (*Rebase).Free) return rebase } diff --git a/remote.go b/remote.go index a4b23b5ce..1e6a0f9ac 100644 --- a/remote.go +++ b/remote.go @@ -5,12 +5,13 @@ package git #include -extern void _go_git_setup_callbacks(git_remote_callbacks *callbacks); +extern void _go_git_populate_remote_callbacks(git_remote_callbacks *callbacks); */ import "C" import ( "crypto/x509" + "errors" "reflect" "runtime" "strings" @@ -217,38 +218,56 @@ func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallb if callbacks == nil { return } - C._go_git_setup_callbacks(ptr) + C._go_git_populate_remote_callbacks(ptr) ptr.payload = pointerHandles.Track(callbacks) } //export sidebandProgressCallback -func sidebandProgressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int { +func sidebandProgressCallback(errorMessage **C.char, _str *C.char, _len C.int, data unsafe.Pointer) C.int { callbacks := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.SidebandProgressCallback == nil { - return 0 + return C.int(ErrorCodeOK) } str := C.GoStringN(_str, _len) - return int(callbacks.SidebandProgressCallback(str)) + ret := callbacks.SidebandProgressCallback(str) + if ret < 0 { + return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + } + return C.int(ErrorCodeOK) } //export completionCallback -func completionCallback(completion_type C.git_remote_completion_type, data unsafe.Pointer) int { +func completionCallback(errorMessage **C.char, completion_type C.git_remote_completion_type, data unsafe.Pointer) C.int { callbacks := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.CompletionCallback == nil { - return 0 + return C.int(ErrorCodeOK) + } + ret := callbacks.CompletionCallback(RemoteCompletion(completion_type)) + if ret < 0 { + return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) } - return int(callbacks.CompletionCallback(RemoteCompletion(completion_type))) + return C.int(ErrorCodeOK) } //export credentialsCallback -func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C.char, allowed_types uint, data unsafe.Pointer) int { +func credentialsCallback( + errorMessage **C.char, + _cred **C.git_cred, + _url *C.char, + _username_from_url *C.char, + allowed_types uint, + data unsafe.Pointer, +) C.int { callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.CredentialsCallback == nil { - return C.GIT_PASSTHROUGH + return C.int(ErrorCodePassthrough) } url := C.GoString(_url) username_from_url := C.GoString(_username_from_url) ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types)) + if ret < 0 { + return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + } if cred != nil { *_cred = cred.ptr @@ -256,41 +275,61 @@ func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C cred.ptr = nil runtime.SetFinalizer(cred, nil) } - return int(ret) + return C.int(ErrorCodeOK) } //export transferProgressCallback -func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int { +func transferProgressCallback(errorMessage **C.char, stats *C.git_transfer_progress, data unsafe.Pointer) C.int { callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.TransferProgressCallback == nil { - return 0 + return C.int(ErrorCodeOK) } - return int(callbacks.TransferProgressCallback(newTransferProgressFromC(stats))) + ret := callbacks.TransferProgressCallback(newTransferProgressFromC(stats)) + if ret < 0 { + return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + } + return C.int(ErrorCodeOK) } //export updateTipsCallback -func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data unsafe.Pointer) int { +func updateTipsCallback( + errorMessage **C.char, + _refname *C.char, + _a *C.git_oid, + _b *C.git_oid, + data unsafe.Pointer, +) C.int { callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.UpdateTipsCallback == nil { - return 0 + return C.int(ErrorCodeOK) } refname := C.GoString(_refname) a := newOidFromC(_a) b := newOidFromC(_b) - return int(callbacks.UpdateTipsCallback(refname, a, b)) + ret := callbacks.UpdateTipsCallback(refname, a, b) + if ret < 0 { + return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + } + return C.int(ErrorCodeOK) } //export certificateCheckCallback -func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, data unsafe.Pointer) int { +func certificateCheckCallback( + errorMessage **C.char, + _cert *C.git_cert, + _valid C.int, + _host *C.char, + data unsafe.Pointer, +) C.int { callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) // if there's no callback set, we need to make sure we fail if the library didn't consider this cert valid if callbacks.CertificateCheckCallback == nil { - if _valid == 1 { - return 0 - } else { - return C.GIT_ECERTIFICATE + if _valid == 0 { + return C.int(ErrorCodeCertificate) } + return C.int(ErrorCodeOK) } + host := C.GoString(_host) valid := _valid != 0 @@ -300,7 +339,10 @@ func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, da ccert := (*C.git_cert_x509)(unsafe.Pointer(_cert)) x509_certs, err := x509.ParseCertificates(C.GoBytes(ccert.data, C.int(ccert.len))) if err != nil { - return C.GIT_EUSER + return setCallbackError(errorMessage, err) + } + if len(x509_certs) < 1 { + return setCallbackError(errorMessage, errors.New("empty certificate list")) } // we assume there's only one, which should hold true for any web server we want to talk to @@ -312,45 +354,56 @@ func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, da C.memcpy(unsafe.Pointer(&cert.Hostkey.HashMD5[0]), unsafe.Pointer(&ccert.hash_md5[0]), C.size_t(len(cert.Hostkey.HashMD5))) C.memcpy(unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), unsafe.Pointer(&ccert.hash_sha1[0]), C.size_t(len(cert.Hostkey.HashSHA1))) } else { - cstr := C.CString("Unsupported certificate type") - C.giterr_set_str(C.int(ErrorClassNet), cstr) - C.free(unsafe.Pointer(cstr)) - return -1 // we don't support anything else atm + return setCallbackError(errorMessage, errors.New("unsupported certificate type")) } - return int(callbacks.CertificateCheckCallback(&cert, valid, host)) + ret := callbacks.CertificateCheckCallback(&cert, valid, host) + if ret < 0 { + return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + } + return C.int(ErrorCodeOK) } //export packProgressCallback -func packProgressCallback(stage C.int, current, total C.uint, data unsafe.Pointer) int { +func packProgressCallback(errorMessage **C.char, stage C.int, current, total C.uint, data unsafe.Pointer) C.int { callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.PackProgressCallback == nil { - return 0 + return C.int(ErrorCodeOK) } - return int(callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total))) + ret := callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total)) + if ret < 0 { + return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + } + return C.int(ErrorCodeOK) } //export pushTransferProgressCallback -func pushTransferProgressCallback(current, total C.uint, bytes C.size_t, data unsafe.Pointer) int { +func pushTransferProgressCallback(errorMessage **C.char, current, total C.uint, bytes C.size_t, data unsafe.Pointer) C.int { callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) if callbacks.PushTransferProgressCallback == nil { - return 0 + return C.int(ErrorCodeOK) } - return int(callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes))) + ret := callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes)) + if ret < 0 { + return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + } + return C.int(ErrorCodeOK) } //export pushUpdateReferenceCallback -func pushUpdateReferenceCallback(refname, status *C.char, data unsafe.Pointer) int { +func pushUpdateReferenceCallback(errorMessage **C.char, refname, status *C.char, data unsafe.Pointer) C.int { callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.PushUpdateReferenceCallback == nil { - return 0 + return C.int(ErrorCodeOK) } - return int(callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status))) + ret := callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status)) + if ret < 0 { + return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + } + return C.int(ErrorCodeOK) } func populateProxyOptions(ptr *C.git_proxy_options, opts *ProxyOptions) { @@ -364,6 +417,10 @@ func populateProxyOptions(ptr *C.git_proxy_options, opts *ProxyOptions) { } func freeProxyOptions(ptr *C.git_proxy_options) { + if ptr == nil { + return + } + C.free(unsafe.Pointer(ptr.url)) } diff --git a/remote_test.go b/remote_test.go index 7e1685627..4cc3298dc 100644 --- a/remote_test.go +++ b/remote_test.go @@ -30,7 +30,7 @@ func assertHostname(cert *Certificate, valid bool, hostname string, t *testing.T return ErrorCodeUser } - return 0 + return ErrorCodeOK } func TestCertificateCheck(t *testing.T) { diff --git a/reset.go b/reset.go index b3ecff4a1..155b58bc1 100644 --- a/reset.go +++ b/reset.go @@ -17,8 +17,15 @@ const ( func (r *Repository) ResetToCommit(commit *Commit, resetType ResetType, opts *CheckoutOptions) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_reset(r.ptr, commit.ptr, C.git_reset_t(resetType), opts.toC()) + var err error + cOpts := opts.toC(&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) } diff --git a/revert.go b/revert.go index cabc30386..5c651a29e 100644 --- a/revert.go +++ b/revert.go @@ -15,12 +15,15 @@ type RevertOptions struct { CheckoutOpts CheckoutOptions } -func (opts *RevertOptions) toC() *C.git_revert_options { +func (opts *RevertOptions) toC(errorTarget *error) *C.git_revert_options { + if opts == nil { + return nil + } return &C.git_revert_options{ version: C.GIT_REVERT_OPTIONS_VERSION, mainline: C.uint(opts.Mainline), merge_opts: *opts.MergeOpts.toC(), - checkout_opts: *opts.CheckoutOpts.toC(), + checkout_opts: *opts.CheckoutOpts.toC(errorTarget), } } @@ -33,6 +36,10 @@ func revertOptionsFromC(opts *C.git_revert_options) RevertOptions { } func freeRevertOptions(opts *C.git_revert_options) { + if opts != nil { + return + } + freeMergeOptions(&opts.merge_opts) freeCheckoutOptions(&opts.checkout_opts) } @@ -57,19 +64,19 @@ func (r *Repository) Revert(commit *Commit, revertOptions *RevertOptions) error runtime.LockOSThread() defer runtime.UnlockOSThread() - var cOpts *C.git_revert_options - - if revertOptions != nil { - cOpts = revertOptions.toC() - defer freeRevertOptions(cOpts) - } + var err error + cOpts := revertOptions.toC(&err) + defer freeRevertOptions(cOpts) - ecode := C.git_revert(r.ptr, commit.cast_ptr, cOpts) + ret := C.git_revert(r.ptr, commit.cast_ptr, cOpts) runtime.KeepAlive(r) runtime.KeepAlive(commit) - if ecode < 0 { - return MakeGitError(ecode) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } + if ret < 0 { + return MakeGitError(ret) } return nil @@ -81,11 +88,8 @@ func (r *Repository) RevertCommit(revertCommit *Commit, ourCommit *Commit, mainl runtime.LockOSThread() defer runtime.UnlockOSThread() - var cOpts *C.git_merge_options - - if mergeOptions != nil { - cOpts = mergeOptions.toC() - } + cOpts := mergeOptions.toC() + defer freeMergeOptions(cOpts) var index *C.git_index diff --git a/stash.go b/stash.go index 2b3ea8ce0..3c79eb3f1 100644 --- a/stash.go +++ b/stash.go @@ -3,7 +3,7 @@ package git /* #include -extern void _go_git_setup_stash_apply_progress_callbacks(git_stash_apply_options *opts); +extern void _go_git_populate_stash_apply_callbacks(git_stash_apply_options *opts); extern int _go_git_stash_foreach(git_repository *repo, void *payload); */ import "C" @@ -113,28 +113,28 @@ const ( // StashApplyProgressCallback is the apply operation notification callback. type StashApplyProgressCallback func(progress StashApplyProgress) error - -type stashApplyProgressData struct { - Callback StashApplyProgressCallback - Error error +type stashApplyProgressCallbackData struct { + callback StashApplyProgressCallback + errorTarget *error } -//export stashApplyProgressCb -func stashApplyProgressCb(progress C.git_stash_apply_progress_t, handle unsafe.Pointer) int { +//export stashApplyProgressCallback +func stashApplyProgressCallback(progress C.git_stash_apply_progress_t, handle unsafe.Pointer) C.int { payload := pointerHandles.Get(handle) - data, ok := payload.(*stashApplyProgressData) + data, ok := payload.(*stashApplyProgressCallbackData) if !ok { panic("could not retrieve data for handle") } + if data == nil || data.callback == nil { + return C.int(ErrorCodeOK) + } - if data != nil { - err := data.Callback(StashApplyProgress(progress)) - if err != nil { - data.Error = err - return C.GIT_EUSER - } + err := data.callback(StashApplyProgress(progress)) + if err != nil { + *data.errorTarget = err + return C.int(ErrorCodeUser) } - return 0 + return C.int(ErrorCodeOK) } // StashApplyOptions represents options to control the apply operation. @@ -161,38 +161,34 @@ func DefaultStashApplyOptions() (StashApplyOptions, error) { }, nil } -func (opts *StashApplyOptions) toC() ( - optsC *C.git_stash_apply_options, progressData *stashApplyProgressData) { - - if opts != nil { - progressData = &stashApplyProgressData{ - Callback: opts.ProgressCallback, - } - - optsC = &C.git_stash_apply_options{ - version: C.GIT_STASH_APPLY_OPTIONS_VERSION, - flags: C.git_stash_apply_flags(opts.Flags), - } - populateCheckoutOptions(&optsC.checkout_options, &opts.CheckoutOptions) - if opts.ProgressCallback != nil { - C._go_git_setup_stash_apply_progress_callbacks(optsC) - optsC.progress_payload = pointerHandles.Track(progressData) - } +func (opts *StashApplyOptions) toC(errorTarget *error) *C.git_stash_apply_options { + if opts == nil { + return nil } - return -} - -// should be called after every call to toC() as deferred. -func untrackStashApplyOptionsCallback(optsC *C.git_stash_apply_options) { - if optsC != nil && optsC.progress_payload != nil { - pointerHandles.Untrack(optsC.progress_payload) + optsC := &C.git_stash_apply_options{ + version: C.GIT_STASH_APPLY_OPTIONS_VERSION, + flags: C.git_stash_apply_flags(opts.Flags), + } + populateCheckoutOptions(&optsC.checkout_options, &opts.CheckoutOptions, errorTarget) + if opts.ProgressCallback != nil { + progressData := &stashApplyProgressCallbackData{ + callback: opts.ProgressCallback, + errorTarget: errorTarget, + } + C._go_git_populate_stash_apply_callbacks(optsC) + optsC.progress_payload = pointerHandles.Track(progressData) } + return optsC } func freeStashApplyOptions(optsC *C.git_stash_apply_options) { - if optsC != nil { - freeCheckoutOptions(&optsC.checkout_options) + if optsC == nil { + return } + if optsC.progress_payload != nil { + pointerHandles.Untrack(optsC.progress_payload) + } + freeCheckoutOptions(&optsC.checkout_options) } // Apply applies a single stashed state from the stash list. @@ -220,8 +216,8 @@ func freeStashApplyOptions(optsC *C.git_stash_apply_options) { // // Error codes can be interogated with IsErrorCode(err, ErrorCodeNotFound). func (c *StashCollection) Apply(index int, opts StashApplyOptions) error { - optsC, progressData := opts.toC() - defer untrackStashApplyOptionsCallback(optsC) + var err error + optsC := opts.toC(&err) defer freeStashApplyOptions(optsC) runtime.LockOSThread() @@ -229,8 +225,8 @@ func (c *StashCollection) Apply(index int, opts StashApplyOptions) error { ret := C.git_stash_apply(c.repo.ptr, C.size_t(index), optsC) runtime.KeepAlive(c) - if ret == C.GIT_EUSER { - return progressData.Error + if ret == C.int(ErrorCodeUser) && err != nil { + return err } if ret < 0 { return MakeGitError(ret) @@ -245,26 +241,25 @@ func (c *StashCollection) Apply(index int, opts StashApplyOptions) error { // 'message' is the message used when creating the stash and 'id' // is the commit id of the stash. type StashCallback func(index int, message string, id *Oid) error - type stashCallbackData struct { - Callback StashCallback - Error error + callback StashCallback + errorTarget *error } -//export stashForeachCb -func stashForeachCb(index C.size_t, message *C.char, id *C.git_oid, handle unsafe.Pointer) int { +//export stashForeachCallback +func stashForeachCallback(index C.size_t, message *C.char, id *C.git_oid, handle unsafe.Pointer) C.int { payload := pointerHandles.Get(handle) data, ok := payload.(*stashCallbackData) if !ok { panic("could not retrieve data for handle") } - err := data.Callback(int(index), C.GoString(message), newOidFromC(id)) + err := data.callback(int(index), C.GoString(message), newOidFromC(id)) if err != nil { - data.Error = err - return C.GIT_EUSER + *data.errorTarget = err + return C.int(ErrorCodeUser) } - return 0 + return C.int(ErrorCodeOK) } // Foreach loops over all the stashed states and calls the callback @@ -272,10 +267,11 @@ func stashForeachCb(index C.size_t, message *C.char, id *C.git_oid, handle unsaf // // If callback returns an error, this will stop looping. func (c *StashCollection) Foreach(callback StashCallback) error { + var err error data := stashCallbackData{ - Callback: callback, + callback: callback, + errorTarget: &err, } - handle := pointerHandles.Track(&data) defer pointerHandles.Untrack(handle) @@ -284,8 +280,8 @@ func (c *StashCollection) Foreach(callback StashCallback) error { ret := C._go_git_stash_foreach(c.repo.ptr, handle) runtime.KeepAlive(c) - if ret == C.GIT_EUSER { - return data.Error + if ret == C.int(ErrorCodeUser) && err != nil { + return err } if ret < 0 { return MakeGitError(ret) @@ -323,8 +319,8 @@ func (c *StashCollection) Drop(index int) error { // Returns error code ErrorCodeNotFound if there's no stashed // state for the given index. func (c *StashCollection) Pop(index int, opts StashApplyOptions) error { - optsC, progressData := opts.toC() - defer untrackStashApplyOptionsCallback(optsC) + var err error + optsC := opts.toC(&err) defer freeStashApplyOptions(optsC) runtime.LockOSThread() @@ -332,8 +328,8 @@ func (c *StashCollection) Pop(index int, opts StashApplyOptions) error { ret := C.git_stash_pop(c.repo.ptr, C.size_t(index), optsC) runtime.KeepAlive(c) - if ret == C.GIT_EUSER { - return progressData.Error + if ret == C.int(ErrorCodeUser) && err != nil { + return err } if ret < 0 { return MakeGitError(ret) diff --git a/submodule.go b/submodule.go index ea65bfe98..2b299d17b 100644 --- a/submodule.go +++ b/submodule.go @@ -6,7 +6,9 @@ package git extern int _go_git_visit_submodule(git_repository *repo, void *fct); */ import "C" + import ( + "errors" "runtime" "unsafe" ) @@ -108,30 +110,50 @@ func (c *SubmoduleCollection) Lookup(name string) (*Submodule, error) { // SubmoduleCallback is a function that is called for every submodule found in SubmoduleCollection.Foreach. type SubmoduleCallback func(sub *Submodule, name string) int +type submoduleCallbackData struct { + callback SubmoduleCallback + errorTarget *error +} //export submoduleCallback func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { sub := &Submodule{(*C.git_submodule)(csub), nil} - if callback, ok := pointerHandles.Get(handle).(SubmoduleCallback); ok { - return (C.int)(callback(sub, C.GoString(name))) - } else { + data, ok := pointerHandles.Get(handle).(submoduleCallbackData) + if !ok { panic("invalid submodule visitor callback") } + + ret := data.callback(sub, C.GoString(name)) + if ret < 0 { + *data.errorTarget = errors.New(ErrorCode(ret).String()) + return C.int(ErrorCodeUser) + } + + return C.int(ErrorCodeOK) } -func (c *SubmoduleCollection) Foreach(cbk SubmoduleCallback) error { +func (c *SubmoduleCollection) Foreach(callback SubmoduleCallback) error { + var err error + data := submoduleCallbackData{ + callback: callback, + errorTarget: &err, + } runtime.LockOSThread() defer runtime.UnlockOSThread() - handle := pointerHandles.Track(cbk) + handle := pointerHandles.Track(data) defer pointerHandles.Untrack(handle) ret := C._go_git_visit_submodule(c.repo.ptr, handle) runtime.KeepAlive(c) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } + return nil } @@ -342,17 +364,18 @@ func (sub *Submodule) Open() (*Repository, error) { } func (sub *Submodule) Update(init bool, opts *SubmoduleUpdateOptions) error { - var copts C.git_submodule_update_options - err := populateSubmoduleUpdateOptions(&copts, opts) - if err != nil { - return err - } + var err error + cOpts := populateSubmoduleUpdateOptions(&C.git_submodule_update_options{}, opts, &err) + defer freeSubmoduleUpdateOptions(cOpts) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_submodule_update(sub.ptr, cbool(init), &copts) + ret := C.git_submodule_update(sub.ptr, cbool(init), cOpts) runtime.KeepAlive(sub) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } @@ -360,15 +383,22 @@ func (sub *Submodule) Update(init bool, opts *SubmoduleUpdateOptions) error { return nil } -func populateSubmoduleUpdateOptions(ptr *C.git_submodule_update_options, opts *SubmoduleUpdateOptions) error { +func populateSubmoduleUpdateOptions(ptr *C.git_submodule_update_options, opts *SubmoduleUpdateOptions, errorTarget *error) *C.git_submodule_update_options { C.git_submodule_update_init_options(ptr, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION) if opts == nil { return nil } - populateCheckoutOptions(&ptr.checkout_opts, opts.CheckoutOpts) + populateCheckoutOptions(&ptr.checkout_opts, opts.CheckoutOpts, errorTarget) populateFetchOptions(&ptr.fetch_opts, opts.FetchOptions) - return nil + return ptr +} + +func freeSubmoduleUpdateOptions(ptr *C.git_submodule_update_options) { + if ptr == nil { + return + } + freeCheckoutOptions(&ptr.checkout_opts) } diff --git a/tag.go b/tag.go index 1bea2b7bf..ba33e8256 100644 --- a/tag.go +++ b/tag.go @@ -197,48 +197,48 @@ func (c *TagsCollection) ListWithMatch(pattern string) ([]string, error) { // so repo.LookupTag() will return an error for these tags. Use // repo.References.Lookup() instead. type TagForeachCallback func(name string, id *Oid) error -type tagForeachData struct { - callback TagForeachCallback - err error +type tagForeachCallbackData struct { + callback TagForeachCallback + errorTarget *error } -//export gitTagForeachCb -func gitTagForeachCb(name *C.char, id *C.git_oid, handle unsafe.Pointer) int { +//export tagForeachCallback +func tagForeachCallback(name *C.char, id *C.git_oid, handle unsafe.Pointer) C.int { payload := pointerHandles.Get(handle) - data, ok := payload.(*tagForeachData) + data, ok := payload.(*tagForeachCallbackData) if !ok { panic("could not retrieve tag foreach CB handle") } err := data.callback(C.GoString(name), newOidFromC(id)) if err != nil { - data.err = err - return C.GIT_EUSER + *data.errorTarget = err + return C.int(ErrorCodeUser) } - return 0 + return C.int(ErrorCodeOK) } // Foreach calls the callback for each tag in the repository. func (c *TagsCollection) Foreach(callback TagForeachCallback) error { - data := tagForeachData{ - callback: callback, - err: nil, + var err error + data := tagForeachCallbackData{ + callback: callback, + errorTarget: &err, } - handle := pointerHandles.Track(&data) defer pointerHandles.Untrack(handle) runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C._go_git_tag_foreach(c.repo.ptr, handle) + ret := C._go_git_tag_foreach(c.repo.ptr, handle) runtime.KeepAlive(c) - if err == C.GIT_EUSER { - return data.err + if ret == C.int(ErrorCodeUser) && err != nil { + return err } - if err < 0 { - return MakeGitError(err) + if ret < 0 { + return MakeGitError(ret) } return nil diff --git a/tree.go b/tree.go index cf85f2eac..38c5bdca7 100644 --- a/tree.go +++ b/tree.go @@ -8,6 +8,7 @@ extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr); import "C" import ( + "errors" "runtime" "unsafe" ) @@ -120,33 +121,46 @@ func (t *Tree) EntryCount() uint64 { } type TreeWalkCallback func(string, *TreeEntry) int +type treeWalkCallbackData struct { + callback TreeWalkCallback + errorTarget *error +} //export treeWalkCallback func treeWalkCallback(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int { - root := C.GoString(_root) - - if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok { - return C.int(callback(root, newTreeEntry(entry))) - } else { + data, ok := pointerHandles.Get(ptr).(*treeWalkCallbackData) + if !ok { panic("invalid treewalk callback") } + + ret := data.callback(C.GoString(_root), newTreeEntry(entry)) + if ret < 0 { + *data.errorTarget = errors.New(ErrorCode(ret).String()) + return C.int(ErrorCodeUser) + } + + return C.int(ErrorCodeOK) } func (t *Tree) Walk(callback TreeWalkCallback) error { + var err error + data := treeWalkCallbackData{ + callback: callback, + errorTarget: &err, + } runtime.LockOSThread() defer runtime.UnlockOSThread() - ptr := pointerHandles.Track(callback) - defer pointerHandles.Untrack(ptr) + handle := pointerHandles.Track(&data) + defer pointerHandles.Untrack(handle) - err := C._go_git_treewalk( - t.cast_ptr, - C.GIT_TREEWALK_PRE, - ptr, - ) + ret := C._go_git_treewalk(t.cast_ptr, C.GIT_TREEWALK_PRE, handle) runtime.KeepAlive(t) - if err < 0 { - return MakeGitError(err) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } + if ret < 0 { + return MakeGitError(ret) } return nil diff --git a/wrapper.c b/wrapper.c index 7ca2d5903..7d790817b 100644 --- a/wrapper.c +++ b/wrapper.c @@ -1,24 +1,120 @@ #include "_cgo_export.h" + #include #include #include -typedef int (*gogit_submodule_cbk)(git_submodule *sm, const char *name, void *payload); +// There are two ways in which to declare a callback: +// +// * If there is a guarantee that the callback will always be called within the +// same stack (e.g. by passing the callback directly into a function / into a +// struct that goes into a function), the following pattern is preferred, +// which preserves the error object as-is: +// +// // myfile.go +// type FooCallback func(...) (..., error) +// type FooCallbackData struct { +// callback FooCallback +// errorTarget *error +// } +// +// //export fooCallback +// func fooCallback(..., handle unsafe.Pointer) C.int { +// payload := pointerHandles.Get(handle) +// data := payload.(*fooCallbackData) +// ... +// err := data.callback(...) +// if err != nil { +// *data.errorTarget = err +// return C.int(ErrorCodeUser) +// } +// return C.int(ErrorCodeOK) +// } +// +// func MyFunction(... callback FooCallback) error { +// var err error +// data := fooCallbackData{ +// callback: callback, +// errorTarget: &err, +// } +// handle := pointerHandles.Track(&data) +// defer pointerHandles.Untrack(handle) +// +// runtime.LockOSThread() +// defer runtime.UnlockOSThread() +// +// ret := C._go_git_my_function(..., handle) +// if ret == C.int(ErrorCodeUser) && err != nil { +// return err +// } +// if ret < 0 { +// return MakeGitError(ret) +// } +// return nil +// } +// +// // wrapper.c +// int _go_git_my_function(..., void *payload) +// { +// return git_my_function(..., (git_foo_cb)&fooCallback, payload); +// } +// +// * Otherwise, if the same callback can be invoked from multiple functions or +// from different stacks (e.g. when passing the callback to an object), a +// different pattern should be used instead, which has the downside of losing +// the original error object and converting it to a GitError: +// +// // myfile.go +// type FooCallback func(...) (..., error) +// +// //export fooCallback +// func fooCallback(errorMessage **C.char, ..., handle unsafe.Pointer) C.int { +// callback := pointerHandles.Get(data).(*FooCallback) +// ... +// err := callback(...) +// if err != nil { +// return setCallbackError(errorMessage, err) +// } +// return C.int(ErrorCodeOK) +// } +// +// // wrapper.c +// static int foo_callback(...) +// { +// char *error_message = NULL; +// const int ret = fooCallback(&error_message, ...); +// return set_callback_error(error_message, ret); +// } + +/** + * Sets the thread-local error to the provided string. This needs to happen in + * C because Go might change Goroutines _just_ before returning, which would + * lose the contents of the error message. + */ +static int set_callback_error(char *error_message, int ret) +{ + if (error_message != NULL) { + if (ret < 0) + giterr_set_str(GITERR_CALLBACK, error_message); + free(error_message); + } + return ret; +} -void _go_git_populate_remote_cb(git_clone_options *opts) +void _go_git_populate_clone_callbacks(git_clone_options *opts) { - opts->remote_cb = (git_remote_create_cb)remoteCreateCallback; + opts->remote_cb = (git_remote_create_cb)&remoteCreateCallback; } -void _go_git_populate_checkout_cb(git_checkout_options *opts) +void _go_git_populate_checkout_callbacks(git_checkout_options *opts) { - opts->notify_cb = (git_checkout_notify_cb)checkoutNotifyCallback; - opts->progress_cb = (git_checkout_progress_cb)checkoutProgressCallback; + opts->notify_cb = (git_checkout_notify_cb)&checkoutNotifyCallback; + opts->progress_cb = (git_checkout_progress_cb)&checkoutProgressCallback; } int _go_git_visit_submodule(git_repository *repo, void *fct) { - return git_submodule_foreach(repo, (gogit_submodule_cbk)&submoduleCallback, fct); + return git_submodule_foreach(repo, (git_submodule_cb)&submoduleCallback, fct); } int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr) @@ -28,28 +124,26 @@ int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr) int _go_git_packbuilder_foreach(git_packbuilder *pb, void *payload) { - return git_packbuilder_foreach(pb, (git_packbuilder_foreach_cb)&packbuilderForEachCb, payload); + return git_packbuilder_foreach(pb, (git_packbuilder_foreach_cb)&packbuilderForEachCallback, payload); } int _go_git_odb_foreach(git_odb *db, void *payload) { - return git_odb_foreach(db, (git_odb_foreach_cb)&odbForEachCb, payload); + return git_odb_foreach(db, (git_odb_foreach_cb)&odbForEachCallback, payload); } void _go_git_odb_backend_free(git_odb_backend *backend) { - if (backend->free) - backend->free(backend); - - return; + if (!backend->free) + return; + backend->free(backend); } void _go_git_refdb_backend_free(git_refdb_backend *backend) { - if (backend->free) - backend->free(backend); - - return; + if (!backend->free) + return; + backend->free(backend); } int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLine, void *payload) @@ -58,83 +152,210 @@ int _go_git_diff_foreach(git_diff *diff, int eachFile, int eachHunk, int eachLin git_diff_hunk_cb hcb = NULL; git_diff_line_cb lcb = NULL; - if (eachFile) { - fcb = (git_diff_file_cb)&diffForEachFileCb; - } - - if (eachHunk) { - hcb = (git_diff_hunk_cb)&diffForEachHunkCb; - } - - if (eachLine) { - lcb = (git_diff_line_cb)&diffForEachLineCb; - } + if (eachFile) + fcb = (git_diff_file_cb)&diffForEachFileCallback; + if (eachHunk) + hcb = (git_diff_hunk_cb)&diffForEachHunkCallback; + if (eachLine) + lcb = (git_diff_line_cb)&diffForEachLineCallback; return git_diff_foreach(diff, fcb, NULL, hcb, lcb, payload); } -int _go_git_diff_blobs(git_blob *old, const char *old_path, git_blob *new, const char *new_path, git_diff_options *opts, int eachFile, int eachHunk, int eachLine, void *payload) +int _go_git_diff_blobs( + git_blob *old, + const char *old_path, + git_blob *new, + const char *new_path, + git_diff_options *opts, + int eachFile, + int eachHunk, + int eachLine, + void *payload) { git_diff_file_cb fcb = NULL; git_diff_hunk_cb hcb = NULL; git_diff_line_cb lcb = NULL; - if (eachFile) { - fcb = (git_diff_file_cb)&diffForEachFileCb; - } + if (eachFile) + fcb = (git_diff_file_cb)&diffForEachFileCallback; + if (eachHunk) + hcb = (git_diff_hunk_cb)&diffForEachHunkCallback; + if (eachLine) + lcb = (git_diff_line_cb)&diffForEachLineCallback; - if (eachHunk) { - hcb = (git_diff_hunk_cb)&diffForEachHunkCb; - } + return git_diff_blobs(old, old_path, new, new_path, opts, fcb, NULL, hcb, lcb, payload); +} - if (eachLine) { - lcb = (git_diff_line_cb)&diffForEachLineCb; - } +void _go_git_setup_diff_notify_callbacks(git_diff_options *opts) +{ + opts->notify_cb = (git_diff_notify_cb)&diffNotifyCallback; +} - return git_diff_blobs(old, old_path, new, new_path, opts, fcb, NULL, hcb, lcb, payload); +static int sideband_progress_callback(const char *str, int len, void *payload) +{ + char *error_message = NULL; + const int ret = sidebandProgressCallback(&error_message, (char *)str, len, payload); + return set_callback_error(error_message, ret); } -void _go_git_setup_diff_notify_callbacks(git_diff_options *opts) { - opts->notify_cb = (git_diff_notify_cb)diffNotifyCb; +static int completion_callback(git_remote_completion_type completion_type, void *data) +{ + char *error_message = NULL; + const int ret = completionCallback(&error_message, completion_type, data); + return set_callback_error(error_message, ret); } -void _go_git_setup_callbacks(git_remote_callbacks *callbacks) { - typedef int (*completion_cb)(git_remote_completion_type type, void *data); - typedef int (*update_tips_cb)(const char *refname, const git_oid *a, const git_oid *b, void *data); - typedef int (*push_update_reference_cb)(const char *refname, const char *status, void *data); +static int credentials_callback( + git_cred **cred, + const char *url, + const char *username_from_url, + unsigned int allowed_types, + void *data) +{ + char *error_message = NULL; + const int ret = credentialsCallback( + &error_message, + cred, + (char *)url, + (char *)username_from_url, + allowed_types, + data + ); + return set_callback_error(error_message, ret); +} - callbacks->sideband_progress = (git_transport_message_cb)sidebandProgressCallback; - callbacks->completion = (completion_cb)completionCallback; - callbacks->credentials = (git_cred_acquire_cb)credentialsCallback; - callbacks->transfer_progress = (git_transfer_progress_cb)transferProgressCallback; - callbacks->update_tips = (update_tips_cb)updateTipsCallback; - callbacks->certificate_check = (git_transport_certificate_check_cb) certificateCheckCallback; - callbacks->pack_progress = (git_packbuilder_progress) packProgressCallback; - callbacks->push_transfer_progress = (git_push_transfer_progress) pushTransferProgressCallback; - callbacks->push_update_reference = (push_update_reference_cb) pushUpdateReferenceCallback; +static int transfer_progress_callback(const git_transfer_progress *stats, void *data) +{ + char *error_message = NULL; + const int ret = transferProgressCallback( + &error_message, + (git_transfer_progress *)stats, + data + ); + return set_callback_error(error_message, ret); } -int _go_git_index_add_all(git_index *index, const git_strarray *pathspec, unsigned int flags, void *callback) { - git_index_matched_path_cb cb = callback ? (git_index_matched_path_cb) &indexMatchedPathCallback : NULL; +static int update_tips_callback(const char *refname, const git_oid *a, const git_oid *b, void *data) +{ + char *error_message = NULL; + const int ret = updateTipsCallback( + &error_message, + (char *)refname, + (git_oid *)a, + (git_oid *)b, + data + ); + return set_callback_error(error_message, ret); +} + +static int certificate_check_callback(git_cert *cert, int valid, const char *host, void *data) +{ + char *error_message = NULL; + const int ret = certificateCheckCallback( + &error_message, + cert, + valid, + (char *)host, + data + ); + return set_callback_error(error_message, ret); +} + +static int pack_progress_callback(int stage, unsigned int current, unsigned int total, void *data) +{ + char *error_message = NULL; + const int ret = packProgressCallback( + &error_message, + stage, + current, + total, + data + ); + return set_callback_error(error_message, ret); +} + +static int push_transfer_progress_callback( + unsigned int current, + unsigned int total, + size_t bytes, + void *data) +{ + char *error_message = NULL; + const int ret = pushTransferProgressCallback( + &error_message, + current, + total, + bytes, + data + ); + return set_callback_error(error_message, ret); +} + +static int push_update_reference_callback(const char *refname, const char *status, void *data) +{ + char *error_message = NULL; + const int ret = pushUpdateReferenceCallback( + &error_message, + (char *)refname, + (char *)status, + data + ); + return set_callback_error(error_message, ret); +} + +void _go_git_populate_remote_callbacks(git_remote_callbacks *callbacks) +{ + callbacks->sideband_progress = sideband_progress_callback; + callbacks->completion = completion_callback; + callbacks->credentials = credentials_callback; + callbacks->transfer_progress = transfer_progress_callback; + callbacks->update_tips = update_tips_callback; + callbacks->certificate_check = certificate_check_callback; + callbacks->pack_progress = pack_progress_callback; + callbacks->push_transfer_progress = push_transfer_progress_callback; + callbacks->push_update_reference = push_update_reference_callback; +} + +int _go_git_index_add_all(git_index *index, const git_strarray *pathspec, unsigned int flags, void *callback) +{ + git_index_matched_path_cb cb = callback ? (git_index_matched_path_cb)&indexMatchedPathCallback : NULL; return git_index_add_all(index, pathspec, flags, cb, callback); } -int _go_git_index_update_all(git_index *index, const git_strarray *pathspec, void *callback) { - git_index_matched_path_cb cb = callback ? (git_index_matched_path_cb) &indexMatchedPathCallback : NULL; +int _go_git_index_update_all(git_index *index, const git_strarray *pathspec, void *callback) +{ + git_index_matched_path_cb cb = callback ? (git_index_matched_path_cb)&indexMatchedPathCallback : NULL; return git_index_update_all(index, pathspec, cb, callback); } -int _go_git_index_remove_all(git_index *index, const git_strarray *pathspec, void *callback) { - git_index_matched_path_cb cb = callback ? (git_index_matched_path_cb) &indexMatchedPathCallback : NULL; +int _go_git_index_remove_all(git_index *index, const git_strarray *pathspec, void *callback) +{ + git_index_matched_path_cb cb = callback ? (git_index_matched_path_cb)&indexMatchedPathCallback : NULL; return git_index_remove_all(index, pathspec, cb, callback); } int _go_git_tag_foreach(git_repository *repo, void *payload) { - return git_tag_foreach(repo, (git_tag_foreach_cb)&gitTagForeachCb, payload); + return git_tag_foreach(repo, (git_tag_foreach_cb)&tagForeachCallback, payload); } -int _go_git_merge_file(git_merge_file_result* out, char* ancestorContents, size_t ancestorLen, char* ancestorPath, unsigned int ancestorMode, char* oursContents, size_t oursLen, char* oursPath, unsigned int oursMode, char* theirsContents, size_t theirsLen, char* theirsPath, unsigned int theirsMode, git_merge_file_options* copts) { +int _go_git_merge_file( + git_merge_file_result* out, + char* ancestorContents, + size_t ancestorLen, + char* ancestorPath, + unsigned int ancestorMode, + char* oursContents, + size_t oursLen, + char* oursPath, + unsigned int oursMode, + char* theirsContents, + size_t theirsLen, + char* theirsPath, + unsigned int theirsMode, + git_merge_file_options* copts) +{ git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_input ours = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_input theirs = GIT_MERGE_FILE_INPUT_INIT; @@ -157,12 +378,14 @@ int _go_git_merge_file(git_merge_file_result* out, char* ancestorContents, size_ return git_merge_file(out, &ancestor, &ours, &theirs, copts); } -void _go_git_setup_stash_apply_progress_callbacks(git_stash_apply_options *opts) { - opts->progress_cb = (git_stash_apply_progress_cb)stashApplyProgressCb; +void _go_git_populate_stash_apply_callbacks(git_stash_apply_options *opts) +{ + opts->progress_cb = (git_stash_apply_progress_cb)&stashApplyProgressCallback; } -int _go_git_stash_foreach(git_repository *repo, void *payload) { - return git_stash_foreach(repo, (git_stash_cb)&stashForeachCb, payload); +int _go_git_stash_foreach(git_repository *repo, void *payload) +{ + return git_stash_foreach(repo, (git_stash_cb)&stashForeachCallback, payload); } int _go_git_writestream_write(git_writestream *stream, const char *buffer, size_t len) @@ -182,10 +405,14 @@ void _go_git_writestream_free(git_writestream *stream) int _go_git_odb_write_pack(git_odb_writepack **out, git_odb *db, void *progress_payload) { - return git_odb_write_pack(out, db, (git_transfer_progress_cb)transferProgressCallback, progress_payload); + return git_odb_write_pack(out, db, transfer_progress_callback, progress_payload); } -int _go_git_odb_writepack_append(git_odb_writepack *writepack, const void *data, size_t size, git_transfer_progress *stats) +int _go_git_odb_writepack_append( + git_odb_writepack *writepack, + const void *data, + size_t size, + git_transfer_progress *stats) { return writepack->append(writepack, data, size, stats); } @@ -200,9 +427,12 @@ void _go_git_odb_writepack_free(git_odb_writepack *writepack) writepack->free(writepack); } -int _go_git_indexer_new(git_indexer **out, const char *path, unsigned int mode, git_odb *odb, void *progress_cb_payload) +int _go_git_indexer_new( + git_indexer **out, + const char *path, + unsigned int mode, + git_odb *odb, + void *progress_cb_payload) { - return git_indexer_new(out, path, mode, odb, (git_transfer_progress_cb)transferProgressCallback, progress_cb_payload); + return git_indexer_new(out, path, mode, odb, transfer_progress_callback, progress_cb_payload); } - -/* EOF */ From 57d9083efed802d6b5ab747dbede9715847a6392 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 6 Dec 2020 06:13:38 -0800 Subject: [PATCH 074/103] Build improvements (#707) This change makes the test be verbose and use parallelization if possible (when using gmake to build). (cherry picked from commit 54afccfa0f5a5574525cbba3b4568cbda252a3df) --- .github/workflows/ci.yml | 8 ++++---- Makefile | 8 +++++--- script/build-libgit2.sh | 5 +++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index caa52a453..27ced210a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,7 @@ jobs: - name: Test env: GOPATH: /home/runner/work/git2go - run: make test-static + run: make TEST_ARGS=-test.v test-static build-static: strategy: @@ -64,7 +64,7 @@ jobs: git submodule update --init make build-libgit2-static - name: Test - run: make test-static + run: make TEST_ARGS=-test.v test-static build-dynamic: strategy: @@ -86,7 +86,7 @@ jobs: git submodule update --init make build-libgit2-dynamic - name: Test - run: make test-dynamic + run: make TEST_ARGS=-test.v test-dynamic build-system-dynamic: strategy: @@ -108,7 +108,7 @@ jobs: git submodule update --init sudo ./script/build-libgit2.sh --dynamic --system - name: Test - run: make test + run: make TEST_ARGS=-test.v test build-system-static: strategy: diff --git a/Makefile b/Makefile index 182c53e33..84262f4da 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +TEST_ARGS ?= --count=1 + default: test # System library @@ -5,7 +7,7 @@ default: test # This uses whatever version of libgit2 can be found in the system. test: go run script/check-MakeGitError-thread-lock.go - go test --count=1 ./... + go test $(TEST_ARGS) ./... install: go install ./... @@ -28,7 +30,7 @@ test-dynamic: dynamic-build/install/lib/libgit2.so 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 --count=1 ./... + go test $(TEST_ARGS) ./... install-dynamic: dynamic-build/install/lib/libgit2.so PKG_CONFIG_PATH=dynamic-build/install/lib/pkgconfig \ @@ -47,7 +49,7 @@ static-build/install/lib/libgit2.a: test-static: static-build/install/lib/libgit2.a go run script/check-MakeGitError-thread-lock.go - go test --count=1 --tags "static" ./... + go test --tags "static" $(TEST_ARGS) ./... install-static: static-build/install/lib/libgit2.a go install --tags "static" ./... diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh index c6498366e..36e11e500 100755 --- a/script/build-libgit2.sh +++ b/script/build-libgit2.sh @@ -64,4 +64,9 @@ cmake -DTHREADSAFE=ON \ -DDEPRECATE_HARD=ON \ "${VENDORED_PATH}" && +if which gmake nproc >/dev/null && [ -f Makefile ]; then + # Make the build parallel if gmake is available and cmake used Makefiles. + exec gmake "-j$(nproc --all)" install +fi + exec cmake --build . --target install From 5e09ac76e819743f8b37a0ce2b7e9ad7d7997865 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 6 Dec 2020 11:55:04 -0800 Subject: [PATCH 075/103] Add `NewCredentialSSHKeyFromSigner` (#706) This change adds `NewCredentialSSHKeyFromSigner`, which allows idiomatic use of SSH keys from Go. This also lets us spin off an SSH server in the tests. (cherry picked from commit abf02bc7d79dfb7b0bbcd404ebecb202cff2a18e) --- .github/workflows/ci.yml | 5 + .travis.yml | 1 + credentials.go | 67 ++++- go.mod | 6 + go.sum | 13 + remote_test.go | 279 ++++++++++++++++++ script/build-libgit2.sh | 12 +- testdata/TestGitRepository.git/HEAD | 1 + testdata/TestGitRepository.git/config | 6 + testdata/TestGitRepository.git/description | 1 + testdata/TestGitRepository.git/info/exclude | 6 + testdata/TestGitRepository.git/info/refs | 8 + .../objects/info/commit-graph | Bin 0 -> 2296 bytes .../TestGitRepository.git/objects/info/packs | 2 + ...4e169a0858c13d9ae781a91d76fc33769b8.bitmap | Bin 0 -> 1334 bytes ...ace4e169a0858c13d9ae781a91d76fc33769b8.idx | Bin 0 -> 3032 bytes ...ce4e169a0858c13d9ae781a91d76fc33769b8.pack | Bin 0 -> 6072 bytes testdata/TestGitRepository.git/packed-refs | 9 + .../TestGitRepository.git/refs/heads/master | 1 + wrapper.c | 23 ++ 20 files changed, 433 insertions(+), 7 deletions(-) create mode 100644 go.sum create mode 100644 testdata/TestGitRepository.git/HEAD create mode 100644 testdata/TestGitRepository.git/config create mode 100644 testdata/TestGitRepository.git/description create mode 100644 testdata/TestGitRepository.git/info/exclude create mode 100644 testdata/TestGitRepository.git/info/refs create mode 100644 testdata/TestGitRepository.git/objects/info/commit-graph create mode 100644 testdata/TestGitRepository.git/objects/info/packs create mode 100644 testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.bitmap create mode 100644 testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.idx create mode 100644 testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.pack create mode 100644 testdata/TestGitRepository.git/packed-refs create mode 100644 testdata/TestGitRepository.git/refs/heads/master diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 27ced210a..420682455 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,7 @@ jobs: GOPATH: /home/runner/work/git2go run: | git submodule update --init + sudo apt-get install -y --no-install-recommends libssh2-1-dev make build-libgit2-static go get -tags static -t github.com/${{ github.repository }}/... go build -tags static github.com/${{ github.repository }}/... @@ -62,6 +63,7 @@ jobs: - name: Build run: | git submodule update --init + sudo apt-get install -y --no-install-recommends libssh2-1-dev make build-libgit2-static - name: Test run: make TEST_ARGS=-test.v test-static @@ -84,6 +86,7 @@ jobs: - name: Build run: | git submodule update --init + sudo apt-get install -y --no-install-recommends libssh2-1-dev make build-libgit2-dynamic - name: Test run: make TEST_ARGS=-test.v test-dynamic @@ -106,6 +109,7 @@ jobs: - name: Build libgit2 run: | git submodule update --init + sudo apt-get install -y --no-install-recommends libssh2-1-dev sudo ./script/build-libgit2.sh --dynamic --system - name: Test run: make TEST_ARGS=-test.v test @@ -128,6 +132,7 @@ jobs: - name: Build libgit2 run: | git submodule update --init + sudo apt-get install -y --no-install-recommends libssh2-1-dev sudo ./script/build-libgit2.sh --static --system - name: Test run: go test --count=1 --tags "static,system_libgit2" ./... diff --git a/.travis.yml b/.travis.yml index 1717c8ef0..0b7f4825e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,7 @@ go: - tip install: + - sudo apt-get install -y --no-install-recommends libssh2-1-dev - make build-libgit2-static - go get --tags "static" ./... diff --git a/credentials.go b/credentials.go index 4e42b6e11..2f683cc2a 100644 --- a/credentials.go +++ b/credentials.go @@ -2,9 +2,16 @@ package git /* #include + +void _go_git_populate_credential_ssh_custom(git_cred_ssh_custom *cred); */ import "C" -import "unsafe" +import ( + "crypto/rand" + "unsafe" + + "golang.org/x/crypto/ssh" +) type CredType uint @@ -89,3 +96,61 @@ func NewCredDefault() (int, Cred) { ret := C.git_cred_default_new(&cred.ptr) return int(ret), cred } + +type credentialSSHCustomData struct { + signer ssh.Signer +} + +//export credentialSSHCustomFree +func credentialSSHCustomFree(cred *C.git_cred_ssh_custom) { + if cred == nil { + return + } + + C.free(unsafe.Pointer(cred.username)) + C.free(unsafe.Pointer(cred.publickey)) + pointerHandles.Untrack(cred.payload) + C.free(unsafe.Pointer(cred)) +} + +//export credentialSSHSignCallback +func credentialSSHSignCallback( + errorMessage **C.char, + sig **C.uchar, + sig_len *C.size_t, + data *C.uchar, + data_len C.size_t, + handle unsafe.Pointer, +) C.int { + signer := pointerHandles.Get(handle).(*credentialSSHCustomData).signer + signature, err := signer.Sign(rand.Reader, C.GoBytes(unsafe.Pointer(data), C.int(data_len))) + if err != nil { + return setCallbackError(errorMessage, err) + } + *sig = (*C.uchar)(C.CBytes(signature.Blob)) + *sig_len = C.size_t(len(signature.Blob)) + return C.int(ErrorCodeOK) +} + +// NewCredentialSSHKeyFromSigner creates new SSH credentials using the provided signer. +func NewCredentialSSHKeyFromSigner(username string, signer ssh.Signer) (*Cred, error) { + publicKey := signer.PublicKey().Marshal() + + ccred := (*C.git_cred_ssh_custom)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_cred_ssh_custom{})))) + ccred.parent.credtype = C.GIT_CREDTYPE_SSH_CUSTOM + ccred.username = C.CString(username) + ccred.publickey = (*C.char)(C.CBytes(publicKey)) + ccred.publickey_len = C.size_t(len(publicKey)) + C._go_git_populate_credential_ssh_custom(ccred) + + data := credentialSSHCustomData{ + signer: signer, + } + ccred.payload = pointerHandles.Track(&data) + + cred := Cred{ + ptr: &ccred.parent, + } + + return &cred, nil +} diff --git a/go.mod b/go.mod index 0228d0290..cd69effd0 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module github.com/libgit2/git2go/v27 go 1.13 + +require ( + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 + golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c + golang.org/x/sys v0.0.0-20201204225414-ed752295db88 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 000000000..35ff116bd --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c h1:9HhBz5L/UjnK9XLtiZhYAdue5BVKep3PMmS2LuPDt8k= +golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201204225414-ed752295db88 h1:KmZPnMocC93w341XZp26yTJg8Za7lhb2KhkYmixoeso= +golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/remote_test.go b/remote_test.go index 4cc3298dc..4f10c0d8d 100644 --- a/remote_test.go +++ b/remote_test.go @@ -1,8 +1,21 @@ package git import ( + "bytes" + "crypto/rand" + "crypto/rsa" "fmt" + "io" + "net" + "os" + "os/exec" + "strings" + "sync" "testing" + "time" + + "github.com/google/shlex" + "golang.org/x/crypto/ssh" ) func TestListRemotes(t *testing.T) { @@ -184,3 +197,269 @@ func TestRemotePrune(t *testing.T) { t.Fatal("Expected error getting a pruned reference") } } + +func newChannelPipe(t *testing.T, w io.Writer, wg *sync.WaitGroup) (*os.File, error) { + pr, pw, err := os.Pipe() + if err != nil { + return nil, err + } + + wg.Add(1) + go func() { + _, err := io.Copy(w, pr) + if err != nil && err != io.EOF { + t.Logf("Failed to copy: %v", err) + } + wg.Done() + }() + + return pw, nil +} + +func startSSHServer(t *testing.T, hostKey ssh.Signer, authorizedKeys []ssh.PublicKey) net.Listener { + t.Helper() + + marshaledAuthorizedKeys := make([][]byte, len(authorizedKeys)) + for i, authorizedKey := range authorizedKeys { + marshaledAuthorizedKeys[i] = authorizedKey.Marshal() + } + + config := &ssh.ServerConfig{ + PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) { + marshaledPubKey := pubKey.Marshal() + for _, marshaledAuthorizedKey := range marshaledAuthorizedKeys { + if bytes.Equal(marshaledPubKey, marshaledAuthorizedKey) { + return &ssh.Permissions{ + // Record the public key used for authentication. + Extensions: map[string]string{ + "pubkey-fp": ssh.FingerprintSHA256(pubKey), + }, + }, nil + } + } + t.Logf("unknown public key for %q:\n\t%+v\n\t%+v\n", c.User(), pubKey.Marshal(), authorizedKeys) + return nil, fmt.Errorf("unknown public key for %q", c.User()) + }, + } + config.AddHostKey(hostKey) + + listener, err := net.Listen("tcp", "localhost:0") + if err != nil { + t.Fatalf("Failed to listen for connection: %v", err) + } + + go func() { + nConn, err := listener.Accept() + if err != nil { + if strings.Contains(err.Error(), "use of closed network connection") { + return + } + t.Logf("Failed to accept incoming connection: %v", err) + return + } + defer nConn.Close() + + conn, chans, reqs, err := ssh.NewServerConn(nConn, config) + if err != nil { + t.Logf("failed to handshake: %+v, %+v", conn, err) + return + } + + // The incoming Request channel must be serviced. + go func() { + for newRequest := range reqs { + t.Logf("new request %v", newRequest) + } + }() + + // Service only the first channel request + newChannel := <-chans + defer func() { + for newChannel := range chans { + t.Logf("new channel %v", newChannel) + newChannel.Reject(ssh.UnknownChannelType, "server closing") + } + }() + + // Channels have a type, depending on the application level + // protocol intended. In the case of a shell, the type is + // "session" and ServerShell may be used to present a simple + // terminal interface. + if newChannel.ChannelType() != "session" { + newChannel.Reject(ssh.UnknownChannelType, "unknown channel type") + return + } + channel, requests, err := newChannel.Accept() + if err != nil { + t.Logf("Could not accept channel: %v", err) + return + } + defer channel.Close() + + // Sessions have out-of-band requests such as "shell", + // "pty-req" and "env". Here we handle only the + // "exec" request. + req := <-requests + if req.Type != "exec" { + req.Reply(false, nil) + return + } + // RFC 4254 Section 6.5. + var payload struct { + Command string + } + if err := ssh.Unmarshal(req.Payload, &payload); err != nil { + t.Logf("invalid payload on channel %v: %v", channel, err) + req.Reply(false, nil) + return + } + args, err := shlex.Split(payload.Command) + if err != nil { + t.Logf("invalid command on channel %v: %v", channel, err) + req.Reply(false, nil) + return + } + if len(args) < 2 || (args[0] != "git-upload-pack" && args[0] != "git-receive-pack") { + t.Logf("invalid command (%v) on channel %v: %v", args, channel, err) + req.Reply(false, nil) + return + } + req.Reply(true, nil) + + go func(in <-chan *ssh.Request) { + for req := range in { + t.Logf("draining request %v", req) + } + }(requests) + + // The first parameter is the (absolute) path of the repository. + args[1] = "./testdata" + args[1] + + cmd := exec.Command(args[0], args[1:]...) + cmd.Stdin = channel + var wg sync.WaitGroup + stdoutPipe, err := newChannelPipe(t, channel, &wg) + if err != nil { + t.Logf("Failed to create stdout pipe: %v", err) + return + } + cmd.Stdout = stdoutPipe + stderrPipe, err := newChannelPipe(t, channel.Stderr(), &wg) + if err != nil { + t.Logf("Failed to create stderr pipe: %v", err) + return + } + cmd.Stderr = stderrPipe + + go func() { + wg.Wait() + channel.CloseWrite() + }() + + err = cmd.Start() + if err != nil { + t.Logf("Failed to start %v: %v", args, err) + return + } + + // Once the process has started, we need to close the write end of the + // pipes from this process so that we can know when the child has done + // writing to it. + stdoutPipe.Close() + stderrPipe.Close() + + timer := time.AfterFunc(5*time.Second, func() { + t.Log("process timed out, terminating") + cmd.Process.Kill() + }) + defer timer.Stop() + + err = cmd.Wait() + if err != nil { + t.Logf("Failed to run %v: %v", args, err) + return + } + }() + return listener +} + +func TestRemoteSSH(t *testing.T) { + t.Parallel() + pubKeyUsername := "testuser" + + hostPrivKey, err := rsa.GenerateKey(rand.Reader, 512) + if err != nil { + t.Fatalf("Failed to generate the host RSA private key: %v", err) + } + hostSigner, err := ssh.NewSignerFromKey(hostPrivKey) + if err != nil { + t.Fatalf("Failed to generate SSH hostSigner: %v", err) + } + + privKey, err := rsa.GenerateKey(rand.Reader, 512) + if err != nil { + t.Fatalf("Failed to generate the user RSA private key: %v", err) + } + signer, err := ssh.NewSignerFromKey(privKey) + if err != nil { + t.Fatalf("Failed to generate SSH signer: %v", err) + } + // This is in the format "xx:xx:xx:...", so we remove the colons so that it + // matches the fmt.Sprintf() below. + // Note that not all libssh2 implementations support the SHA256 fingerprint, + // so we use MD5 here for testing. + publicKeyFingerprint := strings.Replace(ssh.FingerprintLegacyMD5(hostSigner.PublicKey()), ":", "", -1) + + listener := startSSHServer(t, hostSigner, []ssh.PublicKey{signer.PublicKey()}) + defer listener.Close() + + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + certificateCheckCallbackCalled := false + fetchOpts := FetchOptions{ + RemoteCallbacks: RemoteCallbacks{ + CertificateCheckCallback: func(cert *Certificate, valid bool, hostname string) ErrorCode { + hostkeyFingerprint := fmt.Sprintf("%x", cert.Hostkey.HashMD5[:]) + if hostkeyFingerprint != publicKeyFingerprint { + t.Logf("server hostkey %q, want %q", hostkeyFingerprint, publicKeyFingerprint) + return ErrorCodeAuth + } + certificateCheckCallbackCalled = true + return ErrorCodeOK + }, + CredentialsCallback: func(url, username string, allowedTypes CredType) (ErrorCode, *Cred) { + if allowedTypes&(CredTypeSshKey|CredTypeSshCustom) != 0 { + cred, err := NewCredentialSSHKeyFromSigner(pubKeyUsername, signer) + if err != nil { + t.Logf("failed to create credentials: %v", err) + return ErrorCodeAuth, nil + } + return ErrorCodeOK, cred + } + t.Logf("unknown credential type %+v", allowedTypes) + return ErrorCodeAuth, nil + }, + }, + } + + remote, err := repo.Remotes.Create( + "origin", + fmt.Sprintf("ssh://%s@%s/TestGitRepository", pubKeyUsername, listener.Addr().String()), + ) + checkFatal(t, err) + defer remote.Free() + + err = remote.Fetch(nil, &fetchOpts, "") + checkFatal(t, err) + if !certificateCheckCallbackCalled { + t.Fatalf("CertificateCheckCallback was not called") + } + + heads, err := remote.Ls() + checkFatal(t, err) + + if len(heads) == 0 { + t.Error("Expected remote heads") + } +} diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh index 36e11e500..d57c5a5f7 100755 --- a/script/build-libgit2.sh +++ b/script/build-libgit2.sh @@ -62,11 +62,11 @@ cmake -DTHREADSAFE=ON \ -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ -DCMAKE_INSTALL_PREFIX="${BUILD_INSTALL_PREFIX}" \ -DDEPRECATE_HARD=ON \ - "${VENDORED_PATH}" && + "${VENDORED_PATH}" -if which gmake nproc >/dev/null && [ -f Makefile ]; then - # Make the build parallel if gmake is available and cmake used Makefiles. - exec gmake "-j$(nproc --all)" install +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 - -exec cmake --build . --target install diff --git a/testdata/TestGitRepository.git/HEAD b/testdata/TestGitRepository.git/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/testdata/TestGitRepository.git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/testdata/TestGitRepository.git/config b/testdata/TestGitRepository.git/config new file mode 100644 index 000000000..44b9489df --- /dev/null +++ b/testdata/TestGitRepository.git/config @@ -0,0 +1,6 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = true +[remote "origin"] + url = https://github.com/libgit2/TestGitRepository diff --git a/testdata/TestGitRepository.git/description b/testdata/TestGitRepository.git/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/testdata/TestGitRepository.git/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/testdata/TestGitRepository.git/info/exclude b/testdata/TestGitRepository.git/info/exclude new file mode 100644 index 000000000..a5196d1be --- /dev/null +++ b/testdata/TestGitRepository.git/info/exclude @@ -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] +# *~ diff --git a/testdata/TestGitRepository.git/info/refs b/testdata/TestGitRepository.git/info/refs new file mode 100644 index 000000000..e10a56385 --- /dev/null +++ b/testdata/TestGitRepository.git/info/refs @@ -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 diff --git a/testdata/TestGitRepository.git/objects/info/commit-graph b/testdata/TestGitRepository.git/objects/info/commit-graph new file mode 100644 index 0000000000000000000000000000000000000000..013e2f092cc416b5682fd65b574111cb1edf3f18 GIT binary patch literal 2296 zcmZ>E5Aa}QWMT04ba7*V02d(J2f}1=advSGfv{O$xVpHzLf9O4AT)^Nc!FT#WibL} z31KFn8hT)6pgpv}EI@N{VOF3VxtI;8mz@O60aS|*a{}eLfS8*Kmc&Y)^=t*_~c)ud(^yWQkO7?h`sDlTEjOpU$rHUGvE2-JBwCw%t4F^yK-`r|b`f zU+=D9+?XA7zOB0Ep{J4d##*a06WxCHOutYOXr!_G^MQzcZjrmg1*g1qO}gN1I#*3G zKPWGw0_6xwyB?bujlSO^!j_~gm*uSImFG1x#lg*TyP{$q*NgEG5)U}En4mePFbgR}J6 zlZXE5i0{x4v3~Z>e_z9zx;mGG9dWdpKHn8pUq4MeC+UT4nD%t93!pYsVU+?9~R3mxV8|EUh@YaDB&R`KW{| zY%dHigzZ?PYH?LmfJgV!jV!ITi@rWT^X;QV!~UJC9sIgFQodVHO_w)H6T6nJ_1pUX z+1=*^xm|LaS6)~!=h;Q2NrrxxZ$A9SXZddfFkV4rEvQTqa0=a+xo@_sv2_}w^*xzl zk@rXWSKr(`Cl@HkUjQV4K*cF^^WWYqeXS3hcIF%LUen)pb7KA_&)SY?Gsf#);ow0j|E7hqv)8Y2D}_yKe?a9;ycD<_%lpRabp2e!gHzW@Ye{wtERrDgK z5BJ|{R;#ZE%CSMs0fzIYi*0H##v5mAKbO8+-Lo@7`BRB4^9rCEP#pkry9_W4TX(kC zH-9;_i^ulHZ~g3emIbRmwoU=cu|mxO#>FOCg%xX`msxtsE!dk@{WJE(>d#9|P5{*) zh2cisPpeK(&S=O@Wm;gh@)n25m95*bs0LJKLEW<{@~rpyL$%9vvRincGwhQ|SpIBg&{B{*)EuCjw`^?styfW8I=keE zfWiW2`Hx>FW-bQofTo84pcXoeBUlH1|0G literal 0 HcmV?d00001 diff --git a/testdata/TestGitRepository.git/objects/info/packs b/testdata/TestGitRepository.git/objects/info/packs new file mode 100644 index 000000000..d876b387a --- /dev/null +++ b/testdata/TestGitRepository.git/objects/info/packs @@ -0,0 +1,2 @@ +P pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.pack + diff --git a/testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.bitmap b/testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.bitmap new file mode 100644 index 0000000000000000000000000000000000000000..df3442c48506365faca4f279cb15d53ba68897f1 GIT binary patch literal 1334 zcma))J4*vW5P;`Qd=&8kQSgBhQ4u>+NC-p|mBa_y`2(UULaJCvP{A`n5R8S`ofd+H zjaa47PO!1CR_qi6+hApJWSzO)Iiq`m1G6(b`|Zr^V`gDx84PFyfUbwjx7Xa+UOD)D zRp>o>%HKtD*BI|XZBU8^z!2@iaR8!d-8Dl9QG19X761^2kSQ_Z`B5i0E^Gx!6Ms#vVRFO>lCMR_=YOX_LN!0`hDQ`q4 z$ILVsMUCv!N%|o^+a%wNLh(wQY9gF$gV0~Lu;kvv8`kUthpG@<+sZah(b)gaJ)YDAK$4}zE4=F z#CzqjlHX52JzA;C4~}9i+$Yc3(w_P}6z4PS7f)}NEAsq&Zd!l5*w_wMH{s+UlV0?1 K9iEro+r9wqU5lUq literal 0 HcmV?d00001 diff --git a/testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.idx b/testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.idx new file mode 100644 index 0000000000000000000000000000000000000000..aff5c2e53fad98138238cb6c093bfecc501336fe GIT binary patch literal 3032 zcmb`Jc{G&m8^<5}zBSf}i9$@)N#T{GXj0ZA*+QXg!)wW8RMs(M$rfeOkdiej8Wd$I zLQVFiLiQ46O-jGZIdx8_^Xu*HocB5BbMEWDuJ7}G?&q)j`_-!1sp%ieJG5 zGv;5w3jHH}wA>ywLvk@Pp5E|0R{T+2tqCN3xuIB z@(oc~*AV*+;xLy0B>w}Xz+(+GAOpyLLk`w0|9}F_)=>Ng2G*ZZg5IB?40;tn^*5-) zd=2a$unn{tKd~M5YwQ5DeuFm5f8;ONe@6%E@9czH_a}D2UJuy)FVP3x8hieby`cXY z1L*xDjFoEb&Y5NiW38}RvWHJ#e!$l}4%Zy)+x3k0<#z=m3yxi_slJ9T=a_jsdMP|r zzL3~=g^GTW$RI}w;nWHA-FMD|-@9BMRo~Smml&5A%Fj&iE|gtaR58uQHql}X*sR3b z`TGv1o<6#6tdX2z^D_+`WN z0y$r|#wJMbk?`^ql4*IOe%riXrPhWewGPTeom6+{sTKF()s(FdC>R1(pMGFg5`Cj+ z{G=vMl6v2jxNJXBFqeaElnoF~=y4---aF_k;C61KXtDOj02*&4!1r{%LI$nxDVgqjRr+k0yk_=ln&TX)xvE(=sbMwr z)l%rl(Scz9)3#CJH$`j0YgT6BYVU=q%!jKCve;*GHDu=oiY~rP-b4#4rAN3% zD^u&-`1J+pHA%4QVd5J1S>zlxM`P5U)I-G$G19YteZ( zp}p|TnLQ85M^+3KI`Kq#=BJRizwqoSAFl2AP#DE z#$I)8R|f4@5`Ka+4SWT?w<%r^e2QY(Be+9_`$)E1U97b{Dq0mD5;G_1rczTypX$oF zb0SMk?5ooshMi>_60Bu0eBthU;V{`Jr>q>)^yubU!aYMAPtDFS%vc2tx~H6reFC3drSocIIZxo5<(^|&h(WiO1kEPfCTsi`Q~WgJ6x?)5Yd!CGRc zo{kn0**>3SDs$S@+{YK+yFNjgkCmuBAS-KcmlOLDGxC+ox0xfQe<`m|mh-KF#AUK# z*;vh2yA6E2t)@ceceEr?JxI(w)!F>!3>F=&^+N~vw%}clS|()(Q+oKY}rXHOS#lZTaWzFSUpVPX__x_y2ZSAv8VS5rpsGV ztt3@1#Vf&B1Q%q{#3ZwPtgN@|&GUlQ{n5N>_yyUJ{;oFO+HeZGol*Mw7ZS}pXL#fW zOSG7l5kEgY9s}CXTMw;!o`SQad z7BO@|RvZ3>&zmhb##YzgqA+@mQk)zg?6c>U(!2%vhaYkiXC*|YAc%QA5bfZO7BPXS zx`-gc6Ht2rN1%obC;&N=>nubqxL<{%5QGKlwamx|D_aAUw;`HR5Jb=zylf!CLB?So z18>MBJmAR@gCOj1_cKB^V1;}uxDmV)0mxRUXwdHiJ!B&G8JJyy@0r4E7rg%v{5!zo z34C`6`sKhX==(tj{@jq^)&+p}6!>%^2va=#R}!4$=v;CzrT zIig L^{l)X(6IO)otvu# literal 0 HcmV?d00001 diff --git a/testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.pack b/testdata/TestGitRepository.git/objects/pack/pack-ccace4e169a0858c13d9ae781a91d76fc33769b8.pack new file mode 100644 index 0000000000000000000000000000000000000000..a3a120687a9a123eb9cd9504086363461a7064cd GIT binary patch literal 6072 zcmbVQcRZY1w|)mhlpr#?jB*U38!Z@R7+pjQq7%IZ5haP1M4}UYbb>^SPKe%n^xiws zLK33HhunKk?mao*mwW%1-|yY;-tXGaUTZz;S$kBFQi=coV4VGA!b#l{61$MfP22?F zUXNQZ^#xR#9p@QIBpm=32VlaHf^nQODx~St8v8QFMorE1>5=&|OH&<7>58&kw7Pl} zl zYiH-ugt;i!2SUIE6AQIZp>S!SkJIqT{xnwkUYSQ+M6j0VSm@q5ZNm2Sr}0EV@e|O~ zMkD*UD`&?GQnbe8Ax7;BOK(?o^l}7y5=%OHZ_Hzs=x|Jgp?M^+X=fIExn^&ZhCbx4 zWKCZ#4FT)xVi{v^Z)l=}4Ncb3T_55e>!RyJ40ZMQ53=+*o7mDs6xKIY zN0YQ*FSp=TVoUJr>DhnFkcLs>`5*c}*8>F0$&cnR4BZmmx4OkRN^-rLYZ7BqmAVVo z$Gne;$@w-cs4ETPi#kC`aRLPhT;&T_1|qG`GDUh{$k(OVIxM5mOzQe3sM3bW=(>|d ziahp$54097&82b>_+g>CZwe&OHqfx9hrmWLz6vv8NzhP9*YwXDf=^lR+ey%wN*l1h zFwKlZJ)<@=6Nw8`N3dlti5u!Lb?CA3bA=Z;D$8bOPU79MO=_IKnfZ`5rn-^(aYO3@ zV&t2ccyG?>;FqtP#hV}TpAo;I2_(41;x~XC`AG3S)cN85_ob9-dl#RGq}j^gYqp1A z`yAECq|0Zi5IAwUftvt0<&`E&x$$6qm_(8rliNG-28O?wMh3}(y5{V*b7f8rTI+7` zH3rrzScA6oUntU|29Z~~bKCrjbWKaM9!7}{-iQRX!(GE-F}H-SNT;Xu;um;F<&{D>f^IJhbh&}6dvc!?`@`l!Jx;!4T_R8Hdw@NCy)jC+omBJyh zaDl5+7q5YvSY~cN*F?Z5k#Um`bVdJZrFU&R7Wh$W`s{9=?#|{;eO3-DDNZ{5LtG!2 zVk_Z>Pi%dCdBrthsEIK@zGfmTxR&=}B!RXz^Q8Q0I@c2#^tB`6KslR&p_QusD5&#k zGj8|r@oE}vP^WG@U#LaVaHXaGP7>iHZ@hl1R^DN&wSC<3b#-ATo0_9i@u45_RQdq_@okwWABUc$E>~Nl7d_H&}ASxlq`}Bq2t9Ybxq{$b9LY6EZ zYn$8(Yk%~yB+cXd2D{EN_7mO^63X%omVchjTM?zd4r7H=3p;AS|Jm^RXbr! zZS;N6#z3E%5;v*i3$9SdDUru@lr>2Ux0hRwQQKet_;@n0T$z8*+fO4bR9eXfiRbbDAAapt>Q}~NIGo3G22~tO*N>~I zJA51GIB+?b9R#Pa&_mkCKsrwd#4ZiWSgvD7F1~u;alK6NzTQsuA{ZK{TcTON5vJB} zB;**Q(=N5jXN4XAZuqFA7j5r6HBTI0I=1@k7R?HC93oiO#C+{e+#ve8}n@z)%^)=|=Qob3Cq_ zaC`W3XjX+yqKuCdyQSxYVu{QTxNZs8f{^D?1Ge3pAMVmcGTNEoNgqFF9e|P&tv8bvTKL*jwSA>sf`3Ima`uw80x3j! zB-qn@5!@mCL5)_=Xtt{p)g2rYl)o427#GhW?qek~Q)y(2gJV~!xvn;H5>LN7d?IW0 zx@OA;o-E*&peQh}ATW;sfUkhW--6u|czFN}U(|E|YgfPBg(Y}6SK6`}Y#hB~+_O|m z!qw*VpX=xe-esmJ61(U3tpui;a%?xe0dDi| zLJlNkwo?qv21BoV2LPUwWieOu^CWl?lLE!l@p^VbYutp~FFfR4pIp5r=2^POZJo

PUx1cbEwA`^6+BD6*z zcM@JVDS_yOu7@DV7Dm-rSFb#VeNNjQ-@ZR!;W046LFlgiB3OC1ePB1zIN|jxY(UAI zFn1bl6m+=uc~$?r_Rvz_wEkWa(gOsU5a0P%}FJWj*+ z;2A>NgtCBDZG9|jMlP%fsg6i=*fr9x1_Z@l=v7tlHPLa<8}%SeypF|=rcnN2Htk_eEAG_rrt?dpmL(2-@lXr9ya!{#wAEr1aG5g}mNI$et}~q= zE>p?g_yWc+k>LCZmqg!b?ins~ruOt7nuInDc;ca=c2PQAg&Zcbt&ik%dcWjqymP8= z(@>|N!%w-y)WXezUA&X6Ud(ybaR&(=HO`d`r~CZU7x9P^C6sm*URAnMHJ;-!dIV+O zDzkqb=;@|6Js#zb&;{xU{Kh7pD)yk0L-Nl^I5b((rC$2jl-sau1&Nb<-p+;m)Xwep zt`E0frf@Oo#3M=r;*ceRHlT zjImw>n179j^U(|5vnGVMDt)OC(HH+c{fp;7#>PiBZ5#PzMe`dbD87idBRrpJv)xC& zXM{Z(?*J3p947JNytBXi!qgL#$f@3Pk&hu4B@Q^fAD~wkXFGt9KzSj2phgi>%}+{U znX{kl@@Hqbxw-S;ALXD(pxLyoU6JT{+f&n9*Y9v)ae~v*$#ReFsT61HdPu1k>caJb z^m=1IQLJ4tMMpht$lFaJEsr50kD0k=Q{$WK4TcHPxp~?HHe{#bS+u#y9xN zTD;QN1%^MAxuI0V2N;nmzhKHYwEPdIaLb=F#rjXC7@|t}X)5rpR4!h<6t8#XdO0K; z)ze+*T7%->W5|D7*hCNlhY&>5JVU{lnnn>SA+eIhg_>$rUyn*wi)+?ClHovyjbBAP zc1uWxM_M7QfdX9iumKUNmN&^Eq0>vDxAmc&?`@`e-*C;4im|hOrr{&*W1%hb86twKT_%u*QbE`pol+ zOSR>?N_IvwKG6HrbL=R;0+I8)SoL^A#to*`A4-KlO1`d?v;|gmP7umc4I67&d{>K~ zK`dq^G6JiYsOs9DN@g^fPvp3Mze_rzDsZ^LCA!KVK+8*qJWjwn*K;J^(=$EqJJWM= z%UIP{*a}v+W?P^}aI{fsR5#qp16`acHcAK`39!47CpFzlhCs>U?$VmS5 zaC%24)7=LLVT@Sla4BV;da!WpkrO2}a^!=}yTI(>3e@@Y?+GHe+_L$$8_4HE}w&UExWz!q2(yoJ$;bpCMN=xM)=DfbD3@rj_;RMD=z^s}XBZ9cw0- z8Smp-pv!Pqt&5(=%&^+ioko2iHwh;AcPCkjxZ2v-HO|1g2x{F2Sk+^oVl;2>T{wx} zfz8DF`4es_^SV9V`e`r{Nu8Sv0<_SSH9y-CxNvq@t!L@3>+ph?x-(H%UlzAx&Fr3h z&qdG}oac6*9ZlQeLo#zsD{3`%riTaRG=UeM)HZ47!M~9FUpu?EuCA*IMK4(Rj2}z!-Xnd zbq0&-?ZQ!<==#0YP>B~+*wNQaM4Gi)X<_l&df__hT575dy(OO{ z6~{Zh56Vh5NnziMmGENr_%MLk5H3LrFSmr8fcO-?z8-7iBa)lXuBtcuE1a%|!|su^|0#P!!xADG z(EP%9^ZyhG$&D52=kBSLvP!j(?V|7#duOwrz(&A-h-T_{z#8ksxb|dk^_pGgg}SwD zCu&t5OHOkAZtvC6$u#hHwAzAb7=mrPV8PbRYE#Ig1%LPT>9O69^!vJZxv{D@=#Nap z0M|S1o`RL|m1bjrkBQnO_pFDzNE_4VR{GOIIh9f|{9SIdl(nt~YrFW4wLpXdLCsH# zmPdUXg7*y$A)F3MLho5eqw@~h)qx-p{Nyx@zxehek$;KkGEg!|u=E>F^8aOq6{Y(4 zp#A+N?M)oztzR0*OXErnwBU1CabeC+^p|Nx9(;gb*XtR$1UwR8Jo>F&R(8ny-qh&w zeC=m8q3v2VCJ=R$Y585YJn4n~qFnx)O7IWm(sYvY(Vq-ve9k5!*pY11c34HfJP?iY z*)6|Q{Al#@Wr8jxC5Y42Kt;8{&kC#x6mbFumhz7%iD#nAs?(ahqUGnDl4JlU8fvYd zY_%}U@7pL?znX|2QQWw9_LrT}W=Ll`NH;3hR!NqRMb-#x_% z1J!x)&U6Q%TYmrrWHcXt4(z{?gG)e<+2!#+H~xp4$M1&kk7+8hpKQU1l6J+~^&(z- z8E*SxwZ5LsnK6qmu};b^%sd=E2IILh!L!Txf4Ab;cu{ly9n-H>N`+L$h!iJ&eT+7M#^gb5 z#BDx~v{y#$!*;^J5tyRj{{e~ev6X;Z9hFGv(Q4Y#63NV#vgQ~FFzTWHD+y80nNKSM zmG3K8oOmp8@V_0ghu;?|DX03o&wp*qlAk*D=gj%x^j{tjD3HaE&-|-={6~4mcOjDa zhxPD-e%l>yei!I|pLAy@tY#`Pw+Isenu&tXugH9h3Ve$g0M-FW=wdjpz;||I#ySOw z@Se@W&!#|8K+?tY*?Uk7kX&+BANM|}|9~{V`=S_-*T7ymUvX{-6b>Yw8-gz?oPP`k zfkZBb^9u0uoA@P===o4!Q9free(stream); } +static int credential_ssh_sign_callback( + LIBSSH2_SESSION *session, + unsigned char **sig, size_t *sig_len, + const unsigned char *data, size_t data_len, + void **abstract) +{ + char *error_message = NULL; + const int ret = credentialSSHSignCallback( + &error_message, + sig, + sig_len, + (unsigned char *)data, + data_len, + (void *)*(uintptr_t *)abstract); + return set_callback_error(error_message, ret); +} + +void _go_git_populate_credential_ssh_custom(git_cred_ssh_custom *cred) +{ + cred->parent.free = (void (*)(git_cred *))credentialSSHCustomFree; + cred->sign_callback = credential_ssh_sign_callback; +} + int _go_git_odb_write_pack(git_odb_writepack **out, git_odb *db, void *progress_payload) { return git_odb_write_pack(out, db, transfer_progress_callback, progress_payload); From b9770ebde61c21c53797c5f52bd4ab719480020d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 10 Dec 2020 06:52:11 -0800 Subject: [PATCH 076/103] Ensure that no pointer handles leak during the test (#712) (#715) This change makes sure that pointer handles are correctly cleaned up during tests. (cherry picked from commit e28cce87c7551bffa1f4602ff492348f9a8cba60) Co-authored-by: lhchavez --- clone.go | 26 ++++++++++++-------------- clone_test.go | 1 + git_test.go | 20 ++++++++++++++++++++ object.go | 12 ++++++------ push_test.go | 7 +++++-- reference.go | 12 +++++++----- remote.go | 2 ++ remote_test.go | 23 +++++++++++++++++------ repository.go | 11 ++++++++++- 9 files changed, 80 insertions(+), 34 deletions(-) diff --git a/clone.go b/clone.go index 4a3c3d13c..ba208bc6e 100644 --- a/clone.go +++ b/clone.go @@ -55,38 +55,36 @@ func Clone(url string, path string, options *CloneOptions) (*Repository, error) //export remoteCreateCallback func remoteCreateCallback( - cremote unsafe.Pointer, - crepo unsafe.Pointer, + out **C.git_remote, + crepo *C.git_repository, cname, curl *C.char, - payload unsafe.Pointer, + handle unsafe.Pointer, ) C.int { name := C.GoString(cname) url := C.GoString(curl) - repo := newRepositoryFromC((*C.git_repository)(crepo)) - // We don't own this repository, so make sure we don't try to free it - runtime.SetFinalizer(repo, nil) + repo := newRepositoryFromC(crepo) + repo.weak = true + defer repo.Free() - data, ok := pointerHandles.Get(payload).(*cloneCallbackData) + data, ok := pointerHandles.Get(handle).(*cloneCallbackData) if !ok { panic("invalid remote create callback") } remote, ret := data.options.RemoteCreateCallback(repo, name, url) - // clear finalizer as the calling C function will - // free the remote itself - runtime.SetFinalizer(remote, nil) - if ret < 0 { *data.errorTarget = errors.New(ErrorCode(ret).String()) return C.int(ErrorCodeUser) } - if remote == nil { panic("no remote created by callback") } - cptr := (**C.git_remote)(cremote) - *cptr = remote.ptr + *out = remote.ptr + + // clear finalizer as the calling C function will + // free the remote itself + runtime.SetFinalizer(remote, nil) return C.int(ErrorCodeOK) } diff --git a/clone_test.go b/clone_test.go index ded9847db..acfbbcbcf 100644 --- a/clone_test.go +++ b/clone_test.go @@ -74,4 +74,5 @@ func TestCloneWithCallback(t *testing.T) { if err != nil || remote == nil { t.Fatal("Remote was not created properly") } + defer remote.Free() } diff --git a/git_test.go b/git_test.go index 1bd311af3..1c57f7914 100644 --- a/git_test.go +++ b/git_test.go @@ -1,13 +1,33 @@ package git import ( + "fmt" "io/ioutil" "os" "path" + "reflect" "testing" "time" ) +func TestMain(m *testing.M) { + ret := m.Run() + + // 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() + + Shutdown() + + os.Exit(ret) +} + func cleanupTestRepo(t *testing.T, r *Repository) { var err error if r.IsBare() { diff --git a/object.go b/object.go index e7d38908e..66e848fe0 100644 --- a/object.go +++ b/object.go @@ -77,14 +77,14 @@ func (o *Object) Type() ObjectType { return ret } -// Owner returns a weak reference to the repository which owns this -// object. This won't keep the underlying repository alive. +// Owner returns a weak reference to the repository which owns this object. +// This won't keep the underlying repository alive, but it should still be +// Freed. func (o *Object) Owner() *Repository { - ret := &Repository{ - ptr: C.git_object_owner(o.ptr), - } + repo := newRepositoryFromC(C.git_object_owner(o.ptr)) runtime.KeepAlive(o) - return ret + repo.weak = true + return repo } func dupObject(obj *Object, kind ObjectType) (*C.git_object, error) { diff --git a/push_test.go b/push_test.go index f37288208..311b86d14 100644 --- a/push_test.go +++ b/push_test.go @@ -14,15 +14,18 @@ func TestRemotePush(t *testing.T) { remote, err := localRepo.Remotes.Create("test_push", repo.Path()) checkFatal(t, err) + defer remote.Free() seedTestRepo(t, localRepo) err = remote.Push([]string{"refs/heads/master"}, nil) checkFatal(t, err) - _, err = localRepo.References.Lookup("refs/remotes/test_push/master") + ref, err := localRepo.References.Lookup("refs/remotes/test_push/master") checkFatal(t, err) + defer ref.Free() - _, err = repo.References.Lookup("refs/heads/master") + ref, err = repo.References.Lookup("refs/heads/master") checkFatal(t, err) + defer ref.Free() } diff --git a/reference.go b/reference.go index 02022a4f2..d15962ee4 100644 --- a/reference.go +++ b/reference.go @@ -293,12 +293,14 @@ func (v *Reference) Peel(t ObjectType) (*Object, error) { return allocObject(cobj, v.repo), nil } -// Owner returns a weak reference to the repository which owns this -// reference. +// Owner returns a weak reference to the repository which owns this reference. +// This won't keep the underlying repository alive, but it should still be +// Freed. func (v *Reference) Owner() *Repository { - return &Repository{ - ptr: C.git_reference_owner(v.ptr), - } + repo := newRepositoryFromC(C.git_reference_owner(v.ptr)) + runtime.KeepAlive(v) + repo.weak = true + return repo } // Cmp compares v to ref2. It returns 0 on equality, otherwise a diff --git a/remote.go b/remote.go index 1e6a0f9ac..37fbc9abc 100644 --- a/remote.go +++ b/remote.go @@ -432,10 +432,12 @@ func RemoteIsValidName(name string) bool { return C.git_remote_is_valid_name(cname) == 1 } +// Free releases the resources of the Remote. func (r *Remote) Free() { runtime.SetFinalizer(r, nil) C.git_remote_free(r.ptr) r.ptr = nil + r.repo = nil } type RemoteCollection struct { diff --git a/remote_test.go b/remote_test.go index 4f10c0d8d..ccd436a8e 100644 --- a/remote_test.go +++ b/remote_test.go @@ -23,9 +23,9 @@ func TestListRemotes(t *testing.T) { repo := createTestRepo(t) defer cleanupTestRepo(t, repo) - _, err := repo.Remotes.Create("test", "git://foo/bar") - + remote, err := repo.Remotes.Create("test", "git://foo/bar") checkFatal(t, err) + defer remote.Free() expected := []string{ "test", @@ -53,6 +53,7 @@ func TestCertificateCheck(t *testing.T) { remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository") checkFatal(t, err) + defer remote.Free() options := FetchOptions{ RemoteCallbacks: RemoteCallbacks{ @@ -73,6 +74,7 @@ func TestRemoteConnect(t *testing.T) { remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository") checkFatal(t, err) + defer remote.Free() err = remote.ConnectFetch(nil, nil, nil) checkFatal(t, err) @@ -85,6 +87,7 @@ func TestRemoteLs(t *testing.T) { remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository") checkFatal(t, err) + defer remote.Free() err = remote.ConnectFetch(nil, nil, nil) checkFatal(t, err) @@ -104,6 +107,7 @@ func TestRemoteLsFiltering(t *testing.T) { remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository") checkFatal(t, err) + defer remote.Free() err = remote.ConnectFetch(nil, nil, nil) checkFatal(t, err) @@ -136,11 +140,13 @@ func TestRemotePruneRefs(t *testing.T) { err = config.SetBool("remote.origin.prune", true) checkFatal(t, err) - _, err = repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository") + remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository") checkFatal(t, err) + defer remote.Free() - remote, err := repo.Remotes.Lookup("origin") + remote, err = repo.Remotes.Lookup("origin") checkFatal(t, err) + defer remote.Free() if !remote.PruneRefs() { t.Fatal("Expected remote to be configured to prune references") @@ -159,6 +165,7 @@ func TestRemotePrune(t *testing.T) { remoteRef, err := remoteRepo.CreateBranch("test-prune", commit, true) checkFatal(t, err) + defer remoteRef.Free() repo := createTestRepo(t) defer cleanupTestRepo(t, repo) @@ -170,12 +177,14 @@ func TestRemotePrune(t *testing.T) { remoteUrl := fmt.Sprintf("file://%s", remoteRepo.Workdir()) remote, err := repo.Remotes.Create("origin", remoteUrl) checkFatal(t, err) + defer remote.Free() err = remote.Fetch([]string{"test-prune"}, nil, "") checkFatal(t, err) - _, err = repo.References.Create("refs/remotes/origin/test-prune", head, true, "remote reference") + ref, err := repo.References.Create("refs/remotes/origin/test-prune", head, true, "remote reference") checkFatal(t, err) + defer ref.Free() err = remoteRef.Delete() checkFatal(t, err) @@ -185,6 +194,7 @@ func TestRemotePrune(t *testing.T) { rr, err := repo.Remotes.Lookup("origin") checkFatal(t, err) + defer rr.Free() err = rr.ConnectFetch(nil, nil, nil) checkFatal(t, err) @@ -192,8 +202,9 @@ func TestRemotePrune(t *testing.T) { err = rr.Prune(nil) checkFatal(t, err) - _, err = repo.References.Lookup("refs/remotes/origin/test-prune") + ref, err = repo.References.Lookup("refs/remotes/origin/test-prune") if err == nil { + ref.Free() t.Fatal("Expected error getting a pruned reference") } } diff --git a/repository.go b/repository.go index fdd38f6bc..42e74a03d 100644 --- a/repository.go +++ b/repository.go @@ -35,6 +35,10 @@ type Repository struct { // Stashes represents the collection of stashes and can be used to // save, apply and iterate over stash states in this repository. Stashes StashCollection + + // weak indicates that a repository is a weak pointer and should not be + // freed. + weak bool } func newRepositoryFromC(ptr *C.git_repository) *Repository { @@ -136,8 +140,13 @@ func (v *Repository) SetRefdb(refdb *Refdb) { } func (v *Repository) Free() { + ptr := v.ptr + v.ptr = nil runtime.SetFinalizer(v, nil) - C.git_repository_free(v.ptr) + if v.weak { + return + } + C.git_repository_free(ptr) } func (v *Repository) Config() (*Config, error) { From 05f743a30f2acf4e7d5d1e0a4196ec56231ac8e7 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 13 Dec 2020 11:08:38 -0800 Subject: [PATCH 077/103] More callback refactoring (#713) (#719) This change: * Gets rid of the `.toC()` functions for Options objects, since they were redundant with the `populateXxxOptions()`. * Adds support for `errorTarget` to the `RemoteOptions`, since they are used in the same stack for some functions (like `Fetch()`). Now for those cases, the error returned by the callback will be preserved as-is. (cherry picked from commit 10c67474a89c298172a6703b91980ea37c60d5e5) --- checkout.go | 67 +++++------ cherrypick.go | 25 ++-- clone.go | 30 ++--- diff.go | 46 ++++---- indexer.go | 25 ++-- merge.go | 63 +++++----- odb.go | 19 ++- patch.go | 2 +- rebase.go | 40 ++++--- remote.go | 314 ++++++++++++++++++++++++++++++++------------------ reset.go | 2 +- revert.go | 41 ++++--- stash.go | 30 +++-- submodule.go | 18 +-- wrapper.c | 22 ++-- 15 files changed, 414 insertions(+), 330 deletions(-) diff --git a/checkout.go b/checkout.go index 30f606fac..12a02d346 100644 --- a/checkout.go +++ b/checkout.go @@ -88,13 +88,6 @@ func checkoutOptionsFromC(c *C.git_checkout_options) CheckoutOptions { return opts } -func (opts *CheckoutOptions) toC(errorTarget *error) *C.git_checkout_options { - if opts == nil { - return nil - } - return populateCheckoutOptions(&C.git_checkout_options{}, opts, errorTarget) -} - type checkoutCallbackData struct { options *CheckoutOptions errorTarget *error @@ -145,61 +138,61 @@ func checkoutProgressCallback( data.options.ProgressCallback(C.GoString(path), uint(completed_steps), uint(total_steps)) } -// Convert the CheckoutOptions struct to the corresponding -// C-struct. Returns a pointer to ptr, or nil if opts is nil, in order -// to help with what to pass. -func populateCheckoutOptions(ptr *C.git_checkout_options, opts *CheckoutOptions, errorTarget *error) *C.git_checkout_options { +// populateCheckoutOptions populates the provided C-struct with the contents of +// the provided CheckoutOptions struct. Returns copts, or nil if opts is nil, +// in order to help with what to pass. +func populateCheckoutOptions(copts *C.git_checkout_options, opts *CheckoutOptions, errorTarget *error) *C.git_checkout_options { + C.git_checkout_init_options(copts, C.GIT_CHECKOUT_OPTIONS_VERSION) if opts == nil { return nil } - C.git_checkout_init_options(ptr, 1) - ptr.checkout_strategy = C.uint(opts.Strategy) - ptr.disable_filters = cbool(opts.DisableFilters) - ptr.dir_mode = C.uint(opts.DirMode.Perm()) - ptr.file_mode = C.uint(opts.FileMode.Perm()) - ptr.notify_flags = C.uint(opts.NotifyFlags) + copts.checkout_strategy = C.uint(opts.Strategy) + copts.disable_filters = cbool(opts.DisableFilters) + copts.dir_mode = C.uint(opts.DirMode.Perm()) + copts.file_mode = C.uint(opts.FileMode.Perm()) + copts.notify_flags = C.uint(opts.NotifyFlags) if opts.NotifyCallback != nil || opts.ProgressCallback != nil { - C._go_git_populate_checkout_callbacks(ptr) + C._go_git_populate_checkout_callbacks(copts) data := &checkoutCallbackData{ options: opts, errorTarget: errorTarget, } payload := pointerHandles.Track(data) if opts.NotifyCallback != nil { - ptr.notify_payload = payload + copts.notify_payload = payload } if opts.ProgressCallback != nil { - ptr.progress_payload = payload + copts.progress_payload = payload } } if opts.TargetDirectory != "" { - ptr.target_directory = C.CString(opts.TargetDirectory) + copts.target_directory = C.CString(opts.TargetDirectory) } if len(opts.Paths) > 0 { - ptr.paths.strings = makeCStringsFromStrings(opts.Paths) - ptr.paths.count = C.size_t(len(opts.Paths)) + copts.paths.strings = makeCStringsFromStrings(opts.Paths) + copts.paths.count = C.size_t(len(opts.Paths)) } if opts.Baseline != nil { - ptr.baseline = opts.Baseline.cast_ptr + copts.baseline = opts.Baseline.cast_ptr } - return ptr + return copts } -func freeCheckoutOptions(ptr *C.git_checkout_options) { - if ptr == nil { +func freeCheckoutOptions(copts *C.git_checkout_options) { + if copts == nil { return } - C.free(unsafe.Pointer(ptr.target_directory)) - if ptr.paths.count > 0 { - freeStrarray(&ptr.paths) + C.free(unsafe.Pointer(copts.target_directory)) + if copts.paths.count > 0 { + freeStrarray(&copts.paths) } - if ptr.notify_payload != nil { - pointerHandles.Untrack(ptr.notify_payload) - } else if ptr.progress_payload != nil { - pointerHandles.Untrack(ptr.progress_payload) + if copts.notify_payload != nil { + pointerHandles.Untrack(copts.notify_payload) + } else if copts.progress_payload != nil { + pointerHandles.Untrack(copts.progress_payload) } } @@ -210,7 +203,7 @@ func (v *Repository) CheckoutHead(opts *CheckoutOptions) error { defer runtime.UnlockOSThread() var err error - cOpts := opts.toC(&err) + cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err) defer freeCheckoutOptions(cOpts) ret := C.git_checkout_head(v.ptr, cOpts) @@ -239,7 +232,7 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOptions) error { defer runtime.UnlockOSThread() var err error - cOpts := opts.toC(&err) + cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err) defer freeCheckoutOptions(cOpts) ret := C.git_checkout_index(v.ptr, iptr, cOpts) @@ -259,7 +252,7 @@ func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOptions) error { defer runtime.UnlockOSThread() var err error - cOpts := opts.toC(&err) + cOpts := populateCheckoutOptions(&C.git_checkout_options{}, opts, &err) defer freeCheckoutOptions(cOpts) ret := C.git_checkout_tree(v.ptr, tree.ptr, cOpts) diff --git a/cherrypick.go b/cherrypick.go index 142852cdb..4d7994c6e 100644 --- a/cherrypick.go +++ b/cherrypick.go @@ -25,24 +25,23 @@ func cherrypickOptionsFromC(c *C.git_cherrypick_options) CherrypickOptions { return opts } -func (opts *CherrypickOptions) toC(errorTarget *error) *C.git_cherrypick_options { +func populateCherrypickOptions(copts *C.git_cherrypick_options, opts *CherrypickOptions, errorTarget *error) *C.git_cherrypick_options { + C.git_cherrypick_init_options(copts, C.GIT_CHERRYPICK_OPTIONS_VERSION) if opts == nil { return nil } - c := C.git_cherrypick_options{} - c.version = C.uint(opts.Version) - c.mainline = C.uint(opts.Mainline) - c.merge_opts = *opts.MergeOpts.toC() - c.checkout_opts = *opts.CheckoutOpts.toC(errorTarget) - return &c + copts.mainline = C.uint(opts.Mainline) + populateMergeOptions(&copts.merge_opts, &opts.MergeOpts) + populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOpts, errorTarget) + return copts } -func freeCherrypickOpts(ptr *C.git_cherrypick_options) { - if ptr == nil { +func freeCherrypickOpts(copts *C.git_cherrypick_options) { + if copts == nil { return } - freeMergeOptions(&ptr.merge_opts) - freeCheckoutOptions(&ptr.checkout_opts) + freeMergeOptions(&copts.merge_opts) + freeCheckoutOptions(&copts.checkout_opts) } func DefaultCherrypickOptions() (CherrypickOptions, error) { @@ -64,7 +63,7 @@ func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error { defer runtime.UnlockOSThread() var err error - cOpts := opts.toC(&err) + cOpts := populateCherrypickOptions(&C.git_cherrypick_options{}, &opts, &err) defer freeCherrypickOpts(cOpts) ret := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts) @@ -83,7 +82,7 @@ func (r *Repository) CherrypickCommit(pick, our *Commit, opts CherrypickOptions) runtime.LockOSThread() defer runtime.UnlockOSThread() - cOpts := opts.MergeOpts.toC() + cOpts := populateMergeOptions(&C.git_merge_options{}, &opts.MergeOpts) defer freeMergeOptions(cOpts) var ptr *C.git_index diff --git a/clone.go b/clone.go index ba208bc6e..5c9fa7ef3 100644 --- a/clone.go +++ b/clone.go @@ -94,15 +94,14 @@ type cloneCallbackData struct { errorTarget *error } -func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions, errorTarget *error) *C.git_clone_options { - C.git_clone_init_options(ptr, C.GIT_CLONE_OPTIONS_VERSION) - +func populateCloneOptions(copts *C.git_clone_options, opts *CloneOptions, errorTarget *error) *C.git_clone_options { + C.git_clone_init_options(copts, C.GIT_CLONE_OPTIONS_VERSION) if opts == nil { return nil } - populateCheckoutOptions(&ptr.checkout_opts, opts.CheckoutOpts, errorTarget) - populateFetchOptions(&ptr.fetch_opts, opts.FetchOptions) - ptr.bare = cbool(opts.Bare) + populateCheckoutOptions(&copts.checkout_opts, opts.CheckoutOpts, errorTarget) + populateFetchOptions(&copts.fetch_opts, opts.FetchOptions, errorTarget) + copts.bare = cbool(opts.Bare) if opts.RemoteCreateCallback != nil { data := &cloneCallbackData{ @@ -110,23 +109,24 @@ func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions, errorTar errorTarget: errorTarget, } // Go v1.1 does not allow to assign a C function pointer - C._go_git_populate_clone_callbacks(ptr) - ptr.remote_cb_payload = pointerHandles.Track(data) + C._go_git_populate_clone_callbacks(copts) + copts.remote_cb_payload = pointerHandles.Track(data) } - return ptr + return copts } -func freeCloneOptions(ptr *C.git_clone_options) { - if ptr == nil { +func freeCloneOptions(copts *C.git_clone_options) { + if copts == nil { return } - freeCheckoutOptions(&ptr.checkout_opts) + freeCheckoutOptions(&copts.checkout_opts) + freeFetchOptions(&copts.fetch_opts) - if ptr.remote_cb_payload != nil { - pointerHandles.Untrack(ptr.remote_cb_payload) + if copts.remote_cb_payload != nil { + pointerHandles.Untrack(copts.remote_cb_payload) } - C.free(unsafe.Pointer(ptr.checkout_branch)) + C.free(unsafe.Pointer(copts.checkout_branch)) } diff --git a/diff.go b/diff.go index 1a67d5d85..8b0d7655f 100644 --- a/diff.go +++ b/diff.go @@ -640,29 +640,24 @@ func diffNotifyCallback(_diff_so_far unsafe.Pointer, delta_to_add *C.git_diff_de return C.int(ErrorCodeOK) } -func (opts *DiffOptions) toC(repo *Repository, errorTarget *error) *C.git_diff_options { +func populateDiffOptions(copts *C.git_diff_options, opts *DiffOptions, repo *Repository, errorTarget *error) *C.git_diff_options { + C.git_diff_init_options(copts, C.GIT_DIFF_OPTIONS_VERSION) if opts == nil { return nil } - cpathspec := C.git_strarray{} - if opts.Pathspec != nil { - cpathspec.count = C.size_t(len(opts.Pathspec)) - cpathspec.strings = makeCStringsFromStrings(opts.Pathspec) - } - - copts := &C.git_diff_options{ - version: C.GIT_DIFF_OPTIONS_VERSION, - flags: C.uint32_t(opts.Flags), - ignore_submodules: C.git_submodule_ignore_t(opts.IgnoreSubmodules), - pathspec: cpathspec, - context_lines: C.uint32_t(opts.ContextLines), - interhunk_lines: C.uint32_t(opts.InterhunkLines), - id_abbrev: C.uint16_t(opts.IdAbbrev), - max_size: C.git_off_t(opts.MaxSize), - old_prefix: C.CString(opts.OldPrefix), - new_prefix: C.CString(opts.NewPrefix), + copts.flags = C.uint32_t(opts.Flags) + copts.ignore_submodules = C.git_submodule_ignore_t(opts.IgnoreSubmodules) + if len(opts.Pathspec) > 0 { + copts.pathspec.count = C.size_t(len(opts.Pathspec)) + copts.pathspec.strings = makeCStringsFromStrings(opts.Pathspec) } + copts.context_lines = C.uint32_t(opts.ContextLines) + copts.interhunk_lines = C.uint32_t(opts.InterhunkLines) + copts.id_abbrev = C.uint16_t(opts.IdAbbrev) + copts.max_size = C.git_off_t(opts.MaxSize) + copts.old_prefix = C.CString(opts.OldPrefix) + copts.new_prefix = C.CString(opts.NewPrefix) if opts.NotifyCallback != nil { notifyData := &diffNotifyCallbackData{ @@ -680,8 +675,7 @@ func freeDiffOptions(copts *C.git_diff_options) { if copts == nil { return } - cpathspec := copts.pathspec - freeStrarray(&cpathspec) + freeStrarray(&copts.pathspec) C.free(unsafe.Pointer(copts.old_prefix)) C.free(unsafe.Pointer(copts.new_prefix)) if copts.payload != nil { @@ -702,7 +696,7 @@ func (v *Repository) DiffTreeToTree(oldTree, newTree *Tree, opts *DiffOptions) ( } var err error - copts := opts.toC(v, &err) + copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -729,7 +723,7 @@ func (v *Repository) DiffTreeToWorkdir(oldTree *Tree, opts *DiffOptions) (*Diff, } var err error - copts := opts.toC(v, &err) + copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -761,7 +755,7 @@ func (v *Repository) DiffTreeToIndex(oldTree *Tree, index *Index, opts *DiffOpti } var err error - copts := opts.toC(v, &err) + copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -789,7 +783,7 @@ func (v *Repository) DiffTreeToWorkdirWithIndex(oldTree *Tree, opts *DiffOptions } var err error - copts := opts.toC(v, &err) + copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -816,7 +810,7 @@ func (v *Repository) DiffIndexToWorkdir(index *Index, opts *DiffOptions) (*Diff, } var err error - copts := opts.toC(v, &err) + copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() @@ -872,7 +866,7 @@ func DiffBlobs(oldBlob *Blob, oldAsPath string, newBlob *Blob, newAsPath string, newBlobPath := C.CString(newAsPath) defer C.free(unsafe.Pointer(newBlobPath)) - copts := opts.toC(repo, &err) + copts := populateDiffOptions(&C.git_diff_options{}, opts, repo, &err) defer freeDiffOptions(copts) runtime.LockOSThread() diff --git a/indexer.go b/indexer.go index d1b93722e..601ac9ce5 100644 --- a/indexer.go +++ b/indexer.go @@ -19,34 +19,31 @@ import ( // Indexer can post-process packfiles and create an .idx file for efficient // lookup. type Indexer struct { - ptr *C.git_indexer - stats C.git_transfer_progress - callbacks RemoteCallbacks - callbacksHandle unsafe.Pointer + 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) { - indexer = new(Indexer) - - runtime.LockOSThread() - defer runtime.UnlockOSThread() - var odbPtr *C.git_odb = nil if odb != nil { odbPtr = odb.ptr } - indexer.callbacks.TransferProgressCallback = callback - indexer.callbacksHandle = pointerHandles.Track(&indexer.callbacks) + 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.callbacksHandle) + ret := C._go_git_indexer_new(&indexer.ptr, cstr, 0, odbPtr, indexer.ccallbacks.payload) runtime.KeepAlive(odb) if ret < 0 { - pointerHandles.Untrack(indexer.callbacksHandle) + untrackCallbacksPayload(&indexer.ccallbacks) return nil, MakeGitError(ret) } @@ -93,7 +90,7 @@ func (indexer *Indexer) Commit() (*Oid, error) { // Free frees the indexer and its resources. func (indexer *Indexer) Free() { - pointerHandles.Untrack(indexer.callbacksHandle) + untrackCallbacksPayload(&indexer.ccallbacks) runtime.SetFinalizer(indexer, nil) C.git_indexer_free(indexer.ptr) } diff --git a/merge.go b/merge.go index 19bfd8760..30b39a462 100644 --- a/merge.go +++ b/merge.go @@ -172,21 +172,20 @@ func DefaultMergeOptions() (MergeOptions, error) { return mergeOptionsFromC(&opts), nil } -func (mo *MergeOptions) toC() *C.git_merge_options { - if mo == nil { +func populateMergeOptions(copts *C.git_merge_options, opts *MergeOptions) *C.git_merge_options { + C.git_merge_init_options(copts, C.GIT_MERGE_OPTIONS_VERSION) + if opts == nil { return nil } - return &C.git_merge_options{ - version: C.uint(mo.Version), - flags: C.git_merge_flag_t(mo.TreeFlags), - rename_threshold: C.uint(mo.RenameThreshold), - target_limit: C.uint(mo.TargetLimit), - recursion_limit: C.uint(mo.RecursionLimit), - file_favor: C.git_merge_file_favor_t(mo.FileFavor), - } + copts.flags = C.git_merge_flag_t(opts.TreeFlags) + copts.rename_threshold = C.uint(opts.RenameThreshold) + copts.target_limit = C.uint(opts.TargetLimit) + copts.recursion_limit = C.uint(opts.RecursionLimit) + copts.file_favor = C.git_merge_file_favor_t(opts.FileFavor) + return copts } -func freeMergeOptions(opts *C.git_merge_options) { +func freeMergeOptions(copts *C.git_merge_options) { } type MergeFileFavor int @@ -203,9 +202,9 @@ func (r *Repository) Merge(theirHeads []*AnnotatedCommit, mergeOptions *MergeOpt defer runtime.UnlockOSThread() var err error - cMergeOpts := mergeOptions.toC() + cMergeOpts := populateMergeOptions(&C.git_merge_options{}, mergeOptions) defer freeMergeOptions(cMergeOpts) - cCheckoutOptions := checkoutOptions.toC(&err) + cCheckoutOptions := populateCheckoutOptions(&C.git_checkout_options{}, checkoutOptions, &err) defer freeCheckoutOptions(cCheckoutOptions) gmerge_head_array := make([]*C.git_annotated_commit, len(theirHeads)) @@ -269,7 +268,7 @@ func (r *Repository) MergeCommits(ours *Commit, theirs *Commit, options *MergeOp runtime.LockOSThread() defer runtime.UnlockOSThread() - copts := options.toC() + copts := populateMergeOptions(&C.git_merge_options{}, options) defer freeMergeOptions(copts) var ptr *C.git_index @@ -287,7 +286,7 @@ func (r *Repository) MergeTrees(ancestor *Tree, ours *Tree, theirs *Tree, option runtime.LockOSThread() defer runtime.UnlockOSThread() - copts := options.toC() + copts := populateMergeOptions(&C.git_merge_options{}, options) defer freeMergeOptions(copts) var ancestor_ptr *C.git_tree @@ -446,22 +445,28 @@ func mergeFileOptionsFromC(c C.git_merge_file_options) MergeFileOptions { } } -func populateCMergeFileOptions(c *C.git_merge_file_options, options MergeFileOptions) { - c.ancestor_label = C.CString(options.AncestorLabel) - c.our_label = C.CString(options.OurLabel) - c.their_label = C.CString(options.TheirLabel) - c.favor = C.git_merge_file_favor_t(options.Favor) - c.flags = C.git_merge_file_flag_t(options.Flags) - c.marker_size = C.ushort(options.MarkerSize) +func populateMergeFileOptions(copts *C.git_merge_file_options, opts *MergeFileOptions) *C.git_merge_file_options { + C.git_merge_file_init_options(copts, C.GIT_MERGE_FILE_OPTIONS_VERSION) + if opts == nil { + return nil + } + + copts.ancestor_label = C.CString(opts.AncestorLabel) + copts.our_label = C.CString(opts.OurLabel) + copts.their_label = C.CString(opts.TheirLabel) + copts.favor = C.git_merge_file_favor_t(opts.Favor) + copts.flags = C.git_merge_file_flag_t(opts.Flags) + copts.marker_size = C.ushort(opts.MarkerSize) + return copts } -func freeCMergeFileOptions(c *C.git_merge_file_options) { - if c == nil { +func freeMergeFileOptions(copts *C.git_merge_file_options) { + if copts == nil { return } - C.free(unsafe.Pointer(c.ancestor_label)) - C.free(unsafe.Pointer(c.our_label)) - C.free(unsafe.Pointer(c.their_label)) + 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) { @@ -494,8 +499,8 @@ func MergeFile(ancestor MergeFileInput, ours MergeFileInput, theirs MergeFileInp if ecode < 0 { return nil, MakeGitError(ecode) } - populateCMergeFileOptions(copts, *options) - defer freeCMergeFileOptions(copts) + populateMergeFileOptions(copts, options) + defer freeMergeFileOptions(copts) } runtime.LockOSThread() diff --git a/odb.go b/odb.go index 415ff1c8d..3415fae4a 100644 --- a/odb.go +++ b/odb.go @@ -291,18 +291,16 @@ func (v *Odb) NewWriteStream(size int64, otype ObjectType) (*OdbWriteStream, err // 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) { - writepack := new(OdbWritepack) - runtime.LockOSThread() defer runtime.UnlockOSThread() - writepack.callbacks.TransferProgressCallback = callback - writepack.callbacksHandle = pointerHandles.Track(&writepack.callbacks) + writepack := new(OdbWritepack) + populateRemoteCallbacks(&writepack.ccallbacks, &RemoteCallbacks{TransferProgressCallback: callback}, nil) - ret := C._go_git_odb_write_pack(&writepack.ptr, v.ptr, writepack.callbacksHandle) + ret := C._go_git_odb_write_pack(&writepack.ptr, v.ptr, writepack.ccallbacks.payload) runtime.KeepAlive(v) if ret < 0 { - pointerHandles.Untrack(writepack.callbacksHandle) + untrackCallbacksPayload(&writepack.ccallbacks) return nil, MakeGitError(ret) } @@ -442,10 +440,9 @@ func (stream *OdbWriteStream) Free() { // OdbWritepack is a stream to write a packfile to the ODB. type OdbWritepack struct { - ptr *C.git_odb_writepack - stats C.git_transfer_progress - callbacks RemoteCallbacks - callbacksHandle unsafe.Pointer + ptr *C.git_odb_writepack + stats C.git_transfer_progress + ccallbacks C.git_remote_callbacks } func (writepack *OdbWritepack) Write(data []byte) (int, error) { @@ -479,7 +476,7 @@ func (writepack *OdbWritepack) Commit() error { } func (writepack *OdbWritepack) Free() { - pointerHandles.Untrack(writepack.callbacksHandle) + untrackCallbacksPayload(&writepack.ccallbacks) runtime.SetFinalizer(writepack, nil) C._go_git_odb_writepack_free(writepack.ptr) } diff --git a/patch.go b/patch.go index f53040754..9cb93cb6e 100644 --- a/patch.go +++ b/patch.go @@ -78,7 +78,7 @@ func (v *Repository) PatchFromBuffers(oldPath, newPath string, oldBuf, newBuf [] defer C.free(unsafe.Pointer(cNewPath)) var err error - copts := opts.toC(v, &err) + copts := populateDiffOptions(&C.git_diff_options{}, opts, v, &err) defer freeDiffOptions(copts) runtime.LockOSThread() diff --git a/rebase.go b/rebase.go index 2d7785088..94c83403d 100644 --- a/rebase.go +++ b/rebase.go @@ -79,6 +79,11 @@ type RebaseOptions struct { CheckoutOptions CheckoutOptions } +type rebaseOptionsData struct { + options *RebaseOptions + errorTarget *error +} + // DefaultRebaseOptions returns a RebaseOptions with default values. func DefaultRebaseOptions() (RebaseOptions, error) { opts := C.git_rebase_options{} @@ -104,27 +109,28 @@ func rebaseOptionsFromC(opts *C.git_rebase_options) RebaseOptions { } } -func (ro *RebaseOptions) toC(errorTarget *error) *C.git_rebase_options { - if ro == nil { +func populateRebaseOptions(copts *C.git_rebase_options, opts *RebaseOptions, errorTarget *error) *C.git_rebase_options { + C.git_rebase_init_options(copts, C.GIT_REBASE_OPTIONS_VERSION) + if opts == nil { return nil } - return &C.git_rebase_options{ - version: C.uint(ro.Version), - quiet: C.int(ro.Quiet), - inmemory: C.int(ro.InMemory), - rewrite_notes_ref: mapEmptyStringToNull(ro.RewriteNotesRef), - merge_options: *ro.MergeOptions.toC(), - checkout_options: *ro.CheckoutOptions.toC(errorTarget), - } + + copts.quiet = C.int(opts.Quiet) + copts.inmemory = C.int(opts.InMemory) + copts.rewrite_notes_ref = mapEmptyStringToNull(opts.RewriteNotesRef) + populateMergeOptions(&copts.merge_options, &opts.MergeOptions) + populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget) + + return copts } -func freeRebaseOptions(opts *C.git_rebase_options) { - if opts == nil { +func freeRebaseOptions(copts *C.git_rebase_options) { + if copts == nil { return } - C.free(unsafe.Pointer(opts.rewrite_notes_ref)) - freeMergeOptions(&opts.merge_options) - freeCheckoutOptions(&opts.checkout_options) + C.free(unsafe.Pointer(copts.rewrite_notes_ref)) + freeMergeOptions(&copts.merge_options) + freeCheckoutOptions(&copts.checkout_options) } func mapEmptyStringToNull(ref string) *C.char { @@ -160,7 +166,7 @@ func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedComm var ptr *C.git_rebase var err error - cOpts := opts.toC(&err) + cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err) ret := C.git_rebase_init(&ptr, r.ptr, branch.ptr, upstream.ptr, onto.ptr, cOpts) runtime.KeepAlive(branch) runtime.KeepAlive(upstream) @@ -184,7 +190,7 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) { var ptr *C.git_rebase var err error - cOpts := opts.toC(&err) + cOpts := populateRebaseOptions(&C.git_rebase_options{}, opts, &err) ret := C.git_rebase_open(&ptr, r.ptr, cOpts) runtime.KeepAlive(r) if ret == C.int(ErrorCodeUser) && err != nil { diff --git a/remote.go b/remote.go index 37fbc9abc..122c74db1 100644 --- a/remote.go +++ b/remote.go @@ -6,7 +6,6 @@ package git #include extern void _go_git_populate_remote_callbacks(git_remote_callbacks *callbacks); - */ import "C" import ( @@ -71,6 +70,11 @@ type RemoteCallbacks struct { PushUpdateReferenceCallback } +type remoteCallbacksData struct { + callbacks *RemoteCallbacks + errorTarget *error +} + type FetchPrune uint const ( @@ -85,7 +89,6 @@ const ( type DownloadTags uint const ( - // Use the setting from the configuration. DownloadTagsUnspecified DownloadTags = C.GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED // Ask the server for tags pointing to objects we're already @@ -207,44 +210,58 @@ func newRemoteHeadFromC(ptr *C.git_remote_head) RemoteHead { } } -func untrackCalbacksPayload(callbacks *C.git_remote_callbacks) { - if callbacks != nil && callbacks.payload != nil { - pointerHandles.Untrack(callbacks.payload) +func untrackCallbacksPayload(callbacks *C.git_remote_callbacks) { + if callbacks == nil || callbacks.payload == nil { + return } + pointerHandles.Untrack(callbacks.payload) } -func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) { +func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks, errorTarget *error) *C.git_remote_callbacks { C.git_remote_init_callbacks(ptr, C.GIT_REMOTE_CALLBACKS_VERSION) if callbacks == nil { - return + return ptr } C._go_git_populate_remote_callbacks(ptr) - ptr.payload = pointerHandles.Track(callbacks) + data := &remoteCallbacksData{ + callbacks: callbacks, + errorTarget: errorTarget, + } + ptr.payload = pointerHandles.Track(data) + return ptr } //export sidebandProgressCallback -func sidebandProgressCallback(errorMessage **C.char, _str *C.char, _len C.int, data unsafe.Pointer) C.int { - callbacks := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.SidebandProgressCallback == nil { +func sidebandProgressCallback(errorMessage **C.char, _str *C.char, _len C.int, handle unsafe.Pointer) C.int { + data := pointerHandles.Get(handle).(*remoteCallbacksData) + if data.callbacks.SidebandProgressCallback == nil { return C.int(ErrorCodeOK) } str := C.GoStringN(_str, _len) - ret := callbacks.SidebandProgressCallback(str) + ret := data.callbacks.SidebandProgressCallback(str) if ret < 0 { - return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + err := errors.New(ErrorCode(ret).String()) + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } return C.int(ErrorCodeOK) } //export completionCallback -func completionCallback(errorMessage **C.char, completion_type C.git_remote_completion_type, data unsafe.Pointer) C.int { - callbacks := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.CompletionCallback == nil { +func completionCallback(errorMessage **C.char, completion_type C.git_remote_completion_type, handle unsafe.Pointer) C.int { + data := pointerHandles.Get(handle).(*remoteCallbacksData) + if data.callbacks.CompletionCallback == nil { return C.int(ErrorCodeOK) } - ret := callbacks.CompletionCallback(RemoteCompletion(completion_type)) + ret := data.callbacks.CompletionCallback(RemoteCompletion(completion_type)) if ret < 0 { - return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + err := errors.New(ErrorCode(ret).String()) + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } return C.int(ErrorCodeOK) } @@ -256,17 +273,21 @@ func credentialsCallback( _url *C.char, _username_from_url *C.char, allowed_types uint, - data unsafe.Pointer, + handle unsafe.Pointer, ) C.int { - callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.CredentialsCallback == nil { + data := pointerHandles.Get(handle).(*remoteCallbacksData) + if data.callbacks.CredentialsCallback == nil { return C.int(ErrorCodePassthrough) } url := C.GoString(_url) username_from_url := C.GoString(_username_from_url) - ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types)) + ret, cred := data.callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types)) if ret < 0 { - return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + err := errors.New(ErrorCode(ret).String()) + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } if cred != nil { *_cred = cred.ptr @@ -279,14 +300,18 @@ func credentialsCallback( } //export transferProgressCallback -func transferProgressCallback(errorMessage **C.char, stats *C.git_transfer_progress, data unsafe.Pointer) C.int { - callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.TransferProgressCallback == nil { +func transferProgressCallback(errorMessage **C.char, stats *C.git_transfer_progress, handle unsafe.Pointer) C.int { + data := pointerHandles.Get(handle).(*remoteCallbacksData) + if data.callbacks.TransferProgressCallback == nil { return C.int(ErrorCodeOK) } - ret := callbacks.TransferProgressCallback(newTransferProgressFromC(stats)) + ret := data.callbacks.TransferProgressCallback(newTransferProgressFromC(stats)) if ret < 0 { - return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + err := errors.New(ErrorCode(ret).String()) + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } return C.int(ErrorCodeOK) } @@ -297,18 +322,22 @@ func updateTipsCallback( _refname *C.char, _a *C.git_oid, _b *C.git_oid, - data unsafe.Pointer, + handle unsafe.Pointer, ) C.int { - callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.UpdateTipsCallback == nil { + data := pointerHandles.Get(handle).(*remoteCallbacksData) + if data.callbacks.UpdateTipsCallback == nil { return C.int(ErrorCodeOK) } refname := C.GoString(_refname) a := newOidFromC(_a) b := newOidFromC(_b) - ret := callbacks.UpdateTipsCallback(refname, a, b) + ret := data.callbacks.UpdateTipsCallback(refname, a, b) if ret < 0 { - return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + err := errors.New(ErrorCode(ret).String()) + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } return C.int(ErrorCodeOK) } @@ -319,11 +348,11 @@ func certificateCheckCallback( _cert *C.git_cert, _valid C.int, _host *C.char, - data unsafe.Pointer, + handle unsafe.Pointer, ) C.int { - callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) + data := pointerHandles.Get(handle).(*remoteCallbacksData) // if there's no callback set, we need to make sure we fail if the library didn't consider this cert valid - if callbacks.CertificateCheckCallback == nil { + if data.callbacks.CertificateCheckCallback == nil { if _valid == 0 { return C.int(ErrorCodeCertificate) } @@ -339,10 +368,17 @@ func certificateCheckCallback( ccert := (*C.git_cert_x509)(unsafe.Pointer(_cert)) x509_certs, err := x509.ParseCertificates(C.GoBytes(ccert.data, C.int(ccert.len))) if err != nil { + if data.errorTarget != nil { + *data.errorTarget = err + } return setCallbackError(errorMessage, err) } if len(x509_certs) < 1 { - return setCallbackError(errorMessage, errors.New("empty certificate list")) + err := errors.New("empty certificate list") + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } // we assume there's only one, which should hold true for any web server we want to talk to @@ -354,74 +390,95 @@ func certificateCheckCallback( C.memcpy(unsafe.Pointer(&cert.Hostkey.HashMD5[0]), unsafe.Pointer(&ccert.hash_md5[0]), C.size_t(len(cert.Hostkey.HashMD5))) C.memcpy(unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), unsafe.Pointer(&ccert.hash_sha1[0]), C.size_t(len(cert.Hostkey.HashSHA1))) } else { - return setCallbackError(errorMessage, errors.New("unsupported certificate type")) + err := errors.New("unsupported certificate type") + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } - ret := callbacks.CertificateCheckCallback(&cert, valid, host) + ret := data.callbacks.CertificateCheckCallback(&cert, valid, host) if ret < 0 { - return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + err := errors.New(ErrorCode(ret).String()) + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } return C.int(ErrorCodeOK) } //export packProgressCallback -func packProgressCallback(errorMessage **C.char, stage C.int, current, total C.uint, data unsafe.Pointer) C.int { - callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.PackProgressCallback == nil { +func packProgressCallback(errorMessage **C.char, stage C.int, current, total C.uint, handle unsafe.Pointer) C.int { + data := pointerHandles.Get(handle).(*remoteCallbacksData) + if data.callbacks.PackProgressCallback == nil { return C.int(ErrorCodeOK) } - ret := callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total)) + ret := data.callbacks.PackProgressCallback(int32(stage), uint32(current), uint32(total)) if ret < 0 { - return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + err := errors.New(ErrorCode(ret).String()) + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } return C.int(ErrorCodeOK) } //export pushTransferProgressCallback -func pushTransferProgressCallback(errorMessage **C.char, current, total C.uint, bytes C.size_t, data unsafe.Pointer) C.int { - callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.PushTransferProgressCallback == nil { +func pushTransferProgressCallback(errorMessage **C.char, current, total C.uint, bytes C.size_t, handle unsafe.Pointer) C.int { + data := pointerHandles.Get(handle).(*remoteCallbacksData) + if data.callbacks.PushTransferProgressCallback == nil { return C.int(ErrorCodeOK) } - ret := callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes)) + ret := data.callbacks.PushTransferProgressCallback(uint32(current), uint32(total), uint(bytes)) if ret < 0 { - return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + err := errors.New(ErrorCode(ret).String()) + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } return C.int(ErrorCodeOK) } //export pushUpdateReferenceCallback -func pushUpdateReferenceCallback(errorMessage **C.char, refname, status *C.char, data unsafe.Pointer) C.int { - callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks) - if callbacks.PushUpdateReferenceCallback == nil { +func pushUpdateReferenceCallback(errorMessage **C.char, refname, status *C.char, handle unsafe.Pointer) C.int { + data := pointerHandles.Get(handle).(*remoteCallbacksData) + if data.callbacks.PushUpdateReferenceCallback == nil { return C.int(ErrorCodeOK) } - ret := callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status)) + ret := data.callbacks.PushUpdateReferenceCallback(C.GoString(refname), C.GoString(status)) if ret < 0 { - return setCallbackError(errorMessage, errors.New(ErrorCode(ret).String())) + err := errors.New(ErrorCode(ret).String()) + if data.errorTarget != nil { + *data.errorTarget = err + } + return setCallbackError(errorMessage, err) } return C.int(ErrorCodeOK) } -func populateProxyOptions(ptr *C.git_proxy_options, opts *ProxyOptions) { - C.git_proxy_init_options(ptr, C.GIT_PROXY_OPTIONS_VERSION) +func populateProxyOptions(copts *C.git_proxy_options, opts *ProxyOptions) *C.git_proxy_options { + C.git_proxy_init_options(copts, C.GIT_PROXY_OPTIONS_VERSION) if opts == nil { - return + return nil } - ptr._type = C.git_proxy_t(opts.Type) - ptr.url = C.CString(opts.Url) + copts._type = C.git_proxy_t(opts.Type) + copts.url = C.CString(opts.Url) + return copts } -func freeProxyOptions(ptr *C.git_proxy_options) { - if ptr == nil { +func freeProxyOptions(copts *C.git_proxy_options) { + if copts == nil { return } - C.free(unsafe.Pointer(ptr.url)) + C.free(unsafe.Pointer(copts.url)) } // RemoteIsValidName returns whether the remote name is well-formed. @@ -735,35 +792,54 @@ func (o *Remote) RefspecCount() uint { return uint(count) } -func populateFetchOptions(options *C.git_fetch_options, opts *FetchOptions) { - C.git_fetch_init_options(options, C.GIT_FETCH_OPTIONS_VERSION) +func populateFetchOptions(copts *C.git_fetch_options, opts *FetchOptions, errorTarget *error) *C.git_fetch_options { + C.git_fetch_init_options(copts, C.GIT_FETCH_OPTIONS_VERSION) if opts == nil { - return + return nil } - populateRemoteCallbacks(&options.callbacks, &opts.RemoteCallbacks) - options.prune = C.git_fetch_prune_t(opts.Prune) - options.update_fetchhead = cbool(opts.UpdateFetchhead) - options.download_tags = C.git_remote_autotag_option_t(opts.DownloadTags) - - options.custom_headers = C.git_strarray{} - options.custom_headers.count = C.size_t(len(opts.Headers)) - options.custom_headers.strings = makeCStringsFromStrings(opts.Headers) - populateProxyOptions(&options.proxy_opts, &opts.ProxyOptions) + populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget) + copts.prune = C.git_fetch_prune_t(opts.Prune) + copts.update_fetchhead = cbool(opts.UpdateFetchhead) + copts.download_tags = C.git_remote_autotag_option_t(opts.DownloadTags) + + copts.custom_headers = C.git_strarray{ + count: C.size_t(len(opts.Headers)), + strings: makeCStringsFromStrings(opts.Headers), + } + populateProxyOptions(&copts.proxy_opts, &opts.ProxyOptions) + return copts } -func populatePushOptions(options *C.git_push_options, opts *PushOptions) { - C.git_push_init_options(options, C.GIT_PUSH_OPTIONS_VERSION) - if opts == nil { +func freeFetchOptions(copts *C.git_fetch_options) { + if copts == nil { return } + freeStrarray(&copts.custom_headers) + untrackCallbacksPayload(&copts.callbacks) + freeProxyOptions(&copts.proxy_opts) +} - options.pb_parallelism = C.uint(opts.PbParallelism) +func populatePushOptions(copts *C.git_push_options, opts *PushOptions, errorTarget *error) *C.git_push_options { + C.git_push_init_options(copts, C.GIT_PUSH_OPTIONS_VERSION) + if opts == nil { + return nil + } - options.custom_headers = C.git_strarray{} - options.custom_headers.count = C.size_t(len(opts.Headers)) - options.custom_headers.strings = makeCStringsFromStrings(opts.Headers) + copts.pb_parallelism = C.uint(opts.PbParallelism) + copts.custom_headers = C.git_strarray{ + count: C.size_t(len(opts.Headers)), + strings: makeCStringsFromStrings(opts.Headers), + } + populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget) + return copts +} - populateRemoteCallbacks(&options.callbacks, &opts.RemoteCallbacks) +func freePushOptions(copts *C.git_push_options) { + if copts == nil { + return + } + untrackCallbacksPayload(&copts.callbacks) + freeStrarray(&copts.custom_headers) } // Fetch performs a fetch operation. refspecs specifies which refspecs @@ -777,26 +853,29 @@ func (o *Remote) Fetch(refspecs []string, opts *FetchOptions, msg string) error defer C.free(unsafe.Pointer(cmsg)) } - crefspecs := C.git_strarray{} - crefspecs.count = C.size_t(len(refspecs)) - crefspecs.strings = makeCStringsFromStrings(refspecs) + var err error + crefspecs := C.git_strarray{ + count: C.size_t(len(refspecs)), + strings: makeCStringsFromStrings(refspecs), + } defer freeStrarray(&crefspecs) - coptions := (*C.git_fetch_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_fetch_options{})))) - defer C.free(unsafe.Pointer(coptions)) - - populateFetchOptions(coptions, opts) - defer untrackCalbacksPayload(&coptions.callbacks) - defer freeStrarray(&coptions.custom_headers) + coptions := populateFetchOptions(&C.git_fetch_options{}, opts, &err) + defer freeFetchOptions(coptions) runtime.LockOSThread() defer runtime.UnlockOSThread() ret := C.git_remote_fetch(o.ptr, &crefspecs, coptions, cmsg) runtime.KeepAlive(o) + + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } + return nil } @@ -816,23 +895,27 @@ func (o *Remote) ConnectPush(callbacks *RemoteCallbacks, proxyOpts *ProxyOptions // // 'headers' are extra HTTP headers to use in this connection. func (o *Remote) Connect(direction ConnectDirection, callbacks *RemoteCallbacks, proxyOpts *ProxyOptions, headers []string) error { - var ccallbacks C.git_remote_callbacks - populateRemoteCallbacks(&ccallbacks, callbacks) + var err error + ccallbacks := populateRemoteCallbacks(&C.git_remote_callbacks{}, callbacks, &err) + defer untrackCallbacksPayload(ccallbacks) - var cproxy C.git_proxy_options - populateProxyOptions(&cproxy, proxyOpts) - defer freeProxyOptions(&cproxy) + cproxy := populateProxyOptions(&C.git_proxy_options{}, proxyOpts) + defer freeProxyOptions(cproxy) - cheaders := C.git_strarray{} - cheaders.count = C.size_t(len(headers)) - cheaders.strings = makeCStringsFromStrings(headers) + cheaders := C.git_strarray{ + count: C.size_t(len(headers)), + strings: makeCStringsFromStrings(headers), + } defer freeStrarray(&cheaders) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_connect(o.ptr, C.git_direction(direction), &ccallbacks, &cproxy, &cheaders) + ret := C.git_remote_connect(o.ptr, C.git_direction(direction), ccallbacks, cproxy, &cheaders) runtime.KeepAlive(o) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret != 0 { return MakeGitError(ret) } @@ -896,23 +979,24 @@ func (o *Remote) Ls(filterRefs ...string) ([]RemoteHead, error) { } func (o *Remote) Push(refspecs []string, opts *PushOptions) error { - crefspecs := C.git_strarray{} - crefspecs.count = C.size_t(len(refspecs)) - crefspecs.strings = makeCStringsFromStrings(refspecs) + crefspecs := C.git_strarray{ + count: C.size_t(len(refspecs)), + strings: makeCStringsFromStrings(refspecs), + } defer freeStrarray(&crefspecs) - coptions := (*C.git_push_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_push_options{})))) - defer C.free(unsafe.Pointer(coptions)) - - populatePushOptions(coptions, opts) - defer untrackCalbacksPayload(&coptions.callbacks) - defer freeStrarray(&coptions.custom_headers) + var err error + coptions := populatePushOptions(&C.git_push_options{}, opts, &err) + defer freePushOptions(coptions) runtime.LockOSThread() defer runtime.UnlockOSThread() ret := C.git_remote_push(o.ptr, &crefspecs, coptions) runtime.KeepAlive(o) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } @@ -924,14 +1008,18 @@ func (o *Remote) PruneRefs() bool { } func (o *Remote) Prune(callbacks *RemoteCallbacks) error { - var ccallbacks C.git_remote_callbacks - populateRemoteCallbacks(&ccallbacks, callbacks) + var err error + ccallbacks := populateRemoteCallbacks(&C.git_remote_callbacks{}, callbacks, &err) + defer untrackCallbacksPayload(ccallbacks) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_prune(o.ptr, &ccallbacks) + ret := C.git_remote_prune(o.ptr, ccallbacks) runtime.KeepAlive(o) + if ret == C.int(ErrorCodeUser) && err != nil { + return err + } if ret < 0 { return MakeGitError(ret) } diff --git a/reset.go b/reset.go index 155b58bc1..6a2ed9d1a 100644 --- a/reset.go +++ b/reset.go @@ -19,7 +19,7 @@ func (r *Repository) ResetToCommit(commit *Commit, resetType ResetType, opts *Ch defer runtime.UnlockOSThread() var err error - cOpts := opts.toC(&err) + 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) diff --git a/revert.go b/revert.go index 5c651a29e..d146bbe5d 100644 --- a/revert.go +++ b/revert.go @@ -15,48 +15,47 @@ type RevertOptions struct { CheckoutOpts CheckoutOptions } -func (opts *RevertOptions) toC(errorTarget *error) *C.git_revert_options { +func populateRevertOptions(copts *C.git_revert_options, opts *RevertOptions, errorTarget *error) *C.git_revert_options { + C.git_revert_init_options(copts, C.GIT_REVERT_OPTIONS_VERSION) if opts == nil { return nil } - return &C.git_revert_options{ - version: C.GIT_REVERT_OPTIONS_VERSION, - mainline: C.uint(opts.Mainline), - merge_opts: *opts.MergeOpts.toC(), - checkout_opts: *opts.CheckoutOpts.toC(errorTarget), - } + copts.mainline = C.uint(opts.Mainline) + populateMergeOptions(&copts.merge_opts, &opts.MergeOpts) + populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOpts, errorTarget) + return copts } -func revertOptionsFromC(opts *C.git_revert_options) RevertOptions { +func revertOptionsFromC(copts *C.git_revert_options) RevertOptions { return RevertOptions{ - Mainline: uint(opts.mainline), - MergeOpts: mergeOptionsFromC(&opts.merge_opts), - CheckoutOpts: checkoutOptionsFromC(&opts.checkout_opts), + Mainline: uint(copts.mainline), + MergeOpts: mergeOptionsFromC(&copts.merge_opts), + CheckoutOpts: checkoutOptionsFromC(&copts.checkout_opts), } } -func freeRevertOptions(opts *C.git_revert_options) { - if opts != nil { +func freeRevertOptions(copts *C.git_revert_options) { + if copts != nil { return } - freeMergeOptions(&opts.merge_opts) - freeCheckoutOptions(&opts.checkout_opts) + freeMergeOptions(&copts.merge_opts) + freeCheckoutOptions(&copts.checkout_opts) } // DefaultRevertOptions initialises a RevertOptions struct with default values func DefaultRevertOptions() (RevertOptions, error) { - opts := C.git_revert_options{} + copts := C.git_revert_options{} runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C.git_revert_init_options(&opts, C.GIT_REVERT_OPTIONS_VERSION) + ecode := C.git_revert_init_options(&copts, C.GIT_REVERT_OPTIONS_VERSION) if ecode < 0 { return RevertOptions{}, MakeGitError(ecode) } - defer freeRevertOptions(&opts) - return revertOptionsFromC(&opts), nil + defer freeRevertOptions(&copts) + return revertOptionsFromC(&copts), nil } // Revert the provided commit leaving the index updated with the results of the revert @@ -65,7 +64,7 @@ func (r *Repository) Revert(commit *Commit, revertOptions *RevertOptions) error defer runtime.UnlockOSThread() var err error - cOpts := revertOptions.toC(&err) + cOpts := populateRevertOptions(&C.git_revert_options{}, revertOptions, &err) defer freeRevertOptions(cOpts) ret := C.git_revert(r.ptr, commit.cast_ptr, cOpts) @@ -88,7 +87,7 @@ func (r *Repository) RevertCommit(revertCommit *Commit, ourCommit *Commit, mainl runtime.LockOSThread() defer runtime.UnlockOSThread() - cOpts := mergeOptions.toC() + cOpts := populateMergeOptions(&C.git_merge_options{}, mergeOptions) defer freeMergeOptions(cOpts) var index *C.git_index diff --git a/stash.go b/stash.go index 3c79eb3f1..3b1ae0c8f 100644 --- a/stash.go +++ b/stash.go @@ -161,34 +161,32 @@ func DefaultStashApplyOptions() (StashApplyOptions, error) { }, nil } -func (opts *StashApplyOptions) toC(errorTarget *error) *C.git_stash_apply_options { +func populateStashApplyOptions(copts *C.git_stash_apply_options, opts *StashApplyOptions, errorTarget *error) *C.git_stash_apply_options { + C.git_stash_apply_init_options(copts, C.GIT_STASH_APPLY_OPTIONS_VERSION) if opts == nil { return nil } - optsC := &C.git_stash_apply_options{ - version: C.GIT_STASH_APPLY_OPTIONS_VERSION, - flags: C.git_stash_apply_flags(opts.Flags), - } - populateCheckoutOptions(&optsC.checkout_options, &opts.CheckoutOptions, errorTarget) + copts.flags = C.git_stash_apply_flags(opts.Flags) + populateCheckoutOptions(&copts.checkout_options, &opts.CheckoutOptions, errorTarget) if opts.ProgressCallback != nil { progressData := &stashApplyProgressCallbackData{ callback: opts.ProgressCallback, errorTarget: errorTarget, } - C._go_git_populate_stash_apply_callbacks(optsC) - optsC.progress_payload = pointerHandles.Track(progressData) + C._go_git_populate_stash_apply_callbacks(copts) + copts.progress_payload = pointerHandles.Track(progressData) } - return optsC + return copts } -func freeStashApplyOptions(optsC *C.git_stash_apply_options) { - if optsC == nil { +func freeStashApplyOptions(copts *C.git_stash_apply_options) { + if copts == nil { return } - if optsC.progress_payload != nil { - pointerHandles.Untrack(optsC.progress_payload) + if copts.progress_payload != nil { + pointerHandles.Untrack(copts.progress_payload) } - freeCheckoutOptions(&optsC.checkout_options) + freeCheckoutOptions(&copts.checkout_options) } // Apply applies a single stashed state from the stash list. @@ -217,7 +215,7 @@ func freeStashApplyOptions(optsC *C.git_stash_apply_options) { // Error codes can be interogated with IsErrorCode(err, ErrorCodeNotFound). func (c *StashCollection) Apply(index int, opts StashApplyOptions) error { var err error - optsC := opts.toC(&err) + optsC := populateStashApplyOptions(&C.git_stash_apply_options{}, &opts, &err) defer freeStashApplyOptions(optsC) runtime.LockOSThread() @@ -320,7 +318,7 @@ func (c *StashCollection) Drop(index int) error { // state for the given index. func (c *StashCollection) Pop(index int, opts StashApplyOptions) error { var err error - optsC := opts.toC(&err) + optsC := populateStashApplyOptions(&C.git_stash_apply_options{}, &opts, &err) defer freeStashApplyOptions(optsC) runtime.LockOSThread() diff --git a/submodule.go b/submodule.go index 2b299d17b..45a28ae1d 100644 --- a/submodule.go +++ b/submodule.go @@ -383,22 +383,22 @@ func (sub *Submodule) Update(init bool, opts *SubmoduleUpdateOptions) error { return nil } -func populateSubmoduleUpdateOptions(ptr *C.git_submodule_update_options, opts *SubmoduleUpdateOptions, errorTarget *error) *C.git_submodule_update_options { - C.git_submodule_update_init_options(ptr, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION) - +func populateSubmoduleUpdateOptions(copts *C.git_submodule_update_options, opts *SubmoduleUpdateOptions, errorTarget *error) *C.git_submodule_update_options { + C.git_submodule_update_init_options(copts, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION) if opts == nil { return nil } - populateCheckoutOptions(&ptr.checkout_opts, opts.CheckoutOpts, errorTarget) - populateFetchOptions(&ptr.fetch_opts, opts.FetchOptions) + populateCheckoutOptions(&copts.checkout_opts, opts.CheckoutOpts, errorTarget) + populateFetchOptions(&copts.fetch_opts, opts.FetchOptions, errorTarget) - return ptr + return copts } -func freeSubmoduleUpdateOptions(ptr *C.git_submodule_update_options) { - if ptr == nil { +func freeSubmoduleUpdateOptions(copts *C.git_submodule_update_options) { + if copts == nil { return } - freeCheckoutOptions(&ptr.checkout_opts) + freeCheckoutOptions(&copts.checkout_opts) + freeFetchOptions(&copts.fetch_opts) } diff --git a/wrapper.c b/wrapper.c index 8e17cea85..315a43e1d 100644 --- a/wrapper.c +++ b/wrapper.c @@ -13,7 +13,7 @@ // // // myfile.go // type FooCallback func(...) (..., error) -// type FooCallbackData struct { +// type fooCallbackData struct { // callback FooCallback // errorTarget *error // } @@ -59,20 +59,28 @@ // return git_my_function(..., (git_foo_cb)&fooCallback, payload); // } // -// * Otherwise, if the same callback can be invoked from multiple functions or -// from different stacks (e.g. when passing the callback to an object), a -// different pattern should be used instead, which has the downside of losing -// the original error object and converting it to a GitError: +// * Additionally, if the same callback can be invoked from multiple functions or +// from different stacks (e.g. when passing the callback to an object), the +// following pattern should be used in tandem, which has the downside of +// losing the original error object and converting it to a GitError if the +// callback happens from a different stack: // // // myfile.go // type FooCallback func(...) (..., error) +// type fooCallbackData struct { +// callback FooCallback +// errorTarget *error +// } // // //export fooCallback // func fooCallback(errorMessage **C.char, ..., handle unsafe.Pointer) C.int { -// callback := pointerHandles.Get(data).(*FooCallback) +// data := pointerHandles.Get(data).(*fooCallbackData) // ... -// err := callback(...) +// err := data.callback(...) // if err != nil { +// if data.errorTarget != nil { +// *data.errorTarget = err +// } // return setCallbackError(errorMessage, err) // } // return C.int(ErrorCodeOK) From fb161a65753e9330d5e10514a28ae5f8073d6faf Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 13 Dec 2020 11:08:53 -0800 Subject: [PATCH 078/103] Support more MergeBase functions (#720) (#723) This change adds support for MergeBaseMany, MergeBasesMany, and MergeBaseOctopus. (cherry picked from commit 698ddfb4ac4d8d7d66f68e36ceabcabc5426002b) Co-authored-by: lhchavez --- merge.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++-- merge_test.go | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 3 deletions(-) diff --git a/merge.go b/merge.go index 30b39a462..b9d551703 100644 --- a/merge.go +++ b/merge.go @@ -333,7 +333,7 @@ func (r *Repository) MergeBases(one, two *Oid) ([]*Oid, error) { runtime.KeepAlive(one) runtime.KeepAlive(two) if ret < 0 { - return make([]*Oid, 0), MakeGitError(ret) + return nil, MakeGitError(ret) } oids := make([]*Oid, coids.count) @@ -352,8 +352,78 @@ func (r *Repository) MergeBases(one, two *Oid) ([]*Oid, error) { return oids, nil } -//TODO: int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[]); -//TODO: GIT_EXTERN(int) git_merge_base_octopus(git_oid *out,git_repository *repo,size_t length,const git_oid input_array[]); +// MergeBaseMany finds a merge base given a list of commits. +func (r *Repository) MergeBaseMany(oids []*Oid) (*Oid, error) { + coids := make([]C.git_oid, len(oids)) + for i := 0; i < len(oids); i++ { + coids[i] = *oids[i].toC() + } + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var oid C.git_oid + ret := C.git_merge_base_many(&oid, r.ptr, C.size_t(len(oids)), &coids[0]) + runtime.KeepAlive(r) + runtime.KeepAlive(coids) + if ret < 0 { + return nil, MakeGitError(ret) + } + return newOidFromC(&oid), nil +} + +// MergeBasesMany finds all merge bases given a list of commits. +func (r *Repository) MergeBasesMany(oids []*Oid) ([]*Oid, error) { + inCoids := make([]C.git_oid, len(oids)) + for i := 0; i < len(oids); i++ { + inCoids[i] = *oids[i].toC() + } + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var outCoids C.git_oidarray + ret := C.git_merge_bases_many(&outCoids, r.ptr, C.size_t(len(oids)), &inCoids[0]) + runtime.KeepAlive(r) + runtime.KeepAlive(inCoids) + if ret < 0 { + return nil, MakeGitError(ret) + } + + outOids := make([]*Oid, outCoids.count) + hdr := reflect.SliceHeader{ + Data: uintptr(unsafe.Pointer(outCoids.ids)), + Len: int(outCoids.count), + Cap: int(outCoids.count), + } + goSlice := *(*[]C.git_oid)(unsafe.Pointer(&hdr)) + + for i, cid := range goSlice { + outOids[i] = newOidFromC(&cid) + } + + return outOids, nil +} + +// MergeBaseOctopus finds a merge base in preparation for an octopus merge. +func (r *Repository) MergeBaseOctopus(oids []*Oid) (*Oid, error) { + coids := make([]C.git_oid, len(oids)) + for i := 0; i < len(oids); i++ { + coids[i] = *oids[i].toC() + } + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var oid C.git_oid + ret := C.git_merge_base_octopus(&oid, r.ptr, C.size_t(len(oids)), &coids[0]) + runtime.KeepAlive(r) + runtime.KeepAlive(coids) + if ret < 0 { + return nil, MakeGitError(ret) + } + return newOidFromC(&oid), nil +} type MergeFileResult struct { Automergeable bool diff --git a/merge_test.go b/merge_test.go index 319bef332..d49d07c4c 100644 --- a/merge_test.go +++ b/merge_test.go @@ -163,6 +163,15 @@ func TestMergeBase(t *testing.T) { if mergeBase.Cmp(commitAId) != 0 { t.Fatalf("unexpected merge base") } +} + +func TestMergeBases(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + commitAId, _ := seedTestRepo(t, repo) + commitBId, _ := appendCommit(t, repo) mergeBases, err := repo.MergeBases(commitAId, commitBId) checkFatal(t, err) @@ -176,6 +185,58 @@ func TestMergeBase(t *testing.T) { } } +func TestMergeBaseMany(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + commitAId, _ := seedTestRepo(t, repo) + commitBId, _ := appendCommit(t, repo) + + mergeBase, err := repo.MergeBaseMany([]*Oid{commitAId, commitBId}) + checkFatal(t, err) + + if mergeBase.Cmp(commitAId) != 0 { + t.Fatalf("unexpected merge base") + } +} + +func TestMergeBasesMany(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + commitAId, _ := seedTestRepo(t, repo) + commitBId, _ := appendCommit(t, repo) + + mergeBases, err := repo.MergeBasesMany([]*Oid{commitAId, commitBId}) + checkFatal(t, err) + + if len(mergeBases) != 1 { + t.Fatalf("expected merge bases len to be 1, got %v", len(mergeBases)) + } + + if mergeBases[0].Cmp(commitAId) != 0 { + t.Fatalf("unexpected merge base") + } +} + +func TestMergeBaseOctopus(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + commitAId, _ := seedTestRepo(t, repo) + commitBId, _ := appendCommit(t, repo) + + mergeBase, err := repo.MergeBaseOctopus([]*Oid{commitAId, commitBId}) + checkFatal(t, err) + + if mergeBase.Cmp(commitAId) != 0 { + t.Fatalf("unexpected merge base") + } +} + func compareBytes(t *testing.T, expected, actual []byte) { for i, v := range expected { if actual[i] != v { From 8c043d028eb0c47dcdf1bbf1fa6b3556e6b31660 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 13 Dec 2020 15:47:18 -0800 Subject: [PATCH 079/103] Rename the build files (#724) (#725) This change renames the build files so they come lexicographically before any source files. This makes the compile errors (due to mismatched libgit2 versions) easier to understand, since the `Build_*.go` files will be tried before the rest, and the `#error` in those files will kick in, leading to a much better experience. This unfortunately goes a bit against the defacto standard of using only lowercase characters in filenames, but the better developer experience (and better self-diagnosis when things go wrong instead of having to open a new issue) is worth the deviation. Fixes: #711 Fixes: #617 (cherry picked from commit 4b2ac7c998be677d865367908787f17fb570c679) Co-authored-by: lhchavez --- git_bundled_static.go => Build_bundled_static.go | 0 git_system_dynamic.go => Build_system_dynamic.go | 0 git_system_static.go => Build_system_static.go | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename git_bundled_static.go => Build_bundled_static.go (100%) rename git_system_dynamic.go => Build_system_dynamic.go (100%) rename git_system_static.go => Build_system_static.go (100%) diff --git a/git_bundled_static.go b/Build_bundled_static.go similarity index 100% rename from git_bundled_static.go rename to Build_bundled_static.go diff --git a/git_system_dynamic.go b/Build_system_dynamic.go similarity index 100% rename from git_system_dynamic.go rename to Build_system_dynamic.go diff --git a/git_system_static.go b/Build_system_static.go similarity index 100% rename from git_system_static.go rename to Build_system_static.go From bbb77f1bdf1c36cc0e793015f768e1d731d5cb72 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 3 Feb 2021 06:29:14 -0800 Subject: [PATCH 080/103] Support git_repository_message, git_repository_message_remove (#734) (#737) Closes #646 (cherry picked from commit 07147a8ea8ccf216fa490e7ed4ec84e7c5f5d9ee) Co-authored-by: Byoungchan Lee --- merge_test.go | 8 ++++++++ repository.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/merge_test.go b/merge_test.go index d49d07c4c..72b1a263a 100644 --- a/merge_test.go +++ b/merge_test.go @@ -43,6 +43,14 @@ func TestMergeWithSelf(t *testing.T) { 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) { diff --git a/repository.go b/repository.go index 42e74a03d..07f5df714 100644 --- a/repository.go +++ b/repository.go @@ -688,3 +688,39 @@ func (r *Repository) ClearGitIgnoreRules() error { } return nil } + +// Message retrieves git's prepared message. +// Operations such as git revert/cherry-pick/merge with the -n option stop just +// short of creating a commit with the changes and save their prepared message +// in .git/MERGE_MSG so the next git-commit execution can present it to the +// user for them to amend if they wish. +// +// Use this function to get the contents of this file. Don't forget to remove +// the file after you create the commit. +func (r *Repository) Message() (string, error) { + buf := C.git_buf{} + defer C.git_buf_free(&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 +} From 6f19ef8843b8bf761410c4e78074620fbdb539a7 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 15 Feb 2021 13:57:38 -0800 Subject: [PATCH 081/103] fix: Use `err` instead of error as a variable name for errors (#746) (#749) fix #745 (cherry picked from commit f6c5753df885e8511cf7d6437ee10e81c91a9651) Co-authored-by: Suhaib Mujahid --- git.go | 6 +++--- odb_test.go | 38 +++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/git.go b/git.go index 1b01ec122..9e22231f5 100644 --- a/git.go +++ b/git.go @@ -196,9 +196,9 @@ func NewOid(s string) (*Oid, error) { o := new(Oid) - slice, error := hex.DecodeString(s) - if error != nil { - return nil, error + slice, err := hex.DecodeString(s) + if err != nil { + return nil, err } if len(slice) != 20 { diff --git a/odb_test.go b/odb_test.go index cb3ab5548..ed5c24c55 100644 --- a/odb_test.go +++ b/odb_test.go @@ -61,31 +61,31 @@ func TestOdbStream(t *testing.T) { _, _ = seedTestRepo(t, repo) - odb, error := repo.Odb() - checkFatal(t, error) + odb, err := repo.Odb() + checkFatal(t, err) str := "hello, world!" - writeStream, error := odb.NewWriteStream(int64(len(str)), ObjectBlob) - checkFatal(t, error) - n, error := io.WriteString(writeStream, str) - checkFatal(t, error) + writeStream, err := odb.NewWriteStream(int64(len(str)), ObjectBlob) + checkFatal(t, err) + n, err := io.WriteString(writeStream, str) + checkFatal(t, err) if n != len(str) { t.Fatalf("Bad write length %v != %v", n, len(str)) } - error = writeStream.Close() - checkFatal(t, error) + err = writeStream.Close() + checkFatal(t, err) - expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f") - checkFatal(t, error) + expectedId, err := NewOid("30f51a3fba5274d53522d0f19748456974647b4f") + checkFatal(t, err) if writeStream.Id.Cmp(expectedId) != 0 { t.Fatal("Wrong data written") } - readStream, error := odb.NewReadStream(&writeStream.Id) - checkFatal(t, error) - data, error := ioutil.ReadAll(readStream) + readStream, err := odb.NewReadStream(&writeStream.Id) + checkFatal(t, err) + data, err := ioutil.ReadAll(readStream) if str != string(data) { t.Fatalf("Wrong data read %v != %v", str, string(data)) } @@ -98,8 +98,8 @@ func TestOdbHash(t *testing.T) { _, _ = seedTestRepo(t, repo) - odb, error := repo.Odb() - checkFatal(t, error) + odb, err := repo.Odb() + checkFatal(t, err) str := `tree 115fcae49287c82eb55bb275cbbd4556fbed72b7 parent 66e1c476199ebcd3e304659992233132c5a52c6c @@ -109,11 +109,11 @@ committer John Doe 1390682018 +0000 Initial commit.` for _, data := range [][]byte{[]byte(str), doublePointerBytes()} { - oid, error := odb.Hash(data, ObjectCommit) - checkFatal(t, error) + oid, err := odb.Hash(data, ObjectCommit) + checkFatal(t, err) - coid, error := odb.Write(data, ObjectCommit) - checkFatal(t, error) + coid, err := odb.Write(data, ObjectCommit) + checkFatal(t, err) if oid.Cmp(coid) != 0 { t.Fatal("Hash and write Oids are different") From c0b9936476e882c525c951e57adb13b1eb3ed4db Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 15 Feb 2021 13:57:57 -0800 Subject: [PATCH 082/103] Make index time fields public (#750) (#751) From gorelease: ``` Compatible changes: - IndexTime.Nanoseconds: added - IndexTime.Seconds: added ``` There are no extra tests because there isn't really anything to test closes #304 (cherry picked from commit aeb22bcf7dd6b3b8f75363a20790b84ea4d5de9f) Co-authored-by: michael boulton <61595820+mbfr@users.noreply.github.com> --- index.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.go b/index.go index 4960a73db..31f1a9633 100644 --- a/index.go +++ b/index.go @@ -57,8 +57,8 @@ type Index struct { } type IndexTime struct { - seconds int32 - nanoseconds uint32 + Seconds int32 + Nanoseconds uint32 } type IndexEntry struct { @@ -89,10 +89,10 @@ func newIndexEntryFromC(entry *C.git_index_entry) *IndexEntry { } func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) { - dest.ctime.seconds = C.int32_t(source.Ctime.seconds) - dest.ctime.nanoseconds = C.uint32_t(source.Ctime.nanoseconds) - dest.mtime.seconds = C.int32_t(source.Mtime.seconds) - dest.mtime.nanoseconds = C.uint32_t(source.Mtime.nanoseconds) + dest.ctime.seconds = C.int32_t(source.Ctime.Seconds) + dest.ctime.nanoseconds = C.uint32_t(source.Ctime.Nanoseconds) + dest.mtime.seconds = C.int32_t(source.Mtime.Seconds) + dest.mtime.nanoseconds = C.uint32_t(source.Mtime.Nanoseconds) dest.mode = C.uint32_t(source.Mode) dest.uid = C.uint32_t(source.Uid) dest.gid = C.uint32_t(source.Gid) From 275690e61ad7e5007e563f6eb70c306a4d416ae2 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 7 Mar 2021 17:55:17 -0800 Subject: [PATCH 083/103] Implement git_repository_set_config (#735) (#743) Closes #732 (cherry picked from commit 2fd0495c43c4a54a02e2bbfca886687621f83f9f) Co-authored-by: Byoungchan Lee --- repository.go | 11 ++++++++++ repository_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/repository.go b/repository.go index 07f5df714..1ee9b361c 100644 --- a/repository.go +++ b/repository.go @@ -165,6 +165,17 @@ func (v *Repository) Config() (*Config, error) { return config, nil } +// SetConfig sets the configuration file for this repository. +// +// This configuration file will be used for all configuration queries involving +// this repository. +func (v *Repository) SetConfig(c *Config) error { + C.git_repository_set_config(v.ptr, c.ptr) + runtime.KeepAlive(v) + runtime.KeepAlive(c) + return nil +} + func (v *Repository) Index() (*Index, error) { var ptr *C.git_index diff --git a/repository_test.go b/repository_test.go index 1950c6977..5a0f9202a 100644 --- a/repository_test.go +++ b/repository_test.go @@ -40,3 +40,55 @@ func TestCreateCommitFromIds(t *testing.T) { t.Errorf("mismatched commit ids, expected %v, got %v", expectedCommitId.String(), commitId.String()) } } + +func TestRepositorySetConfig(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + loc, err := time.LoadLocation("Europe/Berlin") + checkFatal(t, err) + sig := &Signature{ + Name: "Rand Om Hacker", + Email: "random@hacker.com", + When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc), + } + + idx, err := repo.Index() + checkFatal(t, err) + err = idx.AddByPath("README") + + treeId, err := idx.WriteTree() + checkFatal(t, err) + + message := "This is a commit\n" + tree, err := repo.LookupTree(treeId) + checkFatal(t, err) + _, err = repo.CreateCommit("HEAD", sig, sig, message, tree) + checkFatal(t, err) + + repoConfig, err := repo.Config() + checkFatal(t, err) + + temp := Config{} + localConfig, err := temp.OpenLevel(repoConfig, ConfigLevelLocal) + checkFatal(t, err) + repoConfig = nil + + err = repo.SetConfig(localConfig) + checkFatal(t, err) + + configFieldName := "core.filemode" + err = localConfig.SetBool(configFieldName, true) + checkFatal(t, err) + + localConfig = nil + + repoConfig, err = repo.Config() + checkFatal(t, err) + + result, err := repoConfig.LookupBool(configFieldName) + checkFatal(t, err) + if result != true { + t.Fatal("result must be true") + } +} From bcfd445806a2386bb180e121d1ec7c7bdc17ea77 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 4 Apr 2021 08:28:09 -0700 Subject: [PATCH 084/103] Git repository item path (#757) (#768) add wrapper for `git_repository_item_path` (cherry picked from commit a4d202ed7b025331ee4a63ebc38f62519cee4750) Co-authored-by: Vladimir Buzuev <44682889+vladimir-buzuev@users.noreply.github.com> --- repository.go | 34 ++++++++++++++++++++++++++++++++++ repository_test.go | 11 +++++++++++ 2 files changed, 45 insertions(+) diff --git a/repository.go b/repository.go index 1ee9b361c..c17b4f47f 100644 --- a/repository.go +++ b/repository.go @@ -735,3 +735,37 @@ func (r *Repository) RemoveMessage() error { } 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_free(&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 +} diff --git a/repository_test.go b/repository_test.go index 5a0f9202a..e403aa993 100644 --- a/repository_test.go +++ b/repository_test.go @@ -92,3 +92,14 @@ func TestRepositorySetConfig(t *testing.T) { 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") + } +} From efed131f1fb1c01fa9c440ac436b43b493819438 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 4 Apr 2021 08:28:49 -0700 Subject: [PATCH 085/103] fix buldled static build on Windows/MinGW (#761) (#763) seems like need more libraries in LDFLAGS: * ws2_32 for socket, connect, htonl, etc * ole32 for CoInitializeEx * rpcrt4 for UuidCreate * crypt32 for CertFreeCertificateContext (cherry picked from commit 0d7c8dadb465a0236d0f130a0cfd5179502f191d) Co-authored-by: Vladimir Buzuev <44682889+vladimir-buzuev@users.noreply.github.com> --- Build_bundled_static.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Build_bundled_static.go b/Build_bundled_static.go index e66f425d7..1e1cb3669 100644 --- a/Build_bundled_static.go +++ b/Build_bundled_static.go @@ -4,7 +4,7 @@ package git /* #cgo windows CFLAGS: -I${SRCDIR}/static-build/install/include/ -#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp +#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp -lws2_32 -lole32 -lrpcrt4 -lcrypt32 #cgo !windows pkg-config: --static ${SRCDIR}/static-build/install/lib/pkgconfig/libgit2.pc #cgo CFLAGS: -DLIBGIT2_STATIC #include From deb154820c0b55789b65db3e50948decde6a4744 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 4 Apr 2021 08:29:27 -0700 Subject: [PATCH 086/103] add wrapper for git_config_open_default (#758) (#765) (cherry picked from commit 1e2cb92b4887132563850db0338e701e740f6a6f) Co-authored-by: Vladimir Buzuev <44682889+vladimir-buzuev@users.noreply.github.com> --- config.go | 14 ++++++++++++++ config_test.go | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/config.go b/config.go index ab9af38ff..c30a2df69 100644 --- a/config.go +++ b/config.go @@ -450,3 +450,17 @@ func ConfigFindProgramdata() (string, error) { return C.GoString(buf.ptr), nil } + +// OpenDefault opens the default config according to git rules +func OpenDefault() (*Config, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + config := new(Config) + + if ret := C.git_config_open_default(&config.ptr); ret < 0 { + return nil, MakeGitError(ret) + } + + return config, nil +} diff --git a/config_test.go b/config_test.go index 196d4adbb..fdf6ec671 100644 --- a/config_test.go +++ b/config_test.go @@ -107,3 +107,13 @@ func TestConfigLookups(t *testing.T) { test(c, t) } } + +func TestOpenDefault(t *testing.T) { + + c, err := OpenDefault() + if err != nil { + t.Errorf("OpenDefault error: '%v'. Expected none\n", err) + return + } + defer c.Free() +} From ab793028ba4d72245facc96ec6f98367dc862ba6 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 4 Sep 2021 12:57:39 -0700 Subject: [PATCH 087/103] Remove the legacy builders (#776) (#780) These builds are no longer working because some of the dependencies now require newer versions of Go. Seems like the ecosystem has moved to Go 1.11+, so we are now forced to follow suit. (cherry picked from commit df7084d36a771e4ef2a418c7d3c4367d920e4cf3) --- .github/workflows/backport.yml | 3 ++- .github/workflows/ci.yml | 42 ++++------------------------------ 2 files changed, 6 insertions(+), 39 deletions(-) diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml index 4337fb1b4..8e6ca2f45 100644 --- a/.github/workflows/backport.yml +++ b/.github/workflows/backport.yml @@ -7,11 +7,12 @@ on: jobs: backport: + name: Backport change to branch ${{ matrix.branch }} + continue-on-error: true strategy: fail-fast: false matrix: branch: [ 'release-0.28', 'release-0.27' ] - name: Backport change to branch ${{ matrix.branch }} runs-on: ubuntu-20.04 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 420682455..5744ee4a8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,45 +9,11 @@ on: jobs: - build-legacy: - strategy: - fail-fast: false - matrix: - go: [ '1.9', '1.10' ] - name: Go ${{ matrix.go }} - - runs-on: ubuntu-18.04 - - steps: - - name: Set up Go - uses: actions/setup-go@v1 - with: - go-version: ${{ matrix.go }} - id: go - - name: Check out code into the GOPATH - uses: actions/checkout@v1 - with: - fetch-depth: 1 - path: src/github.com/${{ github.repository }} - - name: Build - env: - GOPATH: /home/runner/work/git2go - run: | - git submodule update --init - sudo apt-get install -y --no-install-recommends libssh2-1-dev - make build-libgit2-static - go get -tags static -t github.com/${{ github.repository }}/... - go build -tags static github.com/${{ github.repository }}/... - - name: Test - env: - GOPATH: /home/runner/work/git2go - run: make TEST_ARGS=-test.v test-static - build-static: strategy: fail-fast: false matrix: - go: [ '1.11', '1.12', '1.13', '1.14', '1.15' ] + go: [ '1.11', '1.12', '1.13', '1.14', '1.15', '1.16', '1.17' ] name: Go ${{ matrix.go }} runs-on: ubuntu-20.04 @@ -79,7 +45,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v1 with: - go-version: '1.15' + go-version: '1.17' id: go - name: Check out code into the Go module directory uses: actions/checkout@v1 @@ -102,7 +68,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v1 with: - go-version: '1.15' + go-version: '1.17' id: go - name: Check out code into the Go module directory uses: actions/checkout@v1 @@ -125,7 +91,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v1 with: - go-version: '1.15' + go-version: '1.17' id: go - name: Check out code into the Go module directory uses: actions/checkout@v1 From c7d968e12d6338076fd4485fd60a4a63b42d3b2a Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 4 Sep 2021 13:29:52 -0700 Subject: [PATCH 088/103] Add `Repository.CreateCommitBuffer` (#781) (#785) This commit adds the Go binding for `git_commit_create_buffer`. This will be used to support the 1.2.0 commit create callback. (cherry picked from commit fbaf9d1d1ae0bb7b6e7ed9044945d4c9322d4c76) --- commit.go | 8 ++++++ repository.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++ repository_test.go | 54 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) diff --git a/commit.go b/commit.go index 10bf96a80..787f210d0 100644 --- a/commit.go +++ b/commit.go @@ -12,6 +12,14 @@ import ( "unsafe" ) +// MessageEncoding is the encoding of commit messages. +type MessageEncoding string + +const ( + // MessageEncodingUTF8 is the default message encoding. + MessageEncodingUTF8 MessageEncoding = "UTF-8" +) + // Commit type Commit struct { Object diff --git a/repository.go b/repository.go index c17b4f47f..1fed42bff 100644 --- a/repository.go +++ b/repository.go @@ -479,6 +479,69 @@ func (v *Repository) CreateCommit( 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_free(&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) { diff --git a/repository_test.go b/repository_test.go index e403aa993..60758c153 100644 --- a/repository_test.go +++ b/repository_test.go @@ -5,6 +5,60 @@ import ( "time" ) +func TestCreateCommitBuffer(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + loc, err := time.LoadLocation("Europe/Berlin") + checkFatal(t, err) + sig := &Signature{ + Name: "Rand Om Hacker", + Email: "random@hacker.com", + When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc), + } + + idx, err := repo.Index() + checkFatal(t, err) + err = idx.AddByPath("README") + checkFatal(t, err) + err = idx.Write() + checkFatal(t, err) + treeId, err := idx.WriteTree() + checkFatal(t, err) + + message := "This is a commit\n" + tree, err := repo.LookupTree(treeId) + checkFatal(t, err) + + for encoding, expected := range map[MessageEncoding]string{ + MessageEncodingUTF8: `tree b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e +author Rand Om Hacker 1362576600 +0100 +committer Rand Om Hacker 1362576600 +0100 + +This is a commit +`, + MessageEncoding("ASCII"): `tree b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e +author Rand Om Hacker 1362576600 +0100 +committer Rand Om Hacker 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) From 0af4066df92446f4c3819bb2dcf2decfce931a5f Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 4 Sep 2021 13:44:04 -0700 Subject: [PATCH 089/103] Rename the default branch to `main` (#786) (#788) We've renamed the default branch from `master` to `main`, so we need to change a bunch of references to that. (cherry picked from commit be5a99a807beb2fd79dc0ca71b5a92611b1eda52) --- .github/workflows/backport.yml | 2 +- .github/workflows/ci.yml | 2 +- .github/workflows/tag.yml | 2 +- .travis.yml | 25 ------------------------- README.md | 14 +++++++------- 5 files changed, 10 insertions(+), 35 deletions(-) delete mode 100644 .travis.yml diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml index 8e6ca2f45..4172396af 100644 --- a/.github/workflows/backport.yml +++ b/.github/workflows/backport.yml @@ -2,7 +2,7 @@ name: Backport to older releases on: push: branches: - - master + - main jobs: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5744ee4a8..b8a8f48cf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,7 @@ on: pull_request: push: branches: - - master + - main - release-* - v* diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml index b29327450..a1da6c17e 100644 --- a/.github/workflows/tag.yml +++ b/.github/workflows/tag.yml @@ -2,7 +2,7 @@ name: Tag new releases on: push: branches: - - master + - main - release-* jobs: diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0b7f4825e..000000000 --- a/.travis.yml +++ /dev/null @@ -1,25 +0,0 @@ -language: go - -arch: - - AMD64 - - ppc64le - -go: - - tip - -install: - - sudo apt-get install -y --no-install-recommends libssh2-1-dev - - make build-libgit2-static - - go get --tags "static" ./... - -script: - - make test-static - -git: - submodules: true - -branches: - only: - - master - - /v\d+/ - - /release-.*/ diff --git a/README.md b/README.md index 20c11e08a..203e39230 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ git2go ====== -[![GoDoc](https://godoc.org/github.com/libgit2/git2go?status.svg)](http://godoc.org/github.com/libgit2/git2go) [![Build Status](https://travis-ci.org/libgit2/git2go.svg?branch=master)](https://travis-ci.org/libgit2/git2go) +[![GoDoc](https://godoc.org/github.com/libgit2/git2go?status.svg)](http://godoc.org/github.com/libgit2/git2go) [![Build Status](https://travis-ci.org/libgit2/git2go.svg?branch=main)](https://travis-ci.org/libgit2/git2go) Go bindings for [libgit2](http://libgit2.github.com/). @@ -10,7 +10,7 @@ Due to the fact that Go 1.11 module versions have semantic meaning and don't nec | libgit2 | git2go | |---------|---------------| -| master | (will be v30) | +| main | (will be v30) | | 0.99 | v29 | | 0.28 | v28 | | 0.27 | v27 | @@ -26,7 +26,7 @@ import "github.com/libgit2/git2go/v27" which will ensure there are no sudden changes to the API. -The `master` branch follows the tip of libgit2 itself (with some lag) and as such has no guarantees on the stability of libgit2's API. Thus this only supports statically linking against libgit2. +The `main` branch follows the tip of libgit2 itself (with some lag) and as such has no guarantees on the stability of libgit2's API. Thus this only supports statically linking against libgit2. ### Which branch to send Pull requests to @@ -56,16 +56,16 @@ Follow the instructions for [Versioned branch, dynamic linking](#versioned-branc go test -tags static,system_libgit2 github.com/my/project/... go install -tags static,system_libgit2 github.com/my/project/... -### Master branch, or vendored static linking +### `main` branch, or vendored static linking -If using `master` or building a branch with the vendored libgit2 statically, we need to build libgit2 first. In order to build it, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL (outside of Windows or macOS) and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. Note that even if libgit2 is included in the resulting binary, its dependencies will not be. +If using `main` or building a branch with the vendored libgit2 statically, we need to build libgit2 first. In order to build it, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL (outside of Windows or macOS) and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. Note that even if libgit2 is included in the resulting binary, its dependencies will not be. Run `go get -d github.com/libgit2/git2go` to download the code and go to your `$GOPATH/src/github.com/libgit2/git2go` directory. From there, we need to build the C code and put it into the resulting go binary. git submodule update --init # get libgit2 make install-static -will compile libgit2, link it into git2go and install it. The `master` branch is set up to follow the specific libgit2 version that is vendored, so trying dynamic linking may or may not work depending on the exact versions involved. +will compile libgit2, link it into git2go and install it. The `main` branch is set up to follow the specific libgit2 version that is vendored, so trying dynamic linking may or may not work depending on the exact versions involved. In order to let Go pass the correct flags to `pkg-config`, `-tags static` needs to be passed to all `go` commands that build any binaries. For instance: @@ -85,7 +85,7 @@ libgit2 may use OpenSSL and LibSSH2 for performing encrypted network connections Running the tests ----------------- -For the stable version, `go test` will work as usual. For the `master` branch, similarly to installing, running the tests requires building a local libgit2 library, so the Makefile provides a wrapper that makes sure it's built +For the stable version, `go test` will work as usual. For the `main` branch, similarly to installing, running the tests requires building a local libgit2 library, so the Makefile provides a wrapper that makes sure it's built make test-static From d1b05aeb84a1d28f6604d4c5b82474f81e51a827 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 4 Sep 2021 13:51:37 -0700 Subject: [PATCH 090/103] Add `CreateCommitWithSignature` (#782) (#791) This change adds the wrapper for `git_commit_create_with_signature`. (cherry picked from commit 15434610fec67e704d3ad443b03054d1611f98fe) Co-authored-by: lhchavez --- commit.go | 37 ++++--------------------------------- repository.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/commit.go b/commit.go index 787f210d0..5253b5f6f 100644 --- a/commit.go +++ b/commit.go @@ -75,40 +75,11 @@ func (c *Commit) WithSignatureUsing(f CommitSigningCallback) (*Oid, error) { // WithSignature creates a new signed commit from the given signature and signature field func (c *Commit) WithSignature(signature string, signatureField string) (*Oid, error) { - totalCommit := c.ContentToSign() - - oid := new(Oid) - - var csf *C.char = nil - if signatureField != "" { - csf = C.CString(signatureField) - defer C.free(unsafe.Pointer(csf)) - } - - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - cTotalCommit := C.CString(totalCommit) - cSignature := C.CString(signature) - defer C.free(unsafe.Pointer(cTotalCommit)) - defer C.free(unsafe.Pointer(cSignature)) - - ret := C.git_commit_create_with_signature( - oid.toC(), - c.Owner().ptr, - cTotalCommit, - cSignature, - csf, + return c.Owner().CreateCommitWithSignature( + c.ContentToSign(), + signature, + signatureField, ) - - runtime.KeepAlive(c) - runtime.KeepAlive(oid) - - if ret < 0 { - return nil, MakeGitError(ret) - } - - return oid, nil } func (c *Commit) ExtractSignature() (string, string, error) { diff --git a/repository.go b/repository.go index 1fed42bff..deaaef1bb 100644 --- a/repository.go +++ b/repository.go @@ -479,6 +479,39 @@ func (v *Repository) CreateCommit( return oid, nil } +// CreateCommitWithSignature creates a commit object from the given contents and +// signature. +func (v *Repository) CreateCommitWithSignature( + commitContent, signature, signatureField string, +) (*Oid, error) { + cCommitContent := C.CString(commitContent) + defer C.free(unsafe.Pointer(cCommitContent)) + var cSignature *C.char + if signature != "" { + cSignature = C.CString(string(signature)) + defer C.free(unsafe.Pointer(cSignature)) + } + var cSignatureField *C.char + if signatureField != "" { + cSignatureField = C.CString(string(signatureField)) + defer C.free(unsafe.Pointer(cSignatureField)) + } + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + oid := new(Oid) + ret := C.git_commit_create_with_signature(oid.toC(), v.ptr, cCommitContent, cSignature, cSignatureField) + + runtime.KeepAlive(v) + runtime.KeepAlive(oid) + if ret < 0 { + return nil, MakeGitError(ret) + } + + return oid, nil +} + // CreateCommitBuffer creates a commit and write it into a buffer. func (v *Repository) CreateCommitBuffer( author, committer *Signature, From 0c87b5a542c6468acdb0dcb736e3253abf969514 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 4 Sep 2021 14:03:17 -0700 Subject: [PATCH 091/103] Add DiffIgnoreWitespaceEol and deprecate DiffIgnoreWitespaceEol (#774) (#795) DiffIgnoreWitespaceEol contains a typo and does not have the same name as it's libgit2 counterpart. Fixes #773 (cherry picked from commit d4524761d9e08ba5430f5a94f56648c1ef0f651c) Co-authored-by: Gustav Westling --- deprecated.go | 7 +++++++ diff.go | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/deprecated.go b/deprecated.go index 1e24c10ca..9e38bb673 100644 --- a/deprecated.go +++ b/deprecated.go @@ -27,6 +27,13 @@ type BlobCallbackData struct { // CheckoutOpts is a deprecated alias of CheckoutOptions. type CheckoutOpts = CheckoutOptions +// diff.go + +const ( + // Deprecated: DiffIgnoreWhitespaceEol is a deprecated alias of DiffIgnoreWhitespaceEOL. + DiffIgnoreWitespaceEol = DiffIgnoreWhitespaceEOL +) + // features.go const ( diff --git a/diff.go b/diff.go index 8b0d7655f..bc7f8bc7b 100644 --- a/diff.go +++ b/diff.go @@ -486,7 +486,7 @@ const ( DiffIgnoreWhitespace DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE DiffIgnoreWhitespaceChange DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_CHANGE - DiffIgnoreWitespaceEol DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_EOL + DiffIgnoreWhitespaceEOL DiffOptionsFlag = C.GIT_DIFF_IGNORE_WHITESPACE_EOL DiffShowUntrackedContent DiffOptionsFlag = C.GIT_DIFF_SHOW_UNTRACKED_CONTENT DiffShowUnmodified DiffOptionsFlag = C.GIT_DIFF_SHOW_UNMODIFIED From 13670e24c562127f1b5758c355d419ad31282a53 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 4 Sep 2021 14:37:03 -0700 Subject: [PATCH 092/103] Prepare for the v1.2.0 release (#796) (#799) This change adds a few more deprecation messages just before we remove them. (cherry picked from commit 2077003fa5b9689e44a8c8956639f0b424af0d96) --- deprecated.go | 181 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 118 insertions(+), 63 deletions(-) diff --git a/deprecated.go b/deprecated.go index 9e38bb673..659e508ce 100644 --- a/deprecated.go +++ b/deprecated.go @@ -13,10 +13,10 @@ import ( // blob.go -// BlobChunkCallback is not used. +// Deprecated: BlobChunkCallback is not used. type BlobChunkCallback func(maxLen int) ([]byte, error) -// BlobCallbackData is not used. +// Deprecated: BlobCallbackData is not used. type BlobCallbackData struct { Callback BlobChunkCallback Error error @@ -24,7 +24,7 @@ type BlobCallbackData struct { // checkout.go -// CheckoutOpts is a deprecated alias of CheckoutOptions. +// Deprecated: CheckoutOpts is a deprecated alias of CheckoutOptions. type CheckoutOpts = CheckoutOptions // diff.go @@ -37,90 +37,145 @@ const ( // features.go const ( - // FeatureHttps is a deprecated alias of FeatureHTTPS. + // Deprecated: FeatureHttps is a deprecated alias of FeatureHTTPS. FeatureHttps = FeatureHTTPS - // FeatureSsh is a deprecated alias of FeatureSSH. + // Deprecated: FeatureSsh is a deprecated alias of FeatureSSH. FeatureSsh = FeatureSSH ) // git.go const ( - ErrClassNone = ErrorClassNone - ErrClassNoMemory = ErrorClassNoMemory - ErrClassOs = ErrorClassOS - ErrClassInvalid = ErrorClassInvalid - ErrClassReference = ErrorClassReference - ErrClassZlib = ErrorClassZlib + // 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 - ErrClassConfig = ErrorClassConfig - ErrClassRegex = ErrorClassRegex - ErrClassOdb = ErrorClassOdb - ErrClassIndex = ErrorClassIndex - ErrClassObject = ErrorClassObject - ErrClassNet = ErrorClassNet - ErrClassTag = ErrorClassTag - ErrClassTree = ErrorClassTree - ErrClassIndexer = ErrorClassIndexer - ErrClassSSL = ErrorClassSSL - ErrClassSubmodule = ErrorClassSubmodule - ErrClassThread = ErrorClassThread - ErrClassStash = ErrorClassStash - ErrClassCheckout = ErrorClassCheckout - ErrClassFetchHead = ErrorClassFetchHead - ErrClassMerge = ErrorClassMerge - ErrClassSsh = ErrorClassSSH - ErrClassFilter = ErrorClassFilter - ErrClassRevert = ErrorClassRevert - ErrClassCallback = ErrorClassCallback - ErrClassRebase = ErrorClassRebase - ErrClassPatch = ErrorClassPatch + // 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 ( - ErrOk = ErrorCodeOK - ErrGeneric = ErrorCodeGeneric - ErrNotFound = ErrorCodeNotFound - ErrExists = ErrorCodeExists - ErrAmbiguous = ErrorCodeAmbiguous - ErrAmbigious = ErrorCodeAmbiguous - ErrBuffs = ErrorCodeBuffs - ErrUser = ErrorCodeUser - ErrBareRepo = ErrorCodeBareRepo - ErrUnbornBranch = ErrorCodeUnbornBranch - ErrUnmerged = ErrorCodeUnmerged + // 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 - ErrInvalidSpec = ErrorCodeInvalidSpec - ErrConflict = ErrorCodeConflict - ErrLocked = ErrorCodeLocked - ErrModified = ErrorCodeModified - ErrAuth = ErrorCodeAuth - ErrCertificate = ErrorCodeCertificate - ErrApplied = ErrorCodeApplied - ErrPeel = ErrorCodePeel - ErrEOF = ErrorCodeEOF - ErrUncommitted = ErrorCodeUncommitted - ErrDirectory = ErrorCodeDirectory - ErrMergeConflict = ErrorCodeMergeConflict - ErrPassthrough = ErrorCodePassthrough - ErrIterOver = ErrorCodeIterOver + // 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 ) // index.go -// IndexAddOpts is a deprecated alias of IndexAddOption. +// Deprecated: IndexAddOpts is a deprecated alias of IndexAddOption. type IndexAddOpts = IndexAddOption -// IndexStageOpts is a deprecated alias of IndexStageState. +// Deprecated: IndexStageOpts is a deprecated alias of IndexStageState. type IndexStageOpts = IndexStageState // submodule.go -// SubmoduleCbk is a deprecated alias of SubmoduleCallback. +// Deprecated: SubmoduleCbk is a deprecated alias of SubmoduleCallback. type SubmoduleCbk = SubmoduleCallback -// SubmoduleVisitor is not used. +// Deprecated: SubmoduleVisitor is not used. func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { sub := &Submodule{(*C.git_submodule)(csub), nil} @@ -133,7 +188,7 @@ func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) // tree.go -// CallbackGitTreeWalk is not used. +// Deprecated: CallbackGitTreeWalk is not used. func CallbackGitTreeWalk(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer) C.int { root := C.GoString(_root) From 2f3b2b5f250e96aec4cb11952963f39cca5974a5 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 5 Sep 2021 14:06:59 -0700 Subject: [PATCH 093/103] Make all non-user-creatable structures non-comparable (#802) (#805) This change makes all non-user-creatable structures non-comparable. This makes it easier to add changes later that don't introduce breaking changes from the go compatibility guarantees perspective. This, of course, implies that this change _is_ a breaking change, but since these structures are not intended to be created by users (or de-referenced), it should be okay. (cherry picked from commit dbe032c347b1a1308a4b880e7c5a06d8dfb4d507) --- blame.go | 1 + blob.go | 2 ++ branch.go | 2 ++ commit.go | 1 + config.go | 2 ++ credentials.go | 3 ++- deprecated.go | 2 +- describe.go | 1 + diff.go | 2 ++ git.go | 4 ++++ handles.go | 1 + index.go | 4 +++- indexer.go | 1 + mempack.go | 1 + merge.go | 4 +++- note.go | 3 +++ object.go | 1 + odb.go | 8 +++++++- packbuilder.go | 1 + patch.go | 1 + rebase.go | 1 + refdb.go | 4 +++- reference.go | 6 +++++- remote.go | 2 ++ repository.go | 1 + revparse.go | 1 + stash.go | 1 + status.go | 1 + submodule.go | 4 +++- tag.go | 2 ++ tree.go | 2 ++ walk.go | 1 + 32 files changed, 63 insertions(+), 8 deletions(-) diff --git a/blame.go b/blame.go index de32bb32a..5bcaa705e 100644 --- a/blame.go +++ b/blame.go @@ -85,6 +85,7 @@ func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error) } type Blame struct { + doNotCompare ptr *C.git_blame } diff --git a/blob.go b/blob.go index 652b729aa..f33771cd7 100644 --- a/blob.go +++ b/blob.go @@ -15,6 +15,7 @@ import ( ) type Blob struct { + doNotCompare Object cast_ptr *C.git_blob } @@ -96,6 +97,7 @@ func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, err } type BlobWriteStream struct { + doNotCompare ptr *C.git_writestream repo *Repository } diff --git a/branch.go b/branch.go index 3985ad21e..69ee4f5e8 100644 --- a/branch.go +++ b/branch.go @@ -19,6 +19,7 @@ const ( ) type Branch struct { + doNotCompare *Reference } @@ -27,6 +28,7 @@ func (r *Reference) Branch() *Branch { } type BranchIterator struct { + doNotCompare ptr *C.git_branch_iterator repo *Repository } diff --git a/commit.go b/commit.go index 5253b5f6f..03d6ea57f 100644 --- a/commit.go +++ b/commit.go @@ -22,6 +22,7 @@ const ( // Commit type Commit struct { + doNotCompare Object cast_ptr *C.git_commit } diff --git a/config.go b/config.go index c30a2df69..08ba2e23b 100644 --- a/config.go +++ b/config.go @@ -52,6 +52,7 @@ func newConfigEntryFromC(centry *C.git_config_entry) *ConfigEntry { } type Config struct { + doNotCompare ptr *C.git_config } @@ -361,6 +362,7 @@ func OpenOndisk(parent *Config, path string) (*Config, error) { } type ConfigIterator struct { + doNotCompare ptr *C.git_config_iterator cfg *Config } diff --git a/credentials.go b/credentials.go index 2f683cc2a..d2311625e 100644 --- a/credentials.go +++ b/credentials.go @@ -23,6 +23,7 @@ const ( ) type Cred struct { + doNotCompare ptr *C.git_cred } @@ -38,7 +39,7 @@ func (o *Cred) Type() CredType { } func credFromC(ptr *C.git_cred) *Cred { - return &Cred{ptr} + return &Cred{ptr: ptr} } func NewCredUserpassPlaintext(username string, password string) (int, Cred) { diff --git a/deprecated.go b/deprecated.go index 659e508ce..739107315 100644 --- a/deprecated.go +++ b/deprecated.go @@ -177,7 +177,7 @@ type SubmoduleCbk = SubmoduleCallback // Deprecated: SubmoduleVisitor is not used. func SubmoduleVisitor(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { - sub := &Submodule{(*C.git_submodule)(csub), nil} + sub := &Submodule{ptr: (*C.git_submodule)(csub)} callback, ok := pointerHandles.Get(handle).(SubmoduleCallback) if !ok { diff --git a/describe.go b/describe.go index 0b750760e..53a9baa1c 100644 --- a/describe.go +++ b/describe.go @@ -176,6 +176,7 @@ func (repo *Repository) DescribeWorkdir(opts *DescribeOptions) (*DescribeResult, // // Use Format() to get a string out of it. type DescribeResult struct { + doNotCompare ptr *C.git_describe_result } diff --git a/diff.go b/diff.go index bc7f8bc7b..5ef3dc476 100644 --- a/diff.go +++ b/diff.go @@ -131,6 +131,7 @@ func diffLineFromC(line *C.git_diff_line) DiffLine { } type Diff struct { + doNotCompare ptr *C.git_diff repo *Repository runFinalizer bool @@ -218,6 +219,7 @@ func (diff *Diff) FindSimilar(opts *DiffFindOptions) error { } type DiffStats struct { + doNotCompare ptr *C.git_diff_stats } diff --git a/git.go b/git.go index 9e22231f5..413f15bf8 100644 --- a/git.go +++ b/git.go @@ -119,6 +119,10 @@ var ( ErrInvalid = errors.New("Invalid state for operation") ) +// doNotCompare is an idiomatic way of making structs non-comparable to avoid +// future field additions to make them non-comparable. +type doNotCompare [0]func() + var pointerHandles *HandleList func init() { diff --git a/handles.go b/handles.go index c0d4b3cbb..36ca1f047 100644 --- a/handles.go +++ b/handles.go @@ -11,6 +11,7 @@ import ( ) type HandleList struct { + doNotCompare sync.RWMutex // stores the Go pointers handles map[unsafe.Pointer]interface{} diff --git a/index.go b/index.go index 31f1a9633..501fc4888 100644 --- a/index.go +++ b/index.go @@ -52,6 +52,7 @@ const ( ) type Index struct { + doNotCompare ptr *C.git_index repo *Repository } @@ -108,7 +109,7 @@ func freeCIndexEntry(entry *C.git_index_entry) { } func newIndexFromC(ptr *C.git_index, repo *Repository) *Index { - idx := &Index{ptr, repo} + idx := &Index{ptr: ptr, repo: repo} runtime.SetFinalizer(idx, (*Index).Free) return idx } @@ -616,6 +617,7 @@ func (v *Index) RemoveConflict(path string) error { } type IndexConflictIterator struct { + doNotCompare ptr *C.git_index_conflict_iterator index *Index } diff --git a/indexer.go b/indexer.go index 601ac9ce5..53ac420c4 100644 --- a/indexer.go +++ b/indexer.go @@ -19,6 +19,7 @@ import ( // 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 diff --git a/mempack.go b/mempack.go index 39d706a1f..6d4119879 100644 --- a/mempack.go +++ b/mempack.go @@ -18,6 +18,7 @@ import ( // Mempack is a custom ODB backend that permits packing object in-memory. type Mempack struct { + doNotCompare ptr *C.git_odb_backend } diff --git a/merge.go b/merge.go index b9d551703..2df0706cf 100644 --- a/merge.go +++ b/merge.go @@ -17,6 +17,7 @@ import ( ) type AnnotatedCommit struct { + doNotCompare ptr *C.git_annotated_commit r *Repository } @@ -426,11 +427,12 @@ func (r *Repository) MergeBaseOctopus(oids []*Oid) (*Oid, error) { } type MergeFileResult struct { + doNotCompare + ptr *C.git_merge_file_result Automergeable bool Path string Mode uint Contents []byte - ptr *C.git_merge_file_result } func newMergeFileResultFromC(c *C.git_merge_file_result) *MergeFileResult { diff --git a/note.go b/note.go index 21bed57aa..a2583a9c9 100644 --- a/note.go +++ b/note.go @@ -13,6 +13,7 @@ import ( // This object represents the possible operations which can be // performed on the collection of notes for a repository. type NoteCollection struct { + doNotCompare repo *Repository } @@ -139,6 +140,7 @@ func (c *NoteCollection) DefaultRef() (string, error) { // Note type Note struct { + doNotCompare ptr *C.git_note r *Repository } @@ -189,6 +191,7 @@ func (n *Note) Message() string { // NoteIterator type NoteIterator struct { + doNotCompare ptr *C.git_note_iterator r *Repository } diff --git a/object.go b/object.go index 66e848fe0..32f7ca905 100644 --- a/object.go +++ b/object.go @@ -22,6 +22,7 @@ const ( ) type Object struct { + doNotCompare ptr *C.git_object repo *Repository } diff --git a/odb.go b/odb.go index 3415fae4a..165778f1e 100644 --- a/odb.go +++ b/odb.go @@ -22,10 +22,12 @@ import ( ) type Odb struct { + doNotCompare ptr *C.git_odb } type OdbBackend struct { + doNotCompare ptr *C.git_odb_backend } @@ -45,7 +47,7 @@ func NewOdb() (odb *Odb, err error) { } func NewOdbBackendFromC(ptr unsafe.Pointer) (backend *OdbBackend) { - backend = &OdbBackend{(*C.git_odb_backend)(ptr)} + backend = &OdbBackend{ptr: (*C.git_odb_backend)(ptr)} return backend } @@ -313,6 +315,7 @@ func (v *OdbBackend) Free() { } type OdbObject struct { + doNotCompare ptr *C.git_odb_object } @@ -356,6 +359,7 @@ func (object *OdbObject) Data() (data []byte) { } type OdbReadStream struct { + doNotCompare ptr *C.git_odb_stream Size uint64 Type ObjectType @@ -396,6 +400,7 @@ func (stream *OdbReadStream) Free() { } type OdbWriteStream struct { + doNotCompare ptr *C.git_odb_stream Id Oid } @@ -440,6 +445,7 @@ func (stream *OdbWriteStream) Free() { // 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 diff --git a/packbuilder.go b/packbuilder.go index 5d3a93375..0ec0fb1e5 100644 --- a/packbuilder.go +++ b/packbuilder.go @@ -16,6 +16,7 @@ import ( ) type Packbuilder struct { + doNotCompare ptr *C.git_packbuilder r *Repository } diff --git a/patch.go b/patch.go index 9cb93cb6e..d55fbffae 100644 --- a/patch.go +++ b/patch.go @@ -10,6 +10,7 @@ import ( ) type Patch struct { + doNotCompare ptr *C.git_patch } diff --git a/rebase.go b/rebase.go index 94c83403d..bf243e141 100644 --- a/rebase.go +++ b/rebase.go @@ -142,6 +142,7 @@ func mapEmptyStringToNull(ref string) *C.char { // Rebase is the struct representing a Rebase object. type Rebase struct { + doNotCompare ptr *C.git_rebase r *Repository options *C.git_rebase_options diff --git a/refdb.go b/refdb.go index 578f43cc0..a8b171580 100644 --- a/refdb.go +++ b/refdb.go @@ -13,11 +13,13 @@ import ( ) type Refdb struct { + doNotCompare ptr *C.git_refdb r *Repository } type RefdbBackend struct { + doNotCompare ptr *C.git_refdb_backend } @@ -38,7 +40,7 @@ func (v *Repository) NewRefdb() (refdb *Refdb, err error) { } func NewRefdbBackendFromC(ptr unsafe.Pointer) (backend *RefdbBackend) { - backend = &RefdbBackend{(*C.git_refdb_backend)(ptr)} + backend = &RefdbBackend{ptr: (*C.git_refdb_backend)(ptr)} return backend } diff --git a/reference.go b/reference.go index d15962ee4..53a641fc4 100644 --- a/reference.go +++ b/reference.go @@ -17,11 +17,13 @@ const ( ) type Reference struct { + doNotCompare ptr *C.git_reference repo *Repository } type ReferenceCollection struct { + doNotCompare repo *Repository } @@ -363,11 +365,13 @@ func (v *Reference) Free() { } type ReferenceIterator struct { + doNotCompare ptr *C.git_reference_iterator repo *Repository } type ReferenceNameIterator struct { + doNotCompare *ReferenceIterator } @@ -422,7 +426,7 @@ func (repo *Repository) NewReferenceIteratorGlob(glob string) (*ReferenceIterato } func (i *ReferenceIterator) Names() *ReferenceNameIterator { - return &ReferenceNameIterator{i} + return &ReferenceNameIterator{ReferenceIterator: i} } // NextName retrieves the next reference name. If the iteration is over, diff --git a/remote.go b/remote.go index 122c74db1..6e192d7a4 100644 --- a/remote.go +++ b/remote.go @@ -150,6 +150,7 @@ type ProxyOptions struct { } type Remote struct { + doNotCompare ptr *C.git_remote callbacks RemoteCallbacks repo *Repository @@ -498,6 +499,7 @@ func (r *Remote) Free() { } type RemoteCollection struct { + doNotCompare repo *Repository } diff --git a/repository.go b/repository.go index deaaef1bb..3f88a2025 100644 --- a/repository.go +++ b/repository.go @@ -14,6 +14,7 @@ import ( // Repository type Repository struct { + doNotCompare ptr *C.git_repository // Remotes represents the collection of remotes and can be // used to add, remove and configure remotes for this diff --git a/revparse.go b/revparse.go index 950932b8f..34e1fa39b 100644 --- a/revparse.go +++ b/revparse.go @@ -20,6 +20,7 @@ const ( ) type Revspec struct { + doNotCompare to *Object from *Object flags RevparseFlag diff --git a/stash.go b/stash.go index 3b1ae0c8f..65b61ee91 100644 --- a/stash.go +++ b/stash.go @@ -35,6 +35,7 @@ const ( // StashCollection represents the possible operations that can be // performed on the collection of stashes for a repository. type StashCollection struct { + doNotCompare repo *Repository } diff --git a/status.go b/status.go index 3923e1a45..ea521fa6f 100644 --- a/status.go +++ b/status.go @@ -55,6 +55,7 @@ func statusEntryFromC(statusEntry *C.git_status_entry) StatusEntry { } type StatusList struct { + doNotCompare ptr *C.git_status_list r *Repository } diff --git a/submodule.go b/submodule.go index 45a28ae1d..3551ef156 100644 --- a/submodule.go +++ b/submodule.go @@ -21,6 +21,7 @@ type SubmoduleUpdateOptions struct { // Submodule type Submodule struct { + doNotCompare ptr *C.git_submodule r *Repository } @@ -82,6 +83,7 @@ const ( ) type SubmoduleCollection struct { + doNotCompare repo *Repository } @@ -117,7 +119,7 @@ type submoduleCallbackData struct { //export submoduleCallback func submoduleCallback(csub unsafe.Pointer, name *C.char, handle unsafe.Pointer) C.int { - sub := &Submodule{(*C.git_submodule)(csub), nil} + sub := &Submodule{ptr: (*C.git_submodule)(csub)} data, ok := pointerHandles.Get(handle).(submoduleCallbackData) if !ok { diff --git a/tag.go b/tag.go index ba33e8256..0d63ea237 100644 --- a/tag.go +++ b/tag.go @@ -13,6 +13,7 @@ import ( // Tag type Tag struct { + doNotCompare Object cast_ptr *C.git_tag } @@ -64,6 +65,7 @@ func (t *Tag) TargetType() ObjectType { } type TagsCollection struct { + doNotCompare repo *Repository } diff --git a/tree.go b/tree.go index 38c5bdca7..14fe7e449 100644 --- a/tree.go +++ b/tree.go @@ -24,6 +24,7 @@ const ( ) type Tree struct { + doNotCompare Object cast_ptr *C.git_tree } @@ -167,6 +168,7 @@ func (t *Tree) Walk(callback TreeWalkCallback) error { } type TreeBuilder struct { + doNotCompare ptr *C.git_treebuilder repo *Repository } diff --git a/walk.go b/walk.go index 6020c97c3..77ec3a1b8 100644 --- a/walk.go +++ b/walk.go @@ -22,6 +22,7 @@ const ( ) type RevWalk struct { + doNotCompare ptr *C.git_revwalk repo *Repository } From 6fbd99e60f8bbe22f8c0e9dae5ca100334908f39 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 5 Sep 2021 16:03:45 -0700 Subject: [PATCH 094/103] Add support for custom smart transports (#806) (#809) This change adds support for git smart transports. This will be then used to implement http, https, and ssh transports that don't rely on the libgit2 library. (cherry picked from commit f1fa96c7b7f548389c7560d3a1a0bce83be56c9f) --- clone.go | 1 + git.go | 6 + git_test.go | 14 ++ remote.go | 110 +++++++++++- repository.go | 2 + transport.go | 430 ++++++++++++++++++++++++++++++++++++++++++++++ transport_test.go | 72 ++++++++ wrapper.c | 113 ++++++++++++ 8 files changed, 742 insertions(+), 6 deletions(-) create mode 100644 transport.go create mode 100644 transport_test.go diff --git a/clone.go b/clone.go index 5c9fa7ef3..f5c055f57 100644 --- a/clone.go +++ b/clone.go @@ -85,6 +85,7 @@ func remoteCreateCallback( // 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) } diff --git a/git.go b/git.go index 413f15bf8..b3a65c2b2 100644 --- a/git.go +++ b/git.go @@ -124,6 +124,7 @@ var ( type doNotCompare [0]func() var pointerHandles *HandleList +var remotePointers *remotePointerList func init() { initLibGit2() @@ -131,6 +132,7 @@ func init() { func initLibGit2() { pointerHandles = NewHandleList() + remotePointers = newRemotePointerList() C.git_libgit2_init() @@ -156,7 +158,11 @@ func initLibGit2() { // 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() } diff --git a/git_test.go b/git_test.go index 1c57f7914..101350fa6 100644 --- a/git_test.go +++ b/git_test.go @@ -13,6 +13,10 @@ import ( func TestMain(m *testing.M) { 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 { @@ -23,6 +27,16 @@ func TestMain(m *testing.M) { } 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) diff --git a/remote.go b/remote.go index 6e192d7a4..d02c315ac 100644 --- a/remote.go +++ b/remote.go @@ -14,6 +14,7 @@ import ( "reflect" "runtime" "strings" + "sync" "unsafe" ) @@ -156,6 +157,64 @@ type Remote struct { repo *Repository } +type remotePointerList struct { + sync.RWMutex + // stores the Go pointers + pointers map[*C.git_remote]*Remote +} + +func newRemotePointerList() *remotePointerList { + return &remotePointerList{ + pointers: make(map[*C.git_remote]*Remote), + } +} + +// track adds the given pointer to the list of pointers to track and +// returns a pointer value which can be passed to C as an opaque +// pointer. +func (v *remotePointerList) track(remote *Remote) { + v.Lock() + v.pointers[remote.ptr] = remote + v.Unlock() + + runtime.SetFinalizer(remote, (*Remote).Free) +} + +// untrack stops tracking the git_remote pointer. +func (v *remotePointerList) untrack(remote *Remote) { + v.Lock() + delete(v.pointers, remote.ptr) + v.Unlock() +} + +// clear stops tracking all the git_remote pointers. +func (v *remotePointerList) clear() { + v.Lock() + var remotes []*Remote + for remotePtr, remote := range v.pointers { + remotes = append(remotes, remote) + delete(v.pointers, remotePtr) + } + v.Unlock() + + for _, remote := range remotes { + remote.free() + } +} + +// get retrieves the pointer from the given *git_remote. +func (v *remotePointerList) get(ptr *C.git_remote) (*Remote, bool) { + v.RLock() + defer v.RUnlock() + + r, ok := v.pointers[ptr] + if !ok { + return nil, false + } + + return r, true +} + type CertificateKind uint const ( @@ -490,17 +549,42 @@ func RemoteIsValidName(name string) bool { return C.git_remote_is_valid_name(cname) == 1 } -// Free releases the resources of the Remote. -func (r *Remote) Free() { +// free releases the resources of the Remote. +func (r *Remote) free() { runtime.SetFinalizer(r, nil) C.git_remote_free(r.ptr) r.ptr = nil r.repo = nil } +// Free releases the resources of the Remote. +func (r *Remote) Free() { + r.repo.Remotes.untrackRemote(r) + r.free() +} + type RemoteCollection struct { doNotCompare repo *Repository + + sync.RWMutex + remotes map[*C.git_remote]*Remote +} + +func (c *RemoteCollection) trackRemote(r *Remote) { + c.Lock() + c.remotes[r.ptr] = r + c.Unlock() + + remotePointers.track(r) +} + +func (c *RemoteCollection) untrackRemote(r *Remote) { + c.Lock() + delete(c.remotes, r.ptr) + c.Unlock() + + remotePointers.untrack(r) } func (c *RemoteCollection) List() ([]string, error) { @@ -535,7 +619,7 @@ func (c *RemoteCollection) Create(name string, url string) (*Remote, error) { if ret < 0 { return nil, MakeGitError(ret) } - runtime.SetFinalizer(remote, (*Remote).Free) + c.trackRemote(remote) return remote, nil } @@ -571,7 +655,7 @@ func (c *RemoteCollection) CreateWithFetchspec(name string, url string, fetch st if ret < 0 { return nil, MakeGitError(ret) } - runtime.SetFinalizer(remote, (*Remote).Free) + c.trackRemote(remote) return remote, nil } @@ -588,7 +672,7 @@ func (c *RemoteCollection) CreateAnonymous(url string) (*Remote, error) { if ret < 0 { return nil, MakeGitError(ret) } - runtime.SetFinalizer(remote, (*Remote).Free) + c.trackRemote(remote) return remote, nil } @@ -605,10 +689,24 @@ func (c *RemoteCollection) Lookup(name string) (*Remote, error) { if ret < 0 { return nil, MakeGitError(ret) } - runtime.SetFinalizer(remote, (*Remote).Free) + c.trackRemote(remote) return remote, nil } +func (c *RemoteCollection) Free() { + var remotes []*Remote + c.Lock() + for remotePtr, remote := range c.remotes { + remotes = append(remotes, remote) + delete(c.remotes, remotePtr) + } + c.Unlock() + + for _, remote := range remotes { + remotePointers.untrack(remote) + } +} + func (o *Remote) Name() string { s := C.git_remote_name(o.ptr) runtime.KeepAlive(o) diff --git a/repository.go b/repository.go index 3f88a2025..316835d8f 100644 --- a/repository.go +++ b/repository.go @@ -46,6 +46,7 @@ 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 @@ -144,6 +145,7 @@ func (v *Repository) Free() { ptr := v.ptr v.ptr = nil runtime.SetFinalizer(v, nil) + v.Remotes.Free() if v.weak { return } diff --git a/transport.go b/transport.go new file mode 100644 index 000000000..8cdd8f728 --- /dev/null +++ b/transport.go @@ -0,0 +1,430 @@ +package git + +/* +#include +#include + +typedef struct { + git_smart_subtransport parent; + void *handle; +} _go_managed_smart_subtransport; + +typedef struct { + git_smart_subtransport_stream parent; + void *handle; +} _go_managed_smart_subtransport_stream; + +int _go_git_transport_register(const char *prefix, void *handle); +int _go_git_transport_smart(git_transport **out, git_remote *owner, int stateless, _go_managed_smart_subtransport *subtransport_payload); +void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stream *stream); +*/ +import "C" +import ( + "errors" + "fmt" + "io" + "reflect" + "runtime" + "sync" + "unsafe" +) + +var ( + // globalRegisteredSmartTransports is a mapping of global, git2go-managed + // transports. + globalRegisteredSmartTransports = struct { + sync.Mutex + transports map[string]*RegisteredSmartTransport + }{ + transports: make(map[string]*RegisteredSmartTransport), + } +) + +// unregisterManagedTransports unregisters all git2go-managed transports. +func unregisterManagedTransports() error { + globalRegisteredSmartTransports.Lock() + originalTransports := globalRegisteredSmartTransports.transports + globalRegisteredSmartTransports.transports = make(map[string]*RegisteredSmartTransport) + globalRegisteredSmartTransports.Unlock() + + var err error + for protocol, managed := range originalTransports { + unregisterErr := managed.Free() + if err == nil && unregisterErr != nil { + err = fmt.Errorf("failed to unregister transport for %q: %v", protocol, unregisterErr) + } + } + return err +} + +// SmartServiceAction is an action that the smart transport can ask a +// subtransport to perform. +type SmartServiceAction int + +const ( + // SmartServiceActionUploadpackLs is used upon connecting to a remote, and is + // used to perform reference discovery prior to performing a pull operation. + SmartServiceActionUploadpackLs SmartServiceAction = C.GIT_SERVICE_UPLOADPACK_LS + + // SmartServiceActionUploadpack is used when performing a pull operation. + SmartServiceActionUploadpack SmartServiceAction = C.GIT_SERVICE_UPLOADPACK + + // SmartServiceActionReceivepackLs is used upon connecting to a remote, and is + // used to perform reference discovery prior to performing a push operation. + SmartServiceActionReceivepackLs SmartServiceAction = C.GIT_SERVICE_RECEIVEPACK_LS + + // SmartServiceActionReceivepack is used when performing a push operation. + SmartServiceActionReceivepack SmartServiceAction = C.GIT_SERVICE_RECEIVEPACK +) + +// Transport encapsulates a way to communicate with a Remote. +type Transport struct { + doNotCompare + ptr *C.git_transport +} + +// SmartCredentials calls the credentials callback for this transport. +func (t *Transport) SmartCredentials(user string, methods CredType) (*Cred, error) { + cred := &Cred{} + var cstr *C.char + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if user != "" { + cstr = C.CString(user) + defer C.free(unsafe.Pointer(cstr)) + } + ret := C.git_transport_smart_credentials(&cred.ptr, t.ptr, cstr, C.int(methods)) + if ret != 0 { + return nil, MakeGitError(ret) + } + + return cred, nil +} + +// SmartSubtransport is the interface for custom subtransports which carry data +// for the smart transport. +type SmartSubtransport interface { + // Action creates a SmartSubtransportStream for the provided url and + // requested action. + Action(url string, action SmartServiceAction) (SmartSubtransportStream, error) + + // Close closes the SmartSubtransport. + // + // Subtransports are guaranteed a call to Close between + // calls to Action, except for the following two "natural" progressions + // of actions against a constant URL. + // + // 1. UPLOADPACK_LS -> UPLOADPACK + // 2. RECEIVEPACK_LS -> RECEIVEPACK + Close() error + + // Free releases the resources of the SmartSubtransport. + Free() +} + +// SmartSubtransportStream is the interface for streams used by the smart +// transport to read and write data from a subtransport. +type SmartSubtransportStream interface { + io.Reader + io.Writer + + // Free releases the resources of the SmartSubtransportStream. + Free() +} + +// SmartSubtransportCallback is a function which creates a new subtransport for +// the smart transport. +type SmartSubtransportCallback func(remote *Remote, transport *Transport) (SmartSubtransport, error) + +// RegisteredSmartTransport represents a transport that has been registered. +type RegisteredSmartTransport struct { + doNotCompare + name string + stateless bool + callback SmartSubtransportCallback + handle unsafe.Pointer +} + +// NewRegisteredSmartTransport adds a custom transport definition, to be used +// in addition to the built-in set of transports that come with libgit2. +func NewRegisteredSmartTransport( + name string, + stateless bool, + callback SmartSubtransportCallback, +) (*RegisteredSmartTransport, error) { + return newRegisteredSmartTransport(name, stateless, callback, false) +} + +func newRegisteredSmartTransport( + name string, + stateless bool, + callback SmartSubtransportCallback, + global bool, +) (*RegisteredSmartTransport, error) { + if !global { + // Check if we had already registered a smart transport for this protocol. If + // we had, free it. The user is now responsible for this transport for the + // lifetime of the library. + globalRegisteredSmartTransports.Lock() + if managed, ok := globalRegisteredSmartTransports.transports[name]; ok { + delete(globalRegisteredSmartTransports.transports, name) + globalRegisteredSmartTransports.Unlock() + + err := managed.Free() + if err != nil { + return nil, err + } + } else { + globalRegisteredSmartTransports.Unlock() + } + } + + cstr := C.CString(name) + defer C.free(unsafe.Pointer(cstr)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + registeredSmartTransport := &RegisteredSmartTransport{ + name: name, + stateless: stateless, + callback: callback, + } + registeredSmartTransport.handle = pointerHandles.Track(registeredSmartTransport) + + ret := C._go_git_transport_register(cstr, registeredSmartTransport.handle) + if ret != 0 { + pointerHandles.Untrack(registeredSmartTransport.handle) + return nil, MakeGitError(ret) + } + + runtime.SetFinalizer(registeredSmartTransport, (*RegisteredSmartTransport).Free) + return registeredSmartTransport, nil +} + +// Free releases all resources used by the RegisteredSmartTransport and +// unregisters the custom transport definition referenced by it. +func (t *RegisteredSmartTransport) Free() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cstr := C.CString(t.name) + defer C.free(unsafe.Pointer(cstr)) + + if ret := C.git_transport_unregister(cstr); ret < 0 { + return MakeGitError(ret) + } + + pointerHandles.Untrack(t.handle) + runtime.SetFinalizer(t, nil) + t.handle = nil + return nil +} + +//export smartTransportCallback +func smartTransportCallback( + errorMessage **C.char, + out **C.git_transport, + owner *C.git_remote, + handle unsafe.Pointer, +) C.int { + registeredSmartTransport := pointerHandles.Get(handle).(*RegisteredSmartTransport) + remote, ok := remotePointers.get(owner) + if !ok { + err := errors.New("remote pointer not found") + return setCallbackError(errorMessage, err) + } + + managed := &managedSmartSubtransport{ + remote: remote, + callback: registeredSmartTransport.callback, + subtransport: (*C._go_managed_smart_subtransport)(C.calloc(1, C.size_t(unsafe.Sizeof(C._go_managed_smart_subtransport{})))), + } + managedHandle := pointerHandles.Track(managed) + managed.handle = managedHandle + managed.subtransport.handle = managedHandle + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C._go_git_transport_smart(out, owner, cbool(registeredSmartTransport.stateless), managed.subtransport) + if ret != 0 { + pointerHandles.Untrack(managedHandle) + } + return ret +} + +//export smartTransportSubtransportCallback +func smartTransportSubtransportCallback( + errorMessage **C.char, + wrapperPtr *C._go_managed_smart_subtransport, + owner *C.git_transport, +) C.int { + subtransport := pointerHandles.Get(wrapperPtr.handle).(*managedSmartSubtransport) + + underlyingSmartSubtransport, err := subtransport.callback(subtransport.remote, &Transport{ptr: owner}) + if err != nil { + return setCallbackError(errorMessage, err) + } + subtransport.underlying = underlyingSmartSubtransport + return C.int(ErrorCodeOK) +} + +type managedSmartSubtransport struct { + owner *C.git_transport + callback SmartSubtransportCallback + remote *Remote + subtransport *C._go_managed_smart_subtransport + underlying SmartSubtransport + handle unsafe.Pointer + currentManagedStream *managedSmartSubtransportStream +} + +func getSmartSubtransportInterface(subtransport *C.git_smart_subtransport) *managedSmartSubtransport { + wrapperPtr := (*C._go_managed_smart_subtransport)(unsafe.Pointer(subtransport)) + return pointerHandles.Get(wrapperPtr.handle).(*managedSmartSubtransport) +} + +//export smartSubtransportActionCallback +func smartSubtransportActionCallback( + errorMessage **C.char, + out **C.git_smart_subtransport_stream, + t *C.git_smart_subtransport, + url *C.char, + action C.git_smart_service_t, +) C.int { + subtransport := getSmartSubtransportInterface(t) + + underlyingStream, err := subtransport.underlying.Action(C.GoString(url), SmartServiceAction(action)) + if err != nil { + return setCallbackError(errorMessage, err) + } + + // It's okay to do strict equality here: we expect both to be identical. + if subtransport.currentManagedStream == nil || subtransport.currentManagedStream.underlying != underlyingStream { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + stream := (*C._go_managed_smart_subtransport_stream)(C.calloc(1, C.size_t(unsafe.Sizeof(C._go_managed_smart_subtransport_stream{})))) + managed := &managedSmartSubtransportStream{ + underlying: underlyingStream, + streamPtr: stream, + } + managedHandle := pointerHandles.Track(managed) + managed.handle = managedHandle + stream.handle = managedHandle + + C._go_git_setup_smart_subtransport_stream(stream) + + subtransport.currentManagedStream = managed + } + + *out = &subtransport.currentManagedStream.streamPtr.parent + return C.int(ErrorCodeOK) +} + +//export smartSubtransportCloseCallback +func smartSubtransportCloseCallback(errorMessage **C.char, t *C.git_smart_subtransport) C.int { + subtransport := getSmartSubtransportInterface(t) + + subtransport.currentManagedStream = nil + + if subtransport.underlying != nil { + err := subtransport.underlying.Close() + if err != nil { + return setCallbackError(errorMessage, err) + } + } + + return C.int(ErrorCodeOK) +} + +//export smartSubtransportFreeCallback +func smartSubtransportFreeCallback(t *C.git_smart_subtransport) { + subtransport := getSmartSubtransportInterface(t) + + if subtransport.underlying != nil { + subtransport.underlying.Free() + subtransport.underlying = nil + } + pointerHandles.Untrack(subtransport.handle) + C.free(unsafe.Pointer(subtransport.subtransport)) + subtransport.handle = nil + subtransport.subtransport = nil +} + +type managedSmartSubtransportStream struct { + owner *C.git_smart_subtransport_stream + streamPtr *C._go_managed_smart_subtransport_stream + underlying SmartSubtransportStream + handle unsafe.Pointer +} + +func getSmartSubtransportStreamInterface(subtransportStream *C.git_smart_subtransport_stream) *managedSmartSubtransportStream { + managedSubtransportStream := (*C._go_managed_smart_subtransport_stream)(unsafe.Pointer(subtransportStream)) + return pointerHandles.Get(managedSubtransportStream.handle).(*managedSmartSubtransportStream) +} + +//export smartSubtransportStreamReadCallback +func smartSubtransportStreamReadCallback( + errorMessage **C.char, + s *C.git_smart_subtransport_stream, + buffer *C.char, + bufSize C.size_t, + bytesRead *C.size_t, +) C.int { + stream := getSmartSubtransportStreamInterface(s) + + var p []byte + header := (*reflect.SliceHeader)(unsafe.Pointer(&p)) + header.Cap = int(bufSize) + header.Len = int(bufSize) + header.Data = uintptr(unsafe.Pointer(buffer)) + + n, err := stream.underlying.Read(p) + *bytesRead = C.size_t(n) + if n == 0 && err != nil { + if err == io.EOF { + return C.int(ErrorCodeOK) + } + + return setCallbackError(errorMessage, err) + } + + return C.int(ErrorCodeOK) +} + +//export smartSubtransportStreamWriteCallback +func smartSubtransportStreamWriteCallback( + errorMessage **C.char, + s *C.git_smart_subtransport_stream, + buffer *C.char, + bufLen C.size_t, +) C.int { + stream := getSmartSubtransportStreamInterface(s) + + var p []byte + header := (*reflect.SliceHeader)(unsafe.Pointer(&p)) + header.Cap = int(bufLen) + header.Len = int(bufLen) + header.Data = uintptr(unsafe.Pointer(buffer)) + + if _, err := stream.underlying.Write(p); err != nil { + return setCallbackError(errorMessage, err) + } + + return C.int(ErrorCodeOK) +} + +//export smartSubtransportStreamFreeCallback +func smartSubtransportStreamFreeCallback(s *C.git_smart_subtransport_stream) { + stream := getSmartSubtransportStreamInterface(s) + + stream.underlying.Free() + pointerHandles.Untrack(stream.handle) + C.free(unsafe.Pointer(stream.streamPtr)) + stream.handle = nil + stream.streamPtr = nil +} diff --git a/transport_test.go b/transport_test.go new file mode 100644 index 000000000..9f50047ca --- /dev/null +++ b/transport_test.go @@ -0,0 +1,72 @@ +package git + +import ( + "io" + "reflect" + "testing" +) + +type testSmartSubtransport struct { +} + +func (t *testSmartSubtransport) Action(url string, action SmartServiceAction) (SmartSubtransportStream, error) { + return &testSmartSubtransportStream{}, nil +} + +func (t *testSmartSubtransport) Close() error { + return nil +} + +func (t *testSmartSubtransport) Free() { +} + +type testSmartSubtransportStream struct { +} + +func (s *testSmartSubtransportStream) Read(buf []byte) (int, error) { + payload := "" + + "001e# service=git-upload-pack\n" + + "0000005d0000000000000000000000000000000000000000 HEAD\x00symref=HEAD:refs/heads/master agent=libgit\n" + + "003f0000000000000000000000000000000000000000 refs/heads/master\n" + + "0000" + + return copy(buf, []byte(payload)), io.EOF +} + +func (s *testSmartSubtransportStream) Write(buf []byte) (int, error) { + return 0, io.EOF +} + +func (s *testSmartSubtransportStream) Free() { +} + +func TestTransport(t *testing.T) { + t.Parallel() + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + callback := func(remote *Remote, transport *Transport) (SmartSubtransport, error) { + return &testSmartSubtransport{}, nil + } + registeredSmartTransport, err := NewRegisteredSmartTransport("foo", true, callback) + checkFatal(t, err) + defer registeredSmartTransport.Free() + + remote, err := repo.Remotes.Create("test", "foo://bar") + checkFatal(t, err) + defer remote.Free() + + err = remote.ConnectFetch(nil, nil, nil) + checkFatal(t, err) + + remoteHeads, err := remote.Ls() + checkFatal(t, err) + + expectedRemoteHeads := []RemoteHead{ + {&Oid{}, "HEAD"}, + {&Oid{}, "refs/heads/master"}, + } + if !reflect.DeepEqual(expectedRemoteHeads, remoteHeads) { + t.Errorf("mismatched remote heads. expected %v, got %v", expectedRemoteHeads, remoteHeads) + } +} diff --git a/wrapper.c b/wrapper.c index 315a43e1d..c65f6740b 100644 --- a/wrapper.c +++ b/wrapper.c @@ -467,3 +467,116 @@ int _go_git_indexer_new( { return git_indexer_new(out, path, mode, odb, transfer_progress_callback, progress_cb_payload); } + +static int smart_transport_callback( + git_transport **out, + git_remote *owner, + void *param) +{ + char *error_message = NULL; + const int ret = smartTransportCallback( + &error_message, + out, + owner, + param); + return set_callback_error(error_message, ret); +} + +int _go_git_transport_register(const char *prefix, void *param) +{ + return git_transport_register(prefix, smart_transport_callback, param); +} + +static int smart_subtransport_action_callback( + git_smart_subtransport_stream **out, + git_smart_subtransport *transport, + const char *url, + git_smart_service_t action) +{ + char *error_message = NULL; + const int ret = smartSubtransportActionCallback( + &error_message, + out, + transport, + (char *)url, + action); + return set_callback_error(error_message, ret); +} + +static int smart_subtransport_close_callback(git_smart_subtransport *transport) +{ + char *error_message = NULL; + const int ret = smartSubtransportCloseCallback( + &error_message, + transport); + return set_callback_error(error_message, ret); +} + +static int smart_subtransport_callback( + git_smart_subtransport **out, + git_transport *owner, + void *param) +{ + _go_managed_smart_subtransport *subtransport = (_go_managed_smart_subtransport *)param; + subtransport->parent.action = smart_subtransport_action_callback; + subtransport->parent.close = smart_subtransport_close_callback; + subtransport->parent.free = smartSubtransportFreeCallback; + + *out = &subtransport->parent; + char *error_message = NULL; + const int ret = smartTransportSubtransportCallback(&error_message, subtransport, owner); + return set_callback_error(error_message, ret); +} + +int _go_git_transport_smart( + git_transport **out, + git_remote *owner, + int stateless, + _go_managed_smart_subtransport *subtransport_payload) +{ + git_smart_subtransport_definition definition = { + smart_subtransport_callback, + stateless, + subtransport_payload, + }; + + return git_transport_smart(out, owner, &definition); +} + +static int smart_subtransport_stream_read_callback( + git_smart_subtransport_stream *stream, + char *buffer, + size_t buf_size, + size_t *bytes_read) +{ + char *error_message = NULL; + const int ret = smartSubtransportStreamReadCallback( + &error_message, + stream, + buffer, + buf_size, + bytes_read); + return set_callback_error(error_message, ret); +} + +static int smart_subtransport_stream_write_callback( + git_smart_subtransport_stream *stream, + const char *buffer, + size_t len) +{ + char *error_message = NULL; + const int ret = smartSubtransportStreamWriteCallback( + &error_message, + stream, + (char *)buffer, + len); + return set_callback_error(error_message, ret); +} + +void _go_git_setup_smart_subtransport_stream(_go_managed_smart_subtransport_stream *stream) +{ + _go_managed_smart_subtransport_stream *managed_stream = (_go_managed_smart_subtransport_stream *)stream; + managed_stream->parent.read = smart_subtransport_stream_read_callback; + managed_stream->parent.write = smart_subtransport_stream_write_callback; + managed_stream->parent.free = smartSubtransportStreamFreeCallback; +} From 29eaaff9fdecdcce5a2d7b4ead13f7e79374004c Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 5 Sep 2021 17:01:02 -0700 Subject: [PATCH 095/103] Add support for managed HTTP/S transports (#810) (#813) This change uses the newly-exposed Transport interface to use Go's implementation of http.Client instead of httpclient via libgit2. (cherry picked from commit b983e1daebf528443e2a3954cd595fa3664ec93f) --- credentials.go | 25 +++++ git.go | 20 ++-- git_test.go | 4 + http.go | 240 ++++++++++++++++++++++++++++++++++++++++ remote.go | 7 ++ remote_test.go | 24 ++++ script/build-libgit2.sh | 1 + transport.go | 61 ++++++++++ 8 files changed, 375 insertions(+), 7 deletions(-) create mode 100644 http.go diff --git a/credentials.go b/credentials.go index d2311625e..d7b9fcd51 100644 --- a/credentials.go +++ b/credentials.go @@ -8,6 +8,8 @@ void _go_git_populate_credential_ssh_custom(git_cred_ssh_custom *cred); import "C" import ( "crypto/rand" + "errors" + "runtime" "unsafe" "golang.org/x/crypto/ssh" @@ -42,6 +44,29 @@ func credFromC(ptr *C.git_cred) *Cred { return &Cred{ptr: ptr} } +// GetUserpassPlaintext returns the plaintext username/password combination stored in the Cred. +func (o *Cred) GetUserpassPlaintext() (username, password string, err error) { + if o.Type() != CredTypeUserpassPlaintext { + err = errors.New("credential is not userpass plaintext") + return + } + + plaintextCredPtr := (*C.git_cred_userpass_plaintext)(unsafe.Pointer(o.ptr)) + username = C.GoString(plaintextCredPtr.username) + password = C.GoString(plaintextCredPtr.password) + return +} + +func NewCredUsername(username string) (int, Cred) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cred := Cred{} + cusername := C.CString(username) + ret := C.git_cred_username_new(&cred.ptr, cusername) + return int(ret), cred +} + func NewCredUserpassPlaintext(username string, password string) (int, Cred) { cred := Cred{} cusername := C.CString(username) diff --git a/git.go b/git.go index b3a65c2b2..758d5c9b1 100644 --- a/git.go +++ b/git.go @@ -135,22 +135,28 @@ func initLibGit2() { remotePointers = newRemotePointerList() C.git_libgit2_init() + features := Features() // Due to the multithreaded nature of Go and its interaction with // calling C functions, we cannot work with a library that was not built // with multi-threading support. The most likely outcome is a segfault // or panic at an incomprehensible time, so let's make it easy by // panicking right here. - if Features()&FeatureThreads == 0 { + if features&FeatureThreads == 0 { panic("libgit2 was not built with threading support") } - // This is not something we should be doing, as we may be - // stomping all over someone else's setup. The user should do - // this themselves or use some binding/wrapper which does it - // in such a way that they can be sure they're the only ones - // setting it up. - C.git_openssl_set_locking() + if features&FeatureHTTPS == 0 { + if err := registerManagedHTTP(); err != nil { + panic(err) + } + } else { + // This is not something we should be doing, as we may be stomping all over + // someone else's setup. The user should do this themselves or use some + // binding/wrapper which does it in such a way that they can be sure + // they're the only ones setting it up. + C.git_openssl_set_locking() + } } // Shutdown frees all the resources acquired by libgit2. Make sure no diff --git a/git_test.go b/git_test.go index 101350fa6..592e06fe5 100644 --- a/git_test.go +++ b/git_test.go @@ -11,6 +11,10 @@ import ( ) func TestMain(m *testing.M) { + if err := registerManagedHTTP(); err != nil { + panic(err) + } + ret := m.Run() if err := unregisterManagedTransports(); err != nil { diff --git a/http.go b/http.go new file mode 100644 index 000000000..69a64a096 --- /dev/null +++ b/http.go @@ -0,0 +1,240 @@ +package git + +import ( + "errors" + "fmt" + "io" + "net/http" + "net/url" + "sync" +) + +// RegisterManagedHTTPTransport registers a Go-native implementation of an +// HTTP/S transport that doesn't rely on any system libraries (e.g. +// libopenssl/libmbedtls). +// +// If Shutdown or ReInit are called, make sure that the smart transports are +// freed before it. +func RegisterManagedHTTPTransport(protocol string) (*RegisteredSmartTransport, error) { + return NewRegisteredSmartTransport(protocol, true, httpSmartSubtransportFactory) +} + +func registerManagedHTTP() error { + globalRegisteredSmartTransports.Lock() + defer globalRegisteredSmartTransports.Unlock() + + for _, protocol := range []string{"http", "https"} { + if _, ok := globalRegisteredSmartTransports.transports[protocol]; ok { + continue + } + managed, err := newRegisteredSmartTransport(protocol, true, httpSmartSubtransportFactory, true) + if err != nil { + return fmt.Errorf("failed to register transport for %q: %v", protocol, err) + } + globalRegisteredSmartTransports.transports[protocol] = managed + } + return nil +} + +func httpSmartSubtransportFactory(remote *Remote, transport *Transport) (SmartSubtransport, error) { + var proxyFn func(*http.Request) (*url.URL, error) + proxyOpts, err := transport.SmartProxyOptions() + if err != nil { + return nil, err + } + switch proxyOpts.Type { + case ProxyTypeNone: + proxyFn = nil + case ProxyTypeAuto: + proxyFn = http.ProxyFromEnvironment + case ProxyTypeSpecified: + parsedUrl, err := url.Parse(proxyOpts.Url) + if err != nil { + return nil, err + } + + proxyFn = http.ProxyURL(parsedUrl) + } + + return &httpSmartSubtransport{ + transport: transport, + client: &http.Client{ + Transport: &http.Transport{ + Proxy: proxyFn, + }, + }, + }, nil +} + +type httpSmartSubtransport struct { + transport *Transport + client *http.Client +} + +func (t *httpSmartSubtransport) Action(url string, action SmartServiceAction) (SmartSubtransportStream, error) { + var req *http.Request + var err error + switch action { + case SmartServiceActionUploadpackLs: + req, err = http.NewRequest("GET", url+"/info/refs?service=git-upload-pack", nil) + + case SmartServiceActionUploadpack: + req, err = http.NewRequest("POST", url+"/git-upload-pack", nil) + if err != nil { + break + } + req.Header.Set("Content-Type", "application/x-git-upload-pack-request") + + case SmartServiceActionReceivepackLs: + req, err = http.NewRequest("GET", url+"/info/refs?service=git-receive-pack", nil) + + case SmartServiceActionReceivepack: + req, err = http.NewRequest("POST", url+"/info/refs?service=git-upload-pack", nil) + if err != nil { + break + } + req.Header.Set("Content-Type", "application/x-git-receive-pack-request") + + default: + err = errors.New("unknown action") + } + + if err != nil { + return nil, err + } + + req.Header.Set("User-Agent", "git/2.0 (git2go)") + + stream := newManagedHttpStream(t, req) + if req.Method == "POST" { + stream.recvReply.Add(1) + stream.sendRequestBackground() + } + + return stream, nil +} + +func (t *httpSmartSubtransport) Close() error { + return nil +} + +func (t *httpSmartSubtransport) Free() { + t.client = nil +} + +type httpSmartSubtransportStream struct { + owner *httpSmartSubtransport + req *http.Request + resp *http.Response + reader *io.PipeReader + writer *io.PipeWriter + sentRequest bool + recvReply sync.WaitGroup + httpError error +} + +func newManagedHttpStream(owner *httpSmartSubtransport, req *http.Request) *httpSmartSubtransportStream { + r, w := io.Pipe() + return &httpSmartSubtransportStream{ + owner: owner, + req: req, + reader: r, + writer: w, + } +} + +func (self *httpSmartSubtransportStream) Read(buf []byte) (int, error) { + if !self.sentRequest { + self.recvReply.Add(1) + if err := self.sendRequest(); err != nil { + return 0, err + } + } + + if err := self.writer.Close(); err != nil { + return 0, err + } + + self.recvReply.Wait() + + if self.httpError != nil { + return 0, self.httpError + } + + return self.resp.Body.Read(buf) +} + +func (self *httpSmartSubtransportStream) Write(buf []byte) (int, error) { + if self.httpError != nil { + return 0, self.httpError + } + return self.writer.Write(buf) +} + +func (self *httpSmartSubtransportStream) Free() { + if self.resp != nil { + self.resp.Body.Close() + } +} + +func (self *httpSmartSubtransportStream) sendRequestBackground() { + go func() { + self.httpError = self.sendRequest() + }() + self.sentRequest = true +} + +func (self *httpSmartSubtransportStream) sendRequest() error { + defer self.recvReply.Done() + self.resp = nil + + var resp *http.Response + var err error + var userName string + var password string + for { + req := &http.Request{ + Method: self.req.Method, + URL: self.req.URL, + Header: self.req.Header, + } + if req.Method == "POST" { + req.Body = self.reader + req.ContentLength = -1 + } + + req.SetBasicAuth(userName, password) + resp, err = http.DefaultClient.Do(req) + if err != nil { + return err + } + + if resp.StatusCode == http.StatusOK { + break + } + + if resp.StatusCode == http.StatusUnauthorized { + resp.Body.Close() + + cred, err := self.owner.transport.SmartCredentials("", CredTypeUserpassPlaintext) + if err != nil { + return err + } + + userName, password, err = cred.GetUserpassPlaintext() + if err != nil { + return err + } + + continue + } + + // Any other error we treat as a hard error and punt back to the caller + resp.Body.Close() + return fmt.Errorf("Unhandled HTTP error %s", resp.Status) + } + + self.sentRequest = true + self.resp = resp + return nil +} diff --git a/remote.go b/remote.go index d02c315ac..10eab2bc1 100644 --- a/remote.go +++ b/remote.go @@ -150,6 +150,13 @@ type ProxyOptions struct { Url string } +func proxyOptionsFromC(copts *C.git_proxy_options) *ProxyOptions { + return &ProxyOptions{ + Type: ProxyType(copts._type), + Url: C.GoString(copts.url), + } +} + type Remote struct { doNotCompare ptr *C.git_remote diff --git a/remote_test.go b/remote_test.go index ccd436a8e..5f64b42e0 100644 --- a/remote_test.go +++ b/remote_test.go @@ -209,6 +209,30 @@ func TestRemotePrune(t *testing.T) { } } +func TestRemoteCredentialsCalled(t *testing.T) { + t.Parallel() + + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + + remote, err := repo.Remotes.CreateAnonymous("https://github.com/libgit2/non-existent") + checkFatal(t, err) + defer remote.Free() + + fetchOpts := FetchOptions{ + RemoteCallbacks: RemoteCallbacks{ + CredentialsCallback: func(url, username string, allowedTypes CredType) (ErrorCode, *Cred) { + return ErrorCodeUser, nil + }, + }, + } + + err = remote.Fetch(nil, &fetchOpts, "fetch") + if IsErrorCode(err, ErrorCodeUser) { + t.Fatalf("remote.Fetch() = %v, want %v", err, ErrorCodeUser) + } +} + func newChannelPipe(t *testing.T, w io.Writer, wg *sync.WaitGroup) (*os.File, error) { pr, pw, err := os.Pipe() if err != nil { diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh index d57c5a5f7..11d956d05 100755 --- a/script/build-libgit2.sh +++ b/script/build-libgit2.sh @@ -58,6 +58,7 @@ cd "${BUILD_PATH}/build" && cmake -DTHREADSAFE=ON \ -DBUILD_CLAR=OFF \ -DBUILD_SHARED_LIBS"=${BUILD_SHARED_LIBS}" \ + -DUSE_HTTPS=OFF \ -DCMAKE_C_FLAGS=-fPIC \ -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ -DCMAKE_INSTALL_PREFIX="${BUILD_INSTALL_PREFIX}" \ diff --git a/transport.go b/transport.go index 8cdd8f728..c89835435 100644 --- a/transport.go +++ b/transport.go @@ -1,6 +1,8 @@ package git /* +#include + #include #include @@ -83,6 +85,19 @@ type Transport struct { ptr *C.git_transport } +// SmartProxyOptions gets a copy of the proxy options for this transport. +func (t *Transport) SmartProxyOptions() (*ProxyOptions, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var cpopts C.git_proxy_options + if ret := C.git_transport_smart_proxy_options(&cpopts, t.ptr); ret < 0 { + return nil, MakeGitError(ret) + } + + return proxyOptionsFromC(&cpopts), nil +} + // SmartCredentials calls the credentials callback for this transport. func (t *Transport) SmartCredentials(user string, methods CredType) (*Cred, error) { cred := &Cred{} @@ -103,6 +118,52 @@ func (t *Transport) SmartCredentials(user string, methods CredType) (*Cred, erro return cred, nil } +// SmartCertificateCheck calls the certificate check for this transport. +func (t *Transport) SmartCertificateCheck(cert *Certificate, valid bool, hostname string) error { + var ccert *C.git_cert + switch cert.Kind { + case CertificateHostkey: + chostkeyCert := C.git_cert_hostkey{ + parent: C.git_cert{ + cert_type: C.GIT_CERT_HOSTKEY_LIBSSH2, + }, + _type: C.git_cert_ssh_t(cert.Kind), + } + C.memcpy(unsafe.Pointer(&chostkeyCert.hash_md5[0]), unsafe.Pointer(&cert.Hostkey.HashMD5[0]), C.size_t(len(cert.Hostkey.HashMD5))) + C.memcpy(unsafe.Pointer(&chostkeyCert.hash_sha1[0]), unsafe.Pointer(&cert.Hostkey.HashSHA1[0]), C.size_t(len(cert.Hostkey.HashSHA1))) + ccert = (*C.git_cert)(unsafe.Pointer(&chostkeyCert)) + + case CertificateX509: + cx509Cert := C.git_cert_x509{ + parent: C.git_cert{ + cert_type: C.GIT_CERT_X509, + }, + len: C.size_t(len(cert.X509.Raw)), + data: C.CBytes(cert.X509.Raw), + } + defer C.free(cx509Cert.data) + ccert = (*C.git_cert)(unsafe.Pointer(&cx509Cert)) + } + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + chostname := C.CString(hostname) + defer C.free(unsafe.Pointer(chostname)) + + cvalid := C.int(0) + if valid { + cvalid = C.int(1) + } + + ret := C.git_transport_smart_certificate_check(t.ptr, ccert, cvalid, chostname) + if ret != 0 { + return MakeGitError(ret) + } + + return nil +} + // SmartSubtransport is the interface for custom subtransports which carry data // for the smart transport. type SmartSubtransport interface { From e434188460e222070aac95bb820d8d1c92151dd9 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 5 Sep 2021 18:27:52 -0700 Subject: [PATCH 096/103] Add support for managed SSH transport #minor (#814) (#817) This change drops the (hard) dependency on libssh2 and instead uses Go's implementation of SSH when libgit2 is not built with it. --- credentials.go | 20 +++- git.go | 5 + remote.go | 23 ++-- script/build-libgit2.sh | 1 + ssh.go | 237 ++++++++++++++++++++++++++++++++++++++++ wrapper.c | 5 + 6 files changed, 279 insertions(+), 12 deletions(-) create mode 100644 ssh.go diff --git a/credentials.go b/credentials.go index d7b9fcd51..79ed37fb5 100644 --- a/credentials.go +++ b/credentials.go @@ -9,7 +9,7 @@ import "C" import ( "crypto/rand" "errors" - "runtime" + "fmt" "unsafe" "golang.org/x/crypto/ssh" @@ -57,10 +57,22 @@ func (o *Cred) GetUserpassPlaintext() (username, password string, err error) { return } -func NewCredUsername(username string) (int, Cred) { - runtime.LockOSThread() - defer runtime.UnlockOSThread() +// GetSSHKey returns the SSH-specific key information from the Cred object. +func (o *Cred) GetSSHKey() (username, publickey, privatekey, passphrase string, err error) { + if o.Type() != CredTypeSshKey { + err = fmt.Errorf("credential is not an SSH key: %v", o.Type()) + return + } + sshKeyCredPtr := (*C.git_cred_ssh_key)(unsafe.Pointer(o.ptr)) + username = C.GoString(sshKeyCredPtr.username) + publickey = C.GoString(sshKeyCredPtr.publickey) + privatekey = C.GoString(sshKeyCredPtr.privatekey) + passphrase = C.GoString(sshKeyCredPtr.passphrase) + return +} + +func NewCredUsername(username string) (int, Cred) { cred := Cred{} cusername := C.CString(username) ret := C.git_cred_username_new(&cred.ptr, cusername) diff --git a/git.go b/git.go index 758d5c9b1..5796cdfac 100644 --- a/git.go +++ b/git.go @@ -157,6 +157,11 @@ func initLibGit2() { // 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 diff --git a/remote.go b/remote.go index 10eab2bc1..7ae7e0628 100644 --- a/remote.go +++ b/remote.go @@ -16,6 +16,8 @@ import ( "strings" "sync" "unsafe" + + "golang.org/x/crypto/ssh" ) type TransferProgress struct { @@ -239,20 +241,25 @@ type Certificate struct { Hostkey HostkeyCertificate } +// HostkeyKind is a bitmask of the available hashes in HostkeyCertificate. type HostkeyKind uint const ( - HostkeyMD5 HostkeyKind = C.GIT_CERT_SSH_MD5 - HostkeySHA1 HostkeyKind = C.GIT_CERT_SSH_SHA1 + HostkeyMD5 HostkeyKind = C.GIT_CERT_SSH_MD5 + HostkeySHA1 HostkeyKind = C.GIT_CERT_SSH_SHA1 + HostkeySHA256 HostkeyKind = 1 << 2 + HostkeyRaw HostkeyKind = 1 << 3 ) -// Server host key information. If Kind is HostkeyMD5 the MD5 field -// will be filled. If Kind is HostkeySHA1, then HashSHA1 will be -// filled. +// Server host key information. A bitmask containing the available fields. +// Check for combinations of: HostkeyMD5, HostkeySHA1, HostkeySHA256, HostkeyRaw. type HostkeyCertificate struct { - Kind HostkeyKind - HashMD5 [16]byte - HashSHA1 [20]byte + Kind HostkeyKind + HashMD5 [16]byte + HashSHA1 [20]byte + HashSHA256 [32]byte + Hostkey []byte + SSHPublicKey ssh.PublicKey } type PushOptions struct { diff --git a/script/build-libgit2.sh b/script/build-libgit2.sh index 11d956d05..4dd57f629 100755 --- a/script/build-libgit2.sh +++ b/script/build-libgit2.sh @@ -59,6 +59,7 @@ cmake -DTHREADSAFE=ON \ -DBUILD_CLAR=OFF \ -DBUILD_SHARED_LIBS"=${BUILD_SHARED_LIBS}" \ -DUSE_HTTPS=OFF \ + -DUSE_SSH=OFF \ -DCMAKE_C_FLAGS=-fPIC \ -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ -DCMAKE_INSTALL_PREFIX="${BUILD_INSTALL_PREFIX}" \ diff --git a/ssh.go b/ssh.go new file mode 100644 index 000000000..65dfbcfaf --- /dev/null +++ b/ssh.go @@ -0,0 +1,237 @@ +package git + +/* +#include + +void _go_git_credential_free(git_cred *cred); +*/ +import "C" +import ( + "crypto/md5" + "crypto/sha1" + "crypto/sha256" + "errors" + "fmt" + "io" + "io/ioutil" + "net" + "net/url" + "runtime" + "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 + } + + 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 %q", u.Path) + + case SmartServiceActionReceivepackLs, SmartServiceActionReceivepack: + if t.currentStream != nil { + if t.lastAction == SmartServiceActionReceivepackLs { + return t.currentStream, nil + } + t.Close() + } + cmd = fmt.Sprintf("git-receive-pack %q", u.Path) + + default: + return nil, fmt.Errorf("unexpected action: %v", action) + } + + cred, err := t.transport.SmartCredentials("", CredTypeSshKey) + if err != nil { + return nil, err + } + defer C._go_git_credential_free(cred.ptr) + + sshConfig, err := getSSHConfigFromCredential(cred) + if err != nil { + return nil, err + } + sshConfig.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error { + marshaledKey := key.Marshal() + cert := &Certificate{ + Kind: CertificateHostkey, + Hostkey: HostkeyCertificate{ + Kind: HostkeySHA1 | HostkeyMD5 | HostkeySHA256 | HostkeyRaw, + HashMD5: md5.Sum(marshaledKey), + HashSHA1: sha1.Sum(marshaledKey), + HashSHA256: sha256.Sum256(marshaledKey), + Hostkey: marshaledKey, + SSHPublicKey: key, + }, + } + + return t.transport.SmartCertificateCheck(cert, true, hostname) + } + + var addr string + if u.Port() != "" { + addr = fmt.Sprintf("%s:%s", u.Hostname(), u.Port()) + } else { + addr = fmt.Sprintf("%s:22", u.Hostname()) + } + + t.client, err = ssh.Dial("tcp", addr, sshConfig) + if err != nil { + return nil, err + } + + t.session, err = t.client.NewSession() + if err != nil { + return nil, err + } + + t.stdin, err = t.session.StdinPipe() + if err != nil { + return nil, err + } + + t.stdout, err = t.session.StdoutPipe() + if err != nil { + return nil, err + } + + if err := t.session.Start(cmd); err != nil { + return nil, err + } + + t.lastAction = action + t.currentStream = &sshSmartSubtransportStream{ + owner: t, + } + + return t.currentStream, nil +} + +func (t *sshSmartSubtransport) Close() error { + t.currentStream = nil + if t.client != nil { + t.stdin.Close() + t.session.Wait() + t.session.Close() + t.client = nil + } + return nil +} + +func (t *sshSmartSubtransport) Free() { +} + +type sshSmartSubtransportStream struct { + owner *sshSmartSubtransport +} + +func (stream *sshSmartSubtransportStream) Read(buf []byte) (int, error) { + return stream.owner.stdout.Read(buf) +} + +func (stream *sshSmartSubtransportStream) Write(buf []byte) (int, error) { + return stream.owner.stdin.Write(buf) +} + +func (stream *sshSmartSubtransportStream) Free() { +} + +func getSSHConfigFromCredential(cred *Cred) (*ssh.ClientConfig, error) { + switch cred.Type() { + case CredTypeSshCustom: + credSSHCustom := (*C.git_cred_ssh_custom)(unsafe.Pointer(cred.ptr)) + data, ok := pointerHandles.Get(credSSHCustom.payload).(*credentialSSHCustomData) + if !ok { + return nil, errors.New("unsupported custom SSH credentials") + } + return &ssh.ClientConfig{ + User: C.GoString(credSSHCustom.username), + Auth: []ssh.AuthMethod{ssh.PublicKeys(data.signer)}, + }, nil + } + + username, _, privatekey, passphrase, ret := cred.GetSSHKey() + if ret != nil { + return nil, ret + } + + pemBytes, err := ioutil.ReadFile(privatekey) + if err != nil { + return nil, err + } + + var key ssh.Signer + if passphrase != "" { + key, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(passphrase)) + if err != nil { + return nil, err + } + } else { + key, err = ssh.ParsePrivateKey(pemBytes) + if err != nil { + return nil, err + } + } + + return &ssh.ClientConfig{ + User: username, + Auth: []ssh.AuthMethod{ssh.PublicKeys(key)}, + }, nil +} diff --git a/wrapper.c b/wrapper.c index c65f6740b..8c970c47a 100644 --- a/wrapper.c +++ b/wrapper.c @@ -434,6 +434,11 @@ void _go_git_populate_credential_ssh_custom(git_cred_ssh_custom *cred) cred->sign_callback = credential_ssh_sign_callback; } +void _go_git_credential_free(git_cred *cred) +{ + cred->free(cred); +} + int _go_git_odb_write_pack(git_odb_writepack **out, git_odb *db, void *progress_payload) { return git_odb_write_pack(out, db, transfer_progress_callback, progress_payload); From 2c7a0815027ab9a44f50f85d885b498332d0f49b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:26:34 -0800 Subject: [PATCH 097/103] Make ssh commands used in the git smart transport compatible with libgit2 (#852) (#853) * Fix ssh commands used in go SmartSubtransport Before the fix, the commands sent were of the form: ``` git-upload-pack "/bar/test-reponame" ``` This resulted in the git server returning error: `error parsing command: invalid git command` This change replaces the double quotes with single quotes: ``` git-upload-pack '/bar/test-reponame' ``` * Update ssh.go Co-authored-by: lhchavez (cherry picked from commit 6cea7a7a59f44e0e72ca577fbea65a042b3fb26b) Co-authored-by: Sunny Co-authored-by: lhchavez --- ssh.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ssh.go b/ssh.go index 65dfbcfaf..938414637 100644 --- a/ssh.go +++ b/ssh.go @@ -17,6 +17,7 @@ import ( "net" "net/url" "runtime" + "strings" "unsafe" "golang.org/x/crypto/ssh" @@ -74,6 +75,13 @@ func (t *sshSmartSubtransport) Action(urlString string, action SmartServiceActio 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: @@ -83,7 +91,7 @@ func (t *sshSmartSubtransport) Action(urlString string, action SmartServiceActio } t.Close() } - cmd = fmt.Sprintf("git-upload-pack %q", u.Path) + cmd = fmt.Sprintf("git-upload-pack '%s'", uPath) case SmartServiceActionReceivepackLs, SmartServiceActionReceivepack: if t.currentStream != nil { @@ -92,7 +100,7 @@ func (t *sshSmartSubtransport) Action(urlString string, action SmartServiceActio } t.Close() } - cmd = fmt.Sprintf("git-receive-pack %q", u.Path) + cmd = fmt.Sprintf("git-receive-pack '%s'", uPath) default: return nil, fmt.Errorf("unexpected action: %v", action) From 790cd6fc71a2d2321f2c4c57e750aa809124fbd9 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Tue, 9 Nov 2021 06:41:19 -0800 Subject: [PATCH 098/103] Fix replace statement example in README.md (#859) (#864) (cherry picked from commit 533c82f2707b8ad2f0f667867b3ea91ec08667aa) Co-authored-by: Ignacio Taranto --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 203e39230..9f0229677 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ In order to let Go pass the correct flags to `pkg-config`, `-tags static` needs One thing to take into account is that since Go expects the `pkg-config` file to be within the same directory where `make install-static` was called, so the `go.mod` file may need to have a [`replace` directive](https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive) so that the correct setup is achieved. So if `git2go` is checked out at `$GOPATH/src/github.com/libgit2/git2go` and your project at `$GOPATH/src/github.com/my/project`, the `go.mod` file of `github.com/my/project` might need to have a line like - replace github.com/libgit2/git2go/v27 ../../libgit2/git2go + replace github.com/libgit2/git2go/v27 => ../../libgit2/git2go Parallelism and network operations ---------------------------------- From 79b93a62df31408b07ae262182e1978ee62f6ee1 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Tue, 9 Nov 2021 06:49:20 -0800 Subject: [PATCH 099/103] Generate stringer files automatically (#841) (#869) Added `stringer` annotations to `git.go` for `ErrorClass` and `ErrorCode`. Added `generate` rule for `Makefile` to generate string representations for these types (first building cgo files in `_obj` dir to get C constants). Finally, updated `ci` actions workflow to check that generated files are up to date. Fixes: #543 (cherry picked from commit 5e35338d58b589939b599c98ec9e6b44f94de20a) Co-authored-by: Kirill --- .github/workflows/ci.yml | 23 +++++++++++++++++++++++ Makefile | 4 ++++ git.go | 2 ++ 3 files changed, 29 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b8a8f48cf..93429eed3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -102,3 +102,26 @@ jobs: sudo ./script/build-libgit2.sh --static --system - name: Test run: go test --count=1 --tags "static,system_libgit2" ./... + + check-generate: + name: Check generated files were not modified + runs-on: ubuntu-20.04 + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: '1.17' + id: go + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + - name: Install libgit2 build dependencies + run: | + git submodule update --init + sudo apt-get install -y --no-install-recommends libssh2-1-dev + go install golang.org/x/tools/cmd/stringer@latest + - name: Generate files + run: | + export PATH=$(go env GOPATH)/bin:$PATH + make generate + - name: Check nothing changed + run: git diff --quiet --exit-code || (echo "detected changes after generate" ; git status ; exit 1) diff --git a/Makefile b/Makefile index 84262f4da..71c5ee069 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,10 @@ TEST_ARGS ?= --count=1 default: test + +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. diff --git a/git.go b/git.go index 5796cdfac..64c72f5ee 100644 --- a/git.go +++ b/git.go @@ -14,6 +14,7 @@ import ( "unsafe" ) +//go:generate stringer -type ErrorClass -trimprefix ErrorClass -tags static type ErrorClass int const ( @@ -48,6 +49,7 @@ const ( ErrorClassPatch ErrorClass = C.GITERR_PATCH ) +//go:generate stringer -type ErrorCode -trimprefix ErrorCode -tags static type ErrorCode int const ( From 04b9efcea2308548e5b7a708474f3b9f10167ea7 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 17 Jan 2022 19:01:01 -0800 Subject: [PATCH 100/103] Add EnableFsyncGitDir to enable synchronized writes to the gitdir (#874) (#878) This adds support for the GIT_OPT_ENABLE_FSYNC_GITDIR option in libgit2. Co-authored-by: James Fargher (cherry picked from commit 1fcc9d87430e41cc333ce5b2431df7058e03bbb7) Co-authored-by: James Fargher --- settings.go | 8 ++++++++ settings_test.go | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/settings.go b/settings.go index f278e7be2..89b50e713 100644 --- a/settings.go +++ b/settings.go @@ -101,6 +101,14 @@ func EnableStrictHashVerification(enabled bool) error { } } +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) } diff --git a/settings_test.go b/settings_test.go index 47eb7116e..e3761d459 100644 --- a/settings_test.go +++ b/settings_test.go @@ -65,6 +65,14 @@ func TestEnableStrictHashVerification(t *testing.T) { 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) From d0ff27733f615585eeccc426a8b78f62f082cf9d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 17 Jan 2022 19:07:37 -0800 Subject: [PATCH 101/103] Add ProxyOptions for push operations (#872) (#883) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Analog to #623 but for push operations rather than fetch. (cherry picked from commit 5eca48cda937e67c1f8d68a55c2db22a79d83d0c) Co-authored-by: Aurélien <6292584+au2001@users.noreply.github.com> --- remote.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/remote.go b/remote.go index 7ae7e0628..8804c75dd 100644 --- a/remote.go +++ b/remote.go @@ -270,6 +270,9 @@ type PushOptions struct { // Headers are extra headers for the push operation. Headers []string + + // Proxy options to use for this push operation + ProxyOptions ProxyOptions } type RemoteHead struct { @@ -945,6 +948,7 @@ func populatePushOptions(copts *C.git_push_options, opts *PushOptions, errorTarg strings: makeCStringsFromStrings(opts.Headers), } populateRemoteCallbacks(&copts.callbacks, &opts.RemoteCallbacks, errorTarget) + populateProxyOptions(&copts.proxy_opts, &opts.ProxyOptions) return copts } @@ -954,6 +958,7 @@ func freePushOptions(copts *C.git_push_options) { } untrackCallbacksPayload(&copts.callbacks) freeStrarray(&copts.custom_headers) + freeProxyOptions(&copts.proxy_opts) } // Fetch performs a fetch operation. refspecs specifies which refspecs From 45c49574c9f8878e287086b33dea9a4319cad869 Mon Sep 17 00:00:00 2001 From: Dylan Richardson Date: Sat, 22 Jan 2022 21:03:13 -0600 Subject: [PATCH 102/103] readme: link to godoc for current main branch (#897) This is a release-specific change corresponding to #886 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9f0229677..d9b63687c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ git2go ====== -[![GoDoc](https://godoc.org/github.com/libgit2/git2go?status.svg)](http://godoc.org/github.com/libgit2/git2go) [![Build Status](https://travis-ci.org/libgit2/git2go.svg?branch=main)](https://travis-ci.org/libgit2/git2go) +[![GoDoc](https://godoc.org/github.com/libgit2/git2go?status.svg)](http://godoc.org/github.com/libgit2/git2go/v27) [![Build Status](https://travis-ci.org/libgit2/git2go.svg?branch=main)](https://travis-ci.org/libgit2/git2go) Go bindings for [libgit2](http://libgit2.github.com/). From bdf3ed46a89c4d6d01753996a85ae2dab1841ec5 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 24 Feb 2022 19:18:02 -0800 Subject: [PATCH 103/103] rebase: Add wrapper for `git_rebase_inmemory_index()` (#900) (#903) * rebase: Fix missing initialization of the repo pointer While the `Rebase` structure has a pointer to the repository the rebase is creatde in, this pointer isn't ever initialized. Fix this. * rebase: Add wrapper for `git_rebase_inmemory_index()` Add a new wrapper for `git_rebase_inmemory_index()`, which can be used to retrieve the index for an in-memory rebase. Co-authored-by: Patrick Steinhardt (cherry picked from commit e7d1b2b69fbe476c75a00cf8dcda284337facb50) Co-authored-by: Patrick Steinhardt --- rebase.go | 29 +++++++++++++++++--- rebase_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 4 deletions(-) diff --git a/rebase.go b/rebase.go index bf243e141..67f3a6254 100644 --- a/rebase.go +++ b/rebase.go @@ -181,7 +181,7 @@ func (r *Repository) InitRebase(branch *AnnotatedCommit, upstream *AnnotatedComm return nil, MakeGitError(ret) } - return newRebaseFromC(ptr, cOpts), nil + return newRebaseFromC(ptr, r, cOpts), nil } // OpenRebase opens an existing rebase that was previously started by either an invocation of InitRebase or by another client. @@ -203,7 +203,7 @@ func (r *Repository) OpenRebase(opts *RebaseOptions) (*Rebase, error) { return nil, MakeGitError(ret) } - return newRebaseFromC(ptr, cOpts), nil + return newRebaseFromC(ptr, r, cOpts), nil } // OperationAt gets the rebase operation specified by the given index. @@ -255,6 +255,27 @@ func (rebase *Rebase) Next() (*RebaseOperation, error) { return newRebaseOperationFromC(ptr), nil } +// InmemoryIndex gets the index produced by the last operation, which is the +// result of `Next()` and which will be committed by the next invocation of +// `Commit()`. This is useful for resolving conflicts in an in-memory rebase +// before committing them. +// +// This is only applicable for in-memory rebases; for rebases within a working +// directory, the changes were applied to the repository's index. +func (rebase *Rebase) InmemoryIndex() (*Index, error) { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + var ptr *C.git_index + err := C.git_rebase_inmemory_index(&ptr, rebase.ptr) + runtime.KeepAlive(rebase) + if err < 0 { + return nil, MakeGitError(err) + } + + return newIndexFromC(ptr, rebase.r), nil +} + // Commit commits the current patch. // You must have resolved any conflicts that were introduced during the patch application from the Next() invocation. func (rebase *Rebase) Commit(ID *Oid, author, committer *Signature, message string) error { @@ -320,8 +341,8 @@ func (r *Rebase) Free() { freeRebaseOptions(r.options) } -func newRebaseFromC(ptr *C.git_rebase, opts *C.git_rebase_options) *Rebase { - rebase := &Rebase{ptr: ptr, options: opts} +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 } diff --git a/rebase_test.go b/rebase_test.go index bd035d2ab..696c648a6 100644 --- a/rebase_test.go +++ b/rebase_test.go @@ -9,6 +9,80 @@ import ( // Tests +func TestRebaseInMemoryWithConflict(t *testing.T) { + repo := createTestRepo(t) + defer cleanupTestRepo(t, repo) + seedTestRepo(t, repo) + + // Create two branches with common history, where both modify "common-file" + // in a conflicting way. + _, err := commitSomething(repo, "common-file", "a\nb\nc\n", commitOptions{}) + checkFatal(t, err) + checkFatal(t, createBranch(repo, "branch-a")) + checkFatal(t, createBranch(repo, "branch-b")) + + checkFatal(t, repo.SetHead("refs/heads/branch-a")) + _, err = commitSomething(repo, "common-file", "1\nb\nc\n", commitOptions{}) + checkFatal(t, err) + + checkFatal(t, repo.SetHead("refs/heads/branch-b")) + _, err = commitSomething(repo, "common-file", "x\nb\nc\n", commitOptions{}) + checkFatal(t, err) + + branchA, err := repo.LookupBranch("branch-a", BranchLocal) + checkFatal(t, err) + onto, err := repo.AnnotatedCommitFromRef(branchA.Reference) + checkFatal(t, err) + + // We then rebase "branch-b" onto "branch-a" in-memory, which should result + // in a conflict. + rebase, err := repo.InitRebase(nil, nil, onto, &RebaseOptions{InMemory: 1}) + checkFatal(t, err) + + _, err = rebase.Next() + checkFatal(t, err) + + index, err := rebase.InmemoryIndex() + checkFatal(t, err) + + // We simply resolve the conflict and commit the rebase. + if !index.HasConflicts() { + t.Fatal("expected index to have conflicts") + } + + conflict, err := index.Conflict("common-file") + checkFatal(t, err) + + resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents")) + checkFatal(t, err) + + resolvedEntry := *conflict.Our + resolvedEntry.Id = resolvedBlobID + checkFatal(t, index.Add(&resolvedEntry)) + checkFatal(t, index.RemoveConflict("common-file")) + + var commitID Oid + checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message")) + checkFatal(t, rebase.Finish()) + + // And then assert that we can look up the new merge commit, and that the + // "common-file" has the expected contents. + commit, err := repo.LookupCommit(&commitID) + checkFatal(t, err) + if commit.Message() != "rebased message" { + t.Fatalf("unexpected commit message %q", commit.Message()) + } + + tree, err := commit.Tree() + checkFatal(t, err) + + blob, err := repo.LookupBlob(tree.EntryByName("common-file").Id) + checkFatal(t, err) + if string(blob.Contents()) != "resolved contents" { + t.Fatalf("unexpected resolved blob contents %q", string(blob.Contents())) + } +} + func TestRebaseAbort(t *testing.T) { // TEST DATA