Skip to content

Instantly share code, notes, and snippets.

@snatchev
Last active May 22, 2022 06:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snatchev/34e793af8ab1114d9e42 to your computer and use it in GitHub Desktop.
Save snatchev/34e793af8ab1114d9e42 to your computer and use it in GitHub Desktop.
a fish-shell function to open the current git repo/branch in a browser
function gh --description 'Open the webpage for the current github repo/branch'
set -l fetch_url (command git remote --verbose show -n origin ^/dev/null | command grep Fetch | cut -c 14- )
#did we get an exit status?
if [ $status -gt 0 ]
echo 'Not a git repo.'
return 1
end
if [ -z $fetch_url ]
echo 'Not a git repo.'
return 1
end
if [ -z (echo $fetch_url | grep github ) ]
echo 'Not a github repo.'
return 3
end
set -l branch (command git rev-parse --abbrev-ref HEAD)
if [ $branch = 'HEAD' ]
# we couldn't find a branch or tag, so lets get a sha
set branch (command git rev-parse HEAD)
end
set url (echo "$fetch_url/tree/$branch" | sed 's|git@github.com:\(.*\)\.git|https://github.com/\1|')
open "$url/$argv"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment