Skip to content

Instantly share code, notes, and snippets.

@madhukar93
Forked from richardmcmillen-examtime/Repo
Created June 5, 2019 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madhukar93/2f9cd2ca4542a47a4b94b8f0a7b35e4d to your computer and use it in GitHub Desktop.
Save madhukar93/2f9cd2ca4542a47a4b94b8f0a7b35e4d 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment