Git预接收钩子 [英] Git pre-receive hook

查看:90
本文介绍了Git预接收钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当为git存储库启用预接收钩子时:


它不需要任何参数,但是每次更新它都会收到在标准输入中有一行格式:

<旧值> SP<新值> SP< ref-name> LF



其中< old-value>是存储在ref中的旧对象名称,< new-value>是要存储在ref中的新对象名称,并且是ref的全名。当创建新的参考时,<


任何人都可以解释我如何检查存储库中所有将被更改的文件如果我允许这个提交?



我想通过一些脚本运行这些文件来检查语法等。



谢谢。

解决方案

奇怪的是,从一个git - > Wordpress工具可能会有所帮助。以下内容将为您提供接收中更改的所有文件以及其内容的列表。没有保证,可能有错误,可能不是最有效的方法,等等等等等等。其中一些代码基于 gitshelve 中的内容,这是一件非常棒的事情寻找通用的git maniuplation。

 导入sys 
导入os
导入子流程

def git(args ,** kwargs):
environ = os.environ.copy()
如果kwargs中有'repo':
environ ['GIT_DIR'] = kwargs ['repo']
如果在kwargs中工作:
environ ['GIT_WORK_TREE'] = kwargs ['work']
proc = subprocess.Popen(args,stdout = subprocess.PIPE,env = environ)
返回proc.communicate()
$ b $ 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('\ t')[2],lines)
$ b $ def get_new_file(filename,commit):
(results,code)= git(( 'git','show','%s:%s'%(commit,filename)))
返回结果

repo = os.getcwd()
basedir = os.path.join(repo,..)

line = sys.stdin.read()
(base ,
print========================================================================================================================================================================================================= =,fname
print get_new_file(fname,commit)


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.

解决方案

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 maniuplation.

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)

这篇关于Git预接收钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