Daniel Fütterer
14.01.21 a6a9a06de140a71322d5a1280657d4ab75dd9be6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
##### To Do #####
# - handle different keywords/types (what to process/ignore?)
# - scopes
# - at the moment the script requires a tag in the latest commit to work properly
# the processing ang recognizing of the commit components should be improved
 
import os
 
# a multidimensional list containing all commits separated by lines
commitHistory = []
# a multidimensional list containing all commits grouped by tags
taggedHistory = []
# a list of keywords used in the default conventional git (https://www.conventionalcommits.org/en/v1.0.0/)
# may later get converted into a dict
keywords = ["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test"]
 
# get the formatted output from git log with additional delimiter for easier splitting
stream = os.popen("git log --format=%B%H%n%d%n--END--")
output = stream.read()
output = output.split("--END--")
del output[-1]
 
# compute every commit (sepratae, delete unnecessary components, store into commitHistory)
for commit in output:
    # seperate lines
    wholeCommit = commit.split("\n")
    commit = [x for x in wholeCommit if x]
 
    # delete unnecessary lines
    for i,line in enumerate(commit):
        if line == "":
            del commit[i]
 
    if len(commit[1]) == 40: # Please improve here
        pass
    elif len(commit[2]) == 40:
        del commit[1]
    else: commit[2] = commit[2].strip()
 
    # put commit into list
    commitHistory.append(commit)
    
    # handle commit types (work in progress)
    # variant a
    #if commit[0].startswith("feat"):
    #    print("Feature recognized!")
    #elif commit[0].startswith("chore"):
    #    print("Chore recognized!")
    # variant b
    #for i in keywords:
    #    if commit[0].startswith(i):
    #        print(i)
    #        pass
 
 
# Grouping History using tags
for commit in commitHistory: # latest commit must have a tag
    # Look if commit has a tag
    if (len(commit) == 3) and ("tag: v" in commit[2]):
        taggedHistory.append([commit[2][(commit[2].rfind(": v")+3):-1],commit[0:2]])
    else:
        taggedHistory[-1].append(commit)
 
 
# Construction of the changelog-file
fileTemplate = ["# Changelog"]
for tag in taggedHistory:
    fileTemplate.append("\n## Version " + tag[0])
    for commit in tag[1:]:
        fileTemplate.append("\t- " + commit[0] + " (" + commit[1] + ")")
# write into changelog
with open("changelog.md", "w") as file:
    for line in fileTemplate:
        file.write(line + "\n")