Skip to content

fix: resolve hook paths for worktrees and restore env var#1036

Merged
rjmurillo merged 2 commits intomainfrom
fix/996-worktree-hook-paths
Mar 8, 2026
Merged

fix: resolve hook paths for worktrees and restore env var#1036
rjmurillo merged 2 commits intomainfrom
fix/996-worktree-hook-paths

Conversation

@rjmurillo-bot
Copy link
Copy Markdown
Collaborator

@rjmurillo-bot rjmurillo-bot commented Mar 7, 2026

Summary

Fixes #996

  • Invoke-PreCommit.ps1: Resolve .cs file paths using Join-Path $repoRoot $_ before passing to dotnet format --include. Every other file type in the script already uses this pattern. Without it, dotnet format receives repo-relative paths that fail to resolve in git worktrees where $PWD differs from $repoRoot.
  • Invoke-PrePush.ps1: Save and restore $env:DOTNET_ROLL_FORWARD using a finally block. Prevents environment variable leakage to subprocesses spawned after the hook logic completes.

Bug 2 from the issue (unquoted paths in Invoke-AutoFix) was evaluated and found to be a non-issue. PowerShell passes array elements as discrete arguments to native commands. The -- separator is already present on all relevant git diff and git add calls. No change needed.

Test plan

  • Verify pwsh parses both scripts without errors (confirmed via [Parser]::ParseFile)
  • dotnet build Moq.Analyzers.sln succeeds (confirmed, 0 warnings, 0 errors)
  • Run pre-commit hook from a git worktree with staged .cs files and confirm dotnet format --include resolves paths correctly

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Improved the C# code formatting process during the pre-commit phase by updating path handling to use absolute file paths for enhanced reliability when processing staged files.
    • Enhanced build and test environment variable management to properly preserve original settings and restore them after operations complete, reducing potential side effects.

- Resolve .cs file paths with Join-Path $repoRoot in Invoke-PreCommit.ps1
  so dotnet format --include works correctly in git worktrees
- Save and restore DOTNET_ROLL_FORWARD in Invoke-PrePush.ps1 using a
  finally block to prevent environment variable leakage

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rjmurillo-bot rjmurillo-bot requested a review from rjmurillo as a code owner March 7, 2026 22:33
@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@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 two specific issues within the Git hooks. It corrects the path resolution for dotnet format in the pre-commit hook, ensuring it functions as expected within Git worktrees. Additionally, it improves the pre-push hook by implementing robust management of the DOTNET_ROLL_FORWARD environment variable, preventing unintended side effects on other processes.

Highlights

  • Pre-Commit Hook Path Resolution: Resolved an issue in Invoke-PreCommit.ps1 where dotnet format --include failed to resolve C# file paths correctly in Git worktrees by ensuring paths are joined with the repository root.
  • Pre-Push Environment Variable Management: Enhanced Invoke-PrePush.ps1 to properly save and restore the DOTNET_ROLL_FORWARD environment variable, preventing its leakage to subsequent subprocesses.
Changelog
  • .githooks/hooks/Invoke-PreCommit.ps1
    • Modified the ForEach-Object loop to prepend Join-Path $repoRoot to C# file paths before passing them to dotnet format --include.
  • .githooks/hooks/Invoke-PrePush.ps1
    • Introduced a new variable $originalRollForward to store the initial value of $env:DOTNET_ROLL_FORWARD.
    • Added a finally block to restore $env:DOTNET_ROLL_FORWARD to its original value or remove it if it was not set initially.
Activity
  • The pull request was generated using Claude Code.
  • A test plan was outlined to verify PowerShell script parsing, successful dotnet build, and correct pre-commit hook behavior in a Git worktree with staged C# files.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 7, 2026

Warning

Rate limit exceeded

@rjmurillo-bot has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 6 minutes and 8 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f05a3d6a-6c76-4ccd-9e08-4b94963ae771

📥 Commits

Reviewing files that changed from the base of the PR and between c1f891d and c84b2a7.

📒 Files selected for processing (1)
  • .githooks/hooks/Invoke-PrePush.ps1
📝 Walkthrough

Walkthrough

This pull request fixes two git hook bugs: the pre-commit hook now resolves C# file paths to absolute paths before passing them to dotnet format, and the pre-push hook preserves and restores the DOTNET_ROLL_FORWARD environment variable to prevent unintended global process state changes.

Changes

