更新工具

main
刘涛 2025-04-30 18:06:20 +08:00
parent b77acaeef3
commit effb977613
1 changed files with 120 additions and 118 deletions

View File

@ -1,11 +1,5 @@
#!/bin/bash
# 自动把dll上传到git
# 当前目录在C:/Work/Projects/NLDClient/Tool/dll_tools
# 当前存档dll路径C:/Work/Projects/NLDClient/WaitUpload
# 找到绝对路径/Users/mayuanzheng/Documents/NoRemove/GamePlay_Dll
# 复制所有dll到绝对路径中并覆盖
# 使用git强制提交
# 获取脚本所在的绝对路径
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# 获取dll所在绝对路径
@ -37,12 +31,15 @@ clean_git_locks() {
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
@ -50,59 +47,44 @@ 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到目标路径
# 拷贝所有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"
# 进入目标路径
cd "$TARGET_PATH"
# 检查是否进入成功
if [ $? -ne 0 ]; then
echo "Failed to change directory to $TARGET_PATH"
exit 1
fi
# 强制提交
git add .
# 添加时间戳到提交信息
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
git commit -m "Auto upload dll - $TIMESTAMP"
git push --force
# 检查提交是否成功
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"
# 把指定工程的指定submodule更新到COMMIT_HASH
# 工程 /Users/mayuanzheng/Documents/NoRemove/NLDClient
# 主要子模块路径 ProjectNLD/Assets/Code/Scripts/GamePlay_Dll
# 第二部分: 更新主项目的子模块
# ----------------------------------------
PROJECT_PATH="/Users/mayuanzheng/Documents/NoRemove/NLDClient"
# 要更新的主要子模块路径
PRIMARY_SUBMODULE_PATH="ProjectNLD/Assets/Code/Scripts/GamePlay_Dll"
# 检查工程路径是否存在
@ -111,7 +93,7 @@ if [ ! -d "$PROJECT_PATH" ]; then
exit 1
fi
# 清理项目仓库中的 Git 锁文件
# 清理项目仓库中的 Git 锁文件
clean_git_locks "$PROJECT_PATH"
# 切换到工程目录
@ -121,36 +103,48 @@ if [ $? -ne 0 ]; then
exit 1
fi
# 先拉取主项目的最新变更,如果有冲突自动使用远端版本
echo "Pulling latest changes from remote repository..."
# 首先获取远程分支信息
echo "=== 步骤 1: 更新主项目到最新 ==="
# 获取远程最新变更
git fetch
# 获取当前分支名称
# 获取当前分支
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# 使用 --strategy=theirs 参数确保冲突时使用远端版本
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)"
# 如果 pull 失败,则执行强制重置为远端版本
git reset --hard origin/$CURRENT_BRANCH
if [ $? -ne 0 ]; then
echo "Warning: Failed to reset to remote version, continuing anyway..."
# 不退出,尝试继续执行
fi
fi
# 更新主要的子模块到指定提交
echo "Updating primary submodule to commit: $COMMIT_HASH"
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"
# 进入子模块目录
@ -160,83 +154,91 @@ if [ $? -ne 0 ]; then
exit 1
fi
# 更新子模块到指定提交
git fetch
git checkout "$COMMIT_HASH"
if [ $? -ne 0 ]; then
echo "Failed to update submodule to commit: $COMMIT_HASH"
exit 1
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
# 回到项目根目录
cd "$PROJECT_PATH"
clean_git_locks "$PROJECT_PATH"
# 获取所有远程分支和标签
echo "Fetching from all remotes..."
git fetch --all --tags --prune
# 提交主要子模块更新
git add "$PRIMARY_SUBMODULE_PATH"
# 检查是否有其他子模块需要处理
echo "Checking for other submodules..."
# 获取所有子模块路径
SUBMODULES=$(git config --file .gitmodules --get-regexp path | awk '{ print $2 }')
# 遍历所有子模块,确保它们的状态也被记录
for SUBMODULE in $SUBMODULES; do
# 跳过已处理的主要子模块
if [ "$SUBMODULE" != "$PRIMARY_SUBMODULE_PATH" ]; then
echo "Adding changes for submodule: $SUBMODULE"
# 添加子模块的变更到暂存区
git add "$SUBMODULE"
fi
done
# 提交所有子模块的变更
git commit -m "Update GamePlay_Dll submodule to commit: $COMMIT_HASH and sync other submodules"
if [ $? -ne 0 ]; then
echo "Failed to commit submodule updates"
exit 1
fi
# 尝试推送更改到远程仓库,如果有冲突则使用远端版本并再次推送
echo "Pushing changes to remote repository..."
# 尝试推送
git push
# 如果推送失败,尝试使用远端版本
if [ $? -ne 0 ]; then
echo "Push failed, trying to resolve by using remote version..."
# 获取当前分支名称
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# 强制获取远程版本
git fetch
# 使用 -X theirs 策略拉取,自动选择远端版本解决冲突
git pull -X theirs
# 如果仍然失败,尝试强制重置
if [ $? -ne 0 ]; then
echo "Pull with strategy failed, resetting to remote version..."
git reset --hard origin/$CURRENT_BRANCH
fi
# 再次应用主要子模块更新
git add "$PRIMARY_SUBMODULE_PATH"
# 再次添加其他子模块的变更
for SUBMODULE in $SUBMODULES; do
# 跳过已处理的主要子模块
if [ "$SUBMODULE" != "$PRIMARY_SUBMODULE_PATH" ]; then
echo "Re-adding changes for submodule: $SUBMODULE"
git add "$SUBMODULE"
fi
done
# 重新提交所有子模块的变更
git commit -m "Update GamePlay_Dll submodule to commit: $COMMIT_HASH and sync other submodules"
# 再次推送
git push
if [ $? -ne 0 ]; then
echo "Failed to push even after resolving conflicts"
# 检查提交是否存在
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 "Submodule successfully updated to commit: $COMMIT_HASH and pushed to remote"
# 切换到指定提交
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"