le-change 0.3.1

Ultra-fast Git change detection library with zero-cost abstractions
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//! Unified pattern loading from source files and YAML

use crate::error::{Error, Result};
use crate::patterns::matcher::PatternMatcher;
use crate::types::GroupByKey;
use std::collections::HashMap;
use std::path::Path;

/// A named pattern group loaded from YAML
pub struct PatternGroup {
    /// Group name (YAML key)
    pub name: String,
    /// Compiled pattern matcher for this group
    pub matcher: PatternMatcher,
}

/// Unified pattern loader for source files and YAML
pub struct PatternLoader;

impl PatternLoader {
    /// Load patterns from a source file (one pattern per line)
    ///
    /// Lines starting with `#` are comments and are skipped.
    /// Empty lines are skipped.
    /// Trailing `/` is transformed to `/**`.
    pub fn load_from_file<'buf>(path: &str, buf: &'buf mut String) -> Result<Vec<&'buf str>> {
        *buf = std::fs::read_to_string(path).map_err(|e| {
            Error::Pattern(format!("Failed to read pattern file '{}': {}", path, e))
        })?;

        Ok(buf
            .lines()
            .map(|line| line.trim())
            .filter(|line| !line.is_empty() && !line.starts_with('#'))
            .collect())
    }

    /// Load YAML pattern groups
    ///
    /// YAML format:
    /// ```yaml
    /// frontend:
    ///   - src/components/**
    ///   - src/pages/**
    /// backend:
    ///   - src/api/**
    ///   - src/models/**
    /// ```
    pub fn load_yaml_groups(yaml: &str, negation_first: bool) -> Result<Vec<PatternGroup>> {
        let groups: HashMap<String, Vec<String>> =
            serde_norway::from_str(yaml).map_err(|e| Error::Yaml(e.to_string()))?;

        let mut result = Vec::with_capacity(groups.len());

        for (name, patterns) in groups {
            let pattern_refs: Vec<&str> = patterns.iter().map(|s| s.as_str()).collect();

            // Separate include and exclude patterns (exclude starts with !)
            let mut includes = Vec::new();
            let mut excludes = Vec::new();

            for pattern in &pattern_refs {
                if let Some(stripped) = pattern.strip_prefix('!') {
                    excludes.push(stripped);
                } else {
                    includes.push(*pattern);
                }
            }

            let matcher = PatternMatcher::new(&includes, &excludes, negation_first)?;
            result.push(PatternGroup { name, matcher });
        }

        Ok(result)
    }

    /// Parse a `files_group_by` template string.
    ///
    /// Template must contain exactly one `{group}` placeholder.
    /// Returns prefix (before `{group}`), suffix (after `{group}`), and the
    /// scan directory (prefix without trailing `/`).
    ///
    /// Example: `"stacks/{group}/**"` → prefix=`"stacks/"`, suffix=`"/**"`, scan_dir=`"stacks"`
    pub fn parse_group_by_template(template: &str) -> Result<GroupByTemplate<'_>> {
        let marker = "{group}";
        let first = template.find(marker).ok_or_else(|| {
            Error::Config(format!(
                "files_group_by template '{}' must contain '{{group}}'",
                template
            ))
        })?;

        // Check for duplicate
        if template[first + marker.len()..].contains(marker) {
            return Err(Error::Config(format!(
                "files_group_by template '{}' must contain exactly one '{{group}}'",
                template
            )));
        }

        let prefix = &template[..first];
        let suffix = &template[first + marker.len()..];

        // scan_dir = prefix without trailing separator
        let scan_dir = prefix.trim_end_matches('/');
        let scan_dir = if scan_dir.is_empty() { "." } else { scan_dir };

        Ok(GroupByTemplate {
            prefix,
            suffix,
            scan_dir,
        })
    }

    /// Discover groups from a template by scanning the filesystem.
    ///
    /// When the template suffix contains `**` (e.g. `stacks/{group}/**`), scans
    /// only direct children of `scan_dir` — each top-level directory becomes a group.
    ///
    /// When the suffix does NOT contain `**` (e.g. `stacks/{group}/Pulumi.yaml`),
    /// recursively walks the directory tree to find all paths where the suffix matches
    /// an existing file. This allows `{group}` to capture multi-segment paths like
    /// `cloudsql/bq_to_cloudsql/gcs_to_sql`. Each discovered directory becomes a group
    /// with a `/**` pattern covering its entire subtree.
    pub fn discover_groups_from_template(
        template: &GroupByTemplate<'_>,
        repo_root: &Path,
        negation_first: bool,
        key_mode: GroupByKey,
    ) -> Result<Vec<PatternGroup>> {
        let scan_path = repo_root.join(template.scan_dir);
        if !scan_path.is_dir() {
            return Err(Error::Config(format!(
                "files_group_by scan directory '{}' does not exist",
                template.scan_dir
            )));
        }

        let needs_recursive = !template.suffix.contains("**");

        let mut groups = Vec::new();

        if needs_recursive {
            Self::discover_groups_recursive(
                &scan_path,
                &scan_path,
                template,
                repo_root,
                &mut groups,
                negation_first,
                key_mode,
            )?;
        } else {
            Self::discover_groups_single_level(
                &scan_path,
                template,
                &mut groups,
                negation_first,
                key_mode,
            )?;
        }

        // Sort by name for deterministic order
        groups.sort_by(|a, b| a.name.cmp(&b.name));

        Ok(groups)
    }

    /// Single-level directory scan (original behavior for `**` suffixes).
    fn discover_groups_single_level(
        scan_path: &Path,
        template: &GroupByTemplate<'_>,
        groups: &mut Vec<PatternGroup>,
        negation_first: bool,
        key_mode: GroupByKey,
    ) -> Result<()> {
        let entries = std::fs::read_dir(scan_path).map_err(|e| {
            Error::Config(format!(
                "Failed to read directory '{}': {}",
                scan_path.display(),
                e
            ))
        })?;

        for entry in entries {
            let entry = entry
                .map_err(|e| Error::Config(format!("Failed to read directory entry: {}", e)))?;

            let ft = entry
                .file_type()
                .map_err(|e| Error::Config(format!("Failed to read file type: {}", e)))?;

            if !ft.is_dir() {
                continue;
            }

            let dir_name = entry.file_name();
            let dir_name_str = dir_name.to_string_lossy();

            // Skip hidden directories
            if dir_name_str.starts_with('.') {
                continue;
            }

            // Build the pattern: prefix + dir_name + suffix
            let pattern = format!("{}{}{}", template.prefix, dir_name_str, template.suffix);

            let key = Self::build_group_key(&dir_name_str, &dir_name_str, template, key_mode);

            let matcher = PatternMatcher::new(&[pattern.as_str()], &[], negation_first)?;
            groups.push(PatternGroup { name: key, matcher });
        }

        Ok(())
    }

    /// Recursive directory walk for suffix patterns without `**`.
    ///
    /// Finds all directories under `scan_root` where `prefix + relative_path + suffix`
    /// matches an existing file. Each match becomes a group with a `/**` subtree pattern.
    fn discover_groups_recursive(
        current: &Path,
        scan_root: &Path,
        template: &GroupByTemplate<'_>,
        repo_root: &Path,
        groups: &mut Vec<PatternGroup>,
        negation_first: bool,
        key_mode: GroupByKey,
    ) -> Result<()> {
        let entries = match std::fs::read_dir(current) {
            Ok(e) => e,
            Err(_) => return Ok(()),
        };

        for entry in entries {
            let entry = entry
                .map_err(|e| Error::Config(format!("Failed to read directory entry: {}", e)))?;

            let ft = entry
                .file_type()
                .map_err(|e| Error::Config(format!("Failed to read file type: {}", e)))?;

            if !ft.is_dir() {
                continue;
            }

            let dir_name = entry.file_name();
            let dir_name_str = dir_name.to_string_lossy();

            if dir_name_str.starts_with('.') {
                continue;
            }

            let dir_path = entry.path();
            let relative = dir_path
                .strip_prefix(scan_root)
                .unwrap_or(dir_path.as_path());
            // Windows: strip_prefix yields backslash separators, which would
            // corrupt both the glob pattern and the group key. Normalize.
            let relative_lossy = relative.to_string_lossy();
            let relative_str = crate::platform::PathUtil::to_posix(&relative_lossy);

            // Check if prefix + relative + suffix exists as a file
            let candidate = format!("{}{}{}", template.prefix, relative_str, template.suffix);
            let candidate_path = repo_root.join(&candidate);

            if candidate_path.exists() && !candidate_path.is_dir() {
                // Found a match — create a group covering the entire subtree
                let subtree_pattern = format!("{}{}/**", template.prefix, relative_str);

                let key = Self::build_group_key(&relative_str, &relative_str, template, key_mode);

                let matcher =
                    PatternMatcher::new(&[subtree_pattern.as_str()], &[], negation_first)?;
                groups.push(PatternGroup { name: key, matcher });
            }

            // Continue recursing into subdirectories
            Self::discover_groups_recursive(
                &dir_path,
                scan_root,
                template,
                repo_root,
                groups,
                negation_first,
                key_mode,
            )?;
        }

        Ok(())
    }

    /// Synthesize groups for template-matching paths whose directories no
    /// longer exist on disk (vanished or fully-deleted stacks). Filesystem
    /// discovery cannot see them, so Destroy deploy entries would otherwise
    /// never appear. Appends only groups whose key is not already present;
    /// re-sorts for deterministic order.
    pub fn synthesize_groups_for_paths<'p>(
        template: &GroupByTemplate<'_>,
        key_mode: GroupByKey,
        paths: impl Iterator<Item = &'p str>,
        negation_first: bool,
        groups: &mut Vec<PatternGroup>,
    ) -> Result<()> {
        let single_level = template.suffix.contains("**");
        for path in paths {
            let Some(rel) = path.strip_prefix(template.prefix) else {
                continue;
            };
            let (group_rel, pattern) = if single_level {
                // e.g. template `stacks/{group}/**`: group = first component
                if !rel.contains('/') {
                    continue; // file sits at the scan root, not in a group dir
                }
                let Some(first) = rel.split('/').next().filter(|c| !c.is_empty()) else {
                    continue;
                };
                (
                    first.to_string(),
                    format!("{}{}{}", template.prefix, first, template.suffix),
                )
            } else {
                // e.g. template `stacks/{group}/Pulumi.yaml`: path must equal
                // prefix + rel_dir + suffix; group covers the subtree
                let Some(rel_dir) = rel.strip_suffix(template.suffix).filter(|d| !d.is_empty())
                else {
                    continue;
                };
                (
                    rel_dir.to_string(),
                    format!("{}{}/**", template.prefix, rel_dir),
                )
            };
            let key = Self::build_group_key(&group_rel, &group_rel, template, key_mode);
            if groups.iter().any(|g| g.name == key) {
                continue;
            }
            let matcher = PatternMatcher::new(&[pattern.as_str()], &[], negation_first)?;
            groups.push(PatternGroup { name: key, matcher });
        }
        groups.sort_by(|a, b| a.name.cmp(&b.name));
        Ok(())
    }

    /// Build a group key based on the key mode.
    fn build_group_key(
        name: &str,
        relative_path: &str,
        template: &GroupByTemplate<'_>,
        key_mode: GroupByKey,
    ) -> String {
        match key_mode {
            GroupByKey::Name => name.to_string(),
            GroupByKey::Path => {
                if template.scan_dir == "." {
                    relative_path.to_string()
                } else {
                    format!("{}/{}", template.scan_dir, relative_path)
                }
            }
            GroupByKey::Hash => {
                use std::collections::hash_map::DefaultHasher;
                use std::hash::{Hash, Hasher};
                let mut hasher = DefaultHasher::new();
                relative_path.hash(&mut hasher);
                format!("{:08x}", hasher.finish() as u32)
            }
        }
    }
}

