Skip to content

Instantly share code, notes, and snippets.

@morinap
Created May 14, 2020 14:51
Show Gist options
  • Save morinap/9d1dfb8a21c1318d4c0b90ccc212542e to your computer and use it in GitHub Desktop.
Save morinap/9d1dfb8a21c1318d4c0b90ccc212542e to your computer and use it in GitHub Desktop.
Utility to get a github web URL from the command line for specific things in your repo
#!/bin/sh
set -e
get_rev()
{
ARG=$1
BLOB_COMMIT=`git rev-parse --abbrev-ref $ARG`
if [ -z "$BLOB_COMMIT" ]; then
BLOB_COMMIT=`git rev-parse $ARG`
fi
}
BASE_URL=`git remote -v | grep git@github.com | grep fetch | head -1 | cut -f2 | cut -d' ' -f1 | sed -e's/:/\//' -e 's/git@/https:\/\//' -e 's/\.git$//'`
BLOB=false
COMMIT_SPECIFIED=false
MARKDOWN=false
LINK_TEXT=""
OPEN=false
YANK=false
get_rev "HEAD"
while getopts ":f:c:l:myo" opt; do
case ${opt} in
f )
BLOB=$OPTARG
;;
c)
get_rev $OPTARG
COMMIT_SPECIFIED=true
;;
l)
LINE_NUM_PARAM="#L$OPTARG"
LINE_NUM_TEXT=":$OPTARG"
;;
m)
MARKDOWN=true
;;
o)
OPEN=true
;;
y)
YANK=true
;;
esac
done
#open $BASE_URL
URL=$BASE_URL
if [ $BLOB != false ]; then
URL="$URL/blob/$BLOB_COMMIT/$BLOB$LINE_NUM_PARAM"
LINK_TEXT="$BLOB$LINE_NUM_TEXT"
elif [ $COMMIT_SPECIFIED == true ]; then
URL="$URL/commit/$BLOB_COMMIT"
LINK_TEXT="$BLOB_COMMIT"
fi
if [ $MARKDOWN == true ]; then
OUTPUT="[$LINK_TEXT]($URL)"
else
OUTPUT=$URL
fi
if [ $YANK == true ]; then
printf $OUTPUT | pbcopy
fi
if [ $OPEN == true ]; then
open $URL
fi
echo $OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment