-
-
Notifications
You must be signed in to change notification settings - Fork 0
chore(scripts): safe-worktree.sh — 防 stale baseline 白工守門 #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # safe-worktree.sh — 防 stale baseline 白工的 worktree 建立守門。 | ||
| # | ||
| # 動機:多次出現「從本地過期同名分支開 worktree → 工作早已在 origin 收編 → 整支 PR 白工/回退」 | ||
| # 的事故(例:被取代的 cluster PR)。本腳本一律以 origin/<base> 的最新 tip 為基準建立 worktree, | ||
| # 使過期的本地分支在物理上無法成為起點。 | ||
| # | ||
| # 用法: | ||
| # scripts/safe-worktree.sh <new-branch> <worktree-path> [base-branch] | ||
| # | ||
| # 範例: | ||
| # scripts/safe-worktree.sh fix/foo ../app-foo main | ||
| # scripts/safe-worktree.sh converge/bar ../app-bar chore/ratewise-production-governance-v2 | ||
| # | ||
| set -euo pipefail | ||
|
|
||
| NEW_BRANCH="${1:?用法:safe-worktree.sh <new-branch> <worktree-path> [base-branch]}" | ||
| WORKTREE_PATH="${2:?需要 worktree 路徑}" | ||
| BASE="${3:-main}" | ||
| REMOTE_REF="origin/${BASE}" | ||
|
|
||
| echo "🔄 git fetch --all --prune ..." | ||
| git fetch --all --prune | ||
|
|
||
| if ! git rev-parse --verify --quiet "${REMOTE_REF}" >/dev/null; then | ||
| echo "❌ 找不到遠端分支 ${REMOTE_REF},請確認 base 名稱。" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| REMOTE_SHA="$(git rev-parse "${REMOTE_REF}")" | ||
|
|
||
| # 若本地存在同名分支,檢查是否落後 origin(落後=stale,提醒但仍以 origin 為準)。 | ||
| if git rev-parse --verify --quiet "refs/heads/${BASE}" >/dev/null; then | ||
| LOCAL_SHA="$(git rev-parse "${BASE}")" | ||
| if [ "${LOCAL_SHA}" != "${REMOTE_SHA}" ]; then | ||
| AHEAD="$(git rev-list --count "${REMOTE_REF}..${BASE}")" | ||
| BEHIND="$(git rev-list --count "${BASE}..${REMOTE_REF}")" | ||
| echo "⚠️ 本地 ${BASE} (${LOCAL_SHA:0:8}) 與 ${REMOTE_REF} (${REMOTE_SHA:0:8}) 不一致:ahead ${AHEAD} / behind ${BEHIND}" >&2 | ||
| echo " 一律以 ${REMOTE_REF} 最新 tip 為基準,避免 stale baseline 白工。" >&2 | ||
| fi | ||
| fi | ||
|
|
||
| echo "🌱 git worktree add -b ${NEW_BRANCH} ${WORKTREE_PATH} ${REMOTE_REF}(@ ${REMOTE_SHA:0:8})" | ||
| git worktree add -b "${NEW_BRANCH}" "${WORKTREE_PATH}" "${REMOTE_SHA}" | ||
| echo "✅ worktree 已從 ${REMOTE_REF} @ ${REMOTE_SHA:0:8} 建立:${WORKTREE_PATH}" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.