Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort output by file last modified time #404

Closed
wavexx opened this issue Mar 12, 2017 · 4 comments
Closed

sort output by file last modified time #404

wavexx opened this issue Mar 12, 2017 · 4 comments
Labels
enhancement An enhancement to the functionality of the software. icebox A feature that is recognized as possibly desirable, but is unlikely to implemented any time soon.

Comments

@wavexx
Copy link

wavexx commented Mar 12, 2017

I'm throwing this here for consideration. When I'm searching through my source files, I often not only want to find all occurrences, but when multiple hits are involved, I also want to find the most recent modification (by mtime). It would be nice to be able to sort output files (with context), sorted by time and reverse time.

Afaik, none of the various searching tools allow to do this. Not easily at least.

To do it currently, I actually do two passes:

  • find file candidates fist
  • sort files by mtime
  • search within files

This is necessary as I obviously like to see the match context as well (otherwise I could stop at step 2). But having to find candidates first introduces extra overhead that cannot be avoided.

This requires buffering matches before showing the output, of course, but the search can be ran in parallel still.

@BurntSushi BurntSushi added the enhancement An enhancement to the functionality of the software. label Mar 12, 2017
@BurntSushi
Copy link
Owner

@wavexx This does seem mildly useful, but I don't know when I'll have the time to work on it.

This requires buffering matches before showing the output, of course, but the search can be ran in parallel still.

While strictly true, this is an oversimplification. Namely, running a search in parallel requires buffering to avoid interleaving results. Sorting with parallelism requires something more than just a simple buffering scheme however, and it is rather difficult to do in the current setup (which parallelizes things at level of directory traversal). This is why the --sort-files flag forces ripgrep to operate in single threaded mode.

@BurntSushi BurntSushi added the icebox A feature that is recognized as possibly desirable, but is unlikely to implemented any time soon. label Apr 9, 2017
@BurntSushi
Copy link
Owner

It should be possible to implement this in the ignore crate without too much trouble now that walkdir's sort_by method exposes the necessary information. Namely, you can get the Path from the DirEntry and stat it to determine various attributes. This will have performance implications and should force single threaded mode. We'll also need to think carefully about flags.

@BurntSushi BurntSushi changed the title Optionally sort output files sort output by file last modified time Apr 25, 2018
@BurntSushi
Copy link
Owner

BurntSushi commented Aug 24, 2018

Here is what I propose:

  • Add new --sort and --sortr flags to ripgrep. Both accept the same set of pre-determined choices. The former sorts in ascending order where as the latter sorts in descending order (or "reverse" order).
  • Implementation detail: Whenever --sort or --sortr are given, ripgrep enters single threaded mode.
  • Initially, the set of available parameters to the flag will be path, modified, accessed, created. path will sort by the file path (and therefore, --sort path will behave exactly the same as --sort-files does today). modified/accessed/created will sort by a file's last-modified/last-accessed/created timestamps, when available. When not available, ripgrep will stop execution before searching and print an error if possible. Otherwise, sort order will be unspecified.

We'll likely therefore deprecate --sort-files, but leave it as a hidden alias for --sort path.

Thoughts?

@wavexx
Copy link
Author

wavexx commented Aug 24, 2018

I like it.

BurntSushi added a commit that referenced this issue Aug 26, 2018
These flags each accept one of five choices: none, path, modified,
accessed or created. The value indicates how the results are sorted.
For --sort, results are sorted in ascending order where as for --sortr,
results are sorted in descending order.

Closes #404
BurntSushi added a commit that referenced this issue Aug 26, 2018
These flags each accept one of five choices: none, path, modified,
accessed or created. The value indicates how the results are sorted.
For --sort, results are sorted in ascending order where as for --sortr,
results are sorted in descending order.

Closes #404
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement to the functionality of the software. icebox A feature that is recognized as possibly desirable, but is unlikely to implemented any time soon.
Projects
None yet
Development

No branches or pull requests

2 participants