Cohort / File(s) Summary
Pre-commit path resolution
.githooks/hooks/Invoke-PreCommit.ps1
Updated C# file path handling to convert relative paths to absolute paths using Join-Path with the repository root before passing to dotnet format --include, ensuring proper path resolution in worktrees and non-root working directories.
Pre-push environment variable preservation
.githooks/hooks/Invoke-PrePush.ps1
Added tracking and restoration of the original DOTNET_ROLL_FORWARD environment variable via a finally block, restoring the original value (or removing it if unset) after the hook completes, preventing unintended global process state side effects.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • PR #960 — Both PRs modify the same git hook scripts and extend behavior for environment variable handling and path resolution introduced in that PR.
  • PR #961 — Both PRs modify .githooks/hooks/Invoke-PreCommit.ps1 to normalize file paths for tooling invocations by converting to absolute/variable-based paths.

Suggested labels

bug, build, releasable

Suggested reviewers

  • MattKotsenas
🚥 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 and specifically summarizes the main changes: resolving hook paths for worktrees and restoring environment variables.
Linked Issues check ✅ Passed The pull request successfully implements all primary objectives from issue #996: file paths in Invoke-PreCommit.ps1 are resolved with Join-Path, DOTNET_ROLL_FORWARD is saved and restored in Invoke-PrePush.ps1, and Invoke-AutoFix unquoted paths are validated as not problematic.
Out of Scope Changes check ✅ Passed All changes in Invoke-PreCommit.ps1 and Invoke-PrePush.ps1 are directly aligned with issue #996 requirements; no out-of-scope modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/996-worktree-hook-paths

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

@deepsource-io
Copy link
Copy Markdown

deepsource-io bot commented Mar 7, 2026

DeepSource Code Review

We reviewed changes in 06b2fe4...c84b2a7 on this pull request. Below is the summary for the review, and you can see the individual issues we found as inline review comments.

See full review on DeepSource ↗

PR Report Card

Overall Grade   Security  

Reliability  

Complexity  

Hygiene  

Code Review Summary

Analyzer Status Updated (UTC) Details
C# Mar 7, 2026 10:56p.m. Review ↗

@coderabbitai coderabbitai bot requested a review from MattKotsenas March 7, 2026 22:33
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 two fixes for the git hooks. The first fix correctly resolves file paths for dotnet format in Invoke-PreCommit.ps1, which is important for git worktree support. The second fix in Invoke-PrePush.ps1 ensures that the DOTNET_ROLL_FORWARD environment variable is properly restored after the script runs. While the implementation is correct, I've provided a suggestion to simplify the logic for restoring the environment variable, making it more concise and idiomatic to PowerShell.

Comment thread .githooks/hooks/Invoke-PrePush.ps1 Outdated
rjmurillo
rjmurillo previously approved these changes Mar 7, 2026
coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 7, 2026
Replace if/else block with single assignment since PowerShell removes
an environment variable when assigned $null.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rjmurillo rjmurillo merged commit 9363753 into main Mar 8, 2026
40 checks passed
@rjmurillo rjmurillo deleted the fix/996-worktree-hook-paths branch March 8, 2026 01:14
@rjmurillo rjmurillo added this to the vNext milestone Mar 8, 2026
rjmurillo-bot added a commit that referenced this pull request Mar 8, 2026
## Summary

Fixes #996

- **Invoke-PreCommit.ps1**: Resolve `.cs` file paths using `Join-Path
$repoRoot $_` before passing to `dotnet format --include`. Every other
file type in the script already uses this pattern. Without it, `dotnet
format` receives repo-relative paths that fail to resolve in git
worktrees where `$PWD` differs from `$repoRoot`.
- **Invoke-PrePush.ps1**: Save and restore `$env:DOTNET_ROLL_FORWARD`
using a `finally` block. Prevents environment variable leakage to
subprocesses spawned after the hook logic completes.

Bug 2 from the issue (unquoted paths in `Invoke-AutoFix`) was evaluated
and found to be a non-issue. PowerShell passes array elements as
discrete arguments to native commands. The `--` separator is already
present on all relevant `git diff` and `git add` calls. No change
needed.

## Test plan

- [ ] Verify `pwsh` parses both scripts without errors (confirmed via
`[Parser]::ParseFile`)
- [ ] `dotnet build Moq.Analyzers.sln` succeeds (confirmed, 0 warnings,
0 errors)
- [ ] Run pre-commit hook from a git worktree with staged `.cs` files
and confirm `dotnet format --include` resolves paths correctly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Chores**
* Improved the C# code formatting process during the pre-commit phase by
updating path handling to use absolute file paths for enhanced
reliability when processing staged files.
* Enhanced build and test environment variable management to properly
preserve original settings and restore them after operations complete,
reducing potential side effects.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Richard Murillo <rjmurillo@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Pre-commit hook --include paths may not resolve in worktrees

2 participants