Fast checking "Is this dir a hg repository?"

Isaac Jurado diptongo at gmail.com
Tue Nov 25 21:04:33 UTC 2014


On Tue, Nov 25, 2014 at 11:16 AM, Anton Shestakov <engored at gmail.com> wrote:
> 2014-11-25 16:32 GMT+08:00 Roman Inflianskas <infroma at gmail.com>:
>> Hi, all.
>>
>> I'm interested in writing custom prompt for fish shell that displays current
>> mercurial repository status (branch, is it dirty, etc.). What is the fastest
>> way to check "Is this dir a hg repository?"?
>
> The fastest way to check if a directory tracked in mercurial is
> probably to write a small C program for directory traversal. Second
> best option is a shell script. This is what I'm currently using for my
> bash prompt [1].

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

Best regards.

-- 
Isaac Jurado

"The noblest pleasure is the joy of understanding"
Leonardo da Vinci



More information about the Mercurial mailing list