如何影响git的内置命令?

时间:2015-06-23 18:30:10

标签: git

我希望在没有参数的情况下运行“git status”时运行以下脚本:

#!/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)

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

通过将此行放在~/.gitconfig

中来创建别名
[alias]
    status = !sh -c 'foo $@' --

其中foo是您的脚本,存储在PATH的某个位置。脚本foo应识别是否传递了任何参数,并在这种情况下使用香草git status