forked from paulirish/git-open
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-open
More file actions
executable file
·100 lines (78 loc) · 2.66 KB
/
git-open
File metadata and controls
executable file
·100 lines (78 loc) · 2.66 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env bash
# Opens the GitHub page for a repo/branch in your browser.
# https://github.com/paulirish/git-open/
#
# git open
# git open [remote] [branch]
# are we in a git repo?
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
echo "Not a git repository." 1>&2
exit 1
fi
is_issue=0
# If the first argument is 'issue', we want to load the issue page
if [[ "$1" == 'issue' ]]; then
is_issue=1
# Allow the user to provide other args, aka `git open issue upstream 79`
shift
fi
# assume remote origin if not provided
remote=${1:-"origin"}
# @TODO ls-remote will also expand "insteadOf" items `giturl=$(git ls-remote --get-url $remote)``
giturl=$(git config --get "remote.${remote}.url")
if [[ -z "$giturl" ]]; then
echo "Git remote is not set for $remote" 1>&2
exit 1
fi
# Initial case examples: '[email protected]:user/project', 'https://example.com:8080/scm/user/project.git/'
# Trim "/" and ".git" from the end of the url
giturl=${giturl%/} giturl=${giturl%.git}
# Trim before last '@' and protocol (*://) from beginning
uri=${giturl##*@} uri=${uri##*://}
# Trims before first ':' or '/' to get path
urlpath=${uri#*[/:]}
# Trims after first ':' or '/' to remove path
server=${uri%%[/:]*}
# Get current branch
branch=${2:-$(git symbolic-ref -q --short HEAD)}
# Split arguments on '/'
IFS='/' pathargs=($urlpath)
if (( is_issue )); then
# For issues, take the numbers and preprend 'issues/'
providerBranchRef="issues/${branch//[^0-9]/}"
else
# Make # and % characters url friendly
# github.com/paulirish/git-open/pull/24
branch=${branch//%/%25} branch=${branch//#/%23}
providerBranchRef="tree/$branch"
fi
if [[ "$server" == 'bitbucket.org' ]]; then
# Bitbucket, see https://github.com/paulirish/git-open/issues/80 for why ?at is needed.
providerBranchRef="src?at=$branch"
elif [[ ${pathargs[0]} == 'scm' ]]; then
# Bitbucket server, which starts with 'scm'
# Replace the first element, 'scm', with 'projects'. Keep the first argument, the string 'repos', and finally the rest of the arguments.
pathargs=('projects' ${pathargs[1]} 'repos' "${pathargs[@]:2}")
IFS='/' urlpath="${pathargs[*]}"
providerBranchRef="browse?at=$branch"
fi
# @TODO: support non-https?
openurl="https://$server/$urlpath"
# simplify URL for master
if [[ $branch != "master" ]]; then
openurl="$openurl/$providerBranchRef"
fi
# get current open browser command
case $( uname -s ) in
Darwin) open='open';;
MINGW*) open='start';;
MSYS*) open='start';;
CYGWIN*) open='cygstart';;
*) open='xdg-open';;
esac
# Allow printing the url if BROWSER=echo
if [[ $BROWSER != "echo" ]]; then
exec &>/dev/null
fi
# open it in a browser
${BROWSER:-$open} "$openurl"