Created
June 1, 2017 19:41
-
-
Save sairam/606a035a0a5ebf01df41742eb73207bf to your computer and use it in GitHub Desktop.
open file from CLI in github
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
#!/bin/bash | |
# Usage: open-file-in-github.sh filepath | |
# Usage: open-file-in-github.sh dirpath develop | |
# Usage: open-file-in-github.sh filepath feature/test | |
# works in any subdirectory. does not check if path is tracked or not. | |
filepath=$1 | |
branch=$2 | |
if [[ -d $filepath ]]; then | |
pathtype='tree' | |
elif [[ -f $filepath ]]; then | |
pathtype='blob' | |
else | |
echo "$filepath is not valid" | |
exit 1 | |
fi | |
if [[ "$branch" = "" ]]; then | |
branch=$(git rev-parse --abbrev-ref HEAD) | |
fi | |
gitfilepath=$(git rev-parse --show-prefix)$filepath | |
remoteurl=$(git remote -v | awk '/fetch/{print $2}' | sed -Ee 's#(git@|git://)#https://#' -e 's@com:@com/@' -e 's@\.git@@') | |
open "$remoteurl/$pathtype/$branch/$gitfilepath" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment