-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
38 lines (33 loc) · 1.11 KB
/
common.sh
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
if ! command -v git &> /dev/null; then
echo "The 'git' command is not found on this system."
echo "Please install Git."
exit 1
fi
if command -v xdg-open &> /dev/null; then
# Linux
OPEN_CMD=xdg-open
elif command -v open &> /dev/null; then
# macOS
OPEN_CMD=open
else
echo "No supported open command (open, xdg-open) found on this system."
exit 1
fi
# Use provided remote, or 'origin' if none
REMOTE="${1:-origin}"
git_open () {
local GITHUB_POSTFIX=$1
local GITLAB_POSTFIX=$2
# Transform Git URL to browser base URL
# Examples:
# [email protected]:group/project.git => https://gitlab.com/group/project
# https://github.com:group/project.git => https://github.com/group/project
local BASE_URL="https://$(git remote get-url "${REMOTE}" | sed 's|ssh://||' | sed 's|^git@||' | sed 's|^https://||' | sed 's|\.git$||' | sed 's|:|/|')"
if [[ "${BASE_URL}" == "https://github.com/"* ]]; then
# Open GitHub URL
"${OPEN_CMD}" "${BASE_URL}${GITHUB_POSTFIX}"
else
# Assume we're dealing with GitLab
"${OPEN_CMD}" "${BASE_URL}${GITLAB_POSTFIX}"
fi
}