Skip to content

Commit 8ba4a0a

Browse files
committed
chore: unify worktree display name retrieval
1 parent b76d15f commit 8ba4a0a

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

cmd/wtp/list.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,22 @@ func truncatePath(path string, maxWidth int) string {
250250
return path[:startLen] + ellipsis + path[len(path)-endLen:]
251251
}
252252

253+
// getWorktreeDisplayName returns the display name for a worktree, with fallback for nil config
254+
func getWorktreeDisplayName(wt git.Worktree, cfg *config.Config, mainRepoPath string) string {
255+
if cfg != nil {
256+
return getWorktreeNameFromPath(wt.Path, cfg, mainRepoPath, wt.IsMain)
257+
}
258+
// Fallback when config can't be loaded
259+
if wt.IsMain {
260+
return "@"
261+
}
262+
return filepath.Base(wt.Path)
263+
}
264+
253265
// displayWorktreesQuiet outputs only the worktree names (as shown in PATH column), one per line
254266
func displayWorktreesQuiet(w io.Writer, worktrees []git.Worktree, cfg *config.Config, mainRepoPath string) {
255267
for _, wt := range worktrees {
256-
var pathDisplay string
257-
258-
// Use unified worktree naming function
259-
if cfg != nil {
260-
pathDisplay = getWorktreeNameFromPath(wt.Path, cfg, mainRepoPath, wt.IsMain)
261-
} else {
262-
// Fallback when config can't be loaded
263-
if wt.IsMain {
264-
pathDisplay = "@"
265-
} else {
266-
pathDisplay = filepath.Base(wt.Path)
267-
}
268-
}
269-
268+
pathDisplay := getWorktreeDisplayName(wt, cfg, mainRepoPath)
270269
fmt.Fprintln(w, pathDisplay)
271270
}
272271
}
@@ -301,20 +300,10 @@ func displayWorktreesRelative(
301300
var displayItems []displayData
302301

303302
for _, wt := range worktrees {
304-
var pathDisplay string
305303
var isCurrent bool
306304

307-
// Use unified worktree naming function
308-
if cfg != nil {
309-
pathDisplay = getWorktreeNameFromPath(wt.Path, cfg, mainRepoPath, wt.IsMain)
310-
} else {
311-
// Fallback when config can't be loaded
312-
if wt.IsMain {
313-
pathDisplay = "@"
314-
} else {
315-
pathDisplay = filepath.Base(wt.Path)
316-
}
317-
}
305+
// Get worktree display name
306+
pathDisplay := getWorktreeDisplayName(wt, cfg, mainRepoPath)
318307

319308
// Check if this is the current worktree
320309
if wt.Path == currentPath {

0 commit comments

Comments
 (0)