与 Mercurial 比较,检查 mercurial/treediscovery.py (Mercurial repository identification) 在哪里:
base = list(base)
if base == [nullid]:
if force:
repo.ui.warn(_("warning: repository is unrelated\n"))
else:
raise util.Abort(_("repository is unrelated"))
base 变量存储两个存储库的最后一个公共部分。
Git 在 fetch/push 上发出 warning: no common commits 时具有相同的假设。我只是没有 grep Git 源代码,这需要时间。
通过给出 Mercurial 推/拉检查的这个想法,我们可以假设如果存储库有共同的根,它们是相关的。对于 mercurial,这意味着来自命令的哈希:
$ hg log -r "roots(all())"
对于两个存储库都必须有非空感叹词。
您可能不会通过精心构建存储库来欺骗根检查,因为构建两个存储库看起来像这样(具有共同的部分但不同的根):
0 <--- SHA-256-XXX <--- SHA-256-YYY <--- SHA-256-ZZZ
0 <--- SHA-256-YYY <--- SHA-256-ZZZ
不可能,因为这意味着您反转 SHA-256,因为每个后续哈希都取决于先前的值。 Mercurial 和 Git 都是如此。
在 Git 中查看根目录的相应命令是:
$ git log --format=oneline --all --max-parents=0
你可以玩弄自己:
bash# md git
/home/user/tmp/git
bash# md one
/home/user/tmp/git/one
bash# git init
Initialized empty Git repository in /home/user/tmp/git/one/.git/
bash# echo x1 > x1
bash# git add x1
bash# git ci -m x1
[master (root-commit) 1208fb0] x1
bash# echo x2 > x2
bash# git add x2
bash# git ci -m x2
[master 1c3fe86] x2
bash# cd ..
bash# md two
/home/user/tmp/git/two
bash# git init
Initialized empty Git repository in /home/user/tmp/git/two/.git/
bash# echo y1 > y1
bash# git add y1
bash# git ci -m y1
[master (root-commit) ff56a8e] y1
bash# echo y2 > y2
bash# git add y2
bash# git ci -m y2
[master 18adff5] y2
bash# git fetch ../one/
warning: no common commits
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From ../one
* branch HEAD -> FETCH_HEAD
bash# git co --orphan one
Switched to a new branch 'one'
bash# git merge FETCH_HEAD
bash# git log --format=oneline --all
18adff541c7ce9f1a1f2be2804d6d0e5792ff086 y2
ff56a8e7e9145d2b1b5a760bbc9b12451927ab0c y1
1c3fe8665851e89d37f49633cd2478900217b91c x2
1208fb0f721005207c6afe6a549a9ed0dcc5b0a8 x1
bash# git log --format=oneline --all --max-parents=0
ff56a8e7e9145d2b1b5a760bbc9b12451927ab0c y1
1208fb0f721005207c6afe6a549a9ed0dcc5b0a8 x1
bash# git log --all --graph
* commit 18adff541c7ce9f1a1f2be2804d6d0e5792ff086
| y2
|
* commit ff56a8e7e9145d2b1b5a760bbc9b12451927ab0c
y1
* commit 1c3fe8665851e89d37f49633cd2478900217b91c
| x2
|
* commit 1208fb0f721005207c6afe6a549a9ed0dcc5b0a8
x1
注意 Git 允许部分结帐。我没有为--max-parents=0检查这个案例。