From 81f9a860d4cbc3027b63d7921e4c142b3a88218d Mon Sep 17 00:00:00 2001 From: Markus Lassfolk Date: Mon, 1 Jan 2024 21:54:29 +0100 Subject: [PATCH] Fix IndexError in get_last_tag_from_remote function This commit adds a check to ensure that the output from the subprocess command in the get_last_tag_from_remote function has a sufficient number of lines before attempting to access specific indices. This change prevents the IndexError that occurred when the git command's output was shorter than expected. --- bin/packages/git_status.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/bin/packages/git_status.py b/bin/packages/git_status.py index 7b9c467c..19bf7769 100755 --- a/bin/packages/git_status.py +++ b/bin/packages/git_status.py @@ -129,17 +129,25 @@ def get_last_tag_from_local(verbose=False): print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT)) return '' -# Get last local tag +# Get last remote tag def get_last_tag_from_remote(verbose=False): if verbose: print('retrieving last remote tag ...') #print('git ls-remote --tags') + process = subprocess.run(['git', 'ls-remote', '--tags'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) if process.returncode == 0: - res = process.stdout.split(b'\n')[-2].split(b'/')[-1].replace(b'^{}', b'').decode() - if verbose: - print(res) - return res + output_lines = process.stdout.split(b'\n') + if len(output_lines) > 1: + # Assuming we want the second-to-last line as before + res = output_lines[-2].split(b'/')[-1].replace(b'^{}', b'').decode() + if verbose: + print(res) + return res + else: + if verbose: + print("No tags found or insufficient output from git command.") + return '' else: if verbose: