From 199a7a321854ac368f4532e7e098f01cec2cc7c4 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Aillet Date: Thu, 14 May 2020 21:32:46 +0200 Subject: _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` --- _examples/submodule/main.go | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 _examples/submodule/main.go (limited to '_examples') 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 := 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) + } +} -- cgit From 18a3f6436541b17b3d1706dff540e1ec13f2229f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Aillet Date: Mon, 18 May 2020 11:06:36 +0200 Subject: doc: add submodule to example index --- _examples/README.md | 1 + 1 file changed, 1 insertion(+) (limited to '_examples') 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 -- cgit