Fastest way to check if the current directory is under mercurial?

Viewed 1222

I'm trying to speed up my zsh theme prompt and hg checks are a bit slower than git's. That result in a slower prompt to show when using the hg plugin.

For git we use git rev-parse --is-inside-work-tree which is the standard and pretty fast.

For hg I've tried to find a similar command but nothing is as fast:

  • hg --cwd $PWD root
  • hg summary
  • hg root
  • hg -q stat

All these commands work but are not as efficient as git rev-parse --is-inside-work-tree.

Also tried this little script which is the best one currently. Anything I can do to speed this script even more?

is_dir_hg() {
  local root="$(pwd -P)"
  while [[ $root && ! -d $root/.hg ]]
  do
    root="${root%/*}"
  done

  echo "$root"
}

Note that the solution must detect hg directory even when we're not in the root directory.

2 Answers

I’d use hg id, which sets the return code usefully as well. At least it’s fast to type :-)

Related