Daniel Fütterer
14.01.21 a6a9a06de140a71322d5a1280657d4ab75dd9be6
commit | author | age
07d2e3 1 #!/usr/bin/env python3
DF 2 # -*- coding: utf-8 -*-
3
4 ##### To Do #####
5 # - handle different keywords/types (what to process/ignore?)
6 # - scopes
a6a9a0 7 # - at the moment the script requires a tag in the latest commit to work properly
DF 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     # handle commit types (work in progress)
47     # variant a
a6a9a0 48     #if commit[0].startswith("feat"):
DF 49     #    print("Feature recognized!")
50     #elif commit[0].startswith("chore"):
51     #    print("Chore recognized!")
07d2e3 52     # variant b
a6a9a0 53     #for i in keywords:
DF 54     #    if commit[0].startswith(i):
55     #        print(i)
56     #        pass
07d2e3 57
DF 58
a6a9a0 59 # Grouping History using tags
DF 60 for commit in commitHistory: # latest commit must have a tag
61     # Look if commit has a tag
62     if (len(commit) == 3) and ("tag: v" in commit[2]):
63         taggedHistory.append([commit[2][(commit[2].rfind(": v")+3):-1],commit[0:2]])
64     else:
65         taggedHistory[-1].append(commit)
07d2e3 66
a6a9a0 67
DF 68 # Construction of the changelog-file
69 fileTemplate = ["# Changelog"]
70 for tag in taggedHistory:
71     fileTemplate.append("\n## Version " + tag[0])
72     for commit in tag[1:]:
73         fileTemplate.append("\t- " + commit[0] + " (" + commit[1] + ")")
74 # write into changelog
75 with open("changelog.md", "w") as file:
76     for line in fileTemplate:
77         file.write(line + "\n")