From 38389eb17373076cddb237bbf369dccb0a9bb1dd Mon Sep 17 00:00:00 2001 From: Marcel Kapfer Date: Wed, 26 May 2021 22:25:21 +0200 Subject: [PATCH] Add changelog diff extraction script --- changelog-diff.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 changelog-diff.py diff --git a/changelog-diff.py b/changelog-diff.py new file mode 100755 index 0000000..bda61d1 --- /dev/null +++ b/changelog-diff.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# +# Script for retrieving a condensed changelog between two revisions +# +# 2021 (c) Marcel Kapfer +# Licensed under the MIT/Expat License +# +# NOTES: +# +# - This is a draft! It may burn your house, delete your harddrive +# and/or kill your kitten. + +import sys +import subprocess + + +def changelog_diff(old, new, changelog_file = "./CHANGELOG.md"): + raw_diff = subprocess.run(["git", "diff", "--unified=0", + "{}..{}".format(old, new), changelog_file], + stdout=subprocess.PIPE) + last_block_start = 0 + diff = [] + for line in raw_diff.stdout.decode('utf-8').splitlines()[4::]: + if line.startswith("@@ ") and line.endswith(" @@"): + if last_block_start == 2: + diff.pop() + last_block_start = 1 + elif line.startswith("+"): + last_block_start = last_block_start + 1 + diff.append(line[1::]) + else: + last_block_start = last_block_start + 1 + return diff + + +def main(): + help_str = "Must be: ./changelog-diff old-revision new-revision [changelog file]" + # Check for correct amount of arguments + if len(sys.argv) < 3: + print("[ERROR] Not enough arguments. {}".format(help_str)) + sys.exit(1) + elif len(sys.argv) == 3: + diff = changelog_diff(sys.argv[1], sys.argv[2]) + elif len(sys.argv) == 4: + diff = changelog_diff(sys.argv[1], sys.argv[2], sys.argv[3]) + else: + print("[ERROR] Two much arguments. {}".format(help_str)) + sys.exit(1) + print("\n".join(diff)) + + +if __name__ == "__main__": + main()