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