list: add --sort-by=SPEC, fixes #9009 - #10089
Merged
ThomasWaldmann merged 8 commits intoAug 12, 2026
Merged
Conversation
Spec parsing/validation and the stable, direction-aware multi-field sort of "--sort-by SPEC" options, so that commands only have to provide the sort key extraction for their own fields.
The sort spec parsing/validation and the sorting itself now come from helpers.sorting, only the sort key extraction stays here. The validator is a module level object now, so shell completion can be attached to it.
SPEC is a comma-separated list of fields, each optionally prefixed with ">" for descending or "<" for ascending (the default) order. The sorts are applied stably from the last field to the first, thus the first field is the primary sort criterion, e.g.: borg list --sort-by='>size,path' ARCHIVE Sorting needs all (matching) items in memory, so without --sort-by, the items are still streamed and their order stays undefined.
There are 3 kinds of --sort-by now (archives, items, item diffs), each with its own set of valid sort keys, so the completion helper is generated once per key set from one template per shell. diff --sort-by had no completion at all before. tcsh matches completion rules by option name only, in one flat namespace for all subcommands, so there it stays one helper offering the union of all keys.
Like list --sort-by, repo-list --sort-by and diff --format.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #10089 +/- ##
==========================================
+ Coverage 86.77% 86.82% +0.05%
==========================================
Files 98 99 +1
Lines 17277 17324 +47
Branches 2622 2627 +5
==========================================
+ Hits 14992 15042 +50
+ Misses 1587 1586 -1
+ Partials 698 696 -2 ☔ View full report in Codecov by Harness. |
On Windows the captured output has CRLF line endings, so assertions anchored on "\n" did not match. Match on lines instead.
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.
Adds
borg list --sort-by=SPECfor archive contents, as requested in #9009.SPEC is a comma-separated list of fields, each optionally prefixed with
>for descending or<for ascending (the default) order. The sorts are applied stably from the last field to the first, so the first field is the primary criterion:Supported fields:
path,type,mode,user,uid,group,gid,size,mtime,ctime,atime.Without
--sort-by, items are streamed as before and their order stays undefined (no tie-breaker is applied).Notes on the implementation
--formatwould print, so the order always matches the visible column:type/modesort by the renderedstat.filemode()values,atime/ctimefall back tomtimeif not stored,user/groupfall back to the numeric id if the name is unknown.birthtimefield, as there is no{birthtime}format key either. Could be added later, together with the format key.borg list --format ... | sortif that is a problem. Note that the patterns and--depthfilter before sorting, so only matching items are buffered.helpers/sorting.py.borg diff --sort-bywas converted to it, with no change to its accepted fields, error messages or resulting order (its existing tests pass unchanged).list --sort-byanddiff --sort-bynow have shell completion for their fields (diff --sort-byhad none so far). As there are 3 different sets of sort keys now (archives / items / item diffs), the completion helper is generated once per key set from one template per shell. In tcsh, completion rules match by option name only, over all subcommands, so there one helper offers the union of all sort keys - noted in theborg completiondocs.</>prefix is not implemented: both characters are in bash's defaultCOMP_WORDBREAKS, so the fragment never reaches the completion helper intact, and changingCOMP_WORDBREAKSfor them would break redirection completion for all other commands.The last commit ("diff: --sort-by can only be given once") is the only behaviour change to
diff: it addsaction=Highlander, likelist --sort-by,repo-list --sort-byanddiff --formathave. Easy to drop if unwanted.