summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <daniel.silverstone@codethink.co.uk>2012-05-22 17:31:51 +0100
committerDaniel Silverstone <daniel.silverstone@codethink.co.uk>2012-05-22 17:31:51 +0100
commitb830b1879c21269177eeac2bd3f7c88f57a7f9a5 (patch)
tree25e5d65192366a46c87b0be6ab12a5dee4fd1c87
parent149f1fa43e61182559391712c1baf57d59be0671 (diff)
downloadgit-pull-request-b830b1879c21269177eeac2bd3f7c88f57a7f9a5.tar.bz2
list
-rwxr-xr-xgit-pull-request.sh169
1 files changed, 169 insertions, 0 deletions
diff --git a/git-pull-request.sh b/git-pull-request.sh
new file mode 100755
index 0000000..b3eb9df
--- /dev/null
+++ b/git-pull-request.sh
@@ -0,0 +1,169 @@
+#!/bin/bash
+# Note, yes, this is a bash script, don't expect it to work with dash or zsh
+
+SUBDIRECTORY_OK=Yes
+OPTIONS_KEEPDASHDASH=Yes
+OPTIONS_SPEC="\
+git pull-request [options] <cmd> [args]
+
+Available sub-commands are:
+
+list List pull requests along with their status
+create Create a new pull request
+merge Merge a given pull request into your current branch
+needs-work Indicate a pull request needs additional work before merging
+reject Reject a pull request
+archive Archive a pull request
+
+--
+ General Options
+q,quiet Be less verbose
+g,git Show the git commands being run behind the curtain
+
+ List Options
+H,heads Show the non-archived pull requests
+A,archive Show the archived pull requests
+p,pattern= Specify a regexp to match for which pull requests to show
+"
+
+. git-sh-setup
+
+# Cmdline options
+# GIT_QUIET defined in git-sh-setup and is for 'say'
+# GIT_SHOWCMD if 1 will show any sub-executed git commands
+GIT_SHOWCMD=0
+
+while test $# != 0
+do
+ case "$1" in
+ -q) GIT_QUIET=1 ;;
+ -g) GIT_SHOWCMD=1 ;;
+ -H) SHOW_HEADS=1 ;;
+ -A) SHOW_ARCHIVE=1 ;;
+ -p) SHOW_PATTERN="$2"; shift ;;
+ --) shift; break;;
+ *) usage ;;
+ esac
+ shift
+done
+
+SUBCMD="$1"
+shift
+
+if test "x$SUBCMD" = "x"; then
+ SUBCMD="list"
+fi
+
+#############################################################################
+## Utility functions ######################################################
+#############################################################################
+
+rgit ()
+{
+ test "x$GIT_SHOWCMD" = "x1" && echo >&2 "+ git $*"
+ git "$@"
+}
+
+find_requests ()
+{
+ local SUB
+ SUB="$1"
+ test "x$SUB" = "x" && SUB="heads"
+ rgit show-ref | sane_grep "refs/pull-requests/${SUB}/.*__meta\$" | \
+ sed -e 's!.* refs/pull-requests/'"${SUB}"'/!!' | sed -e 's/__meta$//'
+}
+
+
+load_meta_from ()
+{
+ local SHA
+ SHA="$1"
+
+ unset -v METADATA
+ declare -g -A METADATA
+ METADATA[META_SHA]="$SHA"
+
+ local METATEXT
+ METATEXT=$(rgit cat-file -p "$SHA":pull-request.meta | \
+ sed -e's/=/ /')
+
+ if test "x$METATEXT" = "x"; then
+ echo >&2 Unable to read metadata from "$SHA":pull-request.meta
+ exit 128
+ fi
+
+ while read KEY VALUE; do
+ METADATA["${KEY}"]="${VALUE}"
+ done <<<"${METATEXT}"
+
+ local BAD
+ for REQ in STATE COMMIT ONTOP LANDINTO; do
+ if test "x${METADATA[${REQ}]}" = "x"; then
+ echo >&2 "Reading ${SHA}:pull-request.meta: Metadata lacks $REQ"
+ BAD=1
+ fi
+ done
+ test "x$BAD" = "x" || exit 1
+}
+
+first_commit_on ()
+{
+ rgit rev-list --reverse "$1" | head -n 1
+}
+
+summarise_meta_at ()
+{
+ local PR SHA BRANCH
+ PR="$1"
+ SHA="$2"
+ BRANCH="$3"
+
+ load_meta_from "$SHA"
+
+ echo -ne "${PR}\t${METADATA[STATE]}\t"
+ echo $(rgit log --pretty="format:%s" $(first_commit_on "$SHA"))
+}
+
+summarise_meta ()
+{
+ local COMMIT BRANCH
+ while read COMMIT BRANCH; do
+ summarise_meta_at "$1" "$COMMIT" "$BRANCH"
+ done <<<$(rgit show-ref "$1"__meta)
+}
+
+#############################################################################
+## Command implementations ################################################
+#############################################################################
+
+cmd_list () {
+ test "x$SHOW_HEADS$SHOW_ARCHIVE" = "x" && SHOW_HEADS=1
+ local GREP
+ test "x$SHOW_PATTERN" = "x" || GREP="| sane_grep '$SHOW_PATTERN'"
+ local META_HEADS META_ARCHIVE
+ test "x$SHOW_HEADS" = "x1" && META_HEADS=$(find_requests heads ${GREP})
+ test "x$SHOW_ARCHIVE" = "x1" && META_ARCHIVE=$(find_requests archive ${GREP})
+
+ METAS="${META_HEADS} ${META_ARCHIVE}"
+ if test "x$METAS" = "x "; then
+ say "No pull requests found"
+ exit 1
+ else
+ for META in ${METAS}; do
+ summarise_meta $META
+ done
+ fi
+}
+
+#############################################################################
+## Command invocation #####################################################
+#############################################################################
+
+if declare -F "cmd_${SUBCMD}" >/dev/null 2>/dev/null; then
+ "cmd_${SUBCMD}"
+else
+ echo >&2 $0: Unknown sub-command: $SUBCMD
+ usage
+fi
+
+exit 0