master
cody 3 months ago
parent 98ce786f08
commit b24b0b7f8b

@ -122,6 +122,80 @@ execute_git_operations() {
done
fi
# 执行 git pull 的函数
do_git_pull() {
local password="$1"
local remote="$2"
local branch="$3"
local temp_output=$(mktemp)
# 使用 tee 同时输出到终端和文件,以便后续检查错误
expect << EOF 2>&1 | tee "$temp_output"
set timeout 30
spawn git pull $remote $branch
expect {
"Password:" {
send "$password\r"
exp_continue
}
"password:" {
send "$password\r"
exp_continue
}
"passphrase:" {
send "$password\r"
exp_continue
}
"Username:" {
exp_continue
}
"Authentication failed" {
exp_continue
}
"Permission denied" {
exp_continue
}
"Merge conflict" {
exp_continue
}
"CONFLICT" {
exp_continue
}
eof {
catch wait result
set exit_code [lindex \$result 3]
exit \$exit_code
}
timeout {
exit 1
}
}
EOF
local exit_code=$?
local output=$(cat "$temp_output" 2>/dev/null)
rm -f "$temp_output"
# 检查退出码和输出中的错误信息
if [ $exit_code -ne 0 ]; then
return 1
fi
# 检查是否有合并冲突
if echo "$output" | grep -qiE "(CONFLICT|merge conflict|Automatic merge failed)"; then
echo ""
echo "⚠️ 检测到合并冲突,请手动解决冲突后再执行脚本"
return 2
fi
# 检查输出中是否包含认证失败相关的错误
if echo "$output" | grep -qiE "(Authentication failed|Permission denied|fatal:.*authentication|error:.*authentication)"; then
return 1
fi
return 0
}
# 执行 git push 的函数
do_git_push() {
local password="$1"
@ -190,31 +264,117 @@ EOF
return 0
}
# 首先尝试使用默认密码
# 先执行 pull再执行 push
echo ""
echo "=========================================="
echo "步骤 1: 执行 git pull"
echo "=========================================="
echo ""
echo "执行: git push $selected_remote $current_branch (使用默认密码)"
if do_git_push "$PASSWORD" "$selected_remote" "$current_branch"; then
# 使用局部变量保存当前使用的密码
local current_password="$PASSWORD"
local pull_success=0
# 首先尝试使用默认密码 pull
echo "尝试使用默认密码执行: git pull $selected_remote $current_branch"
if do_git_pull "$current_password" "$selected_remote" "$current_branch"; then
pull_success=1
echo ""
echo "✅ 仓库 $repo_path 提交完成!"
echo "✅ Pull 成功(使用默认密码)"
else
# 默认密码失败,提示用户输入密码
echo ""
echo "⚠️ 默认密码 push 失败,请手动输入密码"
echo ""
read -sp "请输入 Git 密码: " user_password
echo ""
echo ""
echo "使用您输入的密码重新执行: git push $selected_remote $current_branch"
local pull_result=$?
if do_git_push "$user_password" "$selected_remote" "$current_branch"; then
# 如果是认证失败,提示用户输入密码
if [ $pull_result -eq 1 ]; then
echo ""
echo "⚠️ 默认密码 pull 失败(可能是密码错误),请手动输入密码"
echo ""
read -sp "请输入 Git 密码: " user_password
echo ""
echo ""
echo "使用您输入的密码重新执行: git pull $selected_remote $current_branch"
if do_git_pull "$user_password" "$selected_remote" "$current_branch"; then
pull_success=1
current_password="$user_password"
echo ""
echo "✅ Pull 成功(使用手动输入的密码)"
else
pull_result=$?
if [ $pull_result -eq 2 ]; then
# 合并冲突
echo ""
echo "❌ Pull 失败:存在合并冲突,请手动解决后重试"
return 1
else
echo ""
echo "❌ Pull 失败,请检查密码和网络连接"
return 1
fi
fi
elif [ $pull_result -eq 2 ]; then
# 合并冲突
echo ""
echo "✅ 仓库 $repo_path 提交完成!"
echo "❌ Pull 失败:存在合并冲突,请手动解决后重试"
return 1
else
echo ""
echo "❌ 仓库 $repo_path push 失败,请检查密码和网络连接"
echo "❌ Pull 失败,请检查网络连接"
return 1
fi
fi
# Pull 成功后执行 push
echo ""
echo "=========================================="
echo "步骤 2: 执行 git push"
echo "=========================================="
echo ""
echo "执行: git push $selected_remote $current_branch"
local push_success=0
if do_git_push "$current_password" "$selected_remote" "$current_branch"; then
push_success=1
else
# push 失败,如果之前使用的是默认密码,尝试让用户输入
if [ "$current_password" = "Git@2018" ]; then
echo ""
echo "⚠️ 默认密码 push 失败,请手动输入密码"
echo ""
read -sp "请输入 Git 密码: " user_password
echo ""
echo ""
echo "使用您输入的密码重新执行: git push $selected_remote $current_branch"
if do_git_push "$user_password" "$selected_remote" "$current_branch"; then
push_success=1
fi
fi
fi
# 显示最终结果
echo ""
echo "=========================================="
echo "执行结果"
echo "=========================================="
if [ $pull_success -eq 1 ] && [ $push_success -eq 1 ]; then
echo "✅ Pull: 成功"
echo "✅ Push: 成功"
echo ""
echo "✅ 仓库 $repo_path 所有操作完成!"
elif [ $pull_success -eq 1 ] && [ $push_success -eq 0 ]; then
echo "✅ Pull: 成功"
echo "❌ Push: 失败"
echo ""
echo "⚠️ 仓库 $repo_path Pull 成功,但 Push 失败,请检查密码和网络连接"
else
echo "❌ Pull: 失败"
echo "❌ Push: 未执行"
echo ""
echo "❌ 仓库 $repo_path Pull 失败Push 未执行"
fi
echo "=========================================="
echo ""
}

Loading…
Cancel
Save