aboutsummaryrefslogtreecommitdiff
blob: 2bc4aaa1bc324a1730796c0cb1ca618fb041c16d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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