-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-iterate
executable file
·538 lines (531 loc) · 15.2 KB
/
git-iterate
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/bin/bash
#
# Execute Git commands on multiple directories and subdirectories
# > git iterate [-v|-V] DIR1 DIR2 ... -- GIT_COMMAND ...
#
# Parameters:
# -v: Tell in standard error output in which directory the Git command is operating
# -V: Tell in standard output in which directory the Git command is operating
# DIR*: list of super-directories, Git commands will be executed on each valid sub-directory
# GIT_COMMAND and next parameters: Git commands to be executed on each Git directory
IFS=$'\n'
export GIT_PROGRESS_DELAY=-1
function search_git_folders() {
if [ ! -d "$1" ]
then
return
elif [ ! -f "$1/.git" -a ! -d "$1/.git" -a ! -d "$1/refs" ]
then
for folder in $(find "$1" -mindepth 1 -maxdepth 1 -type d)
do
search_git_folders "$folder"
done
else
git --git-dir="$1" branch >/dev/null 2>&1
if [ $? = 0 ]
then
echo "$1"
else
git --git-dir="$1/.git" branch >/dev/null 2>&1
if [ $? = 0 ]
then
echo "$1"
else
for folder in $(find "$1" -mindepth 1 -maxdepth 1 -type d)
do
search_git_folders "$folder"
done
fi
fi
fi
}
# Get parameters and folders
folders=""
foreach="false"
options=""
quiet="false"
display_only="false"
clean=""
behind=""
ahead=""
detached=""
branch_head=""
branch_type=""
some_remote=""
ref_ff_to_ref=""
ref_strict_ff_to_ref=""
submodule=""
conflict=""
for folder in "$@"
do
if [ -z "$options" ]; then
if [ "$folder" = "-q" ]; then
shift
quiet="moderate"
continue
elif [ "$folder" = "-qq" -o "$folder" = "--quiet" ]; then
shift
quiet="true"
continue
elif [ "$folder" = "--to-be-cleaned" ]; then
shift
clean="-3"
continue
elif [ "$folder" = "--very-unclean" ]; then
shift
clean="-2"
continue
elif [ "$folder" = "--unclean" ]; then
shift
clean="-1"
continue
elif [ "$folder" = "--almost-clean" ]; then
shift
clean="1"
continue
elif [ "$folder" = "--quite-clean" ]; then
shift
clean="2"
continue
elif [ "$folder" = "--clean" ]; then
shift
clean="3"
continue
elif [ "$folder" = "--very-clean" ]; then
shift
clean="4"
continue
elif [ "$folder" = "--very-very-clean" ]; then
shift
clean="5"
continue
elif [ "$folder" = "--tracking-head" ]; then
shift
tracking_head="1"
continue
elif [ "$folder" = "--not-tracking-head" ]; then
shift
tracking_head="0"
continue
elif [ "$folder" = "--late" -o "$folder" = "--behind" ]; then
shift
behind="1"
continue
elif [ "$folder" = "--not-late" -o "$folder" = "--not-behind" ]; then
shift
behind="-1"
continue
elif [ "$folder" = "--ahead" ]; then
shift
ahead="1"
continue
elif [ "$folder" = "--not-ahead" ]; then
shift
ahead="-1"
continue
elif [ "$folder" = "--late-or-ahead" -o "$folder" = "--behind-or-ahead" ]; then
shift
behind="2"
ahead="2"
continue
elif [ "$folder" = "--detached" ]; then
shift
detached="true"
continue
elif [ "$folder" = "--not-detached" ]; then
shift
detached="false"
continue
elif [ "`echo -n "$folder"|cut -c1-9`" = "--branch=" ]; then
shift
branch_head=`echo -n "$folder"|cut -c10-`
continue
elif [ "`echo -n "$folder"|cut -c1-19`" = "--with-some-branch=" ]; then
shift
some_branch=`echo -n "$folder"|cut -c20-`
some_branch=`echo -n ",$some_branch,"`
continue
elif [ "`echo -n "$folder"|cut -c1-27`" = "--with-some-branch-of-type=" ]; then
shift
branch_type=`echo -n "$folder"|cut -c28-`
branch_type=`echo -n ",$branch_type,"`
continue
elif [ "`echo -n "$folder"|cut -c1-19`" = "--with-some-remote=" ]; then
shift
some_remote=`echo -n "$folder"|cut -c20-`
some_remote=`echo -n ",$some_remote,"`
continue
elif [ "`echo -n "$folder"|cut -c1-16`" = "--ref-ff-to-ref=" ]; then
shift
ref_ff_to_ref=`echo -n "$folder"|cut -c17-`
continue
elif [ "`echo -n "$folder"|cut -c1-23`" = "--ref-strict-ff-to-ref=" ]; then
shift
ref_strict_ff_to_ref=`echo -n "$folder"|cut -c24-`
continue
elif [ "$folder" = "--submodule" ]; then
shift
submodule="1"
continue
elif [ "$folder" = "--not-submodule" ]; then
shift
submodule="0"
continue
elif [ "$folder" = "--conflict" ]; then
shift
conflict="1"
continue
elif [ "$folder" = "--no-conflict" ]; then
shift
conflict="0"
continue
fi
fi
options="1"
shift
if [ "$folder" = "--" ]
then
break
elif [ "$folder" = "foreach" ]
then
foreach="true"
break
fi
folders="$folders"$'\n'"$folder"
done
# If no folder is given, use local directory (recursively)
if [ "$folders" = "" ]
then
folders=*
fi
#echo quiet="$quiet"
#echo foreach="$foreach"
#echo clean="$clean"
#echo folders="$folders"
#echo $behind
#echo $ahead
if [ "$#" = 0 ]; then
display_only="true"
fi
# Iterate over each folder
first="true"
for folder in $folders
do
folders=$(search_git_folders "$folder"|sort)
for git_folder in $folders
do
cd "$git_folder"
# First check if the Git repo is eligible regarding its status and remote-tracking status
if [ "$clean" = "-3" ]; then
status1=$(git status --short)
status2=$(git stash list)
status3=$(LANG="en" git branch --list -vv)
status4=$(echo "$status3"|grep "^*"|grep -F -e " behind " -e " ahead ")
status5=$(echo "$status3"|grep "^* (HEAD detached at ")
# Display if ( $status1 !== "" || $status2 !== "" || $status3 === "" || $status4 !== "" || $status5 !== "" )
if [ -z "$status1" -a -z "$status2" -a -n "$status3" -a -z "$status4" -a -z "$status5" ]; then
cd "$OLDPWD"
continue
fi
elif [ "$clean" = "-2" ]; then
status=$(git status --short|grep -v "^??")
if [ -z "$status" ]; then
cd "$OLDPWD"
continue
fi
elif [ "$clean" = "-1" ]; then
status=$(git status --short)
if [ -z "$status" ]; then
cd "$OLDPWD"
continue
fi
elif [ "$clean" = "1" ]; then
status1=$(git status --short)
status2=$(echo "$status1"|grep -v "^??")
status3=$(git stash list)
# Display if ( ( status1 !== "" && status2 === "" ) || ( status2 === "" && status3 !== "" ) )
# Display if status2 === "" && ( status1 !== "" || status3 !== "" )
# Do not display if ( ( status1 === "" || status2 !== "" ) && status3 === "" )
# Do not display if status2 !== "" || ( status1 === "" && status3 === "" )
#if [ \( -z "$status1" -o -n "$status2" \) -a -z "$status3" ]; then
if [ -n "$status2" -o \( -z "$status1" -a -z "$status3" \) ]; then
cd "$OLDPWD"
continue
fi
elif [ "$clean" = "2" ]; then
status=$(git status --short|grep -v "^??")
if [ -n "$status" ]; then
cd "$OLDPWD"
continue
fi
elif [ "$clean" = "3" ]; then
status=$(git status --short)
if [ -n "$status" ]; then
cd "$OLDPWD"
continue
fi
elif [ "$clean" = "4" ]; then
status=$(git status --ignored --short)
if [ -n "$status" ]; then
cd "$OLDPWD"
continue
fi
elif [ "$clean" = "5" ]; then
status1=$(git status --ignored --short)
status2=$(git stash list)
if [ -n "$status1" -o -n "$status2" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ "$tracking_head" = "1" ]; then
symbolic_head=$(LANG="en" git rev-parse --symbolic-full-name HEAD)
is_branch=`echo -n "$symbolic_head"|cut -c 1-11`
if [ "$is_branch" != "refs/heads/" ]; then
cd "$OLDPWD"
continue
fi
branch=`echo -n "$symbolic_head"|cut -c 12-`
status1=$(LANG="en" git config "branch.$branch.remote")
status2=$(LANG="en" git config "branch.$branch.merge")
if [ -z "$status1" -o -z "$status2" ]; then
cd "$OLDPWD"
continue
fi
elif [ "$tracking_head" = "0" ]; then
symbolic_head=$(LANG="en" git rev-parse --symbolic-full-name HEAD)
is_branch=`echo -n "$symbolic_head"|cut -c 1-11`
if [ "$is_branch" != "refs/heads/" ]; then
cd "$OLDPWD"
continue
fi
branch=`echo -n "$symbolic_head"|cut -c 12-`
status1=$(LANG="en" git config "branch.$branch.remote")
status2=$(LANG="en" git config "branch.$branch.merge")
if [ -n "$status1" -a -n "$status2" ]; then
cd "$OLDPWD"
continue
fi
fi
behind_found="0"
if [ "$behind" = "1" -o "$behind" = "2" ]; then
# TODO could be improved: there are false positive if the commit message contains " behind "
status1=$(LANG="en" git branch --list -vv)
status2=$(echo "$status1"|grep "^*"|grep -F " behind ")
# Display if ( status1 !== "" && status2 !== "" )
if [ -z "$status1" -o -z "$status2" ]; then
if [ "$behind" != "2" -o "$ahead" != "2" ]; then
cd "$OLDPWD"
continue
fi
else
behind_found="1"
fi
elif [ "$behind" = "-1" ]; then
# TODO could be improved: there are false positive if the commit message contains " behind "
status1=$(LANG="en" git branch --list -vv)
status2=$(echo "$status1"|grep "^*"|grep -F " behind ")
# Display if ( status1 !== "" && status2 === "" )
if [ -z "$status1" -o -n "$status2" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ "$ahead" = "1" -o "$ahead" = "2" ]; then
# TODO could be improved: there are false positive if the commit message contains " ahead "
status1=$(LANG="en" git branch --list -vv)
status2=$(echo "$status1"|grep "^*"|grep -F " ahead ")
# Display if ( status1 !== "" && status2 !== "" )
if [ -z "$status1" -o -z "$status2" ]; then
if [ "$behind" != "2" -o "$ahead" != "2" -o "$behind_found" != "1" ]; then
cd "$OLDPWD"
continue
fi
fi
elif [ "$ahead" = "-1" ]; then
# TODO could be improved: there are false positive if the commit message contains " ahead "
status1=$(LANG="en" git branch --list -vv)
status2=$(echo "$status1"|grep "^*"|grep -F " ahead ")
# Display if ( status1 !== "" && status2 === "" )
if [ -z "$status1" -o -n "$status2" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ "$detached" = "true" ]; then
status1=$(LANG="en" git branch --list -vv)
status2=$(echo "$status1"|grep "^* (HEAD detached at ")
# Display if ( status1 !== "" && status2 !== "" )
if [ -z "$status1" -o -z "$status2" ]; then
cd "$OLDPWD"
continue
fi
elif [ "$detached" = "false" ]; then
status1=$(LANG="en" git branch --list -vv)
status2=$(echo "$status1"|grep "^* (HEAD detached at ")
# Display if ( status1 !== "" && status2 === "" )
if [ -z "$status1" -o -n "$status2" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ -n "$branch_head" ]; then
symbolic_head=$(LANG="en" git rev-parse --symbolic-full-name HEAD)
if [ "$symbolic_head" != "refs/heads/$branch_head" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ -n "$some_branch" ]; then
branches=$(LANG="en" git for-each-ref refs/heads/* --format='%(refname)'|cut -c12-)
if [ -z "$branches" ]; then
cd "$OLDPWD"
continue
fi
found="false"
for branch in $branches; do
if echo "$some_branch"|grep -q ",$branch,"; then
found="true"
break
fi
done
if [ "$found" = "false" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ -n "$some_remote" ]; then
remotes=$(LANG="en" git remote show)
if [ -z "$remotes" ]; then
cd "$OLDPWD"
continue
fi
found="false"
for remote in $remotes; do
if echo "$some_remote"|grep -q ",$remote,"; then
found="true"
break
fi
done
if [ "$found" = "false" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ -n "$ref_ff_to_ref" -o -n "$ref_strict_ff_to_ref" ]; then
if [ -n "$ref_ff_to_ref" ]; then
ref1=$(echo "$ref_ff_to_ref"|cut -d, -f1)
ref2=$(echo "$ref_ff_to_ref"|cut -d, -f2)
else
ref1=$(echo "$ref_strict_ff_to_ref"|cut -d, -f1)
ref2=$(echo "$ref_strict_ff_to_ref"|cut -d, -f2)
fi
nbrefs=$(LANG="en" git for-each-ref --format='%(refname)' "$ref1" "$ref2"|wc -l)
ref1=$(LANG="en" git for-each-ref --format='%(objectname)' "$ref1")
if [ "$nbrefs" != 2 ]; then
cd "$OLDPWD"
continue
fi
mergebase=$(LANG="en" git merge-base "$ref1" "$ref2")
if [ "$mergebase" != "$ref1" ]; then
cd "$OLDPWD"
continue
fi
if [ -n "$ref_strict_ff_to_ref" ]; then
ref2=$(LANG="en" git for-each-ref --format='%(objectname)' "$ref2")
if [ "$mergebase" = "$ref2" ]; then
cd "$OLDPWD"
continue
fi
fi
fi
if [ -n "$branch_type" ]; then
branches=$(LANG="en" git for-each-ref refs/heads/* --format='%(refname)'|cut -c12-)
if [ -z "$branches" ]; then
continue
fi
found="false"
for branch in $branches; do
type=$(LANG="en" git config --local --get "branch.$branch.type")
if [ -z "$type" ]; then
continue
fi
if echo "$branch_type"|grep -q ",$type,"; then
found=true
break
fi
done
if [ "$found" = "false" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ -n "$conflict" ]; then
status=$(LANG="en" git -C "$OLDPWD" status --short -- "$git_folder"|cut -d' ' -f1)
if [ "$status" = "UU" -a "$conflict" = "0" ]; then
cd "$OLDPWD"
continue
elif [ "$status" != "UU" -a "$conflict" = "1" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ -n "$submodule" ]; then
if ! git -C "$OLDPWD" branch >/dev/null 2>&1; then
cd "$OLDPWD"
continue
fi
status=$(LANG="en" git -C "$OLDPWD" ls-files --cached --stage --full-name -- "$git_folder"|cut -d' ' -f1)
if [ "$status" = "160000" -a "$submodule" = "0" ]; then
cd "$OLDPWD"
continue
elif [ "$status" != "160000" -a "$submodule" = "1" ]; then
cd "$OLDPWD"
continue
fi
fi
if [ "$display_only" = "true" ]; then
echo "$git_folder"
cd "$OLDPWD"
continue
fi
if [ "$quiet" = "true" ]
then
if [ "$foreach" = "false" ]
then
errors=$(git $@ 2>&1)
else
errors=$($@ 2>&1)
fi
if [ "$?" != 0 ]
then
if [ "$first" = "false" ]
then
echo >&2
fi
first="false"
echo $git_folder >&2
echo "$errors" >&2
echo "Error $? in $git_folder" >&2
fi
else
if [ "$quiet" != "moderate" ]
then
echo -e "\033[1m$git_folder\033[0m"
fi
if [ "$foreach" = "false" ]
then
git $@
errors="$?"
else
eval $@
errors="$?"
fi
if [ "$errors" != 0 ]
then
echo "Error $errors in $git_folder" >&2
fi
fi
cd "$OLDPWD"
done
done