git2go Patch() 仅返回第一个文件的差异

我试图配置rb-gateway(用 Go 编写的简单代理,它支持 ReviewBoard 的自定义存储库)。git2go 是从下一个分支与libgit2一起构建的。从差异生成补丁期间出现问题。下面的代码总是只返回第一个文件更改。


package main


import (

    "github.com/libgit2/git2go"

    "log"

)


func main() {

    gitRepo, err := git.OpenRepository("repo_path")

    if err != nil {

        log.Fatal(err)

    }


    commitOid, err := git.NewOid("commit_id_sha1")

    if err != nil {

        log.Fatal(err)

    }


    commit, err := gitRepo.LookupCommit(commitOid)

    if err != nil {

        log.Fatal(err)

    }


    commitTree, err := commit.Tree()

    if err != nil {

        log.Fatal(err)

    }


    options, err := git.DefaultDiffOptions()

    if err != nil {

        log.Fatal(err)

    }


    // Specifying full patch indices.

    options.IdAbbrev = 40


    var parentTree *git.Tree

    if commit.ParentCount() > 0 {

        parentTree, err = commit.Parent(0).Tree()

        if err != nil {

            log.Fatal(err)

         }

    }


    gitDiff, err := gitRepo.DiffTreeToTree(parentTree, commitTree, &options)

    if err != nil {

         log.Fatal(err)

    }


    patch, err := gitDiff.Patch(0)

    if err != nil {

        log.Fatal(err)

    }


    patchString, err := patch.String()

    if err != nil {

         log.Fatal(err)

    }


    log.Printf("%s", patchString)


    patch.Free()

}

环境: go version go1.4.2 linux/amd64 git version 1.8.3.1 libgit2 and git2go latest Linux 3.10.0-327.4.5.el7.x86_64 (CentOS)


叮当猫咪
浏览 234回答 1
1回答

富国沪深

