Skip to content

fix(cli): sync footer branch name on filesystems without fs.watch events#28012

Open
manumishra12 wants to merge 5 commits into
google-gemini:mainfrom
manumishra12:fix/27957-branch-name-sync
Open

fix(cli): sync footer branch name on filesystems without fs.watch events#28012
manumishra12 wants to merge 5 commits into
google-gemini:mainfrom
manumishra12:fix/27957-branch-name-sync

Conversation

@manumishra12

Copy link
Copy Markdown

Summary

The footer Branch indicator does not update after a git checkout on filesystems where fs.watch delivers no change events — most notably WSL mounts of Windows drives (/mnt/c/...) and network shares. The displayed branch stays stuck on whatever was checked out when the CLI started.

Fixes #27957

Details

useGitBranchName already watches the git directory with fs.watch and re-fetches the branch when HEAD changes. But fs.watch relies on the OS notification layer (inotify on Linux, FSEvents on macOS), and on WSL mounts of Windows drives and on many network filesystems that layer delivers no events at all — so the watcher silently never fires and the branch is never refreshed (the environment in #27957 is WSL).

This adds a stat-based fs.watchFile poll on HEAD as a reliable fallback that works regardless of the underlying filesystem, running alongside the existing fs.watch (which remains the fast, event-driven path where it works):

  • Both paths funnel into a single debounced scheduleRefresh() helper (no behavior change to the existing fast path).
  • The poll only refreshes when HEAD's mtime actually changes, so steady state does no work.
  • Poll interval is 2s (a single stat of a tiny file; negligible cost).
  • The poll is torn down on unmount via fs.unwatchFile.

No changes are needed in the footer component or UI state — they already react to whatever the hook returns.

How to Validate

Manual (on WSL with the repo under /mnt/<drive>, on a network share, or any affected FS):

  1. Start gemini in a git repo and note the branch shown in the footer.
  2. Switch branches, e.g. ! git checkout -b some-branch (or in another shell).
  3. Expected: within ~2s the footer reflects the new branch. Before this change it stayed on the old branch on affected filesystems.

Automated:

npm run --workspace packages/cli test -- src/ui/hooks/useGitBranchName.test.tsx

A new test covers the polling fallback (when fs.watch never fires), and the cleanup test is extended to assert the poll is removed on unmount.

Pre-Merge Checklist

  • Prettier, ESLint (--max-warnings 0) and TypeScript typecheck pass for the changed files.
  • Unit tests added and passing (useGitBranchName.test.tsx, plus Footer.test.tsx consumers).
  • Change is scoped to a single hook; no public API changes.
  • Linked issue: Checkout git branch not reflected in CLI UI #27957

The footer "Branch" item is driven by useGitBranchName, which watches the
git directory with fs.watch and re-fetches the branch when HEAD changes.
fs.watch relies on the OS notification layer (inotify/FSEvents), which
delivers no events on some filesystems — most notably WSL mounts of Windows
drives and network shares — so `git checkout` never updates the displayed
branch in those environments.

Add a stat-based fs.watchFile poll on HEAD as a reliable fallback alongside
the existing fs.watch, refresh through a shared debounced helper, and tear
the poll down on unmount. Extend the tests to cover the polling path and its
cleanup.

Fixes google-gemini#27957
@manumishra12 manumishra12 requested a review from a team as a code owner June 18, 2026 12:57
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where the CLI's git branch indicator fails to update after a checkout on specific filesystems that do not support standard OS-level file watch events. By introducing a lightweight stat-based polling fallback for the HEAD file, the branch name now reliably reflects the current state across all environments, including WSL and network-mounted drives, without impacting the performance of existing event-driven platforms.

Highlights

  • Reliable Git Branch Tracking: Implemented a fallback polling mechanism using fs.watchFile on the HEAD file to ensure the git branch indicator updates correctly on filesystems where fs.watch events are not triggered, such as WSL mounts and network shares.
  • Efficient Polling Strategy: The new polling mechanism only triggers a refresh when the mtime of the HEAD file changes, ensuring minimal performance impact while maintaining a responsive UI.
  • Improved Test Coverage: Added comprehensive unit tests to verify the polling fallback behavior and ensure proper cleanup of watchers upon component unmount.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the size/m A medium sized PR label Jun 18, 2026
@github-actions

github-actions Bot commented Jun 18, 2026

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 119
  • Additions: +112
  • Deletions: -7
  • Files changed: 2

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a polling fallback mechanism using fs.watchFile on the Git HEAD file to ensure the branch name updates correctly on filesystems where fs.watch does not deliver events (such as WSL mounts or network shares). It also adds corresponding unit tests to verify this behavior and ensure proper cleanup on unmount. The review feedback highlights a critical issue where calling fs.unwatchFile with only the file path removes all listeners for that file across all active hook instances. To prevent this, the reviewer suggests storing and passing the specific listener function to fs.unwatchFile during cleanup, and updating the tests to assert this behavior.

Comment thread packages/cli/src/ui/hooks/useGitBranchName.ts
Comment thread packages/cli/src/ui/hooks/useGitBranchName.ts
Comment thread packages/cli/src/ui/hooks/useGitBranchName.ts Outdated
Comment thread packages/cli/src/ui/hooks/useGitBranchName.test.tsx Outdated
manumishra12 and others added 4 commits June 18, 2026 06:14
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Complete the review change: the named HEAD listener was being declared but
never created/stored, so the unmount guard `watchedHeadPath && watchedHeadListener`
was always false and fs.unwatchFile never ran (leaking the poll on unmount).
Define the listener as a named function, pass it to fs.watchFile, and store it
in watchedHeadListener so cleanup removes only this hook instance's listener.
@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality labels Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality priority/p2 Important but can be addressed in a future release. size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Checkout git branch not reflected in CLI UI

1 participant