From e8d69694706ec1e9f61bb041e91abbd251d796a7 Mon Sep 17 00:00:00 2001 From: Byoungchan Lee Date: Wed, 3 Feb 2021 12:42:21 +0900 Subject: [PATCH 1/2] Support git_repository_message, git_repository_message_remove (#734) Closes #646 (cherry picked from commit 07147a8ea8ccf216fa490e7ed4ec84e7c5f5d9ee) --- merge_test.go | 8 ++++++++ repository.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/merge_test.go b/merge_test.go index d49d07c..72b1a26 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 42e74a0..56ed3f6 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_dispose(&buf) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cErr := C.git_repository_message(&buf, r.ptr) + runtime.KeepAlive(r) + if cErr < 0 { + return "", MakeGitError(cErr) + } + return C.GoString(buf.ptr), nil +} + +// RemoveMessage removes git's prepared message. +func (r *Repository) RemoveMessage() error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + cErr := C.git_repository_message_remove(r.ptr) + runtime.KeepAlive(r) + if cErr < 0 { + return MakeGitError(cErr) + } + return nil +} -- 2.45.2 From ae4f21273bf35a7c869c55bd8e78ec047813bbdd Mon Sep 17 00:00:00 2001 From: lhchavez Date: Wed, 3 Feb 2021 05:46:14 -0800 Subject: [PATCH 2/2] Use the old `git_buf_free()` function This is not yet ready for the future. --- repository.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repository.go b/repository.go index 56ed3f6..07f5df7 100644 --- a/repository.go +++ b/repository.go @@ -699,7 +699,7 @@ func (r *Repository) ClearGitIgnoreRules() error { // the file after you create the commit. func (r *Repository) Message() (string, error) { buf := C.git_buf{} - defer C.git_buf_dispose(&buf) + defer C.git_buf_free(&buf) runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -722,5 +722,5 @@ func (r *Repository) RemoveMessage() error { if cErr < 0 { return MakeGitError(cErr) } - return nil + return nil } -- 2.45.2