provenant-cli 0.0.25

Rust-based ScanCode-compatible scanner for licenses, package metadata, SBOMs, and provenance data.
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
// SPDX-FileCopyrightText: Provenant contributors
// SPDX-License-Identifier: Apache-2.0

//! Match grouping functions.

use super::LINES_THRESHOLD;
use super::types::DetectionGroup;
use crate::license_detection::models::LicenseMatch;

pub fn group_matches_by_region(matches: &[LicenseMatch]) -> Vec<DetectionGroup> {
    group_matches_by_region_with_threshold(matches, LINES_THRESHOLD)
}

/// Group matches by file region with a custom proximity threshold.
///
/// # Arguments
///
/// * `matches` - List of license matches to group, should be sorted by start_line
/// * `proximity_threshold` - Maximum line gap between matches to be in the same group
///
/// # Returns
///
/// A vector of DetectionGroup objects, each containing matches that form a region
pub(super) fn group_matches_by_region_with_threshold(
    matches: &[LicenseMatch],
    proximity_threshold: usize,
) -> Vec<DetectionGroup> {
    let mut groups = Vec::new();
    let mut current_group: Vec<LicenseMatch> = Vec::new();

    for match_item in matches {
        if current_group.is_empty() {
            current_group.push(match_item.clone());
            continue;
        }

        let previous_match = current_group.last().unwrap();

        if previous_match.is_license_intro() {
            current_group.push(match_item.clone());
        } else if previous_match.is_license_clue() || match_item.is_license_intro() {
            if !current_group.is_empty() {
                groups.push(DetectionGroup::new(current_group.clone()));
            }
            current_group = vec![match_item.clone()];
        } else if match_item.is_license_clue() {
            if !current_group.is_empty() {
                groups.push(DetectionGroup::new(current_group.clone()));
            }
            groups.push(DetectionGroup::new(vec![match_item.clone()]));
            current_group = Vec::new();
        } else if should_group_together(previous_match, match_item, proximity_threshold) {
            current_group.push(match_item.clone());
        } else {
            if !current_group.is_empty() {
                groups.push(DetectionGroup::new(current_group.clone()));
            }
            current_group = vec![match_item.clone()];
        }
    }

    if !current_group.is_empty() {
        groups.push(DetectionGroup::new(current_group));
    }

    groups
}

/// Check if two matches should be in the same group based on line proximity.
///
/// Matches are grouped together when line gap is within threshold.
///
/// Based on Python's group_matches() at detection.py:1820-1868:
/// ```python
/// is_in_group_by_threshold = license_match.start_line <= previous_match.end_line + lines_threshold
/// ```
///
/// This means: GROUP if start_line <= prev_end_line + 4 (equivalent to line_gap <= 4)
pub(super) fn should_group_together(
    prev: &LicenseMatch,
    cur: &LicenseMatch,
    threshold: usize,
) -> bool {
    let line_gap = cur.start_line.get().saturating_sub(prev.end_line.get());
    line_gap <= threshold
}

