Last active
July 17, 2017 07:31
-
-
Save cGuille/6329b03e77a268c9709f1adc01d29548 to your computer and use it in GitHub Desktop.
Open the Git repository on GitHub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# INSTALLATION: | |
# Drop this file in your PATH, name it `ghopen` and give it executable permission. | |
# | |
# You can set up the `GHOPEN_BROWSER` env var to define which browser to open. | |
# Example values: 'google-chrome' (default), 'firefox'… | |
# ----- | |
# USAGE: | |
# Just run `ghopen` inside a Git repository having a GitHub remote. | |
# ----- | |
# Published on: | |
# https://gist.github.com/cGuille/6329b03e77a268c9709f1adc01d29548 | |
# ----- | |
URL_OPENER="${GHOPEN_BROWSER:-google-chrome}" | |
function ghopen() { | |
local project_root_path="$(git rev-parse --show-toplevel)" | |
# url = [email protected]:USERNAME/PROJECT.git | |
local project="$(grep -Po '(?<=url = [email protected]:).+' "${project_root_path}/.git/config" |head -n1)" | |
project=${project%.git} # remove the trailing ".git" | |
if [[ $project == '' ]]; then | |
echo "Could not find GitHub repository." >&2 | |
exit 1 | |
fi | |
local gh_project_url="https://github.com/${project}" | |
local path_in_project="$(realpath --relative-to="${project_root_path}" "$(pwd)/$*")" | |
if [[ $path_in_project != '.' ]]; then | |
gh_project_url="${gh_project_url}/tree/$(git rev-parse HEAD)/${path_in_project}" | |
fi | |
exec "${URL_OPENER}" "${gh_project_url}" | |
} | |
ghopen $* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment