Skip to content

fix(agent,tui): resolve git branch detection when starting Zero in subdirectories#613

Merged
kevincodex1 merged 1 commit into
Gitlawb:mainfrom
euxaristia:fix/git-branch-detection-subdirectories
Jul 10, 2026
Merged

fix(agent,tui): resolve git branch detection when starting Zero in subdirectories#613
kevincodex1 merged 1 commit into
Gitlawb:mainfrom
euxaristia:fix/git-branch-detection-subdirectories

Conversation

@euxaristia

@euxaristia euxaristia commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

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) and gitBranch (in TUI view) checked for .git metadata 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 findProjectGitRoot helper from internal/agent as FindProjectGitRoot. 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.go

  • Export findProjectGitRoot as FindProjectGitRoot and hasGitMetadata as HasGitMetadata.
  • Update projectGuidelines to call the exported helper.
  • Update gitBranchForPrompt to use FindProjectGitRoot(cwd) to resolve repository root, and resolve relative worktree gitdirs relative to gitRoot.

internal/agent/system_prompt_test.go

  • Update references of findProjectGitRoot to FindProjectGitRoot.

internal/agent/loop_test.go

  • Add unit test TestGitBranchForPromptInSubdirectory.

internal/tui/view.go

  • Update gitBranch to use agent.FindProjectGitRoot(cwd) to resolve the Git repository root.

internal/tui/tui_fixes_test.go

  • Add unit test TestGitBranchInSubdirectory.

Test plan

  • go test ./internal/agent/... — ok
  • go test ./internal/tui/... — ok

Summary by CodeRabbit

  • Bug Fixes
    • Improved git branch detection when working from nested subdirectories, including worktree setups with relative metadata paths.
    • Branch information now resolves more reliably and returns no value when a git root cannot be found.
    • Fixed prompt context detection so project settings are loaded from the correct repository root.
    • Added tests covering branch lookup from subdirectories and git-root discovery behavior.

…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.
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

Git root detection in the agent and TUI packages is refactored: findProjectGitRoot and hasGitMetadata are exported as FindProjectGitRoot/HasGitMetadata. gitBranchForPrompt and gitBranch now resolve .git and worktree gitdir: paths against the discovered git root rather than the raw working directory, fixing subdirectory lookups. New tests cover subdirectory scenarios.

Changes

Git root resolution fix

Layer / File(s) Summary
Export git root helpers and update agent branch lookup
internal/agent/system_prompt.go, internal/agent/system_prompt_test.go, internal/agent/loop_test.go
findProjectGitRoot/hasGitMetadata exported as FindProjectGitRoot/HasGitMetadata; workspaceContext and gitBranchForPrompt updated to resolve .git and worktree gitdir: paths against the discovered git root; tests updated/added, including a new subdirectory test.
Update TUI gitBranch resolution
internal/tui/view.go, internal/tui/tui_fixes_test.go
gitBranch trims input, uses agent.FindProjectGitRoot to locate the git root, builds .git and worktree gitdir: paths from that root, and a new test validates branch detection from a nested subdirectory.

Estimated code review effort: 2 (Simple) | ~12 minutes

Possibly related PRs

  • Gitlawb/zero#83: Overlaps with fixes to internal/tui/view.go's gitBranch, including git-root detection and worktree gitdir resolution.
  • Gitlawb/zero#129: Both modify internal/agent/system_prompt.go's worktree gitdir: relative-path handling for HEAD lookup, plus associated tests.

Suggested reviewers: gnanam1990, Vasanthdev2004

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: fixing git branch detection in agent and TUI when Zero starts from subdirectories.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
internal/tui/tui_fixes_test.go (1)

163-180: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

LGTM!

The test correctly validates the core fix — gitBranch resolves 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 .git is a file containing gitdir: ../path to 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

📥 Commits

Reviewing files that changed from the base of the PR and between a880ce8 and 97fc329.

📒 Files selected for processing (5)
  • internal/agent/loop_test.go
  • internal/agent/system_prompt.go
  • internal/agent/system_prompt_test.go
  • internal/tui/tui_fixes_test.go
  • internal/tui/view.go

@anandh8x anandh8x left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kevincodex1 kevincodex1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@kevincodex1
kevincodex1 merged commit 0184581 into Gitlawb:main Jul 10, 2026
7 checks passed
@euxaristia
euxaristia deleted the fix/git-branch-detection-subdirectories branch July 17, 2026 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants