Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion gitman/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from .shell import call, pwd


def sanitize_sparse_paths(sparse_paths):
"""Strip trailing glob patterns for cone mode (e.g. 'src/*' -> 'src')."""
return [p.rstrip("/*") if p.endswith("/*") else p for p in sparse_paths]


def git(*args, **kwargs):
return call("git", *args, **kwargs)

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

git("-C", normpath, "sparse-checkout", "init", "--cone")
git("-C", normpath, "sparse-checkout", "set", *sparse_paths)
git(
"-C",
normpath,
"sparse-checkout",
"set",
*sanitize_sparse_paths(sparse_paths)
)
git("-C", normpath, "fetch", "origin")
git("-C", normpath, "checkout", rev)
elif settings.CACHE_DISABLE:
Expand Down Expand Up @@ -245,6 +256,11 @@ def am(patch, _skip=False):
raise ShellError from e


def apply_sparse_checkout(sparse_paths):
"""Re-apply sparse-checkout paths to an existing working tree."""
git("sparse-checkout", "set", *sanitize_sparse_paths(sparse_paths))


def update(
type, repo, path, *, clean=True, fetch=False, rev=None
): # pylint: disable=redefined-outer-name,unused-argument
Expand Down
4 changes: 4 additions & 0 deletions gitman/models/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def update_files(
if fetch or git.is_fetch_required(self.type, self.rev):
git.fetch(self.type, self.repo, self.name, rev=self.rev)

# Re-apply sparse-checkout paths in case they changed since initial clone
if self.sparse_paths and self.sparse_paths[0]:
git.apply_sparse_checkout(self.sparse_paths)

# Update the working tree to the desired revision
git.update(
self.type, self.repo, self.name, fetch=fetch, clean=clean, rev=self.rev
Expand Down
42 changes: 42 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,48 @@ def it_contains_only_the_sparse_paths(config):
expect(dir_listing).contains("src")
expect(len(dir_listing) == 1)

def it_updates_sparse_paths_on_subsequent_install(config):
config.datafile.text = strip("""
location: deps
sources:
- name: gitman_1
type: git
params:
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
- src/*
rev: ddbe17ef173538d1fda29bd99a14bab3c5d86e78
links:
-
scripts:
-
""")
config.datafile.load()
expect(gitman.install(depth=1, force=True)) == True
dir_listing = os.listdir(os.path.join(config.location, "gitman_1"))
expect(dir_listing).contains("src")

# Change sparse_paths and re-install to verify update takes effect
config.datafile.text = strip("""
location: deps
sources:
- name: gitman_1
type: git
params:
repo: https://github.com/jacebrowning/gitman-demo
sparse_paths:
- nonexistent_dir/*
rev: ddbe17ef173538d1fda29bd99a14bab3c5d86e78
links:
-
scripts:
-
""")
config.datafile.load()
expect(gitman.install(depth=1, force=True)) == True
dir_listing = os.listdir(os.path.join(config.location, "gitman_1"))
expect("src" not in dir_listing)

def describe_mixed_names():
@pytest.fixture
def config_with_group(config):
Expand Down
Loading