Skip to content

Commit 974d679

Browse files
committed
fix(ls): place dot dirs on top in version sorting
Also fixes the fact that version sorting had incorrect edge cases due to wrong name passing.
1 parent 63a5faa commit 974d679

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/uu/ls/src/ls.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,15 @@ impl<'a> PathData<'a> {
979979
PathDataDisplayName::Custom(ref cow) => cow,
980980
}
981981
}
982+
983+
fn file_name(&self) -> &OsStr {
984+
match self.display_name {
985+
PathDataDisplayName::SelfReferential => {
986+
self.p_buf.file_name().unwrap_or(self.p_buf.as_os_str())
987+
}
988+
PathDataDisplayName::Custom(ref cow) => cow,
989+
}
990+
}
982991
}
983992

984993
impl Colorable for PathData<'_> {
@@ -1477,8 +1486,8 @@ fn sort_entries(entries: &mut [PathData], config: &Config) {
14771486
Sort::Name => entries.sort_unstable_by(|a, b| a.display_name().cmp(b.display_name())),
14781487
Sort::Version => entries.sort_unstable_by(|a, b| {
14791488
version_cmp(
1480-
os_str_as_bytes_lossy(a.path().as_os_str()).as_ref(),
1481-
os_str_as_bytes_lossy(b.path().as_os_str()).as_ref(),
1489+
os_str_as_bytes_lossy(a.file_name()).as_ref(),
1490+
os_str_as_bytes_lossy(b.file_name()).as_ref(),
14821491
)
14831492
.then(a.path().cmp(b.path()))
14841493
}),

tests/by-util/test_ls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3661,7 +3661,7 @@ fn test_ls_version_sort() {
36613661
);
36623662

36633663
let result = scene.ucmd().arg("-a1v").succeeds();
3664-
expected.insert(expected.len() - 1, "..");
3664+
expected.insert(0, "..");
36653665
expected.insert(0, ".");
36663666
assert_eq!(
36673667
result.stdout_str().split('\n').collect::<Vec<_>>(),

0 commit comments

Comments
 (0)