我如何唯一标识一个git存储库 [英] How can I uniquely identify a git repository

查看:133
本文介绍了我如何唯一标识一个git存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个工具,在克隆所述存储库之前检查是否已经有远程存储库的本地克隆。要做到这一点,我需要一种测试方式,如果B是相同的知识库A - 我猜我的意思是他们有可合并的历史。 B可能与A的命名方式不同,并且可能有其他分支 - 通常的用例。

有没有办法做到这一点?我有一个尝试性的想法如何去做,但我想也许这里有人有一个明确的答案。

初步想法



获取分支列表并搜索常见分支(哈希)。然后对于通用分支,检查初始提交是否相同(通过哈希)。那时我会说'够好'。我想我没事,除非有人一直在搞历史,我愿意忽略这种用例。要做到这一点,我需要一种从远程存储库获取分支和提交信息的方式,而不需要做一个克隆。我可以使用ssh& bash,但只有git-only的解决方案才是最好的。



也欢迎对这个不完整的想法提出反馈。



为什么这不是 Git存储库唯一ID

$ b的副本
$ b

引用的问题是寻找唯一的存储库ID,或者创建一个。没有这样的野兽存在,即使它存在,在这里它是否有意义也值得怀疑,因为我想确定两个存储库是否具有可合并的历史(即,我可以在两者之间取得和合并) - 一个稍好的定义问题。我愿意忽略用户修改历史记录的可能性,但也希望听到如何处理这种情况。

正如你可以在相关问题中看到的那样; git存储库没有唯一标识。然而;您可以比较主分支上第一次提交的 SHA-1 ;在所有情况下,这应该足够了99.999%(假设第一次提交将永远不会改变)。

如果你想更加确定,你可以考虑使用也是第二次提交的SHA-1;再次假设它永远不会改变:)。与前两个提交的SHA-1;我估计你有大概1/2 ^ 320 = 4.7 * 10 ^ -97的机会......



如果你不确定是否有主分支;您可以假设您只有一个无父级root提交,并取其SHA-1。你可以使用这个命令来获得root提交(或提交):

  git rev-list --parents HEAD | egrep^ [a-f0-9] {40} $

(从这个答案



或者(更容易理解,谢谢@TomHale):

  git rev-list --parents HEAD | tail -1 


I would like to create a tool that checks if I already have a local clone of a remote repository before cloning said repository. To do this, I need a way of testing if B is the same as repository A -- by which I guess i mean they have mergeable histories. B might be named differently than A, and might have additional branches -- the usual use cases.

Is there a way to do this? I have a tentative idea how to do it, but I thought perhaps someone here has a definitive answer.

Tentative idea

Get a list of branches and search for common branches (by hash). Then for the common branches, check that the initial commits are the same (by hash). At that point I would say 'good enough'. I figure I'm okay unless someone has been messing with history, which use-case I'm willing to neglect. To do this though, I need a way of getting the branch and commit information from the remote repository, without doing a clone. I can solve this using ssh & bash, but a git-only solution would be preferable.

Feedback on the half-baked idea is also welcome.

Why this is not a duplicate of Git repository unique id

The referenced question is looking for a unique repository id, or a way of creating one. No such beast exists, and even if it did, it is questionable if it would be relevant here, since I want to determine if two repositories have mergeable histories (i.e. I could fetch and merge between the two) -- a slightly better defined problem. I'm willing to ignore the possibilty that a user has modified history, but would love to hear how to handle that case as well.

解决方案

As you can see in the related question; there is NO unique identification for a git repository. However; you could just compare the SHA-1 of the first commit on the master branch; that should suffice in 99.999% of all cases (supposing that the first commit will never be changed).

And if you want to be even more sure, you could consider using also the SHA-1 of the second commit; again supposing it will never change :). with the SHA-1 of the first two commits; I guess you have about 1 / 2^320 = 4.7*10^-97 chance of being wrong ...

If you are not sure there is even a master branch; you could suppose you have only one parentless root commit, and take its SHA-1. You can use this command to get the root commit (or commits):

git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$"

( copied from this answer)

or (easier to understand, thanks @TomHale):

git rev-list --parents HEAD | tail -1

这篇关于我如何唯一标识一个git存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