aboutsummaryrefslogtreecommitdiffstats
path: root/_examples
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2020-05-24 14:34:30 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2020-05-24 14:34:30 +0200
commitd13825550a301d8e087122bbec87a2698424af27 (patch)
treefb9d3fdcde4981d89b32e2a3c98deddfc31d0527 /_examples
parenta107e18b9327e9a5da7a44b3a872b2f43598dfb4 (diff)
parent6d8103df45ce09ffd5323b4ef46d26440400a54f (diff)
downloadgo-git-d13825550a301d8e087122bbec87a2698424af27.tar.gz
Merge branch 'master' of github.com:go-git/go-git into scope-config
Diffstat (limited to '_examples')
-rw-r--r--_examples/README.md1
-rw-r--r--_examples/submodule/main.go55
2 files changed, 56 insertions, 0 deletions
diff --git a/_examples/README.md b/_examples/README.md
index cf9c2d3..6c17941 100644
--- a/_examples/README.md
+++ b/_examples/README.md
@@ -20,6 +20,7 @@ Here you can find a list of annotated _go-git_ examples:
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc
- [progress](progress/main.go) - Printing the progress information from the sideband
- [revision](revision/main.go) - Solve a revision into a commit
+- [submodule](submodule/main.go) - Submodule update remote
### Advanced
- [custom_http](custom_http/main.go) - Replacing the HTTP client using a custom one
diff --git a/_examples/submodule/main.go b/_examples/submodule/main.go
new file mode 100644
index 0000000..1a76193
--- /dev/null
+++ b/_examples/submodule/main.go
@@ -0,0 +1,55 @@
+package main
+
+import (
+ "os"
+
+ "github.com/go-git/go-git/v5"
+ . "github.com/go-git/go-git/v5/_examples"
+)
+
+// Basic example of how to clone a repository including a submodule and
+// updating submodule ref
+func main() {
+ CheckArgs("<url>", "<directory>", "<submodule>")
+ url := os.Args[1]
+ directory := os.Args[2]
+ submodule := os.Args[3]
+
+ // Clone the given repository to the given directory
+ Info("git clone %s %s --recursive", url, directory)
+
+ r, err := git.PlainClone(directory, false, &git.CloneOptions{
+ URL: url,
+ RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
+ })
+
+ CheckIfError(err)
+
+ w, err := r.Worktree()
+ if err != nil {
+ CheckIfError(err)
+ }
+
+ sub, err := w.Submodule(submodule)
+ if err != nil {
+ CheckIfError(err)
+ }
+
+ sr, err := sub.Repository()
+ if err != nil {
+ CheckIfError(err)
+ }
+
+ sw, err := sr.Worktree()
+ if err != nil {
+ CheckIfError(err)
+ }
+
+ Info("git submodule update --remote")
+ err = sw.Pull(&git.PullOptions{
+ RemoteName: "origin",
+ })
+ if err != nil {
+ CheckIfError(err)
+ }
+}