Daniel Fütterer
28.01.21 398ff1150aa0334565b6aa614a08a910d238bfbc
commit | author | age
07d2e3 1 #!/usr/bin/env python3
DF 2 # -*- coding: utf-8 -*-
3
4 ##### To Do #####
f8d69f 5 # - handle further keywords/types other than "feat" and "fix"
398ff1 6 # - deal with parameters configuring the output (dev/usr; types, hash y/n, ...)
DF 7 # Refactoring: rewrite using oop (-> UML)
a6a9a0 8 # the processing ang recognizing of the commit components should be improved
07d2e3 9
DF 10 import os
11
12 # a multidimensional list containing all commits separated by lines
13 commitHistory = []
a6a9a0 14 # a multidimensional list containing all commits grouped by tags
DF 15 taggedHistory = []
07d2e3 16 # a list of keywords used in the default conventional git (https://www.conventionalcommits.org/en/v1.0.0/)
DF 17 # may later get converted into a dict
18 keywords = ["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test"]
19
20 # get the formatted output from git log with additional delimiter for easier splitting
a6a9a0 21 stream = os.popen("git log --format=%B%H%n%d%n--END--")
07d2e3 22 output = stream.read()
a6a9a0 23 output = output.split("--END--")
07d2e3 24 del output[-1]
DF 25
a6a9a0 26 # compute every commit (sepratae, delete unnecessary components, store into commitHistory)
07d2e3 27 for commit in output:
DF 28     # seperate lines
29     wholeCommit = commit.split("\n")
30     commit = [x for x in wholeCommit if x]
a6a9a0 31
DF 32     # delete unnecessary lines
33     for i,line in enumerate(commit):
34         if line == "":
35             del commit[i]
36
37     if len(commit[1]) == 40: # Please improve here
38         pass
39     elif len(commit[2]) == 40:
07d2e3 40         del commit[1]
a6a9a0 41     else: commit[2] = commit[2].strip()
DF 42
07d2e3 43     # put commit into list
DF 44     commitHistory.append(commit)
45
46
a6a9a0 47 # Grouping History using tags
1092d6 48 for commit in commitHistory:
a6a9a0 49     # Look if commit has a tag
DF 50     if (len(commit) == 3) and ("tag: v" in commit[2]):
51         taggedHistory.append([commit[2][(commit[2].rfind(": v")+3):-1],commit[0:2]])
52     else:
1092d6 53         # Check if latest commit has a tag
DF 54         if len(taggedHistory)==0:
55             # If there is no tag, create empty string instead
56             taggedHistory.append(["", commit])
57         else:
58             taggedHistory[-1].append(commit)
07d2e3 59
a6a9a0 60
DF 61 # Construction of the changelog-file
62 fileTemplate = ["# Changelog"]
63 for tag in taggedHistory:
f8d69f 64     # A Dictionairy to store grouped commits
DF 65     commitsByType = {"Fixes":[],"Features":[]}
66
67     # If latest commit has no tag:
1092d6 68     if tag[0] == "":
DF 69         fileTemplate.append("\n## No version number yet ")
70     else:
71         fileTemplate.append("\n## Version " + tag[0])
f8d69f 72     
DF 73     # Grouping by type "feat" and "fix"
a6a9a0 74     for commit in tag[1:]:
398ff1 75         old = commit[0]
DF 76         if commit[0].startswith(keywords[5]): # "fix"
77             if commit[0].startswith("fix: "):
78                 new = old[(old.index(":")+2):]
79                 commit[0] = new
80                 commitsByType["Fixes"].append(commit)
81             else:
82                 new = "**" + old[(old.index("(")+1):old.index(")")] + "**: " + old[(old.index(":")+2):]
83                 commit[0] = new
84                 commitsByType["Fixes"].append(commit)
85         elif commit[0].startswith(keywords[4]): # "feat"
86             if commit[0].startswith("feat: "):
87                 new = old[(old.index(":")+2):]
88                 commit[0] = new
89                 commitsByType["Features"].append(commit)
90             else:
91                 new = "**" + old[(old.index("(")+1):old.index(")")] + "**: " + old[(old.index(":")+2):]
92                 commit[0] = new
93                 commitsByType["Features"].append(commit)
94                 
f8d69f 95     
DF 96     # Creating grouped output
97     if len(commitsByType["Features"]) != 0:
98         fileTemplate.append("### Features")
99         for feature in commitsByType["Features"]:
398ff1 100             fileTemplate.append("- " + feature[0] + " (" + commit[1][:6] + ")")
f8d69f 101     if len(commitsByType["Fixes"]) != 0:
DF 102         fileTemplate.append("### Fixes")
103         for fix in commitsByType["Fixes"]:
398ff1 104             fileTemplate.append("- " + fix[0] + " (" + commit[1][:6] + ")")
f8d69f 105
a6a9a0 106 # write into changelog
DF 107 with open("changelog.md", "w") as file:
108     for line in fileTemplate:
109         file.write(line + "\n")