Watch
1
0
Fork
You've already forked git-lad
0
forked from datalad/git-lad
git-lad/git_lad/commands/get.py

172 lines
4.8 KiB
Python

import enum
from pathlib import (
Path,
PurePath,
PurePosixPath,
)
from typing import cast
import rich_click as click
from datalad_core.get import (
WorktreeContentType,
get_worktree_content,
)
from datalad_core.repo import Worktree
from datasalad.gitpathspec import GitPathSpecs
from git_lad.cmd_resources import (
CmdResources,
std_option,
)
class GetContentTypeMode(enum.Enum):
FILES = WorktreeContentType.FILES
SUBMODULES = WorktreeContentType.SUBMODULES
ALL = WorktreeContentType.FILES | WorktreeContentType.SUBMODULES
@click.command()
@click.argument(
'paths',
nargs=-1,
type=PurePath,
)
@std_option(
'annex-description',
names=('--annex-description',),
append_help="""\
If given, the same description will be assigned to all obtained annex
repository clones.""",
)
@click.option(
'--from-remote',
'from_remote',
metavar='NAME',
help="""\
Name of the (special) remote to download from. With submodule recursion,
the same remote name will be used for all submodules.
""",
)
@std_option(
'private-annex',
names=('--private-annex',),
default=None,
append_help="""\
By default, submodules inherit a private annex configuration
from their parents.""",
)
# @std_option('quiet')
@std_option('recurse_submodules')
@std_option(
'shallow',
names=('--shallow-clone',),
default='auto',
append_help="""\
By default, submodules inherit a shallow setup from their parents.
""",
)
@click.option(
'--type',
'content_type',
type=click.Choice(GetContentTypeMode, case_sensitive=False),
default='files',
help="""\
Work tree content types to consider obtaining. With 'files', only
files in existing repositories are downloaded and no additional
submodules are retrieved, even when they match a given path parameter.
With 'submodules', only submodules are cloned, and no file content is
downloaded. With 'all', any matching submodules are cloned, and file
content for any match files is obtained.
""",
)
@click.pass_obj
def get(
res: CmdResources,
paths,
*,
content_type: WorktreeContentType = WorktreeContentType.FILES,
description: str | None = None,
from_remote: str | None,
private: bool | None = False,
quiet: bool = False,
recurse_submodules: bool | int = False,
shallow: str | None = None,
):
"""Obtain missing worktree content
By default, this command behaves similar to 'git-annex get', and will
obtain any file content not present locally already. Optionally, the
operation can be constrained to a set of paths within the worktree.
The key difference to 'git-annex get' is the ability to also act on
worktree content in submodules, at any level of submodule nesting.
When a <PATH> points into a locally present submodule, any associated
missing file content is obtained as if the entire worktree would belong to
a single repository. By setting --type to 'all' or 'submodule', any missing
submodule (matching a path) is automatically obtained too.
When submodules are obtained, they can, optionally, be cloned shallow and/or
put into git-annex's private mode. By default, this configuration is inherited
from their respective parent repositories.
When no worktree content matched any given path specifications, the command
exits with an error.
\n
Examples:
\n
Get all missing file content in the worktree, ignore submodules:
\b
> git lad get
\n
Get all missing content for a file, obtain submodules as needed:
\b
> git lad get --type all <path>
\n
Get all missing file content, including those in any present submodule:
\b
> git lad get --recurse-submodules
\n
Get any (missing) immediate submodule of the top-level worktree:
\b
> git lad get --type submodules
\n
"""
cwd = Path.cwd()
try:
wt = Worktree.from_path(cwd)
except ValueError as e:
msg = 'Not a Git repository'
raise RuntimeError(msg) from e
# to auto-adjust pathspecs when CWD != worktree.path
cwd_prefix = PurePosixPath(cwd.relative_to(wt.path))
pathspecs = (
GitPathSpecs(
# incoming is a platform path, and we normalize to POSIX for sanity
# beyond this gate
[str(cwd_prefix / PurePosixPath(p)) for p in paths]
)
if paths
else None
)
get_worktree_content(
wt,
pathspecs,
content_type=cast('WorktreeContentType', content_type.value),
from_remote=from_remote,
progress_handler=None if quiet else res.progress_handler,
task_executor=res.task_executor,
# TODO: is this desired?
quiet=quiet,
recurse_submodules=recurse_submodules or False,
annex_init_description=description,
annex_init_private=private,
clone_shallow=shallow,
)