Last active
September 5, 2017 11:28
-
-
Save ffflorian/d2c91297770a6d096a65c6c6bfff52fe to your computer and use it in GitHub Desktop.
Print or browse a git repository's URL.
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 | |
# Print or browse a git repository's URL. | |
# @ffflorian, 2016 | |
# MIT License | |
set -e | |
SCRIPT_NAME="${0##*/}" | |
_print_usage() { | |
cat <<EOF | |
Usage: ${SCRIPT_NAME} [-h] [switch] [folder] | |
Commands: | |
--help (-h) Show help text | |
Switches: | |
--print-only (-p) Just print the URL instead of opening a browser | |
Example: ${SCRIPT_NAME} -p git_project/ | |
EOF | |
} | |
while (( ${#} )); do | |
case "${1}" in | |
-h|--help ) | |
_print_usage | |
exit 0 | |
;; | |
-p|--print-only ) | |
PRINT_ONLY="print_only" | |
shift 1 | |
;; | |
"" ) | |
break | |
;; | |
-* ) | |
echo "Unknown command '${1}'!" | |
_print_usage | |
exit 1 | |
;; | |
* ) | |
CD_FOLDER="${1}" | |
break | |
;; | |
esac | |
done | |
_command_exist() { | |
command -v "${1}" > /dev/null | |
} | |
_init() { | |
if [ ! -z "${CD_FOLDER}" ]; then | |
cd "${CD_FOLDER}" || exit 1 | |
fi | |
_get_web_url | |
if [ "${PRINT_ONLY}" == "print_only" ]; then | |
echo "${WEB_URL}" | |
else | |
_open_browser "${WEB_URL}" | |
fi | |
} | |
_get_git_url() { | |
if (_command_exist "git"); then | |
GIT_URL="$(git config --get remote.origin.url)" | |
else | |
if [ -r ".git/config" ]; then | |
GIT_URL="$(sed -nr 's/.*url = (.*)/\1/gpi' '.git/config')" | |
fi | |
fi | |
if [ -z "${GIT_URL}" ]; then | |
echo "Could not read URL from git! Are you in a git directory or did you specify one?" | |
exit 1 | |
fi | |
} | |
_parse_git_branch() { | |
if (_command_exist "git"); then | |
GIT_BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
else | |
if [ -r ".git/HEAD" ]; then | |
GIT_BRANCH="$(sed -nr 's/ref: refs\/heads\/(.*)$/\1/gpi' '.git/HEAD')" | |
fi | |
fi | |
if [ -z "${GIT_BRANCH}" ]; then | |
echo "Could not read branch from git." | |
fi | |
} | |
_get_web_url() { | |
_get_git_url | |
_parse_git_branch | |
if (grep -qE "^https?://" <<< "${GIT_URL}"); then | |
WEB_URL="${GIT_URL}" | |
elif (grep -qE "[^@]+@" <<< "${GIT_URL}"); then | |
WEB_URL="$(sed -nr 's/[^@]+@([^\.]+\.[^:]+):(.*)\.git/http:\/\/\1\/\2/pi' <<< "${GIT_URL}")" | |
else | |
printf "Could not parse URL from git!\\nURL was: '%s'.\\n" "${GIT_URL}" | |
exit 1 | |
fi | |
WEB_URL="${WEB_URL}/tree/${GIT_BRANCH}" | |
} | |
_open_browser() { | |
if (_command_exist "xdg-open"); then | |
xdg-open "${1}" | |
elif (_command_exist "x-www-browser"); then | |
x-www-browser "${1}" | |
else | |
printf "Could not open the URL automatically. You might copy and paste it into your browser:\\n %s\\n" "${1}" | |
fi | |
} | |
_init |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment