Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

feat: add the directory of empty containers left after cleaning and discarding the work tree

hairyfc0deb054f049
diff --git a/packages/dsh-tauri-worktree/README.md b/packages/dsh-tauri-worktree/README.md
index 73e821fe..07bc9d71 100644
--- a/packages/dsh-tauri-worktree/README.md
+++ b/packages/dsh-tauri-worktree/README.md
@@ -51,9 +51,12 @@ checkout_worktree({ worktree_hash_dirname: '[hash]/[dirname]', branch_name: 'dsh
2. 目录重命名到同卷 `<worktreesRoot>/.trash/[hash]/[dirname]` 后用
`fs.rm({ recursive, force, maxRetries: 10, retryDelay: 100 })` 删除;重命名失败
(跨卷/句柄占用)时退回直接 `fs.rm`,外层再重试 3 次(间隔 2s)。
-3. `git worktree prune --expire now` 只清理 Git 管理记录;之后才删除本插件拥有的
+3. 目录删除后顺带移除因此变空的 `worktrees/[hash]` 与 `.trash/[hash]` 容器目录
+ (`rmdir` 仅对空目录生效,非空/不存在时静默跳过),避免每次放弃/检出都在
+ `<worktreesRoot>` 下残留一个空 `<hash>` 文件夹;收尾为尽力而为,失败不影响删除结果。
+4. `git worktree prune --expire now` 只清理 Git 管理记录;之后才删除本插件拥有的
`dsh/*` 分支。任一步失败都会保留绑定(ledger),可通过再次放弃/重试收敛。
-4. 残留状态收敛:目录-only(中断遗留)在 `ensureWorktree` 重建前被 fs 删除;管理记录-only
+5. 残留状态收敛:目录-only(中断遗留)在 `ensureWorktree` 重建前被 fs 删除;管理记录-only
在创建前被 prune;两者共存时拒绝覆盖已注册但缺少绑定的完整工作树。
## 异步放弃(discard job)
diff --git a/packages/dsh-tauri-worktree/src/host/service/operation.test.ts b/packages/dsh-tauri-worktree/src/host/service/operation.test.ts
index 0d7f5d9f..516c821e 100644
--- a/packages/dsh-tauri-worktree/src/host/service/operation.test.ts
+++ b/packages/dsh-tauri-worktree/src/host/service/operation.test.ts
@@ -199,4 +199,26 @@ describe('ensureWorktree', () => {
expect(result.ok).toBe(true)
expect(existsSync(created.binding.worktreePath)).toBe(false)
})
+
+ it('removes the emptied worktrees/<hash> and .trash/<hash> containers along with the worktree', async () => {
+ const repository = await createRepository('main')
+ const worktreesRoot = await mkdtemp(join(tmpdir(), 'dsh-worktrees-root-'))
+ temporaryDirectories.push(worktreesRoot)
+ const created = await ensureWorktree({}, worktreesRoot, repository, 'container-cleanup-session')
+ expect(created.ok).toBe(true)
+ if (!created.ok)
+ return
+
+ const container = join(worktreesRoot, 'worktrees', created.binding.hash)
+ const trashContainer = join(worktreesRoot, '.trash', created.binding.hash)
+ expect(existsSync(container)).toBe(true)
+
+ const result = await discardWorktree({}, worktreesRoot, { sessionId: 'container-cleanup-session' })
+
+ expect(result.ok).toBe(true)
+ expect(existsSync(created.binding.worktreePath)).toBe(false)
+ // 删除后不再残留空的 <hash> 容器目录(含 .trash 一侧的 rename 中间目录)。
+ expect(existsSync(container)).toBe(false)
+ expect(existsSync(trashContainer)).toBe(false)
+ })
})
diff --git a/packages/dsh-tauri-worktree/src/host/service/operation.ts b/packages/dsh-tauri-worktree/src/host/service/operation.ts
index c7bc1981..cf11a838 100644
--- a/packages/dsh-tauri-worktree/src/host/service/operation.ts
+++ b/packages/dsh-tauri-worktree/src/host/service/operation.ts
@@ -20,6 +20,7 @@ import type {
} from '../types/index.js'
import { createHash } from 'node:crypto'
import { existsSync } from 'node:fs'
+import { rmdir } from 'node:fs/promises'
import process from 'node:process'
import { join, resolve } from 'pathe'
import { WORKTREE_BRANCH_NAME_PATTERN } from '../constants/index.js'
@@ -55,6 +56,27 @@ function worktreeTrashPath(worktreesRoot: string, hash: string, dirname: string)
return join(worktreesRoot, '.trash', hash, dirname)
}
+/**
+ * 顺带删除某 hash 的插件自有容器目录 `worktrees/<hash>` 与 `.trash/<hash>`。
+ * 工作树目录被 rename 到 .trash 再删除后,这两个父目录只剩空壳;一并移除,避免
+ * 每次放弃/检出都在 worktreesRoot 下累积一个空 `<hash>` 文件夹。rmdir 只在目录
+ * 为空时成功——目录不存在(ENOENT)或仍含内容(ENOTEMPTY,如并发重建/异常残留)
+ * 时静默跳过;收尾是尽力而为,绝不因清理失败让删除操作整体报错。
+ */
+async function removeEmptyHashContainers(worktreesRoot: string, hash: string): Promise<void> {
+ for (const container of [
+ join(worktreesRoot, 'worktrees', hash),
+ join(worktreesRoot, '.trash', hash),
+ ]) {
+ try {
+ await rmdir(container)
+ }
+ catch {
+ /* 目录已不存在或非空:无需处理 */
+ }
+ }
+}
+
async function pruneWorktreeAdmin(root: string, signal?: AbortSignal): Promise<OperationResult> {
// Explicit expiry makes directory-less admin entries (state C) disappear
// immediately rather than waiting for Git's default stale-entry age.
@@ -125,6 +147,9 @@ async function removeWorktreeOnDisk(
return { ok: false, error: error instanceof Error ? error.message : String(error) }
}
+ // 工作树目录及其 .trash 副本都已删除:顺带清掉因此变空的 <hash> 容器目录。
+ await removeEmptyHashContainers(worktreesRoot, hash)
+
// Git is deliberately limited to metadata cleanup. It must never traverse
// the worktree because old Git for Windows follows junction targets.
return pruneWorktreeAdmin(root, signal)