huangcm
2025-07-01 676035278781360996553c427a12bf358249ebf7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
 
# Copyright (C) 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
 
# Given a source file, for example Constructor.java, find the patch file for it and apply it.
# Patch is applied to a copy (as opposed to in-place), for example into a build artifact location.
 
if [[ $# -lt 3 ]]; then
  echo "Usage: $(basename $0) <src-file-prefix> <src-file> <dst-file>"
  exit 1
fi
 
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ANDROID_PATCHES_DIR="$DIR/src/patches/android"
 
src_file_prefix="$1"
src_file="$2"
src_file_with_prefix="$2"
dst_file="$3"
dst_dir="$(dirname "$dst_file")"
 
# Sanity checking
 
if ! [[ -d $ANDROID_PATCHES_DIR ]]; then
  echo "FATAL: Android patch directory $ANDROID_PATCHES_DIR does not exist." >&2
  exit 1
fi
 
if ! [[ $src_file == $src_file_prefix* ]]; then
  echo "$src_file_prefix is not a valid prefix of $src_file" >&2
  exit 1
fi
 
if ! [[ -f $src_file ]]; then
  echo "Source file $src_file does not exist." >&2
  exit 1
fi
 
# Remove the src file prefix, giving us the correct grep target for the .patch files.
src_file="${src_file#$src_file_prefix}"
 
patch_file_src=$(grep --files-with-matches -R "diff --git a/$src_file" "$ANDROID_PATCHES_DIR")
if [[ $? -ne 0 ]]; then
  echo "Error: Could not find a corresponding .patch file for $src_file" >&2
  exit 1
fi
 
# Parse the source file from the .patch file (assumes exactly one file diff per .patch file)
src_file_check=$(cat "$patch_file_src" | grep "diff --git a/" | sed -e "s:diff --git a\/::g" | sed -e "s: b\/.*::g")
 
 
if ! [[ -f $patch_file_src ]]; then
  echo "Patch file $patch_file_src does not exist." >&2
  exit 1
fi
 
if ! cat "$patch_file_src" | grep "diff --git a/" > /dev/null; then
  echo "File $patch_file_src is not a valid diff file" >&2
  exit 1
fi
 
if [[ $src_file != $src_file_check ]]; then
  echo "Check-fail: $src_file was not same as found in patch file ($src_file_check)" >&2
  exit 1
fi
 
set -e
 
# Copy the src file and then apply the patch to it.
mkdir -p "$dst_dir"
cp "$src_file_with_prefix" "$dst_file"
if ! [[ -f "$dst_file" ]]; then
  echo "File "$dst_file" does not exist; patching will fail" >&2
  exit 1
fi
cd "$dst_dir"
patch --quiet "$(basename "$dst_file")" "$patch_file_src"