fallow-engine 3.48.0

Typed analysis engine facade for fallow consumers
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
use std::time::Instant;

use fallow_config::ResolvedConfig;
use fallow_output::{FileHealthScore, HotspotEntry, HotspotSummary, RefactoringTarget};

use crate::baseline::{HealthBaselineData, filter_new_health_targets};
use crate::duplicates::DuplicationReport;

use super::HealthExecutionOptions;
use super::change_coupling_section::{ChangeCouplingOutcome, compute_change_coupling};
use super::filters::{
    collect_candidate_paths, filter_files_to_paths, filter_hotspots_by_diff,
    filter_refactoring_targets_by_diff,
};
use super::hotspots::{self, HotspotComputationInput, compute_hotspots};
use super::scoring;
use super::targets::{self, TargetAuxData, compute_refactoring_targets};

pub struct HealthDerivedSectionInput<'a> {
    pub(crate) config: &'a ResolvedConfig,
    pub(crate) files: &'a [fallow_types::discover::DiscoveredFile],
    pub(crate) ignore_set: &'a globset::GlobSet,
    pub(crate) changed_files: Option<&'a rustc_hash::FxHashSet<std::path::PathBuf>>,
    pub(crate) ws_roots: Option<&'a crate::workspace_scope::WorkspaceScope>,
    pub(crate) file_scores: &'a [FileHealthScore],
    pub(crate) churn_fetch: Option<hotspots::ChurnFetchResult>,
    pub(crate) diff_index: Option<&'a fallow_output::DiffIndex>,
    pub(crate) score_output: Option<&'a scoring::FileScoreOutput>,
    pub(crate) loaded_baseline: Option<&'a HealthBaselineData>,
    pub(crate) pre_computed_duplication: Option<DuplicationReport>,
    /// Flag-resolved global CRAP ceiling (`HealthScope::max_crap`); target
    /// factors fall back to it on rows without their own effective ceiling.
    pub(crate) max_crap: f64,
}

pub struct HealthDerivedSections {
    pub(crate) candidate_paths: rustc_hash::FxHashSet<std::path::PathBuf>,
    pub(crate) dupes_report: Option<crate::duplicates::DuplicationReport>,
    pub(crate) duplication_ms: f64,
    pub(crate) hotspots: Vec<HotspotEntry>,
    pub(crate) hotspot_summary: Option<HotspotSummary>,
    pub(crate) hotspots_ms: f64,
    pub(crate) targets: Vec<RefactoringTarget>,
    pub(crate) target_thresholds: Option<fallow_output::TargetThresholds>,
    pub(crate) targets_ms: f64,
    /// The `change_coupling` section after `--top`. `None` when the
    /// hotspots section did not run or the run read no git history.
    pub(crate) change_coupling: Option<fallow_output::ChangeCouplingReport>,
    /// Hotspots before `--top` for scoring, plus complete grouped targets.
    /// Each group applies `--top` to its own displayed lists.
    pub(crate) group_lists: GroupUntruncatedLists,
}

/// Lists before `--top`: hotspots serve project scoring and groups, while
/// targets serve groups. `None` means the project list is complete.
#[derive(Default)]
pub struct GroupUntruncatedLists {
    pub(crate) hotspots: Option<Vec<HotspotEntry>>,
    pub(crate) targets: Option<Vec<RefactoringTarget>>,
}

pub fn prepare_health_derived_sections(
    opts: &HealthExecutionOptions<'_>,
    mut input: HealthDerivedSectionInput<'_>,
) -> HealthDerivedSections {
    let pre_computed_duplication = input.pre_computed_duplication.take();
    let (candidate_paths, dupes_report, duplication_ms) =
        prepare_health_section_dupes(opts, &input, pre_computed_duplication);
    let coupling = prepare_change_coupling(opts, &input);
    let (hotspots, group_hotspots, hotspot_summary, hotspots_ms) = prepare_health_section_hotspots(
        opts,
        HealthHotspotSectionInput {
            config: input.config,
            file_scores: input.file_scores,
            ignore_set: input.ignore_set,
            ws_roots: input.ws_roots,
            churn_fetch: input.churn_fetch,
            diff_index: input.diff_index,
            dispersion: coupling
                .as_ref()
                .map(|outcome| &outcome.analysis.dispersion),
        },
    );
    let target_input = HealthTargetSectionInput {
        score_output: input.score_output,
        file_scores: input.file_scores,
        hotspots: &hotspots,
        loaded_baseline: input.loaded_baseline,
        config: input.config,
        diff_index: input.diff_index,
        dupes_report: dupes_report.as_ref(),
        max_crap: input.max_crap,
        co_change: coupling.as_ref().map(|outcome| &outcome.analysis),
    };
    let (targets, group_targets, target_thresholds, targets_ms) =
        prepare_health_section_targets(opts, &target_input, group_hotspots.as_deref());

    HealthDerivedSections {
        candidate_paths,
        dupes_report,
        duplication_ms,
        hotspots,
        hotspot_summary,
        hotspots_ms,
        targets,
        target_thresholds,
        targets_ms,
        change_coupling: coupling.and_then(|outcome| report_change_coupling(opts, outcome)),
        group_lists: GroupUntruncatedLists {
            hotspots: group_hotspots,
            targets: group_targets,
        },
    }
}

