-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitinfo
executable file
·120 lines (101 loc) · 2.95 KB
/
gitinfo
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
#!/usr/bin/env zsh
#
# gitinfo
# Part of: malte70/scripts
#
# Shows information about a git repository
# for the zsh prompt.
#
SCRIPT_NAME="gitinfo"
SCRIPT_PROJECT_NAME=""
SCRIPT_VERSION="0.20180909"
CACHEDIR="$HOME/.cache/gitinfo"
CACHEFILE="${CACHEDIR}/`pwd | tr '/' '='`"
usage_help() {
cat <<EOF
Usage:
gitinfo
gitinfo --help
gitinfo --clean-cache
gitinfo --clean-cache-pwd
Options:
--help Show this help
--clean-cache Clean up the whole cache
--clean-cache-pwd Clean up the cache for the current directory and all sub-directories
--ignore-cache Ignore the cache
EOF
}
DO_IGNORE_CACHE="yes"
if [[ $1 == "--help" ]]; then
usage_help
exit 0
elif [[ $1 == "--clean-cache" ]]; then
rm -f $CACHEDIR/=*
exit 0
elif [[ $1 == "--clean-cache-pwd" ]]; then
rm -f ${CACHEFILE} 2>/dev/null
rm -f ${CACHEFILE}* 2>/dev/null
exit 0
elif [[ $1 == "--ignore-cache" ]]; then
DO_IGNORE_CACHE="yes"
elif [[ $# -ne 0 ]]; then
usage_help >&2
exit 1
fi
get_root_folder_name() {
GIT_DIR=$(git rev-parse --git-dir )
if [[ $GIT_DIR == ".git" ]]; then
GIT_DIR=$PWD
else
GIT_DIR=$(echo $GIT_DIR | sed 's/\.git//g')
fi
basename $GIT_DIR
}
[ ! -d $CACHEDIR ] && mkdir -p $CACHEDIR
if ! git rev-parse --git-dir &>/dev/null; then
UPSTREAM="no git"
else
if [[ -f $CACHEFILE && $DO_IGNORE_CACHE == "no" && $(python3 -c 'import os.path,time;print("yes" if (time.time()-os.path.getmtime("'"$CACHEFILE"'"))/(60*60*24) > 2.0 else "no")') == "no" ]]; then
UPSTREAM=`cat $CACHEFILE`
else
REMOTE=`git config --get remote.origin.url`
if [ -z "$REMOTE" ]; then
REMOTE="$(get_root_folder_name)"
fi
BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
if [[ $? -eq 128 ]]; then
BRANCH="$(git config --global --get init.defaultbranch)"
fi
#
# Show shortcuts for some known remotes
#
if [[ "${REMOTE:0:14}" == "git@github.com" ]]; then
UPSTREAM="${REMOTE:15:-4}:$BRANCH"
elif [[ "${REMOTE:0:19}" == "git@gist.github.com" ]]; then
UPSTREAM="gist/${REMOTE:20:-4}:$BRANCH"
elif [[ "${REMOTE:0:19}" == "https://github.com/" ]]; then
UPSTREAM="`echo ${REMOTE:19} | sed 's/.git//g'`:$BRANCH"
elif [[ "${REMOTE:0:28}" == "ssh://aur@aur.archlinux.org/" ]]; then
UPSTREAM="aur-ssh/${REMOTE:28:-4}"
elif [[ "${REMOTE:0:26}" == "https://aur.archlinux.org/" ]]; then
UPSTREAM="aur/${REMOTE:26:-4}"
elif [[ "${REMOTE:0:23}" == "https://git.heroku.com/" ]]; then
UPSTREAM="heroku/${REMOTE:23:-4}"
elif [[ "${REMOTE:0:15}" == "git@git.rt3x.de" ]]; then
UPSTREAM="git.rt3x.de/${REMOTE:16:-4}:$BRANCH"
elif [[ "${REMOTE:0:22}" == "git@git.un-hack-bar.de" ]]; then
UPSTREAM="unhb::${REMOTE:23:-4}:$BRANCH"
else
UPSTREAM="${REMOTE}:$BRANCH"
fi
echo -n $UPSTREAM > $CACHEFILE
fi
# number of uncommited changes
# do not cache this.
CHANGES=`git status --short --ignore-submodules=all | wc -l | stripwhite`
if [ $CHANGES -gt 0 ]
then
UPSTREAM="${UPSTREAM}:${CHANGES}"
fi
fi
echo $UPSTREAM