withWorktree

fun <T> withWorktree(ref: String, parentDir: File, block: (File) -> T): T

Materializes ref into a detached worktree under parentDir, runs block with its directory, and removes the worktree afterward (even on failure).