From ffc0f3814b6a54c82100daa7fe80a4973d07e861 Mon Sep 17 00:00:00 2001 From: Joren Verspeurt Date: Wed, 19 Jun 2019 17:16:49 +0200 Subject: [PATCH] Fix for the case where someone commits for the first time on a repository and so getting their previous commit fails --- githooks/prepare-commit-msg | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/githooks/prepare-commit-msg b/githooks/prepare-commit-msg index fc108cd..648337b 100755 --- a/githooks/prepare-commit-msg +++ b/githooks/prepare-commit-msg @@ -33,11 +33,21 @@ def extract_jira_issue_key(message: str) -> Optional[str]: def last_commit_datetime() -> datetime.datetime: # https://git-scm.com/docs/git-log#_pretty_formats author = run_command('git config user.email') - last_datetime = datetime.datetime.strptime( - run_command( - f'git log -1 --branches --format=%aI --author={author}' - ).split('+')[0], - '%Y-%m-%dT%H:%M:%S') # %z + last_author_stamp = run_command( + f'git log -1 --branches --format=%aI --author={author}' + ) + if '+' in last_author_stamp: + last_datetime = datetime.datetime.strptime( + last_author_stamp.split('+')[0], + '%Y-%m-%dT%H:%M:%S' + ) # %z + else: + last_datetime = datetime.datetime.strptime( + run_command( + f'git log -1 --branches --format=%aI' + ).split('+')[0], + '%Y-%m-%dT%H:%M:%S' + ) # %z return last_datetime