1919 run ,
2020)
2121from libvcs ._internal .types import StrOrBytesPath , StrPath
22+ from libvcs .cmd .git_filter import (
23+ Auto ,
24+ Combine ,
25+ GitFilter ,
26+ GitFilterInput ,
27+ coerce_filter ,
28+ filter_specs ,
29+ )
2230
2331_CMD = StrOrBytesPath | Sequence [StrOrBytesPath ]
2432
2533
34+ def _filter_flags (
35+ value : GitFilterInput | None ,
36+ * ,
37+ allow_auto : bool ,
38+ combine_multiple : bool = False ,
39+ ) -> list [str ]:
40+ specs = filter_specs (value )
41+ if not allow_auto and any (spec == Auto ().to_spec () for spec in specs ):
42+ msg = "auto filter is not supported by this Git command"
43+ raise ValueError (msg )
44+ if combine_multiple and len (specs ) > 1 :
45+ pending = [coerce_filter (spec ) for spec in reversed (specs )]
46+ filters : list [GitFilter ] = []
47+ while pending :
48+ child = pending .pop ()
49+ if isinstance (child , Combine ):
50+ pending .extend (reversed (child .filters ))
51+ else :
52+ filters .append (child )
53+ specs = (Combine (tuple (filters )).to_spec (),)
54+ return [f"--filter={ spec } " for spec in specs ]
55+
56+
2657class Git :
2758 """Run commands directly on a git repository."""
2859
@@ -328,6 +359,7 @@ def clone(
328359 no_remote_submodules : bool | None = None ,
329360 verbose : bool | None = None ,
330361 quiet : bool | None = None ,
362+ _filter : GitFilterInput | None = None ,
331363 # Pass-through to run
332364 config : dict [str , t .Any ] | None = None ,
333365 log_in_real_time : bool = False ,
@@ -348,6 +380,9 @@ def clone(
348380 Separate repository (.git/ ) from working tree
349381 force : bool, optional
350382 force operation to run
383+ _filter : GitFilterInput | None, optional
384+ Partial-clone object filter model, spec string, mapping, or
385+ sequence of filters; emits one ``--filter=<spec>`` per item.
351386 make_parents : bool, default: ``True``
352387 Creates checkout directory (`:attr:`self.path`) if it doesn't already exist.
353388
@@ -367,8 +402,7 @@ def clone(
367402 local_flags .append (f"--template={ template } " )
368403 if separate_git_dir is not None :
369404 local_flags .append (f"--separate-git-dir={ separate_git_dir !s} " )
370- if (_filter := kwargs .pop ("_filter" , None )) is not None :
371- local_flags .append (f"--filter={ _filter } " )
405+ local_flags .extend (_filter_flags (_filter , allow_auto = True ))
372406 if depth is not None :
373407 local_flags .extend (["--depth" , str (depth )])
374408 if branch is not None :
@@ -477,12 +511,19 @@ def fetch(
477511 show_forced_updates : bool | None = None ,
478512 no_show_forced_updates : bool | None = None ,
479513 negotiate_only : bool | None = None ,
514+ _filter : GitFilterInput | None = None ,
480515 # libvcs special behavior
481516 check_returncode : bool | None = None ,
482517 ** kwargs : t .Any ,
483518 ) -> str :
484519 """Download from repo. Wraps `git fetch <https://git-scm.com/docs/git-fetch>`_.
485520
521+ Parameters
522+ ----------
523+ _filter : GitFilterInput | None, optional
524+ Partial-clone object filter model, spec string, mapping, or
525+ sequence of filters; emits one ``--filter=<spec>`` per item.
526+
486527 Examples
487528 --------
488529 >>> git = Git(path=example_git_repo.path)
@@ -503,8 +544,7 @@ def fetch(
503544
504545 if submodule_prefix is not None :
505546 local_flags .append (f"--submodule-prefix={ submodule_prefix !r} " )
506- if (_filter := kwargs .pop ("_filter" , None )) is not None :
507- local_flags .append (f"--filter={ _filter } " )
547+ local_flags .extend (_filter_flags (_filter , allow_auto = True ))
508548 if depth is not None :
509549 local_flags .extend (["--depth" , depth ])
510550 if deepen is not None :
@@ -891,6 +931,7 @@ def pull(
891931 show_forced_updates : bool | None = None ,
892932 no_show_forced_updates : bool | None = None ,
893933 negotiate_only : bool | None = None ,
934+ _filter : GitFilterInput | None = None ,
894935 # Pass-through to run
895936 log_in_real_time : bool = False ,
896937 check_returncode : bool | None = None ,
@@ -899,6 +940,12 @@ def pull(
899940 ) -> str :
900941 """Download from repo. Wraps `git pull <https://git-scm.com/docs/git-pull>`_.
901942
943+ Parameters
944+ ----------
945+ _filter : GitFilterInput | None, optional
946+ Rejected with :exc:`ValueError` when nonempty because native
947+ ``git pull`` omits ``--filter``; call :meth:`Git.fetch` first.
948+
902949 Examples
903950 --------
904951 >>> git = Git(path=example_git_repo.path)
@@ -999,8 +1046,9 @@ def pull(
9991046 #
10001047 if submodule_prefix is not None :
10011048 local_flags .append (f"--submodule-prefix={ submodule_prefix !r} " )
1002- if (_filter := kwargs .pop ("_filter" , None )) is not None :
1003- local_flags .append (f"--filter={ _filter } " )
1049+ if filter_specs (_filter ):
1050+ msg = "git pull does not accept filters; call Git.fetch with _filter first"
1051+ raise ValueError (msg )
10041052 if depth is not None :
10051053 local_flags .extend (["--depth" , depth ])
10061054 if deepen is not None :
@@ -2532,13 +2580,21 @@ def update(
25322580 rebase : bool | None = None ,
25332581 merge : bool | None = None ,
25342582 recursive : bool | None = None ,
2583+ _filter : GitFilterInput | None = None ,
25352584 # Pass-through to run()
25362585 log_in_real_time : bool = False ,
25372586 check_returncode : bool | None = None ,
25382587 ** kwargs : t .Any ,
25392588 ) -> str :
25402589 """Git submodule update.
25412590
2591+ Parameters
2592+ ----------
2593+ _filter : GitFilterInput | None, optional
2594+ Partial-clone object filter applied to initialized submodules;
2595+ multiple items flatten into one ``combine:`` flag, and ``auto`` is
2596+ rejected with :exc:`ValueError`.
2597+
25422598 Examples
25432599 --------
25442600 >>> GitSubmoduleCmd(path=example_git_repo.path).update()
@@ -2577,8 +2633,9 @@ def update(
25772633 local_flags .append ("--rebase" )
25782634 elif merge is True :
25792635 local_flags .append ("--merge" )
2580- if (_filter := kwargs .pop ("_filter" , None )) is not None :
2581- local_flags .append (f"--filter={ _filter } " )
2636+ local_flags .extend (
2637+ _filter_flags (_filter , allow_auto = False , combine_multiple = True )
2638+ )
25822639
25832640 return self .run (
25842641 "update" ,
0 commit comments