girt 0.18.0

An incremental, idiomatic Rust implementation of Git
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::ops::Range;

use regex_automata::nfa::thompson::{self, pikevm};
use regex_automata::util::syntax;
use regex_automata::{Anchored, Input, MatchKind, meta};

use crate::posix_regex::{Flavor, literal, translate};

/// Compiled expressions above this size are refused, bounding memory for hostile patterns.
const NFA_SIZE_LIMIT: usize = 8 * 1024 * 1024;

/// How the text of each pattern is read, as `git grep -F`, `-G` and `-E` choose.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum Syntax {
    /// Every byte stands for itself (`-F`).
    Fixed,
    /// POSIX basic regular expressions, Git's default (`-G`). As in GNU, `\+`, `\?` and `\|` are
    /// operators.
    #[default]
    Basic,
    /// POSIX extended regular expressions (`-E`).
    Extended,
}

/// A boolean combination of patterns, as `-e`, `--and`, `--or`, `--not` and parentheses build it.
///
/// Adjacent `-e` patterns on Git's command line are joined with [`Expression::Or`].
///
/// ```
/// use girt::grep::Expression;
///
/// // git grep -e foo --and --not -e bar
/// let expression = Expression::pattern("foo").and(!Expression::pattern("bar"));
/// # let _ = expression;
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Expression {
    /// A line matches when the pattern matches somewhere in it. A pattern containing LF stands
    /// for its lines joined with [`Expression::Or`], as in Git.
    Pattern(Vec<u8>),
    /// A line matches when the inner expression does not.
    Not(Box<Expression>),
    /// A line matches when both sides do.
    And(Box<Expression>, Box<Expression>),
    /// A line matches when either side does.
    Or(Box<Expression>, Box<Expression>),
}

impl Expression {
    /// A single pattern.
    pub fn pattern(text: impl Into<Vec<u8>>) -> Self {
        Self::Pattern(text.into())
    }

    /// Lines both expressions match (`--and`).
    #[must_use]
    pub fn and(self, other: Self) -> Self {
        Self::And(Box::new(self), Box::new(other))
    }

    /// Lines either expression matches (`--or`, or two adjacent patterns).
    #[must_use]
    pub fn or(self, other: Self) -> Self {
        Self::Or(Box::new(self), Box::new(other))
    }
}

/// Lines this expression does not match (`--not`).
impl std::ops::Not for Expression {
    type Output = Self;

    fn not(self) -> Self {
        Self::Not(Box::new(self))
    }
}

/// How every pattern of a [`Query`] matches: syntax, case and whole-word matching.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Matching {
    syntax: Syntax,
    ignore_case: bool,
    word: bool,
    bytes: bool,
}

impl Matching {
    /// Basic POSIX syntax, case-sensitive, matching anywhere: `git grep` without options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Reads patterns with `syntax`.
    #[must_use]
    pub fn with_syntax(mut self, syntax: Syntax) -> Self {
        self.syntax = syntax;
        self
    }

    /// Ignores case (`-i`).
    #[must_use]
    pub fn with_ignore_case(mut self, ignore_case: bool) -> Self {
        self.ignore_case = ignore_case;
        self
    }

    /// Matches byte by byte, as Git does in the C locale, instead of by UTF-8 character, as in a
    /// UTF-8 locale (the default). With bytes, `.` and bracket expressions match any single byte
    /// and case folding is ASCII.
    #[must_use]
    pub fn with_bytes(mut self, bytes: bool) -> Self {
        self.bytes = bytes;
        self
    }

    /// Accepts only matches with a non-word byte or the line edge on each side (`-w`).
    ///
    /// Word bytes are ASCII letters, digits and `_`. As in Git, when the leftmost-longest match
    /// at a position fails this test, the search resumes one byte after that match's start;
    /// shorter matches at the same start are not tried.
    #[must_use]
    pub fn with_word(mut self, word: bool) -> Self {
        self.word = word;
        self
    }
}

