-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitinfo
More file actions
executable file
·138 lines (115 loc) · 3.54 KB
/
gitinfo
File metadata and controls
executable file
·138 lines (115 loc) · 3.54 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
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
130
131
132
133
134
135
136
137
138
#!/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
REMOTE="${REMOTE%.git}"
UPSTREAM="${REMOTE:19}:$BRANCH"
elif [[ "${REMOTE:0:24}" == "https://gist.github.com/" ]]; then
REMOTE="${REMOTE%.git}"
UPSTREAM="gist/${REMOTE:24}:$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
# Old format
UPSTREAM="unhb::${REMOTE:23:-4}:$BRANCH"
elif [[ "${REMOTE:0:33}" == "ssh://git@git.un-hack-bar.de:2222" ]]; then
# New format with SSH port
UPSTREAM="unhb::${REMOTE:34:-4}:$BRANCH"
elif [[ "${REMOTE:0:28}" == "ssh://git@git.un-hack-bar.de" ]]; then
# New format, but WITHOUT SSH port
UPSTREAM="unhb::${REMOTE:31:-4}:$BRANCH"
elif [[ "${REMOTE:0:28}" == "https://git.un-hack-bar.de" ]]; then
REMOTE="${REMOTE%.git}"
UPSTREAM="unhb::${REMOTE:31}:$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