barad-dur 0.18.0

The all-seeing repository analyzer
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
use std::path::Path;

/// Default file extensions excluded from analysis (translation/resource files).
/// These files change together by definition and inflate coupling/churn metrics.
const DEFAULT_EXCLUDE_EXTENSIONS: &[&str] = &[
    // Translation / resource files
    "resx", "po", "pot", "xlf", "xliff", "strings", "arb", "lproj",
    // Documentation files
    "md", "txt", "rst", "adoc", "textile",
];

/// Default compound extensions excluded from analysis (generated files).
/// These use suffix matching on the full filename (e.g. "pb.go" matches "user.pb.go").
const DEFAULT_EXCLUDE_COMPOUND_EXTENSIONS: &[&str] = &[
    // Protocol Buffers generated (compound extensions)
    "pb.go",
    "pb.h",
    "pb.cc",
    "pb.swift",
    // C# generated
    "g.cs",
    "generated.cs",
    // TypeScript declarations
    "d.ts",
    // Minified assets
    "min.js",
    "min.css",
];

/// Default path patterns excluded from analysis (tooling config, lockfiles).
/// Lockfiles inflate churn/coupling metrics without reflecting real code changes.
const DEFAULT_EXCLUDE_PATTERNS: &[&str] = &[
    // Lockfiles — use **/ prefix so nested paths (monorepos) are also excluded
    "**/pnpm-lock.yaml",
    "**/package-lock.json",
    "**/yarn.lock",
    "**/Cargo.lock",
    "**/Gemfile.lock",
    "**/poetry.lock",
    "**/composer.lock",
    "**/go.sum",
    "**/flake.lock",
    "**/*.lock",
    // App / environment config (churn noise, not real code changes)
    "**/appsettings*.json",
    "**/launchSettings.json",
    "**/.env*",
    // Tooling directories
    ".claude/**",
    ".cursor/**",
    ".idea/**",
    ".vscode/**",
    // ORM migrations / generated schemas (auto-generated, inflate churn)
    "**/Migrations/*.Designer.cs",
    "**/Migrations/*ModelSnapshot.cs",
    "**/migrations/*.py",
    "db/schema.rb",
    "prisma/migrations/**",
    "alembic/versions/**",
    // Internationalization / translation directories
    "**/i18n/**",
    "**/l10n/**",
    "**/locales/**",
    "**/locale/**",
    // Generated build artefact directories
    "**/node_modules/**",
    "**/vendor/**",
    "**/__pycache__/**",
    "**/*.egg-info/**",
    "**/target/**",
    "**/.next/**",
    "**/.nuxt/**",
    "**/out/**",
    "**/gen/**",
    "**/generated/**",
    "**/.gradle/**",
    "**/.mvn/**",
    "**/build/**",
    // Python protobuf generated (compound suffix, not a plain extension)
    "**/*_pb2.py",
];

