11use std:: { collections:: HashMap , fmt:: Display } ;
22
3- use bstr:: { BString , ByteSlice } ;
3+ use bstr:: { BStr , BString , ByteSlice } ;
44use but_core:: ref_metadata:: StackId ;
55use but_ctx:: Context ;
66use but_hunk_assignment:: HunkAssignment ;
@@ -22,7 +22,7 @@ fn branch_names(ctx: &Context) -> anyhow::Result<Vec<BString>> {
2222}
2323
2424pub struct IdDb {
25- branch_name_to_cli_id : HashMap < String , CliId > ,
25+ branch_name_to_cli_id : HashMap < BString , CliId > ,
2626 unassigned : CliId ,
2727}
2828
@@ -56,8 +56,8 @@ impl IdDb {
5656 }
5757 }
5858
59- let mut branch_name_to_cli_id: HashMap < String , CliId > = HashMap :: new ( ) ;
60- ' branch_name: for branch_name in & branch_names {
59+ let mut branch_name_to_cli_id: HashMap < BString , CliId > = HashMap :: new ( ) ;
60+ ' branch_name: for branch_name in branch_names {
6161 // Find first non-conflicting pair and use it as CliId.
6262 for pair in branch_name. windows ( 2 ) {
6363 let pair: [ u8 ; 2 ] = pair. try_into ( ) ?;
@@ -67,7 +67,7 @@ impl IdDb {
6767 let id = str:: from_utf8 ( & pair)
6868 . expect ( "if we stored it, it's ascii-alphanum" )
6969 . to_owned ( ) ;
70- branch_name_to_cli_id. insert ( name . clone ( ) , CliId :: Branch { name, id } ) ;
70+ branch_name_to_cli_id. insert ( branch_name , CliId :: Branch { name, id } ) ;
7171 continue ' branch_name;
7272 }
7373 }
@@ -80,15 +80,14 @@ impl IdDb {
8080 } )
8181 }
8282
83- fn find_branches_by_name ( & mut self , ctx : & Context , name : & str ) -> anyhow:: Result < Vec < CliId > > {
83+ fn find_branches_by_name ( & mut self , ctx : & Context , name : & BStr ) -> anyhow:: Result < Vec < CliId > > {
8484 let branch_names = branch_names ( ctx) ?;
8585 let mut matches = Vec :: new ( ) ;
8686
8787 for branch_name in branch_names {
88- let branch_name = branch_name. to_string ( ) ;
89- // Exact match or partial match
90- if branch_name == name || branch_name. contains ( name) {
91- matches. push ( self . branch ( & branch_name) . clone ( ) )
88+ // Partial match is fine
89+ if branch_name. contains_str ( name) {
90+ matches. push ( self . branch ( branch_name. as_ref ( ) ) . clone ( ) )
9291 }
9392 }
9493
@@ -97,12 +96,13 @@ impl IdDb {
9796
9897 /// Returns the ID for a branch of the given name. If no such ID exists,
9998 /// generate one.
100- pub fn branch ( & mut self , name : & str ) -> & CliId {
99+ pub fn branch ( & mut self , name : & BStr ) -> & CliId {
101100 self . branch_name_to_cli_id
102101 . entry ( name. to_owned ( ) )
103- . or_insert_with ( || CliId :: Branch {
104- name : name. to_owned ( ) ,
105- id : hash ( name) ,
102+ . or_insert_with ( || {
103+ let name = name. to_string ( ) ;
104+ let id = hash ( & name) ;
105+ CliId :: Branch { name, id }
106106 } )
107107 }
108108
@@ -214,7 +214,7 @@ impl CliId {
214214 let mut matches = Vec :: new ( ) ;
215215
216216 // First, try exact branch name match
217- if let Ok ( branch_matches) = id_db. find_branches_by_name ( ctx, s) {
217+ if let Ok ( branch_matches) = id_db. find_branches_by_name ( ctx, s. into ( ) ) {
218218 matches. extend ( branch_matches) ;
219219 }
220220
0 commit comments