Skip to content

Commit 453b4dc

Browse files
anakrishCopilot
andcommitted
fix(copilot): robust diff computation for cloud agent environments
The cloud agent checks out a branch like copilot/review-pr-NNN which may not have upstream/main or origin/main refs available for merge-base. Changes: - Add 'main' as third fallback in merge-base resolution - Add gh pr diff as final fallback when no merge-base is found - Explicitly fetch origin/main in copilot-setup-steps.yml - Applied to all 5 diff commands across both skills Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 88c7ef8 commit 453b4dc

3 files changed

Lines changed: 36 additions & 16 deletions

File tree

.github/copilot-setup-steps.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ steps:
88
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
99
with:
1010
fetch-depth: 0 # full history needed for git diff against main
11+
- run: git fetch origin main:refs/remotes/origin/main
12+
name: Ensure origin/main ref is available for diff computation

.github/skills/code-review/SKILL.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ Key constraints (details in copilot-instructions.md):
2525
## Step 1: Get the Diff
2626

2727
```bash
28+
# Find the merge-base with main (handles: upstream remote, origin remote,
29+
# local main branch, or falls back to gh pr diff for cloud agent environments).
2830
BASE=$(git merge-base upstream/main HEAD 2>/dev/null \
29-
|| git merge-base origin/main HEAD 2>/dev/null)
31+
|| git merge-base origin/main HEAD 2>/dev/null \
32+
|| git merge-base main HEAD 2>/dev/null)
3033
if [ -z "$BASE" ]; then
31-
echo "ERROR: Cannot find upstream/main or origin/main. Cannot determine review scope."
32-
exit 1
34+
echo "No merge-base found. Falling back to gh pr diff."
35+
gh pr diff --name-only
36+
gh pr diff -- '*.rs' '*.toml' 'examples/'
37+
else
38+
echo "Reviewing changes since: $BASE"
39+
git diff "$BASE"..HEAD --stat
40+
git diff "$BASE"..HEAD -- '*.rs' '*.toml' 'examples/'
3341
fi
34-
echo "Reviewing changes since: $BASE"
35-
git diff "$BASE"..HEAD --stat
36-
git diff "$BASE"..HEAD -- '*.rs' '*.toml' 'examples/'
3742
```
3843

3944
If the diff is empty, stop and report: "No changes found to review."

.github/skills/deep-review/SKILL.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,20 @@ Use `read_agent` with `wait: true` to wait for each background agent.
4040
## Step 1: Get the Diff and Build Inventory
4141

4242
```bash
43+
# Find the merge-base with main (handles: upstream remote, origin remote,
44+
# local main branch, or falls back to gh pr diff for cloud agent environments).
4345
BASE=$(git merge-base upstream/main HEAD 2>/dev/null \
44-
|| git merge-base origin/main HEAD 2>/dev/null)
46+
|| git merge-base origin/main HEAD 2>/dev/null \
47+
|| git merge-base main HEAD 2>/dev/null)
4548
if [ -z "$BASE" ]; then
46-
echo "ERROR: Cannot find upstream/main or origin/main."
47-
exit 1
49+
echo "No merge-base found. Falling back to gh pr diff."
50+
gh pr diff --name-only
51+
gh pr diff -- '*.rs' '*.toml' 'examples/' | head -2000
52+
else
53+
echo "Reviewing changes since: $BASE"
54+
git diff "$BASE"..HEAD --stat
55+
git diff "$BASE"..HEAD -- '*.rs' '*.toml' 'examples/' | head -2000
4856
fi
49-
echo "Reviewing changes since: $BASE"
50-
git diff "$BASE"..HEAD --stat
51-
git diff "$BASE"..HEAD -- '*.rs' '*.toml' 'examples/' | head -2000
5257
```
5358

5459
If the diff is empty, stop and report: "No changes found to review."
@@ -106,7 +111,9 @@ Use `model: "gpt-5.4"` in the task tool call (provides model diversity).
106111
> Get the diff:
107112
> ```
108113
> BASE=$(git merge-base upstream/main HEAD 2>/dev/null \
109-
> || git merge-base origin/main HEAD 2>/dev/null)
114+
> || git merge-base origin/main HEAD 2>/dev/null \
115+
> || git merge-base main HEAD 2>/dev/null)
116+
> # If BASE is empty, use: gh pr diff -- '*.rs' '*.toml' 'examples/'
110117
> git diff "$BASE"..HEAD -- '*.rs' '*.toml' 'examples/'
111118
> ```
112119
>
@@ -161,7 +168,9 @@ Use `model: "claude-opus-4.6"` in the task tool call.
161168
> Get the diff AND read full source files for context:
162169
> ```
163170
> BASE=$(git merge-base upstream/main HEAD 2>/dev/null \
164-
> || git merge-base origin/main HEAD 2>/dev/null)
171+
> || git merge-base origin/main HEAD 2>/dev/null \
172+
> || git merge-base main HEAD 2>/dev/null)
173+
> # If BASE is empty, use: gh pr diff -- '*.rs' '*.toml' 'examples/'
165174
> git diff "$BASE"..HEAD -- '*.rs' '*.toml' 'examples/'
166175
> ```
167176
> Then use `view` to read the full source files that were changed.
@@ -219,7 +228,9 @@ Use the default model (no `model` parameter).
219228
> Get the diff:
220229
> ```
221230
> BASE=$(git merge-base upstream/main HEAD 2>/dev/null \
222-
> || git merge-base origin/main HEAD 2>/dev/null)
231+
> || git merge-base origin/main HEAD 2>/dev/null \
232+
> || git merge-base main HEAD 2>/dev/null)
233+
> # If BASE is empty, use: gh pr diff -- '*.rs' '*.toml' 'examples/'
223234
> git diff "$BASE"..HEAD -- '*.rs' '*.toml' 'examples/'
224235
> ```
225236
> Use `view` to read surrounding context.
@@ -439,7 +450,9 @@ Launch **1 general-purpose agent in background mode**.
439450
> Get the diff:
440451
> ```
441452
> BASE=$(git merge-base upstream/main HEAD 2>/dev/null \
442-
> || git merge-base origin/main HEAD 2>/dev/null)
453+
> || git merge-base origin/main HEAD 2>/dev/null \
454+
> || git merge-base main HEAD 2>/dev/null)
455+
> # If BASE is empty, use: gh pr diff -- '*.rs' '*.toml' 'examples/'
443456
> git diff "$BASE"..HEAD -- '*.rs' '*.toml' 'examples/'
444457
> ```
445458
> Use `view` to read full source files.

0 commit comments

Comments
 (0)