Debian Codegraph @

helix-vcs/src/lib.rs

raw symbols SCIP
1 //! `helix_vcs` provides types for working with diffs from a Version Control System (VCS).
2 //! Currently `git` is the only supported provider for diffs, but this architecture allows
3 //! for other providers to be added in the future.
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 /// Contains all active diff providers. Diff providers are compiled in via features. Currently
24 /// only `git` is supported.
25 #[derive(Clone)]
26 pub struct DiffProviderRegistry {
27 providers: Vec<DiffProvider>,
28 }
29
30 impl DiffProviderRegistry {
31 /// Get the given file from the VCS. This provides the unedited document as a "base"
32 /// for a diff to be created.
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 /// Get the current name of the current [HEAD](https://stackoverflow.com/questions/2304087/what-is-head-in-git).
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 /// Fire-and-forget changed file iteration. Runs everything in a background task. Keeps
65 /// iteration until `on_change` returns `false`.
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 // currently only git is supported
88 // TODO make this configurable when more providers are added
89 let providers = vec![
90 #[cfg(feature = "git")]
91 DiffProvider::Git,
92 DiffProvider::None,
93 ];
94 DiffProviderRegistry { providers }
95 }
96 }
97
98 /// A union type that includes all types that implement [DiffProvider]. We need this type to allow
99 /// cloning [DiffProviderRegistry] as `Clone` cannot be used in trait objects.
100 ///
101 /// `Copy` is simply to ensure the `clone()` call is the simplest it can be.
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 }