-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-info
executable file
·129 lines (110 loc) · 2.84 KB
/
git-info
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env bash
#
# git-info
#
# License: MIT
# Copyright (c) 2012 Lennart C. L. Kats
set -e
WEB=
if [ "$1" == "-w" ]; then
WEB=-w
shift
fi
REV=
if [ "$1" != "" ] && git rev-parse --verify "$1" &>/dev/null && [ ! -e "$1" ]; then
REV=`git rev-parse $1`
shift
elif [ "$1" == "-r" ]; then
REV=`git rev-parse $2`
shift; shift
fi
if [ "$1" == "-w" ]; then
WEB=-w
shift
fi
if [ "$1" == "" ]; then
FILE=
else
FILE="$1"
FILE=`cd -P -- "$(dirname -- "$FILE")" && echo "$(pwd -P)/$(basename -- "$FILE")"`
while [ ! -e .git ] && [ "`pwd`" != "/" ]; do
cd ..
done
BASEPATH=`ls -d $(pwd -P)`
RELPATH=`echo "$FILE" | sed "s|$BASEPATH/||"`
fi
if ! git remote &>/dev/null; then
cd `dirname "$FILE"`
FILE="`basename "$FILE"`"
fi
REMOTE=$(
git remote -v | grep 'origin.*push' \
| grep -oE '(bitbucket.org|github.com)[:/][^/]+/[^ ]+' \
| sed -E 's/(.*)\.git/\1/' \
| sed -E 's/(bitbucket.org|github.com):/\1\//' \
) || (echo 'No remote for current directory' >&2 && exit 1)
if [ ! "$REMOTE" ]; then
echo Repository not recognized:
git remote -v
exit 1
fi
BRANCH=`git-get-branch 2>/dev/null || git rev-parse HEAD`
BRANCH_IN_URL=`echo $BRANCH | sed 's!/!%2F!g'`
if [ "$FILE" ] && [ ! -d "$FILE" ]; then
git status "$FILE"
fi
if echo $REMOTE | grep -q bitbucket.org; then
BASE_URL="https://$REMOTE"
if [ $REV ] && [ ! "$FILE" ]; then
URL="$BASE_URL/commits/$REV"
DIFFURL= # not supported
elif [ $REV ]; then
URL="$BASE_URL/src/$REV/$RELPATH?at=$BRANCH"
if [ ! -d "$FILE" ]; then
DIFFURL="$BASE_URL/diff/$RELPATH?diff1=master&diff2=$REV&at=$BRANCH"
fi
else
URL="$BASE_URL/src/$BRANCH_IN_URL/$RELPATH?at=$BRANCH"
DIFFURL="$BASE_URL/diff?diff1=master&diff2=$BRANCH"
fi
else
USER=`echo $REMOTE | perl -pe 's|github.com.([^/]+)/.*|$1|'`
REPO=`echo $REMOTE | perl -pe 's|github.com.[^/]+/(.*)|$1|'`
BASE_URL="https://github.com/$USER/$REPO"
if [ $REV ] && [ ! "$FILE" ]; then
URL="$BASE_URL/commit/$REV"
DIFFURL="$BASE_URL/compare/master...$REV"
elif [ $REV ]; then
if [ -d "$FILE" ]; then
URL="$BASE_URL/tree/$REV/$RELPATH"
else
URL="$BASE_URL/blob/$REV/$RELPATH"
fi
DIFFURL= # not supported
else
URL="$BASE_URL/tree/$BRANCH_IN_URL/$RELPATH"
DIFFURL= # not supported
fi
fi
open() {
if which start &>/dev/null && ! [[ `uname -s` =~ Darwin|Linux ]]; then
start "$1"
elif which xdg-open &>/dev/null && ! [[ `uname -s` =~ Darwin ]]; then
xdg-open "$1"
elif which gnome-open &>/dev/null && ! [[ `uname -s` =~ Darwin ]]; then
gnome-open "$1"
elif which kde-open &>/dev/null && ! [[ `uname -s` =~ Darwin ]]; then
kde-open "$1"
else
command open "$1"
fi
}
echo "# URL:"
echo $URL
if [ $DIFFURL ] && [ "$BRANCH" != "master" ]; then
echo "# Diff URL:"
echo $DIFFURL
fi
if [ $WEB ]; then
open $URL
fi