草庐IT

git - 如何制作浅的 git 子模块?

coder 2023-06-23 原文

是否可以有浅子模块?我有一个包含几个子模块的 super 项目,每个子模块都有很长的历史,所以它不必要地拖着所有的历史。

我找到的只有 this unanswered thread .

我应该只是hack git-submodule实现这个?

最佳答案

即将推出的 git1.8.4 (July 2013) 中的新功能:

"git submodule update" can optionally clone the submodule repositories shallowly.


(并且 git 2.10 Q3 2016 允许使用 git config -f .gitmodules submodule.<name>.shallow true 记录。
请参阅此答案的结尾)
commit 275cd184d52b5b81cb89e4ec33e540fb2ae61c1f :

Add the --depth option to the add and update commands of "git submodule", which is then passed on to the clone command. This is useful when the submodule(s) are huge and you're not really interested in anything but the latest commit.


Tests are added and some indention adjustments were made to conform to the rest of the testfile on "submodule update can handle symbolic links in pwd".


Signed-off-by: Fredrik Gustafsson <iveqy@iveqy.com>
Acked-by: Jens Lehmann <Jens.Lehmann@web.de>


这意味着这有效:
# add shallow submodule
git submodule add --depth 1 <repo-url> <path>
git config -f .gitmodules submodule.<path>.shallow true

# later unshallow
git config -f .gitmodules submodule.<path>.shallow false
git submodule update <path>
命令可以按任何顺序运行。 git submodule 命令执行实际的克隆(这次使用深度 1)。 git config 命令使该选项对于稍后将递归克隆 repo 的其他人永久存在。
例如,假设您有 repo https://github.com/foo/bar 并且您想在您的 repo https://github.com/lorem/ipsum 中添加 path/to/submodule 作为子模块。命令可能如下所示:
git submodule add --depth 1 git@github.com:lorem/ipsum.git path/to/submodule
git config -f .gitmodules submodule.path/to/submodule.shallow true
以下结果也相同(相反的顺序):
git config -f .gitmodules submodule.path/to/submodule.shallow true
git submodule add --depth 1 git@github.com:lorem/ipsum.git path/to/submodule
下次有人运行 git clone --recursive git@github.com:foo/bar.git 时,它​​会 pull 入 https://github.com/foo/bar 的整个历史记录,但它只会按预期浅克隆子模块。
与:
--depth

This option is valid for add and update commands.
Create a 'shallow' clone with a history truncated to the specified number of revisions.



atwyman 添加 in the comments :

As far as I can tell this option isn't usable for submodules which don't track master very closely. If you set depth 1, then submodule update can only ever succeed if the submodule commit you want is the latest master. Otherwise you get "fatal: reference is not a tree".


这是真的。
也就是说,直到 git 2.8(2016 年 3 月)。在 2.8 中,submodule update --depth 还有一次成功的机会,即使 SHA1 可以从远程仓库 HEAD 之一直接访问。
请参阅 commit fb43e31Stefan Beller ( stefanbeller )(2016 年 2 月 24 日)。
帮助者: Junio C Hamano ( gitster )
(由 Junio C Hamano -- gitster --commit 9671a76 中 merge ,2016 年 2 月 26 日)

submodule: try harder to fetch needed sha1 by direct fetching sha1


When reviewing a change that also updates a submodule in Gerrit, a common review practice is to download and cherry-pick the patch locally to test it.
However when testing it locally, the 'git submodule update' may fail fetching the correct submodule sha1 as the corresponding commit in the submodule is not yet part of the project history, but also just a proposed change.


If $sha1 was not part of the default fetch, we try to fetch the $sha1 directly. Some servers however do not support direct fetch by sha1, which leads git-fetch to fail quickly.
We can fail ourselves here as the still missing sha1 would lead to a failure later in the checkout stage anyway, so failing here is as good as we can get.



MVGin the comments 指向 commit fb43e31(git 2.9,2016 年 2 月)

It would seem to me that commit fb43e31 requests the missing commit by SHA1 id, so the uploadpack.allowReachableSHA1InWant and uploadpack.allowTipSHA1InWant settings on the server will probably affect whether this works.
I wrote a post to the git list today, pointing out how the use of shallow submodules could be made to work better for some scenarios, namely if the commit is also a tag.
Let's wait and see.


