grit-lib 0.5.19

Core library for the grit Git implementation
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Commit message and identity filters for revision walks (`--author`, `--grep`, …).
//!
//! Patterns use Git's default POSIX basic regular expression (BRE) syntax unless
//! [`CommitFilterBuilder::fixed_strings`] is set. Callers supply an optional
//! [`MailmapTable`] loaded explicitly
//! (for example via [`load_mailmap_table`](crate::config::mailmap::load_mailmap_table));
//! the filter never reads repository config on its own.
//!
//! ```
//! use grit_lib::config::mailmap::MailmapTable;
//! use grit_lib::objects::ObjectId;
//! use grit_lib::repo::{InitOptions, Repository};
//! use grit_lib::revwalk::filter::{CommitFilter, CommitFilterBuilder};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let dir = tempfile::tempdir()?;
//! let repo = Repository::init(dir.path(), &InitOptions::default())?;
//! let filter = CommitFilterBuilder::new()
//!     .author("Alice")
//!     .build()?;
//! let _matches = filter.is_active();
//! # let _ = (repo, filter);
//! # Ok(())
//! # }
//! ```

use std::borrow::Cow;
use std::sync::Arc;

use regex::bytes::{Regex, RegexBuilder, RegexSet, RegexSetBuilder};

use crate::config::mailmap::{map_contact_table, parse_contact, MailmapTable};
use crate::error::{Error, Result};
use crate::objects::{ObjectId, ObjectKind};
use crate::repo::Repository;

/// A single user-supplied match pattern (BRE or fixed string).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pattern(String);

impl Pattern {
    /// Wrap a pattern string from the caller.
    #[must_use]
    pub fn new(raw: impl Into<String>) -> Self {
        Self(raw.into())
    }

    /// Borrow the raw pattern text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<String> for Pattern {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for Pattern {
    fn from(s: &str) -> Self {
        Self(s.to_owned())
    }
}

/// Invalid commit-filter pattern or compilation failure.
#[derive(Debug, thiserror::Error)]
pub enum CommitFilterError {
    /// A pattern could not be compiled.
    #[error("invalid commit filter pattern `{pattern}`: {detail}")]
    InvalidPattern {
        /// Original pattern text.
        pattern: String,
        /// Underlying regex error message.
        detail: String,
    },
}

/// Compiled commit filter applied during [`crate::rev_list::rev_list`].
#[derive(Debug, Clone)]
pub struct CommitFilter {
    inner: Arc<CommitFilterInner>,
}

#[derive(Debug)]
struct CommitFilterInner {
    author: Option<RegexSet>,
    committer: Option<RegexSet>,
    grep: Vec<Regex>,
    all_match: bool,
    invert_grep: bool,
    mailmap: Option<MailmapTable>,
}

impl CommitFilter {
    /// Start building a filter.
    #[must_use]
    pub fn builder() -> CommitFilterBuilder {
        CommitFilterBuilder::new()
    }

    /// Returns true when any author, committer, or grep pattern is configured.
    #[must_use]
    pub fn is_active(&self) -> bool {
        let i = &self.inner;
        i.author.is_some() || i.committer.is_some() || !i.grep.is_empty()
    }

    /// Test whether `oid` satisfies this filter (author/committer/grep/mailmap rules).
    ///
    /// # Errors
    ///
    /// Returns repository and object read errors from the ODB.
    pub fn matches(&self, repo: &Repository, oid: ObjectId) -> Result<bool> {
        let object = repo.odb().read(&oid)?;
        if object.kind != ObjectKind::Commit {
            return Err(Error::CorruptObject(format!(
                "expected commit object for {oid}, found {:?}",
                object.kind
            )));
        }
        Ok(self.matches_bytes(&object.data))
    }

    fn matches_bytes(&self, raw: &[u8]) -> bool {
        let inner = &self.inner;
        let headers = scan_commit_headers(raw);

        if let Some(set) = &inner.author {
            let ident = ident_for_pattern_match(headers.author, inner.mailmap.as_ref());
            if !set.is_match(ident.as_ref()) {
                return false;
            }
        }

        if let Some(set) = &inner.committer {
            let ident = ident_for_pattern_match(headers.committer, inner.mailmap.as_ref());
            if !set.is_match(ident.as_ref()) {
                return false;
            }
        }

        if !inner.grep.is_empty() {
            let body = commit_message_body(raw);
            let grep_ok = if inner.all_match {
                inner.grep.iter().all(|re| re.is_match(body))
            } else {
                inner.grep.iter().any(|re| re.is_match(body))
            };
            let grep_ok = if inner.invert_grep { !grep_ok } else { grep_ok };
            if !grep_ok {
                return false;
            }
        }

        true
    }
}

