OSDN Git Service

When the service of lxcf is disabled, the execution of the lxcf command is made enable.
[lxcf/lxcf.git] / lxcf / script / gitlog2changelog.py
1 #!/usr/bin/python
2 # Copyright 2008 Marcus D. Hanwell <marcus@cryos.org>
3 # Distributed under the terms of the GNU General Public License v2 or later
4
5 # Modified by (C) 2012-2014 TAMUKI Shoichi <tamuki@linet.gr.jp>
6
7 import os, re
8
9 # Execute git log with the desired command line options.
10 fin = os.popen("git log --date=short --stat --summary --since=\"1970-01-01 00:00:00\"", "r")
11 # Create a ChangeLog file in the current directory.
12 fout = open("ChangeLog", "w")
13
14 # Set up the loop variables in order to locate the blocks we want
15 authorFound = False
16 dateFound = False
17 messageFound = False
18 messageNL = False
19 messageSkip = False
20 messageItem = False
21 message = ""
22 filesFound = False
23 files = ""
24 prevAuthorLine = ""
25 commitSet = ""
26
27 # The main part of the loop
28 for line in fin:
29         # The commit line marks the start of a new commit object.
30         if re.search("^commit ", line):
31                 # Start all over again...
32                 authorFound = False
33                 dateFound = False
34                 messageFound = False
35                 messageNL = False
36                 messageSkip = False
37                 messageItem = False
38                 message = ""
39                 filesFound = False
40                 files = ""
41         # Match the author line and extract the part we want
42         elif re.search("^Author: ", line):
43                 author = line[8:].strip()
44                 authorFound = True
45         # Match the date line
46         elif re.search("^Date:   ", line):
47                 date = line[8:].strip()
48                 dateFound = True
49         # Extract the actual commit message for this commit
50         elif authorFound and dateFound and not messageFound:
51                 # Find the commit message if we can
52                 if line == "\n":
53                         if messageNL:
54                                 messageFound = True
55                         else:
56                                 messageNL = True
57                 elif line == "    \n":
58                         if not message.endswith("\n"):
59                                 message += "\n"
60                 # The sign off line is ignored
61                 elif re.search("^    Signed-off-by: ", line):
62                         pass
63                 # This is a special workaround for the git log of LXC Facility
64                 elif date == "2014-03-02" and re.search("^    This patch series", line):
65                         messageSkip = True
66                 elif date == "2014-03-02" and re.search("^    MIME-Version: ", line):
67                         messageSkip = False
68                 elif messageSkip:
69                         pass
70                 else:
71                         if messageItem:
72                                 if not message.endswith("\n"):
73                                         if not re.search("^    \s*[^*-]", line):
74                                                 message += "\n"
75                                 if not re.search("^    \s*[*-]", line):
76                                         messageItem = False
77                         else:
78                                 if re.search("^    \s*[*-]", line):
79                                         if not message.endswith("\n"):
80                                                 message += "\n"
81                                         messageItem = True
82                         if message.endswith("."):
83                                 message += "  "
84                         elif not message.endswith("\n"):
85                                 message += " "
86                         message += line.strip()
87         # Collect the files for this commit. FIXME: Still need to add +/- to files
88         elif authorFound and dateFound and messageFound and not filesFound:
89                 fileList = line.split(" | ", 1)
90                 if len(fileList) == 2:
91                         if files:
92                                 files += ", "
93                         files += fileList[0].strip()
94                 # If this line is hit all of the files have been stored for this commit
95                 elif re.search("^ [0-9]* files? changed, ", line):
96                         filesFound = True
97         # All of the parts of the commit have been found - write out the entry
98         if authorFound and dateFound and messageFound and filesFound:
99                 # First the author line, only outputted if it is the first for that
100                 # author on this day
101                 authorLine = date + "  " + author
102                 if not prevAuthorLine:
103                         fout.write(authorLine + "\n")
104                         commitSet = ""
105                 elif authorLine != prevAuthorLine:
106                         # Write out the commit lines
107                         fout.write(commitSet + "\n")
108                         fout.write("\n" + authorLine + "\n")
109                         commitSet = ""
110
111                 # Assemble the actual commit message line(s) and limit the line length
112                 # to 78 characters.
113                 commitLine = files + ":" + message
114                 lc = len(commitLine)
115                 commit = ""
116                 i = j = 0
117                 while i < lc:
118                         if i == j:
119                                 lv = 0
120                         commit += "\n\t* " if i == 0 else "\n\t  "
121                         if lv == 1:
122                                 commit += "  "
123                         if i + 68 - lv * 2 >= lc:
124                                 i2 = lc - 1
125                         else:
126                                 p = -1
127                                 for s in re.finditer("\s", commitLine[i:i + 69 - lv * 2]):
128                                         p = i + s.start()
129                                 i2 = i + 67 - lv * 2 if p == -1 else p
130                         p = commitLine.find("\n", i, i + 68 - lv * 2)
131                         if 0 <= p < i2:
132                                 i2 = p
133                         if i == j:
134                                 p = commitLine.find("\n", i)
135                                 j = lc if p == -1 else p + 1
136                                 if re.search("^[*-]", commitLine[i:i + j - 1]):
137                                         lv = 1
138                         commit += commitLine[i:i2 + 1].strip()
139                         i = i2 + 1
140
141                 # Concatenate the commit lines in ascending order by time
142                 commitSet = commit + commitSet
143
144                 # Now reset all the variables ready for a new commit block.
145                 authorFound = False
146                 dateFound = False
147                 messageFound = False
148                 messageNL = False
149                 messageSkip = False
150                 messageItem = False
151                 message = ""
152                 filesFound = False
153                 files = ""
154                 prevAuthorLine = authorLine
155
156 # Write out the commit lines
157 fout.write(commitSet + "\n")
158
159 # Write out the relay message
160 #fout.write("\nFor the changes before 1.0.0, see ChangeLog.1\n")
161
162 # Close the input and output lines now that we are finished.
163 fin.close()
164 fout.close()