blob: 9d86436ced2715f9c2ae41389616347a42cf2e0a [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
Alessandro Astonecdf9ae82019-09-28 16:53:08 +0200102 echo "Waiting for device..."
103 adb wait-for-online
104 echo "Found device"
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200105 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD"); then
Alessandro Astonecdf9ae82019-09-28 16:53:08 +0200106 echo "Rebooting to sideload for install"
107 adb reboot sideload-auto-reboot
108 adb wait-for-sideload
109 adb sideload $ZIPPATH
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800110 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200111 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300112 fi
Ethan Chenb69c2ff2016-12-31 13:23:56 -0800113 return $?
Michael Bestas3952f6c2016-08-26 01:12:08 +0300114 else
115 echo "Nothing to eat"
116 return 1
117 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300118}
119
120function omnom()
121{
122 brunch $*
123 eat
124}
125
126function cout()
127{
128 if [ "$OUT" ]; then
129 cd $OUT
130 else
131 echo "Couldn't locate out directory. Try setting OUT."
132 fi
133}
134
135function dddclient()
136{
137 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
138 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
139 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
140 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
141 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
142 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
143 local ARCH=$(get_build_var TARGET_ARCH)
144 local GDB
145 case "$ARCH" in
146 arm) GDB=arm-linux-androideabi-gdb;;
147 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
148 mips|mips64) GDB=mips64el-linux-android-gdb;;
149 x86) GDB=x86_64-linux-android-gdb;;
150 x86_64) GDB=x86_64-linux-android-gdb;;
151 *) echo "Unknown arch $ARCH"; return 1;;
152 esac
153
154 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
155 local EXE="$1"
156 if [ "$EXE" ] ; then
157 EXE=$1
158 if [[ $EXE =~ ^[^/].* ]] ; then
159 EXE="system/bin/"$EXE
160 fi
161 else
162 EXE="app_process"
163 fi
164
165 local PORT="$2"
166 if [ "$PORT" ] ; then
167 PORT=$2
168 else
169 PORT=":5039"
170 fi
171
172 local PID="$3"
173 if [ "$PID" ] ; then
174 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
175 PID=`pid $3`
176 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
177 # that likely didn't work because of returning multiple processes
178 # try again, filtering by root processes (don't contain colon)
179 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
180 if [[ ! "$PID" =~ ^[0-9]+$ ]]
181 then
182 echo "Couldn't resolve '$3' to single PID"
183 return 1
184 else
185 echo ""
186 echo "WARNING: multiple processes matching '$3' observed, using root process"
187 echo ""
188 fi
189 fi
190 fi
191 adb forward "tcp$PORT" "tcp$PORT"
192 local USE64BIT="$(is64bit $PID)"
193 adb shell gdbserver$USE64BIT $PORT --attach $PID &
194 sleep 2
195 else
196 echo ""
197 echo "If you haven't done so already, do this first on the device:"
198 echo " gdbserver $PORT /system/bin/$EXE"
199 echo " or"
200 echo " gdbserver $PORT --attach <PID>"
201 echo ""
202 fi
203
204 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
205 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
206
207 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
208 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"
209 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
210 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
211 # Enable special debugging for ART processes.
212 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
213 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
214 fi
215 echo >>"$OUT_ROOT/gdbclient.cmds" ""
216
217 local WHICH_GDB=
218 # 64-bit exe found
219 if [ "$USE64BIT" != "" ] ; then
220 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
221 # 32-bit exe / 32-bit platform
222 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
223 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
224 # 32-bit exe / 64-bit platform
225 else
226 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
227 fi
228
229 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
230 else
231 echo "Unable to determine build system output dir."
232 fi
233}
234
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200235function lineageremote()
Michael Bestas3952f6c2016-08-26 01:12:08 +0300236{
237 if ! git rev-parse --git-dir &> /dev/null
238 then
239 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
240 return 1
241 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200242 git remote rm lineage 2> /dev/null
Michael Bestas9e6bde52018-04-02 19:54:45 +0300243 local REMOTE=$(git config --get remote.github.projectname)
244 local LINEAGE="true"
245 if [ -z "$REMOTE" ]
Michael Bestasaf3532b2017-08-23 17:40:40 +0300246 then
Michael Bestas9e6bde52018-04-02 19:54:45 +0300247 REMOTE=$(git config --get remote.aosp.projectname)
248 LINEAGE="false"
Michael Bestasaf3532b2017-08-23 17:40:40 +0300249 fi
Michael Bestas9e6bde52018-04-02 19:54:45 +0300250 if [ -z "$REMOTE" ]
251 then
252 REMOTE=$(git config --get remote.caf.projectname)
253 LINEAGE="false"
254 fi
255
256 if [ $LINEAGE = "false" ]
257 then
258 local PROJECT=$(echo $REMOTE | sed -e "s#platform/#android/#g; s#/#_#g")
259 local PFX="LineageOS/"
260 else
261 local PROJECT=$REMOTE
262 fi
263
Michael Bestasad3a5702017-08-24 00:00:48 +0300264 local LINEAGE_USER=$(git config --get review.review.lineageos.org.username)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200265 if [ -z "$LINEAGE_USER" ]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300266 then
Michael Bestas9e6bde52018-04-02 19:54:45 +0300267 git remote add lineage ssh://review.lineageos.org:29418/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300268 else
Michael Bestas9e6bde52018-04-02 19:54:45 +0300269 git remote add lineage ssh://$LINEAGE_USER@review.lineageos.org:29418/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300270 fi
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200271 echo "Remote 'lineage' created"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300272}
273
274function aospremote()
275{
276 if ! git rev-parse --git-dir &> /dev/null
277 then
278 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
279 return 1
280 fi
281 git remote rm aosp 2> /dev/null
Michael Bestasad3a5702017-08-24 00:00:48 +0300282 local PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Rashed Abdel-Tawabfd8b8292017-10-24 21:55:52 -0400283 # Google moved the repo location in Oreo
284 if [ $PROJECT = "build/make" ]
285 then
286 PROJECT="build"
287 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300288 if (echo $PROJECT | grep -qv "^device")
289 then
Michael Bestasad3a5702017-08-24 00:00:48 +0300290 local PFX="platform/"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300291 fi
292 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
293 echo "Remote 'aosp' created"
294}
295
296function cafremote()
297{
298 if ! git rev-parse --git-dir &> /dev/null
299 then
300 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
301 return 1
302 fi
303 git remote rm caf 2> /dev/null
Michael Bestasad3a5702017-08-24 00:00:48 +0300304 local PROJECT=$(pwd -P | sed -e "s#$ANDROID_BUILD_TOP\/##; s#-caf.*##; s#\/default##")
Rashed Abdel-Tawabfd8b8292017-10-24 21:55:52 -0400305 # Google moved the repo location in Oreo
306 if [ $PROJECT = "build/make" ]
307 then
308 PROJECT="build"
309 fi
Rashed Abdel-Tawabb52c7082017-12-24 22:40:31 +0200310 if [[ $PROJECT =~ "qcom/opensource" ]];
311 then
312 PROJECT=$(echo $PROJECT | sed -e "s#qcom\/opensource#qcom-opensource#")
313 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300314 if (echo $PROJECT | grep -qv "^device")
315 then
Michael Bestasad3a5702017-08-24 00:00:48 +0300316 local PFX="platform/"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300317 fi
Zhao Wei Liewfb4b8c52017-01-08 09:03:01 +0800318 git remote add caf https://source.codeaurora.org/quic/la/$PFX$PROJECT
Michael Bestas3952f6c2016-08-26 01:12:08 +0300319 echo "Remote 'caf' created"
320}
321
Michael Bestasa504aa42018-08-10 22:51:37 +0300322function githubremote()
323{
324 if ! git rev-parse --git-dir &> /dev/null
325 then
326 echo ".git directory not found. Please run this from the root directory of the Android repository you wish to set up."
327 return 1
328 fi
329 git remote rm github 2> /dev/null
330 local REMOTE=$(git config --get remote.aosp.projectname)
331
332 if [ -z "$REMOTE" ]
333 then
334 REMOTE=$(git config --get remote.caf.projectname)
335 fi
336
337 local PROJECT=$(echo $REMOTE | sed -e "s#platform/#android/#g; s#/#_#g")
338
339 git remote add github https://github.com/LineageOS/$PROJECT
340 echo "Remote 'github' created"
341}
342
Michael Bestas3952f6c2016-08-26 01:12:08 +0300343function installboot()
344{
Alessandro Astonef8f48772019-09-06 13:07:03 +0200345 if [ ! -e "$OUT/recovery/root/system/etc/recovery.fstab" ];
Michael Bestas3952f6c2016-08-26 01:12:08 +0300346 then
347 echo "No recovery.fstab found. Build recovery first."
348 return 1
349 fi
350 if [ ! -e "$OUT/boot.img" ];
351 then
352 echo "No boot.img found. Run make bootimage first."
353 return 1
354 fi
Alessandro Astonef8f48772019-09-06 13:07:03 +0200355 PARTITION=`grep "^\/boot" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $3'}`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300356 if [ -z "$PARTITION" ];
357 then
358 # Try for RECOVERY_FSTAB_VERSION = 2
Alessandro Astonef8f48772019-09-06 13:07:03 +0200359 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $1'}`
360 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $3'}`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300361 if [ -z "$PARTITION" ];
362 then
363 echo "Unable to determine boot partition."
364 return 1
365 fi
366 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300367 adb wait-for-online
368 adb root
Alessandro Astonecdf9ae82019-09-28 16:53:08 +0200369 adb wait-for-online
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200370 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300371 then
372 adb push $OUT/boot.img /cache/
Michael Bestas3952f6c2016-08-26 01:12:08 +0300373 adb shell dd if=/cache/boot.img of=$PARTITION
Michael W2e203942018-01-14 18:46:02 +0100374 adb shell rm -rf /cache/boot.img
Michael Bestas3952f6c2016-08-26 01:12:08 +0300375 echo "Installation complete."
376 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200377 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300378 fi
379}
380
381function installrecovery()
382{
Alessandro Astonef8f48772019-09-06 13:07:03 +0200383 if [ ! -e "$OUT/recovery/root/system/etc/recovery.fstab" ];
Michael Bestas3952f6c2016-08-26 01:12:08 +0300384 then
385 echo "No recovery.fstab found. Build recovery first."
386 return 1
387 fi
388 if [ ! -e "$OUT/recovery.img" ];
389 then
390 echo "No recovery.img found. Run make recoveryimage first."
391 return 1
392 fi
Alessandro Astonef8f48772019-09-06 13:07:03 +0200393 PARTITION=`grep "^\/recovery" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $3'}`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300394 if [ -z "$PARTITION" ];
395 then
396 # Try for RECOVERY_FSTAB_VERSION = 2
Alessandro Astonef8f48772019-09-06 13:07:03 +0200397 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $1'}`
398 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/system/etc/recovery.fstab | awk {'print $3'}`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300399 if [ -z "$PARTITION" ];
400 then
401 echo "Unable to determine recovery partition."
402 return 1
403 fi
404 fi
Michael Bestas3952f6c2016-08-26 01:12:08 +0300405 adb wait-for-online
406 adb root
Alessandro Astonecdf9ae82019-09-28 16:53:08 +0200407 adb wait-for-online
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200408 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD");
Michael Bestas3952f6c2016-08-26 01:12:08 +0300409 then
410 adb push $OUT/recovery.img /cache/
411 adb shell dd if=/cache/recovery.img of=$PARTITION
Michael W2e203942018-01-14 18:46:02 +0100412 adb shell rm -rf /cache/recovery.img
Michael Bestas3952f6c2016-08-26 01:12:08 +0300413 echo "Installation complete."
414 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200415 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300416 fi
417}
418
419function makerecipe() {
420 if [ -z "$1" ]
421 then
422 echo "No branch name provided."
423 return 1
424 fi
425 cd android
426 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
427 git commit -a -m "$1"
428 cd ..
429
430 repo forall -c '
431
432 if [ "$REPO_REMOTE" = "github" ]
433 then
434 pwd
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200435 lineageremote
436 git push lineage HEAD:refs/heads/'$1'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300437 fi
438 '
439}
440
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200441function lineagegerrit() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300442 if [ "$(__detect_shell)" = "zsh" ]; then
443 # zsh does not define FUNCNAME, derive from funcstack
444 local FUNCNAME=$funcstack[1]
445 fi
446
447 if [ $# -eq 0 ]; then
448 $FUNCNAME help
449 return 1
450 fi
Dan Pasanen03447712016-12-19 11:22:55 -0600451 local user=`git config --get review.review.lineageos.org.username`
Michael Bestas3952f6c2016-08-26 01:12:08 +0300452 local review=`git config --get remote.github.review`
453 local project=`git config --get remote.github.projectname`
454 local command=$1
455 shift
456 case $command in
457 help)
458 if [ $# -eq 0 ]; then
459 cat <<EOF
460Usage:
461 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
462
463Commands:
464 fetch Just fetch the change as FETCH_HEAD
465 help Show this help, or for a specific command
466 pull Pull a change into current branch
467 push Push HEAD or a local branch to Gerrit for a specific branch
468
469Any other Git commands that support refname would work as:
470 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
471
472See '$FUNCNAME help COMMAND' for more information on a specific command.
473
474Example:
475 $FUNCNAME checkout -b topic 1234/5
476works as:
477 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
478 && git checkout -b topic FETCH_HEAD
479will checkout a new branch 'topic' base on patch-set 5 of change 1234.
480Patch-set 1 will be fetched if omitted.
481EOF
482 return
483 fi
484 case $1 in
485 __cmg_*) echo "For internal use only." ;;
486 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200487 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300488 echo "'$FUNCNAME $1' is deprecated."
489 fi
490 ;;
491 help) $FUNCNAME help ;;
492 fetch|pull) cat <<EOF
493usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
494
495works as:
496 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
497 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
498
499Example:
500 $FUNCNAME $1 1234
501will $1 patch-set 1 of change 1234
502EOF
503 ;;
504 push) cat <<EOF
505usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
506
507works as:
508 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
509 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
510
511Example:
512 $FUNCNAME push fix6789:gingerbread
513will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
514HEAD will be pushed from local if omitted.
515EOF
516 ;;
517 *)
518 $FUNCNAME __cmg_err_not_supported $1 && return
519 cat <<EOF
520usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
521
522works as:
523 git fetch http://DOMAIN/p/PROJECT \\
524 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
525 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
526EOF
527 ;;
528 esac
529 ;;
530 __cmg_get_ref)
531 $FUNCNAME __cmg_err_no_arg $command $# && return 1
532 local change_id patchset_id hash
533 case $1 in
534 */*)
535 change_id=${1%%/*}
536 patchset_id=${1#*/}
537 ;;
538 *)
539 change_id=$1
540 patchset_id=1
541 ;;
542 esac
543 hash=$(($change_id % 100))
544 case $hash in
545 [0-9]) hash="0$hash" ;;
546 esac
547 echo "refs/changes/$hash/$change_id/$patchset_id"
548 ;;
549 fetch|pull)
550 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
551 $FUNCNAME __cmg_err_not_repo && return 1
552 local change=$1
553 shift
554 git $command $@ http://$review/p/$project \
555 $($FUNCNAME __cmg_get_ref $change) || return 1
556 ;;
557 push)
558 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
559 $FUNCNAME __cmg_err_not_repo && return 1
560 if [ -z "$user" ]; then
561 echo >&2 "Gerrit username not found."
562 return 1
563 fi
564 local local_branch remote_branch
565 case $1 in
566 *:*)
567 local_branch=${1%:*}
568 remote_branch=${1##*:}
569 ;;
570 *)
571 local_branch=HEAD
572 remote_branch=$1
573 ;;
574 esac
575 shift
576 git push $@ ssh://$user@$review:29418/$project \
577 $local_branch:refs/for/$remote_branch || return 1
578 ;;
579 changes|for)
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200580 if [ "$FUNCNAME" = "lineagegerrit" ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300581 echo >&2 "'$FUNCNAME $command' is deprecated."
582 fi
583 ;;
584 __cmg_err_no_arg)
585 if [ $# -lt 2 ]; then
586 echo >&2 "'$FUNCNAME $command' missing argument."
587 elif [ $2 -eq 0 ]; then
588 if [ -n "$3" ]; then
589 $FUNCNAME help $1
590 else
591 echo >&2 "'$FUNCNAME $1' missing argument."
592 fi
593 else
594 return 1
595 fi
596 ;;
597 __cmg_err_not_repo)
598 if [ -z "$review" -o -z "$project" ]; then
599 echo >&2 "Not currently in any reviewable repository."
600 else
601 return 1
602 fi
603 ;;
604 __cmg_err_not_supported)
605 $FUNCNAME __cmg_err_no_arg $command $# && return
606 case $1 in
607 #TODO: filter more git commands that don't use refname
608 init|add|rm|mv|status|clone|remote|bisect|config|stash)
609 echo >&2 "'$FUNCNAME $1' is not supported."
610 ;;
611 *) return 1 ;;
612 esac
613 ;;
614 #TODO: other special cases?
615 *)
616 $FUNCNAME __cmg_err_not_supported $command && return 1
617 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
618 $FUNCNAME __cmg_err_not_repo && return 1
619 local args="$@"
620 local change pre_args refs_arg post_args
621 case "$args" in
622 *--\ *)
623 pre_args=${args%%-- *}
624 post_args="-- ${args#*-- }"
625 ;;
626 *) pre_args="$args" ;;
627 esac
628 args=($pre_args)
629 pre_args=
630 if [ ${#args[@]} -gt 0 ]; then
631 change=${args[${#args[@]}-1]}
632 fi
633 if [ ${#args[@]} -gt 1 ]; then
634 pre_args=${args[0]}
635 for ((i=1; i<${#args[@]}-1; i++)); do
636 pre_args="$pre_args ${args[$i]}"
637 done
638 fi
639 while ((1)); do
640 case $change in
641 ""|--)
642 $FUNCNAME help $command
643 return 1
644 ;;
645 *@*)
646 if [ -z "$refs_arg" ]; then
647 refs_arg="@${change#*@}"
648 change=${change%%@*}
649 fi
650 ;;
651 *~*)
652 if [ -z "$refs_arg" ]; then
653 refs_arg="~${change#*~}"
654 change=${change%%~*}
655 fi
656 ;;
657 *^*)
658 if [ -z "$refs_arg" ]; then
659 refs_arg="^${change#*^}"
660 change=${change%%^*}
661 fi
662 ;;
663 *:*)
664 if [ -z "$refs_arg" ]; then
665 refs_arg=":${change#*:}"
666 change=${change%%:*}
667 fi
668 ;;
669 *) break ;;
670 esac
671 done
672 $FUNCNAME fetch $change \
673 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
674 || return 1
675 ;;
676 esac
677}
678
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200679function lineagerebase() {
Michael Bestas3952f6c2016-08-26 01:12:08 +0300680 local repo=$1
681 local refs=$2
682 local pwd="$(pwd)"
683 local dir="$(gettop)/$repo"
684
685 if [ -z $repo ] || [ -z $refs ]; then
Dan Pasanen03447712016-12-19 11:22:55 -0600686 echo "LineageOS Gerrit Rebase Usage: "
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200687 echo " lineagerebase <path to project> <patch IDs on Gerrit>"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300688 echo " The patch IDs appear on the Gerrit commands that are offered."
689 echo " They consist on a series of numbers and slashes, after the text"
690 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
691 echo ""
692 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
693 echo ""
694 return
695 fi
696
697 if [ ! -d $dir ]; then
698 echo "Directory $dir doesn't exist in tree."
699 return
700 fi
701 cd $dir
702 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
703 echo "Starting branch..."
704 repo start tmprebase .
705 echo "Bringing it up to date..."
706 repo sync .
707 echo "Fetching change..."
Dan Pasanen03447712016-12-19 11:22:55 -0600708 git fetch "http://review.lineageos.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Michael Bestas3952f6c2016-08-26 01:12:08 +0300709 if [ "$?" != "0" ]; then
710 echo "Error cherry-picking. Not uploading!"
711 return
712 fi
713 echo "Uploading..."
714 repo upload .
715 echo "Cleaning up..."
716 repo abandon tmprebase .
717 cd $pwd
718}
719
720function mka() {
Luca Stefani085af722017-08-17 20:34:44 +0200721 m -j "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300722}
723
724function cmka() {
725 if [ ! -z "$1" ]; then
726 for i in "$@"; do
727 case $i in
728 bacon|otapackage|systemimage)
729 mka installclean
730 mka $i
731 ;;
732 *)
733 mka clean-$i
734 mka $i
735 ;;
736 esac
737 done
738 else
739 mka clean
740 mka
741 fi
742}
743
Michael Bestas3952f6c2016-08-26 01:12:08 +0300744function repolastsync() {
745 RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
746 RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
747 RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
748 echo "Last repo sync: $RLSLOCAL / $RLSUTC"
749}
750
751function reposync() {
Luca Stefani085af722017-08-17 20:34:44 +0200752 repo sync -j 4 "$@"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300753}
754
755function repodiff() {
756 if [ -z "$*" ]; then
757 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
758 return
759 fi
760 diffopts=$* repo forall -c \
761 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
762}
763
764# Return success if adb is up and not in recovery
765function _adb_connected {
766 {
Alessandro Astonecdf9ae82019-09-28 16:53:08 +0200767 if [[ "$(adb get-state)" == device ]]
Michael Bestas3952f6c2016-08-26 01:12:08 +0300768 then
769 return 0
770 fi
771 } 2>/dev/null
772
773 return 1
774};
775
776# Credit for color strip sed: http://goo.gl/BoIcm
777function dopush()
778{
779 local func=$1
780 shift
781
782 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
783 if ! _adb_connected; then
784 echo "No device is online. Waiting for one..."
785 echo "Please connect USB and/or enable USB debugging"
786 until _adb_connected; do
787 sleep 1
788 done
789 echo "Device Found."
790 fi
791
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200792 if (adb shell getprop ro.lineage.device | grep -q "$LINEAGE_BUILD") || [ "$FORCE_PUSH" = "true" ];
Michael Bestas3952f6c2016-08-26 01:12:08 +0300793 then
794 # retrieve IP and PORT info if we're using a TCP connection
Marc K2be9cac2016-10-20 10:27:35 +0200795 TCPIPPORT=$(adb devices \
796 | 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 +0300797 | head -1 | awk '{print $1}')
798 adb root &> /dev/null
799 sleep 0.3
800 if [ -n "$TCPIPPORT" ]
801 then
802 # adb root just killed our connection
803 # so reconnect...
804 adb connect "$TCPIPPORT"
805 fi
806 adb wait-for-device &> /dev/null
807 sleep 0.3
808 adb remount &> /dev/null
809
810 mkdir -p $OUT
811 ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
812 ret=$?;
813 if [ $ret -ne 0 ]; then
814 rm -f $OUT/.log;return $ret
815 fi
816
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500817 is_gnu_sed=`sed --version | head -1 | grep -c GNU`
818
Michael Bestas3952f6c2016-08-26 01:12:08 +0300819 # Install: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500820 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200821 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}\] +//' \
822 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300823 else
Marc K97b035d2016-10-20 10:27:44 +0200824 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}\] +//" \
825 | grep '^Install: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300826 fi
827
828 # Copy: <file>
Rashed Abdel-Tawab01806642017-01-19 15:52:13 -0500829 if [ $is_gnu_sed -gt 0 ]; then
Marc K97b035d2016-10-20 10:27:44 +0200830 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}\] +//' \
831 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300832 else
Marc K97b035d2016-10-20 10:27:44 +0200833 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}\] +//' \
834 | grep '^Copy: ' | cut -d ':' -f 2)"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300835 fi
836
837 # If any files are going to /data, push an octal file permissions reader to device
838 if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
839 CHKPERM="/data/local/tmp/chkfileperm.sh"
840(
841cat <<'EOF'
842#!/system/xbin/sh
843FILE=$@
844if [ -e $FILE ]; then
845 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
846fi
847EOF
848) > $OUT/.chkfileperm.sh
849 echo "Pushing file permissions checker to device"
850 adb push $OUT/.chkfileperm.sh $CHKPERM
851 adb shell chmod 755 $CHKPERM
852 rm -f $OUT/.chkfileperm.sh
853 fi
854
Sam Mortimerc3865952019-10-12 02:14:13 -0700855 RELOUT=$(echo $OUT | sed "s#^${ANDROID_BUILD_TOP}/##")
856
Michael Bestas3952f6c2016-08-26 01:12:08 +0300857 stop_n_start=false
Sam Mortimerc3865952019-10-12 02:14:13 -0700858 for TARGET in $(echo $LOC | tr " " "\n" | sed "s#.*${RELOUT}##" | sort | uniq); do
Roman Birgd51094c2018-03-28 09:47:30 -0700859 # Make sure file is in $OUT/system, $OUT/data, $OUT/odm, $OUT/oem, $OUT/product, $OUT/product_services or $OUT/vendor
Adrian DCa8e06d32018-06-17 00:15:00 +0200860 case $TARGET in
Roman Birgd51094c2018-03-28 09:47:30 -0700861 /system/*|/data/*|/odm/*|/oem/*|/product/*|/product_services/*|/vendor/*)
Adrian DCa8e06d32018-06-17 00:15:00 +0200862 # Get out file from target (i.e. /system/bin/adb)
863 FILE=$OUT$TARGET
Michael Bestas3952f6c2016-08-26 01:12:08 +0300864 ;;
865 *) continue ;;
866 esac
867
868 case $TARGET in
869 /data/*)
870 # fs_config only sets permissions and se labels for files pushed to /system
871 if [ -n "$CHKPERM" ]; then
872 OLDPERM=$(adb shell $CHKPERM $TARGET)
873 OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
874 OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
875 OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
876 fi
877 echo "Pushing: $TARGET"
878 adb push $FILE $TARGET
879 if [ -n "$OLDPERM" ]; then
880 echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
881 adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
882 adb shell chmod "$OLDPERM" $TARGET
883 else
884 echo "$TARGET did not exist previously, you should set file permissions manually"
885 fi
886 adb shell restorecon "$TARGET"
887 ;;
888 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
889 # Only need to stop services once
890 if ! $stop_n_start; then
891 adb shell stop
892 stop_n_start=true
893 fi
894 echo "Pushing: $TARGET"
895 adb push $FILE $TARGET
896 ;;
897 *)
898 echo "Pushing: $TARGET"
899 adb push $FILE $TARGET
900 ;;
901 esac
902 done
903 if [ -n "$CHKPERM" ]; then
904 adb shell rm $CHKPERM
905 fi
906 if $stop_n_start; then
907 adb shell start
908 fi
909 rm -f $OUT/.log
910 return 0
911 else
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200912 echo "The connected device does not appear to be $LINEAGE_BUILD, run away!"
Michael Bestas3952f6c2016-08-26 01:12:08 +0300913 fi
914}
915
916alias mmp='dopush mm'
917alias mmmp='dopush mmm'
918alias mmap='dopush mma'
Zhao Wei Liew64fc5ae2016-12-10 16:48:27 +0800919alias mmmap='dopush mmma'
Michael Bestas3952f6c2016-08-26 01:12:08 +0300920alias mkap='dopush mka'
921alias cmkap='dopush cmka'
922
923function repopick() {
924 T=$(gettop)
Dan Pasanen91f76202017-07-06 08:21:30 -0500925 $T/vendor/lineage/build/tools/repopick.py $@
Michael Bestas3952f6c2016-08-26 01:12:08 +0300926}
927
928function fixup_common_out_dir() {
929 common_out_dir=$(get_build_var OUT_DIR)/target/common
930 target_device=$(get_build_var TARGET_DEVICE)
Sam Mortimer55d6ec52019-09-10 11:40:34 -0700931 common_target_out=common-${target_device}
Luca Stefani5c60e4f2017-08-17 19:28:48 +0200932 if [ ! -z $LINEAGE_FIXUP_COMMON_OUT ]; then
Michael Bestas3952f6c2016-08-26 01:12:08 +0300933 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
934 mv ${common_out_dir} ${common_out_dir}-${target_device}
Sam Mortimer55d6ec52019-09-10 11:40:34 -0700935 ln -s ${common_target_out} ${common_out_dir}
Michael Bestas3952f6c2016-08-26 01:12:08 +0300936 else
937 [ -L ${common_out_dir} ] && rm ${common_out_dir}
938 mkdir -p ${common_out_dir}-${target_device}
Sam Mortimer55d6ec52019-09-10 11:40:34 -0700939 ln -s ${common_target_out} ${common_out_dir}
Michael Bestas3952f6c2016-08-26 01:12:08 +0300940 fi
941 else
942 [ -L ${common_out_dir} ] && rm ${common_out_dir}
943 mkdir -p ${common_out_dir}
944 fi
945}