#! /usr/bin/env python

import sys
import os
import subprocess

usage = """
Executes the specified hg command on all repositories in the current
working directory tree.

hgall [hg command] [hg command options]

For example, the following command pulls changes and updates the local
working copy of all repositories in the current working directory tree:

  hgall pull -u
"""

if len(sys.argv) == 1:
    print usage
else:
    repos = []
    for dirpath, dirnames, filenames in os.walk('.'):
        if '.hg' in dirnames:
            repos.append(dirpath)
    
    for path in repos:
        command = ['hg', '-R', path] + sys.argv[1:]
        print "$ " + " ".join(command)
        retval = subprocess.call(command)
