简体   繁体   English

如何唯一标识git存储库

[英]How can I uniquely identify a git repository

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是否与存储库A相同 - 我猜我的意思是他们有可合并的历史记录。 B might be named differently than A, and might have additional branches -- the usual use cases. B的名称可能与A不同,可能还有其他分支 - 通常的用例。

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. 我可以使用ssh和bash来解决这个问题,但是仅使用git的解决方案会更好。

Feedback on the half-baked idea is also welcome. 对这个半生不熟的想法的反馈也很受欢迎。

Why this is not a duplicate of Git repository unique id 为什么这不是Git存储库唯一ID的重复

The referenced question is looking for a unique repository id, or a way of creating one. 引用的问题是寻找唯一的存储库ID,或者创建一个存储库ID。 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 (ie 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. git存储库没有唯一标识。 However; 然而; you could just compare the SHA-1 of the first commit on the master branch ; 你可以比较主分支上第一次提交SHA-1 ; that should suffice in 99.999% of all cases (supposing that the first commit will never be changed). 这应该足以满足99.999%的所有情况(假设第一次提交永远不会改变)。

And if you want to be even more sure, you could consider using also the SHA-1 of the second commit; 如果你想更加确定,你可以考虑使用第二次提交的SHA-1; again supposing it will never change :). 再次假设它永远不会改变:)。 with the SHA-1 of the first two commits; 使用前两个提交的SHA-1; I guess you have about 1 / 2^320 = 4.7*10^-97 chance of being wrong ... 我猜你有大约1/2 ^ 320 = 4.7 * 10 ^ -97的错误机会......

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. 你可以假设你只有一个无父根提交 ,并采用它的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): 或者(更容易理解,感谢@TomHale):

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

Inside .git/config file you have the url of where the repository was cloned from. .git/config文件中,您有克隆存储库的URL。

You can compare those origins of 2 repositories. 您可以比较2个存储库的起源。

Example: 例:

[remote "origin"]
    url = git://myohost/myproject.git

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM