forked from crowbar/crowbar
/
dev
executable file
·2220 lines (2076 loc) · 85.2 KB
/
dev
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
#!/bin/bash
export LANG="C"
export LC_ALL="C"
GEM_RE='([^0-9].*)-([0-9].*)'
readonly currdir="$PWD"
export PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin"
declare -A DEV_BRANCHES DEV_REMOTE_SOURCES DEV_REMOTE_BRANCHES DEV_ORIGIN_TYPES
declare -a DEV_REMOTES DEV_ALL_REMOTES
# The generation of the dev tool this is.
readonly DEV_VERSION=1
# The key -> value mapping in DEV_BRANCHES defines child -> parent relations
# between branches. A branch is considered a root branch if it has itself
# as a parent.
DEV_BRANCHES["master"]="master"
DEV_BRANCHES["openstack-os-build"]="master"
DEV_BRANCHES["hadoop-os-build"]="master"
DEV_BRANCHES["cloudera-os-build"]="master"
# DEV_REMOTE_SOURCES defines the source parts of the URLs that the
# various remotes map to. The "personal" remote is not listed in this
# hash, and most of the script assumes that origin always points at
# github and that the personal remote should be used as a backup target
# for all branches that were pulled from origin.
DEV_REMOTE_SOURCES["origin"]="https://github.com/dellcloudedge"
# DEV_REMOTE_BRANCHES defines what branches in the main Crowbar repository
# should be pulled and synced with what remotes.
# Barclamps do care about remote branches.
DEV_REMOTE_BRANCHES["origin"]="master openstack-os-build hadoop-os-build cloudera-os-build"
# DEV_REMOTES holds the remotes we will work on in the order they should be
# worked on when ordering matters.
DEV_REMOTES=("origin")
# Source our config file if we have one
[[ -f $HOME/.build-crowbar.conf ]] && \
. "$HOME/.build-crowbar.conf"
# Look for a local one.
[[ -f build-crowbar.conf ]] && \
. "build-crowbar.conf"
# Set up our proxies if we were asked to.
if [[ $USE_PROXY = "1" && $PROXY_HOST ]]; then
proxy_str="http://"
if [[ $PROXY_PASSWORD && $PROXY_USER ]]; then
proxy_str+="$PROXY_USER:$PROXY_PASSWORD@"
elif [[ $PROXY_USER ]]; then
proxy_str+="$PROXY_USER@"
fi
proxy_str+="$PROXY_HOST"
[[ $PROXY_PORT ]] && proxy_str+=":$PROXY_PORT"
[[ $no_proxy ]] || \
no_proxy="localhost,localhost.localdomain,127.0.0.0/8,$PROXY_HOST"
[[ $http_proxy ]] || http_proxy="$proxy_str/"
[[ $https_proxy ]] || https_proxy="$http_proxy"
export no_proxy http_proxy https_proxy
else
unset no_proxy http_proxy https_proxy
fi
DEV_ALL_REMOTES=("${DEV_REMOTES[@]}")
[[ $DEV_AVAILABLE_REMOTES ]] && DEV_REMOTES=($DEV_AVAILABLE_REMOTES)
# Location of the Crowbar checkout we are building from.
[[ $CROWBAR_DIR ]] || CROWBAR_DIR="${0%/*}"
[[ $CROWBAR_DIR = /* ]] || CROWBAR_DIR="$currdir/$CROWBAR_DIR"
[[ -f $CROWBAR_DIR/build_crowbar.sh && -d $CROWBAR_DIR/.git ]] || \
die "$CROWBAR_DIR is not a git checkout of Crowbar!"
export CROWBAR_DIR
. "$CROWBAR_DIR/build_lib.sh" || exit 1
trap - 0 INT QUIT TERM
if [[ $DEV_GITHUB_PASSWD ]]; then
debug "Migrating Github password information to $HOME/.netrc"
for mach in github.com api.github.com; do
grep -q "^$mach" "$HOME/.netrc" &>/dev/null && continue
printf "\nmachine %s login %s password %s\n" \
"$mach" "$DEV_GITHUB_ID" "$DEV_GITHUB_PASSWD" >> "$HOME/.netrc"
done
chmod 600 "$HOME/.netrc"
sed -ie 's/DEV_GITHUB_PASSWD=.*//' "$HOME/.build-crowbar.conf"
debug "Please remove your embedded login information from the remote URLS."
fi
which gem &>/dev/null || \
die "Rubygems not installed, and some of our helpers need a JSON gem."
gem list -i json &>/dev/null || \
die "JSON gem not installed. Please install it with gem install json."
get_repo_cfg() { in_repo git config --get "$1"; }
get_barclamp_cfg() { in_barclamp "$1" git config --get "$2"; }
dev_is_setup() {
[[ $DEV_GITHUB_ID || $1 = setup ]] || \
die "dev has not been configured. Please run $CROWBAR_DIR/dev setup first."
thisrev=$(get_repo_cfg crowbar.dev.version) && \
(( thisrev == DEV_VERSION )) || [[ $1 = setup ]] || {
VERBOSE=true
debug "Crowbar repository out of sync with dev tool revision."
debug "Please run $0 setup to update it."
exit 1
}
}
unset thisrev
set -o pipefail
# Given a branch, echo all the child branches for this
# branch in the same release.
child_branches() {
local prefix="${1%/*}/" parent=${1##*/} br
[[ ${prefix%/} = $1 ]] && prefix=''
for br in "${!DEV_BRANCHES[@]}"; do
[[ ${DEV_BRANCHES[$br]} && \
${DEV_BRANCHES[$br]} != $br && \
${DEV_BRANCHES[$br]} = $parent ]] || continue
in_repo branch_exists "$prefix$br" || continue
echo "$prefix$br"
done
return 0
}
# Given a branch, echo its parent in the current release.
parent_branch() {
local prefix="${1%/*}/" parent=${1##*/} br
[[ ${prefix%/} = $1 ]] && prefix=''
local p="${DEV_BRANCHES[${1##*/}]}"
if [[ $p && $p != ${1##*/} ]]; then
echo "${prefix}${p}"
return 0
fi
return 1
}
# Echo all of the root branches in the current release.
root_branches() {
local b release releases=() prefix
case $1 in
'') releases=("$(current_release)");;
'--all') releases=($(show_releases));;
*) is_in "$1" "$(show_releases)" || \
die "root_branches: $1 is not a release!"
releases=("$1");;
esac
for release in "${releases[@]}"; do
prefix="$(branch_prefix "$release")"
for b in "${!DEV_BRANCHES[@]}"; do
[[ $b = ${DEV_BRANCHES[$b]} ]] || continue
in_repo branch_exists "${prefix}$b" || continue
echo "${prefix}$b"
done
done
return 0
}
ordered_branches() {
# $@ = branches to print all of the children of.
[[ $@ ]] && local branches=("$@") || local branches=("$(root_branches)")
local b children=()
printf "%s\n" "${branches[@]}"
for b in "${branches[@]}"; do
children=($(child_branches "$b"))
[[ $children ]] && ordered_branches "${children[@]}"
done
}
all_ordered_branches() { ordered_branches $(root_branches --all); }
branches_for_release() { ordered_branches $(root_branches "$1"); }
# Given a branch, print the first remote that branch is "owned" by
remote_for_branch() {
local remote
for remote in "${DEV_ALL_REMOTES[@]}"; do
is_in "${1##*/}" "${DEV_REMOTE_BRANCHES[$remote]}" || continue
echo "$remote"
return 0
done
return 1
}
# Given a remote and a list of branches,
# return just the branches that are in that remote.
filter_branches_for_remote() {
local remote="$1" b
shift
for b in "$@"; do
is_in "${b##*/}" "${DEV_REMOTE_BRANCHES[$remote]}" || continue
echo "$b"
done
}
# Test to see if a specific repository is clean.
# Ignores submodules and unchecked-in directories that are git repositories.
git_is_clean() {
local line hpath ret=0 opt
local stat_cmd="git status --porcelain --ignore-submodules" quiet=''
local paths=()
while [[ $1 ]]; do
opt="$1"; shift
case $opt in
--barclamps) stat_cmd="git status --porcelain";;
-q|--quiet) quiet=true;;
--paths)
while [[ $1 && $1 != '-'* ]]; do
paths+=("$1")
shift
done;;
*) die "Unknown option $opt passed to git_is_clean.";;
esac
done
[[ $paths ]] && stat_cmd+=" -- ${paths[*]}"
while read line; do
case $line in
# Untracked file. Ignore it if it is also a git repo,
# complain otherwise.
'??'*) hpath=${line%% ->*}
hpath=${hpath#* }
[[ -d $PWD/$hpath.git || -f $PWD/$hpath.git ]] && continue
ret=1; break;;
'') continue;;
*) ret=1; break;;
esac
done < <($stat_cmd)
[[ $ret = 0 ]] && return
[[ $quiet ]] || {
echo "$PWD:"
git status -- "${paths[@]}"
}
[[ $IGNORE_CLEAN ]] && return 0
return 1
}
git_push() {
[[ $DEBUG = true ]] && return
git push "$@"
}
# Get the current release we are working on, which is a function of
# the currently checked-out branch.
current_release() {
local ans="$(in_repo git symbolic-ref HEAD)" || \
die "current_release: Cannot get current branch information!"
ans=${ans#refs/heads/}
case $ans in
personal/*) continue;;
stable/*) echo "stable";;
release/*)
ans="${ans#release/}"
echo "${ans%%/*}";;
feature/*) echo "${ans%/*}";;
*) echo "development";;
esac
}
# Test to see if a barclamp is clean.
barclamp_is_clean() {
local bc="$1"; shift
in_barclamp "$bc" git_is_clean "$@"; }
# Test to see if all our barclamps are clean.
barclamps_are_clean() {
local bc res=0
for bc in "$CROWBAR_DIR/barclamps/"*; do
is_barclamp "${bc##*/}" || continue
in_barclamp "${bc##*/}" git_is_clean "$@" || res=1
done
return $res
}
# Test to see if all the Crowbar repositories are clean.
crowbar_is_clean() {
local res=0
barclamps_are_clean "$@" && in_repo git_is_clean "$@" && return 0
echo "Your crowbar repositories are not clean."
echo "Please review the git status output above, and add and commit/stash as needed."
return 1
}
# Get the list of barclamps that originate from this branch.
# $1 must be a branch name, and it must be some sort of release branch.
barclamps_from_branch() {
# $1 = branch in Crowbar.
is_in "${1##*/}" "${!DEV_BRANCHES[*]}" || \
die "Will not be able to determine parent of $1."
# If we don't have a parent branch, we are one of the roots.
local parent=$(parent_branch "$1")
if [[ ! $parent ]]; then
barclamps_in_branch "$1"
return 0
fi
sort <(barclamps_in_branch "$1") <(barclamps_in_branch "$parent") |uniq -u
}
# Perform an initial clone of a barclamp. Takes care to ensure that
# the origin remote is set appropriatly.
clone_barclamp() {
# $1 = name of the barclamp
# $2 = name of remote to clone it from.
local repo remote branch
for remote in "${DEV_REMOTES[@]}"; do
for branch in $(filter_branches_for_remote "$remote" \
$(ordered_branches)); do
is_in "$1" \
"$(barclamps_in_branch $branch)" || \
continue
in_repo git submodule init "barclamps/$1"
repo="$(get_repo_cfg "submodule.barclamps/$1.url")"
repo="${repo##*/}"
repo="${repo##*:}"
repo="${repo%.git}"
get_repo_cfg "submodule.barclamps/$1.url" \
"${DEV_REMOTE_SOURCES[$remote]}/$repo"
in_repo git submodule update "barclamps/$1" || \
die "Could not perform initial clone of barclamp $1"
( cd "barclamps/$1"
while read ref br; do
[[ br = refs/remotes/origin/* ]] || continue
br=${br#refs/remotes/}
branch_exists "${br#origin/}" && continue
git branch --track "${br#origin/}" "$br"
done < <(git show-ref master)
)
return
done
done
die "Asked to clone $1, but could not!"
}
quiet_checkout() {
local res=''
if ! res=$(git checkout -q "$@" 2>&1); then
echo "$res" >&2
return 1
fi
}
keys() {
local matcher="$1"
shift
while [[ $1 ]]; do
[[ $1 =~ $matcher ]] && echo "$1"
shift
done
}
show_github_repos() {
local -A res
local r
. <(curl_and_res -f "https://api.github.com/user/repos" | \
parse_yml_or_json - res 2>/dev/null)
for r in $(keys '^[0-9]+\.name$' "${!res[@]}"); do
printf "%s\n" "${res[$r]}"
done
}
__switch_barclamp_to() {
local parent prefix parent_prefix ref
parent="$(find_best_parent "$1")"
prefix="$(branch_prefix "$1")"
parent_prefix=$(branch_prefix "$parent")
for ref in origin personal; do
if git rev-parse --verify -q "$ref/${prefix}master" &>/dev/null; then
git branch -f "${prefix}master" "$ref/${prefix}master"
return 0
fi
done
if ! branch_exists "${parent_prefix}master"; then
__switch_barclamp_to "$parent" || exit 1
fi
git branch -f --no-track \
"$(branch_prefix "$1")master" "${parent_prefix}master"
}
switch_barclamp_to() {
# $1 = barclamp
# $2 = release to switch to.
local prefix
prefix="$(branch_prefix "$2")"
if ! in_barclamp "$1" branch_exists "${prefix}master"; then
in_barclamp "$1" __switch_barclamp_to "$2" || exit 1
fi
in_barclamp "$1" quiet_checkout "${prefix}master";
}
# Set up barclamps for the appropriate branch. Barclamps that are not members
# of the current branch will be checked out to an empty branch, which will be
# created if it does not exist.
switch_barclamps_to() {
# $1 = Either a branch or "submodule-ref"
local -A barclamps
local m t ref p bc head
# Read barclamps that should be there.
while read m t ref p; do
[[ $m = 160000 && $t = commit ]] || continue
if [[ $1 = submodule-ref ]]; then
barclamps[${p#/}]=$ref
else
barclamps[${p#/}]="refs/heads/$1"
fi
done < <(in_repo git ls-tree HEAD barclamps/)
# Update barclamps that are there.
for bc in "$CROWBAR_DIR/barclamps/"*; do
[[ -d $bc/.git || -f $bc/.git ]] || \
clone_barclamp "${bc##*/}"
bc="${bc#$CROWBAR_DIR/}"
if [[ ${barclamps[$bc]} && ${barclamps[$bc]} = refs/heads/* ]]; then
head=$(in_barclamp "${bc##*/}" git symbolic-ref HEAD)
[[ $head = ${barclamps[$bc]} ]] && {
unset barclamps[$bc]
continue
}
barclamps[$bc]="${barclamps[$bc]#refs/heads/}"
elif [[ ${barclamps[$bc]} ]]; then
ref=$(in_barclamp "${bc##*/}" git rev-parse --verify -q HEAD)
[[ $ref = $(in_barclamp "${bc##*/}" \
git rev-parse --verify -q "${barclamps[$bc]}") ]] && {
unset barclamps[$bc]
continue
}
else
[[ $(in_barclamp "${bc##*/}" git symbolic-ref HEAD) = \
refs/heads/empty-branch ]] && {
unset barclamps[$bc]
continue
}
barclamps[$bc]="the empty branch"
fi
debug "Barclamp ${bc##*/}: Checking out ${barclamps[$bc]}"
case ${barclamps[$bc]} in
'the empty branch') in_barclamp "${bc##*/}" to_empty_branch;;
'') continue;;
*) switch_barclamp_to "${bc##*/}" "$(release_for_branch "$1")";;
esac || die "barclamp ${bc##*/}: Could not check out ${barclamps[$bc]}"
unset barclamps[$bc]
done
# Create barclamps that are not there.
for bc in "${!barclamps[@]}"; do
[[ ${barclamps[$bc]} ]] || continue
clone_barclamp "${bc##*/}"
in_barclamp "${bc##*/}" git checkout "${barclamps[$bc]}"
done
}
# Update tracking branches from the named remote,
# or origin if no remote is passed.
# Expects to be called within a specific repository
update_tracking_branches() {
# Create tracking branches for any branches from this remote
# that do not already exist.
local remote=${1:-origin}
while read p br; do
[[ $br = refs/remotes/$remote* ]] || continue
br=${br#refs/remotes/$remote/}
[[ $br = HEAD ]] && continue
[[ $br = personal/* ]] && continue
[[ $br = pull-req* ]] && continue
branch_exists "$br" && continue
debug "$PWD: Creating tracking branch $br from remote $remote"
git branch "$br" "$remote/$br"
done < <(git show-ref)
}
update_all_tracking_branches() {
local remote bc
for remote in "${DEV_REMOTES[@]}"; do
in_repo update_tracking_branches "$remote"
done
for bc in "$CROWBAR_DIR/barclamps/"*; do
[[ -d $bc/.git || -f $bc/.git ]] || continue
(cd "$bc"; update_tracking_branches origin)
done
}
scrub_merged_pull_requests() {
# $@ = branches to test for mergedness
local br ref pull_req
local -A to_remove pull_reqs heads
git_config_has remote.personal.url || return
while read ref br; do
case $br in
refs/heads/*)
ref=${br#refs/heads/}
heads[${ref//\//-}]+="$br ";;
refs/remotes/personal/pull-req-*)
ref=${br#refs/remotes/personal/pull-req-}
ref=${ref#heads-}
ref=${ref%-*}
ref=${ref%-0}
pull_reqs["$br"]="$ref";;
esac
done < <(git show-ref)
[[ ${pull_reqs[*]} ]] || return 0
for pull_req in "${!pull_reqs[@]}"; do
ref="${pull_reqs[$pull_req]}"
[[ ${heads[$ref]} ]] || continue
for br in ${heads["$ref"]}; do
branches_synced . "$br" "$pull_req" || \
[[ $1 = '--all' ]] || continue
to_remove["${pull_req#refs/remotes/personal/}"]="true"
continue 2
done
done
[[ ${!to_remove[*]} ]] || return
git_push --delete personal "${!to_remove[@]}"
git remote prune personal
}
# Fetch (but do not merge) updates from all our remotes, in both the
# main Crowbar repository and the barclamps.
fetch_all() {
local remote remotes=() barclamp b branches=()
dev_is_setup
if [[ $@ ]]; then
remotes=("$@")
else
remotes=("${DEV_REMOTES[@]}" personal)
fi
for remote in "${remotes[@]}"; do
in_repo git_config_has "remote.$remote.url" || continue
debug "Fetching from remote $remote:"
debug " Crowbar"
in_repo git fetch -q "$remote" || continue
branches=($(filter_branches_for_remote "$remote" \
$(all_ordered_branches)))
if [[ ${branches[@]} ]]; then
in_repo update_tracking_branches "$remote"
local -A touched_barclamps
for b in "${branches[@]}"; do
for barclamp in $(barclamps_from_branch "$b"); do
[[ ${touched_barclamps[$barclamp]} ]] && continue
debug " Barclamp $barclamp"
in_barclamp "$barclamp" git fetch -q origin || \
die "Could not fetch updates for $barclamp"
in_barclamp "$barclamp" update_tracking_branches origin
touched_barclamps[$barclamp]="true"
done
done
unset touched_barclamps
else
for barclamp in "$CROWBAR_DIR/barclamps/"*; do
[[ -d $barclamp ]] || continue
( cd "$barclamp"
git_config_has "remote.${remote}.url" || continue
debug " Barclamp ${barclamp##*/}"
git fetch -q "$remote"
update_tracking_branches origin
)
done
fi
done
scrub_merged_pulls
}
scrub_merged_pulls() {
in_repo scrub_merged_pull_requests "$@"
for barclamp in "$CROWBAR_DIR/barclamps/"*; do
[[ -d $barclamp ]] || continue
in_barclamp "${barclamp##*/}" scrub_merged_pull_requests "$@"
done
}
# Helper function for calling curl to talk to github.
curl_and_res() {
local __r
r="$(curl -n "$@" 2>/dev/null)"
case $? in
0) printf '%s' "$r";;
7) echo "Unable to contact Github, please try again later." >&2
return 1;;
22) return 1;;
*) echo "Curl reported error ${?}!." >&2
return 1;;
esac
}
# Fork a repository on Github.
github_fork() {
# $1 = user to fork from
# $2 = repo to fork
curl_and_res -f -X POST \
"https://api.github.com/repos/$1/$2/forks" >&/dev/null || \
die "Could not fork $1/$2!"
}
github_url() {
# $1 = repository name.
# $@ = one of the following:
# -p = Use personal repository (based on DEV_GITHUB_ID) instead of
# dellcloudedge
# -a = Put github authentication info in the generated URL.
local repo="$1" user="dellcloudedge"
shift
while [[ $1 ]]; do
case $1 in
-p) user="$DEV_GITHUB_ID";;
*) die "Unknown option $1 to github_url";;
esac
shift
done
echo "https://github.com/${user}/$repo"
}
git_config_has() { git config --get "$1" &>/dev/null; }
get_repo_name() {
local repo
repo="$(git config --get remote.origin.url)" || \
die "$PWD: Could not get the URL for the origin remote for this repo."
repo="${repo##*:}"
echo "${repo##*/}"
}
add_remote() {
# $1 = name of the remote to add.
# $2 = base part of the repo to add.
git_config_has "remote.$1.url" && return 0
local repo
repo="$(get_repo_name)" || exit 1
repo="$2/$repo"
git ls-remote "$repo" refs/heads/master &>/dev/null || {
debug "No git repo at $repo, skipping."
return 0
}
git remote add "$1" "$repo"
git fetch "$1"
}
rm_remote() {
git_config_has "remote.$1.url" || return 0
git remote rm "$1"
}
set_url_remote() {
git_config_has "remote.$1.url" || return 0
local repo
repo="$(get_repo_name)" || exit 1
repo="$2/$repo"
git ls-remote "$repo" refs/heads/master &>/dev/null || {
debug "No git repo at $repo, skipping."
return 0
}
git set-url "$1" "$repo"
}
show_remote() {
if [[ $1 ]]; then
git remote show "$1"
else
git remote show
fi
}
remote_wrapper() {
# $1 = one of "add", "rm", "set-url", "show"
# $2 = name of remote
# $3 = base part of the remote URL. Only used for add and set-url.
local remote urlbase action bc
remote="$2"
urlbase="$3"
case $1 in
add) action=add_remote
if ! in_repo \
git_config_has "crowbar.remote.${remote}.urlbase"; then
in_repo git config "crowbar.remote.${remote}.urlbase" "$3"
fi;;
rm) action=rm_remote
if in_repo git_config_has "crowbar.remote.${remote}.urlbase"; then
in_repo git config --unset "crowbar.remote.${remote}.urlbase"
fi
;;
set-url) action=set_url_remote
if in_repo git_config_has "crowbar.remote.${remote}.urlbase"; then
in_repo git config "crowbar.remote.${remote}.urlbase" "$3"
else
die "Cannot set-url for a remote your have not added."
fi;;
show) action=show_remote;;
''|help) die "Please pass one of add, rm, set-url, or show.";;
*) die "Unknown action $1 for remote.";;
esac
for bc in "$CROWBAR_DIR/barclamps/"*; do
[[ -d $bc ]] || continue
debug "Barclamp: ${bc##*/}"
in_barclamp "${bc##*/}" $action "$remote" "$urlbase"
done
debug "Crowbar:"
in_repo $action "$remote" "$urlbase"
}
# Perform initial setup. If you make any changes to this function, take
# care to make sure it stays idempotent.
setup() {
local p remote release br head
local -A touched_branches fetched_barclamps
crowbar_is_clean || \
die "Crowbar repo must be clean before trying to set things up!"
# Make sure we have Github login credentials as the very first thing.
[[ $DEV_GITHUB_ID ]] || {
local DEV_GITHUB_PASSWD
read -p "Enter your Github username: " DEV_GITHUB_ID
curl_and_res -f \
"https://api.github.com/users/$DEV_GITHUB_ID" &>/dev/null || \
die "Could not verify that $DEV_GITHUB_ID is a valid Github user.."
while [[ $p != $DEV_GITHUB_PASSWD || ! $p ]]; do
[[ $p ]] && echo "Passwords did not match, try again."
read -s -p "Enter your Github password: " DEV_GITHUB_PASSWD
echo
read -s -p "Enter your Github password again: " p
echo
done
curl_and_res -f -u "$DEV_GITHUB_ID:$DEV_GITHUB_PASSWD" \
https://api.github.com/user &>/dev/null || {
echo "Unable to authenticate as Github user $DEV_GITHUB_ID." >&2
die "Please try again when you have Github access."
}
for mach in github.com api.github.com; do
grep -q "^$mach" "$HOME/.netrc" &>/dev/null && continue
printf "\nmachine %s login %s password %s\n" \
"$mach" "$DEV_GITHUB_ID" "$DEV_GITHUB_PASSWD" >> "$HOME/.netrc"
done
chmod 600 "$HOME/.netrc"
printf "DEV_GITHUB_ID=%q\n" "$DEV_GITHUB_ID" >> "$HOME/.build-crowbar.conf"
}
# We have a baked-in assumption that Github is our origin, and that
# any repositories that are named the same thing as a repository from
# dellcloudedge is in fact a fork of a dellcloudedge repo.
# We should not make that assumption at some point in the future.
echo "Finding Github repos..."
local repos=($(show_github_repos))
if ! is_in crowbar "${repos[*]}"; then
echo "Creating your fork of Crowbar on Github."
github_fork dellcloudedge crowbar || \
die "Unable to create your fork of Crowbar."
fi
if ! in_repo git_config_has remote.origin.pushurl; then
echo "Creating a push URL for crowbar on Github"
in_repo git remote set-url --push origin \
"$(github_url crowbar.git)"
fi
if ! in_repo git_config_has remote.personal.url; then
echo "Adding remote for personal fork of crowbar on Github."
in_repo git remote add personal \
"$(github_url crowbar.git -p)"
in_repo git fetch -q personal || \
die "Cannot fetch repository info for your Crowbar fork on Github!"
in_repo git remote set-head -a personal
fi
in_repo git config branch.autosetupmerge true &>/dev/null
in_repo git config crowbar.backup.method per-remote
head_release="$(current_release)"
# Set up the rest of our upstream remotes.
for remote in "${DEV_REMOTES[@]}"; do
# Add a remote if we don't already have one.
if [[ ${DEV_REMOTE_SOURCES[$remote]} ]] && \
! in_repo git_config_has "remote.$remote.url"; then
in_repo git remote add "$remote" \
"${DEV_REMOTE_SOURCES[$remote]}/crowbar.git"
fi
in_repo git fetch -q "$remote" || \
die "Unable to fetch remote references for $remote"
if ! in_repo git_config_has "crowbar.backup.$remote.method"; then
case $remote in
origin)
in_repo git config crowbar.backup.$remote.method remote
in_repo git config crowbar.backup.$remote.remote personal;;
*)
in_repo git config crowbar.backup.$remote.method prefix
in_repo git config crowbar.backup.$remote.remote "$remote"
in_repo git config crowbar.backup.$remote.prefix \
"personal/$DEV_GITHUB_ID";;
esac
fi
[[ $head ]] || head=$(in_repo git symbolic-ref HEAD) || \
die "HEAD does not point at a branch, cannot finish setup."
in_repo update_tracking_branches "$remote"
for release in $(show_releases); do
switch_release "$release"
checkout master
for b in $(filter_branches_for_remote "$remote" \
$(branches_for_release)); do
in_repo quiet_checkout "$b" || \
die "Cannot checkout $b"
for bc in $(barclamps_from_branch "$b"); do
debug "$release: $remote: $b: $bc"
in_repo git_config_has "submodule.barclamps/$bc.url" || {
clone_barclamp "$bc"
fetched_barclamps["$bc"]="true"
}
repo="$(get_repo_cfg "submodule.barclamps/$bc.url")"
repo="${repo##*/}"
repo="${repo##*:}"
repo="${repo%.git}"
[[ ${fetched_barclamps[$bc]} ]] || {
in_barclamp "$bc" git fetch origin
fetched_barclamps["$bc"]="true"
}
in_barclamp "$bc" update_tracking_branches origin
in_barclamp "$bc" git_config_has \
crowbar.backup.origin.method && continue
if [[ $remote = origin ]]; then
if ! is_in "$repo" "${repos[*]}"; then
github_fork dellcloudedge "$repo" || \
die "Could not create a personal fork of $repo"
fi
if ! in_barclamp "$bc" \
git_config_has remote.personal.url; then
in_barclamp "$bc" git remote add personal \
$(github_url "$repo" -p)
in_barclamp "$bc" git fetch -q personal || \
die "Cannot fetch repository info for your fork of $repo."
in_barclamp "$bc" git remote set-head -a personal
fi
if ! in_barclamp "$bc" \
git_config_has remote.origin.pushurl; then
in_barclamp "$bc" git remote set-url --push origin \
"$(github_url $repo)"
fi
in_barclamp "$bc" git config \
crowbar.backup.origin.method remote
in_barclamp "$bc" git config \
crowbar.backup.origin.remote personal
else
in_barclamp "$bc" git config \
crowbar.backup.origin.method prefix
in_barclamp "$bc" \
git config crowbar.backup.origin.prefix \
"personal/$DEV_GITHUB_ID"
fi
in_barclamp "$bc" git config branch.autosetupmerge true
done
done
done
done
[[ $head_release != $(current_release) ]] && switch_release "$head_release"
[[ $head ]] && quiet_checkout "${head#refs/heads/}"
for bc in "$CROWBAR_DIR/barclamps/"*; do
( cd "$bc"
[[ -d .git || -f .git ]] || continue
quiet_checkout "${DEV_RELEASE_PREFIX}master"
)
done
in_repo git config crowbar.dev.version "$DEV_VERSION"
}
# Test repository $1 to see if commit $2 is in sync with $3.
# In this case, "in sync" is defined as:
# * $2 and $3 point at the same commit, or
# * There are no commits in the set of all commits reachable from $3 that
# are not also reachable from $2.
branches_synced() {
# $1 = repository to operate in
# $2 = local branch to test
# $3 = remote branch to test
[[ -d $1/.git || -f $1/.git ]] || \
die "branches_synced: $1 is not a git repo"
[[ $VERBOSE2 ]] && echo "Checking to see if out of sync: $2 $3"
(cd "$1"; git rev-parse --verify -q "$2" &>/dev/null) || \
return 1
(cd "$1"; git rev-parse --verify -q "$3" &>/dev/null) || \
return 1
# $2 and $3 resolve to the same commit, they are in sync.
(cd "$1"; [[ $(git rev-parse "$2") = $(git rev-parse "$3") ]] ) && return 0
# Test to see if there are any commits in $3 that are not
# reachable from $2. If there are, then the branches are not synced.
(cd "$1"; [[ ! $(git rev-list "$2..$3") ]] ) && return 0
return 1
}
# Back up our current commits to local branches in a barclamp that:
# * Have a matching branch at origin, or
# * Have a matching remote at $1.
backup_to_remote() {
local remote=$1 branch branches=()
shift
git remote prune "$remote"
for branch in "$@"; do
if [[ $branch = ':'* && $(git rev-parse --verify -q \
"refs/remotes/$remote/${branch#:}") ]]; then
branches+=("$branch")
elif branch_exists "$branch" && \
[[ $(git rev-parse --verify -q "refs/remotes/$remote/$branch") != \
$(git rev-parse --verify -q "refs/heads/$branch") ]]; then
branches+=("$branch")
fi
done
if [[ $branches ]]; then
if [[ ! $PWD =~ /barclamps/ ]]; then
debug "Crowbar: Backing up ${branches[*]}"
else
debug "Barclamp ${PWD##*/}: Backing up ${branches[*]}"
fi
git_push -qf "$remote" "${branches[@]}"
fi
}
# Back up our current commits to local branches in a barclamp by force-creating
# a new branch named personal/$DEV_GITHUB_ID/$branch for each branch, and then
# force-pushing the newly created or updated branch back to the origin remote.
backup_with_prefix () {
local remote="$2" prefix="$1" branches=() branch ref
[[ $remote && $prefix ]] || \
die "generic_backup: Must pass a remote and a prefix."
shift 2
local remote_prefix="refs/remotes/$remote/$prefix"
[[ $(git symbolic-ref HEAD) = "refs/heads/$prefix"/* ]] && \
die "prefix_backup: $prefix branch checked out, cannot proceed."
git remote prune "$remote"
for branch in "$@"; do
if [[ $branch = ':'* && $(git rev-parse --verify -q \
"$remote_prefix/${branch#:}") ]]; then
branches+=(":$prefix/${branch#:}")
continue
fi
branch_exists "$branch" || continue
if [[ $branch = ${prefix}* ]]; then
debug "Cleaning up ${branch#refs/heads}"
git branch -D "${branch#refs/heads}"
git_push -q "$remote" ":${branch#refs/heads}"
continue
fi
[[ $(git rev-parse --verify -q "$remote_prefix/$branch") && \
$(git rev-parse "$remote_prefix/$branch") = \
$(git rev-parse "refs/heads/$branch") ]] && continue
branches+=("$branch:$prefix/$branch")
done
if [[ $branches ]]; then
if [[ ! $PWD =~ /barclamps/ ]]; then
debug "Crowbar: Backing up ${branches[*]%%:*}"
else
debug "Barclamp ${PWD##*/}: Backing up ${branches[*]%%:*}"
fi
git_push -qf "$remote" "${branches[@]}"
fi
}
branches_to_backup_to_remote() {
# $1 = "backup" remote
# $2 = "upstream" remote
# $@ = branches to exclude from consideration
# Echos each branch that should be backed up via this method.
local target="$1" upstream="$2" br ref
local -A ignore
shift 2
[[ $upstream && $target && $upstream != $target ]] && \
git_config_has "remote.$upstream.url" && \
git_config_has "remote.$target.url" || \
die "branches_to_remote: Invalid branch $upstream or $target."
for br in "$@"; do
ignore["$br"]="true"
done
# Push branches that have been pushed to personal in the past
while read ref br; do
[[ $br = */HEAD ]] && continue
br="${br#refs/heads/}"
if [[ ${ignore[$br]} = true ]]; then
# Skip branches we were asked to ignore.
continue
elif [[ $(remote_for_branch ${br##*/}) = $upstream ]]; then
# Back up branches that we "have" remotes for.
echo "$br"
elif ! (git rev-parse --verify -q "refs/remotes/$target/$br" || \
git rev-parse --verify -q "refs/remotes/$upstream/$br") &>/dev/null
then
# If we do not have a ref for this branch on the personal remote
# or on the origin remote, this branch should not be backed up.
continue
else
# Back it up.
echo "$br"
fi
done < <(git show-ref --heads)
# Find branches that have been removed locally, and make sure their
# backups have also been removed.
while read ref br; do
[[ $br = */HEAD ]] && continue
[[ $br = refs/remotes/$target/* ]] || continue
branch_exists "${br#refs/remotes/$target/}" && continue
[[ $br = refs/remotes/$target/pull-req* ]] && continue
echo ":${br#refs/remotes/$target/}"
done < <(git show-ref)
}
branches_to_backup_to_prefix() {
local prefix="$1" remote="$2" br ref b
local -A ignore