Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pre-receive hook

Tags:

git

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.

like image 326
rzajac Avatar asked Apr 03 '10 01:04

rzajac


People also ask

How do you do a pre-commit hook?

Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.

Why use pre-commit hook?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.

What is pre Push hook?

A pre-push hook is a client-side git hook that runs right before a reference is pushed to a remote ( git push ).


2 Answers

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)
like image 145
brool Avatar answered Oct 07 '22 03:10

brool


I just did this. Here's the basic flow I used.

In your pre-receive hook, read each line from stdin, which (as you mentioned) look like this:

oldref newref refname
  1. For each (oldref, newref) pair you need to list all the commits:

    git show --format=format:%H --quiet oldref..newref
    
  2. for each commit you need to list all the files:

    git diff --name-only commit^..commit
    
  3. to examine a file, use git show:

    git show commit:filepath
    

    do whatever checking of file contents here. If you want to notify the user of an issue, write to stderr

  4. after iterating all the refs, commits and files exit nonzero to reject the push, or zero to allow it

Note that this approach walks all the commits in order, so if a file is modified in several commits each version will get examined. If you wanted to only view each file in a push once, traverse the commits in reverse order instead, and just don't examine a given file twice. I recommend the first approach however so that all versions of pushed files are examined.

like image 22
drizzo4shizzo Avatar answered Oct 07 '22 01:10

drizzo4shizzo