@@ -16,6 +16,7 @@ import {
1616 resolveLocalCheckoutBranchMismatch ,
1717 resolvePreviousWorktreeLabel ,
1818 resolvePreviousWorktreeSeed ,
19+ sanitizeNewRefName ,
1920 shouldIncludeBranchPickerItem ,
2021 shouldShowComposerContextStrip ,
2122 shouldShowEnvironmentIndicator ,
@@ -729,4 +730,93 @@ describe("shouldIncludeBranchPickerItem", () => {
729730 } ) ,
730731 ) . toBe ( false ) ;
731732 } ) ;
733+
734+ // Typing a spaced name must still surface the ref it would have been created
735+ // as, or the picker shows nothing at all for that query.
736+ it ( "surfaces an existing ref matching the sanitized query" , ( ) => {
737+ expect (
738+ shouldIncludeBranchPickerItem ( {
739+ itemValue : "new-branch" ,
740+ normalizedQuery : "new branch" ,
741+ createBranchItemValue : null ,
742+ checkoutPullRequestItemValue : null ,
743+ } ) ,
744+ ) . toBe ( true ) ;
745+ } ) ;
746+
747+ // A partial query has to reach the ref it would have been created as, so
748+ // searching "hello w" still finds an existing hello-world.
749+ it ( "surfaces a ref from a partial query containing a space" , ( ) => {
750+ expect (
751+ shouldIncludeBranchPickerItem ( {
752+ itemValue : "hello-world" ,
753+ normalizedQuery : "hello w" ,
754+ createBranchItemValue : null ,
755+ checkoutPullRequestItemValue : null ,
756+ } ) ,
757+ ) . toBe ( true ) ;
758+ } ) ;
759+
760+ it ( "excludes refs matching neither the raw nor the sanitized query" , ( ) => {
761+ expect (
762+ shouldIncludeBranchPickerItem ( {
763+ itemValue : "main" ,
764+ normalizedQuery : "new branch" ,
765+ createBranchItemValue : null ,
766+ checkoutPullRequestItemValue : null ,
767+ } ) ,
768+ ) . toBe ( false ) ;
769+ } ) ;
770+ } ) ;
771+
772+ // Git rejects ASCII space and the ASCII control characters in ref names, so a
773+ // typed name like "new branch" can only ever fail. Replacing exactly those can
774+ // turn a failing name into a working one without touching a name git already
775+ // accepts, including one holding non-ASCII whitespace such as U+00A0.
776+ describe ( "sanitizeNewRefName" , ( ) => {
777+ it ( "replaces a space with a dash" , ( ) => {
778+ expect ( sanitizeNewRefName ( "new branch" ) ) . toBe ( "new-branch" ) ;
779+ } ) ;
780+
781+ it ( "collapses a run of whitespace into a single dash" , ( ) => {
782+ expect ( sanitizeNewRefName ( "new branch" ) ) . toBe ( "new-branch" ) ;
783+ } ) ;
784+
785+ it ( "trims surrounding whitespace instead of turning it into dashes" , ( ) => {
786+ expect ( sanitizeNewRefName ( " new branch " ) ) . toBe ( "new-branch" ) ;
787+ } ) ;
788+
789+ it ( "replaces tabs, which git rejects just like spaces" , ( ) => {
790+ expect ( sanitizeNewRefName ( "new\tbranch" ) ) . toBe ( "new-branch" ) ;
791+ } ) ;
792+
793+ // git accepts U+00A0, U+2009 and other non-ASCII whitespace in ref names, so
794+ // rewriting them would silently create a ref the user never typed.
795+ it ( "preserves whitespace that git accepts" , ( ) => {
796+ expect ( sanitizeNewRefName ( "new\u00a0branch" ) ) . toBe ( "new\u00a0branch" ) ;
797+ expect ( sanitizeNewRefName ( "new\u2009branch" ) ) . toBe ( "new\u2009branch" ) ;
798+ } ) ;
799+
800+ it ( "keeps slashes so nested ref names survive" , ( ) => {
801+ expect ( sanitizeNewRefName ( "feature/new thing" ) ) . toBe ( "feature/new-thing" ) ;
802+ } ) ;
803+
804+ it ( "preserves case because git ref names are case sensitive" , ( ) => {
805+ expect ( sanitizeNewRefName ( "Feature/New Thing" ) ) . toBe ( "Feature/New-Thing" ) ;
806+ } ) ;
807+
808+ it ( "leaves an already valid ref name untouched" , ( ) => {
809+ expect ( sanitizeNewRefName ( "feature/login" ) ) . toBe ( "feature/login" ) ;
810+ } ) ;
811+
812+ it ( "returns an empty string for whitespace-only input" , ( ) => {
813+ expect ( sanitizeNewRefName ( " " ) ) . toBe ( "" ) ;
814+ } ) ;
815+
816+ // Scoped deliberately to whitespace: git accepts consecutive dashes, so
817+ // collapsing them would rewrite names the user may have typed on purpose.
818+ it ( "does not collapse dashes the user typed" , ( ) => {
819+ expect ( sanitizeNewRefName ( "new - branch" ) ) . toBe ( "new---branch" ) ;
820+ expect ( sanitizeNewRefName ( "foo--bar" ) ) . toBe ( "foo--bar" ) ;
821+ } ) ;
732822} ) ;
0 commit comments