worktree_settings.rs

  1use std::path::Path;
  2
  3use anyhow::Context as _;
  4use settings::{RegisterSetting, Settings};
  5use util::{
  6    ResultExt,
  7    paths::{PathMatcher, PathStyle},
  8    rel_path::RelPath,
  9};
 10
 11#[derive(Clone, PartialEq, Eq, RegisterSetting)]
 12pub struct WorktreeSettings {
 13    pub project_name: Option<String>,
 14    /// Whether to prevent this project from being shared in public channels.
 15    pub prevent_sharing_in_public_channels: bool,
 16    pub file_scan_exclusions: PathMatcher,
 17    pub file_scan_inclusions: PathMatcher,
 18    /// This field contains all ancestors of the `file_scan_inclusions`. It's used to
 19    /// determine whether to terminate worktree scanning for a given dir.
 20    pub parent_dir_scan_inclusions: PathMatcher,
 21    pub private_files: PathMatcher,
 22    pub hidden_files: PathMatcher,
 23    pub read_only_files: PathMatcher,
 24}
 25
 26impl WorktreeSettings {
 27    pub fn is_path_private(&self, path: &RelPath) -> bool {
 28        path.ancestors()
 29            .any(|ancestor| self.private_files.is_match(ancestor))
 30    }
 31
 32    pub fn is_path_excluded(&self, path: &RelPath) -> bool {
 33        path.ancestors()
 34            .any(|ancestor| self.file_scan_exclusions.is_match(ancestor))
 35    }
 36
 37    pub fn is_path_always_included(&self, path: &RelPath, is_dir: bool) -> bool {
 38        if is_dir {
 39            self.parent_dir_scan_inclusions.is_match(path)
 40        } else {
 41            self.file_scan_inclusions.is_match(path)
 42        }
 43    }
 44
 45    pub fn is_path_hidden(&self, path: &RelPath) -> bool {
 46        path.ancestors()
 47            .any(|ancestor| self.hidden_files.is_match(ancestor))
 48    }
 49
 50    pub fn is_path_read_only(&self, path: &RelPath) -> bool {
 51        self.read_only_files.is_match(path)
 52    }
 53
 54    pub fn is_std_path_read_only(&self, path: &Path) -> bool {
 55        self.read_only_files.is_match_std_path(path)
 56    }
 57}
 58
 59impl Settings for WorktreeSettings {
 60    fn from_settings(content: &settings::SettingsContent) -> Self {
 61        let worktree = content.project.worktree.clone();
 62        let file_scan_exclusions = worktree.file_scan_exclusions.unwrap();
 63        let file_scan_inclusions = worktree.file_scan_inclusions.unwrap();
 64        let private_files = worktree.private_files.unwrap().0;
 65        let hidden_files = worktree.hidden_files.unwrap();
 66        let read_only_files = worktree.read_only_files.unwrap_or_default();
 67        let parsed_file_scan_inclusions: Vec<String> = file_scan_inclusions
 68            .iter()
 69            .flat_map(|glob| {
 70                Path::new(glob)
 71                    .ancestors()
 72                    .skip(1)
 73                    .map(|a| a.to_string_lossy().into())
 74            })
 75            .filter(|p: &String| !p.is_empty())
 76            .collect();
 77
 78        Self {
 79            project_name: worktree.project_name,
 80            prevent_sharing_in_public_channels: worktree.prevent_sharing_in_public_channels,
 81            file_scan_exclusions: path_matchers(file_scan_exclusions, "file_scan_exclusions")
 82                .log_err()
 83                .unwrap_or_default(),
 84            parent_dir_scan_inclusions: path_matchers(
 85                parsed_file_scan_inclusions,
 86                "file_scan_inclusions",
 87            )
 88            .unwrap(),
 89            file_scan_inclusions: path_matchers(file_scan_inclusions, "file_scan_inclusions")
 90                .unwrap(),
 91            private_files: path_matchers(private_files, "private_files")
 92                .log_err()
 93                .unwrap_or_default(),
 94            hidden_files: path_matchers(hidden_files, "hidden_files")
 95                .log_err()
 96                .unwrap_or_default(),
 97            read_only_files: path_matchers(read_only_files, "read_only_files")
 98                .log_err()
 99                .unwrap_or_default(),
100        }
101    }
102}
103
104fn path_matchers(mut values: Vec<String>, context: &'static str) -> anyhow::Result<PathMatcher> {
105    values.sort();
106    PathMatcher::new(values, PathStyle::local())
107        .with_context(|| format!("Failed to parse globs from {}", context))
108}