2023-07-28 12:11:19 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
2024-05-30 20:25:25 +00:00
|
|
|
remoteFork="${1:-origin}"
|
|
|
|
remoteUpstream="${2:-upstream}"
|
|
|
|
targetBranch="${3:-main}"
|
2024-05-30 21:37:53 +00:00
|
|
|
shift && shift && shift
|
2023-07-28 16:14:43 +00:00
|
|
|
TMPDIR="$(mktemp -d)"
|
2023-07-28 12:11:19 +00:00
|
|
|
currentBranch="$(git rev-parse --abbrev-ref HEAD)"
|
2024-05-30 21:29:59 +00:00
|
|
|
user_unparsed="$(tea whoami)"
|
|
|
|
user="$(echo "$user_unparsed" | tr -d '\n' | cut -f4 -d' ')"
|
2023-07-28 12:11:19 +00:00
|
|
|
tempRemoteBranch="$user-$currentBranch"
|
2024-05-30 20:25:25 +00:00
|
|
|
root_dir=$(git rev-parse --show-toplevel)
|
|
|
|
|
2024-05-30 21:29:59 +00:00
|
|
|
|
2024-05-30 20:25:25 +00:00
|
|
|
# Function to check if a remote exists
|
|
|
|
check_remote() {
|
|
|
|
if git remote get-url "$1" > /dev/null 2>&1; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if the remote 'upstream' is defined
|
|
|
|
if ! check_remote "$remoteUpstream"; then
|
|
|
|
echo "Error: $remoteUpstream remote is not defined."
|
|
|
|
echo "Please fork the repository and add the $remoteUpstream remote."
|
|
|
|
echo "$ git remote add $remoteUpstream <upstream-url>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-06-02 07:53:24 +00:00
|
|
|
treefmt --fail-on-change -C "$root_dir"
|
2024-05-30 21:58:02 +00:00
|
|
|
|
2024-05-30 20:25:25 +00:00
|
|
|
upstream_url=$(git remote get-url "$remoteUpstream")
|
2024-05-30 21:58:02 +00:00
|
|
|
set -x
|
|
|
|
git fetch "$remoteUpstream" && git rebase "$remoteUpstream"/main --autostash
|
|
|
|
set +x
|
2024-05-30 20:25:25 +00:00
|
|
|
repo=$(echo "$upstream_url" | sed -E 's#.*:([^/]+/[^.]+)\.git#\1#')
|
2023-07-28 12:11:19 +00:00
|
|
|
|
2024-05-30 21:58:02 +00:00
|
|
|
|
2024-05-30 20:25:25 +00:00
|
|
|
git log --reverse --pretty="format:%s%n%n%b%n%n" "$remoteUpstream/$targetBranch..HEAD" > "$TMPDIR"/commit-msg
|
2023-09-13 13:48:34 +00:00
|
|
|
|
2023-07-28 12:11:19 +00:00
|
|
|
|
|
|
|
$EDITOR "$TMPDIR"/commit-msg
|
|
|
|
|
|
|
|
COMMIT_MSG=$(cat "$TMPDIR"/commit-msg)
|
|
|
|
|
|
|
|
firstLine=$(echo "$COMMIT_MSG" | head -n1)
|
|
|
|
rest=$(echo "$COMMIT_MSG" | tail -n+2)
|
|
|
|
|
|
|
|
if [[ "$firstLine" == "$rest" ]]; then
|
|
|
|
rest=""
|
|
|
|
fi
|
2024-05-30 20:25:25 +00:00
|
|
|
|
|
|
|
git push --force -u "$remoteFork" HEAD:refs/heads/"$tempRemoteBranch"
|
2023-07-28 12:11:19 +00:00
|
|
|
|
2024-05-30 21:58:02 +00:00
|
|
|
|
2023-07-28 12:11:19 +00:00
|
|
|
tea pr create \
|
2024-05-30 20:25:25 +00:00
|
|
|
--repo "$repo" \
|
|
|
|
--head "$user:$tempRemoteBranch" \
|
2023-07-28 12:11:19 +00:00
|
|
|
--title "$firstLine" \
|
|
|
|
--description "$rest" \
|
2024-06-02 07:53:24 +00:00
|
|
|
"$@"
|