Skip to content

Commit db94798

Browse files
committed
_examples: Add sparse checkout example.
1 parent 7e9b3e0 commit db94798

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

COMPATIBILITY.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ compatibility status with go-git.
3434
| `merge` | | ⚠️ (partial) | Fast-forward only | |
3535
| `mergetool` | || | |
3636
| `stash` | || | |
37+
| `sparse-checkout` | || | - [sparse-checkout](_examples/sparse-checkout/main.go) |
3738
| `tag` | || | - [tag](_examples/tag/main.go) <br/> - [tag create and push](_examples/tag-create-push/main.go) |
3839

3940
## Sharing and updating projects

_examples/common_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var args = map[string][]string{
3333
"revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
3434
"sha256": {tempFolder()},
3535
"showcase": {defaultURL, tempFolder()},
36+
"sparse-checkout": {defaultURL, "vendor", tempFolder()},
3637
"tag": {cloneRepository(defaultURL, tempFolder())},
3738
}
3839

_examples/sparse-checkout/main.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/go-git/go-git/v5"
7+
. "github.com/go-git/go-git/v5/_examples"
8+
)
9+
10+
func main() {
11+
CheckArgs("<url>", "<sparse_path>", "<directory>")
12+
url := os.Args[1]
13+
path := os.Args[2]
14+
directory := os.Args[3]
15+
16+
Info("git clone %s %s", url, directory)
17+
18+
r, err := git.PlainClone(directory, false, &git.CloneOptions{
19+
URL: url,
20+
NoCheckout: true,
21+
})
22+
CheckIfError(err)
23+
24+
w, err := r.Worktree()
25+
CheckIfError(err)
26+
27+
err = w.Checkout(&git.CheckoutOptions{
28+
SparseCheckoutDirectories: []string{path},
29+
})
30+
CheckIfError(err)
31+
}

0 commit comments

Comments
 (0)