|
| 1 | +#!/usr/bin/env python |
| 2 | +"""A simple script to validate that a git tag matches a SemVer pattern.""" |
| 3 | + |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | + |
| 7 | +SEMVER_SIMPLE = re.compile(r'(\d+)\.(\d+)\.(\d+)((a|b|rc)(\d+))?') |
| 8 | +SEMVER_PATTERN = re.compile( |
| 9 | + r""" |
| 10 | + ^ # Start of the string |
| 11 | + v? # Optional 'v' prefix (common in Git tags) |
| 12 | + (?P<major>0|[1-9]\d*)\. # Major version |
| 13 | + (?P<minor>0|[1-9]\d*)\. # Minor version |
| 14 | + (?P<patch>0|[1-9]\d*) # Patch version |
| 15 | + (?:-(?P<prerelease> # Optional pre-release section |
| 16 | + (?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*) # Identifier |
| 17 | + (?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))* |
| 18 | + ))? |
| 19 | + (?:\+(?P<build> # Optional build metadata section |
| 20 | + [0-9a-zA-Z-]+ # Identifier |
| 21 | + (?:\.[0-9a-zA-Z-]+)* |
| 22 | + ))? |
| 23 | + $ # End of the string |
| 24 | + """, |
| 25 | + re.VERBOSE, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def get_current_tag() -> str: |
| 30 | + """Get current git tag.""" |
| 31 | + try: |
| 32 | + # Gets the name of the latest tag reachable from the current commit |
| 33 | + result = subprocess.run(['git', 'describe', '--tags', '--abbrev=0'], capture_output=True, text=True, check=True) |
| 34 | + return result.stdout.strip() |
| 35 | + except subprocess.CalledProcessError: |
| 36 | + print("Could not find a reachable tag.") |
| 37 | + return '' |
| 38 | + |
| 39 | + |
| 40 | +def is_semantic_version(tag_name: str) -> bool: |
| 41 | + """Check if a given string complies with the semantic versioning 2.0.0 specification. |
| 42 | +
|
| 43 | + Args: |
| 44 | + tag_name: The name of the Git tag to validate. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + bool: True if the tag is a valid semantic version, False otherwise. |
| 48 | +
|
| 49 | + """ |
| 50 | + # The regex pattern for semantic versioning 2.0.0 (source: https://semver.org/) |
| 51 | + semver_pattern = re.compile( |
| 52 | + r""" |
| 53 | + ^ # Start of the string |
| 54 | + v? # Optional 'v' prefix (common in Git tags) |
| 55 | + (?P<major>0|[1-9]\d*)\. # Major version |
| 56 | + (?P<minor>0|[1-9]\d*)\. # Minor version |
| 57 | + (?P<patch>0|[1-9]\d*) # Patch version |
| 58 | + (?:-(?P<prerelease> # Optional pre-release section |
| 59 | + (?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*) # Identifier |
| 60 | + (?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))* |
| 61 | + ))? |
| 62 | + (?:\+(?P<build> # Optional build metadata section |
| 63 | + [0-9a-zA-Z-]+ # Identifier |
| 64 | + (?:\.[0-9a-zA-Z-]+)* |
| 65 | + ))? |
| 66 | + $ # End of the string |
| 67 | + """, |
| 68 | + re.VERBOSE, |
| 69 | + ) |
| 70 | + |
| 71 | + return bool(semver_pattern.match(tag_name)) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + import sys |
| 76 | + |
| 77 | + git_tag = get_current_tag() |
| 78 | + if not git_tag: |
| 79 | + print('Git tag does not exist for current commit.') |
| 80 | + sys.exit(-1) |
| 81 | + |
| 82 | + if not is_semantic_version(git_tag): |
| 83 | + print(rf"Git tag '{git_tag}' is invalid according to SemVer.") |
| 84 | + sys.exit(-1) |
| 85 | + |
| 86 | + print(rf"Git tag '{git_tag}' is valid.") |
0 commit comments