mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-28 03:55:02 +03:00
smarter defautl branch name
This commit is contained in:
parent
49df76fd0e
commit
c335f3e60d
43
src-tauri/src/dedup.rs
Normal file
43
src-tauri/src/dedup.rs
Normal 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);
|
||||||
|
});
|
||||||
|
}
|
@ -4,6 +4,7 @@ extern crate scopeguard;
|
|||||||
pub mod app;
|
pub mod app;
|
||||||
pub mod bookmarks;
|
pub mod bookmarks;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
|
pub mod dedup;
|
||||||
pub mod deltas;
|
pub mod deltas;
|
||||||
pub mod events;
|
pub mod events;
|
||||||
pub mod files;
|
pub mod files;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#[macro_use(defer)]
|
#[macro_use(defer)]
|
||||||
extern crate scopeguard;
|
extern crate scopeguard;
|
||||||
|
|
||||||
|
mod dedup;
|
||||||
mod app;
|
mod app;
|
||||||
mod assets;
|
mod assets;
|
||||||
mod bookmarks;
|
mod bookmarks;
|
||||||
|
@ -21,6 +21,7 @@ pub use iterator::BranchIterator as Iterator;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
dedup::dedup,
|
||||||
gb_repository,
|
gb_repository,
|
||||||
project_repository::{self, conflicts, diff},
|
project_repository::{self, conflicts, diff},
|
||||||
reader, sessions,
|
reader, sessions,
|
||||||
@ -1137,7 +1138,15 @@ pub fn create_virtual_branch(
|
|||||||
.name
|
.name
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|name| name.to_string())
|
.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 {
|
let mut branch = Branch {
|
||||||
id: Uuid::new_v4().to_string(),
|
id: Uuid::new_v4().to_string(),
|
||||||
|
Loading…
Reference in New Issue
Block a user