Skip to content

Instantly share code, notes, and snippets.

@hansode
Last active December 21, 2015 13:49
Show Gist options
  • Select an option

  • Save hansode/6315582 to your computer and use it in GitHub Desktop.

Select an option

Save hansode/6315582 to your computer and use it in GitHub Desktop.

Sample Output

$ ./ls_pr_branch_map.sh https://github.com/axsh/wakame-vdc.git
0e111a6bca3f74c00a70a365a1bf9e6676c2bbf5 pull/208   release-v13.06.0
1536ba97914356c9bf41beec0fcd1f96a8f2d63b pull/85    feature-demodata-extra-nodes
325d523c315a5d4cc0cb3e23ffd1218fcf5aed2a pull/191   feature-lb-enhancements
3a5005c07a9e6227e275f2b7273f51901867c76e pull/210   bug-issue-209
5e99e1b5d84d4cde2cf22e19866d1458310c75cd pull/149   feature-ip-lease-retention
85916e5ca8481a000db92facd23aad3549b32145 pull/115   bug-ng-scheduler
9fd282ed24f95cb6b1143fd5b4f20e656f656527 pull/234   rewrite-netfilter
cab97ee425b12003ddc43faa65cdf4ff10107991 pull/84    fix-lb-image
d5fca9a621a61dfbb90c17bf53038e55073ce4a9 pull/251   feature-volume-localstore
ddb9e7e90fac29646818d43a9784b60b9bceded5 pull/60    fix_instance_generation
e08148d23f43cf525a949cf85c4bad300a419c42 pull/35    mac_range_config_default
e315515a8cb6d860eeb306b51d016d8665739884 pull/229   bugfix-lb-on-after-commit
ec666a3d40defc4093f3fec05a7068d74880111b pull/253   feature-wakame-init-extra-hosts
#!/bin/bash
#
# requires:
# bash
#
set -e
# for test
function _git() {
cat $(basename ${2##*/})
}
function ls_pr_branch_map() {
local git_repo_uri=$1
[[ -n "${git_repo_uri}" ]] || { echo "[error] git_repo_uri is empty." >&2; }
# git ls-remote [uri]
#
# * pull-req: refs/pull/[0-9]/head
# * branch: refs/heads/.*
#
local ls_heads="$(git ls-remote ${git_repo_uri} | egrep '^[0-9a-f]{40}[[:space:]]+refs/pull/[0-9]+/head$|^[0-9a-f]{40}[[:space:]]+refs/heads/.*$')"
local commit_hash pair
while read commit_hash; do
pair=$(
# normalize
# - refs/pull/[0-9]/head -> pull/[0-9]
# - refs/heads/.* -> .*
#
echo "${ls_heads}" \
| egrep -w "^${commit_hash}" \
| awk '{print $2}' \
| sed 's,^refs/,,; s,^heads/,,; s,/head$,,' \
| xargs echo \
| awk '$1 !~ "^pull/" {printf "%-10s %s\n", $2, $1}'
)
[[ -n "${pair}" ]] || continue
echo "${commit_hash} ${pair}"
done < <(
# list duplicated commit hash.
echo "${ls_heads}" \
| awk '{print $1}' \
| sort \
| uniq -c \
| awk '$1 != 1 {print $2}'
)
}
ls_pr_branch_map "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment