如何在多个子模块中使用 –reference

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

How to use --reference with multiple submodules

问题

让我们假设我有一个包含许多子模块的参考仓库,初始化方式如下:

    git clone "ssh://$USER@example.com/my_repo" repo_reference
    git submodule update --init --recursive

我再次使用 `--reference` 选项克隆它:

    git clone "ssh://$USER@example.com/my_repo" --reference repo_reference repo

现在,我想使用 `repo_reference` 作为参考来初始化它的子模块。

### 尝试 1

    git submodule update --init --recursive --reference repo_reference

但是,参考标志似乎被忽略了。它对每个子模块都执行了完整的远程克隆。

### 尝试 2
我希望可以使用 `$sm_path` 变量:

    git submodule update --init --recursive --reference 'repo_reference/$sm_path'

但它没有被解析:

    fatal: reference repository 'repo_reference/$sm_path' is not a local repository.

### 尝试 3

我尝试使用 `git submodule foreach --recursive`,这样我可以逐个初始化子模块。但是,这个命令[什么也没做](https://stackoverflow.com/q/8778323/1282773),除非所有子模块都被初始化和更新。

### 问题

我如何避免完整克隆并使其工作?
英文:

Let's say I have a reference repo with many submodules initialized as follows:

git clone "ssh://$USER@example.com/my_repo" repo_reference
git submodule update --init --recursive

I'm cloning it again using --reference:

git clone "ssh://$USER@example.com/my_repo" --reference repo_reference repo

And now, I want to initialize its submodules using repo_reference as a reference.

Attempt 1

git submodule update --init --recursive --reference repo_reference

But the reference flag seems to be ignored. It does full clone from the remote for each submodule.

Attempt 2

I hoped I can use the $sm_path variable:

git submodule update --init --recursive --reference 'repo_reference/$sm_path'

But it is not resolved:

fatal: reference repository 'repo_reference/$sm_path' is not a local repository.

Attempt 3

I tried using git submodule foreach --recursive, so I can initialize submodules one by one. But this command does nothing unless all submodules are initialized and updated.

Question

How do I avoid the full clone and make it work?

答案1

得分: 1

在初始克隆之后,遍历.gitmodules中的所有子模块,并逐个更新子模块:

git config -f .gitmodules --get-regexp '^\submodule\..*\.path$' |
while read path_key path; do
    git submodule update --init --reference="repo_reference/$path" "$path"
done

请注意双引号 —— 它们用于代替单引号(撇号),以便让shell扩展变量$path

我已经测试过,对我有效。

没有递归,抱歉。

英文:

After the initial clone run a loop over all submodules from .gitmodules and update submodules one by one:

git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
while read path_key path; do
    git submodule update --init --reference="repo_reference/$path" "$path"
done

Please note double quotes — they're used instead of single quotes (apostrophes) to allow shell to expand the variable $path.

I tested this and it works for me.

No recursion, sorry.

答案2

得分: 1

如果您是首次克隆存储库,可以使用 --recurse-submodules 选项。

git clone \
  "ssh://$USER@example.com/my_repo" \
  --reference repo_reference repo \
  --recurse-submodules

我会假设克隆命令会将参考传递给子模块初始化。不过,需要查看git源代码来验证这一假设。

英文:

If you are cloning the repository fresh, you can use --recurse-submodules.

git clone \
  "ssh://$USER@example.com/my_repo" \
  --reference repo_reference repo \
  --recurse-submodules

I would assume that the clone command would pass the reference to the submodule initialisation. Would have to look at the git source code to check that assumption though.

huangapple
  • 本文由 发表于 2023年2月24日 01:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548280.html
匿名

发表评论

匿名网友

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

确定