diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2020-05-19 00:03:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-19 00:03:46 +0200 |
commit | 838eb2979f6085fc0e5bc8856e7f1944a455a970 (patch) | |
tree | d6aef02848e662cbe35ef852532ca4536d0db56f /_examples/submodule | |
parent | 392a78f88aef72419b81e168e93e46bd0784b423 (diff) | |
parent | 18a3f6436541b17b3d1706dff540e1ec13f2229f (diff) | |
download | go-git-838eb2979f6085fc0e5bc8856e7f1944a455a970.tar.gz |
Merge pull request #63 from pyaillet/doc/submodule-update-example
_examples: submodule, adding an example with submodule update
Diffstat (limited to '_examples/submodule')
-rw-r--r-- | _examples/submodule/main.go | 55 |
1 files changed, 55 insertions, 0 deletions
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) + } +} |