xref: /aosp_15_r20/build/bazel/mk2rbc/apply_scripted_change.sh (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker#! /bin/bash
2*7594170eSAndroid Build Coastguard Worker# Run given scripted change and commit the changes.
3*7594170eSAndroid Build Coastguard Worker#
4*7594170eSAndroid Build Coastguard Worker# Assumes that the current directory is the top-level directory of
5*7594170eSAndroid Build Coastguard Worker# the Android source code, created with 'repo init', and that 'repo'
6*7594170eSAndroid Build Coastguard Worker# tool is on the path.
7*7594170eSAndroid Build Coastguard Worker# For each of the given repo git repositories:
8*7594170eSAndroid Build Coastguard Worker# 1. Checks there are neither modified not untracked files in it.
9*7594170eSAndroid Build Coastguard Worker# 2. Runs the given script, which is supposed to change some files
10*7594170eSAndroid Build Coastguard Worker# 3. Creates a development branch if necessary
11*7594170eSAndroid Build Coastguard Worker# 4. Commits changed files. The commit message is extracted from the
12*7594170eSAndroid Build Coastguard Worker#    script and contains all the lines in it starting with ##CL
13*7594170eSAndroid Build Coastguard Worker#
14*7594170eSAndroid Build Coastguard Worker# As an example, running
15*7594170eSAndroid Build Coastguard Worker#   build/bazel/mk2rbc/apply_scripted_change.sh build/bazel/mk2rbc/replace_is_board_platform.sh hardware/qcom/media
16*7594170eSAndroid Build Coastguard Worker# replaces the old is-board-platform calls with the new is-board-platform2 calls.
17*7594170eSAndroid Build Coastguard Worker
18*7594170eSAndroid Build Coastguard Workerset -eu
19*7594170eSAndroid Build Coastguard Worker
20*7594170eSAndroid Build Coastguard Workerfunction die() { format=$1; shift; printf "$format\n" $@; exit 1; }
21*7594170eSAndroid Build Coastguard Workerfunction usage() { die "Usage: %s script git-repo ..."  ${0##*/}; }
22*7594170eSAndroid Build Coastguard Worker
23*7594170eSAndroid Build Coastguard Worker(($#>=2)) || usage
24*7594170eSAndroid Build Coastguard Workerdeclare -r script=$(realpath $1); shift
25*7594170eSAndroid Build Coastguard Workerrc=0
26*7594170eSAndroid Build Coastguard Worker
27*7594170eSAndroid Build Coastguard Worker[[ -x $script ]] || die "%s is not an executable script" $script
28*7594170eSAndroid Build Coastguard Workerdeclare -r bugid="$(sed -nr 's/^##CL (Bug:|Fixes:) +([0-9]+)$/\2/p' $script)"
29*7594170eSAndroid Build Coastguard Worker[[ -n "$bugid" ]] || die "$script contains neither '##CL Bug: ' nor '##CL Fixes: 'tag"
30*7594170eSAndroid Build Coastguard Worker
31*7594170eSAndroid Build Coastguard Worker
32*7594170eSAndroid Build Coastguard Workerfor gr in $@; do
33*7594170eSAndroid Build Coastguard Worker    [[  -d $gr/.git ]] || { echo $gr is not a Git directory; rc=1; continue; }
34*7594170eSAndroid Build Coastguard Worker    out=$(git -C $gr status --porcelain --untracked-files=no) || { die "so skipping $gr because of the above"; rc=1; continue; }
35*7594170eSAndroid Build Coastguard Worker    [[ -z "$out" ]] || { echo  $gr contains uncommitted changes:; echo "$out" >&2; rc=1; continue; }
36*7594170eSAndroid Build Coastguard Worker    (cd $gr && $script)
37*7594170eSAndroid Build Coastguard Worker    modified="$(git -C $gr status --porcelain | sed -nr 's/^ M (.*)/\1/p')"
38*7594170eSAndroid Build Coastguard Worker    [[ -n "$modified" ]] || { echo no files changed in $gr; continue; }
39*7594170eSAndroid Build Coastguard Worker    [[ -z "$(repo status -q $gr 2>/dev/null)" ]] || repo start b$bugid $gr
40*7594170eSAndroid Build Coastguard Worker    git -C $gr add $modified
41*7594170eSAndroid Build Coastguard Worker    git -C $gr commit -q \
42*7594170eSAndroid Build Coastguard Worker      -F <(sed -nr 's/^##CL *//p' $script; echo -e '\nThis change has been generated by the following script:\n\n```'; grep -vP '^##CL' $script; echo '```')
43*7594170eSAndroid Build Coastguard Workerdone
44*7594170eSAndroid Build Coastguard Worker
45*7594170eSAndroid Build Coastguard Workerexit $rc
46