|
| 1 | +use crate::{ |
| 2 | + asyncjob::{AsyncJob, RunParams}, |
| 3 | + error::Result, |
| 4 | + sync::{self, CommitId, LogWalkerFilter, RepoPath}, |
| 5 | + AsyncGitNotification, ProgressPercent, |
| 6 | +}; |
| 7 | +use std::{ |
| 8 | + sync::{Arc, Mutex}, |
| 9 | + time::{Duration, Instant}, |
| 10 | +}; |
| 11 | + |
| 12 | +/// |
| 13 | +pub struct CommitFilterResult { |
| 14 | + /// |
| 15 | + pub result: Vec<CommitId>, |
| 16 | + /// |
| 17 | + pub duration: Duration, |
| 18 | +} |
| 19 | + |
| 20 | +enum JobState { |
| 21 | + Request { |
| 22 | + commits: Vec<CommitId>, |
| 23 | + repo_path: RepoPath, |
| 24 | + }, |
| 25 | + Response(Result<CommitFilterResult>), |
| 26 | +} |
| 27 | + |
| 28 | +/// |
| 29 | +#[derive(Clone)] |
| 30 | +pub struct AsyncCommitFilterJob { |
| 31 | + state: Arc<Mutex<Option<JobState>>>, |
| 32 | + filter: LogWalkerFilter, |
| 33 | +} |
| 34 | + |
| 35 | +/// |
| 36 | +impl AsyncCommitFilterJob { |
| 37 | + /// |
| 38 | + pub fn new( |
| 39 | + repo_path: RepoPath, |
| 40 | + commits: Vec<CommitId>, |
| 41 | + filter: LogWalkerFilter, |
| 42 | + ) -> Self { |
| 43 | + Self { |
| 44 | + state: Arc::new(Mutex::new(Some(JobState::Request { |
| 45 | + repo_path, |
| 46 | + commits, |
| 47 | + }))), |
| 48 | + filter, |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// |
| 53 | + pub fn result(&self) -> Option<Result<CommitFilterResult>> { |
| 54 | + if let Ok(mut state) = self.state.lock() { |
| 55 | + if let Some(state) = state.take() { |
| 56 | + return match state { |
| 57 | + JobState::Request { .. } => None, |
| 58 | + JobState::Response(result) => Some(result), |
| 59 | + }; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + None |
| 64 | + } |
| 65 | + |
| 66 | + fn run_request( |
| 67 | + &self, |
| 68 | + repo_path: &RepoPath, |
| 69 | + commits: Vec<CommitId>, |
| 70 | + params: &RunParams<AsyncGitNotification, ProgressPercent>, |
| 71 | + ) -> JobState { |
| 72 | + let response = sync::repo(repo_path) |
| 73 | + .map(|repo| self.filter_commits(&repo, commits, params)) |
| 74 | + .map(|(start, result)| CommitFilterResult { |
| 75 | + result, |
| 76 | + duration: start.elapsed(), |
| 77 | + }); |
| 78 | + |
| 79 | + JobState::Response(response) |
| 80 | + } |
| 81 | + |
| 82 | + fn filter_commits( |
| 83 | + &self, |
| 84 | + repo: &git2::Repository, |
| 85 | + commits: Vec<CommitId>, |
| 86 | + params: &RunParams<AsyncGitNotification, ProgressPercent>, |
| 87 | + ) -> (Instant, Vec<CommitId>) { |
| 88 | + let total_amount = commits.len(); |
| 89 | + let start = Instant::now(); |
| 90 | + |
| 91 | + let mut progress = ProgressPercent::new(0, total_amount); |
| 92 | + |
| 93 | + let result = commits |
| 94 | + .into_iter() |
| 95 | + .enumerate() |
| 96 | + .filter_map(|(idx, c)| { |
| 97 | + let new_progress = |
| 98 | + ProgressPercent::new(idx, total_amount); |
| 99 | + |
| 100 | + if new_progress != progress { |
| 101 | + Self::update_progress(params, new_progress); |
| 102 | + progress = new_progress; |
| 103 | + } |
| 104 | + |
| 105 | + (*self.filter)(repo, &c) |
| 106 | + .ok() |
| 107 | + .and_then(|res| res.then_some(c)) |
| 108 | + }) |
| 109 | + .collect::<Vec<_>>(); |
| 110 | + |
| 111 | + (start, result) |
| 112 | + } |
| 113 | + |
| 114 | + fn update_progress( |
| 115 | + params: &RunParams<AsyncGitNotification, ProgressPercent>, |
| 116 | + new_progress: ProgressPercent, |
| 117 | + ) { |
| 118 | + if let Err(e) = params.set_progress(new_progress) { |
| 119 | + log::error!("progress error: {e}"); |
| 120 | + } else if let Err(e) = |
| 121 | + params.send(AsyncGitNotification::CommitFilter) |
| 122 | + { |
| 123 | + log::error!("send error: {e}"); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl AsyncJob for AsyncCommitFilterJob { |
| 129 | + type Notification = AsyncGitNotification; |
| 130 | + type Progress = ProgressPercent; |
| 131 | + |
| 132 | + fn run( |
| 133 | + &mut self, |
| 134 | + params: RunParams<Self::Notification, Self::Progress>, |
| 135 | + ) -> Result<Self::Notification> { |
| 136 | + if let Ok(mut state) = self.state.lock() { |
| 137 | + *state = state.take().map(|state| match state { |
| 138 | + JobState::Request { commits, repo_path } => { |
| 139 | + self.run_request(&repo_path, commits, ¶ms) |
| 140 | + } |
| 141 | + JobState::Response(result) => { |
| 142 | + JobState::Response(result) |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + Ok(AsyncGitNotification::CommitFilter) |
| 148 | + } |
| 149 | +} |
0 commit comments