fn prepare_health_section_dupes(
    opts: &HealthExecutionOptions<'_>,
    input: &HealthDerivedSectionInput<'_>,
    pre_computed_duplication: Option<DuplicationReport>,
) -> (
    rustc_hash::FxHashSet<std::path::PathBuf>,
    Option<crate::duplicates::DuplicationReport>,
    f64,
) {
    prepare_health_duplication_data(HealthDuplicationDataInput {
        opts,
        config: input.config,
        files: input.files,
        changed_files: input.changed_files,
        ws_roots: input.ws_roots,
        ignore_set: input.ignore_set,
        pre_computed_duplication,
    })
}

/// Run change coupling over the churn commits when the run read git history
/// and built the static edge index.
fn prepare_change_coupling(
    opts: &HealthExecutionOptions<'_>,
    input: &HealthDerivedSectionInput<'_>,
) -> Option<ChangeCouplingOutcome> {
    let churn_fetch = input.churn_fetch.as_ref()?;
    let links = input.score_output?.static_links.as_ref()?;
    Some(compute_change_coupling(
        opts,
        input.config,
        churn_fetch,
        links,
        &crate::change_coupling::PairScope {
            root: &input.config.root,
            ignore_set: input.ignore_set,
            ws_roots: input.ws_roots,
            diff_index: input.diff_index,
        },
    ))
}

/// The section as the report carries it: only with the hotspots section,
/// never in score-only output, and cut to `--top`.
fn report_change_coupling(
    opts: &HealthExecutionOptions<'_>,
    outcome: ChangeCouplingOutcome,
) -> Option<fallow_output::ChangeCouplingReport> {
    if !opts.hotspots || opts.score_only_output {
        return None;
    }
    let mut report = outcome.report;
    if let Some(top) = opts.top {
        report.pairs.truncate(top);
    }
    Some(report)
}

struct HealthHotspotSectionInput<'a> {
    config: &'a ResolvedConfig,
    file_scores: &'a [FileHealthScore],
    ignore_set: &'a globset::GlobSet,
    ws_roots: Option<&'a crate::workspace_scope::WorkspaceScope>,
    churn_fetch: Option<hotspots::ChurnFetchResult>,
    diff_index: Option<&'a fallow_output::DiffIndex>,
    dispersion: Option<
        &'a rustc_hash::FxHashMap<std::path::PathBuf, crate::change_coupling::FileDispersion>,
    >,
}

fn prepare_health_section_hotspots(
    opts: &HealthExecutionOptions<'_>,
    input: HealthHotspotSectionInput<'_>,
) -> (
    Vec<HotspotEntry>,
    Option<Vec<HotspotEntry>>,
    Option<HotspotSummary>,
    f64,
) {
    compute_filtered_hotspots(FilteredHotspotInput {
        opts,
        config: input.config,
        file_scores_slice: input.file_scores,
        ignore_set: input.ignore_set,
        ws_roots: input.ws_roots,
        churn_fetch: input.churn_fetch,
        diff_index: input.diff_index,
        dispersion: input.dispersion,
    })
}

struct HealthTargetSectionInput<'a> {
    score_output: Option<&'a scoring::FileScoreOutput>,
    file_scores: &'a [FileHealthScore],
    hotspots: &'a [HotspotEntry],
    loaded_baseline: Option<&'a HealthBaselineData>,
    config: &'a ResolvedConfig,
    diff_index: Option<&'a fallow_output::DiffIndex>,
    dupes_report: Option<&'a crate::duplicates::DuplicationReport>,
    max_crap: f64,
    co_change: Option<&'a crate::change_coupling::ChangeHistoryAnalysis>,
}

