去 Git – 递归子模块

huangapple go评论82阅读模式
英文:

Go Git - Recurse Submodules

问题

我有一个包含子模块的项目,如下所示:

[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

我正在使用 go-git 包,并尝试使用以下选项进行克隆:

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

但它并没有递归地拉取子模块。我只看到空目录。我是否漏掉了什么?

英文:

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?

答案1

得分: 2

即使在您的评论之后,我仍然不知道您目前如何使用go-git;所以请提供您的源代码。

我现在回答您最初的问题,即在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,
		// 启用子模块克隆。
		RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
	})

	if err != nil {
		panic(err);
	}

	println("请查看example-repo/js,以查看克隆的子模块");
}

运行此代码后,您会发现example-repo/js中包含了克隆的子模块。

英文:

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.

huangapple
  • 本文由 发表于 2021年12月17日 12:15:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/70388365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定