[Login]

git/code review

#!/bin/bash

# Usage:
# pr
#   -> creates branch: cr-rroller
# pr feature-1
#   -> creates branch: feature-1
# Set the CR_USERNAME environment variable to override the username from the system default
 
set -e;

# Get the current branch
ORIG_BRANCH=$(git rev-parse --abbrev-ref HEAD)

# Support overriding the username
USERNAME="$CR_USERNAME"
if [ -z "$USERNAME" ]; then
  USERNAME=${USER}
fi
BRANCH="${1}"
if [ $# -eq 0 ] ; then
  BRANCH="cr-${USERNAME}-${ORIG_BRANCH}"
fi
 
echo Creating CR branch: $BRANCH from $ORIG_BRANCH;
 
set +e;
git branch -D $BRANCH;
set -e;
git checkout -b $BRANCH;
git push -f origin $BRANCH;
git checkout $ORIG_BRANCH;
git branch -D $BRANCH;

# Build URL to open PR UI
UPSTREAM=$(git remote get-url origin)
REPO=$(basename -s .git `git config --get remote.origin.url`)
TO_USER=$(echo $UPSTREAM | sed -e 's/.*[\/:]\([^/]*\)\/[^/]*$/\1/')

open "https://www.github.com/$TO_USER/$REPO/compare/master...$BRANCH?expand=1"