Skip to content

Instantly share code, notes, and snippets.

@aridastidar
Forked from richardmcmillen-examtime/Repo
Last active May 18, 2018 07:28
Show Gist options
  • Save aridastidar/df17ed2d54e293ec7fc92c78a9c0ed0a to your computer and use it in GitHub Desktop.
Save aridastidar/df17ed2d54e293ec7fc92c78a9c0ed0a 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) FILENAME=$OPTARG;;
esac
done
RELATIVE_PATH=$(git ls-tree --full-name --name-only HEAD $FILENAME)
BASE=$(git config --get remote.origin.url | sed s/\\.git// | sed 's/:/\//' | sed 's/.*github.com/https:\/\/github.com/')
URL="$BASE/blob/$BRANCH/$RELATIVE_PATH"
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
@anderskig
Copy link

anderskig commented May 18, 2018

Thanks for this. I just forked this with a couple of minor edits. Just running "repo" without arguments will no longer try to open all files in root of repo, but instead open current branch root on Github. I also removed the need to specify -f before the path to file/folder. Here's the updated version: https://gist.github.com/anderskig/ce49928757b3fe371af18d3695e23358

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