Mercurial Version Control Status in the ZSH Command Line Prompt

I sometimes forget to push or pull changes to or from a remote repository. To remedy the problem I wrote myself a little script to show me the status on the prompt.

The script is written for zsh and Mercurial, but can be easily adapted to other version control systems (I guess).

Everytime you change a directory the scripts looks up if there are uncommited changes, remote changes to be pulled or local patchsets to be pushed.

The prompt of a directory within a project with uncommited changes would look something like this:

  user@C2D:~/root-dir/subdir %            root-dir: U

Possible states:

  • U: Uncommited changes (use hg commit)
  • I: Incoming patchsets (use hg pull)
  • O: Outgoing patchsets (use hg push)

I have configured Mercurial to automatically update my repository after a pull, but some people might wish to add another status for “pending updates” or something like that.

The script uses “hg incoming -q” or “hg outgoing -q” to check for sets to be pulled or pushed, so you should configure the default paths in your .hg/hgrc file for pulling or pushing like this:

  default-push = ssh://yoursever.tld/Projects/PlanToTakeOverTheWorld/
  default = ssh://yourserver.tld/Projects/PlanToTakeOverTheWorld/

Unfortunately there are a few problems:

Accessing remote repositories over some network is costly. If you have a slow network connection you might want to temporarily switch off the remote status commands by using “export VCSNOREMOTE=true” please note that “export VCSNOREMOTE=false” has the same effect, in fact any string would switch off the remote commands. To enable them again use “export VCSNOREMOTE=”.

The second problem might be that you have to enter your credentials to access remote servers quite frequently. Use some sort of ssh-agent or something similar to avoid typing in your password all the time.

The third thing to remember is that updating the status with every prompt is useless, so I opted for updating after changing a directory (also if you type cd .) but at least after 60 seconds.

Installation: save anywhere on your disk and source it in your .zshrc file.

Download: vcs_status_prompt

Life is painting a picture, not doing a sum.
— Oliver Wendell Holmes