xiandie/push_audio.sh
2025-06-17 18:27:44 +08:00

106 lines
4.0 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
# ===================================================================
# push_audio.sh
# 仅提交并推送 *.tscn 音效改动到 audio 分支,其它改动会被永久丢弃!
# ===================================================================
# ⚠️ 警告:脚本执行后,非 *.tscn 的本地修改将被
# ⚠️ git reset --hard + git clean -fd
# ⚠️ 彻底删除且无法找回,请确认没有要保留的改动!
# -------------------------------------------------------------------
set -euo pipefail
IFS=$'\n\t'
REMOTE=origin
BRANCH=audio
## -------------------------------------------------------------
color() { printf '\033[%sm%s\033[0m\n' "$1" "$2"; }
info() { color "1;34" "👉 $*"; } # 蓝
warn() { color "1;33" "⚠️ $*"; } # 黄
die() { color "1;31" "$*"; exit 1; } # 红
## -------------------------------------------------------------
# 0. 检测 Git、操作系统、确认危险操作
## -------------------------------------------------------------
command -v git >/dev/null 2>&1 || die "未安装 Git"
git_version=$(git --version | awk '{print $3}')
info "Git 版本:$git_version"
warn "脚本将丢弃所有非 *.tscn 改动5 秒内 Ctrl-C 可取消"
sleep 5
## -------------------------------------------------------------
# 1. 认证检查HTTP/S 及 SSH 两种)
## -------------------------------------------------------------
info "检查 Git 认证状态..."
remote_url=$(git remote get-url "$REMOTE" 2>/dev/null) \
|| die "远程 $REMOTE 不存在,请先 git remote add"
if [[ "$remote_url" =~ ^https?:// ]]; then
if ! git ls-remote "$REMOTE" &>/dev/null; then
die "HTTP(S) 鉴权失败,请先完成登录或配置 token 再执行脚本"
fi
info "HTTP(S) 鉴权通过"
else
# SSH检测是否有可用密钥
if ! ssh -Tq "$(echo "$remote_url" | sed 's#.*@##;s/:.*##')" 2>&1 \
| grep -qi "successfully authenticated"; then
warn "SSH 连接测试结果不确定,若后续 push 失败请检查 SSH key"
fi
fi
## -------------------------------------------------------------
# 2. 切换到 audio 分支
## -------------------------------------------------------------
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$current_branch" != "$BRANCH" ]]; then
info "切换分支 $current_branch$BRANCH ..."
git switch "$BRANCH" 2>/dev/null || git checkout "$BRANCH" \
|| die "切换失败,请确认分支 $BRANCH 存在"
fi
## -------------------------------------------------------------
# 3. 仅暂存 / 提交 *.tscn 改动
## -------------------------------------------------------------
info "检测 *.tscn 改动..."
# 取消任何已暂存的内容,保持工作区状态不变
git reset --quiet
# 标记并暂存 tscn 文件(包括新增)
git add -u -- '*.tscn' 2>/dev/null || true
git add --intent-to-add -- '*.tscn' 2>/dev/null || true
git add -- '*.tscn' 2>/dev/null || true
if git diff --cached --quiet; then
info "没有需要提交的 tscn 改动"
else
msg="调整音效 dB $(date '+%F %H:%M')"
git commit -m "$msg"
info "已提交:$msg"
fi
## -------------------------------------------------------------
# 4. 丢弃其它改动
## -------------------------------------------------------------
info "丢弃非 *.tscn 的本地改动..."
git reset --hard # 回到 HEAD
git clean -fdq # 删除未跟踪文件/目录
## -------------------------------------------------------------
# 5. 拉取远程最新并 rebase
## -------------------------------------------------------------
info "拉取远程最新并 rebase..."
if ! git pull --rebase "$REMOTE" "$BRANCH"; then
die "rebase 冲突,请手动解决完毕后运行:\n git rebase --continue\n再重新执行脚本"
fi
## -------------------------------------------------------------
# 6. 推送
## -------------------------------------------------------------
info "推送到 $REMOTE/$BRANCH ..."
git push "$REMOTE" "$BRANCH" || die "推送失败,请检查网络/权限"
color "1;32" "✅ tscn 改动已成功推送,其余改动已被抛弃"