From c4d692c98f3e06d39cf80bf8fa2035c2e398646b Mon Sep 17 00:00:00 2001 From: df89 <df89@me.com> Date: Sun, 10 Oct 2021 18:24:20 +0200 Subject: [PATCH] refactor: separated script into different files --- main.py | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 107 insertions(+), 0 deletions(-) diff --git a/main.py b/main.py new file mode 100644 index 0000000..27c0420 --- /dev/null +++ b/main.py @@ -0,0 +1,107 @@ +import os.path +import sys + +from functions import getCommitList +from rawcommit import RawCommit +from commit import Commit +from scope import Scope +from commitBody import CommitBody + + +# Compute user input of repository path via console +inputPath = input("Please enter the base path of the repository: ").strip() +userDecision = input("Should the generated changelog be stored in another location (y/n)? ").lower().strip() +if userDecision == "y": + outputPath = input("Please enter the output path: ").strip() +elif userDecision == "n": + print("The changelog will be stored in the same location as the repository.") + outputPath = inputPath +else: + print("invalid input") + sys.exit(1) + + +# create a list of separate commits in chronological order (new to old) +commitHistory = getCommitList(inputPath) + + +# Create a two-dimensional list by tags: [[tag, [commits]],[tag, [commits]],...] +taggedHistory = [] +for commit in commitHistory: + if commit.tag.tagAsString: + taggedHistory.append([commit.tag, commit]) + else: + if len(taggedHistory) == 0: + taggedHistory.append([None, commit]) + else: + taggedHistory[-1].append(commit) + + +# Construction of the changelog-file +fileTemplate = ["# Changelog"] +for tag in taggedHistory: + ## If latest commit has no tag: + if not tag[0]: + fileTemplate.append("\n## Without version number") + else: + fileTemplate.append("\n## Version " + tag[0].tagAsString) + + + ## Grouping by Type + featType = ["Features"] + fixType = ["Fixes"] + otherType = ["Other"] + nonconformCommits = [] + + for commit in tag[1:]: + if commit.body.commitType == CommitBody.commitTypes[5]: # fix + fixType.append(commit) + elif commit.body.commitType == CommitBody.commitTypes[4]: # feat + featType.append(commit) + elif commit.body.commitType == "nonconform": + nonconformCommits.append(commit) + else: + otherType.append(commit) + + ## Sub-Grouping by Scopes within Types + commitlistByType = [featType, fixType, otherType] + for commitsByType in commitlistByType: + if len(commitsByType) == 1: + continue + commitlistByScope = {} + noScope = [] + for commit in commitsByType: + if type(commit) == str: + continue + if commit.body.scope == None: + noScope.append(commit) + elif commit.body.scope in commitlistByScope: + commitlistByScope[commit.body.scope].append(commit) + else: + commitlistByScope[commit.body.scope] = [commit] + + fileTemplate.append("\n### " + commitsByType[0]) + while len(commitlistByScope) > 0: + scope, commits = commitlistByScope.popitem() + fileTemplate.append("- *" + str(scope) + "*") + for commit in commits: + if commitsByType[0] == "Other": + fileTemplate.append(" - (" + commit.body.commitType + ") " + commit.body.subject + commit.appendShortHash()) + else: + fileTemplate.append(" - " + commit.body.subject + commit.appendShortHash()) + if len(noScope) > 0: + fileTemplate.append("- *no scope*") + for commit in noScope: + fileTemplate.append(" - " + commit.body.subject + commit.appendShortHash()) + + ## nonconform commits + if len(nonconformCommits) > 0: + fileTemplate.append("\n### Non-conform commits") + for commit in nonconformCommits: + fileTemplate.append("- " + commit.body.subject + commit.appendShortHash()) + + +# write into changelog.md file +with open(outputPath + "/changelog.md", "w") as file: + for line in fileTemplate: + file.write(line + "\n") \ No newline at end of file -- Gitblit v1.9.1