/// A pattern could not be compiled.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum PatternError {
    /// The pattern is not UTF-8. Git accepts such bytes; girt does not compile them.
    #[error("grep pattern is not UTF-8")]
    Encoding,
    /// The pattern uses a POSIX construct girt does not translate, such as a back-reference.
    /// Git accepts it.
    #[error("unsupported grep pattern construct: {0}")]
    Unsupported(&'static str),
    /// The expression is malformed or exceeds girt's compiled size bound.
    #[error("invalid grep pattern")]
    Invalid(#[source] Box<dyn std::error::Error + Send + Sync>),
}

/// Compiled patterns and the expression that combines them; decides which lines match.
///
/// Patterns are POSIX expressions translated to the dialect girt's regex engine compiles, with
/// the same rules and refusals as
/// [`FunctionLines`](crate::patch::FunctionLines): lines match as in a UTF-8 locale unless
/// [`Matching::with_bytes`] selects the C locale's byte matching, named classes such as
/// `[:alpha:]` are ASCII, and back-references, collating elements and
/// equivalence classes are refused. Reported match positions are POSIX leftmost-longest.
///
/// ```
/// use girt::grep::{Expression, Matching, Query, Syntax};
///
/// let query = Query::new(
///     &Expression::pattern("ab+c"),
///     Matching::new().with_syntax(Syntax::Extended),
/// )?;
/// assert!(query.is_match(b"xabbc"));
/// assert_eq!(query.matches(b"abc abbc"), [0..3, 4..8]);
/// assert!(!query.is_match(b"ac"));
/// # Ok::<(), girt::grep::PatternError>(())
/// ```
#[derive(Debug)]
pub struct Query {
    node: Node,
    leaves: Vec<Leaf>,
    word: bool,
    all_match: bool,
}

#[derive(Debug)]
pub(super) enum Node {
    Leaf(usize),
    Not(Box<Node>),
    And(Box<Node>, Box<Node>),
    Or(Box<Node>, Box<Node>),
}

#[derive(Debug)]
struct Leaf {
    /// Finds the leftmost match start quickly.
    find: meta::Regex,
    /// Extends a match from that start to the longest one.
    longest: pikevm::PikeVM,
    /// Whether the leaf sits under a `--not`; such leaves select lines but are not shown by
    /// `-o`.
    negated: bool,
}

impl Query {
    /// Compiles every pattern of `expression` with `matching`.
    ///
    /// # Errors
    ///
    /// Returns [`PatternError`] for a non-UTF-8 pattern, an unsupported construct or a
    /// malformed expression.
    pub fn new(expression: &Expression, matching: Matching) -> Result<Self, PatternError> {
        let mut leaves = Vec::new();
        let node = compile(expression, matching, false, &mut leaves)?;
        Ok(Self {
            node,
            leaves,
            word: matching.word,
            all_match: false,
        })
    }

    /// Selects a file only when each pattern joined by a top-level [`Expression::Or`] matches
    /// at least one of its lines (`--all-match`). Line selection is unchanged.
    #[must_use]
    pub fn with_all_match(mut self, all_match: bool) -> Self {
        self.all_match = all_match;
        self
    }

    /// Returns whether `line` (without its line terminator) matches.
    #[must_use]
    pub fn is_match(&self, line: &[u8]) -> bool {
        self.eval(&self.node, line)
    }

    /// The positions `-o` and `--column` report in `line`: from each position, the earliest
    /// match of any pattern not under a `--not`, the longest on a tie, then the search continues
    /// after it. An empty match ends the list, as in Git.
    #[must_use]
    pub fn matches(&self, line: &[u8]) -> Vec<Range<usize>> {
        let mut found = Vec::new();
        let mut caches: Vec<_> = self.leaves.iter().map(|_| None).collect();
        let mut at = 0;
        while at <= line.len() {
            let best = self
                .leaves
                .iter()
                .zip(&mut caches)
                .filter(|(leaf, _)| !leaf.negated)
                .filter_map(|(leaf, cache)| self.find(leaf, cache, line, at))
                .min_by(|a, b| a.start.cmp(&b.start).then(b.end.cmp(&a.end)));
            let Some(span) = best.filter(|span| !span.is_empty()) else {
                break;
            };
            at = span.end;
            found.push(span);
        }
        found
    }

    /// The branches of a top-level [`Expression::Or`] chain, for `--all-match`.
    pub(super) fn branches(&self) -> Vec<&Node> {
        fn collect<'a>(node: &'a Node, out: &mut Vec<&'a Node>) {
            match node {
                Node::Or(left, right) => {
                    collect(left, out);
                    collect(right, out);
                }
                other => out.push(other),
            }
        }
        let mut out = Vec::new();
        collect(&self.node, &mut out);
        out
    }

    pub(super) fn all_match(&self) -> bool {
        self.all_match
    }

    pub(super) fn eval(&self, node: &Node, line: &[u8]) -> bool {
        match node {
            Node::Leaf(index) => self
                .leaves
                .get(*index)
                .and_then(|leaf| self.find(leaf, &mut None, line, 0))
                .is_some(),
            Node::Not(inner) => !self.eval(inner, line),
            Node::And(left, right) => self.eval(left, line) && self.eval(right, line),
            Node::Or(left, right) => self.eval(left, line) || self.eval(right, line),
        }
    }

    /// When the expression is a plain `--or` of patterns, the offset of the earliest position in
    /// `haystack` at or after `from` where a pattern may match; lines before it cannot match.
    /// `None` means no line at or after `from` matches. Returns `Some(from)` when the expression
    /// cannot be searched this way.
    pub(super) fn skip_to(&self, haystack: &[u8], from: usize) -> Option<usize> {
        if has_and(&self.node) {
            return Some(from);
        }
        self.leaves
            .iter()
            .filter_map(|leaf| {
                leaf.find
                    .search(&Input::new(haystack).range(from..))
                    .map(|found| found.start())
            })
            .min()
    }

    /// The leftmost-longest match of `leaf` in `line` starting at or after `at`, honoring `-w`.
    fn find(
        &self,
        leaf: &Leaf,
        cache: &mut Option<pikevm::Cache>,
        line: &[u8],
        mut at: usize,
    ) -> Option<Range<usize>> {
        loop {
            let found = leaf.find.search(&Input::new(line).range(at..))?;
            let start = found.start();
            let cache = cache.get_or_insert_with(|| leaf.longest.create_cache());
            let input = Input::new(line).range(start..).anchored(Anchored::Yes);
            let end = leaf
                .longest
                .find(cache, input)
                .map_or(found.end(), |longest| longest.end().max(found.end()));
            if !self.word || is_word_bounded(line, start, end) {
                return Some(start..end);
            }
            at = start + 1;
            if at > line.len() {
                return None;
            }
        }
    }
}

