Cross-repo create_pull_request: rewrite create hunks as modify when target file exists
Problem
Follow-up to #17969. When the agent commits in the workflow repo but create_pull_request targets a different repo, format-patch emits new file mode 100644 hunks for paths that already exist on the target base branch. git am --3way cannot apply these (null base blob).
Proposed fix
After format-patch generation, when GITHUB_REPOSITORY != target-repo and a target checkout exists:
- Scan patch for
new file modehunks - If
origin/<base>:<path>exists in the target checkout, rebuild the hunk as a modify diff against the target blob - Apply rewritten patch with
git am --3wayon the target checkout
Patch (conceptual)
# Before (agent repo β file absent in source tree)
diff --git a/docs/guide.md b/docs/guide.md
new file mode 100644
--- /dev/null
+++ b/docs/guide.md
# After rewrite (target repo has file at origin/main)
diff --git a/docs/guide.md b/docs/guide.md
index abc1234..def5678 100644
--- a/docs/guide.md
+++ b/docs/guide.md
@@ -1 +1 @@
-old content from target
+agent edited content
External contributor PR: https://github.com/Tarekchehahde/gh-aw/pull/10
Related: #17969, #18175
Fork PR: https://github.com/Tarekchehahde/gh-aw/pull/10
Upstream compare: https://github.com/github/gh-aw/compare/main...Tarekchehahde:gh-aw:fix/17969-cross-repo-patch-modify
Local tests: npm run typecheck; vitest run git_patch_utils.test.cjs β 19/19 pass
Related: #17969
/scout investigate
π Recon complete! Scout has charted the territory. Map ready! πΊοΈ
π Scout Research Report
Triggered by @github-actions[bot]
Executive Summary
The root cause described in #44977 checks out against git's own documented behavior: git am/git apply --3way can only 3-way-merge a hunk when the patch records the identity of an existing pre-image blob. A new file mode hunk's pre-image is the null blob (0000000), so there's nothing for --3way to reconstruct an ancestor from β it can only add-add-conflict or hard-fail, never silently "just work," when the target already has that path. The proposed fix (rewrite new file hunks into real modify diffs using the target checkout's blob before git am) is the technically correct approach, and the linked fork PR (Tarekchehahde/gh-aw#10) implements it soundly, with two edge-case gaps worth fixing before merge.
Click to expand detailed findings
Research Findings
Why git am --3way can't self-heal this case
Per the [git-apply docs]((gitscm.com/redacted) --3way "attempt[s] 3-way merge if the patch records the identity of blobs it is supposed to apply to and we have those blobs available locally." A format-patch-generated "new file" hunk records index 0000000..<newsha> β the old side is intentionally the null blob, not a placeholder for "unknown." There is no ancestor to fetch even with --build-fake-ancestor, because the patch itself asserts the file didn't exist. This matches the gh-aw repo's own existing tryRecoverGitAmAddAddConflict fallback in create_pull_request.cjs, which already special-cases add/add conflicts by blindly preferring "theirs" (the patch content) β a coarser, content-blind version of the same problem. That fallback works by luck (the agent's snapshot is usually the full intended file content) but discards any target-side change history and can't repair patches that fail before ever reaching a mergeable conflict state (e.g., git am erroring out at "already exists in working directory" rather than staging an AA conflict).
Fork PR review (Tarekchehahde/gh-aw#10)
The PR adds rewriteCrossRepoCreatePatches() / buildCrossRepoModifyDiff() in git_patch_utils.cjs, wired in from generate_git_patch.cjs via a new targetTreeCwd option (populated from safe_outputs_handlers.cjs when GITHUB_REPOSITORY !== target-repo). For each new file mode block it checks whether origin/<base>:<path> exists in the target checkout; if so, it resolves both blobs via rev-parse/cat-file, diffs them with git diff --no-index, and splices a proper index <old>..<new> 100644 / ---/+++ header with real hunks back into the patch. This is the right shape and is unit-tested against a real git am --3way apply.
Two gaps found during review:
- File mode is hardcoded to
100644. The rewritten header always emits100644regardless of the originalnew file modevalue or the target blob's mode, so an executable script (100755) added/edited cross-repo would silently lose its exec bit after rewrite. The originalnew file mode <mode>line (already captured by the block regex) should be threaded through instead of being discarded. - Path parsing assumes no spaces/quoting.
pathMatchuses/^diff --git a\/(\S+) b\/(\S+)/, which doesn't handle git's quoted-path form (diff --git "a/foo bar" "b/foo bar") thatformat-patchemits for paths containing spaces or unusual characters. This repo already has a fail-closed precedent for exactly this kind of header ambiguity inmanifest_file_helpers.cjs(rejects unparseablediff --githeaders rather than guessing). Today the fork's behavior on a mismatch is to leave the block untouched (falls through to the pre-existing, still-broken create-hunk path) rather than corrupt anything β so it's a completeness gap, not a correctness regression, but worth aligning with the same parser/fail-closed convention rather than a bespoke regex.
Binary files are handled safely by omission: git diff --no-index on binary content produces "Binary files ... differ" with no @@ hunk, hunkStart comes back -1, and the function returns null, falling back to the original (still-failing) create block rather than emitting a corrupt patch.
Recommendations
- Before merging the fork's approach (or an in-repo reimplementation), thread the original file mode through to the rewritten header instead of hardcoding
100644. - Reuse this repo's existing quoted/
diff --githeader parser (frommanifest_file_helpers.cjs) rather than a new regex, so cross-repo rewriting and the existing file-protection policy checks agree on what counts as a parseable header β and so paths with spaces aren't silently skipped. - Add a test case for an executable file (
new file mode 100755) and one for a path containing a space, since neither is currently covered by the fork'sgit_patch_utils.test.cjsadditions. - Consider extending binary-file handling explicitly (e.g., detect and emit a proper binary-patch replace via
git diff --binary --no-index) rather than silently falling back to the still-broken create hunk, so binary asset updates in cross-repo PRs don't regress separately.
Key Sources
- [git-apply documentation β
--3way/--build-fake-ancestor]((gitscm.com/redacted) - Fork PR: Tarekchehahde/gh-aw#10 β rewrite cross-repo create patches as modify diffs
- Related issues: #17969 (original root-cause report), #18175 (added
--3way+ Strategy 3 cross-repo fallback) - In-repo precedent:
actions/setup/js/create_pull_request.cjs(tryRecoverGitAmAddAddConflict),actions/setup/js/manifest_file_helpers.cjs(fail-closeddiff --githeader parsing)
Suggested Next Steps
- Port the fork's
rewriteCrossRepoCreatePatchesapproach into this repo (or review the PR directly) with the file-mode and quoted-path fixes above applied. - Add regression tests for executable files and space-containing paths alongside the existing
git am --3wayintegration test. - Cross-check whether
tryRecoverGitAmAddAddConflict's blunt "prefer patch version" fallback should be narrowed/retired once true modify-diff rewriting is in place, to avoid two divergent recovery paths for the same class of failure.
π Intelligence gathered by Scout Β· agent Β· 57.9 AIC Β· β 31.3 AIC Β· β 6.6K Β· β· Comment /scout to run again
Follow-up on the two rewrite gaps:
- File mode is taken from
ls-tree(with the originalnew file modeas fallback), so executable files keep100755instead of being rewritten as100644. - Path parsing uses the shared
parseDiffGitHeaderhelper, so quoted headers and paths with spaces are rewritten instead of skipped.
Pushed to the fork PR: https://github.com/Tarekchehahde/gh-aw/pull/10
Tests: vitest run git_patch_utils.test.cjs β 21/21 pass.
The rewrite now lives on current main (file mode and quoted-path handling included).
Compare / open PR: https://github.com/github/gh-aw/compare/main...Tarekchehahde:gh-aw:fix/17969-cross-repo-patch-modify?expand=1
Branch: Tarekchehahde:fix/17969-cross-repo-patch-modify
rewriteCrossRepoCreatePatches()turns cross-reponew filehunks into modify diffs againstorigin/<base>in the target checkout.- File mode comes from
ls-tree(fallback: originalnew file mode), so executables keep100755. - Paths go through
parseDiffGitHeader, so quoted headers and paths with spaces rewrite instead of being skipped.
Tests: vitest run git_patch_utils.test.cjs in actions/setup/js β 43/43 pass, including git am --3way for a normal file, an executable, and a path with a space.
Status (2026-10-07; Contributor proposal): On August 21, the contributor reported that the patch rewrite and tests were on a fork branch and provided a comparison link to open an upstream PR. The next step is upstream review of the proposed patch and its edge cases; the fork update is not evidence of a merged fix.
Community issue refresher β 2026-10-16: Apparently resolved for the supported target-checkout flow, using a different approach than the proposed rewrite.
#68815 merged October 14. Current handler routing requires the resolved side repository's checkout even when the target comes from GH_AW_TARGET_REPO_SLUG. Patches/bundles are generated there, so an existing target file has its actual target preimage instead of a null βnew fileβ base.
The real-Git regressions cover both transports, default/release bases, application/import, and missing checkouts. The fork rewrite PR remains open, but is not the upstream solution.
Important scope boundary: current documentation explicitly does not support copying target files into the workflow checkout as a substitute. If that exact source-copy behavior is still desired, it remains a distinct feature request. This assessment reads merged implementation/tests; it is not a fresh cross-repository runtime verification.