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.
This commit is contained in:
Guillem L. Jara
2026-05-01 12:38:59 +02:00
committed by Daniel Hofstetter
parent e4a2ab3955
commit f774940451
2 changed files with 12 additions and 3 deletions
+11 -2
View File
@@ -979,6 +979,15 @@ impl<'a> PathData<'a> {
PathDataDisplayName::Custom(ref cow) => cow,
}
}
fn file_name(&self) -> &OsStr {
match self.display_name {
PathDataDisplayName::SelfReferential => {
self.p_buf.file_name().unwrap_or(self.p_buf.as_os_str())
}
PathDataDisplayName::Custom(ref cow) => cow,
}
}
}
impl Colorable for PathData<'_> {
@@ -1477,8 +1486,8 @@ fn sort_entries(entries: &mut [PathData], config: &Config) {
Sort::Name => entries.sort_unstable_by(|a, b| a.display_name().cmp(b.display_name())),
Sort::Version => entries.sort_unstable_by(|a, b| {
version_cmp(
os_str_as_bytes_lossy(a.path().as_os_str()).as_ref(),
os_str_as_bytes_lossy(b.path().as_os_str()).as_ref(),
os_str_as_bytes_lossy(a.file_name()).as_ref(),
os_str_as_bytes_lossy(b.file_name()).as_ref(),
)
.then(a.path().cmp(b.path()))
}),
+1 -1
View File
@@ -3661,7 +3661,7 @@ fn test_ls_version_sort() {
);
let result = scene.ucmd().arg("-a1v").succeeds();
expected.insert(expected.len() - 1, "..");
expected.insert(0, "..");
expected.insert(0, ".");
assert_eq!(
result.stdout_str().split('\n').collect::<Vec<_>>(),