Files
image-workbench-desktop/scripts/sync-upstream.sh
T

102 lines
4.8 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.
#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────
# 从上游 image-workbench-web 同步 bug 修复到本仓库(桌面版)
#
# 为什么不用 git merge:
# 本仓库 fork 时只导出了文件(git archive),**与上游不共享提交历史**,
# 没有共同祖先 ⇒ git merge 会退化成「两棵无关的树互相覆盖」。
# 所以这里用「差异重放」:
# 1) 算本仓库相对基线 tag(upstream-base) 的全部差异 → 一个补丁
# 2) 以新的上游提交为起点开分支,用三段合并(--3way)把补丁贴上去
# 3) 上游没碰过的行自动合并;上游改过同一行的地方留冲突标记,人工看一眼
#
# 用法:
# ./scripts/sync-upstream.sh # 只看上游有哪些新提交,不改任何东西
# ./scripts/sync-upstream.sh --apply # 真同步,产出 sync/<日期> 分支(不动主线)
# ./scripts/sync-upstream.sh --finish # 人工确认无误后:并入 main 并把基线前移
# ─────────────────────────────────────────────────────────────
set -euo pipefail
REMOTE="${UPSTREAM_REMOTE:-upstream}" # 上游 remote 名(默认 upstream)
BRANCH="${UPSTREAM_BRANCH:-main}" # 上游分支
BASE_TAG="upstream-base" # 本仓库内的基线提交(算桌面侧差异用)
PATCH=".git/desktop-delta.patch" # 桌面侧差异补丁(不进版本库)
MODE="${1:-check}"
cd "$(git rev-parse --show-toplevel)" # 一律在仓库根执行
# 上游基线的 commit 号存在文件里:因为我们和上游没有共同历史,
# 不能用 rev-list 直接数(会把上游全部历史都算成“新增”)。
[ -f .upstream-base ] || { echo "缺少 .upstream-base(记录上游基线 commit)"; exit 1; }
UP_BASE="$(tr -d '[:space:]' < .upstream-base)"
echo "拉取上游 $REMOTE/$BRANCH ..."
git fetch --quiet "$REMOTE" "$BRANCH"
NEW="$(git rev-parse "$REMOTE/$BRANCH")"
echo "上游基线:$UP_BASE"
echo "上游最新:$(git log -1 --format='%h %s' "$NEW")"
if [ "$UP_BASE" = "$NEW" ]; then
echo "已是最新,无需同步"; exit 0
fi
COUNT="$(git rev-list --count "$UP_BASE..$NEW" 2>/dev/null || echo '?')"
echo "新增提交:$COUNT 个"
echo "---- 新提交(最多 50 条)----"
git log --oneline -n 50 "$UP_BASE..$NEW"
case "$MODE" in
check|"")
echo ""
echo "(只读检查,未做任何修改。确认后加 --apply 执行同步)"
exit 0
;;
--apply|apply)
# 1) 桌面侧差异(--binary 必须加:图标等二进制文件否则无法重放)
git diff --binary "$BASE_TAG..HEAD" > "$PATCH"
echo ""
echo "桌面侧差异补丁:$(wc -c < "$PATCH") 字节"
# 2) 以新上游为起点开分支
STAMP="$(date +%Y%m%d-%H%M)"
git checkout -q -b "sync/$STAMP" "$NEW"
# 3) 三段合并重放;有冲突也继续,交给人工
set +e
git apply --3way --whitespace=nowarn "$PATCH"
RC=$?
set -e
if [ "$RC" != "0" ]; then
echo ""
echo "!! 有冲突,需手工处理:"
git status --porcelain | grep -E '^(UU|AA|DU|UD|AU|UA)' || true
echo " 处理完:git add -A && git commit,再执行 $0 --finish"
else
git add -A
git -c user.name=qiqi -c user.email=qiqi@local commit -q \
-m "chore(sync): 重放桌面侧差异到上游 $(git rev-parse --short "$NEW")"
echo ""
echo "同步完成,改动已提交在 sync/$STAMP。核对后执行:$0 --finish"
fi
exit 0
;;
--finish|finish)
CUR="$(git rev-parse --abbrev-ref HEAD)"
case "$CUR" in sync/*) ;; *) echo "请先切到 sync/* 分支再 --finish"; exit 1;; esac
if [ -n "$(git diff --name-only --diff-filter=U)" ]; then
echo "还有未解决的冲突,先处理干净"; exit 1
fi
if grep -rqI '^<<<<<<< ' --exclude-dir=node_modules --exclude-dir=.git . ; then
echo "仍有冲突标记未清理"; exit 1
fi
TARGET="${TARGET_BRANCH:-main}"
git checkout -q "$TARGET"
git -c user.name=qiqi -c user.email=qiqi@local merge --no-ff -m "chore(sync): 合入 $CUR(上游 $(git rev-parse --short "$REMOTE/$BRANCH"))" "$CUR"
git tag -f "$BASE_TAG" HEAD~0 >/dev/null # 基线标记前移到本次合并提交
git rev-parse "$REMOTE/$BRANCH" | tr -d '\n' > .upstream-base
git add .upstream-base
git -c user.name=qiqi -c user.email=qiqi@local commit -q -m "chore(sync): 上游基线推进到 $(git rev-parse --short "$REMOTE/$BRANCH")"
echo "已并入 $TARGET,基线前移到 $(cat .upstream-base)"
;;
*)
echo "未知参数:$MODE(可用:--apply / --finish)"; exit 1
;;
esac