Python 2 EOL: migrate eud2gc.py to Python 3
[claws.git] / tools / gitlog2changelog.py
1 #!/usr/bin/python2
2 # Copyright 2008 Marcus D. Hanwell <marcus@cryos.org>
3 # Adapted for Claws Mail - Copyright 2013 Colin Leroy <colin@colino.net>
4 # Distributed under the terms of the GNU General Public License v2 or later
5
6 import string, re, os, sys
7
8 curRev = ""
9 prevVer = ""
10
11 if len(sys.argv) == 3:
12     curRev = sys.argv[2]
13     prevVer = sys.argv[1]
14 else:
15     curRevCmd = os.popen("git describe")
16     curRev = curRevCmd.read()
17     curRev = curRev[0:len(curRev)-1]
18     curRevCmd.close()
19     if len(sys.argv) == 2:
20         prevVer = sys.argv[1]
21     else:
22         prevVer = re.split('-', curRev, 1)[0]
23
24 # Execute git log with the desired command line options.
25 fin = os.popen('git log ' + prevVer + '..' + curRev +' --summary --stat --no-merges --date=short', 'r')
26 # Create a ChangeLog file in the current directory.
27 fout = sys.stdout
28
29 # Set up the loop variables in order to locate the blocks we want
30 authorFound = False
31 dateFound = False
32 messageFound = False
33 filesFound = False
34 message = ""
35 commit = ""
36 messageNL = False
37 files = ""
38 prevAuthorLine = ""
39
40 # The main part of the loop
41 for line in fin:
42     # The commit line marks the start of a new commit object.
43     if re.match('^commit', line) >= 0:
44         # Start all over again...
45         authorFound = False
46         dateFound = False
47         messageFound = False
48         messageNL = False
49         message = ""
50         filesFound = False
51         files = ""
52         commitCmd = os.popen("git describe "+re.split(' ', line, 1)[1])
53         commit = commitCmd.read()
54         commitCmd.close()
55         commit = commit[0:len(commit)-1]
56         continue
57     # Match the author line and extract the part we want
58     elif re.match('^Author:', line) >=0:
59         authorList = re.split(': ', line, 1)
60         author = re.split('<', authorList[1], 1)[0]
61         author = "[" + author[0:len(author)-1]+"]"
62         authorFound = True
63         continue
64     # Match the date line
65     elif re.match('^Date:', line) >= 0:
66         dateList = re.split(':   ', line, 1)
67         date = dateList[1]
68         date = date[0:len(date)-1]
69         dateFound = True
70         continue
71     # The svn-id lines are ignored
72     elif re.match('    git-svn-id:', line) >= 0:
73         continue
74     # The sign off line is ignored too
75     elif re.search('Signed-off-by', line) >= 0:
76         continue
77     # Extract the actual commit message for this commit
78     elif authorFound & dateFound & messageFound == False:
79         # Find the commit message if we can
80         if len(line) == 1:
81             if messageNL:
82                 messageFound = True
83             else:
84                 messageNL = True
85         elif len(line) == 4:
86             messageFound = True
87         else:
88             if len(message) == 0:
89                 message = message + line.strip()
90             else:
91                 message = message + " " + line.strip()
92     # If this line is hit all of the files have been stored for this commit
93     elif re.search('file(s)? changed', line) >= 0:
94         filesFound = True
95         continue
96     # Collect the files for this commit. FIXME: Still need to add +/- to files
97     elif authorFound & dateFound & messageFound:
98         fileList = re.split(' \| ', line, 2)
99         if len(fileList) > 1:
100             if len(files) > 0:
101                 files = files + "\n\t* " + fileList[0].strip()
102             else:
103                 files = "\t* " + fileList[0].strip()
104     # All of the parts of the commit have been found - write out the entry
105     if authorFound & dateFound & messageFound & filesFound:
106         # First the author line, only outputted if it is the first for that
107         # author on this day
108         authorLine = date + "  " + author
109         if len(prevAuthorLine) != 0:
110             fout.write("\n");
111         fout.write(authorLine + " " + commit + "\n\n")
112
113         # Assemble the actual commit message line(s) and limit the line length
114         # to 80 characters.
115         commitLine = message
116         i = 0
117         commit = ""
118         while i < len(commitLine):
119             if len(commitLine) < i + 70:
120                 commit = commit + "\n\t\t" + commitLine[i:len(commitLine)]
121                 break
122             index = commitLine.rfind(' ', i, i+70)
123             if index > i:
124                 commit = commit + "\n\t\t" + commitLine[i:index]
125                 i = index+1
126             else:
127                 commit = commit + "\n\t\t" + commitLine[i:70]
128                 i = i+71
129
130         # Write out the commit line
131         fout.write(files + "\t\t" + commit + "\n")
132
133         #Now reset all the variables ready for a new commit block.
134         authorFound = False
135         dateFound = False
136         messageFound = False
137         messageNL = False
138         message = ""
139         filesFound = False
140         files = ""
141         prevAuthorLine = authorLine
142
143 # Close the input and output lines now that we are finished.
144 fin.close()
145 fout.close()