Skip to content

Instantly share code, notes, and snippets.

@sairam
Created June 1, 2017 19:41
Show Gist options
  • Save sairam/606a035a0a5ebf01df41742eb73207bf to your computer and use it in GitHub Desktop.
Save sairam/606a035a0a5ebf01df41742eb73207bf to your computer and use it in GitHub Desktop.
open file from CLI in github
#!/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