blob: 06554c625ab976aaadb7780cfd0ff710891f73ec [file] [log] [blame]
#! /bin/bash
# Run given scripted change and commit the changes.
#
# Assumes that the current directory is the top-level directory of
# the Android source code, created with 'repo init', and that 'repo'
# tool is on the path.
# For each of the given repo git repositories:
# 1. Checks there are neither modified not untracked files in it.
# 2. Runs the given script, which is supposed to change some files
# 3. Creates a development branch if necessary
# 4. Commits changed files. The commit message is extracted from the
# script and contains all the lines in it starting with ##CL
#
# As an example, running
# build/bazel/mk2rbc/apply_scripted_change.sh build/bazel/mk2rbc/replace_is_board_platform.sh hardware/qcom/media
# replaces the old is-board-platform calls with the new is-board-platform2 calls.
set -eu
function die() { format=$1; shift; printf "$format\n" $@; exit 1; }
function usage() { die "Usage: %s script git-repo ..." ${0##*/}; }
(($#>=2)) || usage
declare -r script=$(realpath $1); shift
rc=0
[[ -x $script ]] || die "%s is not an executable script" $script
[[ -d .repo ]] || die "$PWD is not an Android source tree (.repo/ is missing)"
declare -r bugid="$(sed -nr 's/^##CL (Bug:|Fixes:) +([0-9]+)$/\2/p' $script)"
[[ -n "$bugid" ]] || die "$script contains neither '##CL Bug: ' nor '##CL Fixes: 'tag"
for gr in $@; do
[[ -d $gr/.git ]] || { echo $gr is not a Git directory; rc=1; continue; }
out=$(git -C $gr status --porcelain --untracked-files=no) || { die "so skipping $gr because of the above"; rc=1; continue; }
[[ -z "$out" ]] || { echo $gr contains uncommitted changes:; echo "$out" >&2; rc=1; continue; }
(cd $gr && $script)
modified="$(git -C $gr status --porcelain | sed -nr 's/^ M (.*)/\1/p')"
[[ -n "$modified" ]] || { echo no files changed in $gr; continue; }
[[ -z "$(repo status -q $gr 2>/dev/null)" ]] || repo start b$bugid $gr
git -C $gr add $modified
git -C $gr commit -q \
-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 '```')
done
exit $rc