From 465faca6d1255cb630de00e374a1675be25aa547 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Fri, 14 Jul 2023 20:58:20 +0100 Subject: Add workspace mode, enable if in git repo (#1053) * Add workspace mode, enable if in git repo * Fix tests * Should now be good * Page filter modes correctly if in workspace --- atuin-client/src/database.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'atuin-client/src/database.rs') diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index 218c1d6e..e37eb2b8 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -1,4 +1,8 @@ -use std::{env, path::Path, str::FromStr}; +use std::{ + env, + path::{Path, PathBuf}, + str::FromStr, +}; use async_trait::async_trait; use atuin_common::utils; @@ -25,6 +29,7 @@ pub struct Context { pub cwd: String, pub hostname: String, pub host_id: String, + pub git_root: Option, } #[derive(Default, Clone)] @@ -52,11 +57,13 @@ pub fn current_context() -> Context { ); let cwd = utils::get_current_dir(); let host_id = Settings::host_id().expect("failed to load host ID"); + let git_root = utils::in_git_repo(cwd.as_str()); Context { session, hostname, cwd, + git_root, host_id: host_id.0.as_simple().to_string(), } } @@ -261,6 +268,7 @@ impl Database for Sqlite { FilterMode::Host => query.and_where_eq("hostname", quote(&context.hostname)), FilterMode::Session => query.and_where_eq("session", quote(&context.session)), FilterMode::Directory => query.and_where_eq("cwd", quote(&context.cwd)), + FilterMode::Workspace => query.and_where_like_any("cwd", context.cwd.clone()), }; if unique { @@ -380,11 +388,18 @@ impl Database for Sqlite { sql.order_desc("timestamp"); } + let git_root = if let Some(git_root) = context.git_root.clone() { + git_root.to_str().unwrap_or("/").to_string() + } else { + context.cwd.clone() + }; + match filter { FilterMode::Global => &mut sql, FilterMode::Host => sql.and_where_eq("hostname", quote(&context.hostname)), FilterMode::Session => sql.and_where_eq("session", quote(&context.session)), FilterMode::Directory => sql.and_where_eq("cwd", quote(&context.cwd)), + FilterMode::Workspace => sql.and_where_like_left("cwd", git_root), }; let orig_query = query; @@ -556,6 +571,7 @@ mod test { session: "beepboopiamasession".to_string(), cwd: "/home/ellie".to_string(), host_id: "test-host".to_string(), + git_root: None, }; let results = db @@ -765,6 +781,7 @@ mod test { session: "beepboopiamasession".to_string(), cwd: "/home/ellie".to_string(), host_id: "test-host".to_string(), + git_root: None, }; let mut db = Sqlite::new("sqlite::memory:").await.unwrap(); -- cgit v1.3.1