#!/usr/bin/env bash
set -e
: "${TMPDIR:=/tmp}"
if [ -n "$1" ] && ! [[ "$1" = --* ]]; then
base="$1"
shift
else
base="$(git-master-name)"
fi
owner="$(gh repo view --json owner --jq .owner.login)"
name="$(gh repo view --json name --jq .name)"
branch="$(git branch --show-current)"
headref="$owner:$branch"
output="$(mktemp)"
if [ "$(git log --oneline "$base".."$branch" | wc -l)" != 1 ]; then
echo "automatic PR creation should only be used when there is one commit" >&2
exit 1
fi
mkdir -p "$TMPDIR/gh-pr-create"
output="$(mktemp -p "$TMPDIR/gh-pr-create")"
git push -u origin "$branch"
gh pr create \
--repo "$owner/$name" --fill --base "$base" --head "$headref" "$@" \
| tee "$output"
tail -n1 < "$output" | xcp