Skip to content

Commit 838eb29

Browse files
authored
Merge pull request go-git#63 from pyaillet/doc/submodule-update-example
_examples: submodule, adding an example with submodule update
2 parents 392a78f + 18a3f64 commit 838eb29

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

_examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Here you can find a list of annotated _go-git_ examples:
2020
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc
2121
- [progress](progress/main.go) - Printing the progress information from the sideband
2222
- [revision](revision/main.go) - Solve a revision into a commit
23+
- [submodule](submodule/main.go) - Submodule update remote
2324

2425
### Advanced
2526
- [custom_http](custom_http/main.go) - Replacing the HTTP client using a custom one

_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)