Skip to content

Instantly share code, notes, and snippets.

@troelskn
Forked from kgn/pre-receive.py
Created October 30, 2011 11:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save troelskn/1325799 to your computer and use it in GitHub Desktop.
Save troelskn/1325799 to your computer and use it in GitHub Desktop.
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', '--name-only', "%s..%s" % (base, commit)), **kw)
return results.strip().split('\n')
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)
@adrianlzt
Copy link

adrianlzt commented Dec 7, 2016

Modification to work also with python3. But some troubles with not ascii chars.

--- pre-receive.py	2016-12-07 17:35:51.948527071 +0100
+++ pre-receive	2016-12-07 19:14:56.608632248 +0100
@@ -1,3 +1,4 @@
+#!/usr/bin/env python
 import sys
 import os
 import subprocess
@@ -13,19 +14,23 @@
 
 def get_changed_files(base, commit, **kw):
     (results, code) = git(('git', 'diff', '--numstat', '--name-only', "%s..%s" % (base, commit)), **kw)
-    return results.strip().split('\n')
+    return results.decode("utf-8").strip().split('\n')
 
 def get_new_file(filename, commit):
     (results, code) = git(('git', 'show', '%s:%s' % (commit, filename)))
-    return results
+    return results.decode("utf-8")
 
 repo = os.getcwd()
 basedir = os.path.join(repo, "..")
 
 line = sys.stdin.read()
 (base, commit, ref) = line.strip().split()
+print("base: ", base)
+print("commit: ", commit)
+print("ref: ", ref)
 modified = get_changed_files(base, commit)
 
+# print files modified and its content
 for fname in modified:
-    print "=====", fname
-    print get_new_file(fname, commit)
+    print("=====", fname)
+    print(get_new_file(fname, commit))

@hasegeli
Copy link

I developed an extensive script to validate commits, if anyone find it helpful:

https://github.com/innogames/igcommit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment