Skip to content

Commit 8a748ca

Browse files
Arquivos renomeados e adicionados no original
1 parent 4b38e75 commit 8a748ca

8 files changed

Lines changed: 132 additions & 2 deletions

File tree

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
[#B-embedding-git]
1+
[[B-embedding-git-in-your-applications]]
22
[appendix]
33
== Embedding Git in your Applications
44

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[]
18+
19+
include::book/B-embedding-git/sections/dulwich.asc[]
File renamed without changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
=== Dulwich
2+
3+
(((Dulwich)))(((Python)))
4+
There is also a pure-Python Git implementation - Dulwich.
5+
The project is hosted under https://www.dulwich.io/[^].
6+
It aims to provide an interface to Git repositories (both local and remote) that doesn't call out to Git directly but instead uses pure Python.
7+
It has an optional C extensions though, that significantly improve the performance.
8+
9+
Dulwich follows Git design and separate two basic levels of API: plumbing and porcelain.
10+
11+
Here is an example of using the lower level API to access the commit message of the last commit:
12+
13+
[source, python]
14+
----
15+
from dulwich.repo import Repo
16+
r = Repo('.')
17+
r.head()
18+
# '57fbe010446356833a6ad1600059d80b1e731e15'
19+
20+
c = r[r.head()]
21+
c
22+
# <Commit 015fc1267258458901a94d228e39f0a378370466>
23+
24+
c.message
25+
# 'Add note about encoding.\n'
26+
----
27+
28+
To print a commit log using high-level porcelain API, one can use:
29+
30+
[source, python]
31+
----
32+
from dulwich import porcelain
33+
porcelain.log('.', max_entries=1)
34+
35+
#commit: 57fbe010446356833a6ad1600059d80b1e731e15
36+
#Author: Jelmer Vernooij <jelmer@jelmer.uk>
37+
#Date: Sat Apr 29 2017 23:57:34 +0000
38+
----
39+
40+
==== Further Reading
41+
42+
The API documentation, tutorial, and many examples of how to do specific tasks with Dulwich are available on the official website https://www.dulwich.io[^].
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 management 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/go-git/go-git/blob/master/COMPATIBILITY.md[^].
9+
10+
Here is a basic example of using Go APIs:
11+
12+
[source, go]
13+
----
14+
import "github.com/go-git/go-git/v5"
15+
16+
r, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
17+
URL: "https://github.com/go-git/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+
[source, go]
25+
----
26+
// retrieves the branch pointed by HEAD
27+
ref, err := r.Head()
28+
29+
// get the commit object, pointed by ref
30+
commit, err := r.CommitObject(ref.Hash())
31+
32+
// retrieves the commit history
33+
history, err := commit.History()
34+
35+
// iterates over the commits and print each
36+
for _, c := range history {
37+
fmt.Println(c)
38+
}
39+
----
40+
41+
==== Advanced Functionality
42+
43+
go-git has few notable advanced features, one of which is a pluggable storage system, which is similar to Libgit2 backends.
44+
The default implementation is in-memory storage, which is very fast.
45+
46+
[source, go]
47+
----
48+
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
49+
URL: "https://github.com/go-git/go-git",
50+
})
51+
----
52+
53+
Pluggable storage provides many interesting options.
54+
For instance, https://github.com/go-git/go-git/tree/master/_examples/storage[^] allows you to store references, objects, and configuration in an Aerospike database.
55+
56+
Another feature is a flexible filesystem abstraction.
57+
Using https://pkg.go.dev/github.com/go-git/go-billy/v5?tab=doc#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, such as the one found at https://github.com/go-git/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+
==== Further Reading
81+
82+
A full treatment of go-git's capabilities is outside the scope of this book.
83+
If you want more information on go-git, there's API documentation at https://pkg.go.dev/github.com/go-git/go-git/v5[^], and a set of usage examples at https://github.com/go-git/go-git/tree/master/_examples[^].
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)