英文:
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"
我的问题
- 如何使用 go-git 添加子模块?
- 我是否需要单独的
sw.Checkout(&git.CheckoutOptions
部分?如果需要,应该按什么顺序执行? - 如何配置主仓库或子模块的稀疏检出?
- 对于
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 <url> <submod_name>
, 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.
✅ 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>
- 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.
⬜ 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?
⬜ 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?
✅ 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>
- Not sure if this needs two seperate calls?
Worktree.Pull()
&Worktree.Pull()
, see code below.
✅ git pull <remote_name> main
✅ git add .
✅ git commit -S -m "commit example message"
My Questions
- How do I add a submodule using go-git?
- Do I need the separate
sw.Checkout(&git.CheckoutOptions
bit? And if so, in what order should I do them? - How do I go about configuring a Sparse Checkout in either the main repo OR the submodule?
- 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 <submod_name>
// Get the repo object (could be git.plainOpen() too)
r, err := git.PlainClone(directory, false, &git.CloneOptions{
<options>
})
// 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(&git.PullOptions{
<options>
}
// Checkout the submodule, Not sure if this is the right order?
err = sw.Checkout(&git.CheckoutOptions{
<options>
}
答案1
得分: 2
从submodule.go
和submodule_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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论