A2.4 附录 B: 在你的应用中嵌入 Git - go-git

go-git

In case you want to integrate Git into a service written in Golang, there also is a pure Go library implementation. This implementation does not have any native dependencies and thus is not prone to manual memory management errors. It is also transparent for the standard Golang performance analysis tooling like CPU, Memory profilers, race detector, etc.

go-git is focused on extensibility, compatibility and supports most of the plumbing APIs, which is documented at https://github.com/src-d/go-git/blob/master/COMPATIBILITY.md.

Here is a basic example of using Go APIs:

  1. import "gopkg.in/src-d/go-git.v4"
  2. r, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
  3. URL: "https://github.com/src-d/go-git",
  4. Progress: os.Stdout,
  5. })

As soon as you have a Repository instance, you can access information and perform mutations on it:

  1. // retrieves the branch pointed by HEAD
  2. ref, err := r.Head()
  3. // get the commit object, pointed by ref
  4. commit, err := r.CommitObject(ref.Hash())
  5. // retrieves the commit history
  6. history, err := commit.History()
  7. // iterates over the commits and print each
  8. for _, c := range history {
  9. fmt.Println(c)
  10. }

Advanced Functionality

go-git has few notable advanced features, one of which is a pluggable storage system, which is similar to Libgit2 backends. The default implementation is in-memory storage, which is very fast.

  1. r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  2. URL: "https://github.com/src-d/go-git",
  3. })

Pluggable storage provides many interesting options. For instance, https://github.com/src-d/go-git/tree/master/_examples/storage allows you to store references, objects, and configuration in an Aerospike database.

Another feature is a flexible filesystem abstraction. Using https://godoc.org/github.com/src-d/go-billy#Filesystem it is easy to store all the files in different way i.e by packing all of them to a single archive on disk or by keeping them all in-memory.

Another advanced use-case includes a fine-tunable HTTP client, such as the one found at https://github.com/src-d/go-git/blob/master/_examples/custom_http/main.go.

  1. customClient := &http.Client{
  2. Transport: &http.Transport{ // accept any certificate (might be useful for testing)
  3. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  4. },
  5. Timeout: 15 * time.Second, // 15 second timeout
  6. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  7. return http.ErrUseLastResponse // don't follow redirect
  8. },
  9. }
  10. // Override http(s) default protocol to use our custom client
  11. client.InstallProtocol("https", githttp.NewClient(customClient))
  12. // Clone repository using the new client if the protocol is https://
  13. r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: url})

Further Reading

A full treatment of go-git’s capabilities is outside the scope of this book. If you want more information on go-git, there’s API documentation at https://godoc.org/gopkg.in/src-d/go-git.v4, and a set of usage examples at https://github.com/src-d/go-git/tree/master/_examples.