Skip to content

Commit 3191d5c

Browse files
committed
check if a is empty
1 parent f3f9b89 commit 3191d5c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ pub fn is_different<A: AsRef<Path>, B: AsRef<Path>>(a_base: A, b_base: B) -> Res
4040
let a_base = a_base.as_ref();
4141
let b_base = b_base.as_ref();
4242

43+
// We start by checking the count in the top-level directories. There's two
44+
// reasons for this: first, if a is empty, but b is not, our for loop will not
45+
// catch it; it will execute zero times and return true, and that's wrong!
46+
// Second, if they're differnet, there's no reason to bother comparing the
47+
// files; we already know they're different.
48+
49+
let a_count = std::fs::read_dir(&a_base)?.count();
50+
let b_count = std::fs::read_dir(&b_base)?.count();
51+
52+
if a_count != b_count {
53+
return Ok(true);
54+
}
55+
56+
// next, we walk all of the entries in a and compare them to b
4357
for entry in WalkDir::new(a_base) {
4458
let entry = entry?;
4559
let a = entry.path();
@@ -76,6 +90,8 @@ pub fn is_different<A: AsRef<Path>, B: AsRef<Path>>(a_base: A, b_base: B) -> Res
7690
return Ok(true);
7791
}
7892
}
93+
94+
// if we made it here, everything in a is in b!
7995
Ok(false)
8096
}
8197

0 commit comments

Comments
 (0)