Skip to content

Commit

Permalink
Add support and tests for clone --recurse-submodules
Browse files Browse the repository at this point in the history
Including tests for clone --recursive.
  • Loading branch information
erijo committed Jan 19, 2025
1 parent a86f238 commit f381bff
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
64 changes: 59 additions & 5 deletions test/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,38 @@ def test_clone(runner, paths, yadm_cmd, repo_config, ds1, good_remote, repo_exis
assert old_repo.exists()


@pytest.mark.usefixtures("remote_with_submodules")
@pytest.mark.parametrize("action", ["recursive", "recurse", "specific"])
def test_clone_submodules(runner, paths, yadm_cmd, repo_config, action):
"""Test clone operation with submodules"""

# clear out the work path
paths.work.remove()
paths.work.mkdir()

env = os.environ.copy()
env["GIT_CONFIG_COUNT"] = "1"
env["GIT_CONFIG_KEY_0"] = "protocol.file.allow"
env["GIT_CONFIG_VALUE_0"] = "always"

args = ["clone", "-w", paths.work]
if action == "recursive":
args += ["--recursive"]
elif action == "recurse":
args += ["--recurse-submodules"]
elif action == "specific":
args += ["--recurse-submodules=a", "--recurse-submodules=d1/c"]
args += [f"file://{paths.remote}"]
run = runner(command=yadm_cmd(*args), env=env)
assert successful_clone(run, paths, repo_config)

for path in ("a", "b", "d1/c"):
if action != "specific" or path != "b":
assert paths.work.join(path).join(".git").exists()
else:
assert not paths.work.join(path).join(".git").exists()


@pytest.mark.usefixtures("remote")
@pytest.mark.parametrize(
"bs_exists, bs_param, answer",
Expand Down Expand Up @@ -305,16 +337,38 @@ def remote(paths, ds1_repo_copy):
"""Function scoped remote (based on ds1)"""
# pylint: disable=unused-argument
# This is ignored because
# @pytest.mark.usefixtures('ds1_remote_copy')
# @pytest.mark.usefixtures('ds1_repo_copy')
# cannot be applied to another fixture.
paths.remote.remove()
paths.repo.move(paths.remote)


def test_no_repo(
runner,
yadm_cmd,
):
@pytest.fixture()
def remote_with_submodules(tmpdir_factory, runner, paths, remote, ds1_work_copy):
"""Function scoped remote with submodules (based on ds1)"""
# pylint: disable=unused-argument
# This is ignored because
# @pytest.mark.usefixtures('remote', 'ds1_work_copy')
# cannot be applied to another fixture.
submodule = tmpdir_factory.mktemp("submodule")
paths.remote.copy(submodule)

env = os.environ.copy()
env["GIT_DIR"] = str(paths.remote)

for path in ("a", "b", "d1/c"):
run = runner(
["git", "-C", paths.work, "-c", "protocol.file.allow=always", "submodule", "add", submodule, path],
env=env,
report=False,
)
assert run.success

run = runner(["git", "-C", paths.work, "commit", "-m", '"Add submodules"'], env=env, report=False)
assert run.success


def test_no_repo(runner, yadm_cmd):
"""Test cloning without specifying a repo"""
run = runner(command=yadm_cmd("clone", "-f"))
assert run.failure
Expand Down
15 changes: 9 additions & 6 deletions yadm
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ function clone() {
DO_BOOTSTRAP=1
local -a args
local -i do_checkout=1
local -i do_submodules=0
local -a submodules
while [[ $# -gt 0 ]]; do
case "$1" in
--bootstrap) # force bootstrap, without prompt
Expand All @@ -769,10 +769,13 @@ function clone() {
-n | --no-checkout)
do_checkout=0
;;
--recursive)
do_submodules=1
--recursive | --recurse-submodules)
submodules+=(":/")
;;
--recurse-submodules=*)
submodules+=(":/${1#*=}")
;;
--bare | --mirror | --recurse-submodules* | --separate-git-dir=*)
--bare | --mirror | --separate-git-dir=*)
# ignore arguments without separate parameter
;;
--separate-git-dir)
Expand Down Expand Up @@ -839,8 +842,8 @@ function clone() {
"$GIT_PROGRAM" checkout -- ":/$file"
done

if [[ $do_submodules -ne 0 ]]; then
"$GIT_PROGRAM" submodule update --init --recursive
if [ ${#submodules[@]} -gt 0 ]; then
"$GIT_PROGRAM" submodule update --init --recursive -- "${submodules[@]}"
fi

if [ -n "$("$GIT_PROGRAM" ls-files --modified)" ]; then
Expand Down

0 comments on commit f381bff

Please sign in to comment.