aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTommi Virtanen <tv@eagain.net>2007-09-01 18:23:26 -0700
committerTommi Virtanen <tv@eagain.net>2007-09-01 18:54:49 -0700
commitacf005ea3533d7fadac31ac8d3d68b455fd2d963 (patch)
treef12e887693a80046f2d97fc7b47edd75ee41d400
parentRefactor gitosis-gitweb to move temp file handling out of main. (diff)
downloadgitosis-dakkar-acf005ea3533d7fadac31ac8d3d68b455fd2d963.tar.gz
gitosis-dakkar-acf005ea3533d7fadac31ac8d3d68b455fd2d963.tar.bz2
gitosis-dakkar-acf005ea3533d7fadac31ac8d3d68b455fd2d963.zip
Add gitosis-run-hook, to be run from git hooks.
Sadly, no unit tests on this level, for now.
-rw-r--r--gitosis/run_hook.py93
-rwxr-xr-xsetup.py1
2 files changed, 94 insertions, 0 deletions
diff --git a/gitosis/run_hook.py b/gitosis/run_hook.py
new file mode 100644
index 0000000..2bc4aaa
--- /dev/null
+++ b/gitosis/run_hook.py
@@ -0,0 +1,93 @@
+"""
+Perform gitosis actions for a git hook.
+"""
+
+import errno
+import logging
+import optparse
+import os
+import sys
+import shutil
+
+from ConfigParser import RawConfigParser
+
+from gitosis import repository
+from gitosis import ssh
+from gitosis import gitweb
+
+log = logging.getLogger('gitosis.run_hook')
+
+def die(msg):
+ log.error(msg)
+ sys.exit(1)
+
+def getParser():
+ parser = optparse.OptionParser(
+ usage='%prog HOOK',
+ description='Perform gitosis actions for a git hook',
+ )
+ parser.set_defaults(
+ config=os.path.expanduser('~/.gitosis.conf'),
+ )
+ parser.add_option('--config',
+ metavar='FILE',
+ help='read config from FILE',
+ )
+ return parser
+
+def post_update(cfg):
+ try:
+ shutil.rmtree('gitosis-export')
+ except OSError, e:
+ if e.errno == errno.ENOENT:
+ pass
+ else:
+ raise
+ repository.export(git_dir='.', path='gitosis-export')
+ os.rename('gitosis-export/gitosis.conf', 'gitosis.conf')
+ gitweb.generate(config=cfg, path='projects.list')
+ ssh.writeAuthorizedKeys(
+ path=os.path.expanduser('~/.ssh/authorized_keys'),
+ keydir='gitosis-export/keydir',
+ )
+
+def main():
+ logging.basicConfig(level=logging.INFO)
+ os.umask(0022)
+
+ parser = getParser()
+ (options, args) = parser.parse_args()
+ try:
+ (hook,) = args
+ except ValueError:
+ parser.error('Missing argument HOOK.')
+
+ cfg = RawConfigParser()
+ try:
+ conffile = file(options.config)
+ except (IOError, OSError), e:
+ if e.errno == errno.ENOENT:
+ # not existing is ok
+ pass
+ else:
+ # I trust the exception has the path.
+ die("Unable to read config file: %s." % e)
+ else:
+ try:
+ cfg.readfp(conffile)
+ finally:
+ conffile.close()
+
+ git_dir = os.environ.get('GIT_DIR')
+ if git_dir is None:
+ die('Must have GIT_DIR set in enviroment.')
+ os.chdir(git_dir)
+ os.environ['GIT_DIR'] = '.'
+
+ if hook == 'post-update':
+ log.info('Running hook %s', hook)
+ post_update(cfg)
+ log.info('Done.')
+ else:
+ log.warning('Ignoring unknown hook: %r', hook)
+ return 0
diff --git a/setup.py b/setup.py
index 8a82e47..697d952 100755
--- a/setup.py
+++ b/setup.py
@@ -17,6 +17,7 @@ setup(
'gitosis-ssh = gitosis.ssh:main',
'gitosis-serve = gitosis.serve:main',
'gitosis-gitweb = gitosis.gitweb:main',
+ 'gitosis-run-hook = gitosis.run_hook:main',
],
},
)