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
This commit is contained in:
Jose Garcia 2021-10-05 13:24:47 -04:00
parent a8d1718f21
commit 1971292175
2 changed files with 25 additions and 11 deletions

View file

@ -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

View file

@ -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
}