如何加快寻找上市git存储库? [英] How to speed up find for listing git repositories?

查看:117
本文介绍了如何加快寻找上市git存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查找位于某个目录中的所有 git 存储库,而不是其子目录,例如〜/ repo 。两种简单的方法是:

pre $ find〜/ repo -depth 2 -type d -name'.git'|同时阅读回购...



<$ p $在〜/ repo / * /。git ...

中回购

使用 find 的版本比具有globbing模式的版本慢。我对此非常惊讶,因为没有真正的理由说明为什么一种方法比另一种方法需要更多的系统调用来收集信息。我尝试了 find 调用的更智能的版本

  find〜/ repo -depth 3 -prune  - o -depth 2 -type d -name'.git'-print |而阅读回购... 

没有任何明显的改善。不幸的是,我无法跟踪系统调用来了解 find 在这里是如何工作的。



什么解释了 这两种方法? (shell是 / bin / sh ,我认为它是一些过时的 bash 版本。)

)来测试 -depth 更新 -depth 2
)未在 GNU的文档 find 。这可能是一个OSX扩展。不要使用它!



使用 -mindepth 2 -maxdepth 2 来代替,正如@ hek2mgl在<他们的答案。





h3> OSX特定的

当<$ $时,似乎OSX版本的 find 不必要地下降到比2级更深的目录使用c $ c> -depth 2 (但这是正确的行为,见下文)。

你可以告诉它不要在 -depth 2 之后立即加上 -prune 如果你把它放在别的地方,就会有任何影响):

  find〜/ repo -depth 2 -prune -type d -name。 git 

一些基准:

  $ time(find。-depth 4 -prune -type d -name .git | wc -l)
20

real 0m0.064s
用户0m0.009s
sys 0m0.046s

已移动 -prune 在t他结束了,它突然需要很多时间才能运行:

  $ time(find。 -depth 4 -type d -name .git -prune | wc -l)
20

real 0m12.726s
user 0m0.325s
sys 0m9.298s



备注



第二个想法是(仔细阅读 man找到 -depth 2 不需要 find 在超过两个的目录中停止递减水平。它可以是一个更复杂的条件的一部分,它需要 -depth 2 或别的东西(fe find。-depth 2 - 或者--name .git )。



要强制停止下降超过两个级别,您必须使用 -maxdepth 2 -depth 2 -prune




  • -maxdepth 告诉它不要超过两层;
  • -depth 2 -prune 告诉它会停止下降到子目录中,如果被检查的目录是两层深的。



它们有相同的行为,选择一个或另一个是优先事项。我会选择 -maxdepth 2 ,因为它更清晰。 结论 b $ b

因为 -depth 2 不可移植,所以最终的命令应该是这样的:

  find〜/ repo -mindepth 2 -maxdepth 2 -type d -name'.git'-print 

感谢@ hek2mgl提及兼容性问题。


I want to find all git repositories lying in some directory, but not its subdirectories, say ~/repo. Two simple approaches are

find ~/repo -depth 2 -type d -name '.git' | while read repo …

or

for repo in ~/repo/*/.git …

The version using find is magnitudes slower than the one with the globbing pattern. I am very surprised by this, because there is no real reason why one method would need more system calls than the other to gather its informations. I tried a smarter version of the find invocation

find ~/repo -depth 3 -prune -o -depth 2 -type d -name '.git' -print | while read repo …

without any noticeable improvement. Unfortunately I was not able to trace system calls to figure out how find is working here.

What explains the huge speed difference between these two methods? (The shell is /bin/sh which I believe to be some obsolete version of bash.)

解决方案

Update: the test -depth with arguments (-depth 2) is not specified in the documentation of GNU find. It is probably an OSX extension. Don't use it!

Use -mindepth 2 -maxdepth 2 instead, as suggested by @hek2mgl in their answer.


OSX specific

It seems the OSX version of find unnecessarily descends into directories deeper than 2 levels when -depth 2 is used (but this is the correct behaviour, see below).

You can tell it to not do that by adding -prune immediately after -depth 2 (it seems it doesn't have any effect if you put it somewhere else):

find ~/repo -depth 2 -prune -type d -name .git

Some benchmarks:

$ time (find . -depth 4 -prune -type d -name .git | wc -l)
      20

real 0m0.064s
user 0m0.009s
sys  0m0.046s

Moved -prune at the end and it suddenly needs a lot of time to run:

$ time (find . -depth 4 -type d -name .git -prune | wc -l)
      20

real 0m12.726s
user 0m0.325s
sys  0m9.298s

Remarks

On a second thought (and after a closer reading of man find) -depth 2 does not require find to stop descending in directories deeper than two levels. It can be part of a more complex condition that requires -depth 2 or something else (f.e. find . -depth 2 -or -name .git).

To force it to stop descending more than two levels you must use either -maxdepth 2 or -depth 2 -prune.

  • -maxdepth tells it to not go deeper than two levels;
  • -depth 2 -prune tells it to stop descending into subdirectories if the directory under examination is two levels deep.

They have equivalent behaviour, choosing one or another is a matter of preference. I would choose -maxdepth 2 because it is more clear.

Conclusion

Because -depth 2 is not portable, the final command should be like:

find ~/repo -mindepth 2 -maxdepth 2 -type d -name '.git' -print

Thanks @hek2mgl for mentioning about the compatibility issue.

这篇关于如何加快寻找上市git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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