clan-core/pkgs/tea-create-pr/script.sh

65 lines
1.6 KiB
Bash
Raw Normal View History

2023-07-28 12:11:19 +00:00
#!/usr/bin/env bash
set -euo pipefail
remoteFork="${1:-origin}"
remoteUpstream="${2:-upstream}"
targetBranch="${3:-main}"
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)"
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"
root_dir=$(git rev-parse --show-toplevel)
# 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
treefmt --fail-on-change -C "$root_dir"
upstream_url=$(git remote get-url "$remoteUpstream")
set -x
git fetch "$remoteUpstream" && git rebase "$remoteUpstream"/main --autostash
set +x
repo=$(echo "$upstream_url" | sed -E 's#.*:([^/]+/[^.]+)\.git#\1#')
2023-07-28 12:11:19 +00:00
git log --reverse --pretty="format:%s%n%n%b%n%n" "$remoteUpstream/$targetBranch..HEAD" > "$TMPDIR"/commit-msg
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
git push --force -u "$remoteFork" HEAD:refs/heads/"$tempRemoteBranch"
2023-07-28 12:11:19 +00:00
2023-07-28 12:11:19 +00:00
tea pr create \
--repo "$repo" \
--head "$user:$tempRemoteBranch" \
2023-07-28 12:11:19 +00:00
--title "$firstLine" \
--description "$rest" \
"$@"