cargo-deltabuild 0.1.0

Detects which crates in a Cargo workspace are affected by changes in a Git feature branch.
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
//! # cargo-deltabuild
//!
//! `cargo-deltabuild` detects which crates in a Cargo workspace are impacted by changes in a Git feature branch.
//! Build, test, and benchmark only the crates you need—saving time and resources in your CI/CD pipeline.
//!
//! For detailed configuration examples and usage information, see the [README.md](https://github.com/tekian/cargo-deltabuild).
//!
//! ## Features
//!
//! - **Robust Detection**: Uses code analysis, pattern matching and runtime heuristics to identify dependencies.
//! - **Impact Categorization**: Separates crates into _Modified_, _Affected_, and _Required_ for precise targeting.
//! - **Configurability**: Highly customizable via config, with per-crate overrides for parsing and detection.
//! - **Dual-branch Git Detection**: Compares two branches or commits to find both modified and deleted files.
//! - **File Control Mechanisms**: Exclude files from analysis or trigger a full rebuild when critical files change.
//!
//! ## Installation
//!
//! ```bash
//! cargo install cargo-deltabuild
//! ```
//!
//! ## Usage
//!
//! 1. **Check out the baseline branch and analyze:**
//!    ```bash
//!    git checkout main
//!    cargo deltabuild analyze > main.json
//!    ```
//!
//! 2. **Check out your feature branch and analyze:**
//!    ```bash
//!    git checkout feature-branch
//!    cargo deltabuild analyze > feature.json
//!    ```
//!
//! 3. **Compare analyses to find impacted crates:**
//!    ```bash
//!    cargo deltabuild run --baseline main.json --current feature.json
//!    ```
//!
//! ## Configuration
//!
//! You can customize `cargo-deltabuild` by providing a `-c config.toml` argument to the command.
//!
//! ```bash
//! cargo deltabuild analyze -c config.toml # ...
//! cargo deltabuild run -c config.toml # ...
//! ```
//!
//! Configuration options can be set globally and overridden per crate. For example:
//!
//! ```toml
//! [parser]
//! foo = true
//! foo_patterns = ["*.foo", "*.bar"]
//!
//! [parser.my-crate]
//! foo_patterns = ["*.baz"] # Override for a specific crate
//! ```
//!
//! Default settings are provided in [config.toml.example](https://github.com/tekian/cargo-deltabuild/blob/main/config.toml.example).
//!
//! ## Output Format
//!
//! The tool outputs JSON with three categories of impacted crates:
//!
//! - **Modified**: Crates directly modified by Git changes.
//! - **Affected**: Modified crates plus all their dependents, direct and indirect.
//! - **Required**: Affected crates plus all their dependencies, direct and indirect.

use argh::FromArgs;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::PathBuf;
use std::time::Instant;

use crate::config::MainConfig;
use crate::crates::Crates;
use crate::error::Result;
use crate::files::FileNode;
use crate::git::GitDiff;

#[doc(hidden)]
mod cargo;
#[doc(hidden)]
mod config;
#[doc(hidden)]
mod crates;
#[doc(hidden)]
mod error;
#[doc(hidden)]
mod files;
#[doc(hidden)]
mod git;
#[doc(hidden)]
mod utils;

/// Main command-line interface for cargo-deltabuild.
#[derive(FromArgs)]
#[argh(description = "Tool to identify impacted crates from git changes.")]
struct Args {
    /// path to the config file
    #[argh(option, short = 'c')]
    config: Option<PathBuf>,

    #[argh(subcommand)]
    command: Commands,
}

#[derive(FromArgs)]
#[argh(subcommand)]
enum Commands {
    Run(RunCommand),
    Analyze(AnalyzeCommand),
}

#[derive(FromArgs)]
#[argh(subcommand, name = "run", description = "run deltabuild and show impacted crates")]
struct RunCommand {
    /// baseline workspace analysis JSON file (e.g., from main branch)
    #[argh(option)]
    baseline: PathBuf,
    /// current workspace analysis JSON file (e.g., from feature branch)
    #[argh(option)]
    current: PathBuf,
}

#[derive(FromArgs)]
#[argh(subcommand, name = "analyze", description = "analyze current workspace and produce JSON file")]
struct AnalyzeCommand {}

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Impact {
    #[serde(rename = "Modified")]
    pub modified: HashSet<String>,
    #[serde(rename = "Affected")]
    pub affected: HashSet<String>,
    #[serde(rename = "Required")]
    pub required: HashSet<String>,
}

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize)]
struct WorkspaceTree {
    pub files: FileNode,
    pub crates: Crates,
}