/// Sort matches by start token position with tie-breaking criteria.
///
/// Python sorts matches at match.py:1099, 1220:
/// ```python
/// sorter = lambda m: (m.qspan.start, -m.hilen(), -m.len(), m.matcher_order)
/// ```
///
/// This ensures:
/// 1. Matches appear in file order (by start position)
/// 2. Longer, more specific matches come first (for overlapping matches)
/// 3. Higher hilen (legalese tokens) prioritized
/// 4. Exact matchers take priority over approximate
pub fn sort_matches_by_line(matches: &mut [LicenseMatch]) {
    matches.sort_by(|a, b| {
        a.start_token
            .cmp(&b.start_token)
            .then_with(|| b.hilen().cmp(&a.hilen()))
            .then_with(|| b.len().cmp(&a.len()))
            .then_with(|| a.matcher_order().cmp(&b.matcher_order()))
    });
}
/// A detection is correct if:
/// - All matchers are "1-hash", "1-spdx-id", or "2-aho" (exact matchers)
/// - All match coverages are 100%
///
/// Based on Python: is_correct_detection() at detection.py:1078
#[cfg(test)]
pub(super) fn is_correct_detection(matches: &[LicenseMatch]) -> bool {
    if matches.is_empty() {
        return false;
    }

    let all_valid_matchers = matches.iter().all(|m| {
        matches!(
            m.matcher,
            crate::license_detection::models::MatcherKind::Hash
                | crate::license_detection::models::MatcherKind::SpdxId
                | crate::license_detection::models::MatcherKind::Aho
        )
    });

    let all_perfect_coverage = matches.iter().all(|m| m.coverage() == 100.0);

    all_valid_matchers && all_perfect_coverage
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::license_detection::models::{LicenseMatch, MatchCoordinates, PositionSpan};
    use crate::models::LineNumber;
    use crate::models::MatchScore;

    fn create_test_match(
        start_line: usize,
        end_line: usize,
        matcher: &str,
        rule_identifier: &str,
    ) -> LicenseMatch {
        let start_line_ln = LineNumber::new(start_line).expect("valid start_line");
        let end_line_ln = LineNumber::new(end_line).expect("valid end_line");
        LicenseMatch {
            rid: 0,
            license_expression: "mit".to_string(),
            license_expression_spdx: Some("MIT".to_string()),
            from_file: Some("test.txt".to_string()),
            start_line: start_line_ln,
            end_line: end_line_ln,
            start_token: start_line,
            end_token: end_line + 1,
            matcher: matcher.parse().expect("invalid test matcher"),
            score: MatchScore::from_percentage(95.0),
            matched_length: 100,
            match_coverage: 95.0,
            rule_relevance: 100,
            rule_identifier: rule_identifier.to_string(),
            rule_url: "https://example.com".to_string(),
            matched_text: Some("MIT License".to_string()),
            referenced_filenames: None,
            rule_kind: crate::license_detection::models::RuleKind::None,
            is_from_license: false,
            rule_length: 100,
            rule_start_token: 0,
            coordinates: MatchCoordinates::query_region(PositionSpan::range(
                start_line,
                end_line + 1,
            )),
            candidate_resemblance: 0.0,
            candidate_containment: 0.0,
        }
    }

    fn create_test_match_with_tokens(
        start_line: usize,
        end_line: usize,
        start_token: usize,
        end_token: usize,
    ) -> LicenseMatch {
        let start_line_ln = LineNumber::new(start_line).expect("valid start_line");
        let end_line_ln = LineNumber::new(end_line).expect("valid end_line");
        LicenseMatch {
            rid: 0,
            license_expression: "mit".to_string(),
            license_expression_spdx: Some("MIT".to_string()),
            from_file: Some("test.txt".to_string()),
            start_line: start_line_ln,
            end_line: end_line_ln,
            start_token,
            end_token,
            matcher: crate::license_detection::models::MatcherKind::Hash,
            score: MatchScore::from_percentage(95.0),
            matched_length: 100,
            match_coverage: 95.0,
            rule_relevance: 100,
            rule_identifier: "mit.LICENSE".to_string(),
            rule_url: "https://example.com".to_string(),
            matched_text: Some("MIT License".to_string()),
            referenced_filenames: None,
            rule_kind: crate::license_detection::models::RuleKind::None,
            is_from_license: false,
            rule_length: 100,
            rule_start_token: 0,
            coordinates: MatchCoordinates::query_region(PositionSpan::range(
                start_token,
                end_token,
            )),
            candidate_resemblance: 0.0,
            candidate_containment: 0.0,
        }
    }

    #[test]
    fn test_group_matches_empty() {
        let matches = Vec::new();
        let groups = group_matches_by_region(&matches);
        assert_eq!(groups.len(), 0);
    }

    #[test]
    fn test_group_matches_single() {
        let match1 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let matches = vec![match1];
        let groups = group_matches_by_region(&matches);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].matches.len(), 1);
    }

    #[test]
    fn test_group_matches_within_threshold() {
        let match1 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let match2 = create_test_match(6, 10, "2-aho", "mit.LICENSE");
        let matches = vec![match1, match2];
        let groups = group_matches_by_region(&matches);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].matches.len(), 2);
    }

    #[test]
    fn test_group_matches_separate_by_threshold() {
        let match1 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let match2 = create_test_match(10, 15, "1-hash", "apache-2.0.LICENSE");
        let matches = vec![match1, match2];
        let groups = group_matches_by_region(&matches);
        assert_eq!(groups.len(), 2);
    }

    #[test]
    fn test_group_matches_exactly_at_line_gap_threshold() {
        let match1 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let match2 = create_test_match(8, 12, "2-aho", "mit.LICENSE");
        let matches = vec![match1, match2];
        let groups = group_matches_by_region(&matches);
        assert_eq!(groups.len(), 1, "Line gap 3 (8-5=3) should be grouped");
    }

    #[test]
    fn test_group_matches_just_past_line_gap_threshold() {
        let match1 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let match2 = create_test_match(10, 14, "2-aho", "mit.LICENSE");
        let matches = vec![match1, match2];
        let groups = group_matches_by_region(&matches);
        assert_eq!(groups.len(), 2, "Line gap 5 (10-5=5) exceeds threshold 4");
    }

    #[test]
    fn test_group_matches_far_apart() {
        let match1 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let match2 = create_test_match(20, 25, "1-hash", "apache-2.0.LICENSE");
        let matches = vec![match1, match2];
        let groups = group_matches_by_region(&matches);
        assert_eq!(groups.len(), 2);
    }

    #[test]
    fn test_group_matches_keeps_leading_clue_standalone() {
        let mut clue = create_test_match(10, 10, "2-aho", "gpl-1.0-plus_351.RULE");
        clue.rule_kind = crate::license_detection::models::RuleKind::Clue;

        let mut reference = create_test_match(10, 10, "2-aho", "gpl_bare_word_only.RULE");
        reference.rule_kind = crate::license_detection::models::RuleKind::Reference;

        let groups = group_matches_by_region(&[clue.clone(), reference.clone()]);

        assert_eq!(groups.len(), 2);
        assert_eq!(groups[0].matches, vec![clue]);
        assert_eq!(groups[1].matches, vec![reference]);
    }

    #[test]
    fn test_group_matches_keeps_trailing_clue_standalone() {
        let mut reference = create_test_match(10, 10, "2-aho", "gpl_bare_word_only.RULE");
        reference.rule_kind = crate::license_detection::models::RuleKind::Reference;

        let mut clue = create_test_match(10, 10, "2-aho", "gpl-1.0-plus_351.RULE");
        clue.rule_kind = crate::license_detection::models::RuleKind::Clue;

        let groups = group_matches_by_region(&[reference.clone(), clue.clone()]);

        assert_eq!(groups.len(), 2);
        assert_eq!(groups[0].matches, vec![reference]);
        assert_eq!(groups[1].matches, vec![clue]);
    }

    #[test]
    fn test_sort_matches_by_line() {
        let mut match1 = create_test_match(10, 15, "1-hash", "mit.LICENSE");
        match1.start_token = 100;
        let mut match2 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        match2.start_token = 10;
        let mut matches = vec![match1, match2];
        sort_matches_by_line(&mut matches);
        assert_eq!(matches[0].start_token, 10);
        assert_eq!(matches[1].start_token, 100);
    }

    #[test]
    fn test_grouping_within_both_thresholds() {
        let m1 = create_test_match_with_tokens(1, 10, 0, 50);
        let m2 = create_test_match_with_tokens(12, 20, 55, 100);
        let groups = group_matches_by_region(&[m1, m2]);
        assert_eq!(
            groups.len(),
            1,
            "Should group when line gap within threshold"
        );
    }

    #[test]
    fn test_grouping_separates_by_line_threshold() {
        let m1 = create_test_match_with_tokens(1, 10, 0, 50);
        let m2 = create_test_match_with_tokens(15, 25, 55, 100);
        let groups = group_matches_by_region(&[m1, m2]);
        assert_eq!(
            groups.len(),
            2,
            "Should separate when line gap exceeds threshold"
        );
    }

    #[test]
    fn test_grouping_at_exact_line_threshold() {
        let m1 = create_test_match_with_tokens(1, 10, 0, 50);
        let m2 = create_test_match_with_tokens(13, 20, 55, 100);
        let groups = group_matches_by_region(&[m1, m2]);
        assert_eq!(
            groups.len(),
            1,
            "Should group at exact line gap within threshold"
        );
    }

    #[test]
    fn test_group_matches_with_custom_threshold_zero() {
        let m1 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let m2 = create_test_match(5, 10, "1-hash", "mit.LICENSE");
        let m3 = create_test_match(12, 15, "1-hash", "apache.LICENSE");
        let groups =
            group_matches_by_region_with_threshold(&[m1.clone(), m2.clone(), m3.clone()], 0);
        assert_eq!(groups.len(), 2, "Threshold 0 should only group gap=0");
    }

    #[test]
    fn test_group_matches_with_custom_threshold_large() {
        let m1 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let m2 = create_test_match(50, 55, "1-hash", "mit.LICENSE");
        let groups = group_matches_by_region_with_threshold(&[m1, m2], 100);
        assert_eq!(
            groups.len(),
            1,
            "Large threshold should group distant matches"
        );
    }

    #[test]
    fn test_group_matches_threshold_exactly_at_boundary() {
        let m1 = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let m2_at_boundary = create_test_match(10, 15, "1-hash", "mit.LICENSE");
        let groups =
            group_matches_by_region_with_threshold(&[m1.clone(), m2_at_boundary.clone()], 4);
        assert_eq!(groups.len(), 2, "Threshold 4: should not group");
        let groups = group_matches_by_region_with_threshold(&[m1, m2_at_boundary], 5);
        assert_eq!(groups.len(), 1, "Threshold 5: should group");
    }

    #[test]
    fn test_is_correct_detection_perfect_hash() {
        let mut m = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        m.match_coverage = 100.0;
        let matches = vec![m];
        assert!(is_correct_detection(&matches));
    }

    #[test]
    fn test_is_correct_detection_perfect_spdx() {
        let mut m = create_test_match(1, 5, "1-spdx-id", "mit.LICENSE");
        m.match_coverage = 100.0;
        let matches = vec![m];
        assert!(is_correct_detection(&matches));
    }

    #[test]
    fn test_is_correct_detection_perfect_aho() {
        let mut m = create_test_match(1, 5, "2-aho", "mit.LICENSE");
        m.match_coverage = 100.0;
        let matches = vec![m];
        assert!(is_correct_detection(&matches));
    }

    #[test]
    fn test_is_correct_detection_multiple_perfect() {
        let mut m1 = create_test_match(1, 10, "1-hash", "#1");
        m1.match_coverage = 100.0;
        let mut m2 = create_test_match(11, 20, "1-spdx-id", "#2");
        m2.match_coverage = 100.0;
        let matches = vec![m1, m2];
        assert!(is_correct_detection(&matches));
    }

    #[test]
    fn test_is_correct_detection_imperfect_coverage() {
        let m = create_test_match(1, 5, "1-hash", "mit.LICENSE");
        let matches = vec![m];
        assert!(!is_correct_detection(&matches));
    }

    #[test]
    fn test_is_correct_detection_unknown_matcher() {
        let mut m = create_test_match(1, 5, "6-unknown", "mit.LICENSE");
        m.match_coverage = 100.0;
        let matches = vec![m];
        assert!(!is_correct_detection(&matches));
    }

    #[test]
    fn test_is_correct_detection_empty() {
        let matches: Vec<LicenseMatch> = vec![];
        assert!(!is_correct_detection(&matches));
    }

    #[test]
    fn test_grouping_separates_by_token_threshold() {
        let m1 = create_test_match_with_tokens(1, 10, 0, 50);
        let m2 = create_test_match_with_tokens(12, 20, 65, 100);
        let groups = group_matches_by_region(&[m1, m2]);
        assert_eq!(
            groups.len(),
            1,
            "Should group when line gap (2) is within threshold - token gap is not used"
        );
    }

    #[test]
    fn test_grouping_at_exact_token_threshold() {
        let m1 = create_test_match_with_tokens(1, 10, 0, 50);
        let m2 = create_test_match_with_tokens(11, 20, 60, 100);
        let groups = group_matches_by_region(&[m1, m2]);
        assert_eq!(groups.len(), 1, "Should group when line gap is 1");
    }

    #[test]
    fn test_grouping_requires_both_thresholds() {
        let m1 = create_test_match_with_tokens(1, 10, 0, 50);
        let m2 = create_test_match_with_tokens(15, 25, 65, 100);
        let groups = group_matches_by_region(&[m1, m2]);
        assert_eq!(
            groups.len(),
            2,
            "Should separate when line gap (5) exceeds threshold (4)"
        );
    }
}