Skip to content

Commit

Permalink
Git status updates off UI thread
Browse files Browse the repository at this point in the history
  • Loading branch information
R0nd committed Oct 21, 2021
1 parent 153c79a commit 7822f53
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ impl App {
self.push_tags_popup.update_git(ev)?;
self.pull_popup.update_git(ev);
self.select_branch_popup.update_git(ev)?;
self.commit.update_git(ev)?;
}

self.files_tab.update_async(ev);
Expand Down
31 changes: 26 additions & 5 deletions src/components/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use asyncgit::{
sync::{
self, get_config_string, CommitId, HookResult, RepoState,
},
CWD,
AsyncGitNotification, CWD,
};
use crossterm::event::Event;
use easy_cast::Cast;
Expand Down Expand Up @@ -44,6 +44,7 @@ pub struct CommitComponent {
git_branch_name: cached::BranchName,
commit_template: Option<String>,
theme: SharedTheme,
can_amend: bool,
}

const FIRST_LINE_LIMIT: usize = 50;
Expand All @@ -70,6 +71,7 @@ impl CommitComponent {
git_branch_name: cached::BranchName::new(CWD),
commit_template: None,
theme,
can_amend: false,
}
}

Expand All @@ -78,6 +80,25 @@ impl CommitComponent {
self.git_branch_name.lookup().ok();
}

///
pub fn update_git(
&mut self,
ev: AsyncGitNotification,
) -> Result<()> {
match ev {
AsyncGitNotification::Status => self.update_status()?,
_ => (),
}

Ok(())
}

fn update_status(&mut self) -> Result<()> {
self.can_amend = self.get_can_amend();

Ok(())
}

fn draw_branch_name<B: Backend>(&self, f: &mut Frame<B>) {
if let Some(name) = self.git_branch_name.last() {
let w = Paragraph::new(format!("{{{}}}", name))
Expand Down Expand Up @@ -227,7 +248,7 @@ impl CommitComponent {
!self.is_empty() && self.is_changed()
}

fn can_amend(&self) -> bool {
fn get_can_amend(&self) -> bool {
matches!(self.mode, Mode::Normal)
&& sync::get_head(CWD).is_ok()
&& (self.is_empty() || !self.is_changed())
Expand All @@ -243,7 +264,7 @@ impl CommitComponent {
}

fn amend(&mut self) -> Result<()> {
if self.can_amend() {
if self.can_amend {
let id = sync::get_head(CWD)?;
self.mode = Mode::Amend(id);

Expand Down Expand Up @@ -293,7 +314,7 @@ impl Component for CommitComponent {

out.push(CommandInfo::new(
strings::commands::commit_amend(&self.key_config),
self.can_amend(),
self.can_amend,
true,
));

Expand Down Expand Up @@ -323,7 +344,7 @@ impl Component for CommitComponent {
self.commit()
);
} else if e == self.key_config.commit_amend
&& self.can_amend()
&& self.can_amend
{
self.amend()?;
} else if e == self.key_config.open_commit_editor {
Expand Down
28 changes: 17 additions & 11 deletions src/tabs/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub struct Status {
git_action_executed: bool,
options: SharedOptions,
key_config: SharedKeyConfig,
pending_rebase: bool,
can_abort_merge: bool,
}

impl DrawableComponent for Status {
Expand Down Expand Up @@ -187,6 +189,8 @@ impl Status {
git_branch_name: cached::BranchName::new(CWD),
key_config,
options,
pending_rebase: Self::get_pending_rebase(),
can_abort_merge: Self::get_can_abort_merge(),
}
}

Expand Down Expand Up @@ -418,6 +422,9 @@ impl Status {

self.update_diff()?;

self.can_abort_merge = Self::get_can_abort_merge();
self.pending_rebase = Self::get_pending_rebase();

if self.git_action_executed {
self.git_action_executed = false;

Expand Down Expand Up @@ -558,12 +565,12 @@ impl Status {
.map_or(true, |state| state.ahead > 0)
}

fn can_abort_merge() -> bool {
fn get_can_abort_merge() -> bool {
sync::repo_state(CWD).unwrap_or(RepoState::Clean)
== RepoState::Merge
}

fn pending_rebase() -> bool {
fn get_pending_rebase() -> bool {
sync::repo_state(CWD).unwrap_or(RepoState::Clean)
== RepoState::Rebase
}
Expand Down Expand Up @@ -635,7 +642,7 @@ impl Status {
fn can_commit(&self) -> bool {
self.index.focused()
&& !self.index.is_empty()
&& !Self::pending_rebase()
&& !self.pending_rebase
}
}

Expand Down Expand Up @@ -692,25 +699,24 @@ impl Component for Status {
out.push(CommandInfo::new(
strings::commands::undo_commit(&self.key_config),
true,
(!Self::pending_rebase() && !focus_on_diff)
|| force_all,
(self.pending_rebase && !focus_on_diff) || force_all,
));

out.push(CommandInfo::new(
strings::commands::abort_merge(&self.key_config),
true,
Self::can_abort_merge() || force_all,
self.can_abort_merge || force_all,
));

out.push(CommandInfo::new(
strings::commands::continue_rebase(&self.key_config),
true,
Self::pending_rebase() || force_all,
self.pending_rebase || force_all,
));
out.push(CommandInfo::new(
strings::commands::abort_rebase(&self.key_config),
true,
Self::pending_rebase() || force_all,
self.pending_rebase || force_all,
));
}

Expand Down Expand Up @@ -817,23 +823,23 @@ impl Component for Status {
));
Ok(EventState::Consumed)
} else if k == self.key_config.abort_merge
&& Self::can_abort_merge()
&& self.can_abort_merge
{
self.queue.push(InternalEvent::ConfirmAction(
Action::AbortMerge,
));

Ok(EventState::Consumed)
} else if k == self.key_config.abort_merge
&& Self::pending_rebase()
&& self.pending_rebase
{
self.queue.push(InternalEvent::ConfirmAction(
Action::AbortRebase,
));

Ok(EventState::Consumed)
} else if k == self.key_config.rebase_branch
&& Self::pending_rebase()
&& self.pending_rebase
{
self.continue_rebase();
self.queue.push(InternalEvent::Update(
Expand Down

0 comments on commit 7822f53

Please sign in to comment.