Skip to content

Instantly share code, notes, and snippets.

@kateinoigakukun
Forked from gottesmm/git-pr-commit
Created March 14, 2022 06:29
Show Gist options
  • Save kateinoigakukun/33acba780c8f5e538fc21494b34fe9db to your computer and use it in GitHub Desktop.
Save kateinoigakukun/33acba780c8f5e538fc21494b34fe9db to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
if [[ $# -lt 1 ]]; then
echo "Not enough arguments?!"
echo "$0 <hash1> ... <hashN>"
exit 1
fi
HASHES=( ${@:1} )
echo "Checking if hashes exist."
for h in "${HASHES[@]}"; do
echo -n " "
if git cat-file -e ${h}^{commit}; then
echo "Good hash: ${h}"
else
echo "Bad hash: $HASH?!"
exit 1
fi
done
# We name the branch after the last hash.
LAST_HASH=$(($# - 1))
BRANCH_NAME="pr-${HASHES[LAST_HASH]}"
OLD_BRANCH=$(git rev-parse --abbrev-ref HEAD)
git branch -f "$BRANCH_NAME" origin/master
git checkout "$BRANCH_NAME"
for h in "${HASHES[@]}"; do
if ! git cherry-pick "${h}"; then
exit 1
fi
done
git push self -f "$BRANCH_NAME:$BRANCH_NAME"
git checkout "$OLD_BRANCH"
git branch -D "$BRANCH_NAME"
set +e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment