Fast checking "Is this dir a hg repository?"

Anton Shestakov engored at gmail.com
Sat Nov 29 05:44:01 UTC 2014


2014-11-26 5:04 GMT+08:00 Isaac Jurado <diptongo at gmail.com>:
> Interesting.  I have a very similar Bash function to use as a quick and
> dirty "hg root" replacement, for shell integration purposes.  Although
> mine is a bit faster because it only uses Bash builtins ;-)
>
>     my_hgroot()
>     {
>         local root="$(pwd -P)"
>         while [[ $root && ! -d $root/.hg ]]
>         do
>             root="${root%/*}"
>         done
>         echo "$root"
>     }
>
>     your_hgroot()
>     {
>         local root="$PWD"
>         while true ; do
>             [[ -d "$root/.hg" ]] && echo "$root" && break
>             [[ "$root" == '/' ]] && break
>             root="$(dirname "$root")"
>         done
>     }
>
> time my_hgroot
>
>     real    0m0.002s
>     user    0m0.000s
>     sys     0m0.000s
>
> time your_hgroot
>
>     real    0m0.011s
>     user    0m0.000s
>     sys     0m0.000s
>
> time hg root
>
>     real    0m0.115s
>     user    0m0.104s
>     sys     0m0.008s

I went a bit further and hacked a crude benchmark for hgroot
implementations [1]. Apparently for me there is an even faster (a tiny
bit) way to solve the problem (named "upsearch", found on SO), I'm
using that in by prompt now.

Now, I know we talk milliseconds of difference or less, but I did this
just for fun.

Thanks for the nudge.

[1] https://bitbucket.org/engored/experiments.md/src/tip/find-closest/?at=default



More information about the Mercurial mailing list