Skip to content

Instantly share code, notes, and snippets.

@kvendrik
Last active June 29, 2019 18:11
Show Gist options
  • Save kvendrik/217c8d6b4f0467c1973a2d78d5d02bd4 to your computer and use it in GitHub Desktop.
Save kvendrik/217c8d6b4f0467c1973a2d78d5d02bd4 to your computer and use it in GitHub Desktop.
Medium Article: or
function __git_is_repository() {
git -C "$1" rev-parse --is-inside-work-tree &> /dev/null
}
function __git_get_remote_url() {
local remote_name remote_url repository_path
remote_name=$([ -n "$1" ] && echo "$1" || echo origin)
repository_path="$2"
if [ -n "$repository_path" ]; then
remote_url="$(git -C "$repository_path" config --get remote."${remote_name}".url)"
else
remote_url="$(git config --get remote."${remote_name}".url)"
fi
echo "$remote_url"
}
function __git_ssh_to_web_url() {
local base
base=$(echo "$1" | sed -e "s/.git$//" -e "s/^git\@//" -e "s/\(.*[:/].*\)/\1/" -e "s/https\:\/\///" -e "s/\:/\//")
echo "https://$base"
}
# Get a repository's web URL
# Usage: __get_repository_web_url [<repository_path_or_name>] [<remote_name>]
function __get_repository_web_url() {
local remote_url repository_web_url repository_path
repository_path="$1"
if [ -n "$repository_path" ] && [ ! -d "$repository_path" ]; then
repository_path="$REPOSITORIES_DIRECTORY/$1"
if [ ! -d "$repository_path" ]; then
echo "$repository_path is not a repository."
return 1
fi
fi
if ! __git_is_repository "$repository_path"; then
echo 'Not a git repository.'
return 1
fi
remote_url=$(__git_get_remote_url "$2" "$repository_path")
if [ -z "$remote_url" ]; then
echo "Remote $2 does not exist."
return 1
fi
repository_web_url=$(__git_ssh_to_web_url "$remote_url")
echo "$repository_web_url"
}
# Open the remote repository
# Usage: or [<repository_name>] [<remote_name>]
function or() {
local repo_url
if ! repo_url="$(__get_repository_web_url "$1" "$2")"; then
echo "$repo_url"
return
fi
open "$repo_url"
}
__r_autocomplete or
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment