3

It is possible to update to a specific tag or branch with the hg update <branch-or-tag> command. However, the built-in completion only shows me tags (not branches). So when i type hg update and hit tab, i get a list of tags, but no branches. Is it possible to add branches to this list?

Or may this even be a bug in Mercurial?

I am using zsh and oh-my-zsh with Mercurial installed via Homebrew.

3

The current code of _hg (Zsh 5.0.2) only completes with tags:

a brief look at the source of _hg (the completion function for mercurial) shows that the completion for hq update (implemented by the function _hg_cmd_update really only returns revisions and tags:

_hg_cmd_update() {
  _arguments -s -w : $_hg_global_opts \
  '(--clean -C)'{-C,--clean}'[overwrite locally modified files]' \
  '(--rev -r)'{-r+,--rev}'[revision]:revision:_hg_tags' \
  ':revision:_hg_tags'
}

Location: I think the easiest thing would be to call locate _hq, but normally I would expect the function to be at /usr/share/zsh/5.0.2/functions. The exact sub-folder may change due to installation options. Safest thing is to download the 5.0.2 tar ball, and look at zsh-5.0.2/Completion/Unix/Command/_hg

  • 1
    Where do i find the zsh completion function so i can patch it? I could only find bash completion... – Patrick Oscity Jan 24 '13 at 7:33
1

I had the same problem with Ubuntu 16.04 default zsh 5.1.1.

To get around it I just manually patched /usr/share/zsh/functions/Completion/Unix/_hg

Replacing the _hg_tags() function with

_hg_tags() {
  local expl
  typeset -a hglabels
  hglabels=( ${(f)"$(_hg_cmd debugnamecomplete -q 2>/dev/null)"} )
  _wanted labels expl 'labels' compadd -a - hglabels
}

edit: Worth mentioning I'm using Mercurial (version 3.7.3) as debugnamecomplete isn't on all versions of mercurial. You can easily test if the version you are using supports it by type hg debugnamecomplete. If not supported just update mercurial as well.

  • This solved it from me. Will this change revert back after zsh updates? – Nebojsac Mar 23 '17 at 9:52
  • 1
    I suspect it would get overwritten. An alternative to avoid zsh updates clobbering it would be to install oh-my-zsh and instead of modifying /usr/share/zsh/functions/Completion/Unix/_hg just copy it to ~/.oh-my-zsh/custom/plugins/hg/_hg then modify it. After that modify ~/.zshrc to load the custom zsh plugin plugins=(... hg). I've actually had to do this for OSX because /usr/share has additional security to prevent modifications to System files. – mattyboy Mar 30 '17 at 10:39
1

I solved the issue hg up branches autocomplete by rewriting the _hg_tags() function with the following:

_hg_tags() {
  typeset -a tags
  local tag rev

  _hg_cmd tags 2> /dev/null | while read tag
  do
    tags+=(${tag/ #    [0-9]#:*})
  done
  _hg_cmd branches 2> /dev/null | while read tag
  do
    tags+=(${tag/ #    [0-9]#:*})
  done
  (( $#tags )) && _describe -t tags 'tags' tags
}
0

The version of the tab completion script for mercurial that ships with zsh is a bit out of date (doesn't support bookmarks or branches). There is a more up to date version in the mercurial source. homebrew doesn't seem to install this script so I found the best thing is to download the mercurial source and add the zsh_completion script to the zsh fpath.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.