forked from MirkoLedda/git-summary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-summary
executable file
·307 lines (248 loc) · 7.73 KB
/
git-summary
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
# git-summary - summarize git repos at some path
#
# Forked from https://github.com/lordadamson/git-summary
#
# Freely distributed under the MIT license. 2018@MirkoLedda
set -eu
# Colorcode
GREEN='\e[0;32m'
ORANGE='\e[0;33m'
RED='\e[0;31m'
PURPLE='\e[0;35m'
NC='\e[0m' # No Color
usage() {
sed 's/^ //' <<EOF
git-summary - summarize git repos at some path
Usage: git-summary.sh [-h] [-l] [-d] [-q] [path]
Given a path to a folder containing one or more git repos,
print a status summary table showing, for each repo:
- the folder name
- the currently checked out branch
- a short 2-column status string showing whether there are:
* Local Changes:
- untracked files "?_"
- uncommitted new files "+_"
- uncommitted changes "M_"
- (nothing) " _"
* Remote Changes:
- unpulled commits for the current branch "_v"
- unpushed commits for the current branch "_^"
- (nothing) "_ "
Arguments:
-h Print this message
-l Local operation only. Without this the script runs
"git fetch" in each repo before checking for unpushed/
unpulled commits. As this can be time consuming, this
flag lets you skip that.
-d Deep lookup. Will search within the entire tree of the
current folder.
-q Print nothing for repos that are up to date. Also print
a final tally.
path Path to folder containing git repos; if omitted, the
current working directory is used.
EOF
}
# Main
git_summary() {
detect_OS
detect_Git4Windows
local local_only=0
local opt
local deeplookup=0
local quiet=0
while getopts "hldq" opt; do
case "${opt}" in
h) usage ; exit 1 ;;
l) local_only=1 ;; # Will skip "git fetch"
d) deeplookup=1 ;;
q) quiet=1 ;;
esac
done
shift $((OPTIND-1))
# Use provided path, or default to pwd
local target=$(${readlink_cmd} -f ${1:-`pwd`})
local repos=$(list_repos $target $deeplookup)
if [[ -z $repos ]]; then
exit
fi
# We compute the repo names and branch names here so we can
# compute their maximum lengths and lay things out nicely. This
# can all be done much more easily via the column(1) utility, but
# that has to consume all its input before it can write anything
# out, which isn't great when you're running "git fetch" on a
# whole bunch of repos. Doing it like this allows us to write the
# output to stdout incrementally.
local branches=$(repo_branches $target)
local max_repo_len=$(max_len "$repos")
local max_branch_len=$(max_len "$branches")
local template=$(printf "%%b%%-%ds %%-%ds %%-5s" $max_repo_len $max_branch_len)
print_header "$template" $max_repo_len $max_branch_len
local repo_count=0
local f
for f in $repos ; do
summarize_one_git_repo $f "$template" "$local_only" "$quiet" >&1 &
(( repo_count+=1 ))
done
wait
if [ $quiet -eq 1 ]; then
echo "Checked ${repo_count} repositories."
fi
}
# Autodetect the OS
detect_OS() {
if [ "$(uname)" == "Darwin" ]; then # macOS
OS=Darwin
readlink_cmd="greadlink"
dirname_cmd="gdirname"
gawk_cmd="awk"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then # Linux
OS=Linux
readlink_cmd="readlink"
dirname_cmd="dirname"
gawk_cmd="gawk"
elif [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then # Cygwin
OS=CYGWIN
readlink_cmd="readlink"
dirname_cmd="dirname"
gawk_cmd="gawk"
else
echo "Cannot identify OS."
exit 1
fi
}
GIT4WINDOWS=1
detect_Git4Windows() {
if [[ "$OS" == "CYGWIN" && "$(git --version)" == *"windows"* ]]; then
GIT4WINDOWS=0
fi
}
gitC() {
local ldir=$1; shift;
if [ $GIT4WINDOWS -eq 0 ]; then
git -C "$(cygpath -w $ldir)" "$@"
else
git -C "$ldir" "$@"
fi
}
print_header () {
local template="$1"
local max_repo_len=$2
local max_branch_len=$3
print_divider () {
printf '=%.0s' $(seq 1 $max_repo_len)
printf ' '
printf '=%.0s' $(seq 1 $max_branch_len)
printf ' '
printf '=%.0s' $(seq 1 5)
printf '\n'
};
echo
printf "$template\n" $NC Repository Branch State
print_divider
}
summarize_one_git_repo () {
local f=$1
local template=$2
local local_only=$3
local quiet=$4
local app_name=$f
local branch_name=`gitC $f symbolic-ref HEAD | sed -e "s/^refs\/heads\///"`
local numState=0
### Check remote state
local rstate=""
local has_upstream=`gitC $f rev-parse --abbrev-ref @{u} 2> /dev/null | wc -l`
if [ $has_upstream -ne 0 ] ; then
if [ $local_only -eq 0 ] ; then
gitC $f fetch -q &> /dev/null
fi
# Unpulled and unpushed on *current* branch
local unpulled=`gitC $f log --pretty=format:'%h' ..@{u} | wc -c`
local unpushed=`gitC $f log --pretty=format:'%h' @{u}.. | wc -c`
if [ $unpulled -ne 0 ]; then
rstate="${rstate}v"
numState=1
else
rstate="${rstate} "
fi
if [ $unpushed -ne 0 ]; then
rstate="${rstate}^"
numState=1
else
rstate="${rstate} "
fi
else
rstate="--"
fi
### Check local state
local state=""
local untracked=`LC_ALL=C gitC $f status | grep Untracked -c`
local new_files=`LC_ALL=C gitC $f status | grep "new file" -c`
local modified=`LC_ALL=C gitC $f status | grep modified -c`
if [ $untracked -ne 0 ]; then
state="${state}?"
numState=2
else
state="${state} "
fi
if [ $new_files -ne 0 ]; then
state="${state}+"
numState=2
else
state="${state} "
fi
if [ $modified -ne 0 ]; then
state="${state}M"
numState=2
else
state="${state} "
fi
### Print to stdout
if [ $numState -eq 0 ]; then
if [ $quiet -eq 0 ]; then
printf "$template\n" $GREEN $app_name $branch_name "$state$rstate" >&1
fi
elif [ $numState -eq 1 ]; then
printf "$template\n" $ORANGE $app_name $branch_name "$state$rstate" >&1
elif [ $numState -eq 2 ]; then
printf "$template\n" $RED $app_name $branch_name "$state$rstate" >&1
fi
}
# Given the path to a git repo, compute its current branch name.
repo_branch () {
gitC "$1" symbolic-ref HEAD | sed -e "s/^refs\/heads\///"
}
# Given a path to a folder containing some git repos, compute the
# names of the folders which actually do contain git repos.
list_repos () {
# https://stackoverflow.com/questions/23356779/how-can-i-store-find-command-result-as-arrays-in-bash
git_directories=()
local find_cmd
if [ $deeplookup -eq 0 ]; then
find_cmd="find $1 -maxdepth 2 -type d -name .git -print0"
else
find_cmd="find $1 -type d -name .git -print0"
fi
while IFS= read -r -d $'\0'; do
git_directories+=("$REPLY")
done < <($find_cmd 2>/dev/null)
for i in ${git_directories[*]}; do
if [[ ! -z $i ]]; then
$dirname_cmd -z $i | xargs -0 -L1
fi
done
}
# Given the path to a folder containing git some repos, compute the
# names of the current branches in the repos.
repo_branches () {
local path=$1
local repo
for repo in $(list_repos $path) ; do
echo $(repo_branch $repo)
done
}
max_len () {
echo "$1" | $gawk_cmd '{ print length }' | sort -rn | head -1
}
trap "printf '$NC'" EXIT
git_summary $@