Merge branch 'maint'
[org-mode.git] / UTILITIES / git-changelog
blob85c5f794c035a73d3021a120c737e0264535fb3b
1 #!/usr/bin/env python
3 # git-changelog
5 # version 2.0, by John Wiegley
7 # The purpose of this code is to turn "git log" output into a complete
8 # ChangeLog, for projects who wish to begin using a ChangeLog, but haven't
9 # been.
11 # This version of git-changelog depends on GitPython:
12 # git://gitorious.org/git-python/mainline.git
14 import time
15 import string
16 import sys
17 import re
18 import os
20 from git import * # GitPython
21 from subprocess import *
23 repo = Repo(os.getcwd())
24 ref = 'origin/master..'
25 path = ''
27 # Usage: git changelog [COMMITISH] [-- [PATH]]
29 saw_dashdash = False
30 if len(sys.argv) > 1:
31 for arg in sys.argv[1:]:
32 if arg == "--":
33 saw_dashdash = True
34 elif saw_dashdash:
35 path = arg
36 else:
37 ref = arg
39 for commit in repo.iter_commits(ref, paths=path):
40 hash_id = commit.sha
41 author = commit.author
42 date = commit.committed_date
43 log_text = commit.message.split('\n')[0]
45 log_text_remainder = commit.message.split('\n\n')[1:]
46 while len(log_text_remainder) > 0 and not log_text_remainder[0]:
47 log_text_remainder = log_text_remainder[1:]
48 log_text_remainder = string.join(log_text_remainder, '\n\t')
49 if log_text_remainder:
50 log_text_remainder = '\n\t' + log_text_remainder
52 diff = commit.diff(commit.parents[0])
53 files = []
54 for f in diff:
55 if not f.a_blob:
56 p = f.b_blob.path
57 elif not f.b_blob:
58 p = f.a_blob.path
59 else:
60 continue
62 p2 = re.sub('^' + path + '/', '', p)
63 if p != p2:
64 files.append(p2)
66 fp = Popen(["fmt", "-72"], shell = True, stdin = PIPE, stdout = PIPE)
67 if files:
68 fp.stdin.write("\t* %s: %s" % (string.join(files, ",\n\t"), log_text))
69 else:
70 fp.stdin.write("\t* %s" % log_text)
71 fp.stdin.close()
72 log_text = fp.stdout.read()
73 del fp
75 print "%s %s <%s>\n" % \
76 (time.strftime("%Y-%m-%d", time.gmtime(date)),
77 author.name, author.email)
79 if path:
80 log_text = re.sub(' ' + path + '/', ' ', log_text)
81 log_text_remainder = re.sub(' ' + path + '/', ' ', log_text_remainder)
83 # If the log_text_remainder already begins with a *, then use that as the
84 # changelog text.
85 if re.match('\s+\* ', log_text_remainder):
86 if log_text_remainder[0] == '\n':
87 print log_text_remainder[1:]
88 else:
89 print log_text_remainder
90 else:
91 print "%s%s" % (log_text, log_text_remainder)
93 # git-changelog ends here