fn main() {
    // Handle both "cargo deltabuild" and direct invocations
    let args: Vec<String> = std::env::args().collect();
    let skip = if args.len() > 1 && args[1] == "deltabuild" { 2 } else { 1 };

    let cli: Args = Args::from_args(
        &[args[0].as_str()],
        &args[skip..].iter().map(|s| s.as_str()).collect::<Vec<_>>()
    ).unwrap_or_else(|early_exit| {
        eprintln!("{}", early_exit.output);
        std::process::exit(if early_exit.status.is_ok() { 0 } else { 1 });
    });

    let config = match config::load_config(cli.config.clone()) {
        Ok(i) => i,
        Err(e) => {
            eprintln!("Error loading config: {}", e);
            std::process::exit(1);
        }
    };

    let eprintln_common_props = || {
        if let Some(config_path) = &cli.config {
            eprintln!();
            eprintln!("Using config file  : {}", config_path.display());
        }
    };

    match &cli.command {
        Commands::Run(run_cmd) =>
            run(config, &run_cmd.baseline, &run_cmd.current, eprintln_common_props),

        Commands::Analyze(_) =>
            analyze(config, eprintln_common_props),
    }
}

#[doc(hidden)]
fn analyze(config: MainConfig, eprintln_common_props: impl FnOnce())
{
    let start = Instant::now();
    eprintln!("Analyzing workspace..");
    eprintln_common_props();

    let metadata = match cargo::metadata() {
        Ok(metadata) => metadata,
        Err(e) => {
            eprintln!("Error getting cargo metadata: {}", e);
            std::process::exit(1);
        }
    };

    let workspace_root = &metadata.workspace_root;

    let git_root = match git::get_top_level() {
        Ok(root) => root,
        Err(e) => {
            eprintln!("Error getting git root: {}", e);
            std::process::exit(1);
        }
    };

    eprintln!();
    eprintln!("Detected Git root        : {}", git_root.display());
    eprintln!("Detected Cargo workspace : {}", workspace_root.display());
    eprintln!();

    let crates = cargo::get_workspace_crates(&metadata);
    let mut files = files::build_tree(&metadata, &crates, &config);
    let crates = crates::parse(&metadata);

    files.to_relative_paths(&git_root);

    eprintln!("Found {} crate(s) in the workspace.", crates.len());
    eprintln!("Found {} file(s) in the workspace.", files.len());
    eprintln!();

    let workspace_tree = WorkspaceTree { files, crates };

    match serde_json::to_string_pretty(&workspace_tree) {
        Ok(json_output) => println!("{}", json_output),
        Err(e) => {
            eprintln!("Error serializing workspace tree to JSON: {}", e);
            std::process::exit(1);
        }
    }

    eprintln!();
    eprintln!("CAUTION: The following files are *NOT* considered compilation inputs:");

    let excludes: Vec<PathBuf> = workspace_tree.files.distinct().into_iter().collect();

    let unrelated = utils::find_unrelated(
        &git_root, &excludes, &config.file_exclude_patterns);

    for file in unrelated {
        eprintln!("{}", file.display());
    }

    let duration = start.elapsed();
    eprintln!("\nAnalysis finished in {:.2?}", duration);
}

