1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations
import subprocess
from pathlib import Path
import pytest
from pygit2 import Repository, init_repository
from pygit2.enums import FileStatus
from pygittools.tui.worktree import categorize_status, stage_path, stage_paths
@pytest.fixture
def repo(tmp_path: Path) -> Repository:
repo_path = tmp_path / "repo"
repo_path.mkdir()
return init_repository(str(repo_path), bare=False)
def _init_nested_repo(path: Path) -> None:
path.mkdir()
subprocess.run(["git", "init"], cwd=path, check=True, capture_output=True)
(path / "hello.txt").write_text("hello\n", encoding="utf-8")
subprocess.run(["git", "-C", str(path), "add", "hello.txt"], check=True, capture_output=True)
subprocess.run(
[
"git",
"-C",
str(path),
"-c",
"user.email=alice@example.com",
"-c",
"user.name=alice",
"commit",
"-m",
"init",
],
check=True,
capture_output=True,
)
def test_stage_path_accepts_untracked_directory_trailing_slash(repo: Repository) -> None:
workdir = Path(repo.workdir)
_init_nested_repo(workdir / "nested")
_, _, untracked = categorize_status(repo)
assert untracked == ["nested/"]
stage_path(repo, untracked[0])
staged, _, remaining = categorize_status(repo)
assert staged == ["nested"]
assert remaining == []
def test_stage_paths_accepts_untracked_directory_trailing_slash(repo: Repository) -> None:
workdir = Path(repo.workdir)
_init_nested_repo(workdir / "pkg")
(workdir / "loose.txt").write_text("x\n", encoding="utf-8")
_, _, untracked = categorize_status(repo)
assert set(untracked) == {"pkg/", "loose.txt"}
stage_paths(repo, untracked)
status = repo.status()
assert status["pkg"] & FileStatus.INDEX_NEW
assert status["loose.txt"] & FileStatus.INDEX_NEW