go-git "git submodule add <url> <submod>"

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

go-git "git submodule add <url> <submod>"

问题

TLDR

我一直在查阅 go-git 的包文档和官方示例,但无法弄清楚如何使用 go-git 描述添加子模块。我想要执行 git submodule add <url> <submod_name>,并配置它进行稀疏检出。

目标

我想要使用 golang 的 go-git 包来转换以下列表。我的总体目标是将子模块进行稀疏检出,子模块来自 Repo-A,而整个仓库是 Repo-B。我想要从 Repo-A 获取内容,进行一些编辑和重新组织,然后将结果推送到 Repo-B。下面我只包含了第一步,具体是如何从头开始配置这个仓库+子模块。

  • git init
  • git remote add -f <remote_name> <repo_url>
  • git clone --depth=1 --no-checkout <submod_repo_url> <submod_name>
  • git submodule add <submod_repo_url> <submod_name>
    • 不确定如何调用这个命令,我认为可能可以通过直接编辑 .git 目录来实现,但这似乎很混乱。如果有解释或建议,将不胜感激。
  • git submodule absorbgitdirs
    • 不确定是否需要这个命令,我找不到对它的详细描述,但似乎无论如何都不会引起问题。
  • git -C pinkbrain config core.sparseCheckout true(注意:仅适用于子模块)
    • 不确定如何使其工作,因为它是一个 .git 配置,但仅适用于子模块。也许只需创建一个新的 git.PlainOpen() 对象,然后以这种方式访问其工作树。
  • echo "cir-1" >> .git/modules/<submod_name>/info/sparse-checkout
  • echo "cir-2" >> .git/modules/<submod_name>/info/sparse-checkout
  • git submodule update --force --checkout <submod_name>
    • 不确定是否需要两个单独的调用?Worktree.Pull()Worktree.Pull(),请参考下面的代码。
  • git pull <remote_name> main
  • git add .
  • git commit -S -m "commit example message"

我的问题

  1. 如何使用 go-git 添加子模块?
  2. 我是否需要单独的 sw.Checkout(&git.CheckoutOptions 部分?如果需要,应该按什么顺序执行?
  3. 如何配置主仓库或子模块的稀疏检出?
  4. 对于 git submodule absorbgitdirs 有什么想法?如果有关于它的任何信息,将不胜感激。

我目前的进展(简化版)

注意:我还有更多内容,但与我的问题无关。下面的代码只是为了子模块更新,因为 go-git 的文档不允许在 git.PullOptions() 中使用 --checkout

// 相当于:git submodule update --force --checkout <submod_name>
// 获取仓库对象(也可以使用 git.plainOpen())
r, err := git.PlainClone(directory, false, &git.CloneOptions{
	<options>
})

// 获取子模块的工作树对象
w, err := r.Worktree()
sub, err := w.Submodule(submodule)
sr, err := sub.Init()
sw, err := sr.Worktree()

// 更新子模块
err = sw.Pull(&git.PullOptions{
	<options>
})

// 检出子模块,不确定这个顺序是否正确?
err = sw.Checkout(&git.CheckoutOptions{
	<options>
})
英文:

TLDR

I've been scouring the package Docs & the official Examples and can't figure out how to describe adding a submodule using go-git. I'm trying to git submodule add &lt;url&gt; &lt;submod_name&gt;, as well as configure it for sparse checkout

Goal

I want to convert the following list using golang's go-git package. My overall goal is to have a sparsely checked out submodule, which will be from Repo-A, then the overall repo will be for Repo-B. I'm trying to source content from Repo-A, make some edits, and reorganize, then push the results to Repo-B. All that I've included below is the first step, specifically for how to configure this repo+submod from scratch.

&#9989; git init
&#9989; git remote add -f &lt;remote_name&gt; &lt;repo_url&gt;
&#9989; git clone --depth=1 --no-checkout &lt;submod_repo_url&gt; &lt;submod_name&gt;
&#11036; git submodule add &lt;submod_repo_url&gt; &lt;submod_name&gt;

  • Not sure how to call this, I'm thinking it may be possible with edits to the .git directory directly, but that seems messy. Any explanations or suggestions would be appreciated.

&#11036; git submodule absorbgitdirs

  • Not sure if this is even needed, I couldn't find a good description of what it does, but it seems to not cause a problem either way?

&#11036; git -C pinkbrain config core.sparseCheckout true (NOTE: will only apply to the submodule)

  • Not sure how to make this work, since its a .git config, but only for the submodule. Maybe just create a new git.PlainOpen() object and access its working tree that way?

&#9989; echo &quot;cir-1&quot; &gt;&gt;.git/modules/&lt;submod_name&gt;/info/sparse-checkout
&#9989; echo &quot;cir-2&quot; &gt;&gt;.git/modules/&lt;submod_name&gt;/info/sparse-checkout
&#9989; git submodule update --force --checkout &lt;submod_name&gt;

  • Not sure if this needs two seperate calls? Worktree.Pull() & Worktree.Pull(), see code below.

&#9989; git pull &lt;remote_name&gt; main
&#9989; git add .
&#9989; git commit -S -m &quot;commit example message&quot;

My Questions

  1. How do I add a submodule using go-git?
  2. Do I need the separate sw.Checkout(&amp;git.CheckoutOptions bit? And if so, in what order should I do them?
  3. How do I go about configuring a Sparse Checkout in either the main repo OR the submodule?
  4. Any thoughts on the git submodule absorbgitdirs? Any info about what it is would be appreciated

What I've got so far (condensed)

NOTE: I have more, but it doesn't relate to my questions
This bit is just for the submodule update, since the go-git documentation doesn't allow for --checkout as part of the git.PullOptions()

// Should be equivalent to : git submodule update --force --checkout &lt;submod_name&gt;
// Get the repo object (could be git.plainOpen() too)
r, err := git.PlainClone(directory, false, &amp;git.CloneOptions{
	&lt;options&gt;
})

// Get the Submodule WorkTree object
w, err := r.Worktree()
sub, err := w.Submodule(submodule)
sr, err := sub.Init()
sw, err := sr.Worktree()

// Get the Update the Submodule
err = sw.Pull(&amp;git.PullOptions{
	&lt;options&gt;
}

// Checkout the submodule, Not sure if this is the right order?
err = sw.Checkout(&amp;git.CheckoutOptions{
	&lt;options&gt;
}

答案1

得分: 2

submodule.gosubmodule_test.go的当前状态来看,似乎没有实现添加子模块的功能。

这意味着你需要使用client.go来自己执行git submodule add命令。

英文:

From the current state of submodule.go and submodule_test.go, adding a submodule does not seem implemented.

That means you would need to use the client.go to exec.Command the git submodule add yourself.

huangapple
  • 本文由 发表于 2022年2月25日 13:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/71261367.html
匿名

发表评论

匿名网友

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

确定