github / github/gh-aw

Cross-repo create_pull_request: rewrite create hunks as modify when target file exists

Open
#44,977 6 comments 0 reactions 0 assignees View on GitHub
bug community scout workflows
Dominant language
Go
Stars
5.1k
Forks
541
Avg merge
5h 48m
Merged PRs (30d)
773

Description

## 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:

1. Scan patch for `new file mode` hunks
2. If `origin/:` exists in the target checkout, rebuild the hunk as a modify diff against the target blob
3. Apply rewritten patch with `git am --3way` on the target checkout

## Patch (conceptual)

```diff
# 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

Contributor guide

Open the contributing guide

Research direction

Start at the create_pull_request implementation and trace cross-repository format-patch generation and the target checkout path. Verify the proposed handling for new file mode hunks against origin/:, then confirm the rewritten patch applies with git am --3way; the external contributor PR is the reference for work already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
git, github-actions, go
Domain
ci-cd, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.