/// Returns true if the file should be excluded based on the given glob patterns,
/// user-specified extensions, and (optionally) the built-in default extension list.
pub fn is_excluded(
    path: &Path,
    patterns: &[String],
    extensions: &[String],
    use_defaults: bool,
) -> bool {
    let path_str = path.to_string_lossy();
    let path_lower = path_str.to_lowercase();

    // Check built-in defaults (by extension and path pattern)
    if use_defaults {
        if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
            let ext_lower = ext.to_lowercase();
            if DEFAULT_EXCLUDE_EXTENSIONS.iter().any(|&e| e == ext_lower) {
                return true;
            }
        }
        if DEFAULT_EXCLUDE_COMPOUND_EXTENSIONS
            .iter()
            .any(|&e| path_lower.ends_with(&format!(".{}", e)))
        {
            return true;
        }
        if DEFAULT_EXCLUDE_PATTERNS
            .iter()
            .any(|p| glob_match::glob_match(p, &path_str))
        {
            return true;
        }
    }

    // Check user-specified extensions (simple and compound, e.g. "jar", "min.js")
    if !extensions.is_empty() {
        for ext in extensions {
            let ext_lower = ext.trim_start_matches('.').to_lowercase();
            if path_lower.ends_with(&format!(".{}", ext_lower)) {
                return true;
            }
        }
    }

    // Check user-provided glob patterns
    for pattern in patterns {
        if glob_match::glob_match(pattern, &path_str) {
            return true;
        }
    }

    false
}

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

    #[test]
    fn is_excluded_matches_default_extensions() {
        let p = Path::new("src/Resources/Strings.resx");
        assert!(is_excluded(p, &[], &[], true));
        assert!(!is_excluded(p, &[], &[], false));
    }

    #[test]
    fn is_excluded_matches_po_files() {
        assert!(is_excluded(
            Path::new("locale/fr/messages.po"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(Path::new("lang/en.pot"), &[], &[], true));
        assert!(is_excluded(Path::new("i18n/strings.xlf"), &[], &[], true));
    }

    #[test]
    fn is_excluded_matches_user_globs() {
        let patterns = vec!["**/i18n/**".to_string()];
        assert!(is_excluded(
            Path::new("src/assets/i18n/sfk-messages/fr-FR.ts"),
            &patterns,
            &[],
            false
        ));
        assert!(!is_excluded(
            Path::new("src/main.rs"),
            &patterns,
            &[],
            false
        ));
    }

    #[test]
    fn is_excluded_combines_defaults_and_user_patterns() {
        let patterns = vec!["**/i18n/**".to_string()];
        // Matched by default extension
        assert!(is_excluded(Path::new("foo.resx"), &patterns, &[], true));
        // Matched by user pattern
        assert!(is_excluded(
            Path::new("src/i18n/en.ts"),
            &patterns,
            &[],
            true
        ));
        // Not matched by either
        assert!(!is_excluded(Path::new("src/main.rs"), &patterns, &[], true));
    }

    #[test]
    fn is_excluded_matches_config_files_by_default() {
        assert!(is_excluded(
            Path::new("src/server/BusinessHub.API/appsettings.json"),
            &[],
            &[],
            true,
        ));
        assert!(is_excluded(
            Path::new("src/server/BusinessHub.API/appsettings.Development.json"),
            &[],
            &[],
            true,
        ));
        assert!(is_excluded(
            Path::new("Properties/launchSettings.json"),
            &[],
            &[],
            true,
        ));
        assert!(is_excluded(Path::new("some/path/foo.lock"), &[], &[], true));
        assert!(is_excluded(Path::new(".env.production"), &[], &[], true));
        // Regular JSON should NOT be excluded
        assert!(!is_excluded(
            Path::new("src/data/schema.json"),
            &[],
            &[],
            true
        ));
    }

    #[test]
    fn is_excluded_matches_i18n_directories_by_default() {
        assert!(is_excluded(
            Path::new("src/client/src/assets/i18n/sfk-messages/en-US.ts"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("app/l10n/strings_fr.arb"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("src/locales/en.json"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("config/locale/fr.yml"),
            &[],
            &[],
            true
        ));
        // Non-i18n .ts files should NOT be excluded
        assert!(!is_excluded(Path::new("src/main.ts"), &[], &[], true));
    }

    #[test]
    fn is_excluded_case_insensitive_extension() {
        assert!(is_excluded(Path::new("Strings.RESX"), &[], &[], true));
        assert!(is_excluded(Path::new("lang.Resx"), &[], &[], true));
    }

    #[test]
    fn is_excluded_matches_documentation_files() {
        assert!(is_excluded(Path::new("README.md"), &[], &[], true));
        assert!(is_excluded(Path::new("docs/guide.rst"), &[], &[], true));
        assert!(is_excluded(Path::new("CHANGELOG.txt"), &[], &[], true));
        assert!(is_excluded(Path::new("docs/api.adoc"), &[], &[], true));
        assert!(is_excluded(Path::new("notes.textile"), &[], &[], true));
        // Not excluded when defaults disabled
        assert!(!is_excluded(Path::new("README.md"), &[], &[], false));
    }

    #[test]
    fn is_excluded_matches_default_lockfiles() {
        assert!(is_excluded(Path::new("pnpm-lock.yaml"), &[], &[], true));
        assert!(is_excluded(Path::new("package-lock.json"), &[], &[], true));
        assert!(is_excluded(Path::new("yarn.lock"), &[], &[], true));
        assert!(is_excluded(Path::new("Cargo.lock"), &[], &[], true));
        assert!(is_excluded(Path::new("go.sum"), &[], &[], true));
        assert!(is_excluded(Path::new("poetry.lock"), &[], &[], true));
        // Not excluded when defaults disabled
        assert!(!is_excluded(Path::new("pnpm-lock.yaml"), &[], &[], false));
    }

    #[test]
    fn is_excluded_matches_nested_lockfiles() {
        // Monorepo layout: lockfiles in subdirectories must also be excluded
        assert!(is_excluded(
            Path::new("apps/web/pnpm-lock.yaml"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("packages/ui/package-lock.json"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("services/api/go.sum"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(Path::new("backend/Cargo.lock"), &[], &[], true));
    }

    #[test]
    fn is_excluded_matches_orm_generated_files() {
        // EF Core
        assert!(is_excluded(
            Path::new("Data/Migrations/20240101_Init.Designer.cs"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("Data/Migrations/AppDbContextModelSnapshot.cs"),
            &[],
            &[],
            true
        ));
        // Django
        assert!(is_excluded(
            Path::new("myapp/migrations/0001_initial.py"),
            &[],
            &[],
            true
        ));
        // Rails
        assert!(is_excluded(Path::new("db/schema.rb"), &[], &[], true));
        // Prisma
        assert!(is_excluded(
            Path::new("prisma/migrations/20240101/migration.sql"),
            &[],
            &[],
            true
        ));
        // Regular source should not match
        assert!(!is_excluded(
            Path::new("src/Models/User.cs"),
            &[],
            &[],
            true
        ));
    }

    #[test]
    fn is_excluded_matches_default_tooling_dirs() {
        assert!(is_excluded(
            Path::new(".claude/settings.json"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new(".cursor/rules/my-rule"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new(".idea/workspace.xml"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new(".vscode/settings.json"),
            &[],
            &[],
            true
        ));
        // Not excluded when defaults disabled
        assert!(!is_excluded(
            Path::new(".claude/settings.json"),
            &[],
            &[],
            false
        ));
    }

    #[test]
    fn is_excluded_by_user_extension_simple() {
        let exts = vec!["jar".to_string()];
        assert!(is_excluded(Path::new("lib/commons.jar"), &[], &exts, false));
        assert!(is_excluded(Path::new("deps/app.JAR"), &[], &exts, false));
        assert!(!is_excluded(Path::new("src/main.rs"), &[], &exts, false));
    }

    #[test]
    fn is_excluded_by_user_extension_compound() {
        let exts = vec!["min.js".to_string()];
        assert!(is_excluded(
            Path::new("dist/bundle.min.js"),
            &[],
            &exts,
            false
        ));
        assert!(!is_excluded(Path::new("src/app.js"), &[], &exts, false));
    }

    #[test]
    fn is_excluded_extension_leading_dot_normalised() {
        // Users may write ".jar" or "jar" — both should work.
        let exts = vec![".jar".to_string()];
        assert!(is_excluded(Path::new("lib/foo.jar"), &[], &exts, false));
    }

    #[test]
    fn is_excluded_file_without_extension_not_excluded() {
        let exts = vec!["jar".to_string()];
        assert!(!is_excluded(Path::new("Makefile"), &[], &exts, false));
        assert!(!is_excluded(Path::new("Dockerfile"), &[], &exts, false));
        assert!(!is_excluded(Path::new("LICENSE"), &[], &exts, false));
    }

    #[test]
    fn is_excluded_dotted_directory_not_confused_with_extension() {
        let exts = vec!["2".to_string()];
        // "src/v1.2/main.rs" ends with ".rs", not ".2"
        assert!(!is_excluded(
            Path::new("src/v1.2/main.rs"),
            &[],
            &exts,
            false
        ));
        // but a file literally named "v1.2" (last extension is "2") is excluded
        assert!(is_excluded(Path::new("src/v1.2"), &[], &exts, false));
    }

    #[test]
    fn is_excluded_extension_independent_of_defaults() {
        // User extensions work even when defaults are off.
        let exts = vec!["jar".to_string()];
        assert!(is_excluded(Path::new("lib/foo.jar"), &[], &exts, false));
        // And do not suppress defaults when on.
        assert!(is_excluded(Path::new("README.md"), &[], &exts, true));
    }

    #[test]
    fn is_excluded_matches_generated_directories() {
        assert!(is_excluded(
            Path::new("node_modules/lodash/index.js"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("vendor/github.com/foo/bar.go"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("src/__pycache__/utils.cpython-311.pyc"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("myapp.egg-info/PKG-INFO"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("target/debug/build/out/main.rs"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new(".next/server/pages/index.js"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new(".nuxt/components.d.ts"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(Path::new("out/Release/chrome"), &[], &[], true));
        assert!(is_excluded(
            Path::new("src/gen/proto/user.go"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("src/generated/api/client.ts"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(Path::new(".gradle/caches/foo"), &[], &[], true));
        assert!(is_excluded(
            Path::new(".mvn/wrapper/maven-wrapper.jar"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("build/outputs/apk/debug.apk"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(Path::new("proto/user_pb2.py"), &[], &[], true));
        // dist is intentionally NOT excluded
        assert!(!is_excluded(Path::new("dist/published.js"), &[], &[], true));
        // regular source must NOT be excluded
        assert!(!is_excluded(Path::new("src/main.rs"), &[], &[], true));
        // use_defaults=false disables these
        assert!(!is_excluded(
            Path::new("node_modules/foo/bar.js"),
            &[],
            &[],
            false
        ));
    }

    #[test]
    fn is_excluded_matches_generated_extensions() {
        // Protocol Buffers
        assert!(is_excluded(Path::new("proto/user.pb.go"), &[], &[], true));
        assert!(is_excluded(Path::new("proto/user.pb.h"), &[], &[], true));
        assert!(is_excluded(Path::new("proto/user.pb.cc"), &[], &[], true));
        assert!(is_excluded(
            Path::new("proto/user.pb.swift"),
            &[],
            &[],
            true
        ));
        // C# generated
        assert!(is_excluded(
            Path::new("src/Api/Client.g.cs"),
            &[],
            &[],
            true
        ));
        assert!(is_excluded(
            Path::new("src/Api/Client.generated.cs"),
            &[],
            &[],
            true
        ));
        // TypeScript declarations
        assert!(is_excluded(Path::new("types/index.d.ts"), &[], &[], true));
        // Minified assets
        assert!(is_excluded(Path::new("dist/app.min.js"), &[], &[], true));
        assert!(is_excluded(
            Path::new("dist/styles.min.css"),
            &[],
            &[],
            true
        ));
        // Regular source should still pass
        assert!(!is_excluded(Path::new("src/main.rs"), &[], &[], true));
        assert!(!is_excluded(Path::new("src/user.go"), &[], &[], true));
        assert!(!is_excluded(Path::new("src/client.ts"), &[], &[], true));
        // use_defaults=false: generated extensions should NOT be excluded
        assert!(!is_excluded(Path::new("proto/user.pb.go"), &[], &[], false));
    }
}