Skip to content

Instantly share code, notes, and snippets.

@richardmcmillen-examtime
Last active January 29, 2022 00:10
Show Gist options
  • Save richardmcmillen-examtime/7448300 to your computer and use it in GitHub Desktop.
Save richardmcmillen-examtime/7448300 to your computer and use it in GitHub Desktop.
Open a project file on GitHub.
#!/bin/bash
#
# Open the specified file on GitHub. It will use the master branch by default:
#
# repo -f app/controllers/application_controller.rb
#
# Specify a different branch:
#
# repo -b another-branch -f app/controllers/application_controller.rb
if [ -d .git ]; then
echo "Opening on Github..."
# Set the default branch to current branch instead of 'master'
# BRANCH=$(git rev-parse --abbrev-ref HEAD)
BRANCH='master'
while getopts ":b:f:" option
do
case "$option" in
b) BRANCH=$OPTARG;;
f) FILEPATH=$OPTARG;;
esac
done
BASE=$(git config --get remote.origin.url | sed s/\\.git// | sed 's/:/\//' | sed 's/.*github.com/https:\/\/github.com/')
URL="$BASE/blob/$BRANCH/$FILEPATH"
if which xdg-open > /dev/null
then
xdg-open $URL
elif which gnome-open > /dev/null
then
gnome-open $URL
elif which open > /dev/null
then
open $URL
fi
echo "Opened $URL"
else
echo "Not a git repo"
fi
@lgarron
Copy link

lgarron commented Nov 13, 2013

GitHub's recommended remote protocol is HTTPS (https://github.com/...), so you might want to filter that on line 19.

Also, line 20 would look a lot more heartwarming if it were HTTPS instead of HTTP. ;-)

@richardmcmillen-examtime
Copy link
Author

@lgarron Thanks for that. Good advice. I have updated the gist accordingly. This will now play nicely with both SSH and HTTPS

@aridastidar
Copy link

aridastidar commented May 20, 2017

Thanks for the handy work. I've made a minor tweak to the script to be able to open any file relative to the parent working tree avoiding the need to specify the complete path on the command line. Here's the updated gist - https://gist.github.com/aritraghoshdastidar/df17ed2d54e293ec7fc92c78a9c0ed0a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment