✨ feat(vscode): 手动删除 worktree 时连同 feature 分支一起删(本地+远程)
抽出共享 cleanupFeatureBranch(handlers/branchCleanup.ts):受保护分支跳过、 远程走 gitea API、本地强删(git branch -D),全程 best-effort 只记日志。 「拖到完成」的分支清理改为复用它(71 行 → 12 行);handleDeleteWorktree 在 删完 worktree 目录后同样调用它,确认框与完成提示带上分支名。 放在 handlers 层而非 git/branchSync.ts:后者刻意不依赖 vscode(有单测覆盖 deleteLocalBranch),本函数需 logger(→vscode)+ gitea API。
This commit is contained in:
1 parent
045e203590
commit
f39e61aa7f
3 files changed
+108
-71
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import { deleteLocalBranch } from '../../git/branchSync'
|
||||
import { deleteBranch } from '../../gitea/api'
|
||||
import { logger } from '../../logging/logger'
|
||||
|
||||
/**
|
||||
* 清理一个 feature 分支(远程 + 本地),供「拖到完成」和「手动删除 worktree」两处共用。
|
||||
*
|
||||
* - 受保护分支(main/master/devBranch/autoBuildBranch)直接跳过,绝不删。
|
||||
* - 远程删除走 gitea API,需 remote + token;缺任一则只删本地,不阻断。
|
||||
* - 本地删除是强删(deleteLocalBranch 内部 `git branch -D`):调用方语义是「连分支一起删」,
|
||||
* 未合并也照删。
|
||||
* - 全程 best-effort:远程 / 本地各自 try/catch,失败只记日志——分支清理从不回滚已完成的主流程
|
||||
* (worktree 已删、PR 已合并)。branch 为空直接返回。
|
||||
*
|
||||
* 放在 handlers 层而非 `git/branchSync.ts`:后者刻意不依赖 `vscode`(有单测覆盖
|
||||
* deleteLocalBranch),而本函数要用 logger(→ vscode)和 gitea API。
|
||||
*/
|
||||
export async function cleanupFeatureBranch(opts: {
|
||||
workspaceRoot: string
|
||||
branch: string
|
||||
issueNumber: number
|
||||
devBranch?: string
|
||||
autoBuildBranch?: string
|
||||
/** 缺 remote 或 token 时跳过远程删除,只删本地。 */
|
||||
remote?: { host: string, owner: string, repo: string }
|
||||
token?: string
|
||||
}): Promise<void> {
|
||||
const { workspaceRoot, branch, issueNumber, devBranch, autoBuildBranch, remote, token } = opts
|
||||
if (!branch)
|
||||
return
|
||||
|
||||
const protectedBranches = new Set(
|
||||
['main', 'master', devBranch, autoBuildBranch].filter((b): b is string => Boolean(b)),
|
||||
)
|
||||
if (protectedBranches.has(branch)) {
|
||||
logger.add({ level: 'warn', source: 'panel', message: `跳过受保护分支清理 ${branch} (issue #${issueNumber})` })
|
||||
return
|
||||
}
|
||||
|
||||
if (remote && token) {
|
||||
try {
|
||||
await deleteBranch({ host: remote.host, token, owner: remote.owner, repo: remote.repo, branch })
|
||||
logger.add({ level: 'info', source: 'panel', message: `已删除远程 feature 分支 ${branch} (issue #${issueNumber})` })
|
||||
}
|
||||
catch (err) {
|
||||
logger.add({
|
||||
level: 'warn',
|
||||
source: 'panel',
|
||||
message: `删除远程 feature 分支失败 ${branch} (issue #${issueNumber})`,
|
||||
details: err instanceof Error ? err.message : String(err),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const deleted = await deleteLocalBranch(workspaceRoot, branch)
|
||||
logger.add({
|
||||
level: deleted.ok ? 'info' : 'warn',
|
||||
source: 'panel',
|
||||
message: `${deleted.ok ? '已删除' : '删除失败'}本地 feature 分支 ${branch} (issue #${issueNumber})`,
|
||||
details: deleted.stderr || deleted.stdout,
|
||||
})
|
||||
}
|
||||
catch (err) {
|
||||
logger.add({
|
||||
level: 'warn',
|
||||
source: 'panel',
|
||||
message: `删除本地 feature 分支抛错 ${branch} (issue #${issueNumber})`,
|
||||
details: err instanceof Error ? err.message : String(err),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user