I guess this is a reason why fb43e31 made the fetch for a specific SHA1 a fallback after the fetch for the default branch.
Nevertheless, in case of “--depth 1” I think it would make sense to abort early: if none of the listed refs matches the requested one, and asking by SHA1 isn't supported by the server, then there is no point in fetching anything, since we won't be able to satisfy the submodule requirement either way.



2016 年 8 月更新(3 年后)
使用 Git 2.10(2016 年第三季度),您将能够做到
 git config -f .gitmodules submodule.<name>.shallow true
有关更多信息,请参阅“Git submodule without extra weight ”。

Git 2.13(2017 年第二季度)确实通过 commit 8d3047c 添加了 Sebastian Schuberth ( sschuberth )(2017 年 4 月 19 日)。
(由 Sebastian Schuberth -- sschuberth --commit 8d3047c 中 merge ,2017 年 4 月 20 日)

a clone of this submodule will be performed as a shallow clone (with a history depth of 1)


但是, Ciro Santilli 添加了 in the comments (以及详细信息 in his answer )

shallow = true on .gitmodules only affects the reference tracked by the HEAD of the remote when using --recurse-submodules, even if the target commit is pointed to by a branch, and even if you put branch = mybranch on the .gitmodules as well.



Git 2.20(2018 年第四季度)改进了子模块支持,当工作树中缺少 HEAD:.gitmodules 文件时,子模块支持已更新为从 .gitmodules 处的 blob 读取。
commit 2b1257ecommit 76e9bdc ( 2018 年 10 月 25 日)和 commit b5c259fcommit 23dd8f5commit b2faad4commit 2502ffccommit 996df4dcommit d1b13dfcommit 45f5ef3commit bcbc780 (2018 年 10 月 5 日之前)
(由 Antonio Ospite ( ao2 )Junio C Hamano -- gitster -- 中 merge ,2018 年 11 月 13 日)

submodule: support reading .gitmodules when it's not in the working tree


When the .gitmodules file is not available in the working tree, try using the content from the index and from the current branch.
This covers the case when the file is part of the repository but for some reason it is not checked out, for example because of a sparse checkout.


This makes it possible to use at least the 'git submodule' commands which read the gitmodules configuration file without fully populating the working tree.


Writing to .gitmodules will still require that the file is checked out, so check for that before calling config_set_in_gitmodules_file_gently.


Add a similar check also in git-submodule.sh::cmd_add() to anticipate the eventual failure of the "git submodule add" command when .gitmodules is not safely writeable; this prevents the command from leaving the repository in a spurious state (e.g. the submodule repository was cloned but .gitmodules was not updated because config_set_in_gitmodules_file_gently failed).


Moreover, since config_from_gitmodules() now accesses the global object store, it is necessary to protect all code paths which call the function against concurrent access to the global object store.
Currently this only happens in builtin/grep.c::grep_submodules(), so call grep_read_lock() before invoking code involving config_from_gitmodules().


NOTE: there is one rare case where this new feature does not work properly yet: nested submodules without .gitmodules in their working tree.



注意:Git 2.24(2019 年第四季度)修复了浅层克隆子模块时可能出现的段错误。
请参阅 commit abb4824commit ddb3c85(2019 年 9 月 30 日)。
(由 Ali Utku Selen ( auselen )Junio C Hamano -- gitster -- 中 merge ,2019 年 10 月 9 日)

Git 2.25(2020 年第一季度),阐明了 git submodule update 文档。
请参阅 commit 678a9cacommit f0e58b3(2019 年 11 月 24 日)。
(由 Philippe Blain ( phil-blain )Junio C Hamano -- gitster -- 中 merge ,2019 年 12 月 5 日)

doc: mention that 'git submodule update' fetches missing commits

Helped-by: Junio C Hamano
Helped-by: Johannes Schindelin
Signed-off-by: Philippe Blain


'git submodule update' will fetch new commits from the submodule remote if the SHA-1 recorded in the superproject is not found. This was not mentioned in the documentation.