fn has_and(node: &Node) -> bool {
    match node {
        Node::Leaf(_) => false,
        Node::Not(_) | Node::And(..) => true,
        Node::Or(left, right) => has_and(left) || has_and(right),
    }
}

fn is_word_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || byte == b'_'
}

fn is_word_bounded(line: &[u8], start: usize, end: usize) -> bool {
    let before = start
        .checked_sub(1)
        .and_then(|index| line.get(index))
        .is_some_and(|&byte| is_word_byte(byte));
    let after = line.get(end).is_some_and(|&byte| is_word_byte(byte));
    !before && !after
}

fn compile(
    expression: &Expression,
    matching: Matching,
    negated: bool,
    leaves: &mut Vec<Leaf>,
) -> Result<Node, PatternError> {
    Ok(match expression {
        Expression::Pattern(text) => {
            // As in Git, each line of a pattern is a pattern of its own, joined with --or.
            let mut node = None;
            for line in text.split(|&byte| byte == b'\n') {
                leaves.push(leaf(line, matching, negated)?);
                let next = Node::Leaf(leaves.len() - 1);
                node = Some(match node {
                    None => next,
                    Some(previous) => Node::Or(Box::new(previous), Box::new(next)),
                });
            }
            node.unwrap_or(Node::Leaf(0))
        }
        Expression::Not(inner) => Node::Not(Box::new(compile(inner, matching, !negated, leaves)?)),
        Expression::And(left, right) => Node::And(
            Box::new(compile(left, matching, negated, leaves)?),
            Box::new(compile(right, matching, negated, leaves)?),
        ),
        Expression::Or(left, right) => Node::Or(
            Box::new(compile(left, matching, negated, leaves)?),
            Box::new(compile(right, matching, negated, leaves)?),
        ),
    })
}

fn leaf(text: &[u8], matching: Matching, negated: bool) -> Result<Leaf, PatternError> {
    let text = std::str::from_utf8(text).map_err(|_| PatternError::Encoding)?;
    let expression = match matching.syntax {
        Syntax::Fixed => {
            let mut out = String::with_capacity(text.len());
            text.chars().for_each(|c| literal(c, &mut out));
            out
        }
        Syntax::Basic => translate(text, Flavor::Basic).map_err(PatternError::Unsupported)?,
        Syntax::Extended => translate(text, Flavor::Extended).map_err(PatternError::Unsupported)?,
    };
    // Multi-line anchors let one search over a whole file find candidate lines; each candidate
    // is then checked against its own line.
    let syntax = syntax::Config::new()
        .case_insensitive(matching.ignore_case)
        .multi_line(true)
        .unicode(!matching.bytes)
        .utf8(!matching.bytes);
    let invalid = |error: Box<dyn std::error::Error + Send + Sync>| PatternError::Invalid(error);
    let find = meta::Regex::builder()
        .syntax(syntax)
        .configure(meta::Config::new().nfa_size_limit(Some(NFA_SIZE_LIMIT)))
        .build(&expression)
        .map_err(|error| invalid(Box::new(error)))?;
    let longest = pikevm::PikeVM::builder()
        .syntax(syntax)
        .thompson(thompson::Config::new().nfa_size_limit(Some(NFA_SIZE_LIMIT)))
        .configure(pikevm::Config::new().match_kind(MatchKind::All))
        .build(&expression)
        .map_err(|error| invalid(Box::new(error)))?;
    Ok(Leaf {
        find,
        longest,
        negated,
    })
}