Skip to content

Commit ca32012

Browse files
authored
Merge pull request #998 from bzz/embedding/add-go-git
Add go-git to Appendinx B: embedding
2 parents 85eba6e + d9a1396 commit ca32012

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

B-embedding-git-in-your-applications.asc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
If your application is for developers, chances are good that it could benefit from integration with source control.
66
Even non-developer applications, such as document editors, could potentially benefit from version-control features, and Git's model works very well for many different scenarios.
77

8-
If you need to integrate Git with your application, you have essentially three choices: spawning a shell and using the Git command-line tool; Libgit2; and JGit.
8+
If you need to integrate Git with your application, you have essentially two options: spawn a shell and call the `git` command-line program, or embed a Git library into your application.
9+
Here we'll cover command-line integration and several of the most popular embeddable Git libraries.
910

1011
include::book/B-embedding-git/sections/command-line.asc[]
1112

1213
include::book/B-embedding-git/sections/libgit2.asc[]
1314

1415
include::book/B-embedding-git/sections/jgit.asc[]
16+
17+
include::book/B-embedding-git/sections/go-git.asc[]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)