0

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?

torek
  • 448,244
  • 59
  • 642
  • 775
RamPrakash
  • 2,218
  • 3
  • 25
  • 54
  • The go-git implementation doesn't use Git, so this is a pure go-git question. – torek Dec 17 '21 at 04:39
  • I cannot reproduce this, the `PlainClone` method correctly clones the submodules of a repo that I tested it with. – Oliver Tale-Yazdi Dec 18 '21 at 00:12
  • 1
    Yes, this needs a [mcve]. – torek Dec 18 '21 at 00:26
  • @OliverTale-Yazdi My parent module contains nothing but `.gitmodule` which is the above file. Do you have anything else? – RamPrakash Dec 18 '21 at 01:49
  • @torek So what happens when you clone this [example repo](https://github.com/githubtraining/example-dependency)? Is the content of the `js` folder present? Otherwise I would need to see the code to help you better. – Oliver Tale-Yazdi Dec 18 '21 at 18:23
  • @OliverTale-Yazdi: using plain (not go-) Git, I get: `git submodule status` => `-c3c588713233609f5bbbb2d9e7f3fb4a660f3f72 js`. The current branch (as recommended by https://github.com/githubtraining/example-dependency) is `master` and its tip commit contains a gitlink named `js` pointing to that commit; the `.gitmodules` file has instructions for cloning the submodule. A subsequent `git submodule update --init` clones the submodule and does a detached-HEAD checkout of that commit, as one might expect. What does `go-git` do when one provides the remaining MRE? Where is that remaining MRE? – torek Dec 19 '21 at 03:22

1 Answers1

2

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.