Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions asyncgit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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},
Expand Down Expand Up @@ -115,6 +117,8 @@ pub enum AsyncGitNotification {
///
PushTags,
///
StashSave,
///
Pull,
///
Blame,
Expand Down
95 changes: 95 additions & 0 deletions asyncgit/src/stash_save_job.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
include_untracked: bool,
keep_index: bool,
},
Response(Result<CommitId>),
}

/// 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<Mutex<Option<JobState>>>,
repo: RepoPath,
}

///
impl AsyncStashSaveJob {
///
pub fn new(
repo: RepoPath,
message: Option<String>,
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<Result<CommitId>> {
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<Self::Notification, Self::Progress>,
) -> Result<Self::Notification> {
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)
}
}
2 changes: 2 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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()
}
Expand Down
127 changes: 89 additions & 38 deletions src/popups/stashmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -20,6 +23,8 @@ pub struct StashMsgPopup {
input: TextInputComponent,
queue: Queue,
key_config: SharedKeyConfig,
async_stash: AsyncSingleJob<AsyncStashSaveJob>,
pending: bool,
}

impl DrawableComponent for StashMsgPopup {
Expand All @@ -43,7 +48,7 @@ impl Component for StashMsgPopup {
strings::commands::stashing_confirm_msg(
&self.key_config,
),
true,
!self.pending,
true,
));
}
Expand All @@ -53,48 +58,18 @@ impl Component for StashMsgPopup {

fn event(&mut self, ev: &Event) -> Result<EventState> {
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
Expand All @@ -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(())
Expand All @@ -134,11 +112,84 @@ 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,
}
}

///
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<asyncgit::sync::CommitId>,
) {
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
),
));
}
}
}
}
3 changes: 3 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down