@@ -33,6 +33,7 @@ def __init__(self, path: str, base_commit_sha: str | None = None):
3333 self .path = path
3434 self .base_commit_sha = base_commit_sha
3535 self ._fetched_ref_commits = {}
36+ self ._default_branch_name : str | None = None
3637 self .ensure_safe_directory (path )
3738 self .repo = Repo (path )
3839 assert self .repo
@@ -422,12 +423,10 @@ def _is_commit_and_branch_default(self) -> bool:
422423 github_ref = os .getenv ('GITHUB_REF' ) # e.g., 'refs/heads/main' or 'refs/pull/123/merge'
423424 gitlab_branch = os .getenv ('CI_COMMIT_BRANCH' )
424425 gitlab_mr_branch = os .getenv ('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME' )
425- gitlab_default_branch = os .getenv ('CI_DEFAULT_BRANCH' , '' )
426426 bitbucket_branch = os .getenv ('BITBUCKET_BRANCH' )
427427 buildkite_branch = os .getenv ('BUILDKITE_BRANCH' )
428428 buildkite_pr = os .getenv ('BUILDKITE_PULL_REQUEST' )
429- buildkite_default_branch = os .getenv ('BUILDKITE_PIPELINE_DEFAULT_BRANCH' )
430-
429+
431430 # Handle Buildkite before GitHub because some Buildkite pipelines
432431 # intentionally provide GitHub-compatible environment variables.
433432 if buildkite_branch :
@@ -437,7 +436,7 @@ def _is_commit_and_branch_default(self) -> bool:
437436 "not default branch"
438437 )
439438 return False
440- default_branch_name = buildkite_default_branch or self .get_default_branch_name ()
439+ default_branch_name = self .get_default_branch_name ()
441440 is_default = buildkite_branch == default_branch_name
442441 log .debug (
443442 f"Buildkite branch: { buildkite_branch } , Default: { default_branch_name } , "
@@ -470,7 +469,7 @@ def _is_commit_and_branch_default(self) -> bool:
470469 elif gitlab_branch or gitlab_mr_branch :
471470 # If this is a merge request, use the source branch
472471 current_branch = gitlab_mr_branch or gitlab_branch
473- default_branch_name = gitlab_default_branch or self .get_default_branch_name ()
472+ default_branch_name = self .get_default_branch_name ()
474473
475474 # For merge requests, they're typically not considered "default branch"
476475 if gitlab_mr_branch :
@@ -644,13 +643,17 @@ def get_default_branch_name(self) -> str:
644643 Returns:
645644 Default branch name (e.g., 'main', 'master')
646645 """
647- cached = getattr (self , "_default_branch_name" , None )
648- if cached :
649- return cached
650- self ._default_branch_name = self ._detect_default_branch_name ()
646+ if self ._default_branch_name is None :
647+ self ._default_branch_name = self ._detect_default_branch_name ()
651648 return self ._default_branch_name
652649
653650 def _detect_default_branch_name (self ) -> str :
651+ for variable in ('CI_DEFAULT_BRANCH' , 'BUILDKITE_PIPELINE_DEFAULT_BRANCH' ):
652+ default_branch = os .getenv (variable )
653+ if default_branch :
654+ log .debug (f"Default branch detected from { variable } : { default_branch } " )
655+ return default_branch
656+
654657 try :
655658 default_branch = self .repo .remotes .origin .refs .HEAD .reference .remote_head
656659 log .debug (f"Default branch detected from origin/HEAD: { default_branch } " )
@@ -666,13 +669,14 @@ def _detect_default_branch_name(self) -> str:
666669 if default_branch :
667670 return default_branch
668671
672+ try :
673+ remote_refs = {str (ref ) for ref in self .repo .remotes .origin .refs }
674+ except Exception :
675+ remote_refs = set ()
669676 for branch_name in ['main' , 'master' ]:
670- try :
671- if f'origin/{ branch_name } ' in [str (ref ) for ref in self .repo .remotes .origin .refs ]:
672- log .debug (f"Using fallback default branch: { branch_name } " )
673- return branch_name
674- except Exception :
675- continue
677+ if f'origin/{ branch_name } ' in remote_refs :
678+ log .debug (f"Using fallback default branch: { branch_name } " )
679+ return branch_name
676680
677681 log .debug ("Using final fallback default branch: main" )
678682 return 'main'
@@ -693,12 +697,8 @@ def _default_branch_from_github_event() -> str | None:
693697 return default_branch or None
694698
695699 def _default_branch_from_remote (self ) -> str | None :
696- # A new process group lets a timeout also kill the remote helpers, which
697- # otherwise hold stdout open and keep communicate() blocked.
698- if IS_WINDOWS :
699- group_kwargs = {"creationflags" : subprocess .CREATE_NEW_PROCESS_GROUP }
700- else :
701- group_kwargs = {"start_new_session" : True }
700+ # A new session lets a timeout also kill the remote helpers, which otherwise
701+ # hold stdout open and keep communicate() blocked. Windows ignores it.
702702 try :
703703 process = subprocess .Popen (
704704 ["git" , "ls-remote" , "--symref" , "origin" , "HEAD" ],
@@ -708,7 +708,7 @@ def _default_branch_from_remote(self) -> str | None:
708708 stdout = subprocess .PIPE ,
709709 stderr = subprocess .DEVNULL ,
710710 text = True ,
711- ** group_kwargs ,
711+ start_new_session = True ,
712712 )
713713 except Exception as error :
714714 log .debug (f"Could not query origin for its default branch: { error } " )
0 commit comments