diff --git a/pkg/commands/git_commands/worktree_loader.go b/pkg/commands/git_commands/worktree_loader.go index 418ca5c06..d9b8c07c4 100644 --- a/pkg/commands/git_commands/worktree_loader.go +++ b/pkg/commands/git_commands/worktree_loader.go @@ -236,9 +236,16 @@ func getUniqueNamesFromPathsAux(paths []pathWithIndexT, depth int) []nameWithInd result := []nameWithIndexT{} for _, group := range groups { - if len(group) == 1 { - path := group[0] - result = append(result, nameWithIndexT{index: path.index, name: sliceAtDepth(path.path, depth)}) + // Identical paths can't be told apart at any depth, so they are named + // like a single path. Git reports two worktrees with the same path when + // a worktree directory was deleted and another worktree was moved to + // its place. + if lo.EveryBy(group, func(path pathWithIndexT) bool { + return trimSlashes(path.path) == trimSlashes(group[0].path) + }) { + for _, path := range group { + result = append(result, nameWithIndexT{index: path.index, name: sliceAtDepth(path.path, depth)}) + } } else { result = append(result, getUniqueNamesFromPathsAux(group, depth+1)...) } diff --git a/pkg/commands/git_commands/worktree_loader_test.go b/pkg/commands/git_commands/worktree_loader_test.go index c537c6be4..2f715a7f0 100644 --- a/pkg/commands/git_commands/worktree_loader_test.go +++ b/pkg/commands/git_commands/worktree_loader_test.go @@ -353,6 +353,28 @@ func TestGetUniqueNamesFromPaths(t *testing.T) { "e/c/d", }, }, + { + input: []string{ + "/a/b/c", + "/a/b/c", + }, + expected: []string{ + "c", + "c", + }, + }, + { + input: []string{ + "/a/b/c", + "/a/b/c/", + "/a/d/c", + }, + expected: []string{ + "b/c", + "b/c", + "d/c", + }, + }, } { actual := getUniqueNamesFromPaths(scenario.input) assert.EqualValues(t, scenario.expected, actual)