Skip to content

refactor(diff): normalize git argument ordering in workspaceTrackedDiff#4

Open
chethanuk wants to merge 1 commit into
mainfrom
fix/issue-374-workspace-diff-arg-order
Open

refactor(diff): normalize git argument ordering in workspaceTrackedDiff#4
chethanuk wants to merge 1 commit into
mainfrom
fix/issue-374-workspace-diff-arg-order

Conversation

@chethanuk

@chethanuk chethanuk commented Jul 15, 2026

Copy link
Copy Markdown
Owner

Description

Normalizes the git argument ordering in workspaceTrackedDiff (internal/diff/git.go) so both runGit invocations follow the same convention as every other diff command in the file: option flags first, then positional refs (HEAD), then --. No behavioral change to the produced diff — git accepts either ordering — this is purely for consistency and maintainability.

The reordered calls use --end-of-options before the positional ref, the same guard the range/commit modes in this file already use — so workspace mode now shares their existing git ≥ 2.24 floor (released Nov 2019). Old and new orderings were verified to produce byte-identical output.

While touching this function, the fallback from git diff HEAD to git diff --staged gained a clarifying comment and a regression test: the fallback is load-bearing for repositories with no commits yet, where HEAD does not resolve (git diff HEAD fails with bad revision 'HEAD') but git diff --staged still surfaces staged changes by diffing the index against the empty tree. TestWorkspaceDiffNoCommitsUsesStagedFallback pins this: it stages a file in a commit-less repository and asserts the workspace diff surfaces it.

Type of Change

  • Refactoring (no functional changes)

How Has This Been Tested?

  • make test passes locally
  • Manual testing (describe below)

New regression test TestWorkspaceDiffNoCommitsUsesStagedFallback fails if the --staged fallback is removed and passes with the reordered arguments, demonstrating the reorder preserves behavior including the no-commit edge case. make check (tidy + fmt + vet) also passes.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable)

Related Issues

Closes alibaba#374

Summary by CodeRabbit

  • New Features

    • Added Ollama Cloud as a built-in model provider, including supported models and configuration through OLLAMA_API_KEY.
    • Expanded documented protocol support to include the OpenAI Responses API.
  • Bug Fixes

    • Workspace diffs now correctly include staged changes in repositories with no commits.
  • Documentation

    • Updated English, Japanese, and Chinese translations to reflect OpenAI Responses API support.

@codeant-ai codeant-ai Bot added the size:XS This PR changes 0-9 lines, ignoring generated files label Jul 15, 2026
gemini-code-assist[bot]

This comment was marked as outdated.

@chethanuk
chethanuk force-pushed the fix/issue-374-workspace-diff-arg-order branch 2 times, most recently from 3fe6215 to cafa137 Compare July 15, 2026 09:14
Repository owner deleted a comment from codeant-ai Bot Jul 15, 2026
Repository owner deleted a comment from codeant-ai Bot Jul 15, 2026
Repository owner deleted a comment from coderabbitai Bot Jul 15, 2026
Repository owner deleted a comment from codeant-ai Bot Jul 15, 2026
Repository owner deleted a comment from gemini-code-assist Bot Jul 15, 2026
Repository owner deleted a comment from coderabbitai Bot Jul 15, 2026
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Caution

Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted.

