Skip to content

Instantly share code, notes, and snippets.

@chmouel
Last active December 18, 2015 08:39
Show Gist options
  • Save chmouel/5755303 to your computer and use it in GitHub Desktop.
Save chmouel/5755303 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
function readlinkf() {
python -c 'import os,sys;print os.path.realpath(sys.argv[1])' $1
}
SHA=
if [[ $1 == "-m" ]];then
SHA=master
shift
fi
set -e
ORIGIN_REMOTE=$(git config remote.origin.url)
ORIGIN_REMOTE=${ORIGIN_REMOTE/.git/}
if [[ ${ORIGIN_REMOTE} =~ [email protected]:([0-9]+) ]];then
url=https://gist.github.com/${BASH_REMATCH[1]}
echo $url
type -p open >/dev/null 2>/dev/null && open $url
exit
fi
if [[ ${ORIGIN_REMOTE} != [email protected]* && ${ORIGIN_REMOTE} != http*github.com* ]];then
exit
fi
if [[ ${ORIGIN_REMOTE} == [email protected]* ]];then
ORIGIN_REMOTE=${ORIGIN_REMOTE/[email protected]:/https:\/\/github.com\/}
fi
URL=${ORIGIN_REMOTE}
LINE=
if [[ -n ${2} ]];then
LINE="#L${2}"
if [[ -n ${3} ]];then
LINE=${LINE}-L${3}
fi
fi
ARG=$1
if [[ -e ${ARG} ]];then
if [[ -z ${SHA} ]];then
BRANCH=$(git rev-parse --short HEAD)
fi
base_repo=$(git rev-parse --show-toplevel)
ffile=$(readlinkf $1)
ARG=${ffile/$base_repo/}
#http://stackoverflow.com/a/20195713/145125
ARG=${ARG#?}
URL=${URL}/blob/${BRANCH}/${ARG}${LINE}
elif [[ -n ${ARG} ]];then
URL=${ORIGIN_REMOTE}/commit/${ARG}${LINE}
else
URL=${ORIGIN_REMOTE}
fi
echo ${URL}
if [[ -x /usr/bin/open ]];then
/usr/bin/open ${URL}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment