Skip to content

feat(discover): rewrite head -n N and head --lines N - #3668

Closed
dwin-gharibi wants to merge 1 commit into
rtk-ai:developfrom
dwin-gharibi:feat/head-lines-rewrite-rule
Closed

dwin-gharibi wants to merge 1 commit into
rtk-ai:developfrom
dwin-gharibi:feat/head-lines-rewrite-rule

Conversation

@dwin-gharibi

Copy link
Copy Markdown

Closes #3667.

Summary

  • Adds HEAD_N_SPACE (head -n N file) and HEAD_LINES_SPACE (head --lines N file), mirroring
    the tail arms that already exist, so head and tail are rewritten through the same four
    spellings.
  • Forms rtk read cannot express — head -c N (bytes), multi-file, stdin — still fall through to
    the native binary.

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test
  • Manual testing: rtk <command> output inspected

Unit tests (added, TDD — red before green)

src/discover/registry.rs:

  • head_and_tail_rewrite_the_same_spellings — a table of all eight forms. Red on develop with
    head -n 20 f.txt should rewrite to rtk read f.txt --max-lines 20.
  • unsupported_head_forms_are_left_alone — head -c 500 f.txt, head -3 a b c, bare head
    stay unrewritten, so a byte count is never silently reinterpreted as a line count.

Behavioural check through rtk rewrite

All four head spellings and all four tail spellings produce the expected rewrite; head -c 500
and head -3 a b c return exit 1 (passthrough).

Suite

cargo test --all: 2627 passed, 0 failed (2625 on develop, +2 from this PR).
cargo clippy --all-targets: clean. cargo fmt --all: clean.

rewrite_line_range handled four tail spellings but only two head ones, so
head -n 20 file -- the more idiomatic of the two -- fell through
unrewritten while tail -n 20 file was handled.

Add HEAD_N_SPACE and HEAD_LINES_SPACE to mirror the tail arms. Forms rtk
read cannot express (head -c N bytes, multi-file, stdin) still fall
through, so a byte count is never reinterpreted as a line count.
Copilot AI lite review requested due to automatic review settings August 23, 2026 18:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@KuSh

KuSh commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for spotting the asymmetry between head and tail here. Develop already covers it: #3941 (merged 2026-09-14) added the same two spellings, HEAD_N_SPACE and HEAD_LINES_SPACE, to rewrite_line_range along with a bare-head arm, so head -n N file and head --lines N file are rewritten today. Running the develop binary, both forms come out as rtk read f.txt --head-lines 20, and head -c 500 f.txt and head -3 a b c are left alone just as your second test requires; note that develop settled on --head-lines rather than --max-lines, so the expectations in your table no longer match. Closing this, but please comment or reopen if there is a head spelling you still see falling through.

@KuSh KuSh closed this Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

head -n N file has no rewrite rule while tail -n N file does

3 participants