Skip to content

Commit 2957dd6

Browse files
authored
Merge pull request #439 from fhamdi-bdai/fhamdi-bdai/respect-sparse-path-updates
Re-apply sparse-checkout paths on subsequent installs
2 parents 31bd8be + 0e3f8d0 commit 2957dd6

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

gitman/git.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
from .shell import call, pwd
1313

1414

15+
def sanitize_sparse_paths(sparse_paths):
16+
"""Strip trailing glob patterns for cone mode (e.g. 'src/*' -> 'src')."""
17+
return [p.rstrip("/*") if p.endswith("/*") else p for p in sparse_paths]
18+
19+
1520
def git(*args, **kwargs):
1621
return call("git", *args, **kwargs)
1722

@@ -69,7 +74,13 @@ def clone(
6974
fd.write("%s/objects" % sparse_paths_repo)
7075

7176
git("-C", normpath, "sparse-checkout", "init", "--cone")
72-
git("-C", normpath, "sparse-checkout", "set", *sparse_paths)
77+
git(
78+
"-C",
79+
normpath,
80+
"sparse-checkout",
81+
"set",
82+
*sanitize_sparse_paths(sparse_paths)
83+
)
7384
git("-C", normpath, "fetch", "origin")
7485
git("-C", normpath, "checkout", rev)
7586
elif settings.CACHE_DISABLE:
@@ -245,6 +256,11 @@ def am(patch, _skip=False):
245256
raise ShellError from e
246257

247258

259+
def apply_sparse_checkout(sparse_paths):
260+
"""Re-apply sparse-checkout paths to an existing working tree."""
261+
git("sparse-checkout", "set", *sanitize_sparse_paths(sparse_paths))
262+
263+
248264
def update(
249265
type, repo, path, *, clean=True, fetch=False, rev=None
250266
): # pylint: disable=redefined-outer-name,unused-argument

gitman/models/source.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ def update_files(
204204
if fetch or git.is_fetch_required(self.type, self.rev):
205205
git.fetch(self.type, self.repo, self.name, rev=self.rev)
206206

207+
# Re-apply sparse-checkout paths in case they changed since initial clone
208+
if self.sparse_paths and self.sparse_paths[0]:
209+
git.apply_sparse_checkout(self.sparse_paths)
210+
207211
# Update the working tree to the desired revision
208212
git.update(
209213
self.type, self.repo, self.name, fetch=fetch, clean=clean, rev=self.rev

tests/test_api.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,48 @@ def it_contains_only_the_sparse_paths(config):
391391
expect(dir_listing).contains("src")
392392
expect(len(dir_listing) == 1)
393393

394+
def it_updates_sparse_paths_on_subsequent_install(config):
395+
config.datafile.text = strip("""
396+
location: deps
397+
sources:
398+
- name: gitman_1
399+
type: git
400+
params:
401+
repo: https://github.com/jacebrowning/gitman-demo
402+
sparse_paths:
403+
- src/*
404+
rev: ddbe17ef173538d1fda29bd99a14bab3c5d86e78
405+
links:
406+
-
407+
scripts:
408+
-
409+
""")
410+
config.datafile.load()
411+
expect(gitman.install(depth=1, force=True)) == True
412+
dir_listing = os.listdir(os.path.join(config.location, "gitman_1"))
413+
expect(dir_listing).contains("src")
414+
415+
# Change sparse_paths and re-install to verify update takes effect
416+
config.datafile.text = strip("""
417+
location: deps
418+
sources:
419+
- name: gitman_1
420+
type: git
421+
params:
422+
repo: https://github.com/jacebrowning/gitman-demo
423+
sparse_paths:
424+
- nonexistent_dir/*
425+
rev: ddbe17ef173538d1fda29bd99a14bab3c5d86e78
426+
links:
427+
-
428+
scripts:
429+
-
430+
""")
431+
config.datafile.load()
432+
expect(gitman.install(depth=1, force=True)) == True
433+
dir_listing = os.listdir(os.path.join(config.location, "gitman_1"))
434+
expect("src" not in dir_listing)
435+
394436
def describe_mixed_names():
395437
@pytest.fixture
396438
def config_with_group(config):

0 commit comments

Comments
 (0)