repo_path: replace .contains() with .starts_with(), flipping the arguments

self.contains(other) means that the self tree contains the other tree (i.e.
the self path is prefix of the other), but it could be confused the other way
around if we were thinking about the path literal, not the tree. Let's add
.starts_with() instead by copying the std::path::Path definition.
This commit is contained in:
Yuya Nishihara 2023-11-27 13:27:35 +09:00
parent 266690a46b
commit 6ce7bd5338
3 changed files with 19 additions and 19 deletions

View file

@ -793,7 +793,7 @@ impl TreeState {
// tracking.
let tracked_paths = file_states
.range::<RepoPath, _>((Bound::Excluded(&*path), Bound::Unbounded))
.take_while(|(sub_path, _)| path.contains(sub_path))
.take_while(|(sub_path, _)| sub_path.starts_with(&path))
.map(|(sub_path, file_state)| (sub_path.clone(), file_state.clone()))
.collect_vec();
for (tracked_path, current_file_state) in tracked_paths {

View file

@ -984,9 +984,9 @@ impl Ord for DiffStreamKey {
fn cmp(&self, other: &Self) -> Ordering {
if self == other {
Ordering::Equal
} else if self.file_after_dir && self.path.contains(&other.path) {
} else if self.file_after_dir && other.path.starts_with(&self.path) {
Ordering::Greater
} else if other.file_after_dir && other.path.contains(&self.path) {
} else if other.file_after_dir && self.path.starts_with(&other.path) {
Ordering::Less
} else {
self.path.cmp(&other.path)

View file

@ -294,9 +294,9 @@ impl RepoPath {
self.value.is_empty()
}
// TODO: might be better to add .starts_with() instead
pub fn contains(&self, other: &RepoPath) -> bool {
other.strip_prefix(self).is_some()
/// Returns true if the `base` is a prefix of this path.
pub fn starts_with(&self, base: &RepoPath) -> bool {
self.strip_prefix(base).is_some()
}
/// Returns the remaining path with the `base` path removed.
@ -470,21 +470,21 @@ mod tests {
}
#[test]
fn test_contains() {
assert!(repo_path("").contains(repo_path("")));
assert!(repo_path("").contains(repo_path("x")));
assert!(!repo_path("x").contains(repo_path("")));
fn test_starts_with() {
assert!(repo_path("").starts_with(repo_path("")));
assert!(repo_path("x").starts_with(repo_path("")));
assert!(!repo_path("").starts_with(repo_path("x")));
assert!(repo_path("x").contains(repo_path("x")));
assert!(repo_path("x").contains(repo_path("x/y")));
assert!(!repo_path("x").contains(repo_path("xy")));
assert!(!repo_path("y").contains(repo_path("x/y")));
assert!(repo_path("x").starts_with(repo_path("x")));
assert!(repo_path("x/y").starts_with(repo_path("x")));
assert!(!repo_path("xy").starts_with(repo_path("x")));
assert!(!repo_path("x/y").starts_with(repo_path("y")));
assert!(repo_path("x/y").contains(repo_path("x/y")));
assert!(repo_path("x/y").contains(repo_path("x/y/z")));
assert!(!repo_path("x/y").contains(repo_path("x/yz")));
assert!(!repo_path("x/y").contains(repo_path("x")));
assert!(!repo_path("x/y").contains(repo_path("xy")));
assert!(repo_path("x/y").starts_with(repo_path("x/y")));
assert!(repo_path("x/y/z").starts_with(repo_path("x/y")));
assert!(!repo_path("x/yz").starts_with(repo_path("x/y")));
assert!(!repo_path("x").starts_with(repo_path("x/y")));
assert!(!repo_path("xy").starts_with(repo_path("x/y")));
}
#[test]