-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin
executable file
·2451 lines (2026 loc) · 69.7 KB
/
bin
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
#
# Copyright 2013-2020 - Ingy döt Net <ingy@ingy.net>
#
# shellcheck disable=1090,1091,2034
# Exit on any errors:
set -e
export FILTER_BRANCH_SQUELCH_WARNING=1
# Import Bash+ helper functions:
SOURCE=${BASH_SOURCE[0]}
while [[ -h $SOURCE ]]; do
DIR=$( cd -P "$( dirname "$SOURCE" )" && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE
done
SOURCE_DIR=$(dirname "$SOURCE")
# if [[ -z $GIT_SUBREPO_ROOT ]]; then
# # If `make install` installation used:
# source "${SOURCE_DIR}/bash+.bash"
# else
# # If `source .rc` method used:
# source "${SOURCE_DIR}/../ext/bashplus/lib/bash+.bash"
# fi
### bash+.bash
bash+:version-check() {
local cmd want got out
IFS=' ' read -r -a cmd <<< "${1:?}"
IFS=. read -r -a want <<< "${2:?}"
: "${want[0]:=0}"
: "${want[1]:=0}"
: "${want[2]:=0}"
if [[ ${cmd[*]} == bash ]]; then
got=("${BASH_VERSINFO[@]}")
BASHPLUS_VERSION_CHECK=${BASH_VERSION-}
else
[[ ${#cmd[*]} -gt 1 ]] || cmd+=(--version)
out=$("${cmd[@]}") ||
{ echo "Failed to run '${cmd[*]}'" >&2; exit 1; }
[[ $out =~ ([0-9]+\.[0-9]+(\.[0-9]+)?) ]] ||
{ echo "Can't determine version number from '${cmd[*]}'" >&2; exit 1; }
BASHPLUS_VERSION_CHECK=${BASH_REMATCH[1]}
IFS=. read -r -a got <<< "$BASHPLUS_VERSION_CHECK"
fi
: "${got[2]:=0}"
(( got[0] > want[0] || ((
got[0] == want[0] && ((
got[1] > want[1] || ((
got[1] == want[1] && got[2] >= want[2]
)) )) )) ))
}
bash+:version-check bash 3.2 ||
{ echo "The 'bashplus' library requires 'Bash 3.2+'." >&2; exit 1; }
@() (echo "$@") # XXX do we want to keep this?
bash+:export:std() {
set -o pipefail
if bash+:version-check bash 4.4; then
set -o nounset
shopt -s inherit_errexit
fi
echo use die warn
}
# Source a bash library call import on it:
bash+:use() {
local library_name=${1:?bash+:use requires library name}; shift
local library_path=; library_path=$(bash+:findlib "$library_name") || true
[[ $library_path ]] ||
bash+:die "Can't find library '$library_name'." 1
source "$library_path"
if bash+:can "$library_name:import"; then
"$library_name:import" "$@"
else
bash+:import "$@"
fi
}
# Copy bash+: functions to unprefixed functions
bash+:import() {
local arg=
for arg; do
if [[ $arg =~ ^: ]]; then
# Word splitting required here
# shellcheck disable=2046
bash+:import $(bash+:export"$arg")
else
bash+:fcopy "bash+:$arg" "$arg"
fi
done
}
# Function copy
bash+:fcopy() {
bash+:can "${1:?bash+:fcopy requires an input function name}" ||
bash+:die "'$1' is not a function" 2
local func
func=$(type "$1" 3>/dev/null | tail -n+3)
[[ ${3-} ]] && "$3"
eval "${2:?bash+:fcopy requires an output function name}() $func"
}
# Find the path of a library
bash+:findlib() {
local library_name
library_name=$(tr '[:upper:]' '[:lower:]' <<< "${1//:://}").bash
local lib=${BASHPLUSLIB:-${BASHLIB:-$PATH}}
library_name=${library_name//+/\\+}
IFS=':' read -r -a libs <<< "$lib"
find "${libs[@]}" -name "${library_name##*/}" 2>/dev/null |
grep -E "$library_name\$" |
head -n1
}
bash+:die() {
local msg=${1:-Died}
msg=${msg//\\n/$'\n'}
printf "%s" "$msg" >&2
if [[ $msg == *$'\n' ]]; then
exit 1
else
printf "\n"
fi
local c
IFS=' ' read -r -a c <<< "$(caller "${DIE_STACK_LEVEL:-${2:-0}}")"
if (( ${#c[@]} == 2 )); then
msg=" at line %d of %s"
else
msg=" at line %d in %s of %s"
fi
# shellcheck disable=2059
printf "$msg\n" "${c[@]}" >&2
exit 1
}
bash+:warn() {
local msg=${1:-Warning}
printf "%s" "${msg//\\n/$'\n'}\n" >&2
}
bash+:can() {
[[ $(type -t "${1:?bash+:can requires a function name}") == function ]]
}
### end bash+.bash
bash+:import :std can version-check
VERSION=0.4.6
REQUIRED_BASH_VERSION=4.0
REQUIRED_GIT_VERSION=2.7.0
GIT_TMP=$(git rev-parse --git-common-dir 2> /dev/null || echo .git)/tmp
# `git rev-parse` turns this into a getopt parser and a command usage message:
GETOPT_SPEC="\
git subrepo <command> <arguments> <options>
Commands:
clone Clone a remote repository into a local subdirectory
init Turn a current subdirectory into a subrepo
pull Pull upstream changes to the subrepo
push Push local subrepo changes upstream
fetch Fetch a subrepo's remote branch (and create a ref for it)
branch Create a branch containing the local subrepo commits
commit Commit a merged subrepo branch into the mainline
status Get status of a subrepo (or all of them)
clean Remove branches, remotes and refs for a subrepo
config Set subrepo configuration properties
help Documentation for git-subrepo (or specific command)
version Display git-subrepo version info
upgrade Upgrade the git-subrepo software itself
See 'git help subrepo' for complete documentation and usage of each command.
Options:
--
h Show the command summary
help Help overview
version Print the git-subrepo version number
a,all Perform command on all current subrepos
A,ALL Perform command on all subrepos and subsubrepos
b,branch= Specify the upstream branch to push/pull/fetch
e,edit Edit commit message
f,force Force certain operations
F,fetch Fetch the upstream content first
M,method= Join method: 'merge' (default) or 'rebase'
m,message= Specify a commit message
file= Specify a commit message file
r,remote= Specify the upstream remote to push/pull/fetch
s,squash Squash commits on push
u,update Add the --branch and/or --remote overrides to .gitrepo
q,quiet Show minimal output
v,verbose Show verbose output
d,debug Show the actual commands used
x,DEBUG Turn on -x Bash debugging
"
#------------------------------------------------------------------------------
# Top level function:
#------------------------------------------------------------------------------
main() {
# Define global variables:
local command= # Subrepo subcommand to run
local command_arguments=() # Command args after getopt parsing
local commit_msg_args=() # Arguments to show in the commit msg
local subrepos=() # List of multiple subrepos
local all_wanted=false # Apply command to all subrepos
local ALL_wanted=false # Apply command to all subrepos and subsubrepos
local force_wanted=false # Force certain operations
local fetch_wanted=false # Fetch requested before a command
local squash_wanted=false # Squash commits on push
local update_wanted=false # Update .gitrepo with --branch and/or --remote
local quiet_wanted=false # Output should be quiet
local verbose_wanted=false # Output should be verbose
local debug_wanted=false # Show debug messages
local subdir= # Subdirectory of the subrepo being used
local subref= # Valid git ref format of subdir
local gitrepo= # Path to .gitrepo file
local worktree= # Worktree created by 'git worktree'
local start_pwd
start_pwd=$(pwd) # Store the original directory
local original_head_commit= # HEAD commit id at start of command
local original_head_branch= # HEAD ref at start of command
local upstream_head_commit= # HEAD commit id from a subrepo fetch
local subrepo_remote= # Remote url for subrepo's upstream repo
local subrepo_branch= # Upstream branch to clone/push/pull
local subrepo_commit= # Upstream HEAD from previous clone/pull
local subrepo_parent= # Local commit from before previous clone/pull
local subrepo_former= # A retired gitrepo key that might still exist
local refs_subrepo_branch= # A subrepo ref -> commit of branch/pull command
local refs_subrepo_commit= # A subrepo ref -> commit last merged
local refs_subrepo_fetch= # A subrepo ref -> FETCH_HEAD after fetch
local refs_subrepo_push= # A subrepo ref -> branch after push
local override_remote= # Remote specified with -r
local override_branch= # Remote specified with -b
local edit_wanted=false # Edit commit message using -e
local wanted_commit_message= # Custom commit message using -m
local commit_msg_file= # Custom commit message using --file
local join_method= # Current join method (rebase/merge)
local FAIL=true # Flag for RUN: fail on error
local OUT=false # Flag for RUN: put output in $output
local TTY=false # Flag for RUN: print output directly
local SAY=true # Flag for RUN: print command for verbose
local EXEC=false # Flag for RUN: run subprocess
local OK=true # Flag that commands have succeeded
local CODE=0 # Failure reason code
local INDENT= # Verbose indentation
local git_version= # Git version in use
# Check environment and parse CLI options:
assert-environment-ok
# Parse and validate command options:
get-command-options "$@"
# Make sure repo is in the proper state:
assert-repo-is-ready
command-init
if $all_wanted && [[ ! $command =~ ^(help|status)$ ]]; then
if [[ -n $subrepo_branch ]]; then
error "options --branch and --all are not compatible"
fi
# Run the command on all subrepos
local args=( "${command_arguments[@]}" )
get-all-subrepos
for subdir in ${subrepos[*]}; do
command-prepare
subrepo_remote=
subrepo_branch=
command_arguments=( "$subdir" "${args[@]}" )
"command:$command"
done
else
# Run the command on a specific subrepo
command-prepare
"command:$command"
fi
}
#------------------------------------------------------------------------------
# API command functions.
#
# Most of these commands call a subrepo:$command function to do the actual
# work. The user facing output (via `say`) is done up here. The
# subrepo:* worker functions are meant to be called internally and don't print
# info to the user.
#------------------------------------------------------------------------------
# `git subrepo clone <url> [<subdir>]` command:
command:clone() {
command-setup +subrepo_remote subdir:guess-subdir
# Clone (or reclone) the subrepo into the subdir:
local reclone_up_to_date=false
subrepo:clone
if "$reclone_up_to_date"; then
say "Subrepo '$subdir' is up to date."
return
fi
# Successful command output:
local re=
$force_wanted && re=re
local remote=$subrepo_remote
say "Subrepo '$remote' ($subrepo_branch) ${re}cloned into '$subdir'."
}
# `git subrepo init <subdir>` command:
command:init() {
command-setup +subdir
local remote=${subrepo_remote:=none}
local branch=${subrepo_branch:=master}
# Init new subrepo from the subdir:
subrepo:init
if OK; then
if [[ $remote == none ]]; then
say "Subrepo created from '$subdir' (with no remote)."
else
say "Subrepo created from '$subdir' with remote '$remote' ($branch)."
fi
else
die "Unknown init error code: '$CODE'"
fi
return 0
}
# `git subrepo pull <subdir>` command:
command:pull() {
command-setup +subdir
subrepo:pull
if OK; then
say "Subrepo '$subdir' pulled from '$subrepo_remote' ($subrepo_branch)."
elif [[ $CODE -eq -1 ]]; then
say "Subrepo '$subdir' is up to date."
elif [[ $CODE -eq 1 ]]; then
error-join
return "$CODE"
else
die "Unknown pull error code: '$CODE'"
fi
return 0
}
# `git subrepo push <subdir>` command:
command:push() {
local branch=
command-setup +subdir branch
subrepo:push
if OK; then
say "Subrepo '$subdir' pushed to '$subrepo_remote' ($subrepo_branch)."
elif [[ $CODE -eq -2 ]]; then
say "Subrepo '$subdir' has no new commits to push."
elif [[ $CODE -eq 1 ]]; then
error-join
return "$CODE"
else
die "Unknown push error code: '$CODE'"
fi
return 0
}
# `git subrepo fetch <subdir>` command
command:fetch() {
command-setup +subdir
if [[ $subrepo_remote == none ]]; then
say "Ignored '$subdir', no remote."
else
subrepo:fetch
say "Fetched '$subdir' from '$subrepo_remote' ($subrepo_branch)."
fi
}
# `git subrepo branch <subdir>` command:
command:branch() {
command-setup +subdir
if $fetch_wanted; then
CALL subrepo:fetch
fi
local branch=subrepo/$subref
if $force_wanted; then
# We must make sure that the worktree is removed as well
worktree=$GIT_TMP/$branch
git:delete-branch "$branch"
fi
if git:branch-exists "$branch"; then
error "Branch '$branch' already exists. Use '--force' to override."
fi
# Create the subrepo branch:
subrepo:branch
say "Created branch '$branch' and worktree '$worktree'."
}
# `git subrepo commit <subdir>` command
command:commit() {
command-setup +subdir subrepo_commit_ref
if "$fetch_wanted"; then
CALL subrepo:fetch
fi
git:rev-exists "$refs_subrepo_fetch" ||
error "Can't find ref '$refs_subrepo_fetch'. Try using -F."
upstream_head_commit=$(git rev-parse "$refs_subrepo_fetch")
[[ ${subrepo_commit_ref-} ]] ||
subrepo_commit_ref=subrepo/$subref
subrepo:commit
say "Subrepo commit '$subrepo_commit_ref' committed as"
say "subdir '$subdir/' to branch '$original_head_branch'."
}
# `git subrepo status [<subdir>]` command:
command:status() {
subrepo:status | ${GIT_SUBREPO_PAGER}
}
status-refs() {
local output=
while read -r line; do
[[ $line =~ ^([0-9a-f]+)\ refs/subrepo/$subref/([a-z]+) ]] || continue
local sha1=; sha1=$(git rev-parse --short "${BASH_REMATCH[1]}")
local type=${BASH_REMATCH[2]}
local ref=refs/subrepo/$subref/$type
if [[ $type == branch ]]; then
output+=" Branch Ref: $sha1 ($ref)"$'\n'
elif [[ $type == commit ]]; then
output+=" Commit Ref: $sha1 ($ref)"$'\n'
elif [[ $type == fetch ]]; then
output+=" Fetch Ref: $sha1 ($ref)"$'\n'
elif [[ $type == pull ]]; then
output+=" Pull Ref: $sha1 ($ref)"$'\n'
elif [[ $type == push ]]; then
output+=" Push Ref: $sha1 ($ref)"$'\n'
fi
done < <(git show-ref)
if [[ $output ]]; then
printf " Refs:\n%s" "$output"
fi
}
# `git subrepo clean <subdir>` command
command:clean() {
command-setup +subdir
local clean_list=()
subrepo:clean
for item in "${clean_list[@]}"; do
say "Removed $item."
done
}
# Wrap git config $gitrepo
command:config() {
command-setup +subdir +config_option config_value
# shellcheck disable=2154
o "Update '$subdir' configuration with $config_option=${config_value-}"
if [[ ! $config_option =~ ^(branch|cmdver|commit|method|remote|version)$ ]]; then
error "Option $config_option not recognized"
fi
if [[ -z ${config_value-} ]]; then
OUT=true RUN git config --file="$gitrepo" "subrepo.$config_option"
say "Subrepo '$subdir' option '$config_option' has value '$output'."
return
fi
if ! $force_wanted; then
# Only allow changing method without force
if [[ $config_option != method ]]; then
error "This option is autogenerated, use '--force' to override."
fi
fi
if [[ $config_option == method ]]; then
if [[ ! $config_value =~ ^(merge|rebase)$ ]]; then
error "Not a valid method. Valid options are 'merge' or 'rebase'."
fi
fi
RUN git config --file="$gitrepo" "subrepo.$config_option" "$config_value"
say "Subrepo '$subdir' option '$config_option' set to '$config_value'."
}
# Launch the manpage viewer:
command:help() {
# source "${SOURCE_DIR}/help-functions.bash"
local cmd=${command_arguments[0]}
if [[ $cmd ]]; then
if can "help:$cmd"; then
"help:$cmd"
echo
else
err "No help found for '$cmd'"
fi
elif $all_wanted; then
help:all
else
exec git help subrepo
fi
msg_ok=0
}
# Print version info.
# TODO: Add short commit id after version.
# Will need to get it from repo or make install can put it somewhere.
command:version() {
cat <<...
git-subrepo Version: $VERSION
Copyright 2013-2020 Ingy döt Net
https://github.com/ingydotnet/git-subrepo
${BASH_SOURCE[0]}
Git Version: $git_version
...
:
}
command:upgrade() {
local path=$0
if [[ $path =~ ^/ && $path =~ ^(.*/git-subrepo)/lib/git-subrepo$ ]]; then
local subrepo_root=${BASH_REMATCH[1]}
(
o "Change directory to '$subrepo_root'."
cd "${BASH_REMATCH[1]}"
branch_name=$(git rev-parse --abbrev-ref HEAD)
if [[ $branch_name != master ]]; then
error "git-subrepo repo is not on the 'master' branch"
fi
o "'git pull' latest version."
RUN git pull --ff-only
say "git-subrepo is up to date."
)
else
die "\
Sorry. Your installation can't use the 'git subrepo upgrade' command. The
command only works if you installed git subrepo by adding
'/path/to/git-subrepo' to your PATH.
If you used 'make install' to install git-subrepo, then just do this:
cd /path/to/git-subrepo
git pull
make install
"
fi
}
#------------------------------------------------------------------------------
# Subrepo command worker functions.
#------------------------------------------------------------------------------
# Clone by fetching remote content into our subdir:
subrepo:clone() {
FAIL=false RUN git rev-parse HEAD
if ! OK; then
error "You can't clone into an empty repository"
fi
# Turn off force unless really a reclone:
if $force_wanted && [[ ! -f $gitrepo ]]; then
force_wanted=false
fi
if $force_wanted; then
o "--force indicates a reclone."
CALL subrepo:fetch
read-gitrepo-file
o "Check if we already are up to date."
if [[ $upstream_head_commit == "$subrepo_commit" ]]; then
reclone_up_to_date=true
return
fi
o "Remove the old subdir."
RUN git rm -r -- "$subdir"
else
assert-subdir-empty
if [[ -z $subrepo_branch ]]; then
o "Determine the upstream head branch."
get-upstream-head-branch
subrepo_branch=$output
fi
CALL subrepo:fetch
fi
o "Make the directory '$subdir/' for the clone."
RUN mkdir -p -- "$subdir"
o "Commit the new '$subdir/' content."
subrepo_commit_ref=$upstream_head_commit
CALL subrepo:commit
}
# Init a new subrepo from current repo:
subrepo:init() {
local branch_name=subrepo/${subref:??}
# Check if subdir is proper candidate for this init:
assert-subdir-ready-for-init
o "Put info into '$subdir/.gitrepo' file."
update-gitrepo-file
o "Add the new '$subdir/.gitrepo' file."
# -f from pull request #219. TODO needs test.
RUN git add -f -- "$gitrepo"
o "Commit new subrepo to the '$original_head_branch' branch."
subrepo_commit_ref=$original_head_commit
RUN git commit -m "$(get-commit-message)"
o "Create ref '$refs_subrepo_commit'."
git:make-ref "$refs_subrepo_commit" "$subrepo_commit_ref"
}
# Properly merge a local subrepo branch with upstream and commit to mainline:
subrepo:pull() {
CALL subrepo:fetch
# If forced pull, then clone instead
if $force_wanted; then
CALL subrepo:clone
return
fi
# Check if we already are up to date
# If the -u flag is present, always perform the operation
if [[ $upstream_head_commit == "$subrepo_commit" ]] && ! $update_wanted; then
OK=false; CODE=-1; return
fi
local branch_name=subrepo/$subref
git:delete-branch "$branch_name"
subrepo_commit_ref=$branch_name
o "Create subrepo branch '$branch_name'."
CALL subrepo:branch
cd "$worktree";
if [[ $join_method == rebase ]]; then
o "Rebase changes to $refs_subrepo_fetch"
FAIL=false OUT=true RUN git rebase "$refs_subrepo_fetch" "$branch_name"
if ! OK; then
say "The \"git rebase\" command failed:"
say
say " ${output//$'\n'/$'\n' }"
CODE=1
return
fi
else
o "Merge in changes from $refs_subrepo_fetch"
FAIL=false RUN git merge "$refs_subrepo_fetch"
if ! OK; then
say "The \"git merge\" command failed:"
say
say " ${output//$'\n'/$'\n' }"
CODE=1
return
fi
fi
o "Back to $start_pwd"
cd "$start_pwd";
o "Create ref '$refs_subrepo_branch' for branch '$branch_name'."
git:make-ref "$refs_subrepo_branch" "$branch_name"
o "Commit the new '$subrepo_commit_ref' content."
CALL subrepo:commit
}
# Push a properly merged subrepo branch upstream:
subrepo:push() {
local branch_name=$branch
local new_upstream=false
local branch_created=false
if [[ -z $branch_name ]]; then
FAIL=false OUT=false CALL subrepo:fetch
if ! OK; then
# Check if we are pushing to a new upstream repo (or branch) and just
# push the commit directly. This is common after a `git subrepo init`:
# Force to case in
local re="(^|"$'\n'")fatal: couldn't find remote ref "
if [[ ${output,,} =~ $re ]]; then
o "Pushing to new upstream: $subrepo_remote ($subrepo_branch)."
new_upstream=true
else
error "Fetch for push failed: $output"
fi
else
# Check that we are up to date:
o "Check upstream head against .gitrepo commit."
if ! $force_wanted; then
if [[ $upstream_head_commit != "$subrepo_commit" ]]; then
error "There are new changes upstream, you need to pull first."
fi
fi
fi
branch_name=subrepo/$subref
# We must make sure that a stale worktree is removed as well
worktree=$GIT_TMP/$branch_name
git:delete-branch "$branch_name"
if $squash_wanted; then
o "Squash commits"
subrepo_parent=HEAD^
fi
o "Create subrepo branch '$branch_name'."
CALL subrepo:branch "$branch_name"
cd "$worktree";
if [[ $join_method == rebase ]]; then
o "Rebase changes to $refs_subrepo_fetch"
FAIL=false OUT=true RUN git rebase "$refs_subrepo_fetch" "$branch_name"
if ! OK; then
say "The \"git rebase\" command failed:"
say
say " ${output//$'\n'/$'\n' }"
CODE=1
return
fi
fi
branch_created=true
cd "$start_pwd"
else
if $squash_wanted; then
error "Squash option (-s) can't be used with branch parameter"
fi
fi
o "Make sure that '$branch_name' exists."
git:branch-exists "$branch_name" ||
error "No subrepo branch '$branch_name' to push."
o "Check if we have something to push"
new_upstream_head_commit=$(git rev-parse "$branch_name")
if ! $new_upstream; then
if [[ $upstream_head_commit == "$new_upstream_head_commit" ]]; then
if $branch_created; then
o "Remove branch '$branch_name'."
git:delete-branch "$branch_name"
fi
OK=false
CODE=-2
return
fi
fi
if ! $force_wanted; then
o "Make sure '$branch_name' contains the '$refs_subrepo_fetch' HEAD."
if ! git:commit-in-rev-list "$upstream_head_commit" "$branch_name"; then
error "Can't commit: '$branch_name' doesn't contain upstream HEAD: " \
"$upstream_head_commit"
fi
fi
local force=''
"$force_wanted" && force=' --force'
o "Push$force branch '$branch_name' to '$subrepo_remote' ($subrepo_branch)."
# shellcheck disable=2086
RUN git push$force "$subrepo_remote" "$branch_name":"$subrepo_branch"
o "Create ref '$refs_subrepo_push' for branch '$branch_name'."
git:make-ref "$refs_subrepo_push" "$branch_name"
if $branch_created; then
o "Remove branch '$branch_name'."
git:delete-branch "$branch_name"
fi
o "Put updates into '$subdir/.gitrepo' file."
upstream_head_commit=$new_upstream_head_commit
subrepo_commit_ref=$upstream_head_commit
update-gitrepo-file
local commit_message
if [[ $wanted_commit_message ]]; then
commit_message=$wanted_commit_message
else
commit_message=$(get-commit-message)
fi
if [[ $commit_msg_file ]]; then
RUN git command --file "$commit_msg_file"
else
RUN git commit -m "$commit_message"
fi
}
# Fetch the subrepo's remote branch content:
subrepo:fetch() {
if [[ $subrepo_remote == none ]]; then
error "Can't fetch subrepo. Remote is 'none' in '$subdir/.gitrepo'."
fi
o "Fetch the upstream: $subrepo_remote ($subrepo_branch)."
RUN git fetch --no-tags --quiet "$subrepo_remote" "$subrepo_branch"
OK || return
o "Get the upstream subrepo HEAD commit."
OUT=true RUN git rev-parse FETCH_HEAD^0
upstream_head_commit=$output
o "Create ref '$refs_subrepo_fetch'."
git:make-ref "$refs_subrepo_fetch" FETCH_HEAD^0
}
# Create a subrepo branch containing all changes
# shellcheck disable=2120
subrepo:branch() {
local branch=${1:-"subrepo/$subref"}
o "Check if the '$branch' branch already exists."
git:branch-exists "$branch" && return
local last_gitrepo_commit=
local first_gitrepo_commit=
o "Subrepo parent: $subrepo_parent"
if [[ $subrepo_parent ]]; then
local prev_commit=
local ancestor=
o "Create new commits with parents into the subrepo fetch"
OUT=true RUN git rev-list --reverse --ancestry-path --topo-order "$subrepo_parent..HEAD"
local commit_list=$output
for commit in $commit_list; do
o "Working on $commit"
FAIL=false OUT=true RUN git config --blob \
"$commit:$subdir/.gitrepo" "subrepo.commit"
if [[ -z $output ]]; then
o "Ignore commit, no .gitrepo file"
continue
fi
local gitrepo_commit=$output
o ".gitrepo reference commit: $gitrepo_commit"
# Only include the commit if it's a child of the previous commit
# This way we create a single path between $subrepo_parent..HEAD
if [[ $ancestor ]]; then
local is_direct_child
is_direct_child=$(
git show -s --pretty=format:"%P" "$commit" |
grep "$ancestor"
) || true
o "is child: $is_direct_child"
if [[ -z $is_direct_child ]]; then
o "Ignore $commit, it's not in the selected path"
continue
fi
fi
# Remember the previous commit from the parent repo path
ancestor=$commit
o "Check for rebase"
if git:rev-exists "$refs_subrepo_fetch"; then
if ! git:commit-in-rev-list "$gitrepo_commit" "$refs_subrepo_fetch"; then
error "Local repository does not contain $gitrepo_commit. Try to 'git subrepo fetch $subref' or add the '-F' flag to always fetch the latest content."
fi
fi
o "Find parents"
local first_parent second_parent
first_parent=()
[[ $prev_commit ]] && first_parent=(-p "$prev_commit")
second_parent=()
if [[ -z $first_gitrepo_commit ]]; then
first_gitrepo_commit=$gitrepo_commit
second_parent=(-p "$gitrepo_commit")
fi
if [[ $join_method != rebase ]]; then
# In the rebase case we don't create merge commits
if [[ $gitrepo_commit != "$last_gitrepo_commit" ]]; then
second_parent=(-p "$gitrepo_commit")
last_gitrepo_commit=$gitrepo_commit
fi
fi
o "Create a new commit ${first_parent[*]} ${second_parent[*]}"
FAIL=false RUN git cat-file -e "$commit":"$subdir"
if OK; then
o "Create with content"
local PREVIOUS_IFS=$IFS
IFS=$'\n'
local author_info
mapfile -t author_info < <(git log -1 --date=default --format=%ad%n%ae%n%an "$commit")
IFS=$PREVIOUS_IFS
# When we create new commits we leave the author information unchanged
# the committer will though be updated to the current user
# This should be analog how cherrypicking is handled allowing git
# to store both the original author but also the responsible committer
# that created the local version of the commit and pushed it.
prev_commit=$(git log -n 1 --date=default --format=%B "$commit" |
GIT_AUTHOR_DATE=${author_info[0]} \
GIT_AUTHOR_EMAIL=${author_info[1]} \
GIT_AUTHOR_NAME=${author_info[2]} \
git commit-tree -F - "${first_parent[@]}" "${second_parent[@]}" "$commit":"$subdir")
else
o "Create empty placeholder"
prev_commit=$(git commit-tree -m "EMPTY" \
"${first_parent[*]}" "${second_parent[*]}" "4b825dc642cb6eb9a060e54bf8d69288fbee4904")
fi
done
o "Create branch '$branch' for this new commit set $prev_commit."
RUN git branch "$branch" "$prev_commit"
else
o "No parent setting, use the subdir content."
RUN git branch "$branch" HEAD
TTY=true FAIL=false RUN git filter-branch -f --subdirectory-filter \
"$subref" "$branch"
fi
o "Remove the .gitrepo file from $first_gitrepo_commit..$branch"
local filter=$branch
[[ $first_gitrepo_commit ]] && filter=$first_gitrepo_commit..$branch
FAIL=false RUN git filter-branch -f --prune-empty --tree-filter \
"rm -f .gitrepo" "$filter"
git:create-worktree "$branch"
o "Create ref '$refs_subrepo_branch'."
git:make-ref "$refs_subrepo_branch" "$branch"
}
# Commit a merged subrepo branch:
subrepo:commit() {
o "Check that '$subrepo_commit_ref' exists."
git:rev-exists "$subrepo_commit_ref" ||
error "Commit ref '$subrepo_commit_ref' does not exist."
if ! "$force_wanted"; then
local upstream=$upstream_head_commit
o "Make sure '$subrepo_commit_ref' contains the upstream HEAD."
if ! git:commit-in-rev-list "$upstream" "$subrepo_commit_ref"; then
error \
"Can't commit: '$subrepo_commit_ref' doesn't contain upstream HEAD."
fi
fi