From 1971292175011112e461240f64ff93131e288c24 Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Tue, 5 Oct 2021 13:24:47 -0400 Subject: [PATCH] Fixes bug there are +2 codespaces with a conflict - Tracks conflicting name going forward for other records - Moves the git status dirty star into a constant so we can reference it --- internal/codespaces/codespace/codespace.go | 5 +++- pkg/cmd/codespace/common.go | 31 +++++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/internal/codespaces/codespace/codespace.go b/internal/codespaces/codespace/codespace.go index 69188ea4b..66f4a2c3f 100644 --- a/internal/codespaces/codespace/codespace.go +++ b/internal/codespaces/codespace/codespace.go @@ -32,11 +32,14 @@ func (c *Codespace) DisplayName(includeName, includeGitStatus bool) string { return c.RepositoryNWO + ": " + branch } +// GitStatusDirty represents an unsaved changes status. +const GitStatusDirty = "*" + // BranchWithGitStatus returns the branch with a star // if the branch is currently being worked on. func (c *Codespace) BranchWithGitStatus() string { if c.HasUnsavedChanges() { - return c.Branch + "*" + return c.Branch + GitStatusDirty } return c.Branch diff --git a/pkg/cmd/codespace/common.go b/pkg/cmd/codespace/common.go index 1d96bbce4..05d7e615e 100644 --- a/pkg/cmd/codespace/common.go +++ b/pkg/cmd/codespace/common.go @@ -74,22 +74,30 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*codespace.Codesp idx int } + namesWithConflict := make(map[string]bool) codespacesByName := make(map[string]codespaceWithIndex) codespacesNames := make([]string, 0, len(codespaces)) for _, cs := range codespaces { csName := cs.DisplayName(false, false) displayNameWithGitStatus := cs.DisplayName(false, true) - if seenCodespace, ok := codespacesByName[csName]; ok { - // there is an existing codespace on the repo and branch - // we need to disambiguate by adding the codespace name - // to the existing entry and the one we are adding now - fullDisplayName := seenCodespace.cs.DisplayName(true, false) - fullDisplayNameWithGitStatus := seenCodespace.cs.DisplayName(true, true) + _, hasExistingConflict := namesWithConflict[csName] + if seenCodespace, ok := codespacesByName[csName]; ok || hasExistingConflict { + // There is an existing codespace on the repo and branch. + // We need to disambiguate by adding the codespace name + // to the existing entry and the one we are processing now. + if !hasExistingConflict { + fullDisplayName := seenCodespace.cs.DisplayName(true, false) + fullDisplayNameWithGitStatus := seenCodespace.cs.DisplayName(true, true) - codespacesByName[fullDisplayName] = codespaceWithIndex{seenCodespace.cs, seenCodespace.idx} - codespacesNames[seenCodespace.idx] = fullDisplayNameWithGitStatus - delete(codespacesByName, csName) // delete the existing map entry with old name + codespacesByName[fullDisplayName] = codespaceWithIndex{seenCodespace.cs, seenCodespace.idx} + codespacesNames[seenCodespace.idx] = fullDisplayNameWithGitStatus + delete(codespacesByName, csName) // delete the existing map entry with old name + + // All other codespaces with the same name should update + // to their specific name, this tracks conflicting names going forward + namesWithConflict[csName] = true + } // update this codespace names to include the name to disambiguate csName = cs.DisplayName(true, false) @@ -119,7 +127,10 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*codespace.Codesp return nil, fmt.Errorf("error getting answers: %w", err) } - selectedCodespace := strings.Replace(answers.Codespace, "*", "", -1) + // Codespaces are indexed without the git status included as compared + // to how it is displayed in the prompt, so the git status symbol needs + // cleaning up in case it is included. + selectedCodespace := strings.Replace(answers.Codespace, codespace.GitStatusDirty, "", -1) codespace := codespacesByName[selectedCodespace].cs return codespace, nil }