|
| 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 implmentation. In contrast to using a Libgit2 bindings, this implementation does not have any native dependencies, not prone to memory management errors and is transparent for standard Golang performance analysis tooling like CPU, Memory profilers, race detector, etc. |
| 5 | + |
| 6 | +go-git is focused on extensibility, compatibility and supports most of the plumbing APIs, wich is documented https://github.com/src-d/go-git/blob/master/COMPATIBILITY.md[] |
| 7 | + |
| 8 | +Here is a basic example of using Go APIs |
| 9 | + |
| 10 | +[source, go] |
| 11 | +----- |
| 12 | +import "gopkg.in/src-d/go-git.v4" |
| 13 | +
|
| 14 | +r, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{ |
| 15 | + URL: "https://github.com/src-d/go-git", |
| 16 | + Progress: os.Stdout, |
| 17 | +}) |
| 18 | +----- |
| 19 | + |
| 20 | +As soon as you have a `Repository` instance, |
| 21 | + |
| 22 | + |
| 23 | +[source, go] |
| 24 | +----- |
| 25 | +// retrieves the branch pointed by HEAD |
| 26 | +ref, err := r.Head() |
| 27 | +
|
| 28 | +// get the commit object, pointed by ref |
| 29 | +commit, err := r.CommitObject(ref.Hash()) |
| 30 | +
|
| 31 | +// retrieves the commit history |
| 32 | +history, err := commit.History() |
| 33 | +
|
| 34 | +// iterates over the commits and print each |
| 35 | +for _, c := range history { |
| 36 | + fmt.Println(c) |
| 37 | +} |
| 38 | +----- |
| 39 | + |
| 40 | + |
| 41 | +==== Advanced Functionality |
| 42 | + |
| 43 | +go-git has few notable advanced features, one of wich is a pluggable storage system, similar to Libgit2 backends, with a default implementation of in-memory storage. |
| 44 | + |
| 45 | +[source, go] |
| 46 | +----- |
| 47 | +r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ |
| 48 | + URL: "https://github.com/src-d/go-git", |
| 49 | +}) |
| 50 | +----- |
| 51 | + |
| 52 | +That way all operations over the repository become bazingly fast as they never hit the disk. |
| 53 | + |
| 54 | +Other storage implementaions include ability to store references, objects and configuration in a database i.e https://github.com/src-d/go-git/tree/master/_examples/storage[] an Aerospike. |
| 55 | + |
| 56 | +Another feature is a flexible filesystem abstraction. |
| 57 | +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. |
| 58 | + |
| 59 | +Another advanced use-case includes a fine-tunable HTTP client https://github.com/src-d/go-git/blob/master/_examples/custom_http/main.go[] |
| 60 | + |
| 61 | +[source, go] |
| 62 | +----- |
| 63 | +customClient := &http.Client{ |
| 64 | + Transport: &http.Transport{ // accept any certificate (might be useful for testing) |
| 65 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 66 | + }, |
| 67 | + Timeout: 15 * time.Second, // 15 second timeout |
| 68 | + CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 69 | + return http.ErrUseLastResponse // don't follow redirect |
| 70 | + }, |
| 71 | +} |
| 72 | +
|
| 73 | +// Override http(s) default protocol to use our custom client |
| 74 | +client.InstallProtocol("https", githttp.NewClient(customClient)) |
| 75 | +
|
| 76 | +// Clone repository using the new client if the protocol is https:// |
| 77 | +r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: url}) |
| 78 | +----- |
| 79 | + |
| 80 | + |
| 81 | +==== Further Reading |
| 82 | + |
| 83 | +A full treatment of go-git's capabilities is outside the scope of this book. |
| 84 | +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