---
description: Create a GitHub PR for current changes using a repository URL and optional auth env file
argument-hint: "[repo-url] [auth-env-file]"
---

Create a GitHub pull request for the changes in the current workspace, targeting the GitHub repository at ${1:-the origin remote (or a repository you will be asked for)}. Install the GitHub CLI (`gh`) if it is missing, and handle both existing git repos and brand-new projects.

## Arguments

- `$1` — GitHub repository URL (e.g. `https://github.com/octocat/repo`, `git@github.com:octocat/repo.git`, or `owner/repo`). If empty, fall back to the existing `origin` remote; if there is none, ask the user.
- `$2` — optional env file with GitHub login details (see **Authentication**).

## Steps

1. **Check for GitHub CLI**.
   - Run `bash` commands to check `gh --version` or `which gh`.
   - If `gh` is present, note the version and continue.
   - If `gh` is missing, install it:
     - **macOS / Homebrew**: `brew install gh`
     - **Windows / winget**: `winget install --id GitHub.cli`
     - **Windows / chocolatey**: `choco install gh`
     - **Ubuntu / Debian**: `sudo apt update && sudo apt install -y gh`
     - **Fedora / RHEL**: `sudo dnf install -y gh`
   - After installing, verify with `gh --version`.

2. **Authenticate `gh`**.

### Preferred: auth env file (`$2`)

If `$2` was provided:

- Verify the file exists before use; if it is missing, report that and fall back to existing `gh` authentication.
- The env file should contain variables such as:
  ```bash
  GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxx
  GITHUB_USERNAME=octocat
  GIT_USER_NAME=Octo Cat
  GIT_USER_EMAIL=octocat@example.com
  ```
  `GITHUB_TOKEN` (a personal access token with repo scope) is required; the remaining variables are used for git identity.
- Load the file and authenticate in a single bash command, and **do not echo, log, or persist the values anywhere**:
  ```bash
  set -a; . <auth-env-file>; set +a
  printf '%s' "$GITHUB_TOKEN" | gh auth login --with-token
  gh auth setup-git
  gh auth status
  ```
- `gh auth setup-git` configures git's credential helper so `git push` over HTTPS works without embedding the token in remote URLs.
- Never print the token, never write it into git config or remote URLs, and never commit the env file. Make sure it is listed in `.gitignore` if it lives inside the workspace.

### Fallback: existing `gh` authentication

If no env file was provided:

- Run `gh auth status`.
- If not authenticated, ask the user to run `gh auth login` manually (do not paste tokens into chat). Do not proceed until authenticated.

3. **Resolve the target repository**.
   - Normalize `$1` to `owner/repo` (accept `https://github.com/owner/repo.git`, `git@github.com:owner/repo.git`, or `owner/repo`).
   - If `$1` is empty, use the existing `origin` remote (`git remote get-url origin`).
   - If neither exists, ask the user for the repository URL before proceeding.
   - Determine the default (base) branch of the target repository:
     ```bash
     gh repo view <owner>/<repo> --json defaultBranchRef --jq .defaultBranchRef.name
     ```

4. **Detect git project state**.
   - Check whether `.git` exists in the current workspace.

### A. Existing git project

If `.git` exists:

1. **Inspect changes**.
   - Run `git status --short` to see modified/untracked files.
   - Show the user a concise summary of the changes.

2. **Point the remote at the target repository**.
   - If `$1` was provided:
     - If no `origin` remote exists, run `git remote add origin <repo-url>`.
     - If `origin` points elsewhere, run `git remote set-url origin <repo-url>` (tell the user before changing it).
   - If `$1` is empty, keep the existing `origin` as-is.

3. **Prepare a branch**.
   - Get the current branch with `git branch --show-current`.
   - If currently on the default branch (`main`/`master`), create and switch to a feature branch:
     ```bash
     git checkout -b update/<short-description>
     ```
   - Use a descriptive branch name based on the changes.

4. **Configure git identity if needed**.
   - Run `git config user.name` and `git config user.email`.
   - If either is missing and an auth env file was provided, source it in a single bash command and set the identity locally from `GIT_USER_NAME`/`GIT_USER_EMAIL` (fall back to `GITHUB_USERNAME` for the name):
     ```bash
     set -a; . <auth-env-file>; set +a
     git config user.name "${GIT_USER_NAME:-$GITHUB_USERNAME}"
     git config user.email "$GIT_USER_EMAIL"
     ```
   - Otherwise, ask the user for a name and email, then set them locally:
     ```bash
     git config user.name "Name"
     git config user.email "email@example.com"
     ```

5. **Commit and push**.
   - Stage the relevant changes with `git add`.
   - Commit with a clear message. Ask the user for a message if one is not obvious, or use a sensible default summarizing the changes.
   - Push the branch:
     ```bash
     git push -u origin <branch-name>
     ```

6. **Create the pull request**.
   - Use `gh pr create` with non-interactive flags against the target repository:
     ```bash
     gh pr create --repo <owner>/<repo> --title "<title>" --body "<body>" --base <base-branch>
     ```
   - Derive the PR title and body from the commit message and changed files.
   - If a PR already exists for this branch, run `gh pr view <branch> --repo <owner>/<repo>` and report the URL instead.

### B. New project without git

If `.git` does not exist:

1. **Resolve the repository**.
   - If `$1` was provided, use its `owner/repo` (do not ask the user).
     - Check whether the repository already exists with `gh repo view <owner>/<repo>`.
     - If it does not exist, ask whether it should be public or private, then create it:
       ```bash
       gh repo create <owner>/<repo> --<public|private>
       ```
     - Ask the user before creating a public repo.
   - If `$1` is empty, **ask the user** for:
     - The desired GitHub repository name (or full URL).
     - Whether it should be public or private.
     - The GitHub owner/organization (default to the authenticated user).
     - Then create the remote repository:
       ```bash
       gh repo create <owner>/<repo> --<public|private>
       ```

2. **Initialize git and the remote**.
   - Run:
     ```bash
     git init -b main
     git remote add origin <repo-url>
     ```
   - If `main` is not the desired default branch name, use `master` or ask the user.

3. **Configure git identity**.
   - Ask for name and email if not already set globally, or use `GIT_USER_NAME`/`GIT_USER_EMAIL` (falling back to `GITHUB_USERNAME`) from the auth env file when provided (see step 4.4 in section A).

4. **Commit the current code**.
   - Run:
     ```bash
     git add .
     git commit -m "Initial commit"
     git push -u origin main
     ```

5. **Report the repository URL**.

## Rules

- Never paste credentials or tokens into the chat; never echo, log, or print the auth env file contents.
- Never write the token into remote URLs or git config.
- Do not run interactive `gh auth login` automatically; use the auth env file when provided, otherwise prompt the user to authenticate manually.
- Ensure the auth env file is listed in `.gitignore` and never committed; suggest deleting it when the session is complete.
- Ask the user before creating a public repo if privacy matters.
- Do not overwrite or ignore local git configuration without asking.
- If there are no changes to commit, report that and stop.
- Keep the PR title/body concise and relevant to the actual changes.

After completing, summarize:
- Whether `gh` was installed or already present.
- How authentication was handled (auth env file or existing `gh` login).
- The repository and branch used.
- The commit message and PR URL (if created).
- Any errors or manual steps the user still needs to take.
