Skip to content

fix(dg): pr diff runs outside a clone; pr checkout says it needs one - #672

Merged
PastaPastaPasta merged 2 commits into
masterfrom
fix/dg-pr-diff-anywhere
Nov 23, 2026
Merged

PastaPastaPasta merged 2 commits into
masterfrom
fix/dg-pr-diff-anywhere

Conversation

@PastaPastaPasta

Copy link
Copy Markdown
Owner

dg pr diff <repo> <n> run anywhere but inside a git repository failed with the "report it" error:

$ cd ~ && dg pr diff FRxiZ1SgLCn2NYgwTLTUUW3jMeKhn7WFFwcQMatPusgH/nov-qa-web 3
error: could not read the pull request                                  [E101]
  cause: fetching the PR head from its source repository: git fetch failed: fatal: not a git repository (or any parent up to mount point /) …
  fix:   run it again with RUST_LOG=debug for detail; if it persists, report it with that output at …

gh pr diff works from any directory, and reading a diff shouldn't need a clone.

  • dg pr diff: when the current directory isn't in a git repository, it fetches into a scratch bare repository (the existing scratch_repo(), removed on drop). Inside a clone it still fetches there, so the objects are kept for next time.
  • dg pr checkout: it does need a repository. Outside one it now fails with E206 and says to clone first, instead of E101.

Live on testnet with this branch's build (AyvEsSQ…/ux5-browse, PR #1), from an empty directory that isn't a repository:

$ dg pr diff AyvEsSQfK5qBotCsi2gZpePa7DcniqWzZ281TaNsJGv7/ux5-browse 1
diff --git a/README.md b/README.md
…
+Added on a branch.
$ echo $?
0
$ dg pr checkout AyvEsSQfK5qBotCsi2gZpePa7DcniqWzZ281TaNsJGv7/ux5-browse 1
error: dg pr checkout AyvEsSQfK5qBotCsi2gZpePa7DcniqWzZ281TaNsJGv7/ux5-browse 1 needs a git repository to put the branch in [E206]
  fix:   clone it first, then run this inside the clone: dg repo clone AyvEsSQfK5qBotCsi2gZpePa7DcniqWzZ281TaNsJGv7/ux5-browse
  note:  nothing was fetched

The directory is still empty afterwards. Unit test diff_workdir_uses_the_repository_it_is_in_or_a_scratch_one covers both cases. clippy -D warnings is clean, and the CHANGELOG has a Fixed entry.

Not in scope: a merged PR's dg pr diff is empty, because the base now contains the head. That is tracked in #670.

Found in UX wave 5's CLI probe.

🤖 Generated with Claude Code

dg pr diff outside a git repository failed with E101 "report it",
because git fetch had no repository to fetch into. It now fetches into a
scratch repository when the current directory is not in one, as gh pr
diff runs anywhere. dg pr checkout outside a repository now fails with
E206 and says to clone first, instead of E101.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 17 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9282a857-47f4-406d-938e-12716e1d0ac4
Commits

Reviewing files that changed from the base of the PR and between c03f765 and 356c262.

Files selected for processing: 3

  • CHANGELOG.md
  • crates/dg/src/pr/mod.rs
  • docs/errors.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@PastaPastaPasta PastaPastaPasta left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Verdict: approve

The fix is correct and small. dg pr diff now works outside a clone, and dg pr checkout fails with a clear E206 instead of E101. I built and ran pr::tests::diff_workdir_uses_the_repository_it_is_in_or_a_scratch_one at f46d76a: it passes.

Correctness of git rev-parse --git-dir (crates/dg/src/pr/mod.rs:4542, :4779). I checked it by hand. It succeeds in a subdirectory, in a linked worktree, in a bare repo, inside .git, and with GIT_DIR set from a non-repository directory. It fails only in a plain non-repository directory. That is the right test for diff, because git diff base...head works in a bare repo too. GIT_DIR reaches every git call that follows, so with it set dg uses that repository rather than a scratch one, as before.

Scratch lifetime. let (cwd, _scratch) = … is a named binding, not _, so the TempDir lives until diff returns. That covers both fetches, the final git diff and ctx.emit. Every ? path drops it as well.

Private repos. Nothing new is exposed. The key still goes to git only through read_env(ctx, private), the same way scratch_with_pr already sends it into a scratch bare repo for other commands. tempfile creates the directory 0700. One pre-existing gap: a SIGINT mid-fetch skips the drop, so the fetched objects stay in $TMPDIR. That already happens with scratch_with_pr and is not introduced here.

Nits (none blocking):

  1. crates/dg/src/pr/mod.rs:5026: the new test sits between checkout_fixture's /// doc comment and its fn. The comment ("A repository on main … returns the repository and the head oid") now documents the test, and checkout_fixture has none. Move the test above the comment or below the fixture.
  2. docs/errors.md:136-140 (E206): the docs don't list dg pr checkout outside a repository as a cause, and "What to do" says git init / cd, while the new fix says dg repo clone. One sentence would align them, for example: "dg pr checkout outside a git repository: clone the repository first (dg repo clone <repo>)."
  3. crates/dg/src/pr/mod.rs:4542: in a repository git rejects for dubious ownership (safe.directory), rev-parse fails. Checkout then says "needs a git repository" in a directory that is one, and diff quietly falls back to a scratch repo. This is rare and the scratch fallback is harmless, but checkout could carry .cause() with git's stderr (git::git(...) instead of git_ok) so the real reason shows.
  4. Optional: the checkout guard runs after locate(), so a typo'd PR number outside a repo still costs the Platform reads first. Moving the cwd check above locate would fail faster, and "nothing was fetched" would still hold.

The CHANGELOG Fixed entry is accurate. The diff is minimal and scoped, and leaving the merged-PR empty diff to #670 is reasonable.


🤖 Posted autonomously by Claude (claude-opus-5-5) on behalf of pasta.

@PastaPastaPasta
PastaPastaPasta merged commit 6b4e3e5 into master Nov 23, 2026
20 checks passed
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.

1 participant