Git pre-receive hook

后端 未结 3 1003
滥情空心
滥情空心 2020-11-28 13:25

When you enable pre-receive hook for git repository:

It takes no arguments, but for each ref to be updated it receives on standard input a line of the fo

3条回答
  •  一向
    一向 (楼主)
    2020-11-28 13:32

    Oddly, I had some code laying around from a git -> Wordpress utility that might help. The following will give you a list of all files changed in the receive, as well as their contents. No guarantees, may have bugs, may not be the most efficient way to do it, blah blah blah. Some of this code is based off stuff in gitshelve, which is a really great thing to look at for generic git manipulation.

    import sys
    import os
    import subprocess
    
    def git(args, **kwargs):
        environ = os.environ.copy()
        if 'repo' in kwargs:
            environ['GIT_DIR'] = kwargs['repo']
        if 'work' in kwargs:
            environ['GIT_WORK_TREE'] = kwargs['work']
        proc = subprocess.Popen(args, stdout=subprocess.PIPE, env=environ)
        return proc.communicate()
    
    def get_changed_files(base, commit, **kw):
        (results, code) = git(('git', 'diff', '--numstat', "%s..%s" % (base, commit)), **kw)
        lines = results.split('\n')[:-1]
        return map(lambda x: x.split('\t')[2], lines)
    
    def get_new_file(filename, commit):
        (results, code) = git(('git', 'show', '%s:%s' % (commit, filename)))
        return results
    
    repo = os.getcwd()
    basedir = os.path.join(repo, "..")
    
    line = sys.stdin.read()
    (base, commit, ref) = line.strip().split()
    modified = get_changed_files(base, commit)
    
    for fname in modified:
        print "=====", fname
        print get_new_file(fname, commit)
    

提交回复
热议问题