/// Builder for [`CommitFilter`].
#[derive(Debug, Clone, Default)]
pub struct CommitFilterBuilder {
    author: Vec<Pattern>,
    committer: Vec<Pattern>,
    grep: Vec<Pattern>,
    ignore_case: bool,
    all_match: bool,
    invert_grep: bool,
    fixed_strings: bool,
    mailmap: Option<MailmapTable>,
}

impl CommitFilterBuilder {
    /// Empty builder.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an `--author` pattern (multiple patterns OR together).
    #[must_use]
    pub fn author(mut self, pattern: impl Into<Pattern>) -> Self {
        self.author.push(pattern.into());
        self
    }

    /// Add a `--committer` pattern (multiple patterns OR together).
    #[must_use]
    pub fn committer(mut self, pattern: impl Into<Pattern>) -> Self {
        self.committer.push(pattern.into());
        self
    }

    /// Add a `--grep` pattern (OR unless [`Self::all_match`], then AND).
    #[must_use]
    pub fn grep(mut self, pattern: impl Into<Pattern>) -> Self {
        self.grep.push(pattern.into());
        self
    }

    /// Case-insensitive matching for all pattern groups (`-i`).
    #[must_use]
    pub fn ignore_case(mut self, yes: bool) -> Self {
        self.ignore_case = yes;
        self
    }

    /// Require every `--grep` pattern to match (`--all-match`).
    #[must_use]
    pub fn all_match(mut self, yes: bool) -> Self {
        self.all_match = yes;
        self
    }

    /// Invert the `--grep` group only (`--invert-grep`).
    #[must_use]
    pub fn invert_grep(mut self, yes: bool) -> Self {
        self.invert_grep = yes;
        self
    }

    /// Treat patterns as fixed strings (`--fixed-strings` / `-F`).
    #[must_use]
    pub fn fixed_strings(mut self, yes: bool) -> Self {
        self.fixed_strings = yes;
        self
    }

    /// Optional mailmap table for author/committer identity mapping.
    #[must_use]
    pub fn mailmap(mut self, table: MailmapTable) -> Self {
        self.mailmap = Some(table);
        self
    }

    /// Compile patterns into a [`CommitFilter`].
    ///
    /// # Errors
    ///
    /// Returns [`CommitFilterError::InvalidPattern`] when any pattern fails to compile.
    pub fn build(self) -> std::result::Result<CommitFilter, CommitFilterError> {
        let author = compile_pattern_set(&self.author, self.ignore_case, self.fixed_strings)?;
        let committer = compile_pattern_set(&self.committer, self.ignore_case, self.fixed_strings)?;
        let mut grep = Vec::with_capacity(self.grep.len());
        for pat in &self.grep {
            grep.push(compile_grep_pattern(
                pat.as_str(),
                self.ignore_case,
                self.fixed_strings,
            )?);
        }
        Ok(CommitFilter {
            inner: Arc::new(CommitFilterInner {
                author,
                committer,
                grep,
                all_match: self.all_match,
                invert_grep: self.invert_grep,
                mailmap: self.mailmap,
            }),
        })
    }
}

struct CommitHeaderFields<'a> {
    author: Option<&'a [u8]>,
    committer: Option<&'a [u8]>,
}

fn commit_message_body(raw: &[u8]) -> &[u8] {
    raw.windows(2)
        .position(|w| w == b"\n\n")
        .map(|pos| &raw[pos + 2..])
        .unwrap_or(&[])
}

fn scan_commit_headers(raw: &[u8]) -> CommitHeaderFields<'_> {
    let mut author = None;
    let mut committer = None;
    let mut line_start = 0usize;
    for (i, &b) in raw.iter().enumerate() {
        if b == b'\n' {
            if line_start == i {
                break;
            }
            let line = &raw[line_start..i];
            if let Some(rest) = line.strip_prefix(b"author ") {
                author = Some(rest);
            } else if let Some(rest) = line.strip_prefix(b"committer ") {
                committer = Some(rest);
            }
            line_start = i + 1;
        }
    }
    CommitHeaderFields { author, committer }
}

fn trim_end_ascii_ws(ident: &[u8]) -> &[u8] {
    let end = ident
        .iter()
        .rposition(|&b| b != b' ' && b != b'\t')
        .map_or(0, |i| i + 1);
    &ident[..end]
}

