#!/bin/bash

# 脚本所在绝对目录(/home/workdir/test/vendor/{厂家}/{产品})
# patch.yml格式
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# 补丁配置文件路径
#build:
#  - vendor/sc280/patch/build/0001_build.patch


PATCH_YML="${SCRIPT_DIR}/patch.yml"

# 检查配置文件是否存在
if [ ! -f "$PATCH_YML" ]; then
    echo "错误:未找到补丁配置文件 $PATCH_YML"
    exit 1
fi

# 解析YAML获取目标目录和补丁路径
parse_patches() {
    local dir=""
    while IFS= read -r line; do
        [[ -z "$line" || "$line" =~ ^# ]] && continue  # 跳过空行和注释
        if [[ ! "$line" =~ ^[[:space:]]*- ]]; then
            # 提取目标目录(如 build、foundation/...)
            dir=$(echo "$line" | sed -e 's/:[[:space:]]*$//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
        else
            # 提取补丁文件路径
            patch_file=$(echo "$line" | sed -e 's/^[[:space:]]*-[[:space:]]*//' -e 's/[[:space:]]*$//')
            if [ -n "$dir" ] && [ -n "$patch_file" ]; then
                echo "$dir|$patch_file"
            fi
        fi
    done < "$PATCH_YML"
}

# 修正补丁路径:移除"vendor/sc280/"前缀
fix_patch_path() {
    local original_path="$1"
    echo "$original_path" | sed 's#^vendor/sc280/##'
}

# 应用补丁
apply_patches() {
    echo "开始应用补丁..."
    parse_patches | while IFS="|" read -r target_dir patch_file; do
        # 1. 处理补丁文件路径(已验证正确)
        fixed_patch=$(fix_patch_path "$patch_file")
        abs_patch="${SCRIPT_DIR}/${fixed_patch}"

        # 2. 处理目标目录路径:从sc280目录上跳3级到test目录(关键修正!)
        # 原路径:/home/dengshuihua/workdir/test/vendor/smartchip/sc280
        # 上跳3级:sc280 -> smartchip -> vendor -> test(正确根目录)
        project_root="${SCRIPT_DIR}/../../../"  # 修正为3个../
        abs_target="${project_root}/${target_dir}"

        # 3. 检查文件和目录
        if [ ! -f "$abs_patch" ]; then
            echo "警告:补丁文件 $abs_patch 不存在,跳过"
            continue
        fi
        if [ ! -d "$abs_target" ]; then
            echo "警告:目标目录 $abs_target 不存在,跳过"
            continue
        fi

        # 4. 应用补丁
        echo "应用补丁:$abs_patch -> $abs_target"
        patch -d "$abs_target" -p1 --forward -i "$abs_patch"
        if [ $? -ne 0 ]; then
            echo "警告:$abs_patch 应用失败(可能已应用或冲突)"
        fi
    done
    echo "补丁应用完成"
}

# 撤销补丁
revert_patches() {
    echo "开始撤销补丁..."
    parse_patches | tac | while IFS="|" read -r target_dir patch_file; do
        fixed_patch=$(fix_patch_path "$patch_file")
        abs_patch="${SCRIPT_DIR}/${fixed_patch}"
        project_root="${SCRIPT_DIR}/../../../"  # 同样修正为3个../
        abs_target="${project_root}/${target_dir}"

        if [ ! -f "$abs_patch" ]; then
            echo "警告:补丁文件 $abs_patch 不存在,跳过"
            continue
        fi
        if [ ! -d "$abs_target" ]; then
            echo "警告:目标目录 $abs_target 不存在,跳过"
            continue
        fi

        echo "撤销补丁:$abs_patch <- $abs_target"
        patch -d "$abs_target" -p1 -R -i "$abs_patch"
        if [ $? -ne 0 ]; then
            echo "警告:$abs_patch 撤销失败(可能未应用)"
        fi
    done
    echo "补丁撤销完成"
}

# 帮助信息
show_help() {
    echo "用法:$0 [选项]"
    echo "  apply   - 应用所有补丁"
    echo "  revert  - 撤销所有补丁"
    echo "  help    - 显示帮助信息"
}

# 主逻辑
case "$1" in
    apply)
        apply_patches
        ;;
    revert)
        revert_patches
        ;;
    help|*)
        show_help
        ;;
esac

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