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
·77 lines (61 loc) · 1.35 KB
/
git-open
File metadata and controls
executable file
·77 lines (61 loc) · 1.35 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
#!/bin/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?
git rev-parse --is-inside-work-tree &>/dev/null
if [[ $? != 0 ]]
then
echo "Not a git repository."
exit 1
fi
# assume origin if not provided
# fallback to upstream if neither is present.
remote="origin"
if [ ! -z "$1" ]
then
remote="$1"
fi
remote_url="remote.${remote}.url"
giturl=$(git config --get $remote_url)
if [ -z "$giturl" ]
then
echo "$remote_url not set."
exit 1
fi
# URL normalization
# Github support
if grep -q github <<<$giturl; then
giturl=${giturl/git\@github\.com\:/https://github.com/}
providerUrlDifference=tree
# bitbucket support
elif grep -q bitbucket <<<$giturl; then
giturl=${giturl/git\@bitbucket\.org\:/https://bitbucket.org/}
providerUrlDifference=branch
fi
giturl=${giturl%\.git}
# get current branch
if [ -z "$2" ]
then
branch=`git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||'`
else
branch="$2"
fi
if [ ! -z "$branch" ]
then
giturl="${giturl}/${providerUrlDifference}/${branch}"
fi
# simplify URL for master
giturl=${giturl/tree\/master/}
# get current open browser command
if [ $(uname -s) == "Darwin" ]
then
open=open
else
open=xdg-open
fi
# open it in a browser
$open $giturl &> /dev/null
exit 0