@@ -42,27 +42,37 @@ def get_latest_git_revision(branch_name):
4242 raise Exception (f"Failed to get the latest revision for branch '{ branch_name } '." ) from e
4343
4444
45- def get_trigger_branch (repo , default_branch ):
45+ def get_trigger_branch (repo , default_branch , exclude_branches = None ):
4646 """
4747 Gets the trigger branch name, handling detached HEAD state in CI environments.
4848
4949 In CI environments, the repository might be checked out at a specific commit (detached HEAD).
5050 This function tries multiple methods to determine the branch:
51- 1. Check if HEAD is attached to a branch
51+ 1. Check if HEAD is attached to a branch (but skip if it's a release branch or excluded branch)
5252 2. Check environment variables (YAMATO_BRANCH, CI_COMMIT_REF_NAME, etc.)
5353 3. Use git commands to find which remote branch contains the current commit
5454 4. Fall back to the default branch if nothing else works
5555
5656 Args:
5757 repo: GitPython Repo object
5858 default_branch: Default branch name to fall back to
59+ exclude_branches: Optional list of branch names to exclude (e.g., release branches)
5960
6061 Returns:
6162 str: The branch name
6263 """
64+ exclude_branches = exclude_branches or []
65+ current_branch = None
66+
6367 try :
6468 # Try to get the active branch name (works when HEAD is attached)
65- return repo .active_branch .name
69+ current_branch = repo .active_branch .name
70+ # If we're on a release branch or excluded branch, don't use it - use other methods
71+ if current_branch .startswith ('release/' ) or current_branch in exclude_branches :
72+ print (f"Current branch '{ current_branch } ' is a release/excluded branch, using other methods to find trigger branch..." )
73+ current_branch = None
74+ else :
75+ return current_branch
6676 except (TypeError , ValueError ):
6777 # HEAD is detached, try other methods
6878 pass
@@ -95,19 +105,24 @@ def get_trigger_branch(repo, default_branch):
95105 )
96106
97107 branches = [b .strip () for b in result .stdout .strip ().split ('\n ' ) if b .strip ()]
98- # Filter to find the most likely branch (prefer default branch, then develop, then others)
108+ # Filter out release branches and excluded branches
109+ valid_branches = []
99110 for branch_line in branches :
100111 branch = branch_line .replace ('origin/' , '' ).strip ()
101- if branch and branch == default_branch :
102- print (f"Found trigger branch from remote branches: { branch } " )
103- return branch
112+ if branch and not branch .startswith ('release/' ) and branch not in exclude_branches :
113+ valid_branches .append (branch )
104114
105- # If default branch not found, use the first one
106- if branches :
107- branch = branches [0 ].replace ('origin/' , '' ).strip ()
108- if branch :
115+ # Prefer default branch, then other valid branches
116+ for branch in valid_branches :
117+ if branch == default_branch :
109118 print (f"Found trigger branch from remote branches: { branch } " )
110119 return branch
120+
121+ # If default branch not found, use the first valid branch
122+ if valid_branches :
123+ branch = valid_branches [0 ]
124+ print (f"Found trigger branch from remote branches: { branch } " )
125+ return branch
111126 except Exception as e :
112127 print (f"Warning: Could not determine branch from remote branches: { e } " )
113128
0 commit comments