diff --git a/asyncgit/src/treefiles.rs b/asyncgit/src/treefiles.rs index 9dd798f0..434c9c24 100644 --- a/asyncgit/src/treefiles.rs +++ b/asyncgit/src/treefiles.rs @@ -6,9 +6,17 @@ use crate::{ }; use std::sync::{Arc, Mutex}; +/// +pub struct FileTreeResult { + /// + pub commit: CommitId, + /// + pub result: Result>, +} + enum JobState { Request { commit: CommitId, repo: RepoPath }, - Response(Result>), + Response(FileTreeResult), } /// @@ -30,7 +38,7 @@ impl AsyncTreeFilesJob { } /// - pub fn result(&self) -> Option>> { + pub fn result(&self) -> Option { if let Ok(mut state) = self.state.lock() { if let Some(state) = state.take() { return match state { @@ -60,7 +68,10 @@ impl AsyncJob for AsyncTreeFilesJob { std::thread::sleep( std::time::Duration::from_secs(2), ); - JobState::Response(files) + JobState::Response(FileTreeResult { + commit, + result: files, + }) } JobState::Response(result) => { JobState::Response(result) diff --git a/src/components/revision_files.rs b/src/components/revision_files.rs index 691fba37..ac5ff44d 100644 --- a/src/components/revision_files.rs +++ b/src/components/revision_files.rs @@ -99,10 +99,7 @@ impl RevisionFilesComponent { if !same_id { self.files = None; - self.async_treefiles.spawn(AsyncTreeFilesJob::new( - self.repo.borrow().clone(), - commit, - )); + self.request_files(commit); self.revision = Some(get_commit_info(&self.repo.borrow(), &commit)?); @@ -129,17 +126,36 @@ impl RevisionFilesComponent { ev, AsyncNotification::Git(AsyncGitNotification::TreeFiles) ) { - if let Some(last) = self.async_treefiles.take_last() { - if let Some(Ok(last)) = last.result() { - let filenames: Vec<&Path> = last - .iter() - .map(|f| f.path.as_path()) - .collect(); - self.tree = - FileTree::new(&filenames, &BTreeSet::new())?; - self.tree.collapse_but_root(); + self.refresh_files()?; + } - self.files = Some(last); + Ok(()) + } + + fn refresh_files(&mut self) -> Result<(), anyhow::Error> { + if let Some(last) = self.async_treefiles.take_last() { + if let Some(result) = last.result() { + if self + .revision + .as_ref() + .map(|commit| commit.id == result.commit) + .unwrap_or_default() + { + if let Ok(last) = result.result { + let filenames: Vec<&Path> = last + .iter() + .map(|f| f.path.as_path()) + .collect(); + self.tree = FileTree::new( + &filenames, + &BTreeSet::new(), + )?; + self.tree.collapse_but_root(); + + self.files = Some(last); + } + } else if let Some(rev) = &self.revision { + self.request_files(rev.id); } } } @@ -364,6 +380,13 @@ impl RevisionFilesComponent { title } + + fn request_files(&mut self, commit: CommitId) { + self.async_treefiles.spawn(AsyncTreeFilesJob::new( + self.repo.borrow().clone(), + commit, + )); + } } impl DrawableComponent for RevisionFilesComponent { @@ -514,6 +537,7 @@ impl Component for RevisionFilesComponent { fn show(&mut self) -> Result<()> { self.visible = true; + self.refresh_files()?; Ok(()) } }