ex_cli/git/
cache.rs

1use crate::error::MyResult;
2use crate::git::cache::TriOption::*;
3use crate::git::flags::GitFlags;
4use crate::git::repo::GitRepo;
5use std::cell::RefCell;
6use std::collections::HashMap;
7use std::ops::DerefMut;
8use std::path::{Path, PathBuf};
9use std::rc::Rc;
10
11enum TriOption<T> {
12    Yes(T),
13    No,
14    Maybe,
15}
16
17type GitRepoMap = HashMap<PathBuf, Option<Rc<RefCell<GitRepo>>>>;
18
19pub struct GitCache {
20    flags: GitFlags,
21    repos: RefCell<GitRepoMap>,
22}
23
24impl GitCache {
25    pub fn new(flags: GitFlags) -> Self {
26        let repos = RefCell::new(HashMap::new());
27        Self { flags, repos }
28    }
29
30    pub fn test_ignored(&self, path: &Path) -> bool {
31        if let Some(parent) = path.parent() {
32            if let Some(repo) = self.find_repository(parent) {
33                let repo = repo.borrow();
34                return repo.test_ignored(path).unwrap_or(true);
35            }
36        }
37        true
38    }
39
40    pub fn test_allowed(&self, path: &Path) -> MyResult<Option<GitFlags>> {
41        if let Some(parent) = path.parent() {
42            if let Some(repo) = self.find_repository(parent) {
43                let mut repo = repo.borrow_mut();
44                let result = repo.test_allowed(&self.flags, path)?;
45                return Ok(result);
46            }
47        }
48        if self.flags.untracked {
49            let result = GitFlags::new().with_untracked(true);
50            return Ok(Some(result));
51        }
52        Ok(None)
53    }
54
55    fn find_repository(&self, path: &Path) -> Option<Rc<RefCell<GitRepo>>> {
56        let mut repos = self.repos.borrow_mut();
57        match Self::find_recursive(repos.deref_mut(), path) {
58            Yes(repo) => {
59                Some(repo)
60            }
61            No => {
62                None
63            }
64            Maybe => {
65                if let Some(repo) = GitRepo::open_repository(path, repos.keys()) {
66                    let root = repo.get_root().to_path_buf();
67                    let repo = Rc::new(RefCell::new(repo));
68                    repos.insert(root, Some(Rc::clone(&repo)));
69                    Some(repo)
70                } else {
71                    let path = PathBuf::from(path);
72                    repos.insert(path, None);
73                    None
74                }
75            }
76        }
77    }
78
79    fn find_recursive(repos: &GitRepoMap, path: &Path) -> TriOption<Rc<RefCell<GitRepo>>> {
80        if let Some(repo) = repos.get(path) {
81            if let Some(repo) = repo {
82                Yes(Rc::clone(repo))
83            } else {
84                No
85            }
86        } else if let Some(parent) = path.parent() {
87            if let Yes(repo) = Self::find_recursive(repos, parent) {
88                Yes(repo)
89            } else {
90                Maybe
91            }
92        } else {
93            Maybe
94        }
95    }
96}