| 1 |
# Extending go-git |
| 2 |
|
| 3 |
`go-git` was built in a highly extensible manner, which enables some of its functionalities to be changed or extended without the need of changing its codebase. Here are the key extensibility features: |
| 4 |
|
| 5 |
## Dot Git Storers |
| 6 |
|
| 7 |
Dot git storers are the components responsible for storing the Git internal files, including objects and references. |
| 8 |
|
| 9 |
The built-in storer implementations include [memory](storage/memory) and [filesystem](storage/filesystem). The `memory` storer stores all the data in memory, and its use look like this: |
| 10 |
|
| 11 |
```go |
| 12 |
r, err := git.Init(memory.NewStorage(), nil) |
| 13 |
``` |
| 14 |
|
| 15 |
The `filesystem` storer stores the data in the OS filesystem, and can be used as follows: |
| 16 |
|
| 17 |
```go |
| 18 |
r, err := git.Init(filesystem.NewStorage(osfs.New("/tmp/foo")), nil) |
| 19 |
``` |
| 20 |
|
| 21 |
New implementations can be created by implementing the [storage.Storer interface](storage/storer.go#L16). |
| 22 |
|
| 23 |
## Filesystem |
| 24 |
|
| 25 |
Git repository worktrees are managed using a filesystem abstraction based on [go-billy](https://github.com/go-git/go-billy). The Git operations will take place against the specific filesystem implementation. Initialising a repository in Memory can be done as follows: |
| 26 |
|
| 27 |
```go |
| 28 |
fs := memfs.New() |
| 29 |
r, err := git.Init(memory.NewStorage(), fs) |
| 30 |
``` |
| 31 |
|
| 32 |
The same operation can be done against the OS filesystem: |
| 33 |
|
| 34 |
```go |
| 35 |
fs := osfs.New("/tmp/foo") |
| 36 |
r, err := git.Init(memory.NewStorage(), fs) |
| 37 |
``` |
| 38 |
|
| 39 |
New filesystems (e.g. cloud based storage) could be created by implementing `go-billy`'s [Filesystem interface](https://github.com/go-git/go-billy/blob/326c59f064021b821a55371d57794fbfb86d4cb3/fs.go#L52). |
| 40 |
|
| 41 |
## Transport Schemes |
| 42 |
|
| 43 |
Git supports various transport schemes, including `http`, `https`, `ssh`, `git`, `file`. `go-git` defines the [transport.Transport interface](plumbing/transport/common.go#L48) to represent them. |
| 44 |
|
| 45 |
The built-in implementations can be replaced by calling `client.InstallProtocol`. |
| 46 |
|
| 47 |
An example of changing the built-in `https` implementation to skip TLS could look like this: |
| 48 |
|
| 49 |
```go |
| 50 |
customClient := &http.Client{ |
| 51 |
Transport: &http.Transport{ |
| 52 |
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 53 |
}, |
| 54 |
} |
| 55 |
|
| 56 |
client.InstallProtocol("https", githttp.NewClient(customClient)) |
| 57 |
``` |
| 58 |
|
| 59 |
Some internal implementations enables code reuse amongst the different transport implementations. Some of these may be made public in the future (e.g. `plumbing/transport/internal/common`). |
| 60 |
|
| 61 |
## Cache |
| 62 |
|
| 63 |
Several different operations across `go-git` lean on caching of objects in order to achieve optimal performance. The caching functionality is defined by the [cache.Object interface](plumbing/cache/common.go#L17). |
| 64 |
|
| 65 |
Two built-in implementations are `cache.ObjectLRU` and `cache.BufferLRU`. However, the caching functionality can be customized by implementing the interface `cache.Object` interface. |
| 66 |
|
| 67 |
## Hash |
| 68 |
|
| 69 |
`go-git` uses the `crypto.Hash` interface to represent hash functions. The built-in implementations are `github.com/pjbgf/sha1cd` for SHA1 and Go's `crypto/SHA256`. |
| 70 |
|
| 71 |
The default hash functions can be changed by calling `hash.RegisterHash`. |
| 72 |
```go |
| 73 |
func init() { |
| 74 |
hash.RegisterHash(crypto.SHA1, sha1.New) |
| 75 |
} |
| 76 |
``` |
| 77 |
|
| 78 |
New `SHA1` or `SHA256` hash functions that implement the `hash.RegisterHash` interface can be registered by calling `RegisterHash`. |