-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-checkout-modules
executable file
·754 lines (604 loc) · 18.8 KB
/
git-checkout-modules
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#! /usr/bin/env bash
# Author: Thomas DEBESSE <[email protected]>
# License: ISC
set -e
set -u
program_name="$(basename "${0}")"
first_header='true'
printHelp () {
sed -e 's/\\t/\t/' <<-EOF
${program_name}: sync all git modules to same reference when possible
Usage: ${program_name} [-C DIRECTORY] [OPTION|NAME] …
Options:
\t-C DIRECTORY change current directory to this directory before doing anything,
\t must be first option if used
\t--help print this help
\t--print print current modules references
\t--update checkout submodules with registered references, clone if missing
\t--fetch fetch submodules
\t--pull pull submodules
\t--commit commit module and submodules
\t--commit:branch=NAME commit module and submodules if branch name is NAME
\t--merge=NAME merge module and submodules branch named NAME
\t--push push submodules
\t--run-before=COMMAND run command before entering submodules (to the deepest)
\t--run-after=COMMAND run command after entering submodules (from the deepest)
\t--list-upstream-branches
\t list all upstream module and submodules branch names
\t--detect-branch checkout branch for current module if detect branch for current commit
\t--detect-tag checkout tag for current module if detect tag for current commit
\t--current-branch checkout submodules with current branch name
\t--current-branch:has=PATTERN
\t checkout submodules with current branch name if name contains PATTERN
\t--current-tag checkout submodules with current tag name
\t--current-tag:has=PATTERN
\t checkout submodules with current tag name if name contains PATTERN
\t--ref=NAME checkout module and submodules with this reference name
\t--ref=NAME:has=PATTERN
\t checkout module and submodules with this reference name if name contains PATTERN
\t--sub-ref=NAME checkout submodules with this reference name
\t--sub-ref=NAME:has=PATTERN
\t checkout submodules with this reference name if name contains PATTERN
Names:
\tNAME checkout module and submodules with reference NAME, fail if parent does not know this reference
EOF
exit
}
printf2 () {
printf "${@}" >&2
}
getCommitId () {
git rev-parse --verify HEAD
}
getBranchName () {
local branch_name="$(git rev-parse --abbrev-ref HEAD)"
if [ "${branch_name}" = 'HEAD' ]
then
local guessed_name="$(git name-rev --name-only HEAD)"
if [ "${guessed_name}" = 'undefined' ]
then
printf 'HEAD\n'
elif printf "${guessed_name}" | egrep -q '^tags/|~|\^'
then
printf 'HEAD\n'
else
printf '%s\n' "${guessed_name}"
fi
else
printf '%s\n' "${branch_name}"
fi
}
getTagName () {
local tag_name="$(git name-rev --tags --name-only HEAD)"
if printf "${tag_name}" | egrep -q '~|\^'
then
printf 'undefined\n'
else
printf '%s\n' "${tag_name}"
fi
}
fetchModule () {
local repo_path="${1}"; shift
local current_branch="$(git branch --show-current)"
if [ "${current_branch}" != '' ]
then
printf2 "Fetch module path: '%s'\n" "${repo_path}"
git fetch
fi
}
pullModule () {
local repo_path="${1}"; shift
local current_branch="$(git branch --show-current)"
if [ "${current_branch}" != '' ]
then
printf2 "Pull module path: '%s'\n" "${repo_path}"
git pull
fi
}
addModules () {
listModules \
| xargs -I{} -P1 git add {}
}
commitModule () {
local repo_path="${1}"; shift
local branch_name="${1}"; shift
local current_branch="$(git branch --show-current)"
if [ "${current_branch}" != '' ]
then
if [ -z "${branch_name}" -o "${branch_name}" = "${current_branch}" ]
then
printf2 "Add submodules for module path: '%s'\n" "${repo_path}"
addModules
printf2 "Commit module path: '%s'\n" "${repo_path}"
git diff --cached --exit-code || git commit -m "sync ${current_branch} submodules"
fi
fi
}
mergeModule () {
local repo_path="${1}"; shift
local branch_name="${1}"; shift
local merge_strategy="${1}"; shift
local current_branch="$(git branch --show-current)"
if [ "${current_branch}" != '' ]
then
if [ -z "${branch_name}" ]
then
true
elif [ "${branch_name}" = "${current_branch}" ]
then
printf2 "Not merging '${branch_name}' on '${current_branch}'\n"
else
printf2 "Add submodules for module path: '%s'\n" "${repo_path}"
addModules
printf2 "Commit module path: '%s'\n" "${repo_path}"
git diff --cached --exit-code || git commit -m "sync ${current_branch} submodules"
printf2 "Merge branch: '%s'\n" "${branch_name}"
if [ "${merge_strategy}" = 'manual' ]
then
while ! git merge --no-edit "${branch_name}"
do
printf2 "Failed to merge branch, opening a shell.\n"
printf2 "Please fix the merge and exit this shell to let the task continue.\n"
printf2 "1. Run: git mergetool\n"
printf2 "2. Use (l)ocal to solve possible submodule merge conflicts.\n"
printf2 " Merge possible non-submodule merge conflicts with an editor.\n"
printf2 "3. Commit the changes with: git merge --continue\n"
printf2 "4. Exit the shell with: exit\n"
/usr/bin/env bash
done
else
git merge --no-edit -X"${merge_strategy}" "${branch_name}"
fi
fi
fi
}
pushModule () {
local repo_path="${1}"; shift
local current_branch="$(git branch --show-current)"
if [ "${current_branch}" != '' ]
then
local is_ours='true'
if git push --dry-run >/dev/null 2>&1
then
printf ''
elif [ "${?}" = '128' ]
then
is_ours='false'
fi
if ! "${is_ours}"
then
printf2 "Not pushing foreign module path: '%s'\n" "${repo_path}"
else
printf2 "Push module path: '%s'\n" "${repo_path}"
if ! git push
then
printf2 "Failed to push branch, opening a shell.\n"
printf2 "Please fix the push and exit this shell to let the task continue.\n"
/usr/bin/env bash
fi
fi
fi
}
runModule () {
local repo_path="${1}"; shift
local command_string="${1}"; shift
if [ -n "${command_string}" ]
then
printf2 "Run command on module path: '%s', '%s'\n" "${repo_path}" "${command_string}"
bash -c "set -e; set -u; ${command_string}"
fi
}
listUpstreamBranchModule () {
git ls-remote origin \
| awk '{ print $2 }' \
| egrep '^refs/heads/' \
| cut -c12- \
| sort -u
}
listModules () {
git config --file .gitmodules --get-regexp path \
| awk '{ print $2 }'
}
updateModules () {
local repo_path="${1}"; shift
printf2 "Update submodules for module path: '%s'\n" "${repo_path}"
git submodule update --init
}
walkModules () {
local action_name="${1}"; shift
local parent_path="${1}"; shift
local repo_path="${1}"; shift
if ! [ -d "${parent_path}/${repo_path}" ]
then
git checkout "${parent_path}/${repo_path}"
git submodule update --init "${parent_path}/${repo_path}"
fi
cd "${parent_path}/${repo_path}"
case "${action_name}" in
'list')
printf '%s\n' "${repo_path}"
;;
'print')
printf '%s %s %s %s\n' "${repo_path}" "$(getBranchName)" "$(getTagName)" "$(getCommitId)"
;;
'update')
updateModules "${repo_path}"
;;
'checkout')
local reference_name="${1}"
updateModules "${repo_path}"
if git checkout "${reference_name}" >/dev/null 2>&1
then
printf2 "Module path '%s': checked out '%s'\n" "${repo_path}" "${reference_name}"
else
printf2 "No '%s' reference in module path '%s'\n" "${reference_name}" "${repo_path}"
fi
;;
'subcheckout')
action_name='checkout'
;;
'fetch')
fetchModule "${repo_path}"
;;
'pull')
pullModule "${repo_path}"
;;
'run-before')
local command_string="${1}"
runModule "${repo_path}" "${command_string}"
;;
'list-upstream-branches')
listUpstreamBranchModule "${repo_path}"
;;
esac
for module_path in $(listModules)
do
walkModules "${action_name}" "${parent_path}" "${repo_path}/${module_path}" "${1:-}"
done
cd "${parent_path}/${repo_path}"
case "${action_name}" in
'commit')
local branch_name="${1:-}"
commitModule "${repo_path}" "${branch_name}"
;;
'merge')
local branch_name="${1:-}"
mergeModule "${repo_path}" "${branch_name}" 'manual'
;;
'merge-ours')
local branch_name="${1:-}"
mergeModule "${repo_path}" "${branch_name}" 'ours'
;;
'merge-theirs')
local branch_name="${1:-}"
mergeModule "${repo_path}" "${branch_name}" 'ours'
;;
'push')
pushModule "${repo_path}"
;;
'run-after')
local command_string="${1}"; shift
runModule "${repo_path}" "${command_string}"
;;
esac
}
printModules () {
local parent_path="${1}"; shift
local repo_path="${1}"; shift
{
printf 'MODULE BRANCH TAG REFERENCE\n'
walkModules 'print' "${parent_path}" "${repo_path}"
} | column -t
}
getOptionLength () {
local option_name="${1}"
# HACK: '\n' has same length than '='
printf -- "${option_name}" | cut -f1 -d'=' | wc -c
}
testPattern () {
local reference_name="${1}"; shift
local pattern_name="${1}"; shift
printf "${reference_name}" | egrep -q "${pattern_name}"
}
testReference () {
local reference_name="${1}"; shift
git rev-parse --verify --quiet "${reference_name}" >/dev/null 2>&1
}
checkBranch () {
local reference_name="${1}"; shift
if [ "${reference_name}" = 'HEAD' ]
then
errorPrintf 'Detached HEAD'
exit 3
fi
}
checkTag () {
local reference_name="${1}"; shift
if [ "${reference_name}" = 'undefined' ]
then
errorPrintf 'No tag on HEAD'
exit 3
fi
}
warningPrintf () {
local format_string="${1}"; shift
printf2 'WARNING: '"${format_string}"'\n' ${@:-}
}
errorPrintf () {
local format_string="${1}"; shift
printf2 'ERROR: '"${format_string}"'\n' ${@:-}
}
headerPrintf () {
local format_string="${1}"; shift
if ! "${first_header}"
then
printf2 '\n'
else
first_header='false'
fi
printf2 "${format_string}"'\n' ${@:-}
}
if [ "${1:-}" = '-C' ]
then
if [ -z "${1:-}" ]
then
errorPrintf 'missing path for -C option'
fi
shift
if [ ! -d "${1}" ]
then
errorPrintf 'not a folder: %s' "${1}"
fi
cd "${1}"
shift
fi
parent_path="$(dirname "${PWD}")"
repo_path="$(basename "${PWD}")"
branch_name="$(getBranchName)"
tag_name="$(getTagName)"
separator=$'\t'
keyword_list=''
while ! [ -z "${1:-}" ]
do
case "${1}" in
'--help'|'-h')
printHelp
;;
*)
keyword_list+="${keyword_list:+${separator}}${1}"
shift
;;
esac
done
BKP_IFS="${IFS}"
IFS="${separator}"
for keyword_name in ${keyword_list}
do
IFS="${BKP_IFS}"
case "${keyword_name}" in
'--list')
walkModules 'list' "${parent_path}" "${repo_path}"
;;
'--print')
headerPrintf 'Print modules references'
printModules "${parent_path}" "${repo_path}"
;;
'--update'|'--revert')
headerPrintf 'Checkout modules references with registered reference, clone if missing'
walkModules 'update' "${parent_path}" "${repo_path}"
;;
'--detect-branch')
reference_name="${branch_name}"
if [ "${reference_name}" = 'HEAD' ]
then
errorPrintf "Cannot detect branch name for current commit: '%s'\n" "$(getCommitId)"
exit 4
fi
headerPrintf "Checkout detected branch '%s'" "${reference_name}"
git checkout "${reference_name}"
;;
'--detect-tag')
reference_name="${tag_name}"
if [ "${reference_name}" = 'HEAD' ]
then
errorPrintf "Cannot detect tag name for current commit: '%s'\n" "$(getCommitId)"
exit 4
fi
headerPrintf "Checkout detected tag '%s'" "${reference_name}"
git checkout "${reference_name}"
;;
'--current-branch')
reference_name="${branch_name}"
checkBranch "${reference_name}"
headerPrintf "Checkout modules with branch '%s'" "${reference_name}"
walkModules 'checkout' "${parent_path}" "${repo_path}" "${reference_name}"
;;
'--current-branch:has='*|'--branch-has='*)
option_length="$(getOptionLength "${keyword_name}")"
pattern_name="${keyword_name:${option_length}}"
reference_name="${branch_name}"
if [ -z "${reference_name}" ]
then
warningPrintf "Empty reference name: '%s'\n" "${reference_name}"
continue
fi
if ! testPattern "${reference_name}" "${pattern_name}"
then
warningPrintf "Current branch '%s' in current module does not has pattern: '%s'\n" "${reference_name}" "${pattern_name}"
continue
fi
headerPrintf "Checkout modules with branch '%s' because of pattern '%s'" "${reference_name}" "${pattern_name}"
walkModules 'checkout' "${parent_path}" "${repo_path}" "${reference_name}"
;;
'--current-tag')
reference_name="${tag_name}"
checkTag "${reference_name}"
headerPrintf "Checkout modules with tag '%s'" "${reference_name}"
walkModules 'checkout' "${parent_path}" "${repo_path}" "${reference_name}"
;;
'--current-tag:has='*|'--tag-has='*)
option_length="$(getOptionLength "${keyword_name}")"
pattern_name="${keyword_name:${option_length}}"
reference_name="${tag_name}"
if [ -z "${reference_name}" ]
then
warningPrintf "Empty reference name: '%s'\n" "${reference_name}"
continue
fi
if ! testPattern "${reference_name}" "${pattern_name}"
then
warningPrintf "Current tag '%s' in current module does not have pattern: '%s'\n" "${reference_name}" "${pattern_name}"
continue
fi
headerPrintf "Checkout modules with tag '%s' because of pattern '%s'" "${reference_name}" "${pattern_name}"
walkModules 'checkout' "${parent_path}" "${repo_path}" "${reference_name}"
;;
'--ref='*':has='*)
option_length="$(getOptionLength "${keyword_name}")"
reference_name="$(echo "${keyword_name:${option_length}}" | cut -f1 -d':')"
pattern_name="$(echo "${keyword_name:${option_length}}" | cut -f2 -d':')"
pattern_name="${pattern_name:4}"
if [ -z "${reference_name}" ]
then
warningPrintf "Empty reference name: '%s'\n" "${reference_name}"
continue
fi
if ! testPattern "${reference_name}" "${pattern_name}"
then
warningPrintf "Reference '%s' in module '%s' does not have pattern: '%s'\n" "${reference_name}" "${repo_path}" "${pattern_name}"
continue
fi
headerPrintf "Checkout submodules with reference '%s' because of pattern '%s'" "${reference_name}" "${pattern_name}"
walkModules 'checkout' "${parent_path}" "${repo_path}" "${reference_name}"
;;
'--ref='*)
option_length="$(getOptionLength "${keyword_name}")"
reference_name="${keyword_name:${option_length}}"
if [ -z "${reference_name}" ]
then
warningPrintf "Empty reference name: '%s'\n" "${reference_name}"
continue
fi
headerPrintf "Checkout submodules with reference '%s'" "${reference_name}"
walkModules 'checkout' "${parent_path}" "${repo_path}" "${reference_name}"
;;
'--sub-ref='*':has='*|'--only-sub='*':has='*)
option_length="$(getOptionLength "${keyword_name}")"
reference_name="$(echo "${keyword_name:${option_length}}" | cut -f1 -d':')"
pattern_name="$(echo "${keyword_name:${option_length}}" | cut -f2 -d':')"
pattern_name="${pattern_name:4}"
if [ -z "${reference_name}" ]
then
warningPrintf "Empty reference name: '%s'\n" "${reference_name}"
continue
fi
if ! testPattern "${reference_name}" "${pattern_name}"
then
warningPrintf "Reference '%s' in module '%s' does not have pattern: '%s'\n" "${reference_name}" "${repo_path}" "${pattern_name}"
continue
fi
headerPrintf "Checkout submodules with reference '%s' because of pattern '%s'" "${reference_name}" "${pattern_name}"
walkModules 'subcheckout' "${parent_path}" "${repo_path}" "${reference_name}"
;;
'--sub-ref='*|'--only-sub='*)
option_length="$(getOptionLength "${keyword_name}")"
reference_name="${keyword_name:${option_length}}"
if [ -z "${reference_name}" ]
then
warningPrintf "Empty reference name: '%s'\n" "${reference_name}"
continue
fi
headerPrintf "Checkout submodules with reference '%s'" "${reference_name}"
walkModules 'subcheckout' "${parent_path}" "${repo_path}" "${reference_name}"
;;
'--fetch')
headerPrintf "Fetch submodules"
walkModules 'fetch' "${parent_path}" "${repo_path}"
;;
'--pull')
headerPrintf "Pull submodules"
walkModules 'pull' "${parent_path}" "${repo_path}"
;;
'--commit')
headerPrintf "Commit submodules"
walkModules 'commit' "${parent_path}" "${repo_path}"
;;
'--commit:branch='*)
option_length="$(getOptionLength "${keyword_name}")"
branch_name="${keyword_name:${option_length}}"
if [ -z "${branch_name}" ]
then
errorPrintf 'Missing branch name to commite\n'
fi
headerPrintf "Commit submodules if branch name is '%s'" "${branch_name}"
walkModules 'commit' "${parent_path}" "${repo_path}" "${branch_name}"
;;
'--merge='*)
option_length="$(getOptionLength "${keyword_name}")"
branch_name="${keyword_name:${option_length}}"
if [ -z "${branch_name}" ]
then
errorPrintf 'Missing branch name to merge\n'
fi
headerPrintf "Merging submodules with branch named '%s'" "${branch_name}"
walkModules 'merge' "${parent_path}" "${repo_path}" "${branch_name}"
;;
'--merge-ours='*)
option_length="$(getOptionLength "${keyword_name}")"
branch_name="${keyword_name:${option_length}}"
if [ -z "${branch_name}" ]
then
errorPrintf 'Missing branch name to merge\n'
fi
headerPrintf "Merging submodules with branch named '%s'" "${branch_name}"
walkModules 'merge-ours' "${parent_path}" "${repo_path}" "${branch_name}"
;;
'--merge-theirs='*)
option_length="$(getOptionLength "${keyword_name}")"
branch_name="${keyword_name:${option_length}}"
if [ -z "${branch_name}" ]
then
errorPrintf 'Missing branch name to merge\n'
fi
headerPrintf "Merging submodules with branch named '%s'" "${branch_name}"
walkModules 'merge-theirs' "${parent_path}" "${repo_path}" "${branch_name}"
;;
'--push')
headerPrintf "Push submodules"
walkModules 'push' "${parent_path}" "${repo_path}"
;;
'--run-before='*)
option_length="$(getOptionLength "${keyword_name}")"
command_string="${keyword_name:${option_length}}"
headerPrintf "Run command before entering submodules"
walkModules 'run-before' "${parent_path}" "${repo_path}" "${command_string}"
;;
'--run-after='*)
option_length="$(getOptionLength "${keyword_name}")"
command_string="${keyword_name:${option_length}}"
headerPrintf "Run command before after entering submodules"
walkModules 'run-after' "${parent_path}" "${repo_path}" "${command_string}"
;;
'--list-upstream-branches')
headerPrintf "List all upstream module and submodules branch names"
walkModules 'list-upstream-branches' "${parent_path}" "${repo_path}" | sort -u
;;
'--'*)
errorPrintf 'Unknown option: %s\n' "${keyword_name}"
exit 1
;;
*)
reference_name="${keyword_name}"
if ! testReference "${reference_name}"
then
if ! testReference "origin/${reference_name}"
then
errorPrintf "Reference does not exist in current module: '%s'\n" "${reference_name}"
exit 2
fi
fi
headerPrintf "Checkout modules references with '%s'" "${reference_name}"
walkModules 'checkout' "${parent_path}" "${repo_path}" "${reference_name}"
;;
esac
done