Skip to content
Open
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
5 changes: 5 additions & 0 deletions ai_sync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def cmd_init(a) -> int:

def _default_project() -> str:
try:
r = subprocess.run(["git", "rev-parse", "--git-common-dir"], capture_output=True, text=True, timeout=5)
if r.returncode == 0 and r.stdout.strip():
common = Path(r.stdout.strip()).resolve()
if common.name == ".git":
return common.parent.name
r = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, text=True, timeout=5)
return Path(r.stdout.strip()).name if r.returncode == 0 else ""
except (OSError, subprocess.TimeoutExpired):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_ai_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,35 @@ def test_fresh_lock_is_left_alone(self):
core.git_run(self.cfg, ["add", "x.md"])
self.assertTrue(lock.exists())
self.assertFalse(core.clear_stale_lock("some other git error"))


class DefaultProjectTests(Base):
"""_default_project() resolves git worktrees to the main repo name, not the worktree directory."""

def test_resolves_main_repo_name_from_worktree_and_subdirs(self):
repo = self.t / "my-project"
repo.mkdir()
_git(repo, "init")
(repo / "README.md").write_text("hi\n")
_git(repo, "add", "README.md")
_git(repo, "commit", "-m", "init")
sub = repo / "nested" / "dir"
sub.mkdir(parents=True)

wt = self.t / "wt-session-123"
_git(repo, "worktree", "add", "-b", "wt-branch", str(wt))
wt_sub = wt / "nested"
wt_sub.mkdir(parents=True, exist_ok=True)

non_git = self.t / "plain-dir"
non_git.mkdir()

prev = os.getcwd()
try:
for path in (repo, sub, wt, wt_sub):
os.chdir(path)
self.assertEqual(cli._default_project(), "my-project")
os.chdir(non_git)
self.assertEqual(cli._default_project(), "")
finally:
os.chdir(prev)
Loading