Skip to content

Commit c4095f9

Browse files
committed
fix: use full shallow clone in process_file_updates
use full shallow clone in process_file_updates Signed-off-by: Elena German <elgerman@redhat.com> Assisted-by: Cursor
1 parent e6cb89c commit c4095f9

4 files changed

Lines changed: 76 additions & 71 deletions

File tree

scripts/python/helpers/vcs/git.py

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ def clone(
9696
) -> Path:
9797
"""Clone *clone_url* into *parent_dir*.
9898
99-
When *shallow* is true, perform a blob-filtered shallow clone and optional
100-
sparse checkout of *sparse_dirs* at *revision*.
99+
When *shallow* is true, perform a shallow clone at *revision*. With
100+
*sparse_dirs*, use blob-filtered sparse checkout; otherwise clone the full
101+
tree at depth 1 (legacy bash ``git_clone_and_checkout`` without ``--sparse-dir``).
101102
102103
Creates *parent_dir* when it does not exist yet.
103104
"""
@@ -111,35 +112,48 @@ def clone(
111112
if revision is None:
112113
msg = "revision is required for a shallow clone"
113114
raise ValueError(msg)
114-
if not sparse_dirs:
115-
msg = "sparse_dirs is required for a shallow clone"
116-
raise ValueError(msg)
117-
_run_git_cmd(
118-
[
119-
"git",
120-
"clone",
121-
"--filter=blob:none",
122-
"--no-checkout",
123-
"--depth",
124-
"1",
125-
"--branch",
126-
revision,
127-
clone_url,
128-
str(repo_dir),
129-
],
130-
cwd=parent_dir,
131-
stderr_path=stderr_path,
132-
)
133-
_run_git_cmd(
134-
["git", "sparse-checkout", "set", *sparse_dirs],
135-
cwd=repo_dir,
136-
stderr_path=stderr_path,
137-
)
138-
_run_git_cmd(
139-
["git", "checkout", revision],
140-
cwd=repo_dir,
141-
stderr_path=stderr_path,
142-
)
115+
if sparse_dirs:
116+
_run_git_cmd(
117+
[
118+
"git",
119+
"clone",
120+
"--filter=blob:none",
121+
"--no-checkout",
122+
"--depth",
123+
"1",
124+
"--branch",
125+
revision,
126+
clone_url,
127+
str(repo_dir),
128+
],
129+
cwd=parent_dir,
130+
stderr_path=stderr_path,
131+
)
132+
_run_git_cmd(
133+
["git", "sparse-checkout", "set", *sparse_dirs],
134+
cwd=repo_dir,
135+
stderr_path=stderr_path,
136+
)
137+
_run_git_cmd(
138+
["git", "checkout", revision],
139+
cwd=repo_dir,
140+
stderr_path=stderr_path,
141+
)
142+
else:
143+
_run_git_cmd(
144+
[
145+
"git",
146+
"clone",
147+
"--depth",
148+
"1",
149+
"--branch",
150+
revision,
151+
clone_url,
152+
str(repo_dir),
153+
],
154+
cwd=parent_dir,
155+
stderr_path=stderr_path,
156+
)
143157
else:
144158
_run_git_cmd(
145159
["git", "clone", clone_url, str(repo_dir)],

scripts/python/helpers/vcs/test_git.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,38 @@ def test_clone_shallow_sparse(tmp_path: Path) -> None:
144144
assert run_cmd.call_count == 3
145145

146146

147+
def test_clone_shallow_full(tmp_path: Path) -> None:
148+
"""Shallow clone without sparse dirs checks out the full tree at depth 1."""
149+
repo_dir = tmp_path / "proj"
150+
with mock.patch.object(git, "_run_git_cmd") as run_cmd:
151+
out = git.clone(
152+
tmp_path,
153+
"https://x/proj.git",
154+
directory_name="proj",
155+
revision="main",
156+
shallow=True,
157+
)
158+
assert out == repo_dir
159+
run_cmd.assert_called_once_with(
160+
[
161+
"git",
162+
"clone",
163+
"--depth",
164+
"1",
165+
"--branch",
166+
"main",
167+
"https://x/proj.git",
168+
str(repo_dir),
169+
],
170+
cwd=tmp_path,
171+
stderr_path=None,
172+
)
173+
174+
147175
def test_clone_shallow_requires_revision(tmp_path: Path) -> None:
148176
"""Shallow clones require a revision."""
149177
with pytest.raises(ValueError, match="revision"):
150-
git.clone(tmp_path, "https://x/p.git", shallow=True, sparse_dirs=["a"])
178+
git.clone(tmp_path, "https://x/p.git", shallow=True)
151179

152180

153181
def test_clone_appends_stderr(tmp_path: Path) -> None:

scripts/python/tasks/internal/process_file_updates.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -300,21 +300,6 @@ def write_paths_manifest(paths_json: str, temp_dir: Path) -> tuple[Path, list[di
300300
return update_paths_file, json.loads(paths_json)
301301

302302

303-
def sparse_dirs_from_paths(paths_data: list[dict[str, Any]]) -> list[str]:
304-
"""Return sparse-checkout paths covering every ``path`` entry in *paths_data*."""
305-
sparse_dirs: set[str] = set()
306-
for entry in paths_data:
307-
entry_path = entry.get("path")
308-
if not entry_path:
309-
continue
310-
path = Path(str(entry_path))
311-
if path.parent == Path("."):
312-
sparse_dirs.add(path.as_posix())
313-
else:
314-
sparse_dirs.add(path.parent.as_posix())
315-
return sorted(sparse_dirs)
316-
317-
318303
def prepare_repository(
319304
repo: str,
320305
revision: str,
@@ -324,8 +309,7 @@ def prepare_repository(
324309
) -> Path:
325310
"""Clone *repo* at *revision*, rebase on *upstream_repo*, and return the repo cwd."""
326311
logger.info("=== UPDATING %s ON BRANCH %s ===", repo, revision)
327-
sparse_dirs = sparse_dirs_from_paths(paths_data)
328-
if not sparse_dirs:
312+
if not any(entry.get("path") for entry in paths_data):
329313
raise tekton.CheckStepError(
330314
"cloning repository",
331315
ValueError("paths JSON must include at least one path entry"),
@@ -334,7 +318,6 @@ def prepare_repository(
334318
temp_dir,
335319
repo,
336320
revision=revision,
337-
sparse_dirs=sparse_dirs,
338321
shallow=True,
339322
)
340323
vcs_git.rebase_onto_remote(

scripts/python/tasks/internal/test_process_file_updates.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -470,27 +470,8 @@ def test_git_functions_init_configures_identity() -> None:
470470
cfg.assert_called_once_with("Author", "a@example.com")
471471

472472

473-
def test_sparse_dirs_from_paths() -> None:
474-
"""Sparse checkout paths cover parent dirs and root-level files."""
475-
paths = [
476-
{"path": "deploy/overlays/prod/kustomization.yaml"},
477-
{"path": "version.yaml"},
478-
{"path": "deploy/base/kustomization.yaml"},
479-
]
480-
assert process_file_updates.sparse_dirs_from_paths(paths) == [
481-
"deploy/base",
482-
"deploy/overlays/prod",
483-
"version.yaml",
484-
]
485-
486-
487-
def test_sparse_dirs_from_paths_skips_missing_path() -> None:
488-
"""Entries without a path are ignored."""
489-
assert process_file_updates.sparse_dirs_from_paths([{"seed": "x"}]) == []
490-
491-
492473
def test_prepare_repository(tmp_path: Path) -> None:
493-
"""Repository is sparse-cloned and rebased on upstream via ``vcs.git``."""
474+
"""Repository is shallow-cloned and rebased on upstream via ``vcs.git``."""
494475
repo_cwd = tmp_path / "cloned"
495476
repo_cwd.mkdir()
496477
paths = [{"path": "deploy/image.yaml", "replacements": []}]
@@ -514,7 +495,6 @@ def test_prepare_repository(tmp_path: Path) -> None:
514495
tmp_path,
515496
"https://gitlab.com/org/repo.git",
516497
revision="main",
517-
sparse_dirs=["deploy"],
518498
shallow=True,
519499
)
520500
rebase.assert_called_once()

0 commit comments

Comments
 (0)