Skip to content

Commit 9666857

Browse files
authored
Merge pull request cli#5577 from cli/jg/fix-npe
codespaces: handle star in display name
2 parents 801cd3b + cd6386c commit 9666857

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

pkg/cmd/codespace/common.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*api.Codespace) (
139139
namesWithConflict := make(map[string]bool)
140140
codespacesByName := make(map[string]codespaceWithIndex)
141141
codespacesNames := make([]string, 0, len(codespaces))
142+
codespacesDirty := make(map[string]bool)
142143
for _, apiCodespace := range codespaces {
143144
cs := codespace{apiCodespace}
144145
csName := cs.displayName(false, false)
@@ -155,7 +156,14 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*api.Codespace) (
155156

156157
codespacesByName[fullDisplayName] = codespaceWithIndex{seenCodespace.cs, seenCodespace.idx}
157158
codespacesNames[seenCodespace.idx] = fullDisplayNameWithGitStatus
158-
delete(codespacesByName, csName) // delete the existing map entry with old name
159+
160+
// Update the git status dirty map to reflect the new name.
161+
if cs.hasUnsavedChanges() {
162+
codespacesDirty[fullDisplayNameWithGitStatus] = true
163+
}
164+
165+
// delete the existing map entry with old name
166+
delete(codespacesByName, csName)
159167

160168
// All other codespaces with the same name should update
161169
// to their specific name, this tracks conflicting names going forward
@@ -169,6 +177,10 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*api.Codespace) (
169177

170178
codespacesByName[csName] = codespaceWithIndex{cs, len(codespacesNames)}
171179
codespacesNames = append(codespacesNames, displayNameWithGitStatus)
180+
181+
if cs.hasUnsavedChanges() {
182+
codespacesDirty[displayNameWithGitStatus] = true
183+
}
172184
}
173185

174186
csSurvey := []*survey.Question{
@@ -193,10 +205,28 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*api.Codespace) (
193205
// Codespaces are indexed without the git status included as compared
194206
// to how it is displayed in the prompt, so the git status symbol needs
195207
// cleaning up in case it is included.
196-
selectedCodespace := strings.Replace(answers.Codespace, gitStatusDirty, "", -1)
208+
selectedCodespace := answers.Codespace
209+
isDirty := codespacesDirty[selectedCodespace]
210+
if isDirty {
211+
selectedCodespace = withoutGitStatus(answers.Codespace)
212+
}
197213
return codespacesByName[selectedCodespace].cs.Codespace, nil
198214
}
199215

216+
// withoutGitStatus returns the string without the git status symbol.
217+
func withoutGitStatus(cname string) string {
218+
return replaceLastOccurence(cname, gitStatusDirty, "")
219+
}
220+
221+
// replaceLastOccurence replaces the last occurence of a string with another string.
222+
func replaceLastOccurence(str, old, replace string) string {
223+
i := strings.LastIndex(str, old)
224+
if i == -1 {
225+
return str
226+
}
227+
return str[:i] + replace + str[i+len(old):]
228+
}
229+
200230
// getOrChooseCodespace prompts the user to choose a codespace if the codespaceName is empty.
201231
// It then fetches the codespace record with full connection details.
202232
// TODO(josebalius): accept a progress indicator or *App and show progress when fetching.

0 commit comments

Comments
 (0)