@@ -152,9 +152,10 @@ def test_install_pip_requirements_with_uv(
152152
153153
154154@pytest .mark .parametrize (
155- "env,extra_expected" ,
155+ "args, env,extra_expected" ,
156156 [
157157 pytest .param (
158+ {},
158159 {
159160 "REPOSITORY_TYPE" : "hg" ,
160161 "BASE_REPOSITORY" : "https://hg.mozilla.org/mozilla-central" ,
@@ -165,20 +166,38 @@ def test_install_pip_requirements_with_uv(
165166 {
166167 "base-repo" : "https://hg.mozilla.org/mozilla-unified" ,
167168 },
168- )
169+ id = "hg" ,
170+ ),
171+ pytest .param (
172+ {"myrepo_shallow_clone" : True },
173+ {
174+ "REPOSITORY_TYPE" : "git" ,
175+ "HEAD_REPOSITORY" : "https://github.com/test/repo.git" ,
176+ "HEAD_REV" : "abc123" ,
177+ },
178+ {"shallow-clone" : True },
179+ id = "git_with_shallow_clone" ,
180+ ),
169181 ],
170182)
171- def test_collect_vcs_options (monkeypatch , run_task_mod , env , extra_expected ):
183+ def test_collect_vcs_options (
184+ monkeypatch ,
185+ run_task_mod ,
186+ args ,
187+ env ,
188+ extra_expected ,
189+ ):
172190 name = "myrepo"
173191 checkout = "checkout"
174192
175193 monkeypatch .setattr (os , "environ" , {})
176194 for k , v in env .items ():
177195 monkeypatch .setenv (f"{ name .upper ()} _{ k .upper ()} " , v )
178196
179- args = Namespace ()
180- setattr (args , f"{ name } _checkout" , checkout )
181- setattr (args , f"{ name } _sparse_profile" , False )
197+ args .setdefault (f"{ name } _checkout" , checkout )
198+ args .setdefault (f"{ name } _shallow_clone" , False )
199+ args .setdefault (f"{ name } _sparse_profile" , False )
200+ args = Namespace (** args )
182201
183202 result = run_task_mod .collect_vcs_options (args , name , name )
184203
@@ -194,6 +213,7 @@ def test_collect_vcs_options(monkeypatch, run_task_mod, env, extra_expected):
194213 "head-ref" : env .get ("HEAD_REF" ),
195214 "head-rev" : env .get ("HEAD_REV" ),
196215 "repo-type" : env .get ("REPOSITORY_TYPE" ),
216+ "shallow-clone" : False ,
197217 "ssh-secret-name" : env .get ("SSH_SECRET_NAME" ),
198218 "sparse-profile" : False ,
199219 "store-path" : env .get ("HG_STORE_PATH" ),
@@ -456,6 +476,104 @@ def test_git_checkout_with_commit(
456476 assert current_rev == head_rev
457477
458478
479+ def test_git_checkout_shallow (
480+ mock_stdin ,
481+ run_task_mod ,
482+ mock_git_repo ,
483+ tmp_path ,
484+ ):
485+ destination = tmp_path / "destination"
486+
487+ # Git ignores `--depth` when cloning from local directories, so use file://
488+ # protocol to force shallow clone.
489+ repo_url = f"file://{ mock_git_repo ['path' ]} "
490+ base_rev = mock_git_repo ["main" ][- 1 ]
491+ head_rev = mock_git_repo ["branch" ][- 1 ]
492+
493+ # Use shallow clone with head_ref != head_rev
494+ run_task_mod .git_checkout (
495+ destination_path = str (destination ),
496+ head_repo = repo_url ,
497+ base_repo = repo_url ,
498+ base_rev = base_rev ,
499+ head_ref = "mybranch" ,
500+ head_rev = head_rev ,
501+ ssh_key_file = None ,
502+ ssh_known_hosts_file = None ,
503+ shallow = True ,
504+ )
505+ shallow_file = destination / ".git" / "shallow"
506+ assert shallow_file .exists ()
507+
508+ # Verify we're on the correct commit
509+ final_rev = subprocess .check_output (
510+ ["git" , "rev-parse" , "HEAD" ],
511+ cwd = str (destination ),
512+ universal_newlines = True ,
513+ ).strip ()
514+ assert final_rev == head_rev
515+
516+ # Verify both base_rev and head_rev are available.
517+ for sha in (base_rev , head_rev ):
518+ result = subprocess .run (
519+ ["git" , "cat-file" , "-t" , sha ],
520+ cwd = str (destination ),
521+ capture_output = True ,
522+ text = True ,
523+ )
524+ assert result .returncode == 0 , f"Commit { sha } should be available"
525+ assert result .stdout .strip () == "commit"
526+
527+
528+ def test_git_fetch_shallow (
529+ mock_stdin ,
530+ run_task_mod ,
531+ mock_git_repo ,
532+ tmp_path ,
533+ ):
534+ destination = tmp_path / "destination"
535+
536+ # Git ignores `--depth` when cloning from local directories, so use file://
537+ # protocol to force shallow clone.
538+ repo_url = f"file://{ mock_git_repo ['path' ]} "
539+
540+ run_task_mod .run_command (
541+ b"vcs" ,
542+ [
543+ "git" ,
544+ "clone" ,
545+ "--depth=1" ,
546+ "--no-checkout" ,
547+ repo_url ,
548+ str (destination ),
549+ ],
550+ )
551+ shallow_file = destination / ".git" / "shallow"
552+ assert shallow_file .exists ()
553+
554+ # Verify base_rev doesn't exist yet
555+ base_rev = mock_git_repo ["branch" ][- 1 ]
556+ result = subprocess .run (
557+ ["git" , "cat-file" , "-t" , base_rev ],
558+ cwd = str (destination ),
559+ capture_output = True ,
560+ text = True ,
561+ )
562+ assert result .returncode != 0
563+
564+ run_task_mod .git_fetch (str (destination ), base_rev , remote = repo_url , shallow = True )
565+
566+ # Verify base_rev is now available
567+ result = subprocess .run (
568+ ["git" , "cat-file" , "-t" , base_rev ],
569+ cwd = str (destination ),
570+ capture_output = True ,
571+ text = True ,
572+ )
573+ assert result .returncode == 0
574+ assert result .stdout .strip () == "commit"
575+
576+
459577def test_display_python_version_should_output_python_versions_title (
460578 run_task_mod , capsys
461579):
0 commit comments