1package main
2
3import (
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
12func 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}