Last active
March 16, 2017 04:48
-
-
Save jtrein/7359a460337deb87bc3fce884170b924 to your computer and use it in GitHub Desktop.
Open Git URL from Command Line
This file contains 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
#!/bin/bash | |
## | |
# Git Open Remote Repo URL | |
# - by default opens at current tracked branch | |
# - opens with default web browser | |
# - supports HTTPS, ssh protocols using default "origin" | |
# Options: | |
# -b opens base repo URL | |
# -r change remote | |
## | |
# usage help | |
usage() { echo "Usage: $0 [-r <string>] [-b]" 1>&2; exit 1; } | |
# parse opts | |
while getopts ":r:b" o; do | |
case "${o}" in | |
r) | |
r=${OPTARG} | |
echo "Opening remote \"${r}\"..." | |
;; | |
b) | |
b="base" | |
echo "Opening base repo url..." | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
# get raw repo url | |
REPO_URL=$(git remote get-url --push ${r:-origin}) || exit 1; | |
# get branch name | |
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) | |
# get remote url, minus ".git" | |
if [[ $REPO_URL =~ ^https ]]; then | |
URL=$(echo $REPO_URL | sed -e "s/\.git$/\\/tree\\/$BRANCH_NAME/") | |
else | |
URL=$(echo $REPO_URL | sed -e "s/git@\([A-z -]\+\.com\):\([A-z -\/]\+\)\.git/https:\/\/\1\/\2\/tree\/$BRANCH_NAME/") | |
fi | |
# open base URL | |
if [[ ${b} != "" ]]; then | |
URL=$(echo $URL | sed -e "s/\/tree\/$BRANCH_NAME//") | |
fi | |
echo "Opening git repo: "$URL | |
if hash xdg-open 2>/dev/null; then | |
xdg-open $URL | |
else | |
open $URL | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment