fix(agent,tui): resolve git branch detection when starting Zero in subdirectories#613
Conversation
…bdirectories Currently, both gitBranchForPrompt (agent) and gitBranch (tui) check for a .git entry directly inside the current working directory. When starting Zero in nested folders/subdirectories of a repository, the check fails and the branch segment is omitted. Export findProjectGitRoot as FindProjectGitRoot in agent package, and use it in both functions to resolve the correct repository root before checking HEAD and resolving the branch name. Also resolve relative worktree gitdirs relative to the repository root.
WalkthroughGit root detection in the agent and TUI packages is refactored: ChangesGit root resolution fix
Estimated code review effort: 2 (Simple) | ~12 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/tui/tui_fixes_test.go (1)
163-180: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winLGTM!
The test correctly validates the core fix —
gitBranchresolves the branch from a nested subdirectory by walking up to the git root. This would fail with the old cwd-only lookup, making it a solid regression test.One optional suggestion: the PR description mentions handling relative worktree
gitdir:paths, but there's no test covering that scenario here. Consider adding a companion test where.gitis a file containinggitdir: ../pathto verify the worktree resolution logic from a subdirectory.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/tui/tui_fixes_test.go` around lines 163 - 180, Add a companion regression test for `gitBranch` that covers worktree-style `.git` files with a relative `gitdir:` path, since the current test only validates walking up from a nested subdirectory to the repo root. Reuse the existing test setup in `internal/tui/tui_fixes_test.go` and extend it so the subdirectory lookup resolves through a `.git` file containing `gitdir: ../path`, then assert `gitBranch` still returns the correct branch from that subdirectory.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/tui/tui_fixes_test.go`:
- Around line 163-180: Add a companion regression test for `gitBranch` that
covers worktree-style `.git` files with a relative `gitdir:` path, since the
current test only validates walking up from a nested subdirectory to the repo
root. Reuse the existing test setup in `internal/tui/tui_fixes_test.go` and
extend it so the subdirectory lookup resolves through a `.git` file containing
`gitdir: ../path`, then assert `gitBranch` still returns the correct branch from
that subdirectory.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d7373365-774e-4e10-94cc-784b936404c5
📒 Files selected for processing (5)
internal/agent/loop_test.gointernal/agent/system_prompt.gointernal/agent/system_prompt_test.gointernal/tui/tui_fixes_test.gointernal/tui/view.go
anandh8x
left a comment
There was a problem hiding this comment.
LGTM.
Right fix: walk up to the nearest .git root instead of only looking in cwd. That matches how git itself resolves the repo, and reuses the helper we already had for AGENTS.md instead of inventing a second path.
Both call sites (prompt + TUI) get the same treatment, and relative worktree gitdir paths are resolved against the worktree root so nested cwd does not break HEAD lookup. Subdir tests pin it. All CI green. Ship it.
Summary
Fixes a medium-severity issue where starting Zero from a nested subdirectory of a Git repository fails to detect the current branch name (e.g. for prompt assembly or in TUI header/welcome screens).
Previously, both
gitBranchForPrompt(in prompt assembly) andgitBranch(in TUI view) checked for.gitmetadata directly in the current working directory (cwd). In subdirectories, this check fails, causing the branch name to be omitted.This fix exports the existing unexported
findProjectGitRoothelper frominternal/agentasFindProjectGitRoot. Both branch resolution paths are updated to first find the nearest Git repository root via this helper and resolve the branch relative to it.Changes
internal/agent/system_prompt.gofindProjectGitRootasFindProjectGitRootandhasGitMetadataasHasGitMetadata.projectGuidelinesto call the exported helper.gitBranchForPromptto useFindProjectGitRoot(cwd)to resolve repository root, and resolve relative worktree gitdirs relative togitRoot.internal/agent/system_prompt_test.gofindProjectGitRoottoFindProjectGitRoot.internal/agent/loop_test.goTestGitBranchForPromptInSubdirectory.internal/tui/view.gogitBranchto useagent.FindProjectGitRoot(cwd)to resolve the Git repository root.internal/tui/tui_fixes_test.goTestGitBranchInSubdirectory.Test plan
go test ./internal/agent/...— okgo test ./internal/tui/...— okSummary by CodeRabbit