diff --git a/Cargo.lock b/Cargo.lock index a422ee3..81bd875 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -783,6 +783,7 @@ dependencies = [ "serde", "serde_json", "sqlx", + "strsim", "thiserror", "tokio", "tokio-tungstenite", @@ -2307,6 +2308,12 @@ dependencies = [ ] [[package]] +name = "strsim" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" + +[[package]] name = "subtle" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/giterated-daemon/Cargo.toml b/giterated-daemon/Cargo.toml index c43a603..ffee1cd 100644 --- a/giterated-daemon/Cargo.toml +++ b/giterated-daemon/Cargo.toml @@ -48,5 +48,7 @@ thiserror = "1" anyhow = "1" sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-native-tls", "postgres", "macros", "migrate", "chrono" ] } secrecy = "0.8.0" +# Fuzzy text search +strsim = "0.11" #uuid = { version = "1.4", features = [ "v4", "serde" ] } diff --git a/giterated-daemon/src/backend/git.rs b/giterated-daemon/src/backend/git.rs index 35af1c9..212988e 100644 --- a/giterated-daemon/src/backend/git.rs +++ b/giterated-daemon/src/backend/git.rs @@ -828,8 +828,15 @@ impl RepositoryBackend for GitBackend { // Get the total amount of filtered branches let branch_count = filtered_branches.len(); - // Sort the branches by commit date - filtered_branches.sort_by(|(_, _, _, c1), (_, _, _, c2)| c2.time.cmp(&c1.time)); + if let Some(search) = &request.search { + // TODO: Caching + // Search by sorting using a simple fuzzy search algorithm + filtered_branches.sort_by(|(n1, _, _, _), (n2, _, _, _)| strsim::damerau_levenshtein(search, n1).cmp(&strsim::damerau_levenshtein(search, n2))); + } else { + // Sort the branches by commit date + filtered_branches.sort_by(|(_, _, _, c1), (_, _, _, c2)| c2.time.cmp(&c1.time)); + } + // Go to the requested position let mut filtered_branches = filtered_branches.iter().skip(request.range.0); diff --git a/giterated-models/src/repository/operations.rs b/giterated-models/src/repository/operations.rs index eee751a..f7deaaf 100644 --- a/giterated-models/src/repository/operations.rs +++ b/giterated-models/src/repository/operations.rs @@ -255,6 +255,9 @@ impl GiteratedOperation for RepositoryStatisticsRequest { /// A request to get a list of branches in the repository. /// Also returns the total amount of branches after the filter is applied. +/// +/// Optional search parameter that'll search through the filtered branches +/// /// Skips over references with invalid UTF-8 names. /// /// # Authentication @@ -268,6 +271,8 @@ impl GiteratedOperation for RepositoryStatisticsRequest { pub struct RepositoryBranchesRequest { pub filter: RepositoryBranchFilter, pub range: (usize, usize), + // pub sort: Option + pub search: Option, } impl GiteratedOperation for RepositoryBranchesRequest { @@ -399,12 +404,14 @@ impl + std::fmt::Debug> Object<'_, S filter: RepositoryBranchFilter, range_start: usize, range_end: usize, + search: Option, operation_state: &S, ) -> Result<(Vec, usize), OperationError> { self.request::( RepositoryBranchesRequest { filter, range: (range_start, range_end), + search, }, operation_state, )