|
| 1 | +#!/usr/bin/env python3 |
| 2 | +############################################################################### |
| 3 | +# Copyright (c) 2019, 2024 Ericsson |
| 4 | +# |
| 5 | +# All rights reserved. This program and the accompanying materials |
| 6 | +# are made available under the terms of the Eclipse Public License 2.0 |
| 7 | +# which accompanies this distribution, and is available at |
| 8 | +# https://www.eclipse.org/legal/epl-2.0 |
| 9 | +# |
| 10 | +# SPDX-License-Identifier: EPL-2.0 |
| 11 | +############################################################################### |
| 12 | + |
| 13 | +import io |
| 14 | +from os import getlogin |
| 15 | +import subprocess |
| 16 | +import re |
| 17 | +import argparse |
| 18 | +import datetime |
| 19 | +from datetime import date |
| 20 | + |
| 21 | +def valid_date(s): |
| 22 | + try: |
| 23 | + # Try to parse the string into a date time with the format YYYY-MM-DD |
| 24 | + datetime.datetime.strptime(s, "%Y-%m-%d") |
| 25 | + return s |
| 26 | + except ValueError: |
| 27 | + msg = "not a valid date: {0!r}".format(s) |
| 28 | + raise argparse.ArgumentTypeError(msg) |
| 29 | + |
| 30 | +date_time = date.today().strftime("%Y-%m-%d") |
| 31 | +report = dict() |
| 32 | +parser = argparse.ArgumentParser(description = 'Generates a new and noteworthy in markdown from a git tree using two dates (yyyy-MM-dd).') |
| 33 | +parser.add_argument('-a','--after', help = 'Include commits after and including this specific date (yyyy-MM-dd)', required = True, type=valid_date) |
| 34 | +parser.add_argument('-b','--before', help = '(Optional) Include commits before and including this specific date (yyyy-MM-dd)', required = False, default = date_time, type=valid_date) |
| 35 | + |
| 36 | +args = parser.parse_args() |
| 37 | + |
| 38 | +tagPattern = re.compile("^\[([A-Za-z]*)\]\s*(.*\S)\s*") |
| 39 | + |
| 40 | +notTags = ["main"] |
| 41 | +startHead = "date:" |
| 42 | +isItStartHead = False |
| 43 | +commitMessage = "" |
| 44 | +hasTags = False |
| 45 | + |
| 46 | +def update_entry(entry, line): |
| 47 | + if entry not in report: |
| 48 | + report[entry] = list() |
| 49 | + |
| 50 | + report[entry].append(line) |
| 51 | + |
| 52 | +def process_line(line): |
| 53 | + global isItStartHead |
| 54 | + global startHead |
| 55 | + global commitMessage |
| 56 | + global hasTags |
| 57 | + global endOfCommit |
| 58 | + |
| 59 | + # This mark the end of a commit header, the next non-empty line is the commit message |
| 60 | + if line.lower().startswith(startHead): |
| 61 | + if hasTags is False and len(commitMessage) != 0: |
| 62 | + update_entry("unknown", commitMessage) |
| 63 | + |
| 64 | + isItStartHead = True |
| 65 | + hasTags = False |
| 66 | + commitMessage = "" |
| 67 | + return |
| 68 | + |
| 69 | + # This is the commit message, store in case no tags are found |
| 70 | + if isItStartHead and len(line) != 0: |
| 71 | + commitMessage = line |
| 72 | + isItStartHead = False |
| 73 | + return |
| 74 | + |
| 75 | + # Check if the line contains some tags |
| 76 | + matcher = tagPattern.match(line.strip()) |
| 77 | + if matcher is not None: |
| 78 | + tag = matcher.group(1).strip() |
| 79 | + |
| 80 | + # If the line contains a valid tag, add it to the report |
| 81 | + if tag not in notTags: |
| 82 | + update_entry(tag, matcher.group(2)) |
| 83 | + hasTags = True |
| 84 | + |
| 85 | +if __name__=='__main__': |
| 86 | + after = args.after |
| 87 | + before = args.before |
| 88 | + cmd = ['git', '--no-pager','log', '--after', after, '--until', before] |
| 89 | + proc = subprocess.Popen(cmd, stdout = subprocess.PIPE) |
| 90 | + commit = "" |
| 91 | + |
| 92 | + for line in io.TextIOWrapper(proc.stdout, encoding = "utf-8"): |
| 93 | + try: |
| 94 | + line = line.strip() |
| 95 | + if (line.startswith("commit")): |
| 96 | + commit = line[len("commit "):].strip() |
| 97 | + process_line(line) |
| 98 | + except UnicodeDecodeError as e: |
| 99 | + print ("Error {0} in {1}, could not parse commit message {2}".format(e, commit, line)) |
| 100 | + print ("New and Noteworthy for {0} to {1}\n".format(after, before)) |
| 101 | + print ("# NewInxx.y") |
| 102 | + for entry in report: |
| 103 | + print ("\n## " + entry.title() + "\n") |
| 104 | + for line in report[entry]: |
| 105 | + print("- " + line) |
0 commit comments