#!/usr/bin/env python3 # -*- coding: utf-8 -*- ##### To Do ##### # - handle different keywords/types (what to process/ignore?) # - scopes # 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: # 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: # Check if latest commit has a tag if len(taggedHistory)==0: # If there is no tag, create empty string instead taggedHistory.append(["", commit]) else: taggedHistory[-1].append(commit) # Construction of the changelog-file fileTemplate = ["# Changelog"] for tag in taggedHistory: if tag[0] == "": fileTemplate.append("\n## No version number yet ") else: 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")