datalad-core/datalad_core/clone/clone.py
Adina Wagner 402a26e2b3 fix: allow multi-line output for show-ref
'show-ref git-annex' can return more than a single line of output, e.g.
when a manual sync has been performed, and 'refs/heads/synced/git-annex'
is created in response in addition to 'refs/heads/git-annex'.

This fix calls show-ref with a function that does not raise
an AssertionError when receiving more than one line of output.
2026-03-23 12:01:42 +01:00

120 lines
3.5 KiB
Python

import enum
from pathlib import Path
from datasalad.progress import ProgressHandler
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
class AnnexInitMode(enum.StrEnum):
OFF = enum.auto()
NOENABLEREMOTE = enum.auto()
FULL = enum.auto()
def clone(
repository: 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,
):
"""Clone a repository/dataset into a new directory"""
# 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(repository), 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:') :]))
clone_repo(
repository,
dest,
progress_handler=progress_handler,
**clone_repo_kwargs, # type: ignore
)
if annex_init == AnnexInitMode.OFF:
# all below is about annex initialization
return
# 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', repository, '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', repository, 'git-annex:git-annex'],
cwd=dest,
)
if not has_annex:
# nothing more to do or try
return
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 '{directory}'",
)