fn ident_without_timestamp_bytes(ident: &[u8]) -> &[u8] {
    let Some(gt) = ident.iter().rposition(|&b| b == b'>') else {
        return trim_end_ascii_ws(ident);
    };
    trim_end_ascii_ws(&ident[..=gt])
}

/// Identity bytes matched against `--author` / `--committer` (timestamp stripped).
fn ident_for_pattern_match<'a>(
    ident: Option<&'a [u8]>,
    mailmap: Option<&MailmapTable>,
) -> Cow<'a, [u8]> {
    let Some(ident) = ident else {
        return Cow::Borrowed(b"");
    };
    let bare = ident_without_timestamp_bytes(ident);
    let Some(table) = mailmap else {
        return Cow::Borrowed(bare);
    };
    // Mailmap rules are text; non-UTF-8 idents keep raw bytes (same as matching without mailmap).
    let Ok(bare_str) = std::str::from_utf8(bare) else {
        return Cow::Borrowed(bare);
    };
    let (name, email) = parse_contact(bare_str);
    let (name, email) = map_contact_table(name.as_deref(), email.as_deref(), table);
    if email.is_empty() {
        Cow::Owned(name.into_bytes())
    } else if name.is_empty() {
        Cow::Owned(format!("<{email}>").into_bytes())
    } else {
        Cow::Owned(format!("{name} <{email}>").into_bytes())
    }
}

fn compile_pattern_set(
    patterns: &[Pattern],
    ignore_case: bool,
    fixed_strings: bool,
) -> std::result::Result<Option<RegexSet>, CommitFilterError> {
    if patterns.is_empty() {
        return Ok(None);
    }
    let mut rust_patterns = Vec::with_capacity(patterns.len());
    for pat in patterns {
        rust_patterns.push(pattern_to_rust_regex(
            pat.as_str(),
            ignore_case,
            fixed_strings,
        )?);
    }
    RegexSetBuilder::new(&rust_patterns)
        .case_insensitive(ignore_case)
        .build()
        .map(Some)
        .map_err(|e| CommitFilterError::InvalidPattern {
            pattern: patterns
                .first()
                .map_or_else(String::new, |p| p.as_str().to_owned()),
            detail: e.to_string(),
        })
}

fn compile_grep_pattern(
    pattern: &str,
    ignore_case: bool,
    fixed_strings: bool,
) -> std::result::Result<Regex, CommitFilterError> {
    let rust_pat = pattern_to_rust_regex(pattern, ignore_case, fixed_strings)?;
    let with_newline = format!("(?m){rust_pat}");
    RegexBuilder::new(&with_newline)
        .case_insensitive(ignore_case)
        .build()
        .map_err(|e| CommitFilterError::InvalidPattern {
            pattern: pattern.to_owned(),
            detail: e.to_string(),
        })
}

fn pattern_to_rust_regex(
    pattern: &str,
    _ignore_case: bool,
    fixed_strings: bool,
) -> std::result::Result<String, CommitFilterError> {
    if fixed_strings {
        return Ok(regex::escape(pattern));
    }
    bre_translate_to_rust_regex(pattern).map_err(|detail| CommitFilterError::InvalidPattern {
        pattern: pattern.to_owned(),
        detail,
    })
}

