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

56 lines
1.4 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}"
2023-07-28 12:11:19 +00:00
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="$(git config --get remote.origin.url | awk -F'[@:/]' '{print $3}' | tr -d '\n')"
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
upstream_url=$(git remote get-url "$remoteUpstream")
repo=$(echo "$upstream_url" | sed -E 's#.*:([^/]+/[^.]+)\.git#\1#')
2023-07-28 12:11:19 +00:00
treefmt -C "$root_dir"
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
tea pr create \
--repo "$repo" \
--head "$user:$tempRemoteBranch" \
2023-07-28 12:11:19 +00:00
--title "$firstLine" \
--description "$rest" \
"$@"