smarter defautl branch name

This commit is contained in:
Nikita Galaiko 2023-07-25 14:26:15 +02:00
parent 49df76fd0e
commit c335f3e60d
4 changed files with 55 additions and 1 deletions

43
src-tauri/src/dedup.rs Normal file
View File

@ -0,0 +1,43 @@
use std::collections::HashSet;
// dedup makes sure that _new_ is not in _existing_ by adding a number to it.
// the number is increased until the name is unique.
pub fn dedup(existing: &[&str], new: &str) -> String {
let used_numbers = existing
.iter()
.filter(|x| x.starts_with(new))
.filter_map(|x| {
x.strip_prefix(new)
.map(|x| x.trim_start())
.map(|x| x.parse::<i32>().unwrap_or(-1))
})
.collect::<HashSet<_>>();
if used_numbers.is_empty() || !used_numbers.contains(&-1) {
new.to_string()
} else {
// pick first unused number
let mut number = 1;
while used_numbers.contains(&number) {
number += 1;
}
format!("{} {}", new, number)
}
}
#[test]
fn test_dedup() {
vec![
(vec!["foo", "foo 2"], "foo", "foo 1"),
(vec!["foo", "foo 1", "foo 2"], "foo", "foo 3"),
(vec!["foo", "foo 1", "foo 2"], "foo 1", "foo 1 1"),
(vec!["foo", "foo 1", "foo 2"], "foo 2", "foo 2 1"),
(vec!["foo", "foo 1", "foo 2"], "foo 3", "foo 3"),
(vec!["foo 2"], "foo", "foo"),
(vec!["foo", "foo 1", "foo 2", "foo 4"], "foo", "foo 3"),
]
.iter()
.enumerate()
.for_each(|(i, (existing, new, expected))| {
assert_eq!(dedup(existing, new), expected.to_string(), "test {}", i + 1);
});
}

View File

@ -4,6 +4,7 @@ extern crate scopeguard;
pub mod app;
pub mod bookmarks;
pub mod database;
pub mod dedup;
pub mod deltas;
pub mod events;
pub mod files;

View File

@ -1,6 +1,7 @@
#[macro_use(defer)]
extern crate scopeguard;
mod dedup;
mod app;
mod assets;
mod bookmarks;

View File

@ -21,6 +21,7 @@ pub use iterator::BranchIterator as Iterator;
use uuid::Uuid;
use crate::{
dedup::dedup,
gb_repository,
project_repository::{self, conflicts, diff},
reader, sessions,
@ -1137,7 +1138,15 @@ pub fn create_virtual_branch(
.name
.as_ref()
.map(|name| name.to_string())
.unwrap_or_else(|| format!("Virtual branch {}", all_virtual_branches.len() + 1));
.unwrap_or_else(|| {
dedup(
&all_virtual_branches
.iter()
.map(|b| b.name.as_str())
.collect::<Vec<_>>(),
"Virtual branch",
)
});
let mut branch = Branch {
id: Uuid::new_v4().to_string(),