Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 69 lines (56 sloc) 2.04 KB
#!/bin/bash
#
# Usage: push
#
# Send your local branch changes to the remote branch,
# and copy the relevant GitHub 'compare' URL to your clipboard (!)
# This is Mac-only for the time being
#
# Any extra args to this command will be passed through to 'git push',
# e.g. for doing "push -f"
#
# Colors
color_error="$(tput sgr 0 1)$(tput setaf 1)"
color_reset="$(tput sgr0)"
# TODO DRY this b/w pull and push
branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') || exit $?
default_remote="origin"
remote=$(git config "branch.${branch}.remote" || echo "$default_remote")
remote_branch=$( (git config "branch.${branch}.merge" || echo "refs/heads/$branch") | cut -d/ -f3- )
# Push & save output
push=$(git push --set-upstream $* $remote $remote_branch 2>&1)
exit_code=$?
if [ $exit_code != 0 ]; then
echo -e "${color_error}Ouch, push failed!${color_reset} code=$exit_code, output=$push"
exit $exit_code
elif echo $push | grep "Everything up-to-date" >/dev/null; then
echo "✌️ Git says everything is up-to-date!"
exit 0
fi
# Parse relevant commit refs and let user know what we did
# 1st-time push to new branch gets special treatment
if echo $push | grep "\[new branch\]" >/dev/null; then
refs="master...$branch"
echo "🚀 Pushed a new branch '$branch' remotely"
else
refs=$(echo $push | awk '{ print $3}' | sed 's/\.\./\.\.\./')
echo "🚀 Pushed:"
echo "$push"
fi
# Parse output into a sexy GitHub compare URL!
remote_url=$(git remote show $remote -n | grep Push | awk '{ print $3 }')
if [[ "$remote_url" =~ "github.com" ]]; then
if [[ ${remote_url:0:4} == "git@" ]]; then
regEx='s/.*\:\(.*\)\.git/\1/'
url='https://github.com/'
else
regEx='s/\(.*\)\.git/\1/'
fi
repo_name=$(echo $remote_url | sed $regEx)
github_url="$url$repo_name/compare/$refs"
copied="\nCompare URL copied to clipboard:"
which pbcopy >& /dev/null && echo $github_url | pbcopy && echo -e "$copied"
which xclip >& /dev/null && echo $github_url | xclip -selection clipboard && echo -e "$copied"
echo $github_url
fi
exit 0