|
| 1 | +#!/bin/bash |
| 2 | +# ============================================================================ |
| 3 | +# Linux Kernel Submodule 管理脚本 |
| 4 | +# ============================================================================ |
| 5 | +# 功能: |
| 6 | +# - init: 幂等初始化 Linux kernel submodule 到 third_party/linux |
| 7 | +# - reset: 硬复原到远程最新状态(完全清理后重新初始化) |
| 8 | +# - status: 查看当前 submodule 状态 |
| 9 | +# |
| 10 | +# 使用方法: |
| 11 | +# ./scripts/linux-submodule.sh init |
| 12 | +# ./scripts/linux-submodule.sh reset |
| 13 | +# ./scripts/linux-submodule.sh status |
| 14 | +# ============================================================================ |
| 15 | + |
| 16 | +set -eEuo pipefail |
| 17 | + |
| 18 | +# 颜色输出 |
| 19 | +readonly RED='\033[0;31m' |
| 20 | +readonly GREEN='\033[0;32m' |
| 21 | +readonly YELLOW='\033[0;33m' |
| 22 | +readonly BLUE='\033[0;34m' |
| 23 | +readonly NC='\033[0m' # No Color |
| 24 | + |
| 25 | +# Linux kernel 官方仓库 URLs |
| 26 | +readonly LINUX_KERNEL_UPSTREAM="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git" |
| 27 | +readonly LINUX_KERNEL_GITEE="https://gitee.com/mirrors/linux_stable.git" |
| 28 | + |
| 29 | +# 默认分支 |
| 30 | +readonly DEFAULT_BRANCH="linux-6.19.y" |
| 31 | + |
| 32 | +# 第三方目录 |
| 33 | +THIRD_PARTY_DIR="" |
| 34 | +SUBMODULE_PATH="" |
| 35 | + |
| 36 | +# 项目根目录 |
| 37 | +PROJECT_ROOT="" |
| 38 | + |
| 39 | +# 日志函数(输出到 stderr,避免污染命令替换) |
| 40 | +log_info() { |
| 41 | + echo -e "${BLUE}[INFO]${NC} $*" >&2 |
| 42 | +} |
| 43 | + |
| 44 | +log_success() { |
| 45 | + echo -e "${GREEN}[SUCCESS]${NC} $*" >&2 |
| 46 | +} |
| 47 | + |
| 48 | +log_warn() { |
| 49 | + echo -e "${YELLOW}[WARN]${NC} $*" >&2 |
| 50 | +} |
| 51 | + |
| 52 | +log_error() { |
| 53 | + echo -e "${RED}[ERROR]${NC} $*" >&2 |
| 54 | +} |
| 55 | + |
| 56 | +# 查找项目根目录(包含 .git 的目录) |
| 57 | +find_project_root() { |
| 58 | + local current_dir="$PWD" |
| 59 | + while [[ "$current_dir" != "/" ]]; do |
| 60 | + if [[ -d "$current_dir/.git" ]]; then |
| 61 | + echo "$current_dir" |
| 62 | + return 0 |
| 63 | + fi |
| 64 | + current_dir="$(dirname "$current_dir")" |
| 65 | + done |
| 66 | + log_error "无法找到 Git 项目根目录" |
| 67 | + exit 1 |
| 68 | +} |
| 69 | + |
| 70 | +# 初始化路径变量 |
| 71 | +init_paths() { |
| 72 | + PROJECT_ROOT="$(find_project_root)" |
| 73 | + THIRD_PARTY_DIR="$PROJECT_ROOT/third_party" |
| 74 | + SUBMODULE_PATH="$THIRD_PARTY_DIR/linux" |
| 75 | + |
| 76 | + log_info "项目根目录: $PROJECT_ROOT" |
| 77 | + log_info "Submodule 路径: $SUBMODULE_PATH (相对于项目根目录: third_party/linux)" |
| 78 | +} |
| 79 | + |
| 80 | +# 检查 git 命令是否可用 |
| 81 | +check_git() { |
| 82 | + if ! command -v git &> /dev/null; then |
| 83 | + log_error "git 命令未找到,请先安装 git" |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +# 检查网络连接 |
| 89 | +check_network() { |
| 90 | + local url="$1" |
| 91 | + if curl -I -s --connect-timeout 5 "$url" > /dev/null 2>&1; then |
| 92 | + return 0 |
| 93 | + else |
| 94 | + return 1 |
| 95 | + fi |
| 96 | +} |
| 97 | + |
| 98 | +# 选择可用的镜像 |
| 99 | +select_mirror() { |
| 100 | + log_info "检测网络连接..." |
| 101 | + local mirror="" |
| 102 | + if check_network "https://git.kernel.org"; then |
| 103 | + mirror="$LINUX_KERNEL_UPSTREAM" |
| 104 | + log_success "使用官方镜像: git.kernel.org" |
| 105 | + elif check_network "https://gitee.com"; then |
| 106 | + mirror="$LINUX_KERNEL_GITEE" |
| 107 | + log_warn "官方镜像不可达,使用 Gitee 镜像" |
| 108 | + else |
| 109 | + log_error "无法连接到任何镜像源" |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | + printf '%s' "$mirror" |
| 113 | +} |
| 114 | + |
| 115 | +# 幂等初始化 submodule |
| 116 | +cmd_init() { |
| 117 | + cd "$PROJECT_ROOT" |
| 118 | + local mirror |
| 119 | + mirror="$(select_mirror)" |
| 120 | + |
| 121 | + log_info "开始初始化 Linux kernel submodule..." |
| 122 | + |
| 123 | + # 创建 third_party 目录(幂等) |
| 124 | + mkdir -p "$THIRD_PARTY_DIR" |
| 125 | + |
| 126 | + # 检查 submodule 是否已存在于 .gitmodules 或 git index |
| 127 | + if git config --file .gitmodules --get submodule.third_party/linux &> /dev/null; then |
| 128 | + log_info "Submodule 配置已存在于 .gitmodules" |
| 129 | + elif git ls-files --error-unmatch third_party/linux &> /dev/null; then |
| 130 | + log_info "Submodule 已存在于 git index(但缺少 .gitmodules 配置),补充配置..." |
| 131 | + # 补充 .gitmodules 配置(index 已有记录,不能再用 submodule add) |
| 132 | + git config -f .gitmodules submodule.third_party/linux.path third_party/linux |
| 133 | + git config -f .gitmodules submodule.third_party/linux.url "$mirror" |
| 134 | + git config -f .gitmodules submodule.third_party/linux.branch "$DEFAULT_BRANCH" |
| 135 | + git add .gitmodules |
| 136 | + else |
| 137 | + log_info "添加 submodule 配置..." |
| 138 | + git submodule add -b "$DEFAULT_BRANCH" --name third_party/linux \ |
| 139 | + "$mirror" third_party/linux |
| 140 | + fi |
| 141 | + |
| 142 | + # 检查 submodule 是否已初始化 |
| 143 | + if [[ -f "$SUBMODULE_PATH/.git" ]]; then |
| 144 | + log_info "Submodule 已初始化,更新到最新版本..." |
| 145 | + cd "$SUBMODULE_PATH" |
| 146 | + git fetch origin |
| 147 | + git checkout "$DEFAULT_BRANCH" |
| 148 | + git pull origin "$DEFAULT_BRANCH" |
| 149 | + cd "$PROJECT_ROOT" |
| 150 | + else |
| 151 | + log_info "初始化 submodule..." |
| 152 | + git submodule update --init --checkout -- third_party/linux |
| 153 | + fi |
| 154 | + |
| 155 | + log_success "Linux kernel submodule 初始化完成!" |
| 156 | + log_info "路径: $SUBMODULE_PATH" |
| 157 | + log_info "分支: $DEFAULT_BRANCH" |
| 158 | +} |
| 159 | + |
| 160 | +# 硬复原到远程最新状态 |
| 161 | +cmd_reset() { |
| 162 | + cd "$PROJECT_ROOT" |
| 163 | + |
| 164 | + log_warn "即将执行硬复原操作,这将删除所有本地修改!" |
| 165 | + read -p "确认继续? [y/N] " -n 1 -r |
| 166 | + echo |
| 167 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 168 | + log_info "操作已取消" |
| 169 | + return 0 |
| 170 | + fi |
| 171 | + |
| 172 | + log_info "开始硬复原 Linux kernel submodule..." |
| 173 | + |
| 174 | + # 1. 从 .git/config 中移除 submodule 配置(如果存在) |
| 175 | + if git config --local --get submodule.third_party/linux.url &> /dev/null; then |
| 176 | + log_info "移除 .git/config 中的 submodule 配置..." |
| 177 | + git config --local --remove-section submodule.third_party/linux 2>/dev/null || true |
| 178 | + fi |
| 179 | + |
| 180 | + # 2. 从 .gitmodules 中移除配置(如果存在) |
| 181 | + if git config --file .gitmodules --get submodule.third_party/linux.url &> /dev/null; then |
| 182 | + log_info "移除 .gitmodules 中的 submodule 配置..." |
| 183 | + git config --file .gitmodules --remove-section submodule.third_party/linux 2>/dev/null || true |
| 184 | + fi |
| 185 | + |
| 186 | + # 3. 从 git index 中移除(如果存在) |
| 187 | + if git ls-files --error-unmatch third_party/linux &> /dev/null; then |
| 188 | + log_info "从 git index 中移除..." |
| 189 | + git rm --cached third_party/linux 2>/dev/null || true |
| 190 | + fi |
| 191 | + |
| 192 | + # 4. 删除物理目录(如果存在) |
| 193 | + if [[ -d "$SUBMODULE_PATH" ]]; then |
| 194 | + log_info "删除物理目录..." |
| 195 | + rm -rf "$SUBMODULE_PATH" |
| 196 | + fi |
| 197 | + |
| 198 | + # 5. 清理 .git/modules 中的缓存(如果存在) |
| 199 | + if [[ -d "$PROJECT_ROOT/.git/modules/third_party" ]]; then |
| 200 | + log_info "清理 .git/modules 缓存..." |
| 201 | + rm -rf "$PROJECT_ROOT/.git/modules/third_party" |
| 202 | + fi |
| 203 | + |
| 204 | + log_success "清理完成,重新初始化..." |
| 205 | + |
| 206 | + # 重新初始化 |
| 207 | + cmd_init |
| 208 | +} |
| 209 | + |
| 210 | +# 查看状态 |
| 211 | +cmd_status() { |
| 212 | + cd "$PROJECT_ROOT" |
| 213 | + |
| 214 | + log_info "=== Submodule 状态 ===" |
| 215 | + |
| 216 | + # 检查 .gitmodules 是否存在配置 |
| 217 | + if git config --file .gitmodules --get submodule.third_party/linux.url &> /dev/null; then |
| 218 | + local url |
| 219 | + url="$(git config --file .gitmodules --get submodule.third_party/linux.url)" |
| 220 | + log_info ".gitmodules 配置: ✓" |
| 221 | + log_info " URL: $url" |
| 222 | + else |
| 223 | + log_warn ".gitmodules 配置: ✗ (未配置)" |
| 224 | + fi |
| 225 | + |
| 226 | + # 检查物理目录是否存在 |
| 227 | + if [[ -d "$SUBMODULE_PATH" ]]; then |
| 228 | + log_info "物理目录: ✓ ($SUBMODULE_PATH)" |
| 229 | + |
| 230 | + # 检查是否是有效的 git 仓库 |
| 231 | + if [[ -d "$SUBMODULE_PATH/.git" ]]; then |
| 232 | + cd "$SUBMODULE_PATH" |
| 233 | + local branch |
| 234 | + local commit |
| 235 | + local status |
| 236 | + branch="$(git branch --show-current 2>/dev/null || echo "无分支")" |
| 237 | + commit="$(git rev-parse --short HEAD 2>/dev/null || echo "未知")" |
| 238 | + status="$(git status --porcelain 2>/dev/null && echo "有未提交的修改" || echo "干净")" |
| 239 | + |
| 240 | + log_info " 当前分支: $branch" |
| 241 | + log_info " 当前提交: $commit" |
| 242 | + log_info " 工作区: $status" |
| 243 | + |
| 244 | + # 检查是否是 submodule |
| 245 | + cd "$PROJECT_ROOT" |
| 246 | + if git submodule status third_party/linux &> /dev/null; then |
| 247 | + log_info " Git submodule 状态:" |
| 248 | + git submodule status third_party/linux |
| 249 | + fi |
| 250 | + else |
| 251 | + log_warn " .git 目录不存在" |
| 252 | + fi |
| 253 | + else |
| 254 | + log_warn "物理目录: ✗ (不存在)" |
| 255 | + fi |
| 256 | + |
| 257 | + # 检查 .git/config 配置 |
| 258 | + if git config --local --get submodule.third_party/linux.url &> /dev/null; then |
| 259 | + log_info ".git/config 配置: ✓" |
| 260 | + else |
| 261 | + log_info ".git/config 配置: ✗ (未初始化)" |
| 262 | + fi |
| 263 | +} |
| 264 | + |
| 265 | +# 显示帮助信息 |
| 266 | +cmd_help() { |
| 267 | + cat << EOF |
| 268 | +Linux Kernel Submodule 管理脚本 |
| 269 | +
|
| 270 | +使用方法: |
| 271 | + $0 <command> |
| 272 | +
|
| 273 | +命令: |
| 274 | + init 幂等初始化 Linux kernel submodule 到 third_party/linux |
| 275 | + 如果已存在则更新到最新版本 |
| 276 | +
|
| 277 | + reset 硬复原到远程最新状态 |
| 278 | + 完全清理 submodule 后重新初始化(会删除本地修改) |
| 279 | +
|
| 280 | + status 查看当前 submodule 状态 |
| 281 | + 显示配置、分支、提交等信息 |
| 282 | +
|
| 283 | + help 显示此帮助信息 |
| 284 | +
|
| 285 | +示例: |
| 286 | + $0 init |
| 287 | + $0 status |
| 288 | + $0 reset |
| 289 | +
|
| 290 | +EOF |
| 291 | +} |
| 292 | + |
| 293 | +# 主函数 |
| 294 | +main() { |
| 295 | + check_git |
| 296 | + init_paths |
| 297 | + |
| 298 | + local command="${1:-help}" |
| 299 | + |
| 300 | + case "$command" in |
| 301 | + init) |
| 302 | + cmd_init |
| 303 | + ;; |
| 304 | + reset) |
| 305 | + cmd_reset |
| 306 | + ;; |
| 307 | + status) |
| 308 | + cmd_status |
| 309 | + ;; |
| 310 | + help|--help|-h) |
| 311 | + cmd_help |
| 312 | + ;; |
| 313 | + *) |
| 314 | + log_error "未知命令: $command" |
| 315 | + echo |
| 316 | + cmd_help |
| 317 | + exit 1 |
| 318 | + ;; |
| 319 | + esac |
| 320 | +} |
| 321 | + |
| 322 | +main "$@" |
0 commit comments