/// Translate a Git BRE pattern into Rust `regex` syntax (not including `(?m)`).
pub(crate) fn bre_translate_to_rust_regex(pattern: &str) -> std::result::Result<String, String> {
    let mut out = String::with_capacity(pattern.len() + 8);
    let chars: Vec<char> = pattern.chars().collect();
    let mut i = 0usize;
    let mut in_bracket = false;

    while i < chars.len() {
        if in_bracket {
            if chars[i] == ']' && i > 0 {
                out.push(']');
                in_bracket = false;
                i += 1;
            } else if chars[i] == '[' {
                out.push('[');
                i += 1;
            } else if chars[i] == '\\' {
                if i + 1 < chars.len() {
                    let next = chars[i + 1];
                    out.push('\\');
                    out.push(next);
                    i += 2;
                } else {
                    out.push('\\');
                    i += 1;
                }
            } else {
                out.push(chars[i]);
                i += 1;
            }
            continue;
        }

        if chars[i] == '[' {
            out.push('[');
            in_bracket = true;
            i += 1;
            if i < chars.len() && (chars[i] == '^' || chars[i] == '!') {
                out.push(chars[i]);
                i += 1;
            }
            if i < chars.len() && chars[i] == ']' {
                out.push(']');
                i += 1;
            }
            continue;
        }

        if chars[i] == '\\' && i + 1 < chars.len() {
            match chars[i + 1] {
                '(' => {
                    out.push('(');
                    i += 2;
                }
                ')' => {
                    out.push(')');
                    i += 2;
                }
                '|' => {
                    out.push('|');
                    i += 2;
                }
                '+' => {
                    out.push('+');
                    i += 2;
                }
                '?' => {
                    out.push('?');
                    i += 2;
                }
                '{' => {
                    out.push('{');
                    i += 2;
                }
                '}' => {
                    out.push('}');
                    i += 2;
                }
                _ => {
                    out.push('\\');
                    out.push(chars[i + 1]);
                    i += 2;
                }
            }
            continue;
        }

        if matches!(chars[i], '(' | ')' | '|' | '+' | '?' | '{' | '}') {
            out.push('\\');
            out.push(chars[i]);
            i += 1;
            continue;
        }

        out.push(chars[i]);
        i += 1;
    }

    if in_bracket {
        return Err("unterminated bracket expression".to_owned());
    }

    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bre_metachars_become_literals_or_operators() {
        assert_eq!(
            bre_translate_to_rust_regex("fix(parser)").unwrap(),
            r"fix\(parser\)"
        );
        assert_eq!(bre_translate_to_rust_regex("a+b").unwrap(), r"a\+b");
        assert_eq!(bre_translate_to_rust_regex(r"\(x\|y\)").unwrap(), "(x|y)");
    }

    #[test]
    fn bre_bracket_class_preserved() {
        assert_eq!(bre_translate_to_rust_regex("[ab]").unwrap(), "[ab]");
        assert_eq!(bre_translate_to_rust_regex("[^ab]").unwrap(), "[^ab]");
    }

    #[test]
    fn matcher_author_or_and_grep() {
        let filter = CommitFilterBuilder::new()
            .author("Alice")
            .grep("fix")
            .build()
            .unwrap();
        let commit =
            b"tree abc\nauthor Alice <a@b.c> 1 +0000\ncommitter Alice <a@b.c> 1 +0000\n\nfix bug\n";
        assert!(filter.matches_bytes(commit));
        let other =
            b"tree abc\nauthor Bob <b@b.c> 1 +0000\ncommitter Bob <b@b.c> 1 +0000\n\nfix bug\n";
        assert!(!filter.matches_bytes(other));
    }

    #[test]
    fn grep_all_match_and_invert() {
        let both = CommitFilterBuilder::new()
            .grep("fix")
            .grep("bug")
            .all_match(true)
            .build()
            .unwrap();
        let msg = b"tree t\nauthor A <a> 1 +0000\ncommitter A <a> 1 +0000\n\nfix bug\n";
        assert!(both.matches_bytes(msg));
        let partial = b"tree t\nauthor A <a> 1 +0000\ncommitter A <a> 1 +0000\n\nother only\n";
        assert!(!both.matches_bytes(partial));

        let invert = CommitFilterBuilder::new()
            .grep("fix")
            .invert_grep(true)
            .build()
            .unwrap();
        assert!(!invert.matches_bytes(msg));
        assert!(invert.matches_bytes(partial));
    }

    #[test]
    fn fixed_strings_escape_regex() {
        let filter = CommitFilterBuilder::new()
            .grep("a+b")
            .fixed_strings(true)
            .build()
            .unwrap();
        let hit = b"tree t\nauthor A <a> 1 +0000\ncommitter A <a> 1 +0000\n\ncontains a+b\n";
        assert!(filter.matches_bytes(hit));
        let miss = b"tree t\nauthor A <a> 1 +0000\ncommitter A <a> 1 +0000\n\naaaab\n";
        assert!(!filter.matches_bytes(miss));
    }

    #[test]
    fn non_utf8_author_name_matches_email_pattern() {
        let filter = CommitFilterBuilder::new()
            .author("alice@example.com")
            .build()
            .unwrap();
        let commit = b"tree abc\nauthor \xFFAlice <alice@example.com> 1 +0000\ncommitter C <c@x.com> 1 +0000\n\nm\n";
        assert!(filter.matches_bytes(commit));
    }

    #[test]
    fn mailmap_applied_to_author_match() {
        let mut table = MailmapTable::default();
        crate::config::mailmap::read_mailmap_string(
            &mut table,
            "Proper Name <proper@email.com> <old@email.com>",
        );
        let filter = CommitFilterBuilder::new()
            .author("Proper Name")
            .mailmap(table)
            .build()
            .unwrap();
        let raw = b"tree t\nauthor Wrong <old@email.com> 1 +0000\ncommitter Wrong <old@email.com> 1 +0000\n\nm\n";
        assert!(filter.matches_bytes(raw));
    }
}