From d38ede333959771b2f915cdf432555eb151874a3 Mon Sep 17 00:00:00 2001 From: ghostmikz Date: Wed, 18 Nov 2026 11:08:17 +0800 Subject: [PATCH] feat: show "Stashing..." indicator and run stash asynchronously Stashing in a large repository could freeze the UI for several seconds with no feedback. Move the blocking stash_save into a background AsyncStashSaveJob (mirroring the existing fetch/pull jobs) and show a "Stashing..." title in the stash popup while it runs. Closes #1495 --- CHANGELOG.md | 1 + asyncgit/src/lib.rs | 4 ++ asyncgit/src/stash_save_job.rs | 95 ++++++++++++++++++++++++ src/app.rs | 2 + src/popups/stashmsg.rs | 127 +++++++++++++++++++++++---------- src/strings.rs | 3 + 6 files changed, 194 insertions(+), 38 deletions(-) create mode 100644 asyncgit/src/stash_save_job.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index a1ca0eaac2..6f3bca8390 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * support x509 commit signing [[@kaden-l-nelson](https://github.com/kaden-l-nelson)] ([#2514](https://github.com/gitui-org/gitui/issues/2514)) +* show a "Stashing..." indicator while a stash is being saved, running the stash asynchronously [[@ghostmikz](https://github.com/ghostmikz)] ([#1495](https://github.com/gitui-org/gitui/issues/1495)) ### Changed * use [tombi](https://github.com/tombi-toml/tombi) for all toml file formatting diff --git a/asyncgit/src/lib.rs b/asyncgit/src/lib.rs index 98cc7238f9..42c1d068a6 100644 --- a/asyncgit/src/lib.rs +++ b/asyncgit/src/lib.rs @@ -59,6 +59,7 @@ mod push_tags; pub mod remote_progress; pub mod remote_tags; mod revlog; +mod stash_save_job; mod status; pub mod sync; mod tags; @@ -78,6 +79,7 @@ pub use crate::{ push_tags::{AsyncPushTags, PushTagsRequest}, remote_progress::{RemoteProgress, RemoteProgressState}, revlog::{AsyncLog, FetchStatus}, + stash_save_job::AsyncStashSaveJob, status::{AsyncStatus, StatusParams}, sync::{ diff::{DiffLine, DiffLineType, FileDiff}, @@ -115,6 +117,8 @@ pub enum AsyncGitNotification { /// PushTags, /// + StashSave, + /// Pull, /// Blame, diff --git a/asyncgit/src/stash_save_job.rs b/asyncgit/src/stash_save_job.rs new file mode 100644 index 0000000000..858cb0e873 --- /dev/null +++ b/asyncgit/src/stash_save_job.rs @@ -0,0 +1,95 @@ +//! + +use crate::{ + asyncjob::{AsyncJob, RunParams}, + error::Result, + sync::{stash_save, CommitId, RepoPath}, + AsyncGitNotification, +}; + +use std::sync::{Arc, Mutex}; + +enum JobState { + Request { + message: Option, + include_untracked: bool, + keep_index: bool, + }, + Response(Result), +} + +/// Runs a (potentially slow) `stash_save` on a background thread so the UI +/// stays responsive and can show that stashing is in progress. +#[derive(Clone)] +pub struct AsyncStashSaveJob { + state: Arc>>, + repo: RepoPath, +} + +/// +impl AsyncStashSaveJob { + /// + pub fn new( + repo: RepoPath, + message: Option, + include_untracked: bool, + keep_index: bool, + ) -> Self { + Self { + repo, + state: Arc::new(Mutex::new(Some(JobState::Request { + message, + include_untracked, + keep_index, + }))), + } + } + + /// Returns the result of the stash operation once the job has finished. + pub fn result(&self) -> Option> { + if let Ok(mut state) = self.state.lock() { + if let Some(state) = state.take() { + return match state { + JobState::Request { .. } => None, + JobState::Response(result) => Some(result), + }; + } + } + + None + } +} + +impl AsyncJob for AsyncStashSaveJob { + type Notification = AsyncGitNotification; + type Progress = (); + + fn run( + &mut self, + _params: RunParams, + ) -> Result { + if let Ok(mut state) = self.state.lock() { + *state = state.take().map(|state| match state { + JobState::Request { + message, + include_untracked, + keep_index, + } => { + let result = stash_save( + &self.repo, + message.as_deref(), + include_untracked, + keep_index, + ); + + JobState::Response(result) + } + JobState::Response(result) => { + JobState::Response(result) + } + }); + } + + Ok(AsyncGitNotification::StashSave) + } +} diff --git a/src/app.rs b/src/app.rs index ddb157af3f..b7cfeb427d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -426,6 +426,7 @@ impl App { if let AsyncNotification::Git(ev) = ev { self.status_tab.update_git(ev)?; self.stashing_tab.update_git(ev)?; + self.stashmsg_popup.update_git(ev); self.revlog.update_git(ev)?; self.file_revlog_popup.update_git(ev)?; self.inspect_commit_popup.update_git(ev)?; @@ -475,6 +476,7 @@ impl App { || self.push_tags_popup.any_work_pending() || self.pull_popup.any_work_pending() || self.fetch_popup.any_work_pending() + || self.stashmsg_popup.any_work_pending() || self.revision_files_popup.any_work_pending() || self.tags_popup.any_work_pending() } diff --git a/src/popups/stashmsg.rs b/src/popups/stashmsg.rs index f742106441..896597b4c1 100644 --- a/src/popups/stashmsg.rs +++ b/src/popups/stashmsg.rs @@ -10,7 +10,10 @@ use crate::{ tabs::StashingOptions, }; use anyhow::Result; -use asyncgit::sync::{self, RepoPathRef}; +use asyncgit::{ + asyncjob::AsyncSingleJob, sync::RepoPathRef, + AsyncGitNotification, AsyncStashSaveJob, +}; use crossterm::event::Event; use ratatui::{layout::Rect, Frame}; @@ -20,6 +23,8 @@ pub struct StashMsgPopup { input: TextInputComponent, queue: Queue, key_config: SharedKeyConfig, + async_stash: AsyncSingleJob, + pending: bool, } impl DrawableComponent for StashMsgPopup { @@ -43,7 +48,7 @@ impl Component for StashMsgPopup { strings::commands::stashing_confirm_msg( &self.key_config, ), - true, + !self.pending, true, )); } @@ -53,48 +58,18 @@ impl Component for StashMsgPopup { fn event(&mut self, ev: &Event) -> Result { if self.is_visible() { + // ignore any input while the stash is being saved + if self.pending { + return Ok(EventState::Consumed); + } + if self.input.event(ev)?.is_consumed() { return Ok(EventState::Consumed); } if let Event::Key(e) = ev { if key_match(e, self.key_config.keys.enter) { - let result = sync::stash_save( - &self.repo.borrow(), - if self.input.get_text().is_empty() { - None - } else { - Some(self.input.get_text()) - }, - self.options.stash_untracked, - self.options.keep_index, - ); - match result { - Ok(_) => { - self.input.clear(); - self.hide(); - - self.queue.push( - InternalEvent::TabSwitch( - AppTabs::Stashlist, - ), - ); - } - Err(e) => { - self.hide(); - log::error!( - "e: {} (options: {:?})", - e, - self.options - ); - self.queue.push( - InternalEvent::ShowErrorMsg(format!( - "stash error:\n{}\noptions:\n{:?}", - e, self.options - )), - ); - } - } + self.stash(); } // stop key event propagation @@ -113,6 +88,9 @@ impl Component for StashMsgPopup { } fn show(&mut self) -> Result<()> { + // reset the title in case a previous stash left it as "Stashing..." + self.input + .set_title(strings::stash_popup_title(&self.key_config)); self.input.show()?; Ok(()) @@ -134,6 +112,8 @@ impl StashMsgPopup { .with_input_type(InputType::Singleline), key_config: env.key_config.clone(), repo: env.repo.clone(), + async_stash: AsyncSingleJob::new(env.sender_git.clone()), + pending: false, } } @@ -141,4 +121,75 @@ impl StashMsgPopup { pub const fn options(&mut self, options: StashingOptions) { self.options = options; } + + /// + pub const fn any_work_pending(&self) -> bool { + self.pending + } + + /// + pub fn update_git(&mut self, ev: AsyncGitNotification) { + if ev == AsyncGitNotification::StashSave { + self.pending = self.async_stash.is_pending(); + + if !self.pending { + if let Some(job) = self.async_stash.take_last() { + if let Some(result) = job.result() { + self.handle_result(&result); + } + } + } + } + } + + /// spawn the stash as a background job and show that it is in progress + fn stash(&mut self) { + let message = if self.input.get_text().is_empty() { + None + } else { + Some(self.input.get_text().to_string()) + }; + + self.pending = true; + self.input.set_title(strings::stash_popup_stashing( + &self.key_config, + )); + + self.async_stash.spawn(AsyncStashSaveJob::new( + self.repo.borrow().clone(), + message, + self.options.stash_untracked, + self.options.keep_index, + )); + } + + fn handle_result( + &mut self, + result: &asyncgit::Result, + ) { + match result { + Ok(_) => { + self.input.clear(); + self.hide(); + + self.queue.push(InternalEvent::TabSwitch( + AppTabs::Stashlist, + )); + } + Err(e) => { + self.hide(); + log::error!( + "stash error: {} (options: {:?})", + e, + self.options + ); + self.queue.push(InternalEvent::ShowErrorMsg( + format!( + "stash error:\n{}\noptions:\n{:?}", + e, self.options + ), + )); + } + } + } } diff --git a/src/strings.rs b/src/strings.rs index 93496cd2ca..4267032556 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -143,6 +143,9 @@ pub fn stash_popup_title(_key_config: &SharedKeyConfig) -> String { pub fn stash_popup_msg(_key_config: &SharedKeyConfig) -> String { "type name (optional)".to_string() } +pub fn stash_popup_stashing(_key_config: &SharedKeyConfig) -> String { + "Stashing...".to_string() +} pub fn confirm_title_reset() -> String { "Reset".to_string() }