#!/bin/bash set -eu if [ $# -lt 1 ] ; then STR="$(cat)" else echo "No options yet!" exit 1 fi # 1. Collect URL of the incoming repo URL="$(echo "$STR" | sed -n -e '/^are available in the Git repository at:/,+2 { s/[[:space:]]\+// s/\(=[[:digit:]]\{2\}\)\+$// /^\(http\|git\)/p }')" # 2. Check it is in our remotes mapfile REMOTES < <(git remote -v|awk '{print $1,$2;}') REMOTE='' for rem in "${REMOTES[@]}" ; do if [[ ${rem} =~ $URL ]] ; then REMOTE="$(echo "${rem}" | awk -F ' ' '{print $1;}' )" break fi done # 3. Find the tip of the offered branch END="$(echo "$STR" | awk '/^for you to fetch changes up to / { print $NF }' | sed -e 's/[=:]*$//')" # 4. Fetch the remote if [[ -n "$REMOTE" ]] ; then git fetch "$REMOTE" "$END" else git fetch "$URL" "$END" fi # 5. open new branch git checkout -B _4review FETCH_HEAD