#!/usr/bin/env python

import os
import sys

def main():

	# usage string
	usage = 'usage: gitall command'

	# command check
	if len(sys.argv) < 2:
		sys.exit(usage)

	command = 'git ' + ' '.join(sys.argv[1:])
	printDelimiter()
	print 'Running command:', command

	# get a list of git directories in the specified parent
	gitDirectories = getSubdirectories('.', isGitDirectory)

	for gitDirectory in gitDirectories:
		os.chdir(gitDirectory)
		printDelimiter()
		print 'Current respository location:', os.getcwd()
		os.system(command)

	printDelimiter()

def getSubdirectories(directory, filter = None):
	directory = os.path.abspath(directory)
	subdirectories = os.walk(directory).next()[1]
	if filter is None:
		return [directory + '/' + i for i in subdirectories]
	else:
		return [directory + '/' + i for i in subdirectories if filter(directory + '/' + i)]

def isGitDirectory(directory):
	return os.path.isdir(directory + '/.git/')

def printDelimiter():
	print '\033[91m'
	print ('#' * 80)
	print '\033[0m'

if __name__ == '__main__':
	main()
