blob: c22b9d6d1073a15badda3a6424437d08c643f3f4 [file] [log] [blame]
Luca Stefani5c60e4f2017-08-17 19:28:48 +02001function __print_lineage_functions_help() {
Michael Bestas3952f6c2016-08-26 01:12:08 +03002cat <<EOF
Dan Pasanen03447712016-12-19 11:22:55 -06003Additional LineageOS functions:
Michael Bestas3952f6c2016-08-26 01:12:08 +03004- cout: Changes directory to out.
5- mmp: Builds all of the modules in the current directory and pushes them to the device.
6- mmap: Builds all of the modules in the current directory and its dependencies, then pushes the package to the device.
7- mmmp: Builds all of the modules in the supplied directories and pushes them to the device.
Luca Stefani5c60e4f2017-08-17 19:28:48 +02008- lineagegerrit: A Git wrapper that fetches/pushes patch from/to LineageOS Gerrit Review.
9- lineagerebase: Rebase a Gerrit change and push it again.
10- lineageremote: Add git remote for LineageOS Gerrit Review.
Michael Bestas3952f6c2016-08-26 01:12:08 +030011- aospremote: Add git remote for matching AOSP repository.
12- cafremote: Add git remote for matching CodeAurora repository.
Michael Bestasa504aa42018-08-10 22:51:37 +030013- githubremote: Add git remote for LineageOS Github.
Michael Bestas3952f6c2016-08-26 01:12:08 +030014- mka: Builds using SCHED_BATCH on all processors.
15- mkap: Builds the module(s) using mka and pushes them to the device.
16- cmka: Cleans and builds using mka.
17- repodiff: Diff 2 different branches or tags within the same repo
18- repolastsync: Prints date and time of last repo sync.
19- reposync: Parallel repo sync using ionice and SCHED_BATCH.
20- repopick: Utility to fetch changes from Gerrit.
21- installboot: Installs a boot.img to the connected device.
22- installrecovery: Installs a recovery.img to the connected device.
23EOF
24}
25
Luca Stefani076c27b2017-08-17 20:30:00 +020026function mk_timer()
27{
28 local start_time=$(date +"%s")
29 $@
30 local ret=$?
31 local end_time=$(date +"%s")
32 local tdiff=$(($end_time-$start_time))
33 local hours=$(($tdiff / 3600 ))
34 local mins=$((($tdiff % 3600) / 60))
35 local secs=$(($tdiff % 60))
36 local ncolors=$(tput colors 2>/dev/null)
37 echo
38 if [ $ret -eq 0 ] ; then
39 echo -n "#### make completed successfully "
40 else
41 echo -n "#### make failed to build some targets "
42 fi
43 if [ $hours -gt 0 ] ; then
44 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
45 elif [ $mins -gt 0 ] ; then
46 printf "(%02g:%02g (mm:ss))" $mins $secs
47 elif [ $secs -gt 0 ] ; then
48 printf "(%s seconds)" $secs
49 fi
50 echo " ####"
51 echo
52 return $ret
53}
54
Michael Bestas3952f6c2016-08-26 01:12:08 +030055function brunch()
56{
57 breakfast $*
58 if [ $? -eq 0 ]; then
59 mka bacon
60 else
61 echo "No such item in brunch menu. Try 'breakfast'"
62 return 1
63 fi
64 return $?
65}
66
67function breakfast()
68{
69 target=$1
70 local variant=$2
Michael Bestas3952f6c2016-08-26 01:12:08 +030071
72 if [ $# -eq 0 ]; then
73 # No arguments, so let's have the full menu
74 lunch
75 else
76 echo "z$target" | grep -q "-"
77 if [ $? -eq 0 ]; then
78 # A buildtype was specified, assume a full device name
79 lunch $target
80 else
Simon Shields63ce74b2016-12-28 11:33:29 +110081 # This is probably just the Lineage model name
Michael Bestas3952f6c2016-08-26 01:12:08 +030082 if [ -z "$variant" ]; then
83 variant="userdebug"
84 fi
Matt Mowered8c2482017-01-02 02:26:01 -060085
Luca Stefani5c60e4f2017-08-17 19:28:48 +020086 lunch lineage_$target-$variant
Michael Bestas3952f6c2016-08-26 01:12:08 +030087 fi
88 fi
89 return $?
90}
91
92alias bib=breakfast
93
94function eat()
95{
96 if [ "$OUT" ] ; then
Paul Keithb11d5732017-11-02 20:31:33 +010097 ZIPPATH=`ls -tr "$OUT"/lineage-*.zip | tail -1`
Michael Bestas3952f6c2016-08-26 01:12:08 +030098 if [ ! -f $ZIPPATH ] ; then
99 echo "Nothing to eat"
100 return 1
101 fi
102 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
Adrian DC6393a3f2018-06-16 19:56:43 +0200103 if [ $(adb get-state) != device -a $(adb shell 'test -e /sbin/recovery 2> /dev/null; echo $?') != 0 ] ; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300104 echo "No device is online. Waiting for one..."
105 echo "Please connect USB and/or enable USB debugging"
Adrian DC6393a3f2018-06-16 19:56:43 +0200106 until [ $(adb get-state) = device -o $(adb shell 'test -e /sbin/recovery 2> /dev/null; echo $?') = 0 ];do
Michael Bestas3952f6c2016-08-26 01:12:08 +0300107 sleep 1
108 done
109 echo "Device Found.."
110 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200111 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD"); then
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800112 # if adbd isn't root we can't write to /cache/recovery/
113 adb root
114 sleep 1
115 adb wait-for-device
116 cat << EOF > /tmp/command
Michael Bestas3952f6c2016-08-26 01:12:08 +0300117--sideload_auto_reboot
118EOF
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800119 if adb push /tmp/command /cache/recovery/ ; then
120 echo "Rebooting into recovery for sideload installation"
121 adb reboot recovery
122 adb wait-for-sideload
123 adb sideload $ZIPPATH
124 fi
125 rm /tmp/command
126 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200127 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300128 fi
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800129 return $?
Michael Bestas3952f6c2016-08-26 01:12:08 +0300130 else
131 echo "Nothing to eat"
132 return 1
133 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300134}
135
136function omnom()
137{
138 brunch $*
139 eat
140}
141
142function cout()
143{
144 if [ "$OUT" ]; then
145 cd $OUT
146 else
147 echo "Couldn't locate out directory. Try setting OUT."
148 fi
149}
150
151function dddclient()
152{
153 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
154 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
155 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
156 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
157 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
158 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
159 local ARCH=$(get_build_var TARGET_ARCH)
160 local GDB
161 case "$ARCH" in
162 arm) GDB=arm-linux-androideabi-gdb;;
163 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
164 mips|mips64) GDB=mips64el-linux-android-gdb;;
165 x86) GDB=x86_64-linux-android-gdb;;
166 x86_64) GDB=x86_64-linux-android-gdb;;
167 *) echo "Unknown arch $ARCH"; return 1;;
168 esac
169
170 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
171 local EXE="$1"
172 if [ "$EXE" ] ; then
173 EXE=$1
174 if [[ $EXE =~ ^[^/].* ]] ; then
175 EXE="system/bin/"$EXE
176 fi
177 else
178 EXE="app_process"
179 fi
180
181 local PORT="$2"
182 if [ "$PORT" ] ; then
183 PORT=$2
184 else
185 PORT=":5039"
186 fi
187
188 local PID="$3"
189 if [ "$PID" ] ; then
190 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
191 PID=`pid $3`
192 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
193 # that likely didn't work because of returning multiple processes
194 # try again, filtering by root processes (don't contain colon)
195 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
196 if [[ ! "$PID" =~ ^[0-9]+$ ]]
197 then
198 echo "Couldn't resolve '$3' to single PID"
199 return 1
200 else
201 echo ""
202 echo "WARNING: multiple processes matching '$3' observed, using root process"
203 echo ""
204 fi
205 fi
206 fi
207 adb forward "tcp$PORT" "tcp$PORT"
208 local USE64BIT="$(is64bit $PID)"
209 adb shell gdbserver$USE64BIT $PORT --attach $PID &
210 sleep 2
211 else
212 echo ""
213 echo "If you haven't done so already, do this first on the device:"
214 echo " gdbserver $PORT /system/bin/$EXE"
215 echo " or"
216 echo " gdbserver $PORT --attach <PID>"
217 echo ""
218 fi
219
220 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
221 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
222
223 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
224 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl"
225 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
226 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
227 # Enable special debugging for ART processes.
228 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
229 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
230 fi
231 echo >>"$OUT_ROOT/gdbclient.cmds" ""
232
233 local WHICH_GDB=
234 # 64-bit exe found
235 if [ "$USE64BIT" != "" ] ; then
236 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
237 # 32-bit exe / 32-bit platform
238 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
239 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
240 # 32-bit exe / 64-bit platform
241 else
242 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
243 fi
244
245 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
246 else
247 echo "Unable to determine build system output dir."
248 fi
249}
250
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200251function lineageremote()
Michael Bestas3952f6c2016-08-26 01:12:08 +0300252{
253 if ! git rev-parse --git-dir &> /dev/null
254 then
255 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
256 return 1
257 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200258 git remote rm lineage 2> /dev/null
Michael Bestas9e6bde52018-04-02 19:54:45 +0300259 local REMOTE=$(git config --get remote.github.projectname)
260 local LINEAGE="true"
261 if [ -z "$REMOTE" ]
Michael Bestasaf3532b2017-08-23 17:40:40 +0300262 then
Michael Bestas9e6bde52018-04-02 19:54:45 +0300263 REMOTE=$(git config --get remote.aosp.projectname)
264 LINEAGE="false"
Michael Bestasaf3532b2017-08-23 17:40:40 +0300265 fi
Michael Bestas9e6bde52018-04-02 19:54:45 +0300266 if [ -z "$REMOTE" ]
267 then
268 REMOTE=$(git config --get remote.caf.projectname)
269 LINEAGE="false"
270 fi
271
272 if [ $LINEAGE = "false" ]
273 then
274 local PROJECT=$(echo $REMOTE | sed -e "s#platform/#android/#g; s#/#_#g")
275 local PFX="LineageOS/"
276 else
277 local PROJECT=$REMOTE
278 fi
279
Michael Bestasad3a5702017-08-24 00:00:48 +0300280 local LINEAGE_USER=$(git config --get review.review.lineageos.org.username)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200281 if [ -z "$LINEAGE_USER" ]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300282 then
Michael Bestas9e6bde52018-04-02 19:54:45 +0300283 git remote add lineage ssh://review.lineageos.org:29418/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300284 else
Michael Bestas9e6bde52018-04-02 19:54:45 +0300285 git remote add lineage ssh://$LINEAGE_USER@review.lineageos.org:29418/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300286 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200287 echo "Remote 'lineage' created"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300288}
289
290function aospremote()
291{
292 if ! git rev-parse --git-dir &> /dev/null
293 then
294 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
295 return 1
296 fi
297 git remote rm aosp 2> /dev/null
Michael Bestasad3a5702017-08-24 00:00:48 +0300298 local PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Rashed Abdel-Tawabfd8b8292017-10-24 21:55:52 -0400299 # Google moved the repo location in Oreo
300 if [ $PROJECT = "build/make" ]
301 then
302 PROJECT="build"
303 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300304 if (echo $PROJECT | grep -qv "^device")
305 then
Michael Bestasad3a5702017-08-24 00:00:48 +0300306 local PFX="platform/"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300307 fi
308 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
309 echo "Remote 'aosp' created"
310}
311
312function cafremote()
313{
314 if ! git rev-parse --git-dir &> /dev/null
315 then
316 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
317 return 1
318 fi
319 git remote rm caf 2> /dev/null
Michael Bestasad3a5702017-08-24 00:00:48 +0300320 local PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Rashed Abdel-Tawabfd8b8292017-10-24 21:55:52 -0400321 # Google moved the repo location in Oreo
322 if [ $PROJECT = "build/make" ]
323 then
324 PROJECT="build"
325 fi
Rashed Abdel-Tawabb52c7082017-12-24 22:40:31 +0200326 if [[ $PROJECT =~ "qcom/opensource" ]];
327 then
328 PROJECT=$(echo $PROJECT | sed -e "s#qcom\/opensource#qcom-opensource#")
329 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300330 if (echo $PROJECT | grep -qv "^device")
331 then
Michael Bestasad3a5702017-08-24 00:00:48 +0300332 local PFX="platform/"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300333 fi
Zhao Wei Liewfb4b8c52017-01-08 09:03:01 +0800334 git remote add caf https://source.codeaurora.org/quic/la/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300335 echo "Remote 'caf' created"
336}
337
Michael Bestasa504aa42018-08-10 22:51:37 +0300338function githubremote()
339{
340 if ! git rev-parse --git-dir &> /dev/null
341 then
342 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
343 return 1
344 fi
345 git remote rm github 2> /dev/null
346 local REMOTE=$(git config --get remote.aosp.projectname)
347
348 if [ -z "$REMOTE" ]
349 then
350 REMOTE=$(git config --get remote.caf.projectname)
351 fi
352
353 local PROJECT=$(echo $REMOTE | sed -e "s#platform/#android/#g; s#/#_#g")
354
355 git remote add github https://github.com/LineageOS/$PROJECT
356 echo "Remote 'github' created"
357}
358
Michael Bestas3952f6c2016-08-26 01:12:08 +0300359function installboot()
360{
Alessandro Astonef8f48772019-09-06 13:07:03 +0200361 if [ ! -e "$OUT/recovery/root/system/etc/recovery.fstab" ];
Michael Bestas3952f6c2016-08-26 01:12:08 +0300362 then
363 echo "No recovery.fstab found. Build recovery first."
364 return 1
365 fi
366 if [ ! -e "$OUT/boot.img" ];
367 then
368 echo "No boot.img found. Run make bootimage first."
369 return 1
370 fi
Alessandro Astonef8f48772019-09-06 13:07:03 +0200371 PARTITION=`grep "^\/boot" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $3'}`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300372 if [ -z "$PARTITION" ];
373 then
374 # Try for RECOVERY_FSTAB_VERSION = 2
Alessandro Astonef8f48772019-09-06 13:07:03 +0200375 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $1'}`
376 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $3'}`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300377 if [ -z "$PARTITION" ];
378 then
379 echo "Unable to determine boot partition."
380 return 1
381 fi
382 fi
383 adb start-server
384 adb wait-for-online
385 adb root
386 sleep 1
387 adb wait-for-online shell mount /system 2>&1 > /dev/null
388 adb wait-for-online remount
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200389 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300390 then
391 adb push $OUT/boot.img /cache/
Ashwin Rameshb0ea62a2017-08-13 12:00:15 +0530392 if [ -e "$OUT/system/lib/modules/*" ];
393 then
394 for i in $OUT/system/lib/modules/*;
395 do
396 adb push $i /system/lib/modules/
397 done
398 adb shell chmod 644 /system/lib/modules/*
399 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300400 adb shell dd if=/cache/boot.img of=$PARTITION
Michael W2e203942018-01-14 18:46:02 +0100401 adb shell rm -rf /cache/boot.img
Michael Bestas3952f6c2016-08-26 01:12:08 +0300402 echo "Installation complete."
403 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200404 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300405 fi
406}
407
408function installrecovery()
409{
Alessandro Astonef8f48772019-09-06 13:07:03 +0200410 if [ ! -e "$OUT/recovery/root/system/etc/recovery.fstab" ];
Michael Bestas3952f6c2016-08-26 01:12:08 +0300411 then
412 echo "No recovery.fstab found. Build recovery first."
413 return 1
414 fi
415 if [ ! -e "$OUT/recovery.img" ];
416 then
417 echo "No recovery.img found. Run make recoveryimage first."
418 return 1
419 fi
Alessandro Astonef8f48772019-09-06 13:07:03 +0200420 PARTITION=`grep "^\/recovery" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $3'}`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300421 if [ -z "$PARTITION" ];
422 then
423 # Try for RECOVERY_FSTAB_VERSION = 2
Alessandro Astonef8f48772019-09-06 13:07:03 +0200424 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $1'}`
425 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $3'}`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300426 if [ -z "$PARTITION" ];
427 then
428 echo "Unable to determine recovery partition."
429 return 1
430 fi
431 fi
432 adb start-server
433 adb wait-for-online
434 adb root
435 sleep 1
436 adb wait-for-online shell mount /system 2>&1 >> /dev/null
437 adb wait-for-online remount
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200438 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300439 then
440 adb push $OUT/recovery.img /cache/
441 adb shell dd if=/cache/recovery.img of=$PARTITION
Michael W2e203942018-01-14 18:46:02 +0100442 adb shell rm -rf /cache/recovery.img
Michael Bestas3952f6c2016-08-26 01:12:08 +0300443 echo "Installation complete."
444 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200445 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300446 fi
447}
448
449function makerecipe() {
450 if [ -z "$1" ]
451 then
452 echo "No branch name provided."
453 return 1
454 fi
455 cd android
456 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
457 git commit -a -m "$1"
458 cd ..
459
460 repo forall -c '
461
462 if [ "$REPO_REMOTE" = "github" ]
463 then
464 pwd
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200465 lineageremote
466 git push lineage HEAD:refs/heads/'$1'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300467 fi
468 '
469}
470
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200471function lineagegerrit() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300472 if [ "$(__detect_shell)" = "zsh" ]; then
473 # zsh does not define FUNCNAME, derive from funcstack
474 local FUNCNAME=$funcstack[1]
475 fi
476
477 if [ $# -eq 0 ]; then
478 $FUNCNAME help
479 return 1
480 fi
Dan Pasanen03447712016-12-19 11:22:55 -0600481 local user=`git config --get review.review.lineageos.org.username`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300482 local review=`git config --get remote.github.review`
483 local project=`git config --get remote.github.projectname`
484 local command=$1
485 shift
486 case $command in
487 help)
488 if [ $# -eq 0 ]; then
489 cat <<EOF
490Usage:
491 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
492
493Commands:
494 fetch Just fetch the change as FETCH_HEAD
495 help Show this help, or for a specific command
496 pull Pull a change into current branch
497 push Push HEAD or a local branch to Gerrit for a specific branch
498
499Any other Git commands that support refname would work as:
500 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
501
502See '$FUNCNAME help COMMAND' for more information on a specific command.
503
504Example:
505 $FUNCNAME checkout -b topic 1234/5
506works as:
507 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
508 && git checkout -b topic FETCH_HEAD
509will checkout a new branch 'topic' base on patch-set 5 of change 1234.
510Patch-set 1 will be fetched if omitted.
511EOF
512 return
513 fi
514 case $1 in
515 __cmg_*) echo "For internal use only." ;;
516 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200517 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300518 echo "'$FUNCNAME $1' is deprecated."
519 fi
520 ;;
521 help) $FUNCNAME help ;;
522 fetch|pull) cat <<EOF
523usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
524
525works as:
526 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
527 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
528
529Example:
530 $FUNCNAME $1 1234
531will $1 patch-set 1 of change 1234
532EOF
533 ;;
534 push) cat <<EOF
535usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
536
537works as:
538 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
539 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
540
541Example:
542 $FUNCNAME push fix6789:gingerbread
543will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
544HEAD will be pushed from local if omitted.
545EOF
546 ;;
547 *)
548 $FUNCNAME __cmg_err_not_supported $1 && return
549 cat <<EOF
550usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
551
552works as:
553 git fetch http://DOMAIN/p/PROJECT \\
554 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
555 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
556EOF
557 ;;
558 esac
559 ;;
560 __cmg_get_ref)
561 $FUNCNAME __cmg_err_no_arg $command $# && return 1
562 local change_id patchset_id hash
563 case $1 in
564 */*)
565 change_id=${1%%/*}
566 patchset_id=${1#*/}
567 ;;
568 *)
569 change_id=$1
570 patchset_id=1
571 ;;
572 esac
573 hash=$(($change_id % 100))
574 case $hash in
575 [0-9]) hash="0$hash" ;;
576 esac
577 echo "refs/changes/$hash/$change_id/$patchset_id"
578 ;;
579 fetch|pull)
580 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
581 $FUNCNAME __cmg_err_not_repo && return 1
582 local change=$1
583 shift
584 git $command $@ http://$review/p/$project \
585 $($FUNCNAME __cmg_get_ref $change) || return 1
586 ;;
587 push)
588 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
589 $FUNCNAME __cmg_err_not_repo && return 1
590 if [ -z "$user" ]; then
591 echo >&2 "Gerrit username not found."
592 return 1
593 fi
594 local local_branch remote_branch
595 case $1 in
596 *:*)
597 local_branch=${1%:*}
598 remote_branch=${1##*:}
599 ;;
600 *)
601 local_branch=HEAD
602 remote_branch=$1
603 ;;
604 esac
605 shift
606 git push $@ ssh://$user@$review:29418/$project \
607 $local_branch:refs/for/$remote_branch || return 1
608 ;;
609 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200610 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300611 echo >&2 "'$FUNCNAME $command' is deprecated."
612 fi
613 ;;
614 __cmg_err_no_arg)
615 if [ $# -lt 2 ]; then
616 echo >&2 "'$FUNCNAME $command' missing argument."
617 elif [ $2 -eq 0 ]; then
618 if [ -n "$3" ]; then
619 $FUNCNAME help $1
620 else
621 echo >&2 "'$FUNCNAME $1' missing argument."
622 fi
623 else
624 return 1
625 fi
626 ;;
627 __cmg_err_not_repo)
628 if [ -z "$review" -o -z "$project" ]; then
629 echo >&2 "Not currently in any reviewable repository."
630 else
631 return 1
632 fi
633 ;;
634 __cmg_err_not_supported)
635 $FUNCNAME __cmg_err_no_arg $command $# && return
636 case $1 in
637 #TODO: filter more git commands that don't use refname
638 init|add|rm|mv|status|clone|remote|bisect|config|stash)
639 echo >&2 "'$FUNCNAME $1' is not supported."
640 ;;
641 *) return 1 ;;
642 esac
643 ;;
644 #TODO: other special cases?
645 *)
646 $FUNCNAME __cmg_err_not_supported $command && return 1
647 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
648 $FUNCNAME __cmg_err_not_repo && return 1
649 local args="$@"
650 local change pre_args refs_arg post_args
651 case "$args" in
652 *--\ *)
653 pre_args=${args%%-- *}
654 post_args="-- ${args#*-- }"
655 ;;
656 *) pre_args="$args" ;;
657 esac
658 args=($pre_args)
659 pre_args=
660 if [ ${#args[@]} -gt 0 ]; then
661 change=${args[${#args[@]}-1]}
662 fi
663 if [ ${#args[@]} -gt 1 ]; then
664 pre_args=${args[0]}
665 for ((i=1; i<${#args[@]}-1; i++)); do
666 pre_args="$pre_args ${args[$i]}"
667 done
668 fi
669 while ((1)); do
670 case $change in
671 ""|--)
672 $FUNCNAME help $command
673 return 1
674 ;;
675 *@*)
676 if [ -z "$refs_arg" ]; then
677 refs_arg="@${change#*@}"
678 change=${change%%@*}
679 fi
680 ;;
681 *~*)
682 if [ -z "$refs_arg" ]; then
683 refs_arg="~${change#*~}"
684 change=${change%%~*}
685 fi
686 ;;
687 *^*)
688 if [ -z "$refs_arg" ]; then
689 refs_arg="^${change#*^}"
690 change=${change%%^*}
691 fi
692 ;;
693 *:*)
694 if [ -z "$refs_arg" ]; then
695 refs_arg=":${change#*:}"
696 change=${change%%:*}
697 fi
698 ;;
699 *) break ;;
700 esac
701 done
702 $FUNCNAME fetch $change \
703 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
704 || return 1
705 ;;
706 esac
707}
708
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200709function lineagerebase() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300710 local repo=$1
711 local refs=$2
712 local pwd="$(pwd)"
713 local dir="$(gettop)/$repo"
714
715 if [ -z $repo ] || [ -z $refs ]; then
Dan Pasanen03447712016-12-19 11:22:55 -0600716 echo "LineageOS Gerrit Rebase Usage: "
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200717 echo " lineagerebase <path to project> <patch IDs on Gerrit>"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300718 echo " The patch IDs appear on the Gerrit commands that are offered."
719 echo " They consist on a series of numbers and slashes, after the text"
720 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
721 echo ""
722 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
723 echo ""
724 return
725 fi
726
727 if [ ! -d $dir ]; then
728 echo "Directory $dir doesn't exist in tree."
729 return
730 fi
731 cd $dir
732 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
733 echo "Starting branch..."
734 repo start tmprebase .
735 echo "Bringing it up to date..."
736 repo sync .
737 echo "Fetching change..."
Dan Pasanen03447712016-12-19 11:22:55 -0600738 git fetch "http://review.lineageos.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Michael Bestas3952f6c2016-08-26 01:12:08 +0300739 if [ "$?" != "0" ]; then
740 echo "Error cherry-picking. Not uploading!"
741 return
742 fi
743 echo "Uploading..."
744 repo upload .
745 echo "Cleaning up..."
746 repo abandon tmprebase .
747 cd $pwd
748}
749
750function mka() {
Luca Stefani085af722017-08-17 20:34:44 +0200751 m -j "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300752}
753
754function cmka() {
755 if [ ! -z "$1" ]; then
756 for i in "$@"; do
757 case $i in
758 bacon|otapackage|systemimage)
759 mka installclean
760 mka $i
761 ;;
762 *)
763 mka clean-$i
764 mka $i
765 ;;
766 esac
767 done
768 else
769 mka clean
770 mka
771 fi
772}
773
Michael Bestas3952f6c2016-08-26 01:12:08 +0300774function repolastsync() {
775 RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
776 RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
777 RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
778 echo "Last repo sync: $RLSLOCAL / $RLSUTC"
779}
780
781function reposync() {
Luca Stefani085af722017-08-17 20:34:44 +0200782 repo sync -j 4 "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300783}
784
785function repodiff() {
786 if [ -z "$*" ]; then
787 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
788 return
789 fi
790 diffopts=$* repo forall -c \
791 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
792}
793
794# Return success if adb is up and not in recovery
795function _adb_connected {
796 {
797 if [[ "$(adb get-state)" == device &&
Adrian DC6393a3f2018-06-16 19:56:43 +0200798 "$(adb shell 'test -e /sbin/recovery; echo $?')" != 0 ]]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300799 then
800 return 0
801 fi
802 } 2>/dev/null
803
804 return 1
805};
806
807# Credit for color strip sed: http://goo.gl/BoIcm
808function dopush()
809{
810 local func=$1
811 shift
812
813 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
814 if ! _adb_connected; then
815 echo "No device is online. Waiting for one..."
816 echo "Please connect USB and/or enable USB debugging"
817 until _adb_connected; do
818 sleep 1
819 done
820 echo "Device Found."
821 fi
822
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200823 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD") || [ "$FORCE_PUSH" = "true" ];
Michael Bestas3952f6c2016-08-26 01:12:08 +0300824 then
825 # retrieve IP and PORT info if we're using a TCP connection
Marc K2be9cac2016-10-20 10:27:35 +0200826 TCPIPPORT=$(adb devices \
827 | egrep '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]):[0-9]+[^0-9]+' \
Michael Bestas3952f6c2016-08-26 01:12:08 +0300828 | head -1 | awk '{print $1}')
829 adb root &> /dev/null
830 sleep 0.3
831 if [ -n "$TCPIPPORT" ]
832 then
833 # adb root just killed our connection
834 # so reconnect...
835 adb connect "$TCPIPPORT"
836 fi
837 adb wait-for-device &> /dev/null
838 sleep 0.3
839 adb remount &> /dev/null
840
841 mkdir -p $OUT
842 ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
843 ret=$?;
844 if [ $ret -ne 0 ]; then
845 rm -f $OUT/.log;return $ret
846 fi
847
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500848 is_gnu_sed=`sed --version | head -1 | grep -c GNU`
849
Michael Bestas3952f6c2016-08-26 01:12:08 +0300850 # Install: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500851 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200852 LOC="$(cat $OUT/.log | sed -r -e 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' -e 's/^\[ {0,2}[0-9]{1,3}% [0-9]{1,6}\/[0-9]{1,6}\] +//' \
853 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300854 else
Marc K97b035d2016-10-20 10:27:44 +0200855 LOC="$(cat $OUT/.log | sed -E "s/"$'\E'"\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" -E "s/^\[ {0,2}[0-9]{1,3}% [0-9]{1,6}\/[0-9]{1,6}\] +//" \
856 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300857 fi
858
859 # Copy: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500860 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200861 LOC="$LOC $(cat $OUT/.log | sed -r -e 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' -e 's/^\[ {0,2}[0-9]{1,3}% [0-9]{1,6}\/[0-9]{1,6}\] +//' \
862 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300863 else
Marc K97b035d2016-10-20 10:27:44 +0200864 LOC="$LOC $(cat $OUT/.log | sed -E "s/"$'\E'"\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" -E 's/^\[ {0,2}[0-9]{1,3}% [0-9]{1,6}\/[0-9]{1,6}\] +//' \
865 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300866 fi
867
868 # If any files are going to /data, push an octal file permissions reader to device
869 if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
870 CHKPERM="/data/local/tmp/chkfileperm.sh"
871(
872cat <<'EOF'
873#!/system/xbin/sh
874FILE=$@
875if [ -e $FILE ]; then
876 ls -l $FILE | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}' | cut -d ' ' -f1
877fi
878EOF
879) > $OUT/.chkfileperm.sh
880 echo "Pushing file permissions checker to device"
881 adb push $OUT/.chkfileperm.sh $CHKPERM
882 adb shell chmod 755 $CHKPERM
883 rm -f $OUT/.chkfileperm.sh
884 fi
885
Sam Mortimerc3865952019-10-12 02:14:13 -0700886 RELOUT=$(echo $OUT | sed "s#^${ANDROID_BUILD_TOP}/##")
887
Michael Bestas3952f6c2016-08-26 01:12:08 +0300888 stop_n_start=false
Sam Mortimerc3865952019-10-12 02:14:13 -0700889 for TARGET in $(echo $LOC | tr " " "\n" | sed "s#.*${RELOUT}##" | sort | uniq); do
Michael Bestas3952f6c2016-08-26 01:12:08 +0300890 # Make sure file is in $OUT/system or $OUT/data
Adrian DCa8e06d32018-06-17 00:15:00 +0200891 case $TARGET in
892 /system/*|/data/*)
893 # Get out file from target (i.e. /system/bin/adb)
894 FILE=$OUT$TARGET
Michael Bestas3952f6c2016-08-26 01:12:08 +0300895 ;;
896 *) continue ;;
897 esac
898
899 case $TARGET in
900 /data/*)
901 # fs_config only sets permissions and se labels for files pushed to /system
902 if [ -n "$CHKPERM" ]; then
903 OLDPERM=$(adb shell $CHKPERM $TARGET)
904 OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
905 OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
906 OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
907 fi
908 echo "Pushing: $TARGET"
909 adb push $FILE $TARGET
910 if [ -n "$OLDPERM" ]; then
911 echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
912 adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
913 adb shell chmod "$OLDPERM" $TARGET
914 else
915 echo "$TARGET did not exist previously, you should set file permissions manually"
916 fi
917 adb shell restorecon "$TARGET"
918 ;;
919 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
920 # Only need to stop services once
921 if ! $stop_n_start; then
922 adb shell stop
923 stop_n_start=true
924 fi
925 echo "Pushing: $TARGET"
926 adb push $FILE $TARGET
927 ;;
928 *)
929 echo "Pushing: $TARGET"
930 adb push $FILE $TARGET
931 ;;
932 esac
933 done
934 if [ -n "$CHKPERM" ]; then
935 adb shell rm $CHKPERM
936 fi
937 if $stop_n_start; then
938 adb shell start
939 fi
940 rm -f $OUT/.log
941 return 0
942 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200943 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300944 fi
945}
946
947alias mmp='dopush mm'
948alias mmmp='dopush mmm'
949alias mmap='dopush mma'
Zhao Wei Liew64fc5ae2016-12-10 16:48:27 +0800950alias mmmap='dopush mmma'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300951alias mkap='dopush mka'
952alias cmkap='dopush cmka'
953
954function repopick() {
955 T=$(gettop)
Dan Pasanen91f76202017-07-06 08:21:30 -0500956 $T/vendor/lineage/build/tools/repopick.py $@
Michael Bestas3952f6c2016-08-26 01:12:08 +0300957}
958
959function fixup_common_out_dir() {
960 common_out_dir=$(get_build_var OUT_DIR)/target/common
961 target_device=$(get_build_var TARGET_DEVICE)
Sam Mortimer55d6ec52019-09-10 11:40:34 -0700962 common_target_out=common-${target_device}
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200963 if [ ! -z $LINEAGE_FIXUP_COMMON_OUT ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300964 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
965 mv ${common_out_dir} ${common_out_dir}-${target_device}
Sam Mortimer55d6ec52019-09-10 11:40:34 -0700966 ln -s ${common_target_out} ${common_out_dir}
Michael Bestas3952f6c2016-08-26 01:12:08 +0300967 else
968 [ -L ${common_out_dir} ] && rm ${common_out_dir}
969 mkdir -p ${common_out_dir}-${target_device}
Sam Mortimer55d6ec52019-09-10 11:40:34 -0700970 ln -s ${common_target_out} ${common_out_dir}
Michael Bestas3952f6c2016-08-26 01:12:08 +0300971 fi
972 else
973 [ -L ${common_out_dir} ] && rm ${common_out_dir}
974 mkdir -p ${common_out_dir}
975 fi
976}