rdmtools/trr379_rdmtools/utils.py
2026-03-24 12:48:11 +01:00

59 lines
1.7 KiB
Python

from pathlib import (
Path,
PurePosixPath,
)
import rich_click as click
import tempfile
from datalad_core.runners import (
CommandError,
call_git,
call_git_oneline,
call_git_success,
)
def get_remote_dataset_state(url: str) -> tuple[str, str]:
ref = 'HEAD'
with tempfile.TemporaryDirectory() as wdir:
try:
call_git(['clone', '--bare', '-q', '--depth', '1', url, wdir])
except CommandError as e:
msg = f'Cannot clone repository from {url}'
raise ValueError(msg) from e
dsid = call_git_oneline(
['config', '--blob', f'{ref}:.datalad/config',
'datalad.dataset.id'],
cwd=wdir,
)
gitsha = call_git_oneline(['rev-parse', ref], cwd=wdir)
return dsid, gitsha
def post_failure(txt: str) -> None:
click.echo(click.style(txt, fg='red'))
def post_success(txt: str) -> None:
click.echo(click.style(txt, fg='green'))
def post_noop_result(txt: str) -> None:
click.echo(click.style(txt, fg='yellow'))
def has_staged_changes(path: Path) -> bool:
return not call_git_success(['diff', '--cached', '--quiet'], cwd=path)
# TODO: this needs to support an option to NOT update the submodule state
# to the latest upstream. In many cases (whenever we do not want to write
# to a submodule), we just need the tracked version, not anything after that.
# For robustness alone.
def shallow_clone_submodule(path: Path, subm_rpath: PurePosixPath) -> None:
call_git(
# --rebase takes care of not having a detached HEAD
['submodule', 'update', '--remote', '--no-fetch', '--init',
'--depth', '1', '--', str(subm_rpath)],
cwd=path,
)