#[doc(hidden)]
fn run(config: MainConfig, baseline: &PathBuf, current: &PathBuf, eprintln_common_props: impl FnOnce()) {
    eprintln!("Running deltabuild..\n");
    eprintln_common_props();

    // Get git root to ensure we're working with consistent path bases
    let git_root = match git::get_top_level() {
        Ok(root) => root,
        Err(e) => {
            eprintln!("Error getting git root: {}", e);
            std::process::exit(1);
        }
    };

    eprintln!("Looking up git changes..");

    let diff = match git::diff(&git_root, config.git.clone()) {
        Ok(i) => i,
        Err(e) => {
            eprintln!("Error creating diff: {}", e);
            std::process::exit(1);
        }
    };

    if diff.changed.is_empty() && diff.deleted.is_empty() {
        eprintln!("No file has been changed or deleted, quitting.");
        std::process::exit(0);
    }

    for changed in &diff.changed {
        eprintln!("Changed file: {}", &changed.display());
    }

    for deleted in &diff.deleted {
        eprintln!("Deleted file: {}", &deleted.display());
    }

    eprintln!();
    eprintln!("Using baseline analysis : {}", baseline.display());
    eprintln!("Using current analysis  : {}", current.display());
    eprintln!();

    let baseline_tree: WorkspaceTree = match utils::deser_json(baseline) {
        Ok(tree) => tree,
        Err(e) => {
            eprintln!("Error loading current workspace tree: {}", e);
            std::process::exit(1);
        }
    };

    let current_tree: WorkspaceTree = match utils::deser_json(current) {
        Ok(tree) => tree,
        Err(e) => {
            eprintln!("Error loading branch workspace tree: {}", e);
            std::process::exit(1);
        }
    };

    let result = match get_impacted_crates(&baseline_tree, &current_tree, &diff, &config) {
        Ok(i) => i,
        Err(e) => {
            eprintln!("Error calculating impacted crates: {}", e);
            std::process::exit(1);
        }
    };

    match serde_json::to_string_pretty(&result) {
        Ok(json_output) => println!("{}", json_output),
        Err(e) => {
            eprintln!("Error serializing result to JSON: {}", e);
            std::process::exit(1);
        }
    }

    let total_crates = current_tree.crates.len();

    let required_crates_len = result.required.len();
    let affected_crates_len = result.affected.len();
    let modified_crates_len = result.modified.len();

    eprintln!(
        "{:<11} {:>3} {}", "Modified",
        modified_crates_len, "(Crates directly modified by Git changes.)");

    eprintln!(
        "{:<11} {:>3} {}", "Affected",
        affected_crates_len, "(Modified crates plus all their dependents, direct and indirect.)");

    eprintln!(
        "{:<11} {:>3} {}", "Required",
        required_crates_len, "(Affected crates plus all their dependencies, direct and indirect.)");

    eprintln!(
        "{:<11} {:>3} {}", "Total",
        total_crates, "(Total crates in this workspace.)");

    eprintln!();
}

#[doc(hidden)]
fn get_impacted_crates(
    baseline_tree: &WorkspaceTree,
    current_tree: &WorkspaceTree,
    git_diff: &GitDiff,
    config: &MainConfig,
) -> Result<Impact> {
    let mut modified = HashSet::new();

    if !config.trip_wire_patterns.is_empty() {
        use glob::Pattern;

        let trip_wire_patterns: Vec<Pattern> = config.trip_wire_patterns
            .iter()
            .filter_map(|pattern| Pattern::new(pattern).ok())
            .collect();

        let mut tripped_files = Vec::new();

        for deleted_file in &git_diff.deleted {
            let file_str = deleted_file.to_string_lossy();
            if trip_wire_patterns.iter().any(|pattern| pattern.matches(&file_str)) {
                tripped_files.push(file_str.to_string());
            }
        }

        for changed_file in &git_diff.changed {
            let file_str = changed_file.to_string_lossy();
            if trip_wire_patterns.iter().any(|pattern| pattern.matches(&file_str)) {
                tripped_files.push(file_str.to_string());
            }
        }

        if !tripped_files.is_empty() {
            eprintln!("WARNING: Trip wire activated due to changes in the following file(s):");
            for file in &tripped_files {
                eprintln!("- {}", file);
            }
            eprintln!();

            let all_crates: HashSet<String> = current_tree.crates
                .get_all_crate_names()
                .into_iter()
                .collect();

            return Ok(Impact {
                modified: all_crates.clone(),
                affected: all_crates.clone(),
                required: all_crates,
            });
        } else {
            eprintln!("Trip wire is enabled, but no matching files were found, good.");
            eprintln!();
        }
    }

    for deleted_file in &git_diff.deleted {
        let crates_for_file = baseline_tree
            .files
            .find_crates_containing_file(deleted_file);

        for crate_name in crates_for_file {
            modified.insert(crate_name);
        }
    }

    for changed_file in &git_diff.changed {
        let crates_for_file = current_tree.files.find_crates_containing_file(changed_file);

        for crate_name in crates_for_file {
            modified.insert(crate_name);
        }
    }

    let main_files = baseline_tree.files.distinct();
    let branch_files = current_tree.files.distinct();

    for new_file in branch_files.difference(&main_files) {
        let crates_for_file = current_tree.files.find_crates_containing_file(new_file);

        for crate_name in crates_for_file {
            modified.insert(crate_name);
        }
    }

    // Affected = Modified + all their dependents
    let mut affected = modified.clone();
    for crate_name in &modified {
        match current_tree.crates.get_dependents_transitive(crate_name) {
            Some(transitive_dependents) => {
                for dependent in transitive_dependents {
                    affected.insert(dependent);
                }
            }
            None => {}
        }
    }

    // Required = Affected + all their dependencies
    let mut required = affected.clone();
    for crate_name in &affected {
        match current_tree.crates.get_dependencies_transitive(crate_name) {
            Some(transitive_deps) => {
                for dependency in transitive_deps {
                    required.insert(dependency);
                }
            }
            None => {}
        }
    }

    Ok(Impact { modified, affected, required })
}