Repository navigation
feat(head): print all but the last N lines or bytes with -n -N and -c -N - #218
Conversation
ea04c53 to
db19b13
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. -n -N / -c -N match GNU head 9.1 in every case we threw at it. No blocking or should-fix findings.
One optional follow-up: the GNU-parity claim was checked by hand. A tests/differential/cases/head_all_but_last.sh (recorded against bash + coreutils, next to head_tail_bytes.sh) would lock it in. For example: -n -2, -n -0, -n -1 on a file with no trailing newline, and -c -2.
While testing I found a pre-existing bug in the sibling command: tail -n -2 silently prints 10 lines (tail.rs:25, unwrap_or(10)). Filed as #253. It's out of scope here.
✅ What I verified
- Head
db19b13on based4c57a7, Rust 1.100.0 aarch64. cargo test --test shell_integration head: 27 passed, including all 13 new tests.cargo test --lib --bins: 70 passed.cargo clippy --all-targets: no warnings inhead.rsor the new test lines. The existing warnings come from the newer toolchain. I didn't runcargo fmtbecause rustfmt wasn't installed. Log:strands-agents/shell/pr/218/cargo-test.login the artifacts portal.- Output bytes and exit status match
/usr/bin/head(GNU 9.1) for:- the separate, attached and long forms of
-n/-c, plus--lines -2; -0, a count larger than the input, empty input, no trailing newline, and CRLF;- non-UTF-8 input on the new line path;
- mixing options in both orders, and legacy
head -5 -n -1; -n -+1, and the error values--1,-,-x,' -1';-- -file, a missing file, and-n -0/-c -0on a file;-c -Naround the 8 KiB chunk boundary (N = 1, 8191, 8192, 8193, 16384, 19999, 20000, 20001 on 20000 random bytes, read as a file and viacat, compared by md5);- 200 to 300 KB single lines;
tail -5andtail -n +2, which sharelegacy_line_count, still work.
- the separate, attached and long forms of
- Performance: on a 9.4 MB file,
-c -4000000and-n -10run as fast as the positive forms.
Routing: I ran a correctness/tests pass and an adversarial pass. I skipped the API pass because there's no public API, binding or model-facing change, only the head help text. The first adversarial run timed out, so I retried it with a narrower scope against a prebuilt binary, and it finished.
Appendix: non-blocking (5)
- ⚪
head -c -18446744073709551615: GNU errors with "Value too large", while this prints nothing. Nobody realistically passes that count. - ⚪ Pre-existing, positive counts do the same:
- size suffixes (
-c -1K) aren't parsed; -isn't read as stdin;- a closed downstream pipe prints
head: pipe closedto stderr.
- size suffixes (
- ⚪ Pre-existing: the positive
-npath still usesread_lineinto aString(head.rs:58-61), so it fails on invalid UTF-8. The new negative path usesread_until, which avoids this. - Pre-existing, filed #253:
tail -n -2andtail -n abcsilently fall back to defaults (tail.rs:23-25).
|
Thanks for checking the raw bytes and the chunk boundaries. The new integration tests already cover those four suggested cases, including negative zero and a final line without a newline. A recorded GNU fixture would make the reference behavior easier to maintain, so that makes sense as a follow-up. Thanks for filing #253 for tail. The existing suffix/stdin/positive-count UTF-8 gaps and the extreme-count validation can stay separate from this negative-count change. |
Description
head -n -2is supposed to print everything except the last 2 lines, but we couldn't parse a negative count, so it just failed:Now
-n -Nprints all but the last N lines, and-c -Ndoes the same for bytes, including the attached and long forms (-n-2,--lines=-2,--bytes=-3). The legacyhead -5form and positive counts work exactly as before.I compared each case with GNU head on Linux, including
-0(prints everything), a count bigger than the input (prints nothing), a last line without a newline, and mixing-nand-c. The output matched in every case. I left out multiple files with headers, because ourheadonly takes one file today and that's a separate feature.Related Issues
Part of #207
Documentation PR
N/A
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.