Show More
@@ -1,119 +1,119 b'' | |||
|
1 | 1 | use std::fmt; |
|
2 | 2 | use std::path::{Path, PathBuf}; |
|
3 | 3 | |
|
4 | 4 | /// Kind of error encoutered by FindRoot |
|
5 | 5 | #[derive(Debug)] |
|
6 | 6 | pub enum FindRootErrorKind { |
|
7 | 7 | /// Root of the repository has not been found |
|
8 | 8 | /// Contains the current directory used by FindRoot |
|
9 | 9 | RootNotFound(PathBuf), |
|
10 | 10 | /// The current directory does not exists or permissions are insufficient |
|
11 | 11 | /// to get access to it |
|
12 | 12 | GetCurrentDirError(std::io::Error), |
|
13 | 13 | } |
|
14 | 14 | |
|
15 | 15 | /// A FindRoot error |
|
16 | 16 | #[derive(Debug)] |
|
17 | 17 | pub struct FindRootError { |
|
18 | 18 | /// Kind of error encoutered by FindRoot |
|
19 | 19 | pub kind: FindRootErrorKind, |
|
20 | 20 | } |
|
21 | 21 | |
|
22 | 22 | impl std::error::Error for FindRootError {} |
|
23 | 23 | |
|
24 | 24 | impl fmt::Display for FindRootError { |
|
25 | 25 | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
26 | 26 | unimplemented!() |
|
27 | 27 | } |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | /// Find the root of the repository |
|
31 | 31 | /// by searching for a .hg directory in the current directory and its |
|
32 | 32 | /// ancestors |
|
33 | 33 | pub struct FindRoot<'a> { |
|
34 | 34 | current_dir: Option<&'a Path>, |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | impl<'a> FindRoot<'a> { |
|
38 | 38 | pub fn new() -> Self { |
|
39 | 39 | Self { current_dir: None } |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | pub fn new_from_path(current_dir: &'a Path) -> Self { |
|
43 | 43 | Self { |
|
44 | 44 | current_dir: Some(current_dir), |
|
45 | 45 | } |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | pub fn run(&self) -> Result<PathBuf, FindRootError> { |
|
49 | 49 | let current_dir = match self.current_dir { |
|
50 | 50 | None => std::env::current_dir().or_else(|e| { |
|
51 | 51 | Err(FindRootError { |
|
52 | 52 | kind: FindRootErrorKind::GetCurrentDirError(e), |
|
53 | 53 | }) |
|
54 | 54 | })?, |
|
55 | 55 | Some(path) => path.into(), |
|
56 | 56 | }; |
|
57 | 57 | |
|
58 | 58 | if current_dir.join(".hg").exists() { |
|
59 |
return Ok(current_dir |
|
|
59 | return Ok(current_dir); | |
|
60 | 60 | } |
|
61 |
let |
|
|
62 |
|
|
|
61 | let ancestors = current_dir.ancestors(); | |
|
62 | for parent in ancestors { | |
|
63 | 63 | if parent.join(".hg").exists() { |
|
64 | 64 | return Ok(parent.into()); |
|
65 | 65 | } |
|
66 | 66 | } |
|
67 | 67 | Err(FindRootError { |
|
68 | 68 | kind: FindRootErrorKind::RootNotFound(current_dir.to_path_buf()), |
|
69 | 69 | }) |
|
70 | 70 | } |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | #[cfg(test)] |
|
74 | 74 | mod tests { |
|
75 | 75 | use super::*; |
|
76 | 76 | use std::fs; |
|
77 | 77 | use tempfile; |
|
78 | 78 | |
|
79 | 79 | #[test] |
|
80 | 80 | fn dot_hg_not_found() { |
|
81 | 81 | let tmp_dir = tempfile::tempdir().unwrap(); |
|
82 | 82 | let path = tmp_dir.path(); |
|
83 | 83 | |
|
84 | 84 | let err = FindRoot::new_from_path(&path).run().unwrap_err(); |
|
85 | 85 | |
|
86 | 86 | // TODO do something better |
|
87 | 87 | assert!(match err { |
|
88 | 88 | FindRootError { kind } => match kind { |
|
89 | 89 | FindRootErrorKind::RootNotFound(p) => p == path.to_path_buf(), |
|
90 | 90 | _ => false, |
|
91 | 91 | }, |
|
92 | 92 | }) |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | #[test] |
|
96 | 96 | fn dot_hg_in_current_path() { |
|
97 | 97 | let tmp_dir = tempfile::tempdir().unwrap(); |
|
98 | 98 | let root = tmp_dir.path(); |
|
99 | 99 | fs::create_dir_all(root.join(".hg")).unwrap(); |
|
100 | 100 | |
|
101 | 101 | let result = FindRoot::new_from_path(&root).run().unwrap(); |
|
102 | 102 | |
|
103 | 103 | assert_eq!(result, root) |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | #[test] |
|
107 | 107 | fn dot_hg_in_parent() { |
|
108 | 108 | let tmp_dir = tempfile::tempdir().unwrap(); |
|
109 | 109 | let root = tmp_dir.path(); |
|
110 | 110 | fs::create_dir_all(root.join(".hg")).unwrap(); |
|
111 | 111 | |
|
112 | 112 | let result = |
|
113 | 113 | FindRoot::new_from_path(&root.join("some/nested/directory")) |
|
114 | 114 | .run() |
|
115 | 115 | .unwrap(); |
|
116 | 116 | |
|
117 | 117 | assert_eq!(result, root) |
|
118 | 118 | } |
|
119 | 119 | } /* tests */ |
General Comments 0
You need to be logged in to leave comments.
Login now