diff --git a/dev_scripts_helpers/git/git_hooks/pre-commit.py b/dev_scripts_helpers/git/git_hooks/pre-commit.py index 0906ebb1c..dada29e5e 100755 --- a/dev_scripts_helpers/git/git_hooks/pre-commit.py +++ b/dev_scripts_helpers/git/git_hooks/pre-commit.py @@ -48,6 +48,8 @@ def _write_output_to_file(lines: List[str]) -> None: # dshgghout.check_master() # + dshgghout.check_branch_name() + # dshgghout.check_author() # dshgghout.check_file_size() diff --git a/dev_scripts_helpers/git/git_hooks/utils.py b/dev_scripts_helpers/git/git_hooks/utils.py index 476269512..fc02db56a 100644 --- a/dev_scripts_helpers/git/git_hooks/utils.py +++ b/dev_scripts_helpers/git/git_hooks/utils.py @@ -476,6 +476,48 @@ def check_python_compile( _handle_error(func_name, error, abort_on_error) +# ############################################################################# +# check_branch_name +# ############################################################################# + + +def check_branch_name(abort_on_error: bool = True) -> None: + """ + Check if the current branch name follows the expected naming convention. + + - Expected format: {Prefix}Task{Number}_{Description} + - Example: HelpersTask123_Add_new_feature + """ + func_name = _report() + # Get current branch name. + verbose = True + cmd = "git rev-parse --abbrev-ref HEAD" + _, branch_name = _system_to_string(cmd, verbose=verbose) + branch_name = branch_name.strip() + print(f"Branch is '{branch_name}'") + # Skip validation for master/main branches. + if branch_name in ["master", "main"]: + print("Skipping branch name validation for master/main branch") + error = False + else: + # Check if branch name follows the expected pattern. + # Pattern: {Prefix}Task{Number}_{Description} + # Examples: HelpersTask123_Add_feature, AmpTask456_Fix_bug + pattern = r"^\S+Task\d+_\S+$" + if not re.match(pattern, branch_name): + msg = ( + f"Branch name '{branch_name}' does not follow the expected naming convention.\n" + f"Expected format: {{Prefix}}Task{{Number}}_{{Description}}\n" + f"Examples: HelpersTask123_Add_feature, AmpTask456_Fix_bug" + ) + _LOG.error(msg) + error = True + else: + error = False + # Handle error. + _handle_error(func_name, error, abort_on_error) + + # ############################################################################# # check_gitleaks # #############################################################################