summaryrefslogtreecommitdiff
path: root/gerritbot/syncgerrit
blob: d5faaebf542a7bd348d59f44a2c433ccf250d49a (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
#!/usr/bin/env python
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import os
import os.path
import logging
import minilog

repodir = os.path.join(os.environ['HOME'], 'syncrepos')
sourceurl = 'https://git.libreoffice.org/'
desturl = 'ssh://logerrit/'
repos = ['contrib/dev-tools']
branches = {}
branches['contrib/dev-tools'] = ['master']

loglevel='INFO'
logfile=os.path.join(os.environ['HOME'], 'sync.log')

class RepoSyncer():
    def __init__(self, repo, logger, loghandler):
        self.repo = repo
        self.logger = logger
        self.localpath = os.path.join(repodir, self.repo + '.git')

    def make_extra(self, phase):
        return { 'repo' : self.repo, 'phase' : phase }

    def ensure_exists(self, remotebaseurl):
        if not os.path.exists(self.localpath):
            os.makedirs(self.localpath)
            minilog.logged_call( \
                ['git', 'clone', '--bare', '--mirror', remotebaseurl + self.repo, self.localpath], \
                self.logger, \
                self.make_extra('clone'))

    def update_repo(self):
        minilog.logged_call( \
            ['git', '--git-dir=%s' % self.localpath, 'fetch', '--all'], \
            self.logger, \
            self.make_extra('update'))

    def push_branches(self, remotebaseurl, branches):
        contribprefix = 'contrib/'
        destinationpath = repo
        if destinationpath.startswith(contribprefix):
            destinationpath = destinationpath[len(contribprefix):]
        refspec = ['%s:%s' % (branch, branch) for branch in branches]
        minilog.logged_call( \
            ['git', '--git-dir=%s' % self.localpath, 'push', '-f', remotebaseurl + destinationpath] + refspec, \
            self.logger, \
            self.make_extra('push'))

logger = logging.getLogger('sync')
loghandler = logging.FileHandler(logfile)
loghandler.setFormatter(logging.Formatter('%(asctime)s %(repo)-20s %(phase)-6s %(levelname)-8s %(message)s'))
logger.addHandler(loghandler)
logger.setLevel(loglevel)
for repo in repos:
    try:
        syncer = RepoSyncer(repo, logger, loghandler)
        syncer.ensure_exists(sourceurl)
        syncer.update_repo()
        syncer.push_branches(desturl, branches[repo])
    except Exception as e:
        logger.error('syncing failed: %s.' % str(e), extra={'repo' : repo, 'phase' : 'fail'})