警告:在 Git 2.25(2020 年第一季度)中,“git clone --recurse-submodules”和备用对象存储之间的交互设计不当。

The documentation and code have been taught to make more clear recommendations when the users see failures.


请参阅 commit ef61045commit 4f3e57ecommit 10c64a0(2019 年 12 月 2 日)。
(由 Jonathan Tan ( jhowtan )Junio C Hamano -- gitster -- 中 merge ,2019 年 12 月 10 日)

submodule--helper: advise on fatal alternate error

Signed-off-by: Jonathan Tan
Acked-by: Jeff King


When recursively cloning a superproject with some shallow modules defined in its .gitmodules, then recloning with "--reference=<path>", an error occurs. For example:


git clone --recurse-submodules --branch=master -j8 \
  https://android.googlesource.com/platform/superproject \
  master
git clone --recurse-submodules --branch=master -j8 \
  https://android.googlesource.com/platform/superproject \
  --reference master master2

fails with:


fatal: submodule '<snip>' cannot add alternate: reference repository
'<snip>' is shallow

When a alternate computed from the superproject's alternate cannot be added, whether in this case or another, advise about configuring the "submodule.alternateErrorStrategy" configuration option and using "--reference-if-able" instead of "--reference" when cloning.


这在:
在 Git 2.25(2020 年第一季度)中,“git clone --recurse-submodules”和备用对象存储之间的交互设计不当。

Doc: explain submodule.alternateErrorStrategy

Signed-off-by: Jonathan Tan
Acked-by: Jeff King


Commit 31224cbdc7 ("clone: recursive and reference option triggers submodule alternates", 2016-08-17, Git v2.11.0-rc0 -- merge listed in batch #1) taught Git to support the configuration options "submodule.alternateLocation" and "submodule.alternateErrorStrategy" on a superproject.


If "submodule.alternateLocation" is configured to "superproject" on a superproject, whenever a submodule of that superproject is cloned, it instead computes the analogous alternate path for that submodule from $GIT_DIR/objects/info/alternates of the superproject, and references it.


The "submodule.alternateErrorStrategy" option determines what happens if that alternate cannot be referenced.
However, it is not clear that the clone proceeds as if no alternate was specified when that option is not set to "die" (as can be seen in the tests in 31224cbdc7).
Therefore, document it accordingly.


commit 5dd1d59 现在包括:
submodule.alternateErrorStrategy::

Specifies how to treat errors with the alternates for a submodule as computed via submodule.alternateLocation.
Possible values are ignore, info, die.
Default is die.
Note that if set to ignore or info, and if there is an error with the computed alternate, the clone proceeds as if no alternate was specified.



注意:“config submodule documentation ”( git submodule update --quiet ) 没有将安静选项向下传播到底层 man ( git fetch ),这已在 Git 2.32(2021 年第二季度)中得到纠正。
请参阅 mancommit 62af4bd(2021 年 4 月 30 日)。
(由 Nicholas Clark ( nwc10 ) merge 于 Junio C Hamano -- gitster -- ,2021 年 5 月 11 日)

submodule update: silence underlying fetch with "--quiet"

Signed-off-by: Nicholas Clark


Commands such as

$ git submodule update --quiet --init --depth=1

involving shallow clones, call the shell function fetch_in_submodule, which in turn invokes git fetch.
Pass the --quiet option onward there.

关于git - 如何制作浅的 git 子模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2144406/

有关git - 如何制作浅的 git 子模块?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  4. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  5. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  6. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  7. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  8. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  9. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  10. ruby - 如何使用文字标量样式在 YAML 中转储字符串? - 2

    我有一大串格式化数据(例如JSON),我想使用Psychinruby​​同时保留格式转储到YAML。基本上,我希望JSON使用literalstyle出现在YAML中:---json:|{"page":1,"results":["item","another"],"total_pages":0}但是,当我使用YAML.dump时,它不使用文字样式。我得到这样的东西:---json:!"{\n\"page\":1,\n\"results\":[\n\"item\",\"another\"\n],\n\"total_pages\":0\n}\n"我如何告诉Psych以想要的样式转储标量?解

随机推荐