Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Last active September 15, 2021 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save OliverJAsh/929c761c8ecbf14d0010634a3f015740 to your computer and use it in GitHub Desktop.
Save OliverJAsh/929c761c8ecbf14d0010634a3f015740 to your computer and use it in GitHub Desktop.
git-merge-pr
#compdef git-merge-pr
_git-merge-pr () {
# Multiple lines = -l
# compadd -l -d "('foo:a b c' 'bar:d e f')" foo bar
# lines=('foo:a b c' 'bar:d e f')
# compadd -l -d lines foo bar
# lines=('foo:a b c' 'bar:d e f')
# words=('foo' 'bar')
# compadd -l -d lines -a -- words
# https://github.com/zsh-users/zsh-completions/blob/cf565254e26bb7ce03f51889e9a29953b955b1fb/zsh-completions-howto.org#writing-simple-completion-functions-using-_describe
# local -a subcmds
# subcmds=('c:description for c command' 'd:description for d command' 'e:foo')
# _describe 'command' subcmds
# Note: requires hub and jq to be installed
# https://developer.github.com/v3/pulls/#list-pull-requests
# hub api /repos/{owner}/{repo}/pulls
REPO_PATH="repos/{owner}/{repo}"
PRS_PATH="$REPO_PATH/pulls"
PRS=$(
hub api $PRS_PATH \
| jq 'map({title, number, head})'
)
PRS_DETAIL_STRING=$(
echo $PRS \
| jq --raw-output 'map((.number | tostring) + ": " + .head.ref + ", " + .title) | .[]'
)
# https://unix.stackexchange.com/questions/29724/how-to-properly-collect-an-array-of-lines-in-zsh
# TODO: avoid leaking IFS?
IFS=$'\n' PRS_DETAIL=($(echo "$PRS_DETAIL_STRING"))
_describe 'pull request' PRS_DETAIL
}
#!/bin/bash
# If a script errors, force the script to fail immediately.
set -e
# Note: requires hub and jq to be installed
# https://hub.github.com/hub-api.1.html
# https://github.com/github/hub/issues/1483#issuecomment-458182625
# https://github.com/vitorgalvao/tiny-scripts/blob/master/climergebutton
# https://gist.github.com/OliverJAsh/929c761c8ecbf14d0010634a3f015740
# https://github.com/cli/cli/issues/373
# https://github.com/cli/cli/pull/899
# `gh pr merge`
# https://github.com/cli/cli/issues/380
# https://github.com/github/hub/pull/2280
# Differences from `hub merge`:
# https://github.com/github/hub/issues/1483#issuecomment-395723161
# Delete local head branch
# Delete remote head branch
# Update/sync local base branch
# Update other PR's base branch (dependent) to this PR's base branch
# Warn about other dependent PRs
ID=$1
# https://unix.stackexchange.com/questions/225943/except-the-1st-argument/225951#225951
REST=${@:2}
REPO_PATH="repos/{owner}/{repo}"
PR_PATH="$REPO_PATH/pulls/$ID"
function merge_pr () {
echo "Merging $ID"
# https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button
hub api -XPUT $PR_PATH/merge
# TODO: ?
# echo $REST | xargs hub api -XPUT $PR_PATH/merge
}
function after () {
# https://developer.github.com/v3/pulls/#get-a-single-pull-request
RESPONSE=$(hub api $PR_PATH)
BASE_BRANCH=$(echo $RESPONSE | jq -r '.base.ref')
HEAD_BRANCH=$(echo $RESPONSE | jq -r '.head.ref')
# TODO: use correct remote, as in
# https://github.com/tj/git-extras/blob/500ea2b11981b43742b26f85ba4c43633230ff63/bin/git-delete-branch
# Why does `git config branch.$branch.remote` return nothing?
# REMOTE="origin"
# TODO: careful, could exist on other remotes?
# TODO: support no config, e.g. revert PR
REMOTE=`git config branch.$HEAD_BRANCH.remote`
echo "Updating local base branch ($BASE_BRANCH)"
git checkout $BASE_BRANCH
git pull --rebase
echo "Deleting remote head branch ($REMOTE/$HEAD_BRANCH)"
# Don't fail if remote fails
set +e
git push $REMOTE :$HEAD_BRANCH
set -e
echo "Deleting local head branch ($HEAD_BRANCH)"
git branch --delete $HEAD_BRANCH
}
merge_pr
after
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment