datalad-core/datalad_core/git_utils/status.py
Michael Hanke e8e8d7263a
Some checks failed
Codespell / Check for spelling errors (push) Successful in 52s
Codespell / Check for spelling errors (pull_request) Successful in 56s
Ruff / Code linting (push) Successful in 1m1s
Ruff / Code linting (pull_request) Successful in 1m4s
Conventional commits / check-messages (pull_request) Successful in 1m9s
Unit tests (Linux) / check (push) Failing after 1m24s
Unit tests (Linux) / check (pull_request) Failing after 1m28s
Type annotation (project) / check (push) Successful in 1m35s
Type annotation (PR) / check (pull_request) Successful in 1m42s
feat: get_worktree_content()
This is the work horse for populating a worktree with (missing) tracked
content: downloading annex keys and cloning submodules.

This implementation has support for Git (non-literal) pathspecs.

This implementation is also capable of using a global task executor, for
example, to parallelize download operations *across* repositories.
However, this feature is not thoroughly tested. Moreover, there is no
support for multiple git-annex download-jobs *within* a repository, yet.
2026-07-21 05:50:44 +02:00

61 lines
2.4 KiB
Python

from collections.abc import Iterable
from pathlib import (
Path,
PurePosixPath,
)
from datasalad.gitpathspec import GitPathSpecs
from datalad_core.git_utils import types as gt
from datalad_core.runners import iter_git_subproc_zlines
def iter_repo_status(
# TODO: should be worktree
repository: Path,
*,
untracked_files: gt.UntrackedFilesMode | None = None,
ignore_submodules: gt.IgnoreSubmodulesMode | None = None,
pathspecs: GitPathSpecs | None = None,
) -> Iterable[
gt.RepoIgnoredRecord | gt.RepoUntrackedRecord | gt.RepoModificationRecord
]:
"""Front-end for `git status --porcelain=2`
The output of the `status` command is yielded as individual records
(one per path), with dedicated types used for each of the different
record types. The values for ``untracked_files`` and ``ignore_submodules``
are passed to ``--untracked-files`` and ``--ignore-submodules``,
respectively, using the exact same semantics. Constraining the
report to content matching a set of pathspecs is also supported.
"""
cmd = ['status', '-z', '--porcelain=2', '--no-renames']
if untracked_files is not None:
cmd.append(f'--untracked-files={untracked_files}')
if ignore_submodules is not None:
cmd.append(f'--ignore-submodules={ignore_submodules}')
if pathspecs:
cmd.append('--')
cmd.extend(pathspecs.arglist())
for line in iter_git_subproc_zlines(cmd, cwd=repository):
comp = line.split(' ', maxsplit=8)
match comp[0]:
case '!':
yield gt.RepoIgnoredRecord(path=PurePosixPath(comp[1]))
case '?':
yield gt.RepoUntrackedRecord(path=PurePosixPath(comp[1]))
case '1':
yield gt.RepoModificationRecord(
modification=gt.ModificationState.from_str(comp[1]),
submodule_state=gt.SubmoduleChange.from_str(comp[2]),
mode_head=gt.GitObjectMode.from_str(comp[3]),
mode_index=gt.GitObjectMode.from_str(comp[4]),
mode_worktree=gt.GitObjectMode.from_str(comp[5]),
name_head=comp[6],
name_index=comp[7],
path=PurePosixPath(comp[8]),
)
case _:
msg = f'Got unexpected modification report from git-status {line!r}'
raise RuntimeError(msg)