printer: fix handling of `has_match` for summary printer
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Previously, `Quiet` mode in the summary printer always acted like "print matching paths," except without the printing. This happened even if we wanted to "print non-matching paths." Since this only afflicted quiet mode, this had the effect of flipping the exit status when `--files-without-match --quiet` was used. Fixes #3108, Ref #3118
authored by Andrew Gallant · committed by Andrew Gallant · parent 859d542 · Browse files · 4 files·+81 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+81 added−18 removed
- tests:+41 added−0 removed
- other:+40 added−18 removed
Provenance
- Commit
126bbea- Tree
83c5e9a- Author
Andrew Gallant <jamslam@gmail.com>- Committer
Andrew Gallant <jamslam@gmail.com>- Parent
859d542
4 files changed+81 added−18 removed
modified:crates/core/flags/hiargs.rs+10 added-1 removed
560 unmodified lines561 search_mode: SearchMode,562 wtr: W,563 ) -> Printer<W> {564 let summary_kind = if self.quiet {565+ match search_mode {566+ SearchMode::FilesWithMatches567+ | SearchMode::Count568+ | SearchMode::CountMatches569+ | SearchMode::JSON570+ | SearchMode::Standard => SummaryKind::QuietWithMatch,571+ SearchMode::FilesWithoutMatch => {565- SummaryKind::Quiet572+ SummaryKind::QuietWithoutMatch573+ }574+ }575 } else {576 match search_mode {577 SearchMode::FilesWithMatches => SummaryKind::PathWithMatch,578 SearchMode::FilesWithoutMatch => SummaryKind::PathWithoutMatch,903 unmodified linesmodified:crates/printer/src/summary.rs+28 added-17 removed
85 unmodified lines86 /// Don't show any output and the stop the search once a match is found.87 ///88 /// Note that if `stats` is enabled, then searching continues in order to89 /// compute statistics.90- Quiet,90+ QuietWithMatch,91+ /// Don't show any output and the stop the search once a non-matching file92+ /// is found.93+ ///94+ /// Note that if `stats` is enabled, then searching continues in order to95+ /// compute statistics.96+ QuietWithoutMatch,97 }98 99 impl SummaryKind {100 /// Returns true if and only if this output mode requires a file path.5 unmodified lines106 use self::SummaryKind::*;107 108 match *self {109 PathWithMatch | PathWithoutMatch => true,104- Count | CountMatches | Quiet => false,110+ Count | CountMatches | QuietWithMatch | QuietWithoutMatch => false,111 }112 }113 114 /// Returns true if and only if this output mode requires computing2 unmodified lines117 use self::SummaryKind::*;118 119 match *self {120 CountMatches => true,115- Count | PathWithMatch | PathWithoutMatch | Quiet => false,121+ Count | PathWithMatch | PathWithoutMatch | QuietWithMatch122+ | QuietWithoutMatch => false,123 }124 }125 126 /// Returns true if and only if a printer using this output mode can1 unmodified line128 fn quit_early(&self) -> bool {129 use self::SummaryKind::*;130 131 match *self {125- PathWithMatch | Quiet => true,126- Count | CountMatches | PathWithoutMatch => false,132+ PathWithMatch | QuietWithMatch => true,133+ Count | CountMatches | PathWithoutMatch | QuietWithoutMatch => {134+ false135+ }136 }137 }138 }139 114 unmodified lines254 /// [`SummarySink::stats`] method.255 ///256 /// When this is enabled, this printer may need to do extra work in order257 /// to compute certain statistics, which could cause the search to take249- /// longer. For example, in `Quiet` mode, a search can quit after finding250- /// the first match, but if `stats` is enabled, then the search will251- /// continue after the first match in order to compute statistics.258+ /// longer. For example, in `QuietWithMatch` mode, a search can quit after259+ /// finding the first match, but if `stats` is enabled, then the search260+ /// will continue after the first match in order to compute statistics.261 ///262 /// For a complete description of available statistics, see [`Stats`].263 ///264 /// Note that some output modes, such as `CountMatches`, automatically248 unmodified lines513 /// This is unaffected by the result of searches before the previous514 /// search.515 pub fn has_match(&self) -> bool {516 match self.summary.config.kind {508- SummaryKind::PathWithoutMatch => self.match_count == 0,517+ SummaryKind::PathWithoutMatch | SummaryKind::QuietWithoutMatch => {518+ self.match_count == 0519+ }520 _ => self.match_count > 0,521 }522 }523 235 unmodified lines759 // If our binary detection method is to convert binary data, then we760 // don't quit and therefore search the entire contents of the file.761 //762 // There is an unfortunate inconsistency here. Namely, when using752- // Quiet or PathWithMatch, then the printer can quit after the first753- // match seen, which could be long before seeing binary data. This754- // means that using PathWithMatch can print a path where as using763+ // QuietWithMatch or PathWithMatch, then the printer can quit after the764+ // first match seen, which could be long before seeing binary data.765+ // This means that using PathWithMatch can print a path where as using766 // Count might not print it at all because of binary data.767 //768 // It's not possible to fix this without also potentially significantly758- // impacting the performance of Quiet or PathWithMatch, so we accept759- // the bug.769+ // impacting the performance of QuietWithMatch or PathWithMatch, so we770+ // accept the bug.771 if self.binary_byte_offset.is_some()772 && searcher.binary_detection().quit_byte().is_some()773 {774 // Squash the match count. The statistics reported will still33 unmodified lines808 if self.match_count == 0 {809 self.write_path_line(searcher)?;810 }811 }801- SummaryKind::Quiet => {}812+ SummaryKind::QuietWithMatch | SummaryKind::QuietWithoutMatch => {}813 }814 Ok(())815 }816 }315 unmodified lines1132 #[test]1133 fn quiet() {1134 let matcher = RegexMatcher::new(r"Watson|Sherlock").unwrap();1135 let mut printer = SummaryBuilder::new()1125- .kind(SummaryKind::Quiet)1136+ .kind(SummaryKind::QuietWithMatch)1137 .build_no_color(vec![]);1138 let match_count = {1139 let mut sink = printer.sink_with_path(&matcher, "sherlock");1140 SearcherBuilder::new()13 unmodified lines1154 #[test]1155 fn quiet_with_stats() {1156 let matcher = RegexMatcher::new(r"Watson|Sherlock").unwrap();1157 let mut printer = SummaryBuilder::new()1147- .kind(SummaryKind::Quiet)1158+ .kind(SummaryKind::QuietWithMatch)1159 .stats(true)1160 .build_no_color(vec![]);1161 let match_count = {1162 let mut sink = printer.sink_with_path(&matcher, "sherlock");15 unmodified linesmodified:tests/regression.rs+41 added
1475 unmodified lines1476 // This used to ignore the glob given and included `asdf./foo` in output.1477 let got = dir.command().args(&["--files", "-g", "!asdf./"]).stdout();1478 eqnice!("asdf/foo\n", got);1479 });1480+1481+// See: https://github.com/BurntSushi/ripgrep/issues/31081482+rgtest!(r3108_files_without_match_quiet_exit, |dir: Dir, _: TestCommand| {1483+ dir.create("yes-match", "abc");1484+ dir.create("non-match", "xyz");1485+1486+ dir.command().args(&["-q", "abc", "non-match"]).assert_exit_code(1);1487+ dir.command().args(&["-q", "abc", "yes-match"]).assert_exit_code(0);1488+ dir.command()1489+ .args(&["--files-with-matches", "-q", "abc", "non-match"])1490+ .assert_exit_code(1);1491+ dir.command()1492+ .args(&["--files-with-matches", "-q", "abc", "yes-match"])1493+ .assert_exit_code(0);1494+1495+ dir.command()1496+ .args(&["--files-without-match", "abc", "non-match"])1497+ .assert_exit_code(0);1498+ dir.command()1499+ .args(&["--files-without-match", "abc", "yes-match"])1500+ .assert_exit_code(1);1501+1502+ let got = dir1503+ .command()1504+ .args(&["--files-without-match", "abc", "non-match"])1505+ .stdout();1506+ eqnice!("non-match\n", got);1507+1508+ dir.command()1509+ .args(&["--files-without-match", "-q", "abc", "non-match"])1510+ .assert_exit_code(0);1511+ dir.command()1512+ .args(&["--files-without-match", "-q", "abc", "yes-match"])1513+ .assert_exit_code(1);1514+1515+ let got = dir1516+ .command()1517+ .args(&["--files-without-match", "-q", "abc", "non-match"])1518+ .stdout();1519+ eqnice!("", got);1520+});modified:CHANGELOG.md+2 added
23 unmodified lines24 * [BUG #2944](https://github.com/BurntSushi/ripgrep/pull/2944):25 Fix a bug where the "bytes searched" in `--stats` output could be incorrect.26 * [BUG #2990](https://github.com/BurntSushi/ripgrep/issues/2990):27 Fix a bug where ripgrep would mishandle globs that ended with a `.`.28+* [BUG #3108](https://github.com/BurntSushi/ripgrep/issues/3108):29+ Fix a bug where `-q --files-without-match` inverted the exit code.30 31 Feature enhancements:32 33 * Many enhancements to the default set of file types available for filtering.1727 unmodified lines