Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let iter_gitworktree(fp=True) report on symlinked content #555

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions datalad_next/iter_collections/gitworktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,14 @@
# report on a pending item, this is not a "higher-stage"
# report by ls-files
item = _get_item(path, link_target, fp, *pending_item)
if fp and item.type == FileSystemItemType.file:
with (Path(path) / item.name).open('rb') as fp:
item.fp = fp
yield item
else:
fp_src = _get_fp_src(fp, path, item)
if fp_src is None:
# nothing to open
yield item
else:
with fp_src.open('rb') as active_fp:
item.fp = active_fp
yield item

if ipath is None:
# this is the trailing `None` record. we are done here
Expand Down Expand Up @@ -328,3 +330,29 @@
keep_ends=False,
)
)


def _get_fp_src(
fp: bool,
basepath: Path,
item: GitWorktreeItem | GitWorktreeFileSystemItem,
) -> Path | None:
if not fp:
# no file pointer request, we are done
return None

# if we get here, this is about file pointers...
fp_src = None
if item.type in (FileSystemItemType.file,
FileSystemItemType.symlink):
fp_src = item.name
if fp_src is None:
# nothing to open
return None

Check warning on line 351 in datalad_next/iter_collections/gitworktree.py

View check run for this annotation

Codecov / codecov/patch

datalad_next/iter_collections/gitworktree.py#L351

Added line #L351 was not covered by tests

fp_src_fullpath = basepath / fp_src
if not fp_src_fullpath.exists():
# nothing there to open (would resolve through a symlink)
return None

return fp_src_fullpath
61 changes: 61 additions & 0 deletions datalad_next/iter_collections/tests/test_itergitworktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import pytest

from datalad_next.utils import check_symlink_capability

from datalad_next.tests.utils import rmtree

from ..gitworktree import (
Expand Down Expand Up @@ -170,3 +172,62 @@ def test_iter_gitworktree_deadsymlinks(existing_dataset, no_result_rendering):
# it may take a different form, hence not checking for type
assert len(all_items) == 1
assert all_items[0].name == PurePath('file1')


def prep_fp_tester(ds):
# we expect to process an exact number of files below
# 3 annexed files, 1 untracked, 1 in git,
# and possibly 1 symlink in git, 1 symlink untracked
# we count them up on creation, and then down on test
fcount = 0

content_tmpl = 'content: #ö file_{}'
for i in ('annex1', 'annex2', 'annex3'):
(ds.pathobj / f'file_{i}').write_text(
content_tmpl.format(i), encoding='utf-8')
fcount += 1
ds.save()
ds.drop(
ds.pathobj / 'file_annex1',
reckless='availability',
)
# and also add a file to git directly and a have one untracked too
for i in ('untracked', 'ingit'):
(ds.pathobj / f'file_{i}').write_text(
content_tmpl.format(i), encoding='utf-8')
fcount += 1
ds.save('file_ingit', to_git=True)
# and add symlinks (untracked and in git)
if check_symlink_capability(
ds.pathobj / '_dummy', ds.pathobj / '_dummy_target'
):
for i in ('symlinkuntracked', 'symlinkingit'):
tpath = ds.pathobj / f'target_{i}'
lpath = ds.pathobj / f'file_{i}'
tpath.write_text(
content_tmpl.format(i), encoding='utf-8')
lpath.symlink_to(tpath)
fcount += 1
ds.save('file_symlinkingit', to_git=True)
return fcount, content_tmpl


def test_iter_gitworktree_basic_fp(existing_dataset, no_result_rendering):
ds = existing_dataset
fcount, content_tmpl = prep_fp_tester(ds)

for ai in filter(
lambda i: i.name.name.startswith('file_'),
iter_gitworktree(ds.pathobj, fp=True)
):
fcount -= 1
if ai.fp:
# for annexed files the fp can be an annex pointer file.
# in the context of `iter_gitworktree` this is not a
# recognized construct
assert content_tmpl.format(
ai.name.name[5:]) == ai.fp.read().decode() \
or ai.name.name.startswith('file_annex')
else:
assert (ds.pathobj / ai.name).exists() is False
assert not fcount
Loading