NLDClient-yudde/Tool/dll_tools/push_to_git.sh

244 lines
7.4 KiB
Bash

#!/bin/bash
# 自动把dll上传到git
# 获取脚本所在的绝对路径
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# 获取dll所在绝对路径
SRC_DLL_PATH="$SCRIPT_DIR/../../WaitUpload"
# 目标路径
TARGET_PATH="/Users/mayuanzheng/Documents/NoRemove/GamePlay_Dll"
# 定义函数用于清理 Git 锁文件
clean_git_locks() {
local repo_path="$1"
echo "Checking for Git lock files in $repo_path..."
# 清理索引锁文件
if [ -f "$repo_path/.git/index.lock" ]; then
echo "Found .git/index.lock, removing it..."
rm -f "$repo_path/.git/index.lock"
fi
# 清理其他可能的锁文件
if [ -f "$repo_path/.git/HEAD.lock" ]; then
echo "Found .git/HEAD.lock, removing it..."
rm -f "$repo_path/.git/HEAD.lock"
fi
# 检查 refs 目录下的锁文件
find "$repo_path/.git/refs" -name "*.lock" -type f 2>/dev/null | while read lockfile; do
echo "Found lock file: $lockfile, removing it..."
rm -f "$lockfile"
done
}
# 第一部分: 更新 DLL 到 GamePlay_Dll 仓库
# ----------------------------------------
# 检查源路径和目标路径是否存在
if [ ! -d "$SRC_DLL_PATH" ]; then
echo "Source path does not exist: $SRC_DLL_PATH"
exit 1
fi
if [ ! -d "$TARGET_PATH" ]; then
echo "Target path does not exist: $TARGET_PATH"
exit 1
fi
# 先cd到目标路径,还原本地修改并更新到最新
cd "$TARGET_PATH"
if [ $? -ne 0 ]; then
echo "Failed to change directory to $TARGET_PATH"
exit 1
fi
# 清理可能的锁文件
clean_git_locks "$TARGET_PATH"
# 还原本地修改并更新到最新
git reset --hard
git pull
# 拷贝所有dll到目标路径并提交
cp -rf "$SRC_DLL_PATH"/*.dll "$TARGET_PATH"
if [ $? -ne 0 ]; then
echo "Failed to copy dll files"
exit 1
fi
echo "Dll files copied successfully to $TARGET_PATH"
git add .
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
git commit -m "Auto upload dll - $TIMESTAMP"
git push
if [ $? -ne 0 ]; then
echo "Failed to push to git"
exit 1
fi
# 获取最新提交的哈希值
COMMIT_HASH=$(git rev-parse --short HEAD)
echo "Successfully pushed to commit: $COMMIT_HASH"
echo "Dll files pushed to pure git successfully"
# 第二部分: 更新主项目的子模块
# ----------------------------------------
PROJECT_PATH="/Users/mayuanzheng/Documents/NoRemove/NLDClient"
PRIMARY_SUBMODULE_PATH="ProjectNLD/Assets/Code/Scripts/GamePlay_Dll"
# 检查工程路径是否存在
if [ ! -d "$PROJECT_PATH" ]; then
echo "Project path does not exist: $PROJECT_PATH"
exit 1
fi
# 清理主项目仓库中的 Git 锁文件
clean_git_locks "$PROJECT_PATH"
# 切换到工程目录
cd "$PROJECT_PATH"
if [ $? -ne 0 ]; then
echo "Failed to change directory to $PROJECT_PATH"
exit 1
fi
echo "=== 步骤 1: 更新主项目到最新 ==="
# 获取远程最新变更
git fetch
# 获取当前分支
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "Current branch: $CURRENT_BRANCH"
# 使用 -X theirs 策略更新主项目,自动使用远端版本解决冲突
echo "Updating main project to latest version..."
git pull -X theirs
if [ $? -ne 0 ]; then
echo "Warning: Failed to pull with strategy theirs, attempting hard reset..."
clean_git_locks "$(pwd)"
git reset --hard origin/$CURRENT_BRANCH
if [ $? -ne 0 ]; then
echo "Warning: Failed to reset to remote version, continuing anyway..."
fi
fi
echo "=== 步骤 2: 更新所有子模块 ==="
# 先更新所有子模块到最新
echo "Updating all submodules to latest version..."
git submodule update --init --recursive
if [ $? -ne 0 ]; then
echo "Warning: Failed to update all submodules, attempting to continue..."
# 清理可能的锁文件
git submodule foreach 'if [ -d ".git" ]; then if [ -f ".git/index.lock" ]; then rm -f ".git/index.lock"; fi; fi'
# 再次尝试更新子模块
git submodule update --init --recursive
if [ $? -ne 0 ]; then
echo "Warning: Failed to update submodules again, continuing anyway..."
fi
fi
echo "=== 步骤 3: 更新指定子模块到特定版本 ==="
# 检查主要子模块路径是否存在
if [ ! -d "$PRIMARY_SUBMODULE_PATH" ]; then
echo "Primary submodule path does not exist: $PRIMARY_SUBMODULE_PATH"
exit 1
fi
# 清理子模块锁文件
clean_git_locks "$PROJECT_PATH/$PRIMARY_SUBMODULE_PATH"
# 进入子模块目录
cd "$PRIMARY_SUBMODULE_PATH"
if [ $? -ne 0 ]; then
echo "Failed to change directory to submodule: $PRIMARY_SUBMODULE_PATH"
exit 1
fi
echo "Updating submodule '$PRIMARY_SUBMODULE_PATH' to commit: $COMMIT_HASH"
# 获取子模块的远程仓库 URL
SUBMODULE_REPO_URL=$(cd "$PROJECT_PATH" && git config -f .gitmodules --get "submodule.$PRIMARY_SUBMODULE_PATH.url")
if [ -z "$SUBMODULE_REPO_URL" ]; then
echo "Warning: Failed to get URL from .gitmodules, using default URL"
SUBMODULE_REPO_URL="http://1.14.122.170:3000/PHXH/GamePlay_Dll.git"
fi
echo "Submodule repository URL: $SUBMODULE_REPO_URL"
# 确保远程仓库配置正确
if ! git remote | grep -q "^origin$"; then
git remote add origin "$SUBMODULE_REPO_URL"
echo "Added origin remote to submodule"
else
# 更新远程仓库 URL 如果不匹配
CURRENT_URL=$(git remote get-url origin)
if [ "$CURRENT_URL" != "$SUBMODULE_REPO_URL" ]; then
git remote set-url origin "$SUBMODULE_REPO_URL"
echo "Updated origin remote URL"
fi
fi
# 获取所有远程分支和标签
echo "Fetching from all remotes..."
git fetch --all --tags --prune
# 检查提交是否存在
echo "Checking if commit $COMMIT_HASH exists..."
if ! git cat-file -e "$COMMIT_HASH" 2>/dev/null; then
echo "Warning: Commit hash not found as full reference, trying as short hash..."
# 尝试查找短哈希匹配的完整提交
FULL_HASH=$(git log --all --pretty=format:"%H %h" | grep -E " $COMMIT_HASH$" | awk '{print $1}')
if [ -n "$FULL_HASH" ]; then
echo "Found full hash: $FULL_HASH for short hash: $COMMIT_HASH"
COMMIT_HASH=$FULL_HASH
else
echo "Error: Could not find commit $COMMIT_HASH"
echo "Available recent commits:"
git log --oneline -n 10
exit 1
fi
fi
# 切换到指定提交
echo "Checking out commit: $COMMIT_HASH"
git checkout "$COMMIT_HASH"
if [ $? -ne 0 ]; then
echo "Failed to checkout commit: $COMMIT_HASH"
exit 1
fi
echo "Successfully updated submodule to commit: $COMMIT_HASH"
echo "=== 步骤 4: 提交并推送更改 ==="
# 回到项目根目录
cd "$PROJECT_PATH"
clean_git_locks "$PROJECT_PATH"
# 将子模块的更改添加到主项目
git add "$PRIMARY_SUBMODULE_PATH"
git commit -m "Update GamePlay_Dll submodule to commit: $COMMIT_HASH"
# 推送更改到远程仓库
echo "Pushing changes to remote repository..."
git push
if [ $? -ne 0 ]; then
echo "Warning: Push failed, trying to resolve conflicts..."
# 再次获取远程更改
git fetch
# 尝试重新拉取,使用 -X theirs 策略
git pull -X theirs
# 重新应用子模块更改
cd "$PRIMARY_SUBMODULE_PATH"
git checkout "$COMMIT_HASH"
cd "$PROJECT_PATH"
git add "$PRIMARY_SUBMODULE_PATH"
git commit -m "Update GamePlay_Dll submodule to commit: $COMMIT_HASH (retry)"
# 再次推送
git push
if [ $? -ne 0 ]; then
echo "Failed to push changes even after resolving conflicts"
exit 1
fi
fi
echo "=== 完成 ==="
echo "Successfully updated GamePlay_Dll submodule to commit $COMMIT_HASH and pushed to remote"