|
| 1 | +=== go-git |
| 2 | + |
| 3 | +(((go-git)))((("Go"))) |
| 4 | +In case you want to integrate Git into a service written in Golang, there also is a pure Go library implementation. |
| 5 | +This implementation does not have any native dependencies and thus is not prone to manual memory managemen errors. |
| 6 | +It is also transparent for the standard Golang performance analysis tooling like CPU, Memory profilers, race detector, etc. |
| 7 | + |
| 8 | +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[]. |
| 9 | + |
| 10 | +Here is a basic example of using Go APIs: |
| 11 | + |
| 12 | +[source, go] |
| 13 | +----- |
| 14 | +import "gopkg.in/src-d/go-git.v4" |
| 15 | +
|
| 16 | +r, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{ |
| 17 | + URL: "https://github.com/src-d/go-git", |
| 18 | + Progress: os.Stdout, |
| 19 | +}) |
| 20 | +----- |
| 21 | + |
| 22 | +As soon as you have a `Repository` instance, you can access information and perform mutations on it: |
| 23 | + |
| 24 | + |
| 25 | +[source, go] |
| 26 | +----- |
| 27 | +// retrieves the branch pointed by HEAD |
| 28 | +ref, err := r.Head() |
| 29 | +
|
| 30 | +// get the commit object, pointed by ref |
| 31 | +commit, err := r.CommitObject(ref.Hash()) |
| 32 | +
|
| 33 | +// retrieves the commit history |
| 34 | +history, err := commit.History() |
| 35 | +
|
| 36 | +// iterates over the commits and print each |
| 37 | +for _, c := range history { |
| 38 | + fmt.Println(c) |
| 39 | +} |
| 40 | +----- |
| 41 | + |
| 42 | + |
| 43 | +==== Advanced Functionality |
| 44 | + |
| 45 | +go-git has few notable advanced features, one of which is a pluggable storage system, which is similar to Libgit2 backends. |
| 46 | +The default implementation is in-memory storage, which is very fast. |
| 47 | + |
| 48 | +[source, go] |
| 49 | +----- |
| 50 | +r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ |
| 51 | + URL: "https://github.com/src-d/go-git", |
| 52 | +}) |
| 53 | +----- |
| 54 | + |
| 55 | +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. |
| 56 | + |
| 57 | +Another feature is a flexible filesystem abstraction. |
| 58 | +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. |
| 59 | + |
| 60 | +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[]. |
| 61 | + |
| 62 | +[source, go] |
| 63 | +----- |
| 64 | +customClient := &http.Client{ |
| 65 | + Transport: &http.Transport{ // accept any certificate (might be useful for testing) |
| 66 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 67 | + }, |
| 68 | + Timeout: 15 * time.Second, // 15 second timeout |
| 69 | + CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 70 | + return http.ErrUseLastResponse // don't follow redirect |
| 71 | + }, |
| 72 | +} |
| 73 | +
|
| 74 | +// Override http(s) default protocol to use our custom client |
| 75 | +client.InstallProtocol("https", githttp.NewClient(customClient)) |
| 76 | +
|
| 77 | +// Clone repository using the new client if the protocol is https:// |
| 78 | +r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: url}) |
| 79 | +----- |
| 80 | + |
| 81 | + |
| 82 | +==== Further Reading |
| 83 | + |
| 84 | +A full treatment of go-git's capabilities is outside the scope of this book. |
| 85 | +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[]. |
0 commit comments