Skip to content

Add --push option to forge pr create - #156

Merged
andrew merged 4 commits into
git-pkgs:mainfrom
abhinavgautam01:feat/pr-create-push
Aug 28, 2026
Merged

Add --push option to forge pr create#156
andrew merged 4 commits into
git-pkgs:mainfrom
abhinavgautam01:feat/pr-create-push

Conversation

@abhinavgautam01

Copy link
Copy Markdown
Contributor

Closes #152

Summary

Add an opt-in --push flag to forge pr create, allowing the command to push the specified head branch before creating the pull request.

Changes

  • Add --push to forge pr create
  • Push the local head branch to the selected Git remote
  • Configure upstream tracking for the pushed branch
  • Stop PR creation and return a clear error when the push fails
  • Add a reusable git.PushBranch helper
  • Add tests using a temporary bare Git repository

Usage

forge pr create \
  --title "My pull request" \
  --head feature-branch \
  --push

The remote defaults to origin and respects the existing global --remote option.

Testing

  • go build ./...
  • go test -race ./...
  • go tool golangci-lint run ./...
  • git diff --check

All checks pass.

Copilot AI 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.

Pull request overview

Adds an opt-in --push flag to forge pr create, pushing the head branch and configuring upstream tracking before PR creation.

Changes:

  • Adds reusable git.PushBranch functionality.
  • Handles push failures.
  • Adds Git and CLI tests.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/git/git.go Implements branch pushing.
internal/git/git_test.go Tests push behavior and validation.
internal/cli/pr.go Integrates the --push flag.
internal/cli/pr_test.go Tests successful and failed pushes.
internal/cli/pr_checkout_test.go Extends the PR service mock.
Suppressed comments (2)

internal/cli/pr.go:305

  • --head is also allowed to be a remote-qualified value such as owner:feature for GitHub (the value is passed directly to github.NewPullRequest.Head in github/prs.go:199). Passing that whole value to PushBranch constructs refs/heads/owner:feature, which is not a valid local Git ref, so forge pr create --head owner:feature --push always fails before the PR can be created. Extract the local branch portion for the push while retaining the original qualified value in CreatePROpts.Head.
				if err := git.PushBranch(cmd.Context(), "", resolve.RemoteName(), flagHead); err != nil {

internal/cli/pr.go:305

  • This pushes to the selected remote but still sends the bare flagHead to the forge API. That breaks fork PRs when the remote's push target differs from the repository being resolved (for example -R upstream/repo --remote fork, or a remote with a pushurl): GitHub requires the head as <fork-owner>:<branch>, so the API looks for feature in the upstream repo even though it was just pushed to the fork. Derive the local branch and pushed remote owner and pass the qualified head, or explicitly reject this cross-repository combination.
				if err := git.PushBranch(cmd.Context(), "", resolve.RemoteName(), flagHead); err != nil {

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Addressed both suppressed findings. Qualified heads now push only the local branch while preserving owner:branch for the API. Push-remote ownership is resolved from the push URL, with clear validation for fork and mismatched-owner cases.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting changes for two fork-handling issues:

  • OWNER:BRANCH only supplies usable source metadata to GitHub and Gitea. GitLab receives it as a literal source_branch, while Bitbucket receives it as source.branch.name without a source repository, so the push succeeds before PR creation fails.
  • Requiring the push repository name to match the base repository rejects renamed GitHub forks, although GitHub identifies a cross-repository head by its owner and branch within the fork network.

Please add provider-specific source repository handling or reject unsupported fork pushes, relax or replace the repository-name check for qualified GitHub heads, and add regression coverage for both cases.

@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Addressed both issues. Qualified fork heads are now capability-gated to GitHub and Gitea, while unsupported providers reject fork pushes before pushing. Qualified heads validate by domain and owner without requiring matching repository names, so renamed forks are supported. Regression tests, race tests, build and lint all pass.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • internal/git/git.go:145: With an upstream fetch URL and fork push URL, --set-upstream records the branch against the upstream fetch namespace. A later git fetch --prune removes that remote-tracking branch and leaves the local branch upstream marked gone. Preserve existing tracking when fetch and push repositories differ, and add a regression test.

  • internal/cli/pr.go:394: Qualified heads skip repository validation. Targeting acme/base with a push remote for acme/other and --head acme:feature pushes to other, while the API resolves the head against base. Retain the repository-name check when the head owner equals the target owner, while allowing renamed cross-owner forks.

@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Thanks, Addressed both issues. PushBranch now preserves existing tracking when fetch and push repositories differ, with regression coverage through fetch --prune. Qualified same-owner heads retain repository-name validation, while renamed cross-owner forks remain supported. Build, race tests and lint all pass.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The current head addresses the requested fork validation and tracking fixes.

@andrew
andrew merged commit 74fdc4a into git-pkgs:main Aug 28, 2026
4 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.

Feature Request: allow forge pr create to automatically push the branch

3 participants