/// Compute the project targets, plus the complete group list of a grouped run
/// whose `--top` removes targets or hotspots.
///
/// The project targets use the truncated hotspot list, as an ungrouped run
/// does. The group list uses the complete hotspot list, so a group target
/// does not depend on a global `--top` cut.
fn prepare_health_section_targets(
    opts: &HealthExecutionOptions<'_>,
    input: &HealthTargetSectionInput<'_>,
    group_hotspots: Option<&[HotspotEntry]>,
) -> (
    Vec<RefactoringTarget>,
    Option<Vec<RefactoringTarget>>,
    Option<fallow_output::TargetThresholds>,
    f64,
) {
    let filtered = |hotspots: &[HotspotEntry], top: Option<usize>| {
        compute_filtered_targets(
            FilteredTargetInput {
                opts,
                score_output: input.score_output,
                file_scores_slice: input.file_scores,
                hotspots,
                loaded_baseline: input.loaded_baseline,
                config: input.config,
                diff_index: input.diff_index,
                dupes_report: input.dupes_report,
                max_crap: input.max_crap,
                co_change: input.co_change,
            },
            top,
        )
    };
    let (targets, target_thresholds, targets_ms) = filtered(input.hotspots, opts.top);
    let group_targets = (opts.group_by.is_some() && opts.top.is_some() && opts.targets)
        .then(|| filtered(group_hotspots.unwrap_or(input.hotspots), None).0);
    (targets, group_targets, target_thresholds, targets_ms)
}

struct FilteredHotspotInput<'a> {
    opts: &'a HealthExecutionOptions<'a>,
    config: &'a ResolvedConfig,
    file_scores_slice: &'a [FileHealthScore],
    ignore_set: &'a globset::GlobSet,
    ws_roots: Option<&'a crate::workspace_scope::WorkspaceScope>,
    churn_fetch: Option<hotspots::ChurnFetchResult>,
    diff_index: Option<&'a fallow_output::DiffIndex>,
    dispersion: Option<
        &'a rustc_hash::FxHashMap<std::path::PathBuf, crate::change_coupling::FileDispersion>,
    >,
}

fn compute_filtered_hotspots(
    input: FilteredHotspotInput<'_>,
) -> (
    Vec<HotspotEntry>,
    Option<Vec<HotspotEntry>>,
    Option<HotspotSummary>,
    f64,
) {
    let t = Instant::now();
    let (mut hotspots, hotspot_summary) = if let Some(churn_data) = input.churn_fetch {
        compute_hotspots(HotspotComputationInput {
            opts: input.opts,
            config: input.config,
            file_scores: input.file_scores_slice,
            ignore_set: input.ignore_set,
            ws_roots: input.ws_roots,
            churn_fetch: churn_data,
            dispersion: input.dispersion,
        })
    } else {
        (Vec::new(), None)
    };
    let mut group_hotspots = input
        .opts
        .top
        .filter(|&top| top < hotspots.len())
        .map(|top| {
            let displayed = hotspots[..top].to_vec();
            std::mem::replace(&mut hotspots, displayed)
        });
    if let Some(diff_index) = input.diff_index {
        filter_hotspots_by_diff(&mut hotspots, diff_index, &input.config.root);
        if let Some(group_hotspots) = group_hotspots.as_mut() {
            filter_hotspots_by_diff(group_hotspots, diff_index, &input.config.root);
        }
    }
    (
        hotspots,
        group_hotspots,
        hotspot_summary,
        t.elapsed().as_secs_f64() * 1000.0,
    )
}

#[derive(Clone, Copy)]
struct FilteredTargetInput<'a> {
    opts: &'a HealthExecutionOptions<'a>,
    score_output: Option<&'a scoring::FileScoreOutput>,
    file_scores_slice: &'a [FileHealthScore],
    hotspots: &'a [HotspotEntry],
    loaded_baseline: Option<&'a HealthBaselineData>,
    config: &'a ResolvedConfig,
    diff_index: Option<&'a fallow_output::DiffIndex>,
    dupes_report: Option<&'a crate::duplicates::DuplicationReport>,
    max_crap: f64,
    co_change: Option<&'a crate::change_coupling::ChangeHistoryAnalysis>,
}

fn compute_filtered_targets(
    input: FilteredTargetInput<'_>,
    top: Option<usize>,
) -> (
    Vec<RefactoringTarget>,
    Option<fallow_output::TargetThresholds>,
    f64,
) {
    let t = Instant::now();
    let (mut targets, target_thresholds) = compute_targets(&input);
    if let Some(top) = top {
        targets.truncate(top);
    }
    if let Some(diff_index) = input.diff_index {
        filter_refactoring_targets_by_diff(&mut targets, diff_index, &input.config.root);
    }
    (
        targets,
        target_thresholds,
        t.elapsed().as_secs_f64() * 1000.0,
    )
}

