1*6777b538SAndroid Build Coastguard Worker#!/bin/bash 2*6777b538SAndroid Build Coastguard Worker 3*6777b538SAndroid Build Coastguard Worker# Copyright 2023 Google Inc. All rights reserved. 4*6777b538SAndroid Build Coastguard Worker# 5*6777b538SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*6777b538SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*6777b538SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*6777b538SAndroid Build Coastguard Worker# 9*6777b538SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*6777b538SAndroid Build Coastguard Worker# 11*6777b538SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*6777b538SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*6777b538SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*6777b538SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*6777b538SAndroid Build Coastguard Worker# limitations under the License. 16*6777b538SAndroid Build Coastguard Worker 17*6777b538SAndroid Build Coastguard Workerset -eu 18*6777b538SAndroid Build Coastguard Worker 19*6777b538SAndroid Build Coastguard Workermain() { 20*6777b538SAndroid Build Coastguard Worker # Check if the last commit included changes to a .gn or .gni file, which need 21*6777b538SAndroid Build Coastguard Worker # to be reflected in a .patch 22*6777b538SAndroid Build Coastguard Worker if git diff --name-only HEAD~..HEAD | egrep -q '[.]gni?$'; then 23*6777b538SAndroid Build Coastguard Worker echo "INFO: changes to a .gn|.gni file detected." 24*6777b538SAndroid Build Coastguard Worker else 25*6777b538SAndroid Build Coastguard Worker # no patch needed 26*6777b538SAndroid Build Coastguard Worker exit 0 27*6777b538SAndroid Build Coastguard Worker fi 28*6777b538SAndroid Build Coastguard Worker 29*6777b538SAndroid Build Coastguard Worker # HACK: checking for the existence of rebase-merge may not always work as 30*6777b538SAndroid Build Coastguard Worker # expected. 31*6777b538SAndroid Build Coastguard Worker # TODO: find a better solution. One option is to parse `git status` output and 32*6777b538SAndroid Build Coastguard Worker # grep for the "rebase in progress" string. 33*6777b538SAndroid Build Coastguard Worker if [[ -d "$(git rev-parse --git-dir)/rebase-merge" ]]; then 34*6777b538SAndroid Build Coastguard Worker # interactive rebase in progress. 35*6777b538SAndroid Build Coastguard Worker echo "WARNING! .patch files are not updated during interactive rebase" 36*6777b538SAndroid Build Coastguard Worker exit 0 37*6777b538SAndroid Build Coastguard Worker fi 38*6777b538SAndroid Build Coastguard Worker 39*6777b538SAndroid Build Coastguard Worker # There is no flag to skip the post-commit hook (--no-verify only works for 40*6777b538SAndroid Build Coastguard Worker # pre-commit and commit-msg hooks). Briefly remove the executable 41*6777b538SAndroid Build Coastguard Worker # permission to prevent recursion. 42*6777b538SAndroid Build Coastguard Worker chmod -x .git/hooks/post-commit 43*6777b538SAndroid Build Coastguard Worker # Ensure chmod +x is always run, even when script exits early 44*6777b538SAndroid Build Coastguard Worker trap "chmod +x .git/hooks/post-commit" EXIT 45*6777b538SAndroid Build Coastguard Worker 46*6777b538SAndroid Build Coastguard Worker # Remove any existing .patch files from the commit 47*6777b538SAndroid Build Coastguard Worker local -r patches_dir="${ANDROID_BUILD_TOP}/external/cronet/patches" 48*6777b538SAndroid Build Coastguard Worker git reset HEAD~ -- "${patches_dir}/*.patch" 49*6777b538SAndroid Build Coastguard Worker git -c "advice.ignoredHook=false" commit --amend --no-edit 50*6777b538SAndroid Build Coastguard Worker 51*6777b538SAndroid Build Coastguard Worker # Create patch (which only reflects changes to .gn and .gni files). 52*6777b538SAndroid Build Coastguard Worker local -r number_of_patches=$(git ls-tree -r HEAD~ -- "${patches_dir}" | wc -l) 53*6777b538SAndroid Build Coastguard Worker local -r patch_name=$(git format-patch -1 -N --start-number "${number_of_patches}" -o "${patches_dir}" HEAD -- "*.gn" "*.gni") 54*6777b538SAndroid Build Coastguard Worker git add "${patch_name}" 55*6777b538SAndroid Build Coastguard Worker git -c "advice.ignoredHook=false" commit --amend --no-edit 56*6777b538SAndroid Build Coastguard Worker 57*6777b538SAndroid Build Coastguard Worker echo "INFO: a .patch file was added to your CL" 58*6777b538SAndroid Build Coastguard Worker} 59*6777b538SAndroid Build Coastguard Worker 60*6777b538SAndroid Build Coastguard Workermain "$@"; exit 61*6777b538SAndroid Build Coastguard Worker 62