-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update /book/B-embedding-git/sections/
- Loading branch information
1 parent
f6838a3
commit 246f72e
Showing
4 changed files
with
152 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
=== Dulwich | ||
|
||
(((Dulwich)))(((Python))) | ||
There is also a pure-Python Git implementation - Dulwich. | ||
The project is hosted under https://www.dulwich.io/ | ||
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. | ||
It has an optional C extensions though, that significantly improve the performance. | ||
|
||
Dulwich follows git design and separate two basic levels of API: plumbing and porcelain. | ||
|
||
Here is an example of using the lower level API to access the commit message of the last commit: | ||
|
||
[source, python] | ||
---- | ||
from dulwich.repo import Repo | ||
r = Repo('.') | ||
r.head() | ||
# '57fbe010446356833a6ad1600059d80b1e731e15' | ||
c = r[r.head()] | ||
c | ||
# <Commit 015fc1267258458901a94d228e39f0a378370466> | ||
c.message | ||
# 'Add note about encoding.\n' | ||
---- | ||
|
||
To print a commit log using high-level porcelain API, one can use: | ||
|
||
[source, python] | ||
---- | ||
from dulwich import porcelain | ||
porcelain.log('.', max_entries=1) | ||
#commit: 57fbe010446356833a6ad1600059d80b1e731e15 | ||
#Author: Jelmer Vernooij <[email protected]> | ||
#Date: Sat Apr 29 2017 23:57:34 +0000 | ||
---- | ||
|
||
|
||
==== Further Reading | ||
|
||
* The official API documentation is available at https://www.dulwich.io/docs/api/[]. | ||
* Official tutorial at https://www.dulwich.io/docs/tutorial[] has many examples of how to do specific tasks with Dulwich. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
=== go-git | ||
|
||
(((go-git)))(((Go))) | ||
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/go-git/go-git/blob/master/COMPATIBILITY.md[]. | ||
|
||
Here is a basic example of using Go APIs: | ||
|
||
[source, go] | ||
---- | ||
import "github.com/go-git/go-git/v5" | ||
r, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{ | ||
URL: "https://github.com/go-git/go-git", | ||
Progress: os.Stdout, | ||
}) | ||
---- | ||
|
||
As soon as you have a `Repository` instance, you can access information and perform mutations on it: | ||
|
||
[source, go] | ||
---- | ||
// retrieves the branch pointed by HEAD | ||
ref, err := r.Head() | ||
// get the commit object, pointed by ref | ||
commit, err := r.CommitObject(ref.Hash()) | ||
// retrieves the commit history | ||
history, err := commit.History() | ||
// iterates over the commits and print each | ||
for _, c := range history { | ||
fmt.Println(c) | ||
} | ||
---- | ||
|
||
==== 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. | ||
|
||
[source, go] | ||
---- | ||
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ | ||
URL: "https://github.com/go-git/go-git", | ||
}) | ||
---- | ||
|
||
Pluggable storage provides many interesting options. | ||
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. | ||
|
||
Another feature is a flexible filesystem abstraction. | ||
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. | ||
|
||
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[]. | ||
|
||
[source, go] | ||
---- | ||
customClient := &http.Client{ | ||
Transport: &http.Transport{ // accept any certificate (might be useful for testing) | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
Timeout: 15 * time.Second, // 15 second timeout | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
return http.ErrUseLastResponse // don't follow redirect | ||
}, | ||
} | ||
// Override http(s) default protocol to use our custom client | ||
client.InstallProtocol("https", githttp.NewClient(customClient)) | ||
// Clone repository using the new client if the protocol is https:// | ||
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://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[]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters