1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use {
super::TreeGitStatus,
crate::{
git,
task_sync::{Computation, ComputationResult, Dam},
},
crossbeam::channel::bounded,
git2::Repository,
std::{
collections::HashMap,
path::{Path, PathBuf},
sync::Mutex,
},
};
fn compute_tree_status(root_path: &Path) -> ComputationResult<TreeGitStatus> {
match Repository::open(root_path) {
Ok(git_repo) => {
let tree_git_status =
time!(Debug, "compute_tree_status", TreeGitStatus::from(&git_repo),);
match tree_git_status {
Some(gs) => ComputationResult::Done(gs),
None => ComputationResult::None,
}
}
Err(e) => {
debug!("failed to discover repo: {:?}", e);
ComputationResult::None
}
}
}
lazy_static! {
static ref TS_CACHE_MX: Mutex<HashMap<PathBuf, Computation<TreeGitStatus>>> =
Mutex::new(HashMap::new());
}
pub fn get_tree_status(root_path: &Path, dam: &mut Dam) -> ComputationResult<TreeGitStatus> {
match git::closest_repo_dir(root_path) {
None => ComputationResult::None,
Some(repo_path) => {
let comp = TS_CACHE_MX
.lock()
.unwrap()
.get(&repo_path)
.map(|c| (*c).clone());
match comp {
Some(Computation::Finished(comp_res)) => {
comp_res
}
Some(Computation::InProgress(comp_receiver)) => {
debug!("start select on in progress computation");
dam.select(comp_receiver)
}
None => {
let (s, r) = bounded(1);
TS_CACHE_MX
.lock()
.unwrap()
.insert(repo_path.clone(), Computation::InProgress(r));
dam.try_compute(move || {
let comp_res = compute_tree_status(&repo_path);
TS_CACHE_MX
.lock()
.unwrap()
.insert(repo_path.clone(), Computation::Finished(comp_res.clone()));
if let Err(e) = s.send(comp_res.clone()) {
debug!("error while sending comp result: {:?}", e);
}
comp_res
})
}
}
}
}
}
pub fn clear_status_computer_cache() {
let mut ts_cache = TS_CACHE_MX.lock().unwrap();
ts_cache.clear();
}