Skip to content

Commit 0ac0ddf

Browse files
id: use BString internally for branch names
This eliminates the need for some redundant conversions.
1 parent 35415cd commit 0ac0ddf

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

crates/but/src/id.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{collections::HashMap, fmt::Display};
22

3-
use bstr::{BString, ByteSlice};
3+
use bstr::{BStr, BString, ByteSlice};
44
use but_core::ref_metadata::StackId;
55
use but_ctx::Context;
66
use but_hunk_assignment::HunkAssignment;
@@ -22,7 +22,7 @@ fn branch_names(ctx: &Context) -> anyhow::Result<Vec<BString>> {
2222
}
2323

2424
pub 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

crates/but/src/status/json.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn convert_branch_to_json(
330330
review_map: &std::collections::HashMap<String, Vec<but_forge::ForgeReview>>,
331331
id_db: &mut crate::id::IdDb,
332332
) -> anyhow::Result<Branch> {
333-
let cli_id = id_db.branch(&branch.name.to_string()).to_string();
333+
let cli_id = id_db.branch(branch.name.as_ref()).to_string();
334334

335335
let review_id = if review {
336336
crate::forge::review::get_review_numbers(
@@ -379,7 +379,7 @@ pub(super) fn build_workspace_status_json(
379379
let stack_cli_id = details
380380
.branch_details
381381
.first()
382-
.map(|b| id_db.branch(&b.name.to_string()).to_string())
382+
.map(|b| id_db.branch(b.name.as_ref()).to_string())
383383
.unwrap_or_else(|| "unknown".to_string());
384384

385385
let json_assigned_changes = convert_file_assignments(assignments, worktree_changes);

crates/but/src/status/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ pub fn print_group(
385385
let mut first = true;
386386
for branch in &group.branch_details {
387387
let id = id_db
388-
.branch(branch.name.to_str()?)
388+
.branch(branch.name.as_ref())
389389
.to_string()
390390
.underline()
391391
.blue();
@@ -550,7 +550,7 @@ pub(crate) fn all_branches(ctx: &Context) -> anyhow::Result<Vec<CliId>> {
550550
let mut branches = Vec::new();
551551
for stack in stacks {
552552
for head in stack.heads {
553-
branches.push(id_db.branch(&head.name.to_string()).clone());
553+
branches.push(id_db.branch(head.name.as_ref()).clone());
554554
}
555555
}
556556
Ok(branches)

0 commit comments

Comments
 (0)