您需要发布重现问题的所有必要元素:如何创建最小、完整和可验证的示例。这包括输入和预期输出。例如,以可重现的形式显示提交中的所有文件补丁差异:patchdiffs.go:package mainimport (&nbsp; &nbsp; "github.com/libgit2/git2go"&nbsp; &nbsp; "log")/*github.com/libgit2/git2gocommit 80cf533fe4e48ddfab3015d9570f2833951c1deaAuthor: David Pierce <david.pierce@moz.com>Date:&nbsp; &nbsp;Sat Sep 26 15:37:48 2015 -0700&nbsp; &nbsp; Config#LookupString uses git_buf to load value&nbsp;config.go&nbsp; &nbsp; &nbsp; |&nbsp; 8 +++++---&nbsp;config_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++&nbsp;2 files changed, 63 insertions(+), 3 deletions(-)*/func main() {&nbsp; &nbsp; // After go get -v github.com/libgit2/git2go&nbsp; &nbsp; // path to git2go repository in your $GOPATH&nbsp; &nbsp; repoPath := `/home/peter/gopath/src/github.com/libgit2/git2go`&nbsp; &nbsp; gitRepo, err := git.OpenRepository(repoPath)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; // commit SHA-1 checksum&nbsp; &nbsp; commitID := `80cf533fe4e48ddfab3015d9570f2833951c1dea`&nbsp; &nbsp; commitOid, err := git.NewOid(commitID)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; commit, err := gitRepo.LookupCommit(commitOid)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; commitTree, err := commit.Tree()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; options, err := git.DefaultDiffOptions()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; options.IdAbbrev = 40&nbsp; &nbsp; var parentTree *git.Tree&nbsp; &nbsp; if commit.ParentCount() > 0 {&nbsp; &nbsp; &nbsp; &nbsp; parentTree, err = commit.Parent(0).Tree()&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; gitDiff, err := gitRepo.DiffTreeToTree(parentTree, commitTree, &options)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; // Show all file patch diffs in a commit.&nbsp; &nbsp; numDeltas, err := gitDiff.NumDeltas()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; for d := 0; d < numDeltas; d++ {&nbsp; &nbsp; &nbsp; &nbsp; patch, err := gitDiff.Patch(d)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; patchString, err := patch.String()&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; log.Printf("\n%s", patchString)&nbsp; &nbsp; &nbsp; &nbsp; patch.Free()&nbsp; &nbsp; }}输出:$ go run patchdiffs.go2016/01/30 18:35:44&nbsp;diff --git a/config.go b/config.goindex 9d25e3571de22b4121d66bb88949d0e292f1a836..c4c40281abb85a861ba1bf760011e2990cf151b3 100644--- a/config.go+++ b/config.go@@ -115,18 +115,20 @@ func (c *Config) LookupInt64(name string) (int64, error) {&nbsp;}&nbsp;func (c *Config) LookupString(name string) (string, error) {-&nbsp; &nbsp;var ptr *C.char&nbsp; &nbsp; cname := C.CString(name)&nbsp; &nbsp; defer C.free(unsafe.Pointer(cname))+&nbsp; &nbsp;valBuf := C.git_buf{}+&nbsp; &nbsp; runtime.LockOSThread()&nbsp; &nbsp; defer runtime.UnlockOSThread()-&nbsp; &nbsp;if ret := C.git_config_get_string(&ptr, c.ptr, cname); ret < 0 {+&nbsp; &nbsp;if ret := C.git_config_get_string_buf(&valBuf, c.ptr, cname); ret < 0 {&nbsp; &nbsp; &nbsp; &nbsp; return "", MakeGitError(ret)&nbsp; &nbsp; }+&nbsp; &nbsp;defer C.git_buf_free(&valBuf)-&nbsp; &nbsp;return C.GoString(ptr), nil+&nbsp; &nbsp;return C.GoString(valBuf.ptr), nil&nbsp;}&nbsp;func (c *Config) LookupBool(name string) (bool, error) {2016/01/30 18:35:44&nbsp;diff --git a/config_test.go b/config_test.gonew file mode 100644index 0000000000000000000000000000000000000000..e4a2c1f310f1beb9bcb70be50320c53e22417e2a--- /dev/null+++ b/config_test.go@@ -0,0 +1,58 @@+package git++import (+&nbsp; &nbsp;"os"+&nbsp; &nbsp;"testing"+)++func setupConfig() (*Config, error) {+&nbsp; &nbsp;var (+&nbsp; &nbsp; &nbsp; &nbsp;c&nbsp; &nbsp;*Config+&nbsp; &nbsp; &nbsp; &nbsp;err error+&nbsp; &nbsp; &nbsp; &nbsp;p&nbsp; &nbsp;string+&nbsp; &nbsp;)++&nbsp; &nbsp;p, err = ConfigFindGlobal()+&nbsp; &nbsp;if err != nil {+&nbsp; &nbsp; &nbsp; &nbsp;return nil, err+&nbsp; &nbsp;}++&nbsp; &nbsp;c, err = OpenOndisk(nil, p)+&nbsp; &nbsp;if err != nil {+&nbsp; &nbsp; &nbsp; &nbsp;return nil, err+&nbsp; &nbsp;}++&nbsp; &nbsp;c.SetString("foo.bar", "baz")++&nbsp; &nbsp;return c, err+}++func cleanupConfig() {+&nbsp; &nbsp;os.Remove(tempConfig)+}++func TestConfigLookupString(t *testing.T) {+&nbsp; &nbsp;var (+&nbsp; &nbsp; &nbsp; &nbsp;err error+&nbsp; &nbsp; &nbsp; &nbsp;val string+&nbsp; &nbsp; &nbsp; &nbsp;c&nbsp; &nbsp;*Config+&nbsp; &nbsp;)++&nbsp; &nbsp;c, err = setupConfig()+&nbsp; &nbsp;defer cleanupConfig()+&nbsp; &nbsp;if err != nil {+&nbsp; &nbsp; &nbsp; &nbsp;t.Errorf("Setup error: '%v'. Expected none\n", err)+&nbsp; &nbsp; &nbsp; &nbsp;t.FailNow()+&nbsp; &nbsp;}+&nbsp; &nbsp;defer c.Free()++&nbsp; &nbsp;val, err = c.LookupString("foo.bar")+&nbsp; &nbsp;if err != nil {+&nbsp; &nbsp; &nbsp; &nbsp;t.Errorf("Got error: '%v', expected none\n", err)+&nbsp; &nbsp; &nbsp; &nbsp;t.FailNow()+&nbsp; &nbsp;}++&nbsp; &nbsp;if val != "baz" {+&nbsp; &nbsp; &nbsp; &nbsp;t.Errorf("Got '%s', expected 'bar'\n", val)+&nbsp; &nbsp;}+}$
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go