152 lines
5.2 KiB
Python
152 lines
5.2 KiB
Python
from pathlib import Path
|
|
|
|
from datasalad.progress import ProgressHandler
|
|
|
|
from datalad_core.annex_utils import AnnexInitMode
|
|
from datalad_core.repo import (
|
|
Repo,
|
|
Worktree,
|
|
)
|
|
from datalad_core.runners import (
|
|
CommandError,
|
|
call_git,
|
|
call_git_lines,
|
|
call_git_success,
|
|
)
|
|
|
|
# relative imports, the top-level module is not fully imported
|
|
# at this point
|
|
from .clone_repo import clone_repo # noqa TID252
|
|
from .utils import git_url_basename # noqa TID252
|
|
|
|
|
|
def clone_annexrepo(
|
|
source: str,
|
|
directory: Path | None = None,
|
|
*,
|
|
annex_init: AnnexInitMode = AnnexInitMode.FULL,
|
|
bare: bool = False,
|
|
branch: str | None = None,
|
|
description: str | None = None,
|
|
private: bool = False,
|
|
progress_handler: ProgressHandler | None = None,
|
|
quiet: bool = False,
|
|
shallow: str | None = None,
|
|
) -> Repo | Worktree:
|
|
"""Clone a repository/dataset into a new/empty directory
|
|
|
|
This function wraps :func:`clone_repo` for cloning a repository.
|
|
After a successful clone a local annex is initialized, if one
|
|
is detected for the repository.
|
|
|
|
The parameters ``source``, ``bare``, ``branch``, ``quiet``, and
|
|
``progress_handler`` are passed to :func:`clone_repo` as-is. If
|
|
``destination`` is ``None``, a target directory name is derived from
|
|
``source``, in the same way Git does.
|
|
|
|
The post-clone initialization of a local annex is controlled by
|
|
``annex_init``. With :data:`AnnexInitMode.FULL` (default) a standard
|
|
initialization is done, if an annex is detected. Initialization is skipped
|
|
when no ``git-annex`` branch is present on the remote. The mode
|
|
:data:`AnnexInitMode.NOENABLEREMOTE` disables the auto-enabling of special
|
|
remotes that are configured as such. Lastly, :data:`AnnexInitMode.OFF`
|
|
skips an annex initialization entirely, even if the remote has a
|
|
``git-annex`` branch.
|
|
|
|
If a local annex is initialized, ``description`` will be used to help
|
|
identify it (or git-annex assigns an automatic description with ``None``).
|
|
With ``private`` enabled, the local repository annex will not be recorded
|
|
in the git-annex branch (see `private annexes
|
|
<https://git-annex.branchable.com/tips/cloning_a_repository_privately>`_).
|
|
|
|
If ``shallow`` is given, a shallow clone will be attempted. At present,
|
|
the only supported mode is "depth" (see ``git clone --depth``). Legitimate
|
|
values for ``shallow`` must match ``"depth:<int>"``, where ``<int>`` is an
|
|
integer that is passed to ``git clone --depth``. For any value of
|
|
``shallow``, except ``None``, any ``git-annex`` branch on the remote will
|
|
be fetched with a depth of 1.
|
|
|
|
"""
|
|
|
|
# when we can clone --bare, set the switch here
|
|
# conversion to Path() should be OK, function gives
|
|
# just a directory name (not a (hierarchical) path)
|
|
dest = (
|
|
Path(git_url_basename(str(source), is_bare=bare))
|
|
if directory is None
|
|
else directory
|
|
)
|
|
|
|
clone_repo_kwargs = {
|
|
'bare': bare,
|
|
'branch': branch,
|
|
'quiet': quiet,
|
|
}
|
|
if shallow and shallow.startswith('depth:'):
|
|
clone_repo_kwargs['depth'] = str(int(shallow[len('depth:') :]))
|
|
cloned = clone_repo(
|
|
source,
|
|
dest,
|
|
progress_handler=progress_handler,
|
|
**clone_repo_kwargs, # type: ignore
|
|
)
|
|
|
|
if annex_init == AnnexInitMode.OFF:
|
|
# all below is about annex initialization
|
|
return cloned
|
|
|
|
# will print one or more lines if there is any ref named git-annex
|
|
# (local or remote)
|
|
try:
|
|
has_annex = bool(
|
|
call_git_lines(
|
|
['show-ref', 'git-annex'],
|
|
cwd=dest,
|
|
)
|
|
)
|
|
except CommandError:
|
|
has_annex = False
|
|
if shallow and not has_annex:
|
|
# double check that we really have no annex, and the branch wasn't
|
|
# just omitted from the original clone.
|
|
# not going straight for a fetch, because we would not be able to
|
|
# distinguish failure of fetching due to network error, from failure
|
|
# due to no-annex
|
|
has_annex = call_git_success(
|
|
['ls-remote', '-q', '--exit-code', source, 'git-annex'],
|
|
capture_output=True,
|
|
cwd=dest,
|
|
)
|
|
if has_annex:
|
|
# get the branch tip. we should only need that, and we are in
|
|
# shallow mode anyways
|
|
call_git(
|
|
['fetch', '-q', '--depth', '1', source, 'git-annex:git-annex'],
|
|
cwd=dest,
|
|
)
|
|
|
|
if not has_annex:
|
|
# nothing more to do or try
|
|
return cloned
|
|
|
|
if progress_handler:
|
|
init_task = progress_handler.register_task(
|
|
'Initialize repository',
|
|
message=f"Initializing repository annex at '{dest}' ...",
|
|
expect_nonprogress_output=True,
|
|
)
|
|
if private:
|
|
call_git(['config', '--add', 'annex.private', 'true'], cwd=dest)
|
|
|
|
init_cmd = ['annex', 'init', '--quiet']
|
|
if annex_init == AnnexInitMode.NOENABLEREMOTE:
|
|
init_cmd.append('--no-autoenable')
|
|
if description:
|
|
init_cmd.append(description)
|
|
call_git(init_cmd, cwd=dest)
|
|
if progress_handler:
|
|
progress_handler.remove_completed_task(
|
|
init_task,
|
|
message=f"Initialized repository annex at '{dest}'",
|
|
)
|
|
return cloned
|