diff --git a/packages/cli/src/commands/worktree/ls.ts b/packages/cli/src/commands/worktree/ls.ts index af06364e4..e6bbe81a5 100644 --- a/packages/cli/src/commands/worktree/ls.ts +++ b/packages/cli/src/commands/worktree/ls.ts @@ -1,4 +1,6 @@ import type { Command } from 'commander' +import { homedir } from 'node:os' +import { basename, join, sep } from 'node:path' import { connectToDaemon, getDaemonHost } from '../../utils/client.js' import type { CommandOptions, ListResult, OutputSchema, CommandError } from '../../output/index.js' @@ -21,9 +23,20 @@ function shortenPath(path: string): string { /** Extract worktree name from path */ function extractWorktreeName(path: string): string { - // ~/.paseo/worktrees// -> name - const parts = path.split('/') - return parts[parts.length - 1] ?? path + return basename(path) +} + +export function resolvePaseoHomePath(): string { + return process.env.PASEO_HOME ?? join(homedir(), '.paseo') +} + +export function resolvePaseoWorktreesDir(): string { + return join(resolvePaseoHomePath(), 'worktrees') +} + +function isAgentInManagedWorktree(agentCwd: string): boolean { + const worktreesDir = resolvePaseoWorktreesDir() + return agentCwd === worktreesDir || agentCwd.startsWith(worktreesDir + sep) } /** Schema for worktree ls output */ @@ -81,10 +94,7 @@ export async function runLsCommand( // Build a map of worktree paths to agent IDs const worktreeAgentMap = new Map() for (const agent of agents) { - // Check if agent cwd is under ~/.paseo/worktrees/ - const paseoHome = process.env.PASEO_HOME ?? (process.env.HOME + '/.paseo') - const worktreesDir = paseoHome + '/worktrees/' - if (agent.cwd.startsWith(worktreesDir)) { + if (isAgentInManagedWorktree(agent.cwd)) { worktreeAgentMap.set(agent.cwd, agent.id.slice(0, 7)) } } diff --git a/packages/cli/tests/20-worktree-ls-paths.test.ts b/packages/cli/tests/20-worktree-ls-paths.test.ts new file mode 100644 index 000000000..341ce8d66 --- /dev/null +++ b/packages/cli/tests/20-worktree-ls-paths.test.ts @@ -0,0 +1,38 @@ +#!/usr/bin/env npx tsx + +import assert from 'node:assert' +import { homedir } from 'node:os' +import { join } from 'node:path' +import { resolvePaseoHomePath, resolvePaseoWorktreesDir } from '../src/commands/worktree/ls.js' + +console.log('=== Worktree LS Path Helper Tests ===\n') + +const originalPaseoHome = process.env.PASEO_HOME + +try { + { + console.log('Test 1: resolves explicit PASEO_HOME when set') + process.env.PASEO_HOME = '/tmp/paseo-explicit-home' + + assert.strictEqual(resolvePaseoHomePath(), '/tmp/paseo-explicit-home') + assert.strictEqual(resolvePaseoWorktreesDir(), '/tmp/paseo-explicit-home/worktrees') + console.log('\u2713 explicit PASEO_HOME is respected\n') + } + + { + console.log('Test 2: falls back to homedir/.paseo when PASEO_HOME is unset') + delete process.env.PASEO_HOME + + assert.strictEqual(resolvePaseoHomePath(), join(homedir(), '.paseo')) + assert.strictEqual(resolvePaseoWorktreesDir(), join(homedir(), '.paseo', 'worktrees')) + console.log('\u2713 fallback home path is derived from os.homedir()\n') + } +} finally { + if (originalPaseoHome === undefined) { + delete process.env.PASEO_HOME + } else { + process.env.PASEO_HOME = originalPaseoHome + } +} + +console.log('=== All worktree ls path helper tests passed ===')