Repository navigation
fix(ls): keep the child's order when a sort key is given - #4506
Open
houtinghang wants to merge 1 commit into
Open
houtinghang wants to merge 1 commit into
houtinghang wants to merge 1 commit into
Conversation
`rtk ls` always regrouped the listing with directories first, even when the user had asked `ls` for a particular order. `rtk ls -t`, `-lt` and `-S` therefore printed every directory ahead of every file, so an agent running `ls -t | head -1` to find the newest file got a directory, or an older file, instead. The Claude Code hook rewrites `ls -lt` into `rtk ls -lt`, so this changed the meaning of a command the agent never chose to run. Record which entries are directories while reading the child's output, and only partition them to the front when no sort key was given. With `-t`, `-S`, `-U`, `-X`, `-v`, `-f` or GNU `--sort=WORD` the child already ordered both kinds together, and that order is the answer, so it is kept as-is (BSD's `-U`, `-X` and `-v` are not sort keys and keep the grouping). Plain `-l`, `-a` and `-r` still group directories first as before. The display cap now also drops the tail of the sorted order rather than the tail of the files, so the newest entries survive truncation. Fixes rtk-ai#4302
📊 Automated PR Analysis
SummaryFixes Review Checklist
Linked issues: #4302 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
rtk ls -t,-ltand-Slist every directory before every file, so the first line is no longer the newest (or largest) entry (#4302).compact_lsalways split the child's output into adirslist and afileslist and printed them in that order, whatever the user had askedlsto sort by. An agent that runsls -t | head -1to find the newest file gets a directory or an older file, and the Claude Code hook rewritesls -ltintortk ls -lt, so the agent never chose to run the rewritten command.-t,-S,-U,-X,-v,-for GNU--sort=WORD(abbreviations resolved through the existing grammar, so--sor timecounts), the child already ordered both kinds together and that order is kept as-is. On BSD only-t,-Sand-fare sort keys (-Upicks the timestamp-tsorts by;-vand-Xmean something else), so the grouping stays there.-l,-aand-rbehave exactly as before: directories first, then files.-c/-u/--time=WORDwithout-lor-t, which GNU ls sorts by on its own. RTK always hands the child-l, which makes GNU sort by name again, so those need a different change (forwarding--sort=time) and are left alone here.Tests:
test_plan_sort_key_keeps_child_orderpins which flags count as a sort key on GNU and BSD,test_compact_sorted_keeps_child_order/test_compact_unsorted_groups_dirs_firstuse the issue'sls -ltoutput with and without a sort key (short and long rendering), andtest_compact_sorted_truncates_from_the_tailchecks the cap keeps the head of the sorted order. The existingcompact_lstests passsorted = falseand are otherwise unchanged.Closes #4302
Tests
cargo fmt --all --checkcargo clippy --all-targets(no warnings)cargo test(full suite: 4001 unit tests pass). One integration test,unwritable_hooks_dir_never_breaks_the_hook, fails in my WSL environment with and without this change because the tests run as root there and root ignores the chmod the test relies on; unrelated to this PR.touch -ttimestamps plusbig.bin) through the built binary, develop vs. this branch:rtk ls -ltmatches the same order with the octal column;rtk ls,rtk ls -landrtk ls -rprint the same output as develop.AI disclosure
Written with the help of Claude (Anthropic); I reviewed the change and the test results above before opening this PR.
🤖 Generated with Claude Code