How to shadow git's built-in commands?
Question: I'd like to have the following script run when "git status" is ran with no
parameters:

    
    
    #!/usr/bin/env python
    
    import subprocess
    
    o = subprocess.check_output("git -c color.status=always status", shell=True)
    started = False
    done = False
    for line in o.split('\n'):
        line = line.rstrip('\r\n')
        if line.strip() == '' and not done:
            if started:
                started = False
                done = True
            started = True
            print(line)
            continue
        if started and 'modified: ' in line:
            filename = line.split('modified:')[1].strip().split('\x1b')[0]
            p = subprocess.Popen(["git", "diff", filename], stdout=subprocess.PIPE)
            out = subprocess.check_output("diffstat", stdin=p.stdout).split('|')[1]
            print('%s %s' % (line.rstrip(), out.split('\n')[0]))
        else:
            print(line)
    

How can I achieve that?


Answer: Create an alias by placing this line in your `~/.gitconfig`:

    
    
    [alias]
        status = !sh -c 'foo $@' --
    

where `foo` is your script, stored somewhere on the PATH. Script `foo` shall
recognize if any parameters are passed and use a vanilla `git status` in that
case.

