Skip to content

Commit 199a7a3

Browse files
committed
_examples: submodule, adding an example with submodule update
the goal of this pull request is to add an example of how to make the equivalent of `git submodule update --remote` with `go-git`
1 parent 6f9eb4e commit 199a7a3

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

_examples/submodule/main.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
// Basic example of how to clone a repository including a submodule and
11+
// updating submodule ref
12+
func main() {
13+
CheckArgs("<url>", "<directory>", "<submodule>")
14+
url := os.Args[1]
15+
directory := os.Args[2]
16+
submodule := os.Args[3]
17+
18+
// Clone the given repository to the given directory
19+
Info("git clone %s %s --recursive", url, directory)
20+
21+
r, err := git.PlainClone(directory, false, &git.CloneOptions{
22+
URL: url,
23+
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
24+
})
25+
26+
CheckIfError(err)
27+
28+
w, err := r.Worktree()
29+
if err != nil {
30+
CheckIfError(err)
31+
}
32+
33+
sub, err := w.Submodule(submodule)
34+
if err != nil {
35+
CheckIfError(err)
36+
}
37+
38+
sr, err := sub.Repository()
39+
if err != nil {
40+
CheckIfError(err)
41+
}
42+
43+
sw, err := sr.Worktree()
44+
if err != nil {
45+
CheckIfError(err)
46+
}
47+
48+
Info("git submodule update --remote")
49+
err = sw.Pull(&git.PullOptions{
50+
RemoteName: "origin",
51+
})
52+
if err != nil {
53+
CheckIfError(err)
54+
}
55+
}

0 commit comments

Comments
 (0)