go-git“git子模块添加”<子模块>”

发布于 2025-01-09 22:57:12 字数 2840 浏览 0 评论 0原文

TLDR

我一直在搜索软件包 Docs & ;官方 示例 并且不知道如何描述使用 go-git 添加子模块。我正在尝试 git submodule add;,以及将其配置为稀疏结帐

目标

我想使用 golang 的 go-git 包转换以下列表。我的总体目标是拥有一个稀疏签出的子模块,该子模块来自 Repo-A,然后整个存储库将来自 Repo-B。我正在尝试从 Repo-A 获取内容,进行一些编辑并重新组织,然后将结果推送到 Repo-B。我在下面包含的所有内容都是第一步,特别是如何从头开始配置这个 repo+submod。

git init
git remote add -f;
git clone --depth=1 --no-checkout;
git 子模块添加;

  • 不确定如何调用它,我认为直接编辑 .git 目录可能是可能的,但这看起来很混乱。任何解释或建议将不胜感激。

git submodule Absorbgitdirs

  • 不确定是否需要这样做,我找不到它的作用的良好描述,但它似乎无论如何都不会引起问题?

git -C Pinkbrain config core.sparseCheckout true (注意:仅适用于子模块)

  • 不知道如何使其工作,因为它是 .git 配置,但仅适用于子模块。也许只是创建一个新的 git.PlainOpen() 对象并以这种方式访问​​其工作树?

echo "cir-1" >>.git/modules//info/sparse-checkout
echo "cir-2" >>.git/modules//info/sparse-checkout
git submodule update --force --checkout

  • 不确定这是否需要两次单独的调用? Worktree.Pull() & Worktree.Pull(),请参阅下面的代码。

git pull;主要
git add .
git commit -S -m "commit example message"

我的问题

  1. 如何使用 go-git 添加子模块?
  2. 我是否需要单独的 sw.Checkout(&git.CheckoutOptions 位?如果需要,我应该按什么顺序执行它们?
  3. 我如何在主存储库或中配置稀疏结帐 关于 git 子模块 Absorbgitdirs 的任何想法吗
  4. ?有关它的任何信息,我将不胜感激。

到目前为止(精简)

注意:我有更多,但它与我的问题无关
该位仅用于子模块更新,因为 go-git 文档不允许 --checkout 作为 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>
}

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

  1. How do I add a submodule using go-git?
  2. Do I need the separate sw.Checkout(&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 <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>
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2025-01-16 22:57:12

submodule.gosubmodule_test.go,添加子模块似乎没有实现。

这意味着您需要使用 client.goexec.Command自己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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文