Skip to content

Commit 5d42335

Browse files
FIX: Fix tag ordering by applying natural sort correctly to returned tag list in the 'Repository Tags Lister' Python Project
1 parent 3f8225d commit 5d42335

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Repository Tags Lister/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import atexit # For playing a sound when the program finishes
2+
import csv # For writing to a CSV file
23
import os # For running a command in the terminal
34
import platform # For getting the operating system name
4-
import csv # For writing to a CSV file
5+
import re # For regular expressions
56
import subprocess # For running git commands
67
from colorama import Style # For coloring the terminal
78

@@ -57,6 +58,17 @@ def verify_git_installed():
5758
except Exception: # If an error occurs during the process
5859
return False # Git is not installed
5960

61+
def _tag_sort_key(t):
62+
"""
63+
Key function for sorting tags in a human-friendly way.
64+
65+
:param t: Tag name
66+
:return: List of strings and integers for sorting
67+
"""
68+
69+
parts = re.split(r"(\d+)", t) # Split the tag into parts of digits and non-digits
70+
return [int(p) if p.isdigit() else p.lower() for p in parts] # Convert digit parts to integers for proper sorting
71+
6072
def get_repository_tags(repo_url):
6173
"""
6274
Retrieves all tag names from a remote Git repository using native git commands.
@@ -65,6 +77,10 @@ def get_repository_tags(repo_url):
6577
:return: list of tag names
6678
"""
6779

80+
if not verify_git_installed(): # Verify if git is installed
81+
print(f"{BackgroundColors.RED}Git is not installed on this system. Please install Git to retrieve repository tags.{Style.RESET_ALL}")
82+
exit(1) # Exit the program
83+
6884
tags_list = [] # List to store the tag names
6985

7086
try: # Try to retrieve the tags using git ls-remote
@@ -102,6 +118,8 @@ def get_repository_tags(repo_url):
102118
except Exception as e: # If an error occurs during the process
103119
print(f"{BackgroundColors.RED}Failed to retrieve tags: {BackgroundColors.CYAN}{e}{Style.RESET_ALL}")
104120

121+
tags_list = sorted(tags_list, key=_tag_sort_key) # Sort tags from lowest → highest
122+
105123
return tags_list # Return the list of tag names
106124

107125
def create_directory(full_directory_name, relative_directory_name):

0 commit comments

Comments
 (0)