/// Parsed `files_group_by` template
pub struct GroupByTemplate<'a> {
    /// Text before `{group}` (e.g. `"stacks/"`)
    pub prefix: &'a str,
    /// Text after `{group}` (e.g. `"/**"`)
    pub suffix: &'a str,
    /// Directory to scan for groups (prefix without trailing `/`)
    pub scan_dir: &'a str,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_load_from_file() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "# Comment line").unwrap();
        writeln!(file, "**/*.rs").unwrap();
        writeln!(file).unwrap();
        writeln!(file, "src/**/*.ts").unwrap();
        writeln!(file, "  # Another comment  ").unwrap();
        writeln!(file, "docs/").unwrap();

        let path = file.path().to_str().unwrap().to_string();
        let mut buf = String::new();
        let patterns = PatternLoader::load_from_file(&path, &mut buf).unwrap();

        assert_eq!(patterns.len(), 3);
        assert_eq!(patterns[0], "**/*.rs");
        assert_eq!(patterns[1], "src/**/*.ts");
        assert_eq!(patterns[2], "docs/");
    }

    #[test]
    fn test_load_yaml_groups() {
        let yaml = r#"
frontend:
  - "src/components/**"
  - "src/pages/**"
  - "!src/components/test/**"
backend:
  - "src/api/**"
"#;

        let groups = PatternLoader::load_yaml_groups(yaml, true).unwrap();
        assert_eq!(groups.len(), 2);

        // Verify names exist (order may vary due to HashMap)
        let names: Vec<&str> = groups.iter().map(|g| g.name.as_str()).collect();
        assert!(names.contains(&"frontend"));
        assert!(names.contains(&"backend"));

        // Verify matching works
        let frontend = groups.iter().find(|g| g.name == "frontend").unwrap();
        assert!(frontend.matcher.matches_sync("src/components/Button.tsx"));
        assert!(!frontend
            .matcher
            .matches_sync("src/components/test/Button.test.tsx"));
        assert!(!frontend.matcher.matches_sync("src/api/routes.ts"));
    }

    #[test]
    fn test_load_yaml_invalid() {
        let result = PatternLoader::load_yaml_groups("not: [valid: yaml", true);
        assert!(result.is_err());
    }

    // --- files_group_by template tests ---

    #[test]
    fn test_parse_template_basic() {
        let t = PatternLoader::parse_group_by_template("stacks/{group}/**").unwrap();
        assert_eq!(t.prefix, "stacks/");
        assert_eq!(t.suffix, "/**");
        assert_eq!(t.scan_dir, "stacks");
    }

    #[test]
    fn test_parse_template_nested() {
        let t = PatternLoader::parse_group_by_template("infra/regions/{group}/config/**").unwrap();
        assert_eq!(t.prefix, "infra/regions/");
        assert_eq!(t.suffix, "/config/**");
        assert_eq!(t.scan_dir, "infra/regions");
    }

    #[test]
    fn test_parse_template_missing_placeholder() {
        let result = PatternLoader::parse_group_by_template("stacks/**");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_template_duplicate_placeholder() {
        let result = PatternLoader::parse_group_by_template("{group}/{group}/**");
        assert!(result.is_err());
    }

    #[test]
    fn test_discover_groups_name_mode() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join("stacks")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/dev")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/staging")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/prod")).unwrap();

        let t = PatternLoader::parse_group_by_template("stacks/{group}/**").unwrap();
        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Name)
                .unwrap();

        assert_eq!(groups.len(), 3);
        let names: Vec<&str> = groups.iter().map(|g| g.name.as_str()).collect();
        assert!(names.contains(&"dev"));
        assert!(names.contains(&"staging"));
        assert!(names.contains(&"prod"));
    }

    #[test]
    fn test_discover_groups_path_mode() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join("stacks")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/dev")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/prod")).unwrap();

        let t = PatternLoader::parse_group_by_template("stacks/{group}/**").unwrap();
        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Path)
                .unwrap();

        assert_eq!(groups.len(), 2);
        let names: Vec<&str> = groups.iter().map(|g| g.name.as_str()).collect();
        assert!(names.contains(&"stacks/dev"));
        assert!(names.contains(&"stacks/prod"));
    }

    #[test]
    fn test_discover_groups_hash_mode() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join("stacks")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/prod")).unwrap();

        let t = PatternLoader::parse_group_by_template("stacks/{group}/**").unwrap();
        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Hash)
                .unwrap();

        assert_eq!(groups.len(), 1);
        // Hash keys are 8-char hex strings
        assert_eq!(groups[0].name.len(), 8);
        assert!(groups[0].name.chars().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn test_discover_groups_skips_hidden() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join("stacks")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/prod")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/.git")).unwrap();

        let t = PatternLoader::parse_group_by_template("stacks/{group}/**").unwrap();
        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Name)
                .unwrap();

        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].name, "prod");
    }

    #[test]
    fn test_discover_groups_nonexistent_dir() {
        let dir = tempfile::tempdir().unwrap();
        let t = PatternLoader::parse_group_by_template("nonexistent/{group}/**").unwrap();
        let result =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Name);
        assert!(result.is_err());
    }

    #[test]
    fn test_discover_groups_pattern_matching() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join("stacks")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/dev")).unwrap();
        std::fs::create_dir(dir.path().join("stacks/prod")).unwrap();

        let t = PatternLoader::parse_group_by_template("stacks/{group}/**").unwrap();
        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Name)
                .unwrap();

        let dev = groups.iter().find(|g| g.name == "dev").unwrap();
        assert!(dev.matcher.matches_sync("stacks/dev/config.yaml"));
        assert!(!dev.matcher.matches_sync("stacks/prod/config.yaml"));

        let prod = groups.iter().find(|g| g.name == "prod").unwrap();
        assert!(prod.matcher.matches_sync("stacks/prod/config.yaml"));
        assert!(!prod.matcher.matches_sync("stacks/dev/config.yaml"));
    }

    #[test]
    fn test_yaml_groups_with_trailing_slash() {
        // Patterns with trailing slashes should still work correctly.
        // The YAML loader passes patterns directly to PatternMatcher which
        // handles glob matching (trailing slash is a valid glob component).
        let yaml = r#"
infra:
  - "terraform/"
  - "stacks/prod/**"
"#;

        let groups = PatternLoader::load_yaml_groups(yaml, true).unwrap();
        assert_eq!(groups.len(), 1);

        let infra = groups.iter().find(|g| g.name == "infra").unwrap();
        // "stacks/prod/**" should match nested files
        assert!(infra.matcher.matches_sync("stacks/prod/main.tf"));
        assert!(infra.matcher.matches_sync("stacks/prod/modules/vpc.tf"));
        // "terraform/" as a glob — it matches the literal directory name
        // (glob behavior: trailing slash matches directory entries)
        assert!(!infra.matcher.matches_sync("stacks/dev/main.tf"));
    }

    #[test]
    fn test_discover_groups_recursive_deeply_nested() {
        let dir = tempfile::tempdir().unwrap();

        // Create deeply nested stacks with Pulumi.yaml marker files
        let paths = [
            "stacks/dev",
            "stacks/prod",
            "stacks/cloudsql/bq_to_cloudsql/gcs_to_sql",
            "stacks/cloudsql/another_stack",
            "stacks/networking/vpc",
        ];
        for p in &paths {
            std::fs::create_dir_all(dir.path().join(p)).unwrap();
            std::fs::write(dir.path().join(p).join("Pulumi.yaml"), "name: test").unwrap();
        }

        let t = PatternLoader::parse_group_by_template("stacks/{group}/Pulumi.yaml").unwrap();
        assert!(!t.suffix.contains("**")); // triggers recursive mode

        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Name)
                .unwrap();

        let names: Vec<&str> = groups.iter().map(|g| g.name.as_str()).collect();
        assert_eq!(groups.len(), 5);
        assert!(names.contains(&"dev"));
        assert!(names.contains(&"prod"));
        assert!(names.contains(&"cloudsql/bq_to_cloudsql/gcs_to_sql"));
        assert!(names.contains(&"cloudsql/another_stack"));
        assert!(names.contains(&"networking/vpc"));
    }

    #[test]
    fn test_discover_groups_recursive_pattern_matching() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join("stacks/a/b/c")).unwrap();
        std::fs::write(dir.path().join("stacks/a/b/c/Pulumi.yaml"), "name: test").unwrap();
        std::fs::create_dir_all(dir.path().join("stacks/dev")).unwrap();
        std::fs::write(dir.path().join("stacks/dev/Pulumi.yaml"), "name: dev").unwrap();

        let t = PatternLoader::parse_group_by_template("stacks/{group}/Pulumi.yaml").unwrap();
        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Name)
                .unwrap();

        // Groups should match files within their subtree
        let deep = groups.iter().find(|g| g.name == "a/b/c").unwrap();
        assert!(deep.matcher.matches_sync("stacks/a/b/c/Pulumi.yaml"));
        assert!(deep.matcher.matches_sync("stacks/a/b/c/index.ts"));
        assert!(!deep.matcher.matches_sync("stacks/dev/Pulumi.yaml"));

        let dev = groups.iter().find(|g| g.name == "dev").unwrap();
        assert!(dev.matcher.matches_sync("stacks/dev/Pulumi.yaml"));
        assert!(!dev.matcher.matches_sync("stacks/a/b/c/Pulumi.yaml"));
    }

    #[test]
    fn test_discover_groups_recursive_path_mode() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join("stacks/a/b")).unwrap();
        std::fs::write(dir.path().join("stacks/a/b/Pulumi.yaml"), "").unwrap();

        let t = PatternLoader::parse_group_by_template("stacks/{group}/Pulumi.yaml").unwrap();
        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Path)
                .unwrap();

        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].name, "stacks/a/b");
    }

    #[test]
    fn test_discover_groups_recursive_skips_dirs_without_marker() {
        let dir = tempfile::tempdir().unwrap();
        // Only some directories have the marker file
        std::fs::create_dir_all(dir.path().join("stacks/has_marker")).unwrap();
        std::fs::write(dir.path().join("stacks/has_marker/Pulumi.yaml"), "").unwrap();
        std::fs::create_dir_all(dir.path().join("stacks/no_marker")).unwrap();
        std::fs::write(dir.path().join("stacks/no_marker/other.txt"), "").unwrap();

        let t = PatternLoader::parse_group_by_template("stacks/{group}/Pulumi.yaml").unwrap();
        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Name)
                .unwrap();

        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].name, "has_marker");
    }

    #[test]
    fn test_discover_groups_star_star_suffix_stays_single_level() {
        // Backward compatibility: ** in suffix uses single-level scan
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join("stacks/dev/nested")).unwrap();
        std::fs::create_dir_all(dir.path().join("stacks/prod")).unwrap();

        let t = PatternLoader::parse_group_by_template("stacks/{group}/**").unwrap();
        let groups =
            PatternLoader::discover_groups_from_template(&t, dir.path(), true, GroupByKey::Name)
                .unwrap();

        // Should only find dev and prod (direct children), NOT dev/nested
        let names: Vec<&str> = groups.iter().map(|g| g.name.as_str()).collect();
        assert_eq!(groups.len(), 2);
        assert!(names.contains(&"dev"));
        assert!(names.contains(&"prod"));
    }

    #[test]
    fn test_yaml_groups_exclude_pattern() {
        // Verify ! exclude patterns correctly exclude files from matching
        let yaml = r#"
app:
  - "src/**"
  - "!src/test/**"
  - "!src/vendor/**"
"#;

        let groups = PatternLoader::load_yaml_groups(yaml, true).unwrap();
        assert_eq!(groups.len(), 1);

        let app = &groups[0];
        assert_eq!(app.name, "app");

        // Included paths should match
        assert!(app.matcher.matches_sync("src/main.rs"));
        assert!(app.matcher.matches_sync("src/lib/utils.rs"));

        // Excluded paths should NOT match
        assert!(!app.matcher.matches_sync("src/test/unit_test.rs"));
        assert!(!app.matcher.matches_sync("src/vendor/dep/lib.rs"));

        // Paths outside src should NOT match
        assert!(!app.matcher.matches_sync("docs/README.md"));
    }
}