-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-info
executable file
·89 lines (75 loc) · 1.78 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
#!/bin/bash -e
#
# git-info
#
# License: MIT
# Copyright (c) 2012 Lennart C. L. Kats
WEB=
if [ "$1" == "-w" ]; then
WEB=-w
shift
fi
REV=
if [ "$1" != "" ] && git rev-parse "$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=
elif [ -d "$1" ]; then
cd "$1"
FILE=
else
FILE="$1"
fi
if ! git remote &>/dev/null; then
cd `dirname "$FILE"`
FILE="`basename "$FILE"`"
fi
REMOTE=`git remote -v | grep 'origin.*push' | grep -oE 'github.com[:/][^/]+/[^ ]+' | sed -E 's/(.*)\.git/\1/'` ||
(echo 'No remote for current directory' >&2 && exit 1)
if [ ! "$REMOTE" ]; then
echo Repository not recognized:
git remote -v
exit 1
fi
USER=`echo $REMOTE | perl -pe 's|github.com.([^/]+)/.*|$1|'`
REPO=`echo $REMOTE | perl -pe 's|github.com.[^/]+/(.*)|$1|'`
BRANCH=`git-get-branch 2>/dev/null || git rev-parse HEAD`
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/||"`
if [ -d "$FILE" ]; then
FILE=
else
git status "$FILE"
fi
if [ $REV ] && [ ! $FILE ]; then
URL="https://github.com/$USER/$REPO/commit/$REV"
DIFFURL=
elif [ $REV ]; then
URL="https://github.com/$USER/$REPO/blob/$REV/$RELPATH"
DIFFURL="https://github.com/$URER/$REPO/compare/master...$REV"
else
URL="https://github.com/$USER/$REPO/blob/$BRANCH/$RELPATH"
DIFFURL="https://github.com/$USER/$REPO/compare/master...`echo $BRANCH | sed 's!/!%2F!g'`"
fi
echo "# URL:"
echo $URL
if [ $DIFFURL ] && [ "$BRANCH" != "master" ]; then
echo "# Diff URL:"
echo $DIFFURL
fi
if [ $WEB ]; then
open $URL
fi