Skip to content

Instantly share code, notes, and snippets.

@kconner
Last active June 19, 2020 18:20
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kconner/5057423 to your computer and use it in GitHub Desktop.
Save kconner/5057423 to your computer and use it in GitHub Desktop.
A shell tool for opening GitHub pages
## For bash or zsh:
# Opens the github page, issue, or new pull request for the current git repository in your browser
# Based on https://github.com/jasonneylon/dotfiles/
function gh {
giturl=$(git config --get remote.origin.url)
if [ "$1" = "help" ]; then
echo "gh: Opens this repository's GitHub homepage."
echo "gh 123 246: Opens the page for issues or pull requests #123 and #246."
echo "gh issues: Opens the repository's issues."
echo "gh pulls: Opens the repository's pull requests."
echo "gh new: Opens the page for a new issue on the repository."
echo "gh pr: Opens the page for a new pull request to master."
echo "gh pr branchname: Opens the page for a new pull request to the named branch."
echo "gh help: Shows this message."
elif [ "$giturl" = "" ]; then
echo "Not a git repository or no remote.origin.url set"
else
giturl=${giturl/git\@*github\.com\:/https://github.com/}
giturl=${giturl/\.git//}
if [ "$1" = "pr" ]; then
branch=`git branch | grep \* | cut -f 2 -d " "`
if [ "$2" = "" ]; then
giturl=${giturl}pull/new/master...$branch
open $giturl
else
giturl=${giturl}pull/new/$2...$branch
open $giturl
fi
else
if [ "$1" = "" ]; then
# base URL
open $giturl
elif [ "$1" = "issues" ]; then
# issues list
giturl=${giturl}issues
open $giturl
elif [ "$1" = "pulls" ]; then
# pull requests list
giturl=${giturl}pulls
open $giturl
elif [ "$1" = "new" ]; then
# new issue page
giturl=${giturl}issues/new
open $giturl
else
# issue number
giturl=${giturl}issues/$1
open $giturl
fi
if [ "$2" != "" ]; then
shift
gh "$@"
fi
fi
fi
}
## For fish:
# Opens the github page, issue, or new pull request for the current git repository in your browser
# Based on https://github.com/jasonneylon/dotfiles/
function gh
set command "none"
if [ 0 -lt (count $argv) ]
set command $argv[1]
end
set giturl (git config --get remote.origin.url)
if [ $command = "help" ]
echo "gh: Opens this repository's GitHub homepage."
echo "gh 123 246: Opens the page for issues or pull requests #123 and #246."
echo "gh issues: Opens the repository's issues."
echo "gh pulls: Opens the repository's pull requests."
echo "gh new: Opens the page for a new issue on the repository."
echo "gh pr: Opens the page for a new pull request to master."
echo "gh pr branchname: Opens the page for a new pull request to the named branch."
echo "gh help: Shows this message."
else if [ 0 = (count $giturl) ]
echo "Not a git repository or no remote.origin.url set"
else
set giturl (echo $giturl | sed 's-git@[^:]*github\.com:-https://github.com/-')
set giturl (echo $giturl | sed 's-\.git--')
if [ $command = "pr" ]
set branch (git branch | grep \* | cut -f 2 -d " ")
if [ (count $argv) -lt 2 ]
set giturl $giturl/pull/new/master...$branch
open $giturl
else
set giturl $giturl/pull/new/$argv[2]...$branch
open $giturl
end
else
if [ 0 = (count $argv) ]
# base URL
open $giturl
else if [ $command = "issues" ]
# issues list
set giturl $giturl/issues
open $giturl
else if [ $command = "pulls" ]
# pull requests list
set giturl $giturl/pulls
open $giturl
else if [ $command = "new" ]
# new issue page
set giturl $giturl/issues/new
open $giturl
else
# issue number
set giturl $giturl/issues/$command
open $giturl
end
if [ 1 -lt (count $argv) ]
gh $argv[2..-1]
end
end
end
end
@jeremyellison
Copy link

+5 Insightful.

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