Home > database >  Go Git - Recurse Submodules
Go Git - Recurse Submodules

Time:12-21

I have a project which contains submodules as shown here.

[submodule "repo-a"]
    path = repo-a
    url = https://example.com/scm/repo-a.git
[submodule "repo-b"]
    path = repo-b
    url = https://example.com/scm/repo-b.git
[submodule "repo-c"]
    path = repo-c
    url = https://example.com/scm/repo-c.git

I am using go-git pkg and trying to clone with options as shown here,

cloneOpts := &git.CloneOptions{
      URL:               url,
      RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
}

It does not recursively pull the submodules. I see only empty directories. Am I missing something?

CodePudding user response:

Even after your comments, I have no idea how you are currently using go-git; so please provide your source code.

I am now answering your original question of "how to clone a repo and its submodules" in go-git:

package main

import (
    "os"

    "github.com/go-git/go-git/v5"
)

func main() {
    repoURL := "https://github.com/githubtraining/example-dependency"
    clonePath := "example-repo"

    _, err := git.PlainClone(clonePath, false, &git.CloneOptions{
        URL:      repoURL,
        Progress: os.Stdout,
        // Enable submodule cloning.
        RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
    })

    if err != nil {
        panic(err)
    }

    println("Have a look at example-repo/js to see a cloned sub-module")
}

As you can see after running this, example-repo/js contains the cloned submodule.

  • Related