/
git-summary
executable file
·382 lines (311 loc) · 11.1 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/bin/bash
# git-summary - summarize git repos at some path
#
# Originally forked from https://github.com/lordadamson/git-summary
#
# Freely distributed under the MIT license. 2023@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
# Headers
HEADER_REPO="Repository"
HEADER_BRANCH="Branch"
HEADER_STATE="State"
usage() {
sed 's/^ //' <<EOF
git-summary - summarize git repos at some path
Usage: git-summary.sh [-h] [-l] [-d] [-q] [-f] [path]
Given a path to a folder containing one or more git repos,
print a status summary table showing, for each repo:
- the repo path
- 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.
-s Sort the output. (Runs sequentially to avoid race conditions)
-f Print full repo path names instead of relative ones
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
local sort=0
local full_path=0
while getopts "hldqsf" opt; do
case "${opt}" in
h) usage ; exit 1 ;;
l) local_only=1 ;; # Will skip "git fetch"
d) deeplookup=1 ;;
q) quiet=1 ;;
s) sort=1 ;; # Sort the output
f) full_path=1 ;; # Use full repo paths
esac
done
shift $((OPTIND-1))
# Use provided path, or default to pwd.
# Here we also decide if we want relative or full path names printouts.
local target=$(${readlink_cmd} -f ${1:-`pwd`})
if [ $full_path -eq 0 ] && command -v realpath >/dev/null && command -v python >/dev/null ; then
local rtarget=$(get_relative_path $target)
else
local rtarget=$target
fi
local repos=$(list_repos $rtarget $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 header_space_repo=${#HEADER_REPO}
local header_space_branch=${#HEADER_BRANCH}
local header_space_state=${#HEADER_STATE}
local longest_repo_name=$(max_len "$repos")
local space_repo=$(( $longest_repo_name < $header_space_repo ? $header_space_repo : $longest_repo_name ))
local branches=$(repo_branches $rtarget)
local longest_branch_name=$(max_len "$branches")
local longest_branch_name=$(( $longest_branch_name < $header_space_branch ? $header_space_branch : $longest_branch_name )) # Factor in the header
if command -v tput >/dev/null && eval 'tput cols' >/dev/null 2>&1;
then
# If we have tput, adapt to terminal
local TERM_COLUMNS=$(tput cols)
local space_branch=$(( $TERM_COLUMNS - $space_repo - $header_space_state - 4)) # The 4 is the spaces between columns
local space_branch=$(( $space_branch < $header_space_branch ? $header_space_branch : $space_branch ))
local space_branch=$(( $space_branch > $longest_branch_name ? $longest_branch_name : $space_branch )) # If this is commented then prompt will be full terminal width
else
local space_branch=$longest_branch_name # If we don't have tput, allow full width
fi
local template=$(printf "%%b%%-%ds %%-%d.%ds %%-%ds" $space_repo $space_branch $space_branch $header_space_state) # template that truncates branch names to fit terminal width
print_header "$template" $space_repo $space_branch $header_space_state
local repo_count=0
local repo
for repo in $repos ; do
if [ $sort -eq 0 ] ; then
summarize_one_git_repo $repo "$template" "$local_only" "$quiet" >&1 & # parallelized - FIFO stdout
else
summarize_one_git_repo $repo "$template" "$local_only" "$quiet" >&1 # sequential - sorted stdout
fi
(( 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 [ "$(uname)" == "SunOS" ]; then # SunOS (solaris/smartos/illumos)
OS=SunOS
readlink_cmd="greadlink"
dirname_cmd="gdirname"
gawk_cmd="awk"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then # Linux-based system
if [[ $(cat /proc/version) == *"Chromium OS"* ]]; then # Chromium OS
OS=Linux
readlink_cmd="readlink"
dirname_cmd="dirname_zero"
gawk_cmd="gawk"
else # Vanilla Linux
OS=Linux
readlink_cmd="readlink"
dirname_cmd="dirname"
gawk_cmd="gawk"
fi
elif [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then # Cygwin
OS=CYGWIN
readlink_cmd="readlink"
dirname_cmd="dirname"
gawk_cmd="gawk"
else
echo "Oups, I cannot identify your current OS."
echo "Please open a ticket or a pull request so we can add your OS to the list of supported systems."
exit 1
fi
}
GIT4WINDOWS=1
# `dirname -z` does not exist on Chromium OS (Google Cloud Platform - Container Optimised OS)
dirname_zero(){
printf '%b' $(dirname $2)'\0'
}
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
local max_state_len=$4
print_divider () {
printf '=%.0s' $(seq 1 $max_repo_len)
printf ' '
printf '=%.0s' $(seq 1 $max_branch_len)
printf ' '
printf '=%.0s' $(seq 1 $max_state_len)
printf '\n'
};
echo
printf "$template\n" $NC $HEADER_REPO $HEADER_BRANCH $HEADER_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 -L $1 -maxdepth 2 -type d -name .git -print0"
else
find_cmd="find -L $1 -type d -name .git -print0"
fi
while IFS= read -r -d $'\0'; do
git_directories+=("$REPLY")
done < <($find_cmd | sort -z 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
}
# Function to get the relative path of a file or directory
get_relative_path() {
local target="$1"
local base=$(pwd)
# Convert paths to absolute paths
local target_absolute=$(realpath "$target")
local base_absolute=$(realpath "$base")
# Use Python to calculate the relative path
python -c "import os.path; print(os.path.relpath('$target_absolute', '$base_absolute'))"
}
trap "printf '$NC'" EXIT
git_summary $@