Skip to content

refactor(git_utils): shared git-error panel printer + cwd= over chdir guards#596

Open
mattmillerai wants to merge 2 commits into
mainfrom
matt/be-4357-git-utils-cwd
Open

refactor(git_utils): shared git-error panel printer + cwd= over chdir guards#596
mattmillerai wants to merge 2 commits into
mainfrom
matt/be-4357-git-utils-cwd

Conversation

@mattmillerai

Copy link
Copy Markdown
Collaborator

ELI-5

Two functions in git_utils.pygit_checkout_tag and checkout_pr — each did the same two clunky things: (1) built a nearly-identical fancy red error box by hand, and (2) cd-ed the whole process into the repo folder to run git, then cd-ed back in a finally. This PR pulls the error box into one shared helper, and tells each git command which folder to run in (cwd=) instead of changing the whole process's working directory. Same behavior, ~20 fewer lines per function, and no more global cd that could trip up concurrent use.

What changed

  • New _print_git_error(title, panel_title, context, details, exc) helper renders the shared rich error Panel (bold-red title, bold-yellow context line, italic detail lines, optional stderr block). Both except subprocess.CalledProcessError blocks now call it.
  • cwd=repo_path on every subprocess.run call in both functions, replacing the os.getcwd() / os.chdir(repo_path) / finally: os.chdir(original_dir) scaffolding. import os is now unused and removed.

Why the cwd= change matters (beyond dedup)

os.chdir mutates process-global state. Passing cwd= per-call:

  • is safe for any future threaded/concurrent use (no shared-cwd races), and
  • removes the failure mode where os.chdir(repo_path) itself throws (e.g. missing dir) — there's no global state left to restore.

Behavior is otherwise preserved: git commands still execute against repo_path, and each function still returns False on a git failure.

Behavior note (judgment call)

The checkout_pr error panel is byte-identical to before. The git_checkout_tag panel has minor cosmetic differences on the failure path, which I consider a net improvement:

  • The tag line was previously appended via Text.append(f"[cyan]{tag}[/cyan]"), but Text.append does not parse console markup — so users literally saw [cyan]v1.2.3[/cyan] (brackets and all). It now shows a clean v1.2.3 in the header line.
  • The "Error details:" separator is now italic (was bold-red) with one fewer blank line, matching the shared helper's layout.

If exact byte-for-byte preservation of the old (buggy) tag panel is preferred, I can special-case it — but the current output reads better.

Testing

  • tests/comfy_cli/command/github/test_pr.py — the four mocked checkout_pr tests dropped their now-dead @patch("os.chdir")/@patch("os.getcwd") decorators and now assert cwd=repo_path is passed to every subprocess.run. TestGitCheckoutTag (real git repos, no chdir mocking) already exercises the cwd= path end-to-end.
  • Full suite green locally: 2769 passed, 37 skipped. ruff check clean.

Add `_print_git_error` and use it from both `git_checkout_tag` and
`checkout_pr` except blocks, deduplicating the two structurally-identical
rich error panels.

Replace the `os.getcwd`/`os.chdir`/`finally` scaffolding in both functions
with `cwd=repo_path` on every `subprocess.run` call. This removes
process-global cwd mutation (safer for any future threaded/concurrent use)
and drops the `import os` that only existed for the chdir dance.
@mattmillerai mattmillerai added the agent-coded PR authored by the agent-work loop label Jul 24, 2026
@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 32 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: a0e9dea1-c881-47ff-ad00-825572ab2521

📥 Commits

Reviewing files that changed from the base of the PR and between 85b62da and 667887d.

📒 Files selected for processing (2)
  • comfy_cli/git_utils.py
  • tests/comfy_cli/command/github/test_pr.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch matt/be-4357-git-utils-cwd
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch matt/be-4357-git-utils-cwd

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

@mattmillerai
mattmillerai marked this pull request as ready for review July 24, 2026 09:53
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Jul 24, 2026
@mattmillerai mattmillerai added the cursor-review Request Cursor bot review label Jul 24, 2026

@github-actions github-actions 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.

🔍 Cursor Review — Consolidated panel

Triggered by @mattmillerai.

Found 1 finding(s).

Severity Count
🟡 Medium 1

Panel: 5/8 reviewers contributed findings.

Reviewers that did not contribute: kimi-k2.5:adversarial (empty), claude-opus-4-8-thinking-xhigh:edge-case (parse_error), kimi-k2.5:edge-case (empty)

Comment thread comfy_cli/git_utils.py Outdated
…nt injection

The fork-controlled PR head branch name (pr_info.head_branch, from the
GitHub API and controllable by the PR author) was passed positionally to
'git fetch' on both the fork and non-fork paths. A name beginning with
'-' would be parsed as a git option; 'comfy install --pr' runs against
untrusted forks. Add a '--' end-of-options separator before the refspec
and lock it in with regression assertions.

Addresses cursor-review panel Medium finding (BE-4357).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-coded PR authored by the agent-work loop cursor-review Request Cursor bot review enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant