From 2d9e7380385d9c58b0b0379b2fb7523fc4e3b998 Mon Sep 17 00:00:00 2001 From: Scott Chacon Date: Thu, 9 Mar 2023 11:24:21 -0800 Subject: [PATCH] v1 git status --- src-tauri/src/main.rs | 21 +++++++++- src-tauri/src/repositories/repositories.rs | 43 ++++++++++++++++++++ src/routes/projects/[projectId]/+layout.ts | 9 +++- src/routes/projects/[projectId]/+page.svelte | 31 +++++++++++++- 4 files changed, 100 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 3dd5054db..5878c9e4f 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -370,6 +370,24 @@ fn list_deltas( Ok(deltas) } +#[tauri::command] +fn git_status( + handle: tauri::AppHandle, + project_id: &str, +) -> Result, Error> { + let app_state = handle.state::(); + + let repo = repositories::Repository::open( + &app_state.projects_storage, + &app_state.users_storage, + project_id, + )?; + + let files = repo.status(); + + Ok(files) +} + fn main() { let quit = tauri::CustomMenuItem::new("quit".to_string(), "Quit"); let hide = tauri::CustomMenuItem::new("toggle".to_string(), format!("Hide {}", app_title())); @@ -480,7 +498,8 @@ fn main() { set_user, delete_user, get_user, - search + search, + git_status ]); let tauri_context = generate_context!(); diff --git a/src-tauri/src/repositories/repositories.rs b/src-tauri/src/repositories/repositories.rs index d4ec765d4..2fd9febeb 100644 --- a/src-tauri/src/repositories/repositories.rs +++ b/src-tauri/src/repositories/repositories.rs @@ -69,6 +69,49 @@ impl Repository { session_id, ) } + + // get file status from git + pub fn status(&self) -> HashMap { + println!("Git Status"); + + let mut options = git2::StatusOptions::new(); + options.include_untracked(true); + options.include_ignored(false); + options.recurse_untracked_dirs(true); + + // get the status of the repository + let statuses = self + .git_repository + .statuses(Some(&mut options)) + .with_context(|| "failed to get repository status"); + + let mut files = HashMap::new(); + + match statuses { + Ok(statuses) => { + // iterate over the statuses + for entry in statuses.iter() { + // get the path of the entry + let path = entry.path().unwrap(); + // get the status as a string + let istatus = match entry.status() { + s if s.contains(git2::Status::WT_NEW) => "added", + s if s.contains(git2::Status::WT_MODIFIED) => "modified", + s if s.contains(git2::Status::WT_DELETED) => "deleted", + s if s.contains(git2::Status::WT_RENAMED) => "renamed", + s if s.contains(git2::Status::WT_TYPECHANGE) => "typechange", + _ => continue, + }; + files.insert(path.to_string(), istatus.to_string()); + } + } + Err(_) => { + println!("Error getting status"); + } + } + + return files; + } } fn init( diff --git a/src/routes/projects/[projectId]/+layout.ts b/src/routes/projects/[projectId]/+layout.ts index 886d6b373..07f0fffdd 100644 --- a/src/routes/projects/[projectId]/+layout.ts +++ b/src/routes/projects/[projectId]/+layout.ts @@ -6,11 +6,16 @@ import type { UISession } from '$lib/uisessions'; import { asyncDerived } from '@square/svelte-store'; import type { Delta } from '$lib/deltas'; import { startOfDay } from 'date-fns'; +import { invoke } from '@tauri-apps/api'; export const prerender = false; export const load: LayoutLoad = async ({ parent, params }) => { const { projects } = await parent(); + const filesStatus = building + ? readable>({}) + : await invoke>('git_status', { projectId: params.projectId }); + const sessions = building ? readable([]) : await (await import('$lib/sessions')).default({ projectId: params.projectId }); @@ -66,6 +71,7 @@ export const load: LayoutLoad = async ({ parent, params }) => { }); }); + return dateSessions; }); } @@ -74,6 +80,7 @@ export const load: LayoutLoad = async ({ parent, params }) => { project: projects.get(params.projectId), projectId: params.projectId, sessions: orderedSessions, - dateSessions: dateSessions + dateSessions: dateSessions, + filesStatus: filesStatus }; }; diff --git a/src/routes/projects/[projectId]/+page.svelte b/src/routes/projects/[projectId]/+page.svelte index c41637d1f..33c3ec31f 100644 --- a/src/routes/projects/[projectId]/+page.svelte +++ b/src/routes/projects/[projectId]/+page.svelte @@ -4,6 +4,20 @@ export let data: LayoutData; $: project = data.project; $: dateSessions = data.dateSessions; + $: filesStatus = data.filesStatus; + + function shortPath(path, max = 3) { + if (path.length < 30) { + return path; + } + const pathParts = path.split('/'); + const file = pathParts.pop(); + if (pathParts.length > 0) { + const pp = pathParts.map((p) => p.slice(0, max)).join('/'); + return `${pp}/${file}`; + } + return file; + } // convert a list of timestamps to a sparkline function timestampsToSpark(tsArray) { @@ -138,8 +152,21 @@
-

Work in Progress

-
No uncommitted work
+

Work in Progress

+ {#if Object.entries(filesStatus).length == 0} +
Everything is committed
+ {:else} +
+
    + {#each Object.entries(filesStatus) as activity} +
  • + {activity[1].slice(0, 1)} + {shortPath(activity[0])} +
  • + {/each} +
+
+ {/if}