Created
October 27, 2017 03:04
-
-
Save danielwhite/fb0a60509ffcf17e01508ed1d027b28b to your computer and use it in GitHub Desktop.
List open GitHub PR branches for merging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This script finds incoming PR branches from a GitHub based remote. | |
# | |
# Example | |
# | |
# $ git checkout -b incoming origin/master | |
# $ git incoming origin | xargs git merge | |
# | |
if [ $# != 1 ]; then | |
>&2 echo "Usage: git incoming <remote>" | |
exit 1 | |
fi | |
remote="${1}" | |
fetch_refs="+refs/pull/*/head:refs/remotes/${remote}/pr/*" | |
function configure() { | |
# Check if already configured. | |
git config -l | grep -Fq "remote.${remote}.fetch=${fetch_refs}" | |
if [ $? -eq 0 ]; then | |
return | |
fi | |
git config --add "remote.${remote}.fetch" "${fetch_refs}" | |
} | |
function owner_repo() { | |
# FIXME: Handle case where this does not match. | |
git config "remote.${remote}.url" | sed -n 's|^[email protected]:\(.*\)\.git$|\1|p' | |
} | |
function gh_get() { | |
curl --show-error --silent \ | |
-H "Authorization: token ${GITHUB_API_TOKEN}" \ | |
--url "https://api.github.com/${1}" | |
} | |
function open_prs() { | |
gh_get "repos/$(owner_repo)/pulls?state=open&sort=updated&direction=desc&base=master" | jq '.[] | .number' | |
} | |
# Ensure PR branches are available. | |
configure | |
git fetch origin -q --prune | |
# For each open PR, print the remote branch name. | |
open_prs | while read line; do echo "${remote}/pr/${line}"; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just a clumsy utility to help me with a common operation. Namely: create a branch that works on the merged results of open PRs.