|
2 | 2 |
|
3 | 3 | # git-changelog |
4 | 4 | # |
5 | | -# version 1.0, by John Wiegley |
| 5 | +# version 2.0, by John Wiegley |
6 | 6 | # |
7 | 7 | # The purpose of this code is to turn "git log" output into a complete |
8 | 8 | # ChangeLog, for projects who wish to begin using a ChangeLog, but haven't |
9 | 9 | # been. |
| 10 | +# |
| 11 | +# This version of git-changelog depends on GitPython: |
| 12 | +# git://gitorious.org/git-python/mainline.git |
10 | 13 |
|
11 | | -import datetime |
| 14 | +import time |
12 | 15 | import string |
13 | 16 | import sys |
14 | 17 | import re |
| 18 | +import os |
15 | 19 |
|
| 20 | +from git import * # GitPython |
16 | 21 | from subprocess import * |
17 | 22 |
|
18 | | -p = Popen("git log --stat %s" % string.join(sys.argv[1:], " "), |
19 | | - shell = True, stdout = PIPE).stdout |
20 | | - |
21 | | -line = p.readline() |
22 | | -while line: |
23 | | - match = None |
24 | | - while line and not match: |
25 | | - match = re.match("commit ([0-9a-f]+)", line) |
26 | | - if not match: |
27 | | - line = p.readline() |
28 | | - assert match |
29 | | - |
30 | | - hash_id = match.group(1) |
31 | | - |
32 | | - match = None |
33 | | - while line and not match: |
34 | | - line = p.readline() |
35 | | - match = re.match("Author: (.+)", line) |
36 | | - assert match |
37 | | - |
38 | | - author = match.group(1) |
39 | | - author = re.sub(" <", " <", author) |
40 | | - |
41 | | - line = p.readline() |
42 | | - |
43 | | - match = re.match("Date: +(.+?) [-+][0-9]{4}", line) |
44 | | - assert match |
45 | | - # Tue Sep 30 05:43:49 2003 +0000 |
46 | | - date = datetime.datetime.strptime(match.group(1), '%a %b %d %H:%M:%S %Y') |
47 | | - |
48 | | - line = p.readline() # absorb separator |
49 | | - line = p.readline() |
50 | | - |
51 | | - log_text = "" |
52 | | - log_text_remainder = "" |
53 | | - |
54 | | - while line and line != '\n': |
55 | | - if not log_text: |
56 | | - log_text += line[4:] |
57 | | - else: |
58 | | - log_text_remainder += "\t" + line[4:] |
59 | | - |
60 | | - line = p.readline() |
61 | | - |
62 | | - line = p.readline() |
63 | | - |
64 | | - if not re.match("commit", line): |
65 | | - files = [] |
66 | | - while line and line != '\n': |
67 | | - match = re.match(" (.+?) +\\|", line) |
68 | | - if match: |
69 | | - files.append(match.group(1)) |
70 | | - line = p.readline() |
71 | | - else: |
72 | | - break |
73 | | - |
74 | | - line = p.readline() |
| 23 | +repo = Repo(os.getcwd()) |
| 24 | +path = sys.argv[1] if len(sys.argv) == 2 else '' |
| 25 | + |
| 26 | +for commit in repo.iter_commits('origin/master..', paths=path): |
| 27 | + hash_id = commit.sha |
| 28 | + author = commit.author |
| 29 | + date = commit.committed_date |
| 30 | + log_text = commit.message.split('\n')[0] |
| 31 | + |
| 32 | + log_text_remainder = commit.message.split('\n\n')[1:] |
| 33 | + while len(log_text_remainder) > 0 and not log_text_remainder[0]: |
| 34 | + log_text_remainder = log_text_remainder[1:] |
| 35 | + log_text_remainder = string.join(log_text_remainder, '\n\t') |
| 36 | + if log_text_remainder: |
| 37 | + log_text_remainder = '\n\t' + log_text_remainder |
| 38 | + |
| 39 | + diff = commit.diff(commit.parents[0]) |
| 40 | + files = [] |
| 41 | + for f in diff: |
| 42 | + p = f.a_blob.path or f.b_blob.path |
| 43 | + p2 = re.sub('^' + path + '/', '', p) |
| 44 | + if p != p2: |
| 45 | + files.append(p2) |
75 | 46 |
|
76 | 47 | fp = Popen(["fmt", "-72"], shell = True, stdin = PIPE, stdout = PIPE) |
77 | 48 | fp.stdin.write("\t* %s: %s" % (string.join(files, ",\n\t"), log_text)) |
78 | 49 | fp.stdin.close() |
79 | 50 | log_text = fp.stdout.read() |
80 | 51 | del fp |
81 | 52 |
|
82 | | - print "%s %s\n\n%s%s" % \ |
83 | | - (date.strftime("%Y-%m-%d"), author, log_text, log_text_remainder) |
84 | | - #print "%s %s\n\n%s\t* commit %s\n" % \ |
85 | | - # (date.strftime("%Y-%m-%d"), author, log_text, hash_id) |
86 | | - |
87 | | - if not re.match("commit", line): |
88 | | - line = p.readline() |
| 53 | + print "%s %s <%s>\n\n%s%s" % \ |
| 54 | + (time.strftime("%Y-%m-%d", time.gmtime(date)), |
| 55 | + author.name, author.email, log_text, log_text_remainder) |
89 | 56 |
|
90 | 57 | # git-changelog ends here |
0 commit comments