Very simple branch search implementation
parent: tbd commit: a4f9476
Showing 4 changed files with 25 insertions and 2 deletions
Cargo.lock
@@ -783,6 +783,7 @@ dependencies = [ | ||
783 | 783 | "serde", |
784 | 784 | "serde_json", |
785 | 785 | "sqlx", |
786 | "strsim", | |
786 | 787 | "thiserror", |
787 | 788 | "tokio", |
788 | 789 | "tokio-tungstenite", |
@@ -2307,6 +2308,12 @@ dependencies = [ | ||
2307 | 2308 | ] |
2308 | 2309 | |
2309 | 2310 | [[package]] |
2311 | name = "strsim" | |
2312 | version = "0.11.0" | |
2313 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
2314 | checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" | |
2315 | ||
2316 | [[package]] | |
2310 | 2317 | name = "subtle" |
2311 | 2318 | version = "2.5.0" |
2312 | 2319 | source = "registry+https://github.com/rust-lang/crates.io-index" |
giterated-daemon/Cargo.toml
@@ -48,5 +48,7 @@ thiserror = "1" | ||
48 | 48 | anyhow = "1" |
49 | 49 | sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-native-tls", "postgres", "macros", "migrate", "chrono" ] } |
50 | 50 | secrecy = "0.8.0" |
51 | # Fuzzy text search | |
52 | strsim = "0.11" | |
51 | 53 | |
52 | 54 | #uuid = { version = "1.4", features = [ "v4", "serde" ] } |
giterated-daemon/src/backend/git.rs
@@ -828,8 +828,15 @@ impl RepositoryBackend for GitBackend { | ||
828 | 828 | // Get the total amount of filtered branches |
829 | 829 | let branch_count = filtered_branches.len(); |
830 | 830 | |
831 | // Sort the branches by commit date | |
832 | filtered_branches.sort_by(|(_, _, _, c1), (_, _, _, c2)| c2.time.cmp(&c1.time)); | |
831 | if let Some(search) = &request.search { | |
832 | // TODO: Caching | |
833 | // Search by sorting using a simple fuzzy search algorithm | |
834 | filtered_branches.sort_by(|(n1, _, _, _), (n2, _, _, _)| strsim::damerau_levenshtein(search, n1).cmp(&strsim::damerau_levenshtein(search, n2))); | |
835 | } else { | |
836 | // Sort the branches by commit date | |
837 | filtered_branches.sort_by(|(_, _, _, c1), (_, _, _, c2)| c2.time.cmp(&c1.time)); | |
838 | } | |
839 | ||
833 | 840 | // Go to the requested position |
834 | 841 | let mut filtered_branches = filtered_branches.iter().skip(request.range.0); |
835 | 842 |
giterated-models/src/repository/operations.rs
@@ -255,6 +255,9 @@ impl GiteratedOperation<Repository> for RepositoryStatisticsRequest { | ||
255 | 255 | |
256 | 256 | /// A request to get a list of branches in the repository. |
257 | 257 | /// Also returns the total amount of branches after the filter is applied. |
258 | /// | |
259 | /// Optional search parameter that'll search through the filtered branches | |
260 | /// | |
258 | 261 | /// Skips over references with invalid UTF-8 names. |
259 | 262 | /// |
260 | 263 | /// # Authentication |
@@ -268,6 +271,8 @@ impl GiteratedOperation<Repository> for RepositoryStatisticsRequest { | ||
268 | 271 | pub struct RepositoryBranchesRequest { |
269 | 272 | pub filter: RepositoryBranchFilter, |
270 | 273 | pub range: (usize, usize), |
274 | // pub sort: Option<???> | |
275 | pub search: Option<String>, | |
271 | 276 | } |
272 | 277 | |
273 | 278 | impl GiteratedOperation<Repository> for RepositoryBranchesRequest { |
@@ -399,12 +404,14 @@ impl<S: Clone + Send + Sync, B: ObjectBackend<S> + std::fmt::Debug> Object<'_, S | ||
399 | 404 | filter: RepositoryBranchFilter, |
400 | 405 | range_start: usize, |
401 | 406 | range_end: usize, |
407 | search: Option<String>, | |
402 | 408 | operation_state: &S, |
403 | 409 | ) -> Result<(Vec<RepositoryBranch>, usize), OperationError<RepositoryError>> { |
404 | 410 | self.request::<RepositoryBranchesRequest>( |
405 | 411 | RepositoryBranchesRequest { |
406 | 412 | filter, |
407 | 413 | range: (range_start, range_end), |
414 | search, | |
408 | 415 | }, |
409 | 416 | operation_state, |
410 | 417 | ) |