compliance/cleanup_git_tracking.sh
2025-08-07 17:14:40 +08:00

163 lines
4.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Git跟踪清理脚本
# 用于移除已被跟踪但应该被忽略的文件和目录
set -e
echo "🧹 Git跟踪清理脚本"
echo "===================="
# 检查是否在Git仓库中
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "❌ 当前目录不是Git仓库"
exit 1
fi
# 显示当前状态
echo "📊 当前Git状态:"
git status --short
echo ""
echo "🔍 检查需要移除跟踪的目录..."
# 检查build目录
if git ls-files | grep -q "^build/"; then
echo "📁 发现被跟踪的build目录"
BUILD_TRACKED=true
else
echo "✅ build目录未被跟踪"
BUILD_TRACKED=false
fi
# 检查dist目录
if git ls-files | grep -q "^dist/"; then
echo "📁 发现被跟踪的dist目录"
DIST_TRACKED=true
else
echo "✅ dist目录未被跟踪"
DIST_TRACKED=false
fi
# 检查其他常见的应该被忽略的文件
echo ""
echo "🔍 检查其他应该被忽略的文件..."
SHOULD_IGNORE_FILES=(
"*.pyc"
"__pycache__"
"*.log"
"*.tmp"
"*.temp"
".DS_Store"
"Thumbs.db"
"users.db"
)
FOUND_FILES=()
for pattern in "${SHOULD_IGNORE_FILES[@]}"; do
if git ls-files | grep -q "$pattern"; then
echo "📄 发现被跟踪的文件: $pattern"
FOUND_FILES+=("$pattern")
fi
done
# 如果没有需要清理的内容
if [ "$BUILD_TRACKED" = false ] && [ "$DIST_TRACKED" = false ] && [ ${#FOUND_FILES[@]} -eq 0 ]; then
echo ""
echo "🎉 没有发现需要清理的跟踪文件"
echo "✅ .gitignore规则应该已经生效"
exit 0
fi
# 询问用户是否继续
echo ""
echo "⚠️ 将要执行以下操作:"
if [ "$BUILD_TRACKED" = true ]; then
echo " - 移除build/目录的Git跟踪"
fi
if [ "$DIST_TRACKED" = true ]; then
echo " - 移除dist/目录的Git跟踪"
fi
for file in "${FOUND_FILES[@]}"; do
echo " - 移除$file文件的Git跟踪"
done
echo ""
echo "注意: 这只会移除Git跟踪不会删除本地文件"
echo ""
read -p "是否继续? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "❌ 操作已取消"
exit 1
fi
echo ""
echo "🔄 开始清理Git跟踪..."
# 移除build目录跟踪
if [ "$BUILD_TRACKED" = true ]; then
echo "📁 移除build目录跟踪..."
git rm -r --cached build/ || echo "⚠️ build目录移除失败可能已经移除"
fi
# 移除dist目录跟踪
if [ "$DIST_TRACKED" = true ]; then
echo "📁 移除dist目录跟踪..."
git rm -r --cached dist/ || echo "⚠️ dist目录移除失败可能已经移除"
fi
# 移除其他文件跟踪
for pattern in "${FOUND_FILES[@]}"; do
echo "📄 移除$pattern文件跟踪..."
git rm --cached -r "$pattern" 2>/dev/null || echo "⚠️ $pattern移除失败(可能已经移除)"
done
# 显示更改后的状态
echo ""
echo "📊 清理后的Git状态:"
git status --short
# 询问是否提交更改
echo ""
read -p "是否提交这些更改? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "💾 提交更改..."
git add .gitignore
git commit -m "Remove build/, dist/ and other generated files from Git tracking
- Remove build/ directory from tracking
- Remove dist/ directory from tracking
- Update .gitignore to prevent future tracking
- These directories contain generated/compiled files that should not be versioned"
echo ""
echo "✅ 更改已提交"
echo ""
read -p "是否推送到远程仓库? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "🚀 推送到远程..."
git push
echo "✅ 已推送到远程仓库"
else
echo "⚠️ 记得稍后推送: git push"
fi
else
echo "⚠️ 更改未提交,记得稍后提交:"
echo " git add .gitignore"
echo " git commit -m 'Remove generated files from Git tracking'"
echo " git push"
fi
echo ""
echo "🎉 清理完成!"
echo ""
echo "💡 说明:"
echo "- build/和dist/目录现在不会被Git跟踪"
echo "- 本地文件仍然存在,只是不再被版本控制"
echo "- 远程仓库在下次推送后会移除这些文件"
echo "- 其他开发者拉取更新后这些目录也会从他们的Git跟踪中移除"