Git预收钩

rza*_*jac 8 git

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

它不需要参数,但是对于每个ref都要更新它在标准输入上接收格式的一行:

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

其中<old-value>是存储在ref中的旧对象名,<new-value>是要存储在ref中的新对象名,并且是ref的全名.创建新ref时,<old-value>为40 0.

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

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

谢谢.

bro*_*ool 19

奇怪的是,我有一些代码来自一个可能有帮助的git - > Wordpress实用程序.以下内容将为您提供接收中更改的所有文件的列表及其内容.没有保证,可能有错误,可能不是最有效的方法,等等等等等等.这些代码中的一些基于gitshelve中的东西,这对于通用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)
Run Code Online (Sandbox Code Playgroud)


dri*_*zzo 10

我刚刚这样做了 这是我使用的基本流程.

在你的pre-receive钩子中,从stdin中读取每一行,(如你所提到的)如下所示:

oldref newref refname
Run Code Online (Sandbox Code Playgroud)
  1. 对于每个(oldref,newref)对,您需要列出所有提交:

    git show --format=format:%H --quiet oldref..newref
    
    Run Code Online (Sandbox Code Playgroud)
  2. 对于每个提交,您需要列出所有文件:

    git diff --name-only commit^..commit
    
    Run Code Online (Sandbox Code Playgroud)
  3. 检查文件,使用git show:

    git show commit:filepath
    
    Run Code Online (Sandbox Code Playgroud)

    在这里做任何文件内容检查.如果要通知用户问题,请写入stderr

  4. 在迭代所有引用之后,提交和文件退出非零以拒绝推送,或者为零以允许它

请注意,此方法按顺序遍历所有提交,因此如果在多个提交中修改了文件,则将检查每个版本.如果您只想在推送中查看每个文件,请改为以相反的顺序遍历提交,而不是两次检查给定的文件.我推荐第一种方法,以便检查所有推送文件的版本.