Skip to content

Commit 927867e

Browse files
committed
Improved git-changelog, it now depends on GitPython
1 parent 96df906 commit 927867e

File tree

1 file changed

+33
-66
lines changed

1 file changed

+33
-66
lines changed

git-changelog

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,56 @@
22

33
# git-changelog
44
#
5-
# version 1.0, by John Wiegley
5+
# version 2.0, by John Wiegley
66
#
77
# The purpose of this code is to turn "git log" output into a complete
88
# ChangeLog, for projects who wish to begin using a ChangeLog, but haven't
99
# been.
10+
#
11+
# This version of git-changelog depends on GitPython:
12+
# git://gitorious.org/git-python/mainline.git
1013

11-
import datetime
14+
import time
1215
import string
1316
import sys
1417
import re
18+
import os
1519

20+
from git import * # GitPython
1621
from subprocess import *
1722

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)
7546

7647
fp = Popen(["fmt", "-72"], shell = True, stdin = PIPE, stdout = PIPE)
7748
fp.stdin.write("\t* %s: %s" % (string.join(files, ",\n\t"), log_text))
7849
fp.stdin.close()
7950
log_text = fp.stdout.read()
8051
del fp
8152

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)
8956

9057
# git-changelog ends here

0 commit comments

Comments
 (0)