2222 NoReturn ,
2323 Optional ,
2424 Sequence ,
25+ Set ,
2526 TypeVar ,
2627 Union ,
2728 cast ,
@@ -371,7 +372,13 @@ def _display_ref(path: str) -> str:
371372 return path
372373
373374 @classmethod
374- def _from_line (cls , repo : "Repo" , line : str , refspecs : Sequence [str ] = (), refs : Sequence [str ] = ()) -> "FetchInfo" :
375+ def _from_line (
376+ cls ,
377+ repo : "Repo" ,
378+ line : str ,
379+ refspecs : Sequence [str ] = (),
380+ refs_by_display : Optional [Dict [str , Set [str ]]] = None ,
381+ ) -> "FetchInfo" :
375382 """Decode Git's full verbose fetch output without reading FETCH_HEAD."""
376383 match = cls ._re_fetch_result .match (line )
377384 if match is None :
@@ -394,26 +401,46 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs:
394401 if destination == "FETCH_HEAD" :
395402 ref = SymbolicReference (repo , destination )
396403 else :
404+ # Git's "new" status describes the source namespace.
405+ if flags & cls .NEW_TAG :
406+ wildcard_sources = ["refs/tags/" + source ]
407+ elif flags & cls .NEW_HEAD :
408+ wildcard_sources = ["refs/heads/" + source ]
409+ else :
410+ wildcard_sources = ["refs/remotes/" + source ]
411+ if operation != "[new ref]" :
412+ wildcard_sources .extend (("refs/heads/" + source , "refs/tags/" + source ))
413+ if cls ._display_ref (source ) == source :
414+ wildcard_sources .append (source )
415+
397416 candidates = set ()
398417 for spec in refspecs :
399418 if spec .startswith ("^" ) or ":" not in spec :
400419 continue
401420 remote , local = spec .lstrip ("+" ).split (":" , 1 )
402- remote = cls ._display_ref (remote )
403421 if "*" in remote :
404422 prefix , suffix = remote .split ("*" , 1 )
405- if not source .startswith (prefix ) or not source .endswith (suffix ):
406- continue
407- middle = source [len (prefix ) : len (source ) - len (suffix ) if suffix else None ]
408- local = local .replace ("*" , middle )
409- elif remote != source :
410- continue
411- if cls ._display_ref (local ) == destination :
423+ for path in wildcard_sources :
424+ if not path .startswith (prefix ) or not path .endswith (suffix ):
425+ continue
426+ middle = path [len (prefix ) : len (path ) - len (suffix ) if suffix else None ]
427+ mapped = local .replace ("*" , middle )
428+ if cls ._display_ref (mapped ) == destination :
429+ candidates .add (mapped )
430+ elif (
431+ source in (cls ._display_ref (remote ), cls ._display_ref ("refs/" + remote ))
432+ and cls ._display_ref (local ) == destination
433+ ):
412434 candidates .add (local )
413- if destination .startswith ("refs/" ):
414- candidates = {destination }
435+ matching_refs = refs_by_display .get (destination , ()) if refs_by_display is not None else ()
436+ if (control == "=" or old_commit is not None ) and len (candidates ) > 1 :
437+ candidates .intersection_update (matching_refs )
415438 if not candidates :
416- candidates = {path for path in refs if cls ._display_ref (path ) == destination }
439+ candidates .update (matching_refs )
440+ # Git shortens refs/tags/refs/heads/main to refs/heads/main, so a
441+ # displayed name under refs/ is only a full name if nothing shortens to it.
442+ if not candidates and destination .startswith ("refs/" ):
443+ candidates = {destination }
417444 if len (candidates ) > 1 and ("tag" in operation or control == "t" ):
418445 candidates = {path for path in candidates if path .startswith ("refs/tags/" )}
419446 if len (candidates ) != 1 :
@@ -820,14 +847,17 @@ def _get_fetch_info_from_stderr(
820847 continue
821848
822849 refs = self .repo .git ._call_process_safe ("for_each_ref" , "--format=%(refname)" ).split ("\n " )
850+ refs_by_display : Dict [str , Set [str ]] = {}
851+ for path in refs :
852+ refs_by_display .setdefault (FetchInfo ._display_ref (path ), set ()).add (path )
823853 try :
824854 configured = self .config_reader .config .get_values (self ._config_section_name (), "fetch" )
825855 except (cp .NoSectionError , cp .NoOptionError ):
826856 configured = []
827857 specs = [* refspecs , * configured ]
828858 for line in fetch_info_lines :
829859 try :
830- output .append (FetchInfo ._from_line (self .repo , line , specs , refs ))
860+ output .append (FetchInfo ._from_line (self .repo , line , specs , refs_by_display ))
831861 except ValueError as exc :
832862 _logger .warning ("Cannot decode fetch result %r: %s" , line , exc )
833863 # A source-only refspec reports both FETCH_HEAD and any opportunistic
0 commit comments