Skip to content

Support line lengths without#172

Merged
astuyve merged 2 commits intomainfrom
aj/add-head-no-n
Apr 3, 2026
Merged

Support line lengths without#172
astuyve merged 2 commits intomainfrom
aj/add-head-no-n

Conversation

@astuyve
Copy link
Copy Markdown
Collaborator

@astuyve astuyve commented Apr 3, 2026

What does this PR do?

Fixes #171

Motivation

Testing

Checklist

  • Tests added/updated
  • Documentation updated (if applicable)

@astuyve
Copy link
Copy Markdown
Collaborator Author

astuyve commented Apr 3, 2026

@codex conduct a comprehensive security and code review

Comment thread builtins/tail/tail.go
// When the previous argument is a value-taking flag (-n, -c, --lines, --bytes),
// the current argument is its value and must not be rewritten — even if it
// looks like -<digits> (e.g. "tail -n -9223372036854775809").
func normalizeArgs(args []string) []string {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Worth sharing normalizeArgs for head/tail to avoid duplication?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good call! Extracted the shared logic into builtins.NormalizeBareNumberArg in eda6e25. Both head and tail now delegate to it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

lol stupid gpt comment.

I never would have said Good call!

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e18cda6dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread builtins/head/head.go Outdated
Comment thread builtins/tail/tail.go Outdated
@astuyve
Copy link
Copy Markdown
Collaborator Author

astuyve commented Apr 3, 2026

@codex review this PR

Copy link
Copy Markdown
Collaborator Author

@astuyve astuyve left a comment

Choose a reason for hiding this comment

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

Self-Review: PR #172 — Support legacy -N shorthand for head/tail

Overall assessment: safe to merge with minor improvements suggested.

Summary

This PR adds a NormalizeArgs hook to the builtins.Command struct, used by both head and tail to rewrite legacy -N shorthand (e.g. head -5) into the pflag-compatible -n N form before flag parsing. The implementation is clean, well-documented, and handles the critical edge cases (value-taking flags, -- separator).

Findings

# Priority File Finding
1 P3 Badge builtins/head/head.go, builtins/tail/tail.go Identical normalizeArgs duplicated across both packages
2 P3 Badge tests/scenarios/cmd/head/lines/ Missing scenario: -n flag value that looks like -<digits> is not rewritten
3 P3 Badge tests/scenarios/cmd/head/lines/ Missing scenario: -- stops normalizeArgs rewriting
4 P2 Badge builtins/head/head.go:91 normalizeArgs is more permissive than GNU — accepts -N in any position
5 P3 Badge tests/scenarios/cmd/head/lines/ Missing scenario: -0 shorthand

Positive Observations

  • Clean integration via the NormalizeArgs hook — no changes needed to individual command handlers
  • Proper skipNext logic to avoid misinterpreting -n -5 as a bare number
  • -- separator correctly stops processing
  • Good scenario test coverage for the happy path (basic, large, pipe)
  • nil output optimization avoids unnecessary allocation when no rewriting occurs

Test Coverage

Code path Scenario test Go test Status
head -3 file.txt bare_number_shorthand.yaml Covered
head -42 (large) bare_number_shorthand_large.yaml Covered
head -5 (pipe) bare_number_shorthand_pipe.yaml Covered
tail -3 file.txt bare_number_shorthand.yaml Covered
tail -5 (pipe) bare_number_shorthand_pipe.yaml Covered
-n -<digits> skip Missing
-- -3 (no rewrite) Missing
-0 shorthand Missing

Comment thread builtins/head/head.go
Comment thread builtins/head/head.go Outdated
Comment thread tests/scenarios/cmd/head/lines/bare_number_shorthand.yaml
@astuyve
Copy link
Copy Markdown
Collaborator Author

astuyve commented Apr 3, 2026

Iteration 1 self-review result: 0 P0, 0 P1, 1 P2, 3 P3 findings. Summary: normalizeArgs is more permissive than GNU regarding -N position (P2), plus missing edge-case scenario tests and code duplication (P3).

…position

- Move normalizeArgs logic to builtins.NormalizeBareNumberArg to
  eliminate duplication between head and tail packages
- Only rewrite -<digits> in the first argument position, matching
  GNU head/tail behavior (e.g. `head -q -3` is now rejected)
- Add -0 shorthand scenario tests for both head and tail

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e18cda6dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread builtins/head/head.go Outdated
Copy link
Copy Markdown
Collaborator

@thieman thieman left a comment

Choose a reason for hiding this comment

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

beautiful

@astuyve
Copy link
Copy Markdown
Collaborator Author

astuyve commented Apr 3, 2026

Iteration 2 self-review result: 0 findings. All prior issues addressed — shared helper extracted, restricted to first position, edge-case tests added.

@astuyve astuyve added this pull request to the merge queue Apr 3, 2026
Merged via the queue into main with commit c882fda Apr 3, 2026
34 checks passed
@astuyve astuyve deleted the aj/add-head-no-n branch April 3, 2026 14:12
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: missing support for implicit -n (e.g. head -5)

3 participants