Skip to content

Commit

Permalink
Merge pull request #681 from mih/bf-680
Browse files Browse the repository at this point in the history
Fix no-HEAD handling in `iter_gitstatus()`
  • Loading branch information
mih committed May 14, 2024
2 parents ad6720c + 0fe8442 commit 9b9984e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
6 changes: 6 additions & 0 deletions changelog.d/20240514_071318_michael.hanke_bf_680.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### 🐛 Bug Fixes

- `next-status` now detects staged changes in repositories with no
commit.
Fixes https://github.com/datalad/datalad-next/issues/680 via
https://github.com/datalad/datalad-next/pull/681 (by @mih)
17 changes: 11 additions & 6 deletions datalad_next/iter_collections/gitstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from typing import Generator

from datalad_next.consts import PRE_INIT_COMMIT_SHA
from datalad_next.runners import (
CommandError,
call_git_lines,
Expand Down Expand Up @@ -99,6 +100,10 @@ def iter_gitstatus(
path = Path(path)

head, corresponding_head = get_worktree_head(path)
if head is None:
# no commit at all -> compare to an empty repo.
head = PRE_INIT_COMMIT_SHA

# TODO it would make sense to always (or optionally) compare against any
# existing corresponding_head. This would make the status communicate
# anything that has not made it into the corresponding branch yet
Expand Down Expand Up @@ -162,15 +167,15 @@ def _yield_dir_items(
)
}
# diff constrained to direct children
for item in ([] if head is None else iter_gitdiff(
for item in iter_gitdiff(
path,
from_treeish='HEAD',
from_treeish=head,
# to the worktree
to_treeish=None,
recursive='no',
# TODO trim scope like in repo_items
eval_submodule_state=eval_submodule_state,
)):
):
if item.status != GitDiffStatus.deletion \
and item.gittype in container_types:
if item.gittype == GitTreeItemType.submodule:
Expand Down Expand Up @@ -251,17 +256,17 @@ def _yield_repo_items(
str(item.name): item for item in iter_submodules(path)
}
# start with a repository-contrained diff against the worktree
for item in ([] if head is None else iter_gitdiff(
for item in iter_gitdiff(
path,
from_treeish='HEAD',
from_treeish=head,
# to the worktree
to_treeish=None,
recursive='repository',
# we should be able to go cheaper with the submodule evaluation here.
# We need to redo some check for adjusted mode, and other cases anyways
eval_submodule_state='commit'
if eval_submodule_state == 'full' else eval_submodule_state,
)):
):
# immediately investigate any submodules that are already
# reported modified by Git
if item.gittype == GitTreeItemType.submodule:
Expand Down
12 changes: 12 additions & 0 deletions datalad_next/iter_collections/tests/test_itergitstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,15 @@ def test_status_gitinit(tmp_path):
assert len(res) == 1
assert res[0].name == 'untracked'
assert res[0].status == GitDiffStatus.other


def test_status_nohead_staged(tmp_path):
# initialize a fresh git repo, but make no commits
assert call_git_success(['init'], cwd=tmp_path)
# stage a file
(tmp_path / 'probe').write_text('tostage')
assert call_git_success(['add', 'probe'], cwd=tmp_path)
_assert_testcases(
{i.name: i for i in iter_gitstatus(tmp_path)},
[{'name': 'probe', 'status': GitDiffStatus.addition}],
)
10 changes: 10 additions & 0 deletions datalad_next/repo_utils/worktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
def get_worktree_head(
path: Path,
) -> tuple[str | None, str | None]:
"""Returns the symbolic name of the worktree `HEAD` at the given path
Returns
-------
tuple
The first item is the symbolic name of the worktree `HEAD`, or `None`
if there is no commit.
The second item is the symbolic name of the "corresponding branch" in
an adjusted-mode git-annex repository, or `None`.
"""
try:
HEAD = call_git_lines(
# we add the pathspec disambiguator to get cleaner error messages
Expand Down

0 comments on commit 9b9984e

Please sign in to comment.