| 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (C) 2013 nu774 |
| 4 | # For conditions of distribution and use, see copyright notice in COPYING |
| 5 | |
| 6 | import sys |
| 7 | import re |
| 8 | from subprocess import Popen, PIPE |
| 9 | from itertools import groupby |
| 10 | from collections import namedtuple |
| 11 | |
| 12 | GITLOG_FMT = 'commit %H%nauthor %cn <%ae>%ndate %ad%nsubject %s%nref %d%n%n' |
| 13 | GITLOG_CMD = ['git','log','--date=short','--format={0}'.format(GITLOG_FMT)] |
| 14 | |
| 15 | Commit = namedtuple('Commit', 'commit author date subject ref') |
| 16 | |
| 17 | def parse_gitlog(stream): |
| 18 | re_decode_tag = re.compile(r'(?<=\()([^,)]+)') |
| 19 | commit = dict() |
| 20 | for line in stream: |
| 21 | fields = line.decode('utf-8').rstrip('\r\n').split(' ', 1) |
| 22 | if len(fields) == 2: |
| 23 | key, value = fields |
| 24 | if key == 'ref': |
| 25 | m = re_decode_tag.search(value) |
| 26 | value = ' [{0}]'.format(m.group()) if m else '' |
| 27 | commit[key] = value |
| 28 | elif commit: |
| 29 | yield Commit(**commit) |
| 30 | commit = dict() |
| 31 | |
| 32 | output=sys.stdout.write |
| 33 | |
| 34 | with Popen(GITLOG_CMD, shell=False, stdout=PIPE).stdout as pipe: |
| 35 | commits = parse_gitlog(pipe) |
| 36 | commits_by_date_author = groupby(commits, key=lambda x: (x.date, x.author)) |
| 37 | for (date, author), commits in commits_by_date_author: |
| 38 | output('{0} {1}\n\n'.format(date, author)) |
| 39 | for c in commits: |
| 40 | output(' * {0}{1}\n\n'.format(c.subject, c.ref)) |