From 90d00cf631d076c5562d23987a917328cd5cee45 Mon Sep 17 00:00:00 2001 From: Andrey Pokrovskiy Date: Wed, 3 May 2023 11:20:46 -0700 Subject: [PATCH] Option to skip `packed_refs` in `iter_items()` In cases where tool only need recent-ish refs (e.g. base branch autodetect) it will save a lot of time if `packed_refs` can be excluded from search. --- git/refs/symbolic.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 33c3bf15b..f48e467be 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -661,7 +661,8 @@ def rename(self, new_path: PathLike, force: bool = False) -> "SymbolicReference" @classmethod def _iter_items( - cls: Type[T_References], repo: "Repo", common_path: Union[PathLike, None] = None + cls: Type[T_References], repo: "Repo", common_path: Union[PathLike, None] = None, + packed_refs: bool = True ) -> Iterator[T_References]: if common_path is None: common_path = cls._common_path_default @@ -685,10 +686,11 @@ def _iter_items( # END for each directory to walk # read packed refs - for _sha, rela_path in cls._iter_packed_refs(repo): - if rela_path.startswith(str(common_path)): - rela_paths.add(rela_path) - # END relative path matches common path + if packed_refs: + for _sha, rela_path in cls._iter_packed_refs(repo): + if rela_path.startswith(str(common_path)): + rela_paths.add(rela_path) + # END relative path matches common path # END packed refs reading # return paths in sorted order @@ -704,6 +706,7 @@ def iter_items( cls: Type[T_References], repo: "Repo", common_path: Union[PathLike, None] = None, + packed_refs: bool = True, *args: Any, **kwargs: Any, ) -> Iterator[T_References]: @@ -723,7 +726,7 @@ def iter_items( List is lexicographically sorted The returned objects represent actual subclasses, such as Head or TagReference""" - return (r for r in cls._iter_items(repo, common_path) if r.__class__ == SymbolicReference or not r.is_detached) + return (r for r in cls._iter_items(repo, common_path, packed_refs) if r.__class__ == SymbolicReference or not r.is_detached) @classmethod def from_path(cls: Type[T_References], repo: "Repo", path: PathLike) -> T_References: