Skip to content

feat(cli): add --human and --sort-size to ipfs ls - #11408

Merged
lidel merged 6 commits into
ipfs:masterfrom
capricornusx:master
Jul 29, 2026
Merged

feat(cli): add --human and --sort-size to ipfs ls#11408
lidel merged 6 commits into
ipfs:masterfrom
capricornusx:master

Conversation

@capricornusx

Copy link
Copy Markdown
Contributor
  • --human (-H): SI human-readable sizes in text output (humanize.Bytes)
  • --sort-size (-S): sort directory entries by size, largest first
  • Validation: --sort-size + --stream and --sort-size + --size=false errors
  • Unit tests for formatSize and sort helpers
  • CLI integration tests for both flags
  • Changelog highlight in v0.44

- --human (-H): SI human-readable sizes in text output (humanize.Bytes)
- --sort-size (-S): sort directory entries by size, largest first
- Validation: --sort-size + --stream and --sort-size + --size=false errors
- Unit tests for formatSize and sort helpers
- CLI integration tests for both flags
- Changelog highlight in v0.44
@capricornusx
capricornusx requested a review from a team as a code owner July 25, 2026 19:05
capricornusx and others added 5 commits July 26, 2026 02:05
The tests covering --sort-size could not detect a broken feature. The
unit tests copied the comparator into the test body and sorted with their
own copy, so they passed regardless of what ls.go did. The CLI tests named
each fixture after its size, which left alphabetical order and size order
in agreement, so every ordering subtest passed even with --sort-size
disabled outright.

- extract lsLinkByName and lsLinkBySize so the tests exercise shipped code
- point the unit tests at those two functions
- name fixtures so their alphabetical order disagrees with their sizes
- pin the real directory behaviour: UnixFS directories carry no Filesize,
  so they sort as 0, tie with empty files and break by name rather than
  landing strictly last
Passing --sort-size together with --stream or --size=false made
/api/v0/ls answer 500 Internal Server Error, telling API clients the
server had broken when the caller had simply combined flags that cannot
work together. Clients that retry on 5xx would retry a request that can
never succeed.

cmds.ErrClient maps to 400, matching how the rest of core/commands
reports caller mistakes. CLI output is unchanged.
The help for --human advertised "1K 234M 2G", which no kubo command has
ever printed. All three use humanize.Bytes, so the real output is SI with
a space: 1.2 kB, 234 MB, 2.0 GB. The stale example was copied into
'ipfs ls' from the two commands that already carried it, so correct all
three together.

- ls, repo stat, bitswap stat: examples now match real output
- drop the claim that --enc=json reports bytes. On the CLI, 'ipfs ls'
  has a PostRun that prints the text table whatever --enc says, so no
  JSON is produced there at all. Only the /api/v0/ls response is JSON,
  and that part is true
- directories have no UnixFS Filesize, so they sort as 0 and tie with
  empty files. They do not land strictly last, so stop saying they do
- changelog: a #### highlight with a TOC entry, kept short and pointing
  at 'ipfs ls --help' for the details
- format sizes with strconv.FormatUint rather than fmt.Sprintf("%d")

@lidel lidel left a comment

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.

Thanks for this, @capricornusx. I pushed three commits to your branch rather than leaving a pile of inline comments (felt like cosmetic nits missed by your LLM, but intention was ok). Feature behaviour is unchanged.

  • 4315e51 test(ls): make sort tests fail when sorting breaks
    The --sort-size tests couldn't fail. The unit tests copied the comparator into the test body, and the CLI fixtures (large.bin, medium.bin, small.bin) were already in size order alphabetically, so every ordering subtest passed with the feature disabled outright. Extracted lsLinkByName/lsLinkBySize so the tests hit shipped code, and renamed fixtures so the two orders disagree.

  • 078febb fix(ls): return 400 not 500 for bad flag combos
    --sort-size with --stream or --size=false made /api/v0/ls answer 500, so a client retrying on 5xx would retry a request that can never succeed. cmds.ErrClient maps to 400.

  • 4485c12 docs: fix --human size examples and JSON claims
    1K 234M 2G is not what any of these print (humanize.Bytes gives 1.2 kB, 234 MB), though you inherited that from repo stat and bitswap stat, so I fixed all three. Worth knowing: ipfs ls has a PostRun that prints the text table whatever --enc says, so --enc=json yields no JSON on the CLI at all. That predates this PR, but the new help text promised otherwise. Also dropped "directories sort last", since with no UnixFS Filesize they tie with empty files at 0.

@lidel
lidel enabled auto-merge (squash) July 29, 2026 12:26
@lidel lidel mentioned this pull request Jul 29, 2026
5 tasks
@lidel
lidel merged commit d4bbbef into ipfs:master Jul 29, 2026
22 checks passed
@capricornusx

Copy link
Copy Markdown
Contributor Author

Thanks!

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.

2 participants