@@ -74,12 +74,17 @@ impl GitRepo {
7474 }
7575
7676 /// Check if a local branch exists.
77- /// Equivalent to `git rev-parse --verify <branch>`
77+ /// Only checks for local branches, not remote refs.
7878 pub fn branch_exists ( & self , branch : & str ) -> bool {
7979 let _bench = GitBenchmark :: start ( "git2:branch-exists" ) ;
80- // First try as a local branch, then try direct ref resolution
80+ // Only check for local branches to avoid false positives from remote refs
8181 self . repo . find_branch ( branch, BranchType :: Local ) . is_ok ( )
82- || self . repo . revparse_single ( branch) . is_ok ( )
82+ }
83+
84+ /// Check if a ref exists (local branch, remote ref, or any resolvable ref).
85+ pub fn ref_exists ( & self , ref_name : & str ) -> bool {
86+ let _bench = GitBenchmark :: start ( "git2:ref-exists" ) ;
87+ self . repo . revparse_single ( ref_name) . is_ok ( )
8388 }
8489
8590 pub fn branch_status (
@@ -92,20 +97,32 @@ impl GitRepo {
9297 Some ( parent_branch) => parent_branch. to_string ( ) ,
9398 None => self . remote_main ( DEFAULT_REMOTE ) ?,
9499 } ;
95- let is_descendent = exists && self . is_ancestor ( & parent_branch, branch) ?;
96- let upstream_symbolic_name = self . get_upstream ( branch) ;
97- let upstream_synced = upstream_symbolic_name
98- . as_ref ( )
99- . is_some_and ( |upstream| self . shas_match ( upstream, branch) ) ;
100+
101+ // Only compute these if the branch exists
102+ let ( sha, is_descendent, upstream_status) = if exists {
103+ let sha = self . sha ( branch) ?;
104+ let is_descendent = self . is_ancestor ( & parent_branch, branch) ?;
105+ let upstream_symbolic_name = self . get_upstream ( branch) ;
106+ let upstream_synced = upstream_symbolic_name
107+ . as_ref ( )
108+ . is_some_and ( |upstream| self . shas_match ( upstream, branch) ) ;
109+ let upstream_status =
110+ upstream_symbolic_name. map ( |symbolic_name| UpstreamStatus {
111+ symbolic_name,
112+ synced : upstream_synced,
113+ } ) ;
114+ ( sha, is_descendent, upstream_status)
115+ } else {
116+ // Branch doesn't exist - use placeholder values
117+ ( String :: new ( ) , false , None )
118+ } ;
119+
100120 Ok ( GitBranchStatus {
101- sha : self . sha ( branch ) ? ,
121+ sha,
102122 parent_branch,
103123 exists,
104124 is_descendent,
105- upstream_status : upstream_symbolic_name. map ( |symbolic_name| UpstreamStatus {
106- symbolic_name,
107- synced : upstream_synced,
108- } ) ,
125+ upstream_status,
109126 } )
110127 }
111128
0 commit comments