From acf005ea3533d7fadac31ac8d3d68b455fd2d963 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Sat, 1 Sep 2007 18:23:26 -0700 Subject: Add gitosis-run-hook, to be run from git hooks. Sadly, no unit tests on this level, for now. --- gitosis/run_hook.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 gitosis/run_hook.py (limited to 'gitosis/run_hook.py') 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 -- cgit v1.2.3