#!/usr/bin/env bash # merge-gitignore.sh # 把模板里的忽略规则合并到项目根的 .gitignore: # - 若 .gitignore 不存在 → 直接 cp 模板(含注释头和结构) # - 若已存在 → 逐行判重,只追加模板里缺失的规则行(跳过注释/空行) # # 用法:merge-gitignore.sh [] # template_path 模板文件绝对路径(由 design skill 传入) # target_gitignore_path 目标 .gitignore 路径,默认为当前工作目录下的 .gitignore # # 判重:grep -xF 整行精确匹配 + 字面字符串(非正则),避免 .env 误匹配 .env.local, # 也避免 *.class / *.iml 等通配符被当作 regex。 set -euo pipefail template="${1:?usage: merge-gitignore.sh []}" target="${2:-.gitignore}" if [ ! -f "$template" ]; then echo "[merge-gitignore] ERROR: template not found: $template" >&2 exit 1 fi if [ ! -f "$target" ]; then cp "$template" "$target" echo "[merge-gitignore] created $target from template" exit 0 fi added=0 while IFS= read -r line; do case "$line" in ""|"#"*) continue ;; esac if ! grep -qxF "$line" "$target"; then echo "$line" >> "$target" added=$((added + 1)) fi done < "$template" echo "[merge-gitignore] $target updated (+$added rules)"