Skip to content

Instantly share code, notes, and snippets.

@danielwhite
Created October 27, 2017 03:04
Show Gist options
  • Save danielwhite/fb0a60509ffcf17e01508ed1d027b28b to your computer and use it in GitHub Desktop.
Save danielwhite/fb0a60509ffcf17e01508ed1d027b28b to your computer and use it in GitHub Desktop.
List open GitHub PR branches for merging
#!/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|^git@github.com:\(.*\)\.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
@danielwhite
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment