| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use anyhow::{anyhow, bail, Result}; |
| 6 |
use arc_swap::ArcSwap; |
| 7 |
use std::{ |
| 8 |
path::{Path, PathBuf}, |
| 9 |
sync::Arc, |
| 10 |
}; |
| 11 |
|
| 12 |
#[cfg(feature = "git")] |
| 13 |
mod git; |
| 14 |
|
| 15 |
mod diff; |
| 16 |
|
| 17 |
pub use diff::{DiffHandle, Hunk}; |
| 18 |
|
| 19 |
mod status; |
| 20 |
|
| 21 |
pub use status::FileChange; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#[derive(Clone)] |
| 26 |
pub struct DiffProviderRegistry { |
| 27 |
providers: Vec<DiffProvider>, |
| 28 |
} |
| 29 |
|
| 30 |
impl DiffProviderRegistry { |
| 31 |
|
| 32 |
|
| 33 |
pub fn get_diff_base(&self, file: &Path, trust_full: bool) -> Option<Vec<u8>> { |
| 34 |
self.providers |
| 35 |
.iter() |
| 36 |
.find_map(|provider| match provider.get_diff_base(file, trust_full) { |
| 37 |
Ok(res) => Some(res), |
| 38 |
Err(err) => { |
| 39 |
log::debug!("{err:#?}"); |
| 40 |
log::debug!("failed to open diff base for {}", file.display()); |
| 41 |
None |
| 42 |
} |
| 43 |
}) |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
pub fn get_current_head_name( |
| 48 |
&self, |
| 49 |
file: &Path, |
| 50 |
trust_full: bool, |
| 51 |
) -> Option<Arc<ArcSwap<Box<str>>>> { |
| 52 |
self.providers.iter().find_map(|provider| { |
| 53 |
match provider.get_current_head_name(file, trust_full) { |
| 54 |
Ok(res) => Some(res), |
| 55 |
Err(err) => { |
| 56 |
log::debug!("{err:#?}"); |
| 57 |
log::debug!("failed to obtain current head name for {}", file.display()); |
| 58 |
None |
| 59 |
} |
| 60 |
} |
| 61 |
}) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
pub fn for_each_changed_file( |
| 67 |
self, |
| 68 |
cwd: PathBuf, |
| 69 |
trust_full: bool, |
| 70 |
f: impl Fn(Result<FileChange>) -> bool + Send + 'static, |
| 71 |
) { |
| 72 |
tokio::task::spawn_blocking(move || { |
| 73 |
if self |
| 74 |
.providers |
| 75 |
.iter() |
| 76 |
.find_map(|provider| provider.for_each_changed_file(&cwd, trust_full, &f).ok()) |
| 77 |
.is_none() |
| 78 |
{ |
| 79 |
f(Err(anyhow!("no diff provider returns success"))); |
| 80 |
} |
| 81 |
}); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl Default for DiffProviderRegistry { |
| 86 |
fn default() -> Self { |
| 87 |
|
| 88 |
|
| 89 |
let providers = vec![ |
| 90 |
#[cfg(feature = "git")] |
| 91 |
DiffProvider::Git, |
| 92 |
DiffProvider::None, |
| 93 |
]; |
| 94 |
DiffProviderRegistry { providers } |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
#[derive(Copy, Clone)] |
| 103 |
enum DiffProvider { |
| 104 |
#[cfg(feature = "git")] |
| 105 |
Git, |
| 106 |
None, |
| 107 |
} |
| 108 |
|
| 109 |
impl DiffProvider { |
| 110 |
fn get_diff_base(&self, file: &Path, trust_full: bool) -> Result<Vec<u8>> { |
| 111 |
match self { |
| 112 |
#[cfg(feature = "git")] |
| 113 |
Self::Git => git::get_diff_base(file, trust_full), |
| 114 |
Self::None => bail!("No diff support compiled in"), |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
fn get_current_head_name( |
| 119 |
&self, |
| 120 |
file: &Path, |
| 121 |
trust_full: bool, |
| 122 |
) -> Result<Arc<ArcSwap<Box<str>>>> { |
| 123 |
match self { |
| 124 |
#[cfg(feature = "git")] |
| 125 |
Self::Git => git::get_current_head_name(file, trust_full), |
| 126 |
Self::None => bail!("No diff support compiled in"), |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
fn for_each_changed_file( |
| 131 |
&self, |
| 132 |
cwd: &Path, |
| 133 |
trust_full: bool, |
| 134 |
f: impl Fn(Result<FileChange>) -> bool, |
| 135 |
) -> Result<()> { |
| 136 |
match self { |
| 137 |
#[cfg(feature = "git")] |
| 138 |
Self::Git => git::for_each_changed_file(cwd, trust_full, f), |
| 139 |
Self::None => bail!("No diff support compiled in"), |
| 140 |
} |
| 141 |
} |
| 142 |
} |