summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Hathaway <[email protected]>2020-06-02 12:30:42 -0500
committerGitHub <[email protected]>2020-06-02 10:30:42 -0700
commit5241c72e6ebd21085e56a1c6d284c06154a202b5 (patch)
tree4e2a517025d99f0031d34d1fd10b598d8c53b0af
parent31f877e249e28c29cc4fcd512381a5a5b26e59d9 (diff)
Add support for parsing git trailers (#614)
Adds a wrapper for git_message_trailers which returns a slice of trailer structs.
-rw-r--r--message.go42
-rw-r--r--message_test.go42
2 files changed, 84 insertions, 0 deletions
diff --git a/message.go b/message.go
new file mode 100644
index 0000000..255e8d7
--- /dev/null
+++ b/message.go
@@ -0,0 +1,42 @@
+package git
+
+/*
+#include <git2.h>
+*/
+import "C"
+import (
+ "runtime"
+ "unsafe"
+)
+
+// Trailer represents a single git message trailer.
+type Trailer struct {
+ Key string
+ Value string
+}
+
+// MessageTrailers parses trailers out of a message, returning a slice of
+// Trailer structs. Trailers are key/value pairs in the last paragraph of a
+// message, not including any patches or conflicts that may be present.
+func MessageTrailers(message string) ([]Trailer, error) {
+ var trailersC C.git_message_trailer_array
+
+ messageC := C.CString(message)
+ defer C.free(unsafe.Pointer(messageC))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ecode := C.git_message_trailers(&trailersC, messageC)
+ if ecode < 0 {
+ return nil, MakeGitError(ecode)
+ }
+ defer C.git_message_trailer_array_free(&trailersC)
+ trailers := make([]Trailer, trailersC.count)
+ var trailer *C.git_message_trailer
+ for i, p := 0, uintptr(unsafe.Pointer(trailersC.trailers)); i < int(trailersC.count); i, p = i+1, p+unsafe.Sizeof(C.git_message_trailer{}) {
+ trailer = (*C.git_message_trailer)(unsafe.Pointer(p))
+ trailers[i] = Trailer{Key: C.GoString(trailer.key), Value: C.GoString(trailer.value)}
+ }
+ return trailers, nil
+}
diff --git a/message_test.go b/message_test.go
new file mode 100644
index 0000000..f33ccb7
--- /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 <[email protected]>\n",
+ []Trailer{
+ Trailer{Key: "Co-authored-by", Value: "Alice <[email protected]>"},
+ },
+ },
+ {
+ "commit with two trailers\n\nCo-authored-by: Alice <[email protected]>\nSigned-off-by: Bob <[email protected]>\n",
+ []Trailer{
+ Trailer{Key: "Co-authored-by", Value: "Alice <[email protected]>"},
+ Trailer{Key: "Signed-off-by", Value: "Bob <[email protected]>"}},
+ },
+ }
+ 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)
+ }
+ }
+}