简体   繁体   English

仅在非空时克隆

[英]Clone only if non-empty

Normally, git clone is happy to clone empty repositories, it just prints the warning 通常, git clone很乐意克隆空的存储库,它只是打印警告

warning: You appear to have cloned an empty repository. 警告:您似乎已克隆了一个空的存储库。

Is there any way to tell git to clone only non-empty repositories? 有什么办法告诉git仅克隆非空存储库吗? Ideally, I would prefer git clone to simply fail (without creating a working copy) if the repository is empty. 理想情况下,如果存储库为空,我希望git clone仅失败(不创建工作副本)。

My current solution is in three steps: (1) clone, (2) see if the clone is empty, (3) delete if it is empty. 我当前的解决方案分为三个步骤:(1)克隆,(2)查看克隆是否为空,(3)删除是否为空。 Does the job but hardly elegant (and it prints warnings that I will need to filter out). 做这项工作,但几乎没有优雅(它会打印警告,我需要过滤掉它)。 Is there a more straightforward solution? 有没有更直接的解决方案?

There is not really a way to do that, but you could approximate it (somewhat poorly): set up a remote pointing to the repo you might clone, and then run git ls-remote on that remote. 确实没有做到这一点的方法,但是您可以近似(有点差):设置一个指向您可能克隆的仓库的远程指向,然后在该远程上运行git ls-remote remote。 (You can do this with any git repository, including a temporary empty one.) If ls-remote finds no refs, cloning that repository would have created an empty repository. (您可以对任何git存储库执行此操作,包括临时的空存储库。)如果ls-remote没有找到引用,则克隆该存储库将创建一个空的存储库。

There are two major defects with this method, and one minor: 这种方法有两个主要缺陷,一个是次要缺陷:

  1. You need a place to add the test remote and do the ls-remote. 您需要一个地方来添加测试遥控器并执行ls-remote。
  2. This is as just much work as actually cloning the empty repository. 这与实际克隆空存储库一样多。 You save time when compared to cloning a large non -empty repository, but you will do that (clone the non-empty repo) anyway. 与克隆大型空存储库相比,可以节省时间,但是无论如何,您都可以这样做(克隆非空存储库)。
  3. (Minor) The answer you get is, at least potentially, out of date by the time you get it. (次要的)您得到的答案至少有可能在您获得答案时已经过时。 This is also true of cloning a repo and discovering that it's empty (maybe fetching will now find a non-empty repository), or even of cloning or fetching some other repository: by the time you get and inspect the result, which might be several whole seconds after you start the process, there could be dozens or even thousands of new commits added to the remote repo (this depends on how active the repository is). 克隆存储库并发现其为空(也许现在获取将找到一个非空的存储库),甚至克隆或获取其他存储库也是如此:在获取并检查结果时,可能是几个在开始该过程的整个中,可能有数十个甚至数千个新的提交添加到远程存储库中(这取决于存储库的活跃程度)。

I think you might as well just clone-and-maybe-remove, really. 我认为您真的可以克隆并删除。 (Or just clone and don't remove: there's nothing wrong with an empty repo. I see from a comment that you don't want them; I just don't see the reason why you don't want them. "For automation" is a purpose, but not a reason.) (或者只是克隆,并且移除:没有什么错用空的回购协议,我从你不想让他们评论看,我只是不明白为什么你不希望他们的理由“自动化。 “是目的,而不是原因。)

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

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