git clone --filter
此选项是与远程协议的更新一起添加的,它确实可以防止从服务器下载对象。
例如,仅克隆
d1
https://github.com/cirosantilli/test-git-partial-clone
我可以做到:
git clone \
--depth 1 \
--filter=blob:none \
--sparse \
https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git sparse-checkout set d1
这里有一个不那么简单和更现实的版本
https://github.com/cirosantilli/test-git-partial-clone-big-small
git clone \
--depth 1 \
--filter=blob:none \
--sparse \
https://github.com/cirosantilli/test-git-partial-clone-big-small \
;
cd test-git-partial-clone-big-small
git sparse-checkout set small
该存储库包含:
-
包含10个10MB文件的大目录
-
一个小目录,包含1000个大小为一字节的文件
所有内容都是伪随机的,因此不可压缩。
36.4 Mbps internet上的克隆次数:
这个
sparse-checkout
git clone \
--depth 1 \
--filter=blob:none \
--no-checkout \
https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git checkout master -- d1
但这种方法是有原因的
downloads files one by one very slowly
,使其无法使用,除非目录中的文件很少。
分析最小存储库中的对象
clone命令仅获取:
然后,
git sparse-checkout set
命令仅从服务器获取丢失的blob(文件):
更好的是,稍后GitHub可能会开始支持:
--filter=blob:none \
--filter=tree:0 \
--filter=tree:0
from Git 2.20
将防止不必要的
clone
获取所有树对象,并允许将其延迟到
checkout
. 但在我的2020-09-18测试中,失败的原因是:
fatal: invalid filter-spec 'combine:blob:none+tree:0'
--filter=combine:
复合过滤器(在Git 2.24中添加,由多个
--filter
)尚未实施。
我观察到哪些对象是通过以下方式获取的:
git verify-pack -v .git/objects/pack/*.pack
如所述:
How to list ALL git objects in the database?
它并没有给我一个超清晰的指示,每个对象是什么,但它确实说明了每个对象的类型(
commit
tree
,
blob
git rev-list --objects --all
确实为树/blob生成了更清晰的路径输出,但不幸的是,当我运行它时,它会提取一些对象,这使得很难确定提取了什么。如果有人有更好的命令,请告诉我。
https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/
--filter blob:none
.
git sparse-checkout
我认为这个命令是用来管理一个设置文件,上面写着“我只关心这些子树”,这样将来的命令只会影响这些子树。但这有点难以确定,因为当前的文档有点。。。稀疏;-)
它本身并不阻止抓取blob。
如果这种理解是正确的,那么这将是对
git克隆--过滤器
当我在Git 2.25.1上试用时:
git clone \
--depth 1 \
--filter=blob:none \
--no-checkout \
https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git sparse-checkout init
init
实际获取了所有对象。
然而,在Git2.28中,它没有按照需要获取对象。但如果我这样做了:
git sparse-checkout set d1
d1
未提取并签出,即使这明确说明它应该:
https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/#sparse-checkout-and-partial-clones
请注意部分克隆功能是否可以普遍使用[1]。
[1] :GitHub仍在内部评估此功能,同时在一些选定的存储库(包括本文中使用的示例)上启用此功能。随着功能的稳定和成熟,我们会不断更新它的进度。
所以是的,现在很难确定,部分原因是GitHub开源带来的乐趣。但是让我们注意一下。
命令分解
服务器应配置为:
git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1
--滤器
man git-rev-list
.
Git树上的文档:
https://github.com/cirosantilli/test-git-partial-clone
本地存储库,进行本地克隆,并观察克隆的内容:
#!/usr/bin/env bash
set -eu
list-objects() (
git rev-list --all --objects
echo "master commit SHA: $(git log -1 --format="%H")"
echo "mybranch commit SHA: $(git log -1 --format="%H")"
git ls-tree master
git ls-tree mybranch | grep mybranch
git ls-tree master~ | grep root
)
# Reproducibility.
export GIT_COMMITTER_NAME='a'
export GIT_COMMITTER_EMAIL='a'
export GIT_AUTHOR_NAME='a'
export GIT_AUTHOR_EMAIL='a'
export GIT_COMMITTER_DATE='2000-01-01T00:00:00+0000'
export GIT_AUTHOR_DATE='2000-01-01T00:00:00+0000'
rm -rf server_repo local_repo
mkdir server_repo
cd server_repo
# Create repo.
git init --quiet
git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1
# First commit.
# Directories present in all branches.
mkdir d1 d2
printf 'd1/a' > ./d1/a
printf 'd1/b' > ./d1/b
printf 'd2/a' > ./d2/a
printf 'd2/b' > ./d2/b
# Present only in root.
mkdir 'root'
printf 'root' > ./root/root
git add .
git commit -m 'root' --quiet
# Second commit only on master.
git rm --quiet -r ./root
mkdir 'master'
printf 'master' > ./master/master
git add .
git commit -m 'master commit' --quiet
# Second commit only on mybranch.
git checkout -b mybranch --quiet master~
git rm --quiet -r ./root
mkdir 'mybranch'
printf 'mybranch' > ./mybranch/mybranch
git add .
git commit -m 'mybranch commit' --quiet
echo "# List and identify all objects"
list-objects
echo
# Restore master.
git checkout --quiet master
cd ..
# Clone. Don't checkout for now, only .git/ dir.
git clone --depth 1 --quiet --no-checkout --filter=blob:none "file://$(pwd)/server_repo" local_repo
cd local_repo
# List missing objects from master.
echo "# Missing objects after --no-checkout"
git rev-list --all --quiet --objects --missing=print
echo
echo "# Git checkout fails without internet"
mv ../server_repo ../server_repo.off
! git checkout master
echo
echo "# Git checkout fetches the missing directory from internet"
mv ../server_repo.off ../server_repo
git checkout master -- d1/
echo
echo "# Missing objects after checking out d1"
git rev-list --all --quiet --objects --missing=print
GitHub upstream
.
Git v2.19.0中的输出:
# List and identify all objects
c6fcdfaf2b1462f809aecdad83a186eeec00f9c1
fc5e97944480982cfc180a6d6634699921ee63ec
7251a83be9a03161acde7b71a8fda9be19f47128
62d67bce3c672fe2b9065f372726a11e57bade7e
b64bf435a3e54c5208a1b70b7bcb0fc627463a75 d1
308150e8fddde043f3dbbb8573abb6af1df96e63 d1/a
f70a17f51b7b30fec48a32e4f19ac15e261fd1a4 d1/b
84de03c312dc741d0f2a66df7b2f168d823e122a d2
0975df9b39e23c15f63db194df7f45c76528bccb d2/a
41484c13520fcbb6e7243a26fdb1fc9405c08520 d2/b
7d5230379e4652f1b1da7ed1e78e0b8253e03ba3 master
8b25206ff90e9432f6f1a8600f87a7bd695a24af master/master
ef29f15c9a7c5417944cc09711b6a9ee51b01d89
19f7a4ca4a038aff89d803f017f76d2b66063043 mybranch
1b671b190e293aa091239b8b5e8c149411d00523 mybranch/mybranch
c3760bb1a0ece87cdbaf9a563c77a45e30a4e30e
a0234da53ec608b54813b4271fbf00ba5318b99f root
93ca1422a8da0a9effc465eccbcb17e23015542d root/root
master commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
mybranch commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
040000 tree b64bf435a3e54c5208a1b70b7bcb0fc627463a75 d1
040000 tree 84de03c312dc741d0f2a66df7b2f168d823e122a d2
040000 tree 7d5230379e4652f1b1da7ed1e78e0b8253e03ba3 master
040000 tree 19f7a4ca4a038aff89d803f017f76d2b66063043 mybranch
040000 tree a0234da53ec608b54813b4271fbf00ba5318b99f root
# Missing objects after --no-checkout
?f70a17f51b7b30fec48a32e4f19ac15e261fd1a4
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb
?308150e8fddde043f3dbbb8573abb6af1df96e63
# Git checkout fails without internet
fatal: '/home/ciro/bak/git/test-git-web-interface/other-test-repos/partial-clone.tmp/server_repo' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
# Git checkout fetches the missing directory from internet
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.
# Missing objects after checking out d1
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb
结论:所有斑点均来自外部
d1/
他们失踪了。例如。
0975df9b39e23c15f63db194df7f45c76528bccb
,即
d2/b
d1/a
.
注意
root/root
和
mybranch/mybranch
也不见了,但是
--深度1
我有一个梦想
这个特性可能会彻底改变Git。
in a single repo
没有
ugly third-party tools like
repo
想象
storing huge blobs directly in the repo without any ugly third party extensions
.
想象一下,如果GitHub允许
per file / directory metadata
就像明星和权限一样,你可以在一次回购下储存你所有的个人物品。
想象一下如果
submodules were treated exactly like regular directories
:只需请求一棵树,然后
DNS-like mechanism resolves your request
,首先看看你的
local
~/.git
,然后首先是较近的服务器(企业的镜像/缓存),最后是GitHub。