struct HealthDuplicationDataInput<'a> {
    opts: &'a HealthExecutionOptions<'a>,
    config: &'a ResolvedConfig,
    files: &'a [fallow_types::discover::DiscoveredFile],
    changed_files: Option<&'a rustc_hash::FxHashSet<std::path::PathBuf>>,
    ws_roots: Option<&'a crate::workspace_scope::WorkspaceScope>,
    ignore_set: &'a globset::GlobSet,
    pre_computed_duplication: Option<DuplicationReport>,
}

fn prepare_health_duplication_data(
    input: HealthDuplicationDataInput<'_>,
) -> (
    rustc_hash::FxHashSet<std::path::PathBuf>,
    Option<crate::duplicates::DuplicationReport>,
    f64,
) {
    let candidate_paths = collect_candidate_paths(
        input.files,
        input.config,
        input.changed_files,
        input.ws_roots,
        input.ignore_set,
    );
    let (dupes_report, duplication_ms) =
        compute_health_duplication_report(HealthDuplicationReportInput {
            opts: input.opts,
            config: input.config,
            files: input.files,
            candidate_paths: &candidate_paths,
            pre_computed_duplication: input.pre_computed_duplication,
        });
    (candidate_paths, dupes_report, duplication_ms)
}

struct HealthDuplicationReportInput<'a> {
    opts: &'a HealthExecutionOptions<'a>,
    config: &'a ResolvedConfig,
    files: &'a [fallow_types::discover::DiscoveredFile],
    candidate_paths: &'a rustc_hash::FxHashSet<std::path::PathBuf>,
    pre_computed_duplication: Option<DuplicationReport>,
}

fn compute_health_duplication_report(
    input: HealthDuplicationReportInput<'_>,
) -> (Option<crate::duplicates::DuplicationReport>, f64) {
    let t = Instant::now();
    // `duplicates.enabled: false` leaves the score without a duplication
    // measure and the targets without clone evidence. A run that names the
    // duplication analysis does not change this, so the score of a project
    // does not depend on the analyses that one run selects.
    let measures_duplication = input.config.duplicates.runs_by_default();
    let dupes_report = if measures_duplication && (input.opts.score || input.opts.targets) {
        // A report over every file is the exact result: it covers the same
        // files with the same config as the detection below. A report over
        // more files than the health scope cannot be cut down to the scope,
        // because the detector drops groups below `minOccurrences` and
        // counts its totals over the files that it reads, so the scoped
        // detection runs instead.
        if let Some(report) = input.pre_computed_duplication
            && input.candidate_paths.len() == input.files.len()
        {
            return (Some(report), t.elapsed().as_secs_f64() * 1000.0);
        }
        let scoped_files = filter_files_to_paths(input.files, input.candidate_paths);
        let cache_dir = (!input.opts.no_cache).then_some(input.config.cache_dir.as_path());
        Some(
            crate::duplicates::find_duplicates_with_defaults(
                &input.config.root,
                &scoped_files,
                &input.config.duplicates,
                cache_dir,
            )
            .report,
        )
    } else {
        None
    };
    (dupes_report, t.elapsed().as_secs_f64() * 1000.0)
}

/// Compute refactoring targets when requested, applying the baseline and effort
/// filters. The caller applies `--top`.
fn compute_targets(
    input: &FilteredTargetInput<'_>,
) -> (
    Vec<RefactoringTarget>,
    Option<fallow_output::TargetThresholds>,
) {
    if !input.opts.targets {
        return (Vec::new(), None);
    }
    let Some(output) = input.score_output else {
        return (Vec::new(), None);
    };
    let clone_siblings = input
        .dupes_report
        .map_or_else(rustc_hash::FxHashMap::default, |report| {
            targets::build_clone_sibling_evidence(report)
        });
    let target_aux = TargetAuxData::from_output(output, &clone_siblings, input.co_change);
    let (mut tgts, thresholds) = compute_refactoring_targets(
        input.file_scores_slice,
        &target_aux,
        input.hotspots,
        input.max_crap,
    );
    if let Some(baseline) = input.loaded_baseline {
        tgts = filter_new_health_targets(tgts, baseline, &input.config.root);
    }
    if let Some(ref effort) = input.opts.effort {
        tgts.retain(|t| t.effort == *effort);
    }
    (tgts, Some(thresholds))
}