Skip to content

Instantly share code, notes, and snippets.

@MikeRatcliffe
Last active September 26, 2024 07:34
Show Gist options
  • Save MikeRatcliffe/54b0ee14381ef4f5f9064e1c5f0b20de to your computer and use it in GitHub Desktop.
Save MikeRatcliffe/54b0ee14381ef4f5f9064e1c5f0b20de to your computer and use it in GitHub Desktop.
#!/bin/bash
gt bottom > /dev/null
# Check for Graphite CLI
if [ ! -x "$(command -v gt)" ]; then
echo "This alias requires Graphite CLI (gt), which is not installed. Please install it using the instructions at https://graphite.dev/docs/install-the-cli"
exit 0
fi
# Check for GitHub CLI
if [ ! -x "$(command -v gh)" ]; then
echo "This alias requires GitHub CLI (gh), which is not installed. Please install it using the instructions at https://cli.github.com/"
exit 0
fi
# Check that repository is forked
if [ $(gh repo view --json isFork --jq .isFork) != "true" ]; then
echo "This command can only be run on a fork of a repository. To submit stacks to a repository directly you need to use 'gt submit'"
exit 0
fi
# Check for upstream remote
git remote get-url upstream > /dev/null
if [ $? != 0 ] ; then
echo "To use this command you need to configure an upstream remote. To do this, use 'git remote add upstream https://github.com/someuser/reponame.git', where this is the URL of the original repository that you have forked."
exit 0
fi
# Get branch names
branches=$(gt ls --stack | cat | awk -F" " '{print $2}' | sed '/main/d' | sed '/master/d')
# Check that the current branch is part of a stack
if [ ! "$branches" ]; then
echo "To use this command you must first check out a branch that is part of a stack."
exit 0
fi
# Push commits and create pull requests
for branch in $(echo "$branches"); do
git switch $branch
git push --force-with-lease -u origin $branch
gh pr create --fill
done
# Get commitids
commitids=$(git log --graph --all --oneline --boundary --ancestry-path ...HEAD --pretty=format:"%h" | awk -F' ' '{print $2}')
# Generate stack text
stack=""
pushids=""
for commitid in $(echo "$commitids"); do
pushid=$(git ls-remote upstream 'pull/*/head' | grep -e "$commitid" | awk -F/ '{print $3}')
pushids="$pushids"$'\n'"$pushid"
stack="$stack"$'\n'"* **#$pushid**"
done
# Loop through the commits and stack messages to PR
for pushid in $(echo "$pushids"); do
currstack=$(echo "$stack" | sed "s/\* \*\*#$pushid\*\*/* **#$pushid** 👈/")
message="This is one pull request in a stack... they are all based on top of each other, so the order is important."
message="$message"$'\n'
message="$message"$'\n'"The advantage of this approach is that you are guaranteed no conflicts and reviews should be shorter."
message="$message"$'\n'
message="$message"$'\n'"Review each patch and merge before you move on to the next, this way, nothing will break and the code will be easier to review."
message="$message"$'\n'
message="$message"$'\n'"Start from the bottom (above main) and work your way up the stack until you reach the top."
message="$message"$'\n'"$currstack"
msgguid="7A11E7238A7046D7B37B9070B5D0A4A2"
echo "$message" > $msgguid
gh pr comment $pushid --body-file $msgguid
rm $msgguid
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment