|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Advanced Usage of go-git |
| 4 | +--- |
| 5 | +# Advanced Usage of go-git |
| 6 | + |
| 7 | +This guide covers advanced usage patterns and performance considerations |
| 8 | +when working with go-git. |
| 9 | + |
| 10 | +## Performance Optimizations |
| 11 | + |
| 12 | +When working with large repositories or performing intensive operations, |
| 13 | +consider the following performance tips: |
| 14 | + |
| 15 | +### Reduce the amount of data being cloned |
| 16 | +- Shallow clones with setting depth to 1. |
| 17 | +- Use Sparse Checkout for only representing part of the repository tree |
| 18 | +in the Worktree. |
| 19 | +- When no tags are required, use `git.NoTags`. This will skip the cloning |
| 20 | +of tags, and all the objects they point to - which may result in a |
| 21 | +significant increase in cloned data. |
| 22 | + |
| 23 | +### Storage Optimization |
| 24 | +- The fastest storage is the memory storage. For very large repositories |
| 25 | +make sure there is enough memory available. Used in conjunction with the |
| 26 | +filter option can lead to super efficient operations. |
| 27 | + |
| 28 | +### SHA1 |
| 29 | +- By default, go-git uses `github.com/pjbgf/sha1cd` as the SHA1 algorithm. |
| 30 | +It introduces collision detection, which results in slower performance. |
| 31 | +When only trustworthy servers are used, consider using the native SHA1 |
| 32 | +implementation with `hash.RegisterHash(sha1.New())`. |
| 33 | + |
| 34 | +## In-memory repositories |
| 35 | + |
| 36 | +### Clone in between repositories held in memory |
| 37 | + |
| 38 | +Here's an example of using a memfs filesystem, which can be used for multiple |
| 39 | +repositories. In this case, it can also be used to clone in-between sub-memfs |
| 40 | +filesystems which in fact reside on a higher level memfs. |
| 41 | + |
| 42 | +```go |
| 43 | +var wg sync.WaitGroup |
| 44 | + |
| 45 | +// / |
| 46 | +// /target |
| 47 | +// /src |
| 48 | +// /go-git |
| 49 | +// /go-billy |
| 50 | +func main() { |
| 51 | + rootfs := memfs.New() |
| 52 | + target, _ := rootfs.Chroot("/target") |
| 53 | + src, _ := rootfs.Chroot("/src") |
| 54 | + master, _ := src.Chroot("/go-git-master") |
| 55 | + concurrency, _ := src.Chroot("/go-git-concurrency") |
| 56 | + |
| 57 | + wg.Add(2) |
| 58 | + // providing a thread-safe memfs enables running the below as go routines |
| 59 | + clone(master, "https://github.com/go-git/go-git", "master") |
| 60 | + clone(concurrency, "https://github.com/go-git/go-git", "read-concurrency") |
| 61 | + wg.Wait() |
| 62 | + |
| 63 | + loader := server.NewFilesystemLoader(rootfs) |
| 64 | + client.InstallProtocol("file", server.NewClient(loader)) |
| 65 | + |
| 66 | + url := "file:///src/go-git-master" |
| 67 | + |
| 68 | + dot, err := target.Chroot(".git") |
| 69 | + CheckIfError(err) |
| 70 | + |
| 71 | + Info("git clone %s", url) |
| 72 | + storer := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) |
| 73 | + r, err := git.Clone(storer, target, &git.CloneOptions{ |
| 74 | + URL: url, |
| 75 | + }) |
| 76 | + CheckIfError(err) |
| 77 | + |
| 78 | + ref, err := r.Head() |
| 79 | + CheckIfError(err) |
| 80 | + |
| 81 | + commit, err := r.CommitObject(ref.Hash()) |
| 82 | + CheckIfError(err) |
| 83 | + |
| 84 | + fmt.Println(commit) |
| 85 | + |
| 86 | + url2 := "file:///src/go-git-concurrency" |
| 87 | + err = r.Fetch(&git.FetchOptions{ |
| 88 | + RemoteURL: url2, |
| 89 | + RefSpecs: []config.RefSpec{ |
| 90 | + ... |
| 91 | + }, |
| 92 | + }) |
| 93 | + CheckIfError(err) |
| 94 | +} |
| 95 | + |
| 96 | +func clone(fs billy.Filesystem, url, branch string) { |
| 97 | + storer := filesystem.NewStorage(fs, cache.NewObjectLRUDefault()) |
| 98 | + |
| 99 | + _, err := git.Clone(storer, nil, &git.CloneOptions{ |
| 100 | + URL: url, |
| 101 | + SingleBranch: true, |
| 102 | + ReferenceName: plumbing.NewBranchReferenceName(branch), |
| 103 | + }) |
| 104 | + wg.Done() |
| 105 | + |
| 106 | + CheckIfError(err) |
| 107 | + fmt.Printf("Clone %q from %q: DONE\n", branch, url) |
| 108 | +} |
| 109 | +``` |
0 commit comments