| 1 | #! /usr/bin/python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) 2012 Rob Guttman <guttman@alum.mit.edu> |
|---|
| 5 | # All rights reserved. |
|---|
| 6 | # |
|---|
| 7 | # This software is licensed as described in the file COPYING, which |
|---|
| 8 | # you should have received as part of this distribution. |
|---|
| 9 | # |
|---|
| 10 | |
|---|
| 11 | import os |
|---|
| 12 | import re |
|---|
| 13 | import sys |
|---|
| 14 | import sqlite3 |
|---|
| 15 | from subprocess import Popen, STDOUT, PIPE |
|---|
| 16 | |
|---|
| 17 | EPOCH_MULTIPLIER = 1000000.0 |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | def sync(db_path, repo_dir): |
|---|
| 21 | db = sqlite3.connect(db_path) |
|---|
| 22 | reponame = os.path.basename(repo_dir.rstrip('/')).lower() |
|---|
| 23 | |
|---|
| 24 | # extract changesets to sync |
|---|
| 25 | changeset_lines = get_changeset_lines(repo_dir) |
|---|
| 26 | print "processing %d changesets" % len(changeset_lines) |
|---|
| 27 | |
|---|
| 28 | # purge table for this repo |
|---|
| 29 | cursor = db.cursor() |
|---|
| 30 | cursor.execute(""" |
|---|
| 31 | DELETE FROM codereviewer_map |
|---|
| 32 | WHERE repo='%s' |
|---|
| 33 | """ % reponame) |
|---|
| 34 | |
|---|
| 35 | # insert a row per ticket in commit message |
|---|
| 36 | for changeset_line in changeset_lines: |
|---|
| 37 | print '.', |
|---|
| 38 | rev,when,msg = changeset_line.split('|',2) |
|---|
| 39 | when = long(when) * EPOCH_MULTIPLIER |
|---|
| 40 | ticket_re = re.compile('#([0-9]+)') |
|---|
| 41 | for ticket in ticket_re.findall(msg): |
|---|
| 42 | try: |
|---|
| 43 | cursor.execute(""" |
|---|
| 44 | INSERT INTO codereviewer_map |
|---|
| 45 | (repo,changeset,ticket,time) |
|---|
| 46 | VALUES ('%s','%s','%s',%s); |
|---|
| 47 | """ % (reponame,rev,ticket,when)) |
|---|
| 48 | except sqlite3.IntegrityError: |
|---|
| 49 | print "\nduplicate %s, %s, #%s" % (reponame,rev,ticket) |
|---|
| 50 | db.commit() |
|---|
| 51 | |
|---|
| 52 | def get_changeset_lines(repo_dir): |
|---|
| 53 | """Return all changesets including their commit time and message |
|---|
| 54 | pipe-delimited in chronological order.""" |
|---|
| 55 | cmds = ['cd %s' % repo_dir, 'git log --reverse --format="%H|%ct|%s"'] |
|---|
| 56 | return execute(' && '.join(cmds)).splitlines() |
|---|
| 57 | |
|---|
| 58 | def execute(cmd): |
|---|
| 59 | p = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE) |
|---|
| 60 | out = p.communicate()[0] |
|---|
| 61 | if p.returncode != 0: |
|---|
| 62 | raise Exception('cmd: %s\n%s' % (cmd,out)) |
|---|
| 63 | return out |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | if __name__ == "__main__": |
|---|
| 67 | if len(sys.argv) < 3: |
|---|
| 68 | file = os.path.basename(sys.argv[0]) |
|---|
| 69 | print "usage: %s <db_path> <repo_dir>" % file |
|---|
| 70 | sys.exit(1) |
|---|
| 71 | |
|---|
| 72 | # sync |
|---|
| 73 | db_path = sys.argv[1] |
|---|
| 74 | repo_dir = sys.argv[2] |
|---|
| 75 | sync(db_path,repo_dir) |
|---|