Error details
Not Found - https://docs.github.com/rest/issues/comments#update-an-issue-comment

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

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@internal/diff/git.go`:
- Around line 264-276: Update the git diff handling around runGit to return the
git diff HEAD result whenever err == nil, removing the out != "" condition.
Preserve the context cancellation check and staged-diff fallback for errors,
including repositories without a HEAD.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 58aa6f56-70f5-48ad-a56a-8e72497bdf5e

📥 Commits

Reviewing files that changed from the base of the PR and between 0ec3769 and cafa137.

📒 Files selected for processing (7)
  • internal/diff/git.go
  • internal/diff/git_test.go
  • internal/llm/providers.go
  • internal/llm/providers_test.go
  • pages/src/i18n/en.ts
  • pages/src/i18n/ja.ts
  • pages/src/i18n/zh.ts

Comment thread internal/diff/git.go
Comment on lines +264 to +276
out, err := p.runGit(ctx, "-c", "core.quotepath=false", "diff", "--no-ext-diff", "--no-textconv", "--find-renames", "--src-prefix=a/", "--dst-prefix=b/", "--no-color", "-U"+fmt.Sprint(DiffContextLines), "--end-of-options", "HEAD", "--")
if err == nil && out != "" {
return out, nil
}
if ctx.Err() != nil {
return "", ctx.Err()
}
return p.runGit(ctx, "-c", "core.quotepath=false", "diff", "--no-ext-diff", "--no-textconv", "--find-renames", "--src-prefix=a/", "--dst-prefix=b/", "--staged", "--no-color", "-U"+fmt.Sprint(DiffContextLines), "--")
// Fall back to the staged diff when `git diff HEAD` errored or was empty. This is
// not redundant with the call above: in a repository with no commits yet there is no
// HEAD, so `git diff HEAD` fails with "bad revision 'HEAD'", but `git diff --staged`
// still surfaces staged changes by diffing the index against the empty tree — the only
// way to review a workspace before its first commit.
return p.runGit(ctx, "-c", "core.quotepath=false", "diff", "--no-ext-diff", "--no-textconv", "--find-renames", "--src-prefix=a/", "--dst-prefix=b/", "--no-color", "-U"+fmt.Sprint(DiffContextLines), "--staged", "--")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify the behavior of git diff HEAD vs --staged when WT matches HEAD but index differs.

repo=$(mktemp -d)
cd "$repo"
git init -q
git config user.email "test@example.com"
git config user.name "Test User"
git config commit.gpgsign false

echo "A" > file.txt
git add file.txt
git commit -m "Initial commit"

echo "B" > file.txt
git add file.txt
echo "A" > file.txt

echo "--- git diff HEAD ---"
git diff HEAD
echo "--- git diff --staged ---"
git diff --staged

Repository: chethanuk/open-code-review

Length of output: 492


Drop the out != "" check here. git diff HEAD already errors when HEAD is missing, and on a clean tree it returns empty output; if the index has staged-only changes, this fallback will surface those staged diffs even though the working tree matches HEAD. If the fallback is only for repos without commits, err == nil is enough.

🤖 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/diff/git.go` around lines 264 - 276, Update the git diff handling
around runGit to return the git diff HEAD result whenever err == nil, removing
the out != "" condition. Preserve the context cancellation check and staged-diff
fallback for errors, including repositories without a HEAD.

…or workspace diff

Follow-up to alibaba#381, which normalized the argument ordering in
workspaceTrackedDiff (options -> positional ref -> --) but stopped short of
the --end-of-options guard. This adds it before HEAD in the first runGit
call, bringing workspace mode fully in line with the canonical range/commit/
merge-base calls in this file (git.go:119/126/256), which already require
git >= 2.24.

The --staged fallback call is untouched: it has no positional ref (only the
-- pathspec separator), so --end-of-options would guard nothing there.

Also documents why the --staged fallback is load-bearing (repos with no
commits have no HEAD, so `git diff HEAD` fails while `git diff --staged`
still surfaces staged changes against the empty tree) and pins it with
TestWorkspaceDiffNoCommitsUsesStagedFallback.

Closes alibaba#374
@chethanuk
chethanuk force-pushed the fix/issue-374-workspace-diff-arg-order branch from cafa137 to 3126e26 Compare July 17, 2026 12:21
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@chethanuk, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 55 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: b9a1871c-e102-494c-a17f-82af1811474d

📥 Commits

Reviewing files that changed from the base of the PR and between cafa137 and 3126e26.

📒 Files selected for processing (2)
  • internal/diff/git.go
  • internal/diff/git_test.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-374-workspace-diff-arg-order

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(diff): normalize git argument ordering in workspaceTrackedDiff

1 participant