From a6a9a06de140a71322d5a1280657d4ab75dd9be6 Mon Sep 17 00:00:00 2001
From: Daniel Fütterer <df89@outlook.com>
Date: Thu, 14 Jan 2021 18:03:13 +0100
Subject: [PATCH] feat: Enable Grouping

---
 changelog_generator.py |   67 ++++++++++++++++++++++-----------
 1 files changed, 44 insertions(+), 23 deletions(-)

diff --git a/changelog_generator.py b/changelog_generator.py
index d9cc906..40e877d 100644
--- a/changelog_generator.py
+++ b/changelog_generator.py
@@ -3,54 +3,75 @@
 
 ##### To Do #####
 # - handle different keywords/types (what to process/ignore?)
-# - recognize tags
-# - sort by tags
 # - 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 the Changelog
-log = []
 # 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----DELIMITER----")
+stream = os.popen("git log --format=%B%H%n%d%n--END--")
 output = stream.read()
-output = output.split("----DELIMITER----")
+output = output.split("--END--")
 del output[-1]
 
-# compute every commit
+# compute every commit (sepratae, delete unnecessary components, store into commitHistory)
 for commit in output:
     # seperate lines
     wholeCommit = commit.split("\n")
-    # delete empty strings
     commit = [x for x in wholeCommit if x]
-    # delete long commit message
-    if len(commit) == 3:
+
+    # 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!")
+    #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)
+    #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)
 
-# write into logfile for test purpose
-with open("log.txt", "w") as file:
-    for commit in commitHistory:
-        for line in commit:
-            file.write(line)
-            file.write("\n")
+
+# 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")

--
Gitblit v1.9.1