-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-dirs.sh
executable file
·515 lines (474 loc) · 15.7 KB
/
git-dirs.sh
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/bin/bash
# Finds all git repos that are in sub directories.
# Optionally, print only committed or uncommitted repos.
# -Christopher Welborn 11-28-2015
appname="git-dirs"
appversion="0.1.0"
apppath="$(readlink -f "${BASH_SOURCE[0]}")"
appscript="${apppath##*/}"
# appdir="${apppath%/*}"
# Some color constants.
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
green='\e[0;32m'
lightyellow='\e[0;33m' # shellcheck disable=SC2034
# No Color, normal/reset.
NC='\e[0m'
# Command-line arg flags/arrays
declare -a start_dirs
debug_mode=0
committed_only=0
uncommitted_only=0
pushed_only=0
unpushed_only=0
remote_only=0
nonremote_only=0
# Will quiet `print_error` and `printf_error` when set to 0.
show_errors=1
# Show current branch name beside the repo dir.
show_branch=0
function checkout {
# git checkout, but only print errors.
# Returns the same exit status as the git checkout command.
local branch="$1"
[[ -n "$branch" ]] || {
# shellcheck disable=SC2016
# It's not supposed to expand shellcheck, that's the point.
print_error 'Missing `branch` argument to `checkout()`!'
return 1
}
# shellcheck disable=SC2069
# I am blocking stdout on purpose shellcheck.
git checkout "$branch" 2>&1 >/dev/null
}
function get_changes {
# Return success exit status if the repo has changes.
local changes
if ! changes="$(git stat -s)"; then
print_error "Unable to detect changes for $dname."
# Assume this repo has changes.
return 0
fi
# Repo has changes.
[[ -n "$changes" ]] && return 0
# No changes.
return 1
}
function get_current_branch {
# Output the branch name being worked on.
local rawname
if ! rawname="$(git branch --color=never | grep -E --only-matching '\* .+')"; then
print_error "Unable to get branch name!"
return 1
else
cut -d ' ' -f 2 <<< "$rawname"
fi
return 0
}
function get_remote_name {
# Output the first remote repo name (origin, or something else.)
local fullname
if fullname="$(get_remote_name_full)"; then
cut -d '/' -f 1 <<< "$fullname"
else
# Unable to get remote name.
return 1
fi
}
function get_remote_name_full {
local rawname
if rawname="$(git branch --color=never -r | grep -E --max-count=1 '.+/.+')"; then
tr -d ' ' <<< "$rawname"
else
# No remotes.
return 1
fi
}
function get_unpushed {
# Get number of unpushed commits for a repo.
local repo="${1:-repo}"
local branchname
if ! branchname="$(get_current_branch)"; then
print_error "Unable to get current branch for $repo."
return 1
fi
if [[ -z "$branchname" ]]; then
print_error "get_current_branch returned an empty string for: $repo"
return 1
fi
local unpushed_msg
if ! unpushed_msg="$(git log --pretty=oneline "@{u}..$branchname" 2>/dev/null)"; then
# Unable to get unpushed, probably no upstream.
print_debug "Failed to get unpushed for: $repo"
return 1
fi
if [[ -z "$unpushed_msg" ]]; then
echo "0"
else
wc -l <<< "$unpushed_msg"
fi
}
function print_debug {
# Print only if debug_mode is 1.
(( debug_mode )) && echo -e "${green}" "$@" "${NC}"
}
function print_debug_cnt {
# Print a label/count pair for debugging.
local lbl
local cnt
local sep
lbl="$1"
cnt="$2"
sep=":"
if [[ -z "$cnt" ]]; then
# Continuation of previous line.
cnt=$lbl
lbl=" "
sep=" "
fi
print_debug "$(printf "%s%25s%s%s %s%s%s" "${cyan}" "$lbl" "${NC}" "$sep" "${blue}" "$cnt" "${NC}")"
}
function print_dirs {
# Print all git dirs loaded into the git_dirs array.
# Optionally, filtering with cmdline flags.
local committedcnt
local uncommittedcnt
local errs
local unpushed
local unpushedcnt
local unpushederrcnt
local pushedcnt
local nonremotecnt
local skippedremotecnt
local remotecnt
local skippednonremotecnt
local total
local prevbranch=""
local checkouterr=""
print_debug " Listing repos: $#"
print_debug " committed_only: $committed_only"
print_debug " uncommitted_only: $uncommitted_only"
print_debug " pushed_only: $pushed_only"
print_debug " unpushed_only: $unpushed_only"
print_debug " remote_only: $remote_only"
for dname in "${@}"; do
# print_debug "Switching to directory: $dname"
if ! cd "$dname"; then
print_error "Unable to cd to: $dname"
let errs+=1
continue
fi
if ! prevbranch="$(get_current_branch)"; then
print_debug "Cancelling, can't get branch name: $dname"
continue
fi
if [[ -n "$use_branch" ]]; then
# Previous branch is re-checked out later, if available.
if ! checkouterr="$(checkout "$use_branch")"; then
if [[ -n "$checkouterr" ]]; then
if [[ "$checkouterr" =~ "local changes" ]]; then
printf_error "%-40s %b[%blocal changes%b]\n" "$dname" "$NC" "$red" "$NC"
else
print_error "\nErrors when checking out '$use_branch' in: $dname"
print_error "$checkouterr"
fi
fi
print_debug "Cancelling, can't checkout branch: $dname"
continue
fi
fi
if get_remote_name_full &>/dev/null; then
let remotecnt+=1
if (( nonremote_only )); then
print_debug "Skipping remote for nonremote_only: $dname"
let skippedremotecnt+=1
continue
fi
else
let nonremotecnt+=1
if (( remote_only )); then
# No remote to operate on.
print_debug "Skipping non-remote for remote_only: $dname"
let skippednonremotecnt+=1
continue
fi
fi
# Display decisions based on committed/uncommitted status.
if get_changes; then
let uncommittedcnt+=1
# Repo has changes.
if (( committed_only )); then
print_debug "Skipping changed repo for comitted_only: $dname"
continue
fi
else
let committedcnt+=1
# No changes.
if (( uncommitted_only )); then
print_debug "Skipping unchanged repo for uncommitted_only: $dname"
continue
fi
fi
# Display decisions based on pushed/unpushed status.
if unpushed="$(get_unpushed "$dname")"; then
if (( unpushed > 0 )); then
let unpushedcnt+=1
# Repo has unpushed commits.
if (( pushed_only )); then
print_debug "Skipping $unpushed unpushed commits for pushed_only: $dname"
continue
fi
else
let pushedcnt+=1
# Repo has all commits pushed.
if (( unpushed_only )); then
print_debug "Skipping $unpushed unpushed commits for unpushed_only: $dname"
continue
fi
fi
else
let unpushederrcnt+=1
# Unable to get remote info, no remote.
if (( pushed_only )) || (( unpushed_only )); then
print_debug "Skipping get_unpushed error repo for pushed/unpushed_only: $dname"
continue
fi
fi
# Display repo dir, with optional branch name.
printf "%b%-50s%b" "$blue" "$dname" "$NC"
if ((show_branch)); then
if [[ -n "$prevbranch" ]]; then
printf "%b%s%b" "$CYAN" "$prevbranch" "$NC"
else
printf_error "%s" "Unable to get branch name."
fi
fi
printf "\n"
# Run user command?
if ((${#user_cmd_args[@]})); then
print_debug "Running user command: ${user_cmd_args[*]}"
eval "${user_cmd_args[@]}" || let errs+=1
fi
# Re-checkout previous branch?
if [[ -n "$prevbranch" ]]; then
if ! checkouterr="$(checkout "$prevbranch")"; then
print_error "\nUnable to checkout previous branch '$prevbranch' in: $dname"
[[ -n "$checkouterr" ]] && print_error "$checkouterr"
fi
fi
let total+=1
done
print_debug
print_debug_cnt "Committed repos" "${committedcnt:-0}"
print_debug_cnt "Uncommitted repos" "${uncommittedcnt:-0}"
print_debug_cnt "Remote repos" "${remotecnt:-0}"
print_debug_cnt "Remote skipped" "${skippedremotecnt:-0}"
print_debug_cnt "Non-remote" "${nonremotecnt:-0}"
print_debug_cnt "Non-remote skipped" "${skippednonremotecnt:-0}"
print_debug_cnt "Remote pushed" "${pushedcnt:-0}"
print_debug_cnt "Remote unpushed" "${unpushedcnt:-0}"
print_debug_cnt "Remote unpushed errors" "${unpushederrcnt:-0}"
print_debug_cnt "Total" "$#"
print_debug_cnt "Total printed" "${total:-0}"
# No repos counts as an error.
(( total > 0 )) || let errs+=1
print_debug_cnt "Errors" "${errs:-0}"
(( total == 0 )) && print_debug_cnt "(One of the errors was 'nothing to print'.)"
return $errs
}
function print_error {
# Write a msg to stderr.
((show_errors)) && echo -e "${RED}" "$@" "${NC}" 1>&2
}
function print_usage {
# Show usage reason if first arg is available.
[[ -n "$1" ]] && echo -e "\n$1\n"
# shellcheck disable=SC2028
# ...the REPO_CMD example confuses shellcheck.
echo "$appname v. $appversion
Usage:
$appscript -h | -v
$appscript -B
$appscript [-b BRANCH] [-c | -C] [-l | -r] [-p | -P] [-q] [DIR...]
[-D] ([-- REPO_CMD])
Options:
DIR : One or more directories to look for git repos.
Default: $PWD
-- REPO_CMD : A shell command to run inside of the repo dir.
You must single quote characters such
as $, ;, |, etc.
They will be evaluated after switching to the
repo dir.
-b name,--branch name : Checkout a specific branch when checking.
The branch must exist.
-B,--showbranch : Show which branch is selected for each repo.
-c,--committed : Only show repos without uncommitted changes.
-C,--uncommitted : Only show repos with uncommitted changes.
-D,--debug : Print some debugging info while running.
-h,--help : Show this message.
-l,--local : Only show repos without a remote.
-p,--pushed : Only show repos with all commits pushed to
remote.
-P,--unpushed : Only show repos with commits unpushed to
remote.
-q,--quiet : Quiet error messages.
-r,--remote : Only show repos with a remote.
-v,--version : Show $appname version and exit.
Notes:
-- REPO_CMD :
REPO_CMD is a BASH command, and is evaluated after switching to
the repo dir. If the \`cd\` command fails, nothing is done.
You must put -- before the command.
To git a list of modified files in uncommitted repos:
git dirs -C -- 'echo -e \"\\n\$PWD\"; git stat | grep modified'
* Notice the single quotes around \$PWD, ;, and |.
"
}
function print_usage_fail {
# Print usage message and exit with an error status.
print_usage "$@"
exit 1
}
function printf_error {
# Like printf, but prints to stderr using color.
((show_errors)) && {
printf "%b" "$RED"
# shellcheck disable=SC2059
# using all function arguments on purpose shellcheck, it's a wrapper.
printf "$@"
printf "%b" "$NC"
return 0
}
}
in_cmd_arg=0
in_branch_arg=0
use_branch=""
declare -a user_cmd_args
for arg; do
case "$arg" in
"-B" | "--showbranch" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
show_branch=1
fi
;;
"-b" | "--branch" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
in_branch_arg=1
fi
;;
"-c" | "--committed" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
(( ! uncommitted_only )) || print_usage_fail "-c cannot be used with -C."
committed_only=1
fi
;;
"-C"|"--uncommitted" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
(( ! committed_only )) || print_usage_fail "-c cannot be used with -C."
uncommitted_only=1
fi
;;
"-D"|"--debug" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
debug_mode=1
fi
;;
"-h"|"--help" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
print_usage ""
exit 0
fi
;;
"-l"|"--local" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
nonremote_only=1
fi
;;
"-p"|"--pushed" )
(( ! unpushed_only )) || print_usage_fail "-p cannot be used with -P."
pushed_only=1
;;
"-P"|"--unpushed" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
(( ! pushed_only )) || print_usage_fail "-p cannot be used with -P."
unpushed_only=1
fi
;;
"-q"|"--quiet" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
show_errors=0
fi
;;
"-r"|"--remote" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
remote_only=1
fi
;;
"-v"|"--version" )
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
echo -e "$appname v. $appversion\n"
exit 0
fi
;;
-*)
if ((!in_cmd_arg)) && [[ "$arg" == "--" ]]; then
in_cmd_arg=1
elif ((in_cmd_arg)); then
user_cmd_args+=("$arg")
else
print_usage_fail "Unknown flag argument: $arg"
fi
;;
*)
if ((in_cmd_arg)); then
user_cmd_args+=("$arg")
elif ((in_branch_arg)); then
use_branch="$arg"
in_branch_arg=0
else
start_dirs+=("$arg")
fi
esac
done
if (( ${#start_dirs[@]} == 0 )); then
start_dirs=("${start_dirs[@]}" "$PWD")
fi
let errs=0
for startdir in "${start_dirs[@]}"; do
print_debug "Gathering repo directories in: $startdir"
declare -a git_dirs
while read -d $'\0' -r dname; do
# echo "Looking at: $dname"
# Use absolute paths for git_dirs.
git_dirs+=("$(readlink -f "${dname%/*}")")
done < <(find "$startdir" -type d -name ".git" -print0 | sort -z)
print_dirs "${git_dirs[@]}"
exitcode=${PIPE_STATUS[0]}
(( exitcode == 0 )) || let errs+=1
done
exit $errs