@@ -83,47 +83,8 @@ pub fn fetch_authorship_notes(
8383 remote_name, tracking_ref
8484 ) ) ;
8585
86- // First, check if the remote has refs/notes/ai using ls-remote
87- // This is important for bare repos where the refmap might not be configured
88- let mut ls_remote_args = repository. global_args_for_exec ( ) ;
89- ls_remote_args. push ( "ls-remote" . to_string ( ) ) ;
90- ls_remote_args. push ( remote_name. to_string ( ) ) ;
91- ls_remote_args. push ( "refs/notes/ai" . to_string ( ) ) ;
92-
93- debug_log ( & format ! ( "ls-remote command: {:?}" , ls_remote_args) ) ;
94-
95- match exec_git ( & ls_remote_args) {
96- Ok ( output) => {
97- let result = String :: from_utf8_lossy ( & output. stdout ) . to_string ( ) ;
98- debug_log ( & format ! ( "ls-remote stdout: '{}'" , result) ) ;
99- debug_log ( & format ! (
100- "ls-remote stderr: '{}'" ,
101- String :: from_utf8_lossy( & output. stderr)
102- ) ) ;
103-
104- if result. trim ( ) . is_empty ( ) {
105- debug_log ( & format ! (
106- "no authorship notes found on remote '{}', nothing to sync" ,
107- remote_name
108- ) ) ;
109- return Ok ( NotesExistence :: NotFound ) ;
110- }
111- debug_log ( & format ! (
112- "found authorship notes on remote '{}'" ,
113- remote_name
114- ) ) ;
115- }
116- Err ( e) => {
117- debug_log ( & format ! (
118- "failed to check for authorship notes on remote '{}': {}" ,
119- remote_name, e
120- ) ) ;
121- // Return error instead of assuming no notes - we don't know the state
122- return Err ( e) ;
123- }
124- }
125-
126- // Now fetch the notes to the tracking ref with explicit refspec
86+ // Fetch notes to tracking ref with explicit refspec.
87+ // If the remote does not have refs/notes/ai yet, treat that as NotFound.
12788 let fetch_refspec = format ! ( "+refs/notes/ai:{}" , tracking_ref) ;
12889
12990 // Build the internal authorship fetch with explicit flags and disabled hooks.
@@ -148,6 +109,13 @@ pub fn fetch_authorship_notes(
148109 ) ) ;
149110 }
150111 Err ( e) => {
112+ if is_missing_remote_notes_ref_error ( & e) {
113+ debug_log ( & format ! (
114+ "no authorship notes found on remote '{}', nothing to sync" ,
115+ remote_name
116+ ) ) ;
117+ return Ok ( NotesExistence :: NotFound ) ;
118+ }
151119 debug_log ( & format ! ( "authorship fetch failed: {}" , e) ) ;
152120 return Err ( e) ;
153121 }
@@ -187,6 +155,19 @@ pub fn fetch_authorship_notes(
187155
188156 Ok ( NotesExistence :: Found )
189157}
158+
159+ fn is_missing_remote_notes_ref_error ( error : & GitAiError ) -> bool {
160+ let GitAiError :: GitCliError { stderr, .. } = error else {
161+ return false ;
162+ } ;
163+
164+ let stderr_lower = stderr. to_ascii_lowercase ( ) ;
165+ stderr_lower. contains ( "refs/notes/ai" )
166+ && ( stderr_lower. contains ( "couldn't find remote ref" )
167+ || stderr_lower. contains ( "could not find remote ref" )
168+ || stderr_lower. contains ( "remote ref does not exist" )
169+ || stderr_lower. contains ( "not our ref" ) )
170+ }
190171// for use with post-push hook
191172pub fn push_authorship_notes ( repository : & Repository , remote_name : & str ) -> Result < ( ) , GitAiError > {
192173 // STEP 1: Fetch remote notes into tracking ref and merge before pushing
@@ -368,4 +349,25 @@ mod tests {
368349 ) ;
369350 assert ! ( args. contains( & "push" . to_string( ) ) ) ;
370351 }
352+
353+ #[ test]
354+ fn missing_remote_notes_ref_error_is_detected ( ) {
355+ let err = GitAiError :: GitCliError {
356+ code : Some ( 128 ) ,
357+ stderr : "fatal: couldn't find remote ref refs/notes/ai" . to_string ( ) ,
358+ args : vec ! [ "fetch" . to_string( ) , "origin" . to_string( ) ] ,
359+ } ;
360+ assert ! ( is_missing_remote_notes_ref_error( & err) ) ;
361+ }
362+
363+ #[ test]
364+ fn missing_remote_notes_ref_error_ignores_unrelated_git_errors ( ) {
365+ let err = GitAiError :: GitCliError {
366+ code : Some ( 128 ) ,
367+ stderr : "fatal: Authentication failed for 'https://github.com/org/repo.git/'"
368+ . to_string ( ) ,
369+ args : vec ! [ "fetch" . to_string( ) , "origin" . to_string( ) ] ,
370+ } ;
371+ assert ! ( !is_missing_remote_notes_ref_error( & err) ) ;
372+ }
371373}
0 commit comments