Mercurial > public > mercurial-scm > hg
annotate rust/hg-core/src/operations/find_root.rs @ 46154:ecbb2fc9418c
copies-rust: rename Oracle.is_ancestor to Oracle.is_overwrite
The core information that we want here is about "does information from revision
X overwrite information in Y". To do so, we check is X is an ancestors of Y, but
this is an implementation details, they could be other ways. We update the
naming to clarify this (and align more with wording used in upcoming changesets.
For people curious about other ways: for example we could record the overwrite
graph as it happens and reuse that to check if X overwrite Y, without having to
do potential expensive `is_ancestor` call on the revision graph.
Differential Revision: https://phab.mercurial-scm.org/D9496
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Wed, 02 Dec 2020 16:11:35 +0100 |
parents | dca9cb99971c |
children | 3e2d539d0d1a |
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::fmt; |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
2 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
|
3 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
4 /// Kind of error encoutered by FindRoot |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
5 #[derive(Debug)] |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
6 pub enum FindRootErrorKind { |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
7 /// 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
|
8 /// 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
|
9 RootNotFound(PathBuf), |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
10 /// 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
|
11 /// 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
|
12 GetCurrentDirError(std::io::Error), |
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 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
15 /// A FindRoot error |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
16 #[derive(Debug)] |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
17 pub struct FindRootError { |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
18 /// Kind of error encoutered by FindRoot |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
19 pub kind: FindRootErrorKind, |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
20 } |
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 impl std::error::Error for FindRootError {} |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
23 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
24 impl fmt::Display for FindRootError { |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
25 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
26 unimplemented!() |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
27 } |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
28 } |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
29 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
30 /// 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
|
31 /// 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
|
32 /// ancestors |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
33 pub fn find_root() -> Result<PathBuf, FindRootError> { |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
34 let current_dir = std::env::current_dir().map_err(|e| FindRootError { |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
35 kind: FindRootErrorKind::GetCurrentDirError(e), |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
36 })?; |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
37 Ok(find_root_from_path(¤t_dir)?.into()) |
44980
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
38 } |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
39 |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
40 /// Find the root of the repository |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
41 /// 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
|
42 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
|
43 if start.join(".hg").exists() { |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
44 return Ok(start); |
44980
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
45 } |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
46 for ancestor in start.ancestors() { |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
47 if ancestor.join(".hg").exists() { |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
48 return Ok(ancestor); |
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 } |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
51 Err(FindRootError { |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
52 kind: FindRootErrorKind::RootNotFound(start.into()), |
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
53 }) |
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 #[cfg(test)] |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
57 mod tests { |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
58 use super::*; |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
59 use std::fs; |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
60 use tempfile; |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
61 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
62 #[test] |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
67 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
|
68 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
69 // TODO do something better |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
70 assert!(match err { |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
71 FindRootError { kind } => match kind { |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
72 FindRootErrorKind::RootNotFound(p) => p == path.to_path_buf(), |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
73 _ => false, |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
74 }, |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
75 }) |
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 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
78 #[test] |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 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
|
83 |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
84 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
|
85 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
86 assert_eq!(result, root) |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
87 } |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
88 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
89 #[test] |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
90 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
|
91 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
|
92 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
|
93 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
|
94 |
46135
dca9cb99971c
rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
45441
diff
changeset
|
95 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
|
96 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
|
97 |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
98 assert_eq!(result, root) |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
99 } |
5965efb609b6
hg-core: add FindRoot operation to find repository root path
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
100 } /* tests */ |