commit-wizard 0.0.4

A lightweight CLI assistant for Conventional Commits, semantic versioning, and changelog automation.
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
//! The guided `cw init`: a short set of questions about how the project works,
//! then a few more for people who want to go further.

use std::path::PathBuf;

use scriba::{MultiSelectOption, MultiSelectRequest, SelectOption, SelectRequest};

use crate::{
    core::{Context, CoreResult},
    engine::{
        PromptTrait,
        capabilities::config::wizard::{MANDATORY_TYPES, WizardAnswers, profile_types, split_list},
        config::{BaseConfig, ConfigFormat},
        constants::{CONFIG_STEM, app_config_dir, paths::resolve_new_project_config_path},
        error::ErrorCode,
        models::policy::enforcement::{CommitEnforcementScope, ScopeMode},
    },
};

/// Which config file is being created.
#[derive(Debug, Clone, Copy)]
pub enum InitScope {
    /// The project's `.cwizard.*` (or `cwizard.*` when not hidden).
    Project { hidden: bool },
    /// The per-user `config.*`.
    Global,
}

/// What the wizard decided.
#[derive(Debug, Clone)]
pub struct InitPlan {
    pub profile: String,
    pub file_format: ConfigFormat,
    pub answers: WizardAnswers,
    /// The file already existed and the person agreed to replace it.
    pub overwrite: bool,
}

/// The built-in profile a name refers to.
pub fn profile_config(profile: &str) -> BaseConfig {
    match profile {
        "minimal" => BaseConfig::minimal(),
        "full" => BaseConfig::full(),
        _ => BaseConfig::standard(),
    }
}

fn target_path(ctx: &Context, scope: InitScope, format: ConfigFormat) -> CoreResult<PathBuf> {
    match scope {
        InitScope::Global => {
            Ok(app_config_dir()?.join(format!("{CONFIG_STEM}.{}", format.extension())))
        }
        InitScope::Project { hidden } => {
            let explicit = ctx.explicit_config_path().map(|p| p.as_path());
            let mut path = resolve_new_project_config_path(
                ctx.cwd(),
                Some(ctx.repo_root().as_path()),
                ctx.in_git_repo(),
                explicit,
                hidden,
            );
            if explicit.is_none() {
                path.set_extension(format.extension());
            }
            Ok(path)
        }
    }
}

fn invalid(field: &'static str) -> impl Fn(crate::engine::Error) -> crate::engine::Error {
    move |err| err.with_context("field", field)
}

/// Walk through the questions. `profile` and `file_format` are `Some` when a
/// flag already decided them. Returns `None` if the person backs out.
pub fn guide(
    ctx: &Context,
    scope: InitScope,
    profile: Option<String>,
    file_format: Option<ConfigFormat>,
) -> CoreResult<Option<InitPlan>> {
    let ui = ctx.ui();

    // --- the basics ---------------------------------------------------------
    let profile = match profile {
        Some(profile) => profile,
        None => ui
            .select(&SelectRequest::new(
                "Which profile fits your project?".to_string(),
                vec![
                    SelectOption::new("standard", "Standard").description(
                        "Common commit types, optional scopes, protected main branch (recommended)"
                            .to_string(),
                    ),
                    SelectOption::new("minimal", "Minimal").description(
                        "Almost nothing written down; built-in defaults apply".to_string(),
                    ),
                    SelectOption::new("full", "Full")
                        .description("Every setting spelled out, to edit by hand".to_string()),
                ],
            ))
            .map_err(invalid("profile"))?,
    };

    let file_format = match file_format {
        Some(format) => format,
        None => {
            let choice = ui
                .select(&SelectRequest::new(
                    "Which file format do you prefer?".to_string(),
                    vec![
                        SelectOption::new("toml", "TOML").description(".cwizard.toml".to_string()),
                        SelectOption::new("yaml", "YAML").description(".cwizard.yaml".to_string()),
                        SelectOption::new("json", "JSON").description(".cwizard.json".to_string()),
                    ],
                ))
                .map_err(invalid("file_format"))?;
            ConfigFormat::from_extension(&choice).unwrap_or_default()
        }
    };

    let path = target_path(ctx, scope, file_format)?;
    let mut overwrite = false;
    if path.exists() && !ctx.force() {
        if !ui.confirm(
            &format!("{} already exists. Replace it?", path.display()),
            false,
        )? {
            ui.logger().warn("Nothing was written");
            return Ok(None);
        }
        overwrite = true;
    }

    // --- how the project works ----------------------------------------------
    let preset = profile_config(&profile);
    let types: Vec<(String, bool)> = profile_types(&preset)
        .into_iter()
        .filter(|name| !MANDATORY_TYPES.contains(&name.as_str()))
        .map(|name| (name, true))
        .collect();
    let answers = ask_settings(ctx, &preset, types, false)?;

    // --- last word -------------------------------------------------------------
    if !ui.confirm(&format!("Create {}?", path.display()), true)? {
        ui.logger().warn("Nothing was written");
        return Ok(None);
    }

    Ok(Some(InitPlan {
        profile,
        file_format,
        answers,
        overwrite,
    }))
}

