Skip to content

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

Open
abhinavgautam01 wants to merge 2 commits into
git-pkgs:mainfrom
abhinavgautam01:feat/pr-create-push
Open

Add --push option to forge pr create#156
abhinavgautam01 wants to merge 2 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.

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

2 participants