55import os .path as osp
66import pathlib
77import re
8+ import shlex
89import sys
910from pathlib import Path
1011from unittest import mock , skip
@@ -71,6 +72,29 @@ def test_clone_clears_ambient_source_storage_environment(tmp_path, clone_method)
7172 source .close ()
7273
7374
75+ @pytest .mark .parametrize ("clone_method" , ["clone" , "clone_from" ])
76+ @pytest .mark .parametrize ("option" , ["reference" , "reference-if-able" , "reference-if-ab" ])
77+ @pytest .mark .parametrize ("option_style" , ["kwargs" , "multi_equals" , "multi_split" ])
78+ def test_clone_reference_options_allowed (tmp_path , clone_method , option , option_style ):
79+ with Repo (tmp_path / "source" ) as source , Repo (tmp_path / "reference repo" ) as reference :
80+ if option_style == "kwargs" :
81+ options = {option .replace ("-" , "_" ): reference .git_dir }
82+ else :
83+ separator = "=" if option_style == "multi_equals" else " "
84+ options = {"multi_options" : [f"--{ option } { separator } { shlex .quote (reference .git_dir )} " ]}
85+ destination = tmp_path / "clone"
86+ options .update (allow_unsafe_options = True , no_local = True )
87+ if clone_method == "clone" :
88+ cloned = source .clone (destination , ** options )
89+ else :
90+ cloned = Repo .clone_from (source .git_dir , destination , ** options )
91+ with cloned :
92+ assert cloned .head .commit == source .head .commit
93+ assert cloned .head .commit .tree ["file" ].data_stream .read () == b"content\n "
94+ alternates = Path (cloned .git_dir , "objects" , "info" , "alternates" ).read_text ().strip ()
95+ assert Path (alternates ).resolve () == Path (reference .git_dir , "objects" ).resolve ()
96+
97+
7498class TestClone (TestBase ):
7599 @with_rw_directory (script = "clone.sh" , args = ("nonempty" ,))
76100 def test_checkout_in_non_empty_dir (self , rw_dir ):
@@ -184,6 +208,10 @@ def test_clone_unsafe_options(self, rw_repo):
184208 f"--template={ tmp_dir } " ,
185209 f"--bundle-uri=file://{ tmp_dir } " ,
186210 f"--separate-git-dir={ tmp_dir / 'git-dir' } " ,
211+ f"--reference={ tmp_dir } " ,
212+ f"--reference-if-able={ tmp_dir } " ,
213+ f"--reference-if-ab={ tmp_dir } " ,
214+ f"--ref={ tmp_dir } " ,
187215 ]
188216 for unsafe_option in unsafe_options :
189217 with self .assertRaises (UnsafeOptionError ):
@@ -201,6 +229,10 @@ def test_clone_unsafe_options(self, rw_repo):
201229 {"template" : tmp_dir },
202230 {"bundle_uri" : f"file://{ tmp_dir } " },
203231 {"separate_git_dir" : tmp_dir / "git-dir" },
232+ {"reference" : tmp_dir },
233+ {"reference_if_able" : tmp_dir },
234+ {"reference_if_ab" : tmp_dir },
235+ {"ref" : tmp_dir },
204236 ]
205237 for unsafe_option in unsafe_options :
206238 with self .assertRaises (UnsafeOptionError ):
@@ -316,6 +348,10 @@ def test_clone_from_unsafe_options(self, rw_repo):
316348 "-cprotocol.ext.allow=always" ,
317349 "-vcprotocol.ext.allow=always" ,
318350 f"--separate-git-dir={ tmp_dir / 'git-dir' } " ,
351+ f"--reference={ tmp_dir } " ,
352+ f"--reference-if-able={ tmp_dir } " ,
353+ f"--reference-if-ab={ tmp_dir } " ,
354+ f"--ref={ tmp_dir } " ,
319355 ]
320356 for unsafe_option in unsafe_options :
321357 with self .assertRaises (UnsafeOptionError ):
@@ -329,6 +365,10 @@ def test_clone_from_unsafe_options(self, rw_repo):
329365 {"config" : "protocol.ext.allow=always" },
330366 {"c" : "protocol.ext.allow=always" },
331367 {"separate_git_dir" : tmp_dir / "git-dir" },
368+ {"reference" : tmp_dir },
369+ {"reference_if_able" : tmp_dir },
370+ {"reference_if_ab" : tmp_dir },
371+ {"ref" : tmp_dir },
332372 ]
333373 for unsafe_option in unsafe_options :
334374 with self .assertRaises (UnsafeOptionError ):
0 commit comments