/// Put the option matching `current` first so it is what Enter picks, and
/// mark it when editing.
fn current_first(
    options: Vec<(&'static str, &'static str, &'static str)>,
    current: &str,
    mark_current: bool,
) -> Vec<SelectOption> {
    let mut options = options;
    if let Some(position) = options.iter().position(|(id, _, _)| *id == current) {
        let chosen = options.remove(position);
        options.insert(0, chosen);
    }
    options
        .into_iter()
        .map(|(id, label, description)| {
            let label = if mark_current && id == current {
                format!("{label} (current)")
            } else {
                label.to_string()
            };
            SelectOption::new(id, label).description(description.to_string())
        })
        .collect()
}

/// The questions about how the project works, with `current` supplying the
/// default of each. `types` are the optional commit types and whether each is
/// selected. Used by both `init` (the profile is `current`) and `config edit`
/// (the existing file is).
fn ask_settings(
    ctx: &Context,
    current: &BaseConfig,
    types: Vec<(String, bool)>,
    editing: bool,
) -> CoreResult<WizardAnswers> {
    let ui = ctx.ui();
    let mut answers = WizardAnswers::default();

    if !types.is_empty() {
        let chosen = ui
            .multiselect(&MultiSelectRequest::new(
                format!(
                    "Which commit types will you use? ({} are always included)",
                    MANDATORY_TYPES.join(" and ")
                ),
                types
                    .iter()
                    .map(|(name, selected)| {
                        MultiSelectOption::new(name.clone(), name.clone()).selected(*selected)
                    })
                    .collect(),
            ))
            .map_err(invalid("commit_types"))?;
        answers.keep_types = Some(chosen);
    }

    let current_scope = match current.commit_scopes_mode() {
        ScopeMode::Required => "required",
        ScopeMode::Disabled => "disabled",
        ScopeMode::Optional => "optional",
    };
    let scope_choice = ui
        .select(&SelectRequest::new(
            "How should commit scopes work?".to_string(),
            current_first(
                vec![
                    ("optional", "Optional", "feat(api): ... or feat: ..."),
                    ("required", "Required", "Every commit must name a scope"),
                    ("disabled", "Not used", "Scopes are not allowed"),
                ],
                current_scope,
                editing,
            ),
        ))
        .map_err(invalid("scopes"))?;
    answers.scope_mode = Some(match scope_choice.as_str() {
        "required" => ScopeMode::Required,
        "disabled" => ScopeMode::Disabled,
        _ => ScopeMode::Optional,
    });

    let default_branches = {
        let found = current.branch_protected_patterns();
        if found.is_empty() {
            vec!["main".to_string(), "master".to_string()]
        } else {
            found
        }
    };
    let branches = ui.text(
        "Which branches are protected?",
        Some(&default_branches.join(", ")),
        Some("Comma-separated; patterns such as release/* work"),
    )?;
    let branches = split_list(&branches);
    if !branches.is_empty() {
        answers.protected_branches = Some(branches);
    }

    let current_enforce = match current.check_commits_enforce_on() {
        CommitEnforcementScope::AllBranches => "all_branches",
        CommitEnforcementScope::None => "none",
        CommitEnforcementScope::ProtectedBranches => "protected_branches",
    };
    let enforce = ui
        .select(&SelectRequest::new(
            "Where should commit messages be checked?".to_string(),
            current_first(
                vec![
                    (
                        "protected_branches",
                        "Protected branches only",
                        "Recommended: feature branches stay relaxed",
                    ),
                    ("all_branches", "Every branch", "Strict everywhere"),
                    ("none", "Nowhere", "Never block a push over message style"),
                ],
                current_enforce,
                editing,
            ),
        ))
        .map_err(invalid("enforce_on"))?;
    answers.enforce_on = Some(match enforce.as_str() {
        "all_branches" => CommitEnforcementScope::AllBranches,
        "none" => CommitEnforcementScope::None,
        _ => CommitEnforcementScope::ProtectedBranches,
    });

    answers.allow_push_to_protected = Some(ui.confirm(
        "Allow pushing straight to a protected branch?",
        current.push_allow_protected(),
    )?);

    if ui.confirm("Customize more options?", false)? {
        let length = ui.text(
            "Maximum length of a commit subject",
            Some(&current.commit_subject_max_length().to_string()),
            None,
        )?;
        answers.subject_max_length = Some(length.trim().parse::<u32>().map_err(|_| {
            ErrorCode::InvalidInput
                .error()
                .with_context("field", "subject_max_length")
                .with_context("value", length.trim())
                .with_context("reason", "expected a whole number")
        })?);
        answers.use_emojis = Some(ui.confirm(
            "Put an emoji before each commit type?",
            current.commit_use_emojis(),
        )?);
        answers.require_breaking_footer = Some(ui.confirm(
            "Require a description for breaking changes?",
            current.commit_breaking_require_footer(),
        )?);
        answers.require_ticket = Some(ui.confirm(
            "Require a ticket reference in commits?",
            current.commit_ticket_required(),
        )?);
        answers.tag_prefix = Some(ui.text(
            "Tag prefix",
            Some(&current.versioning_tag_prefix()),
            Some("v gives v1.2.3"),
        )?);
        answers.changelog_output =
            Some(ui.text("Changelog file", Some(&current.changelog_output()), None)?);
    }

    Ok(answers)
}

/// `cw config edit`: the same questions as `cw init`, asked about an existing
/// config, with its current values as the defaults. Shows what would change
/// and saves only if the person agrees.
pub fn edit_config(ctx: &Context, global: bool) -> CoreResult<()> {
    use std::time::Instant;

    use scriba::Table;

    use crate::engine::{
        capabilities::config::{
            effective::{changes, to_document},
            patch::patch_toml,
        },
        config::{ProjectConfig, StandardConfig},
        constants::resolve_global_config_path,
    };

    let start = Instant::now();
    let ui = ctx.ui();

    if !(ctx.is_interactive() && !ctx.auto_yes()) {
        return Err(ErrorCode::UserInteractionRequired
            .error()
            .with_context("command", "config edit")
            .with_context(
                "hint",
                "config edit is a guided editor for a terminal; use `cw config set <key> <value>` in scripts",
            ));
    }

    let path = if global {
        resolve_global_config_path()?
    } else {
        ctx.project_config_path().ok_or_else(|| {
            ErrorCode::ConfigUnreadable.error().with_context(
                "reason",
                "no project config found; run `cw init` to create one",
            )
        })?
    };
    if !path.exists() {
        return Err(ErrorCode::ConfigUnreadable
            .error()
            .with_context("path", path.display().to_string())
            .with_context(
                "reason",
                "config file does not exist; run `cw init` to create one",
            ));
    }

    let format = ConfigFormat::from_path(&path);
    let raw = std::fs::read_to_string(&path)?;
    enum Loaded {
        Project(ProjectConfig),
        Global(StandardConfig),
    }
    let loaded = if global {
        Loaded::Global(StandardConfig::from_str_as(format, &raw)?)
    } else {
        Loaded::Project(ProjectConfig::from_str_as(format, &raw)?)
    };
    let current = match &loaded {
        Loaded::Project(project) => project.inner.config.clone(),
        Loaded::Global(standard) => standard.inner.clone(),
    };

    ui.logger().info(&format!("Editing {}", path.display()));

    // Offer the standard profile's types too, so one can be added; the file's
    // own types start selected. A file with no types table keeps using defaults.
    let own = profile_types(&current);
    let types: Vec<(String, bool)> = if own.is_empty() {
        Vec::new()
    } else {
        let mut names = own.clone();
        for name in profile_types(&BaseConfig::standard()) {
            if !names.contains(&name) {
                names.push(name);
            }
        }
        names
            .into_iter()
            .filter(|name| !MANDATORY_TYPES.contains(&name.as_str()))
            .map(|name| {
                let selected = own.contains(&name);
                (name, selected)
            })
            .collect()
    };

    let answers = ask_settings(ctx, &current, types, true)?;
    let updated = answers.apply(&current)?;

    let before = to_document(&current).ok_or_else(|| ErrorCode::SerializationFailure.error())?;
    let after = to_document(&updated).ok_or_else(|| ErrorCode::SerializationFailure.error())?;
    let differences = changes(&before, &after);

    if differences.is_empty() {
        ui.logger().info("Nothing changed");
        return Ok(());
    }
    for change in &differences {
        ui.logger().info(&format!("  {}", change.describe()));
    }

    let question = format!(
        "Save {} change{} to {}?",
        differences.len(),
        if differences.len() == 1 { "" } else { "s" },
        path.display()
    );
    if !ui.confirm(&question, true)? {
        ui.logger().warn("Nothing was saved");
        return Ok(());
    }

    if !ctx.dry_run() {
        // A TOML file is patched in place so its comments and layout survive;
        // the patch is checked by reading it back, and anything else (or a
        // mismatch) re-renders the whole file.
        let reparsed = |text: &str| match &loaded {
            Loaded::Project(_) => ProjectConfig::from_str_as(ConfigFormat::Toml, text)
                .ok()
                .and_then(|project| to_document(&project.inner.config)),
            Loaded::Global(_) => StandardConfig::from_str_as(ConfigFormat::Toml, text)
                .ok()
                .and_then(|standard| to_document(&standard.inner)),
        };
        let patched = (format == ConfigFormat::Toml)
            .then(|| patch_toml(&raw, &before, &after).ok())
            .flatten()
            .filter(|text| reparsed(text).as_ref() == Some(&after));
        let rendered = match (patched, loaded) {
            (Some(text), _) => text,
            (None, Loaded::Project(mut project)) => {
                project.inner.config = updated;
                format.render(&project)?
            }
            (None, Loaded::Global(mut standard)) => {
                standard.inner = updated;
                format.render(&standard)?
            }
        };
        crate::engine::fs::write_text(&path, &rendered)?;
    }

    let table = Table::new(
        vec![
            "setting".to_string(),
            "before".to_string(),
            "after".to_string(),
        ],
        differences
            .iter()
            .map(|c| {
                vec![
                    c.key.clone(),
                    c.before.clone().unwrap_or_default(),
                    c.after.clone().unwrap_or_default(),
                ]
            })
            .collect(),
    );
    let meta = ui
        .new_output_meta()
        .with_command("config.edit".to_string())
        .with_scope(if global { "global" } else { "project" }.to_string())
        .with_dry_run(ctx.dry_run())
        .with_duration_ms(start.elapsed().as_millis() as u64)
        .with_timestamp(chrono::Utc::now().to_string());
    let content = ui
        .new_output_content()
        .title("Config updated")
        .data("path", path.display().to_string())
        .data("changes", differences.len().to_string())
        .data("saved", (!ctx.dry_run()).to_string())
        .table(Some("Changes".to_string()), table);

    ui.print_with_meta(&content, Some(&meta), true)
}