Skip to content

Instantly share code, notes, and snippets.

@kfox
Last active June 1, 2017 17:50
Show Gist options
  • Save kfox/420ceb0b2c6bd665b5e5 to your computer and use it in GitHub Desktop.
Save kfox/420ceb0b2c6bd665b5e5 to your computer and use it in GitHub Desktop.
Bash function to open a GitHub repo from the command line
# this should be added to or loaded by your .bashrc
# usage: gh
# gh <repo_name>
# gh <user>/<repo_name>
function gh {
repo="$1"
if [ -z "$repo" ]; then
# if no argument provided, then get the
# URL of the repo in your current working directory
repo=$(git ls-remote --get-url)
if [[ $repo == *"[email protected]"* ]]; then
repo=${repo/[email protected]:/https:\/\/github.com/}
repo=${repo/.git//}
fi
elif [[ $repo != *\/* ]]; then
# arg format: vim-vinegar
repo=$(curl -s -X GET "https://api.github.com/search/repositories?q=$repo&fork=false&in=html_url&order=desc" | python -c 'import sys,json;data=json.loads(sys.stdin.read()); print data["items"][0]["html_url"]')
else
# arg format: tpope/vim-vinegar
repo="https://github.com/$repo"
fi
# open the URL in your default browser (Ubuntu)
# xdg-open $repo (or possibly gnome-open $repo)
# open the URL in your default browser (OS X)
open $repo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment