Skip to content

Commit 7a1af24

Browse files
codexByron
authored andcommitted
feat(gix): enumerate reference names natively
List native reference names in Git's order for the managed `for-each-ref` format and literal prefixes. Keep glob patterns, pseudo refs and other formats on the CLI rather than interpreting them as plain prefixes. The staged version passes Ruff and Python syntax checks. Its SHA-1/SHA-256 native smoke and focused pytest selection pass: 6 passed in 1.88s.
1 parent 334cf9e commit 7a1af24

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

‎git/_backend.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,29 @@ def _symbolic_ref(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes:
176176
return target + b"\n"
177177

178178

179+
def _for_each_ref(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes:
180+
if not args or args[0] != "--format=%(refname)":
181+
raise _Unsupported("reference format/options")
182+
split = args.index("--") if "--" in args else len(args)
183+
if args[1:split] or len(args[split + 1 :]) > 1:
184+
raise _Unsupported("root refs or reference options")
185+
prefix = os.fsencode(args[-1]) if len(args) > split + 1 else b""
186+
if any(char in prefix for char in b"*?["):
187+
raise _Unsupported("reference glob patterns")
188+
with repo.references().all() as refs:
189+
names = [ref.name() for ref in refs]
190+
return b"".join(
191+
name + b"\n"
192+
for name in sorted(names)
193+
if not prefix or name == prefix or name.startswith(prefix.rstrip(b"/") + b"/")
194+
)
195+
196+
179197
_HANDLERS: Dict[str, Callable[[Any, List[str], Dict[str, Any]], bytes]] = {
180198
"rev_parse": _rev_parse,
181199
"ls_tree": _ls_tree,
182200
"symbolic_ref": _symbolic_ref,
201+
"for_each_ref": _for_each_ref,
183202
}
184203

185204

0 commit comments

Comments
 (0)