-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromtool.sh
executable file
·734 lines (612 loc) · 20.4 KB
/
romtool.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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
#!/usr/bin/env bash
# Copyright (C) BiancaProject
# Copyright (C) 2019-2023 alanndz <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
NAME="ROMTool"
VERSION="1.0"
CWD=$(pwd)
dbg() { echo -e "\e[92m[*] $@\e[39m"; }
err() { echo -e "\e[91m[!] $@\e[39m"; exit 1; }
grn() { echo -e "\e[92m$@\e[39m"; }
red() { echo -e "\e[91m$@\e[39m"; }
blu() { echo -e "\e[34m$@\e[39m"; }
prin() { echo -e "$@"; }
# Checking dependencies
for dep in git env basename mkdir rm repo
do
! command -v "$dep" &> /dev/null && err "Unable to locate dependency $dep. Exiting."
done
CONF="$CWD/.romtoolconfig"
##### Setup Config #####
Config.xml() { git config -f "$CONF" rom.xml "$@"; }
Config.remote() { git config -f "$CONF" rom.remote "$@"; }
# Environment default for BiancaProject
AOSP="https://android.googlesource.com"
LIST="${CWD}/project.list"
BLACKLIST=$(cat "${CWD}/ROMTool/blacklist")
MANIFEST=$(Config.xml)
REMOTE=$(Config.remote)
MANIFEST="${MANIFEST:-$CWD/.repo/manifests/snippets/bianca.xml}"
REMOTE="${REMOTE:-dudu}"
doInit() {
[[ -e "build/envsetup.sh" ]] || err "Error: Must run from root of repo"
dbg "Info: Creating project.list"
# Build list of Bianca Project forked repos
local PROJECTPATHS=$(grep "remote=\"${REMOTE}" "${MANIFEST}" | sed -n 's/.*path="\([^"]\+\)".*/\1/p')
for PROJECTPATH in ${PROJECTPATHS}; do
if [[ "${BLACKLIST}" =~ "${PROJECTPATH}" ]]; then
continue
fi
echo "${PROJECTPATH}"
done | sort > "${LIST}"
}
checkPath() {
# Checking file build/envsetup.sh
if [ ! -e "build/envsetup.sh" ]; then
err "Error: Must run from root of repo"
fi
# Checking file project.list, if false run doInit
[[ -f $LIST ]] || doInit #err "Error: File project.list not found, please do init first"
}
doList() {
local PROJECTPATH=()
local CURRENTBRANCH=()
local REPO=($(cat $LIST))
for i in "${REPO[@]}"
do
PROJECTPATH+=("$i")
CURRENTBRANCH+=("$(git -C "$CWD/$i" branch --show-current || echo " ")")
done
paste <(printf '%s\n' `blu Repo` "${PROJECTPATH[@]}") \
<(printf '%s\n' `blu Branch` "${CURRENTBRANCH[@]}") \
| column -ts $'\t'
}
changeBranchManifest() {
local XML="$CWD/manifest/snippets/bianca.xml"
local NEW=$1
local SC=($(cat $CWD/success.list))
local FL=($(cat $CWD/failed.list))
for i in ${SC[@]}
do
line=$(grep -n \"$i\" "$XML" | cut -d: -f 1)
if [[ -n $line ]]
then
sed -i "${line}s|remote=\"dudu\"|remote=\"dudu\" revision=\"${NEW}\"|" "$XML"
fi
done
for i in ${FL[@]}
do
line=$(grep -n \"$i\" "$XML" | cut -d: -f 1)
git -C $i rev-parse --verify $NEW &> /dev/null || continue
if [[ -n $line ]]
then
sed -i "${line}s|remote=\"dudu\"|remote=\"dudu\" revision=\"${NEW}\"|" "$XML"
fi
done
git -C "$CWD/manifest" commit -a -m "[DNM]manifest: Track $NEW branch" || return 1
}
doRebase() {
local PROJECTPATHS=$(cat $LIST)
local BRANCH=$1
local TAG=$2
local NEWBRANCH="${BRANCH}-rebase-${TAG}"
# Make sure manifest and forked repos are in a consistent state
prin "#### Verifying there are no uncommitted changes on forked AOSP projects ####"
for i in ${PROJECTPATHS} .repo/manifests; do
# cd "${CWD}/${PROJECTPATH}"
if [[ -n "$(git -C "$CWD/$i" status --porcelain)" ]]; then
err "Path ${i} has uncommitted changes. Please fix."
fi
done
grn "#### Verification complete - no uncommitted changes found ####"
prin
for files in success.list failed.list
do
rm $CWD/$files 2> /dev/null
touch $CWD/$files
done
for PROJECTPATH in ${PROJECTPATHS}; do
if [[ "${BLACKLIST}" =~ "${PROJECTPATH}" ]]; then
continue
fi
case $PROJECTPATH in
build/make) repo_url="$AOSP/platform/build" ;;
*) repo_url="$AOSP/platform/$PROJECTPATH" ;;
esac
if [[ "$PROJECTPATH" == "vendor/bianca" ]]; then
dbg "Detecting vendor/bianca, checking out to $NEWBRANCH"
git -C "$CWD/$PROJECTPATH" checkout -b "$NEWBRANCH" &> /dev/null
echo "$PROJECTPATH" >> $CWD/success.list
continue
fi
if wget -q --spider $repo_url; then
blu "Rebasaing $PROJECTPATH"
if ! git -C "$CWD/$PROJECTPATH" checkout "${BRANCH}" &> /dev/null
then
red "Error: Failed checkout repo $PROJECTPATH to branch $BRANCH, please check again. Continue to next repo"
continue
fi
if ! git -C "$CWD/$PROJECTPATH" fetch -q $repo_url $TAG
then
red "Error: Failed fetching repo $PROJECTPATH, please check connection. Continue to next repo"
continue
fi
git -C "$CWD/$PROJECTPATH" branch -D "$NEWBRANCH" &> /dev/null
git -C "$CWD/$PROJECTPATH" checkout -b "$NEWBRANCH" &> /dev/null
if git -C "$CWD/$PROJECTPATH" rebase FETCH_HEAD &> /dev/null; then
if [[ $(git -C "$CWD/$PROJECTPATH" rev-parse HEAD) != $(git -C "$CWD/$PROJECTPATH" rev-parse $REMOTE/$BRANCH) ]] && [[ $(git -C "$CWD/$PROJECTPATH" diff HEAD $REMOTE/$BRANCH) ]]; then
echo "$PROJECTPATH" >> $CWD/success.list
grn "Rebase $PROJECTPATH success"
else
prin "$PROJECTPATH - unchanged"
git -C "$CWD/$PROJECTPATH" checkout "${BRANCH}" &> /dev/null
git -C "$CWD/$PROJECTPATH" branch -D "$NEWBRANCH" &> /dev/null
fi
else
echo "$PROJECTPATH" >> $CWD/failed.list
red "$PROJECTPATH Rebasing failed"
fi
else
red "Failed checking url $repo_url, please check connection"
echo "$PROJECTPATH" >> $CWD/failed.list
fi
prin
done
prin
grn "These repos success rebasaing:"
cat "$CWD/success.list"
prin
red "These repos failed rebasaing:"
cat "$CWD/failed.list"
prin
if [[ -f "$CWD/manifest/snippets/bianca.xml" ]]
then
dbg "Detected Bianca Project XML. Trying to change branch with new rebased branch"
changeBranchManifest "$NEWBRANCH"
fi
}
doMerge() {
local PROJECTPATHS=$(cat $LIST)
local BRANCH=$1
local TAG=$2
local NEWBRANCH="${BRANCH}-merge-${TAG}"
# Make sure manifest and forked repos are in a consistent state
prin "#### Verifying there are no uncommitted changes on forked AOSP projects ####"
for i in ${PROJECTPATHS} .repo/manifests; do
# cd "${CWD}/${PROJECTPATH}"
if [[ -n "$(git -C "$CWD/$i" status --porcelain)" ]]; then
err "Path ${i} has uncommitted changes. Please fix."
fi
done
grn "#### Verification complete - no uncommitted changes found ####"
prin
for files in success.list failed.list
do
rm $CWD/$files &> /dev/null
touch $CWD/$files
done
for PROJECTPATH in ${PROJECTPATHS}; do
if [[ "${BLACKLIST}" =~ "${PROJECTPATH}" ]]; then
continue
fi
case $PROJECTPATH in
build/make) repo_url="$AOSP/platform/build" ;;
*) repo_url="$AOSP/platform/$PROJECTPATH" ;;
esac
if [[ "$PROJECTPATH" == "vendor/bianca" ]]; then
dbg "Detecting vendor/bianca, checking out to $NEWBRANCH"
git -C "$CWD/$PROJECTPATH" checkout -b "$NEWBRANCH" &> /dev/null
echo "$PROJECTPATH" >> $CWD/success.list
continue
fi
if wget -q --spider $repo_url; then
blu "Merging $PROJECTPATH with $TAG"
if ! git -C "$CWD/$PROJECTPATH" checkout "${BRANCH}" &> /dev/null
then
red "Error: Failed checkout repo $PROJECTPATH to branch $BRANCH, please check again. Continue to next repo"
continue
fi
if ! git -C "$CWD/$PROJECTPATH" fetch -q $repo_url $TAG
then
red "Error: Failed fetching repo $PROJECTPATH, please check connection. Continue to next repo"
continue
fi
git -C "$CWD/$PROJECTPATH" branch -D "$NEWBRANCH" &> /dev/null
git -C "$CWD/$PROJECTPATH" checkout -b "$NEWBRANCH" &> /dev/null
if git -C "$CWD/$PROJECTPATH" merge FETCH_HEAD &> /dev/null; then
if [[ $(git -C "$CWD/$PROJECTPATH" rev-parse HEAD) != $(git -C "$CWD/$PROJECTPATH" rev-parse $REMOTE/$BRANCH) ]] && [[ $(git -C "$CWD/$PROJECTPATH" diff HEAD $REMOTE/$BRANCH) ]]; then
echo "$PROJECTPATH" >> $CWD/success.list
grn "Merging $PROJECTPATH success"
else
prin "$PROJECTPATH - unchanged"
git -C "$CWD/$PROJECTPATH" checkout "${BRANCH}" &> /dev/null
git -C "$CWD/$PROJECTPATH" branch -D "$NEWBRANCH" &> /dev/null
fi
else
echo "$PROJECTPATH" >> $CWD/failed.list
red "$PROJECTPATH Merging failed"
fi
else
red "Failed checking url $repo_url, please check connection"
echo "$PROJECTPATH" >> $CWD/failed.list
fi
prin
done
prin
grn "These repos success merging:"
cat "$CWD/success.list"
prin
red "These repos failed merging:"
cat "$CWD/failed.list"
prin
if [[ -f "$CWD/manifest/snippets/bianca.xml" ]]
then
dbg "Detected Bianca Project XML. Trying to change branch with new merged branch"
changeBranchManifest "$NEWBRANCH"
fi
}
doMergeAbort() {
cat "$CWD/failed.list" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
prin "Repo $1 merged aborted"
if ! git -C "$CWD/$1" merge --abort
then
red "Failed to merge abort"
prin
continue
fi
grn "Success"
prin
done
}
doStart() {
BRANCH=$1
cat "${LIST}" | while read l; do
set ${l}
PROJECTPATH="${1}"
if [ ! -d "${PROJECTPATH}" ]; then continue; fi
prin "Starting repo $PROJECTPATH to $BRANCH"
prin
repo start "${BRANCH}" "${PROJECTPATH}" || red "Failed start repo $PROJECTPATH"
done
dbg "Info: Success start repo to $BRANCH"
}
doFetch() {
local REMOTE=$1
local BRANCH=$2
cat "${LIST}" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
prin "Fetching repo $1 with branch $BRANCH"
if ! git -C "$CWD/$1" fetch -q $REMOTE $BRANCH
then
red "Failed fetching repo $1"
prin
continue
fi
grn "Success"
prin
done
}
doPush() {
local REMOTE=$1
local BRANCH=$2
local CUSTOM=$3
cat "${LIST}" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
prin "Push repo $1 with branch $BRANCH"
if ! git -C "$CWD/$1" push $CUSTOM $REMOTE $BRANCH
then
red "Failed push repo $1"
prin
continue
fi
grn "Success"
prin
done
}
doCheckout() {
local BRANCH=$1
cat "${LIST}" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
prin "Checkouting repo $1 to $BRANCH"
if ! git -C "$CWD/$1" checkout $BRANCH &> /dev/null
then
red "Failed checkout repo $1"
prin
continue
fi
grn "Success"
prin
done
}
doReset() {
local BRANCH=$1
cat "${LIST}" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
prin "Reset-hard repo $1 to $BRANCH"
if ! git -C "$CWD/$1" reset --hard $BRANCH &> /dev/null
then
red "Failed reset-hard repo $1"
prin
continue
fi
grn "Success"
prin
done
}
doBackup() {
local BRANCH=$1
local NEWBRANCH=$2
cat "${LIST}" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
NOW=$(git -C $CWD/$1 branch --show-current)
prin "Backup repo $1 to $NEWBRANCH"
if ! git -C "$CWD/$1" checkout $BRANCH -b $NEWBRANCH &> /dev/null
then
red "Failed backup repo $1"
prin
continue
fi
git -C "$CWD/$1" checkout $NOW &> /dev/null
grn "Success"
prin
done
if [[ -f "$CWD/manifest/snippets/bianca.xml" ]]
then
dbg "Detected Bianca Project XML. Trying to change branch with new backup branch"
changeBranchManifest "$NEWBRANCH"
fi
}
doRebaseAbort() {
cat "$CWD/failed.list" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
prin "Repo $1 rebase aborted"
if ! git -C "$CWD/$1" rebase --abort
then
red "Failed to rebase abort"
prin
continue
fi
grn "Success"
prin
done
}
doTag() {
local TAG=$1
cat "${LIST}" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
prin "Repo $1 add tag: $TAG"
if ! git -C "$CWD/$1" tag $TAG &> /dev/null
then
red "Failed add tag for repo: $1"
prin
continue
fi
grn "Success"
prin
done
}
doTagDel() {
local TAG=$1
cat "${LIST}" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
prin "Repo $1 delete tag: $TAG"
if ! git -C "$CWD/$1" tag -d $TAG
then
red "Failed delete tag for repo: $1"
prin
continue
fi
grn "Success"
prin
done
}
doBranchDel() {
local BRANCH=$1
cat "${LIST}" | while read l; do
set ${l}
if [ ! -d "$1" ]; then continue; fi
prin "Repo $1 delete branch: $BRANCH"
if ! git -C "$CWD/$1" branch -D $BRANCH
then
red "Failed delete branch for repo: $1"
prin
continue
fi
grn "Success"
prin
done
}
usage() {
prin "Usage: $(basename $0) <command> [<argument>]"
prin
prin "Command:"
prin " init Initial with creating project.list"
prin " list Listing project.list"
prin " start <branch> Creating new branch"
prin " checkout <branch> Checkout all repo with already branch"
prin " fetch <remote> <branch> Fetching all repo"
prin " reset-hard <branch> Reseting hard"
prin " rebase <currentbranch> <aosptag> Rebase all repo with new aosp tag"
prin " rebase-abort Abort all repo from from failed.list"
prin " merge <currentbranch> <aosptag> Merge all repo with new aosp tag"
prin " merge-abort Abort all repo from from failed.list"
prin " push <remote> <branch> Push all repo"
prin " push-force <remote> <branch> Push all repo with flag --force"
prin " push-delete <remote> <branch> Push all repo with flag --delete"
prin " backup <branch> <new branch> Backup a branch with new branch"
prin " tag <new tag> Add new tag"
prin " tag-delete <tag> Delete existing tag"
prin " branch-delete <branch> Delete existing branch in local"
prin " help Print usage"
prin
}
# Parse options
while [[ $# -gt 0 ]]; do
case "${1}" in
init)
doInit
exit
;;
start)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
checkPath
doStart "$2"
shift
else
err "Error: Argument for $1 is missing or more/less than 1 argument. Command: start <branch>"
fi
exit
;;
list)
checkPath
doList
exit
;;
rebase)
if [ -n "$2" ] && [ -n "$3" ] && [ ${2:0:1} != "-" ]; then
checkPath
doRebase "$2" "$3"
shift 2
else
err "Error: Argument for $1 is missing or more/less than 2 argument. Command: rebase <currentbranch> <aospnewtag>"
fi
exit
;;
rebase-abort)
checkPath
doRebaseAbort
exit
;;
merge)
if [ -n "$2" ] && [ -n "$3" ] && [ ${2:0:1} != "-" ]; then
checkPath
doMerge "$2" "$3"
shift 2
else
err "Error: Argument for $1 is missing or more/less than 2 argument. Command: merge <currentbranch> <aospnewtag>"
fi
exit
;;
merge-abort)
checkPath
doMergeAbort
exit
;;
fetch)
if [ -n "$2" ] && [ -n "$3" ] && [ ${2:0:1} != "-" ]; then
checkPath
doFetch "$2" "$3"
shift 2
else
err "Error: Argument for $1 is missing or more/less than 2 argument. Command: fetch <remote> <branch>"
fi
exit
;;
push)
if [ -n "$2" ] && [ -n "$3" ] && [ ${2:0:1} != "-" ]; then
checkPath
doPush "$2" "$3"
shift 2
else
err "Error: Argument for $1 is missing or more/less than 2 argument. Command: push <remote> <branch>"
fi
exit
;;
push-force)
if [ -n "$2" ] && [ -n "$3" ] && [ ${2:0:1} != "-" ]; then
checkPath
doPush "$2" "$3" "--force"
shift 2
else
err "Error: Argument for $1 is missing or more/less than 2 argument. Command: push-force <remote> <branch>"
fi
exit
;;
push-delete)
if [ -n "$2" ] && [ -n "$3" ] && [ ${2:0:1} != "-" ]; then
checkPath
doPush "$2" "$3" "--delete"
shift 2
else
err "Error: Argument for $1 is missing or more/less than 2 argument. Command: push-delete <remote> <branch>"
fi
exit
;;
checkout)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
checkPath
doCheckout "$2"
shift
else
err "Error: Argument for $1 is missing or more/less than 1 argument. Command: checkout <branch>"
fi
exit
;;
reset-hard)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
checkPath
doReset "$2"
shift
else
err "Error: Argument for $1 is missing or more/less than 1 argument. Command: reset-hard <branch>"
fi
exit
;;
backup)
if [ -n "$2" ] && [ -n "$3" ] && [ ${2:0:1} != "-" ]; then
checkPath
doBackup "$2" "$3"
shift 2
else
err "Error: Argument for $1 is missing or more/less than 2 argument. Command: backup <branch> <new branch>"
fi
exit
;;
tag)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
checkPath
doTag "$2"
shift
else
err "Error: Argument for $1 is missing or more/less than 1 argument. Command: tag <new tag>"
fi
exit
;;
tag-delete)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
checkPath
doTagDel "$2"
shift
else
err "Error: Argument for $1 is missing or more/less than 1 argument. Command: tag-delete <tag>"
fi
exit
;;
branch-delete)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
checkPath
doBranchDel "$2"
shift
else
err "Error: Argument for $1 is missing or more/less than 1 argument. Command: branch-delete <branch>"
fi
exit
;;
help|*)
usage
exit
;;
esac
shift
done
usage