From 1f2ae28cf05551faa7c6ce51dc7db080d491f683 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 13 Dec 2022 10:45:28 +0100 Subject: [PATCH] Try to auto-detect revision in append mode if not specified --- gitdch | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/gitdch b/gitdch index 9e7d26e..f8fe2f2 100755 --- a/gitdch +++ b/gitdch @@ -10,6 +10,7 @@ import sys import textwrap import git +from git.exc import GitCommandError VERSION = '0.0' DEFAULT_GIT_PATCH = './' @@ -244,6 +245,30 @@ if not options.version: repo.git.describe('--always', '--tags') ) log.info('Currrent version detected: %s', options.version) + +if options.output and options.append and not options.revision: + log.info( + 'Append mode enabled but no revision specify, try to detect it from ' + 'last modification of the changelog file') + try: + last_change_commit = next(repo.iter_commits(paths=options.output)) + # pylint: disable=consider-using-f-string + options.revision = '{0}..HEAD'.format(last_change_commit) + log.info( + 'Last change commit of the output file is "%s": use revision "%s"', + last_change_commit, options.revision) + except StopIteration: + log.warning( + 'Fail to auto-detect last change commit of changelog file: it ' + 'seem not tracked. Continue without revision.') + except GitCommandError: + log.warning( + "Fail to auto-detect last change commit of changelog file. May " + "be it's outside of the git repository. Continue without " + "revision.") + # Reset repo object of to avoid BrokenPipeError + repo = git.Repo(options.git_path) + log.info('Generate changelog from git commits') versions = []