-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-browse
executable file
·60 lines (55 loc) · 1.55 KB
/
git-browse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bash
#
# Open current repository page on default browser
#
# $1 - Optional remote name, defaults to origin
#
# Examples
#
# git browse
# git browse gitlab
#
#
# Caveats
#
# Since I work mainly on macOS, this will rely on the 'open' command to work. If `open` is not
# available, it will simple print the browser url to stdout (You can still use this in linux,
# something along the lines of `xdg-open` $(git browse))
#
# I'm not sure which is the most common open command on linux, so I chose to leave it out.
# Suggestions are welcome
function choose_open_bin() {
local browser_bin
[[ $(command -v open) ]] && browser_bin="open" || browser_bin="echo"
echo $browser_bin
}
function normalize_https() {
local remote_url="$1"
# just remove .git from the url
echo "${remote_url%*.git}"
}
function normalize_ssh() {
local domain path
local remote_url="$1"
# remove git@ from the url
remote_url=${remote_url#git@}
# extract domain
domain=${remote_url%:*}
# extract path
path=${remote_url#*:} # leaves user/repo.git
path=${path%*.git} # removes the .git
echo "https://${domain}/${path}"
}
function open_browser() {
local remote git_url browser_url browser_bin
remote=${1:-origin}
git_url=$(git ls-remote --get-url "${remote}")
if [[ $git_url =~ "@" ]]; then
browser_url=$(normalize_ssh "$git_url")
else
browser_url=$(normalize_https "$git_url")
fi
browser_bin=$(choose_open_bin)
[[ -n $browser_url ]] && $browser_bin "$browser_url"
}
open_browser "$@"