@@ -135,16 +135,25 @@ impl crate::Repository {
135135mod transport;
136136
137137mod remote {
138+ use crate :: bstr:: BStr ;
138139 use std:: { borrow:: Cow , collections:: BTreeSet } ;
139140
140- use crate :: { bstr :: ByteSlice , remote} ;
141+ use crate :: remote;
141142
142143 impl crate :: Repository {
143144 /// Returns a sorted list unique of symbolic names of remotes that
144145 /// we deem [trustworthy][crate::open::Options::filter_config_section()].
145- // TODO: Use `remote::Name` here
146- pub fn remote_names ( & self ) -> BTreeSet < & str > {
147- self . subsection_names_of ( "remote" )
146+ pub fn remote_names ( & self ) -> BTreeSet < Cow < ' _ , BStr > > {
147+ self . config
148+ . resolved
149+ . sections_by_name ( "remote" )
150+ . map ( |it| {
151+ let filter = self . filter_config_section ( ) ;
152+ it. filter ( move |s| filter ( s. meta ( ) ) )
153+ . filter_map ( |section| section. header ( ) . subsection_name ( ) . map ( Cow :: Borrowed ) )
154+ . collect ( )
155+ } )
156+ . unwrap_or_default ( )
148157 }
149158
150159 /// Obtain the branch-independent name for a remote for use in the given `direction`, or `None` if it could not be determined.
@@ -155,25 +164,23 @@ mod remote {
155164 /// # Notes
156165 ///
157166 /// It's up to the caller to determine what to do if the current `head` is unborn or detached.
158- // TODO: use remote::Name here
159- pub fn remote_default_name ( & self , direction : remote:: Direction ) -> Option < Cow < ' _ , str > > {
167+ pub fn remote_default_name ( & self , direction : remote:: Direction ) -> Option < Cow < ' _ , BStr > > {
160168 let name = ( direction == remote:: Direction :: Push )
161169 . then ( || {
162170 self . config
163171 . resolved
164172 . string_filter ( "remote" , None , "pushDefault" , & mut self . filter_config_section ( ) )
165- . and_then ( |s| match s {
166- Cow :: Borrowed ( s) => s. to_str ( ) . ok ( ) . map ( Cow :: Borrowed ) ,
167- Cow :: Owned ( s) => s. to_str ( ) . ok ( ) . map ( |s| Cow :: Owned ( s. into ( ) ) ) ,
168- } )
169173 } )
170174 . flatten ( ) ;
171175 name. or_else ( || {
172176 let names = self . remote_names ( ) ;
173177 match names. len ( ) {
174178 0 => None ,
175- 1 => names. iter ( ) . next ( ) . copied ( ) . map ( Cow :: Borrowed ) ,
176- _more_than_one => names. get ( "origin" ) . copied ( ) . map ( Cow :: Borrowed ) ,
179+ 1 => names. into_iter ( ) . next ( ) ,
180+ _more_than_one => {
181+ let origin = Cow :: Borrowed ( "origin" . into ( ) ) ;
182+ names. contains ( & origin) . then_some ( origin)
183+ }
177184 }
178185 } )
179186 }
@@ -191,8 +198,12 @@ mod branch {
191198 impl crate :: Repository {
192199 /// Return a set of unique short branch names for which custom configuration exists in the configuration,
193200 /// if we deem them [trustworthy][crate::open::Options::filter_config_section()].
201+ ///
202+ /// ### Note
203+ ///
204+ /// Branch names that have illformed UTF-8 will silently be skipped.
194205 pub fn branch_names ( & self ) -> BTreeSet < & str > {
195- self . subsection_names_of ( "branch" )
206+ self . subsection_str_names_of ( "branch" )
196207 }
197208
198209 /// Returns the validated reference on the remote associated with the given `short_branch_name`,
@@ -237,7 +248,7 @@ impl crate::Repository {
237248 . unwrap_or ( config:: section:: is_trusted)
238249 }
239250
240- fn subsection_names_of < ' a > ( & ' a self , header_name : & ' a str ) -> BTreeSet < & ' a str > {
251+ fn subsection_str_names_of < ' a > ( & ' a self , header_name : & ' a str ) -> BTreeSet < & ' a str > {
241252 self . config
242253 . resolved
243254 . sections_by_name ( header_name)
0 commit comments