Last active
August 17, 2021 13:03
-
-
Save aheld/71130b39a8b509c4a68c8b4688cc0b61 to your computer and use it in GitHub Desktop.
bash function to open the github url for a given file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!env bash | |
# | |
# run source ./githublink to load this | |
# | |
# cd repo | |
# gfh #opens the repo url | |
# gfh filename #opens the file | |
ghl () { | |
CLIPONLY=false | |
while test $# -gt 0; do | |
case "$1" in | |
-h|--help) | |
shift | |
echo "ghl - open a github url for a local repo" | |
echo " " | |
echo "ghl [options] [filename]" | |
echo " " | |
echo "options:" | |
echo "-h, --help you are here" | |
echo "-c copy to clipboard and don't open url(osx only)" | |
return 0 | |
;; | |
-c) | |
shift | |
CLIPONLY=true | |
;; | |
*) | |
FILENAME=$1 | |
break | |
;; | |
esac | |
done | |
REPO_URL=`git remote -v | grep fetch | awk '{print $2}' | sed 's/git@/http:\/\//' | sed 's/com:/com\//' | sed 's/.git$//'| head -n1` | |
if [ ${FILENAME} ] | |
then | |
BRANCH=`git rev-parse --abbrev-ref HEAD` | |
FILENAME=`git ls-files $FILENAME --full-name` | |
REPO_URL=${REPO_URL}/tree/${BRANCH}/${FILENAME} | |
fi | |
if ($CLIPONLY) | |
then | |
echo "$REPO_URL" | pbcopy | |
else | |
echo "$REPO_URL" | |
if [[ "$OSTYPE" == "linux-gnu"* ]]; then | |
xdg-open $REPO_URL | |
elif [[ "$OSTYPE" == "darwin"* ]]; then | |
open $REPO_URL | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment