Skip to content

Instantly share code, notes, and snippets.

@pokey
Last active September 24, 2022 18:04
Show Gist options
  • Save pokey/7e1d21f3d3c3f0a700c3b07ef1aff20e to your computer and use it in GitHub Desktop.
Save pokey/7e1d21f3d3c3f0a700c3b07ef1aff20e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Given a commit sha (defaults to current commit), creates PRs for every
# branch on the stack of the given commit. Each PR is created with its base
# as the nearest ancestor of the given branch that is also a branch.
set -euo pipefail
commit="${1:-.}"
base="$(git config branchless.core.mainBranch)"
remote="origin"
git-branchless submit --create "stack(\"$commit\")"
# FIXME: Remove `| tac` once
# https://github.com/arxanas/git-branchless/issues/554 is fixed
branches="$(git-branchless query -b "stack(\"$commit\")" | tac)"
while read -r branch <&3; do
existing_pr="$(gh pr view "$branch" --json url -t "{{.url}}" || echo -n "")"
if [ -n "$existing_pr" ]; then
echo "PR $existing_pr already exists for branch $branch"
continue
fi
ancestor_query="ancestors(parents(\"$branch\")) & branches() & draft()"
parent_branch=$(git-branchless query -b "$ancestor_query - ancestors(parents($ancestor_query))")
if [ -z "$parent_branch" ]; then
parent_branch="$base"
else
if [[ $parent_branch =~ $'\n' ]]; then
echo "ERROR: Branch $branch has more than one parent branch: $parent_branch"
exit 1
fi
gh pr view "$parent_branch" --json url -t "Depends on {{.url}}" | pbcopy
echo "Copied dependency message to clipboard for use in PR body"
fi
gh pr create \
--draft \
--base "$parent_branch" \
--head "$branch"
done 3<<<"$branches"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment