Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

Git pre-receive hook

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 format:

< old-value > SP < new-value > SP < ref-name > LF

where < old-value > is the old object name stored in the ref, < new-value > is the new object name to be stored in the ref and is the full name of the ref. When creating a new ref, < old-value > is 40 0.

Does anyone can explain me how do I examine all the files that will be changed in the repository if i allow this commit?

I'd like to run that files through some scripts to check syntax and so on.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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('
')[:-1]
    return map(lambda x: x.split('')[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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...