annotate rust/hg-core/src/operations/find_root.rs @ 46445:ca3f73cc3cf4

rhg: Simplify CommandError based on its use Differential Revision: https://phab.mercurial-scm.org/D9905
author Simon Sapin <simon.sapin@octobus.net>
date Thu, 28 Jan 2021 19:13:55 +0100
parents 3e2d539d0d1a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
1 use std::path::{Path, PathBuf};
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
2
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
3 /// Error type for `find_root`
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
4 #[derive(Debug)]
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
5 pub enum FindRootError {
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
6 /// Root of the repository has not been found
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
7 /// Contains the current directory used by FindRoot
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
8 RootNotFound(PathBuf),
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
9 /// The current directory does not exists or permissions are insufficient
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
10 /// to get access to it
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
11 GetCurrentDirError(std::io::Error),
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
12 }
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
13
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
14 /// Find the root of the repository
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
15 /// by searching for a .hg directory in the process’ current directory and its
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
16 /// ancestors
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
17 pub fn find_root() -> Result<PathBuf, FindRootError> {
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
18 let current_dir = std::env::current_dir()
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
19 .map_err(|e| FindRootError::GetCurrentDirError(e))?;
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
20 Ok(find_root_from_path(&current_dir)?.into())
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
21 }
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
22
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
23 /// Find the root of the repository
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
24 /// by searching for a .hg directory in the given directory and its ancestors
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
25 pub fn find_root_from_path(start: &Path) -> Result<&Path, FindRootError> {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
26 if start.join(".hg").exists() {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
27 return Ok(start);
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
28 }
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
29 for ancestor in start.ancestors() {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
30 if ancestor.join(".hg").exists() {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
31 return Ok(ancestor);
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
32 }
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
33 }
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
34 Err(FindRootError::RootNotFound(start.into()))
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
35 }
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
36
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
37 #[cfg(test)]
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
38 mod tests {
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
39 use super::*;
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
40 use std::fs;
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
41 use tempfile;
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
42
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
43 #[test]
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
44 fn dot_hg_not_found() {
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
45 let tmp_dir = tempfile::tempdir().unwrap();
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
46 let path = tmp_dir.path();
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
47
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
48 let err = find_root_from_path(&path).unwrap_err();
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
49
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
50 // TODO do something better
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
51 assert!(match err {
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
52 FindRootError::RootNotFound(p) => p == path.to_path_buf(),
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
53 _ => false,
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
54 })
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
55 }
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
56
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
57 #[test]
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
58 fn dot_hg_in_current_path() {
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
59 let tmp_dir = tempfile::tempdir().unwrap();
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
60 let root = tmp_dir.path();
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
61 fs::create_dir_all(root.join(".hg")).unwrap();
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
62
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
63 let result = find_root_from_path(&root).unwrap();
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
64
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
65 assert_eq!(result, root)
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
66 }
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
67
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
68 #[test]
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
69 fn dot_hg_in_parent() {
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
70 let tmp_dir = tempfile::tempdir().unwrap();
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
71 let root = tmp_dir.path();
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
72 fs::create_dir_all(root.join(".hg")).unwrap();
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
73
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
74 let directory = root.join("some/nested/directory");
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 45441
diff changeset
75 let result = find_root_from_path(&directory).unwrap();
44980
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
76
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
77 assert_eq!(result, root)
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
78 }
5965efb609b6 hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
79 } /* tests */