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
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
//! `cw commit`: builds a Conventional Commit message from flags, or by asking for the missing parts on a terminal, and creates the commit.

use crate::engine::capabilities::commit::fix::build_header;
use std::time::Instant;

use scriba::{SelectOption, SelectRequest};

use crate::{
    core::{Context, CoreResult},
    engine::{
        ErrorCode, PromptTrait,
        capabilities::commit::ticket,
        constants::emoji::{ERROR, PREVIEW, SUCCESS},
        models::policy::{
            commit::{ScopeRequirement, TicketPolicy},
            enforcement::TicketSource,
        },
    },
};

/// What was asked of `cw commit`, from flags.
#[derive(Debug, Clone, Default)]
pub struct Options {
    pub allow_empty: bool,
    pub commit_type: Option<String>,
    pub scope: Option<String>,
    pub message: Option<String>,
    pub breaking: bool,
    pub breaking_message: Option<String>,
    pub body: Option<String>,
    pub footer: Vec<String>,
    pub ticket: Option<String>,
    /// Stage every change, untracked files included (`-A`).
    pub stage_all: bool,
    /// Stage changes to tracked files only (`-a`).
    pub stage_tracked: bool,
}

pub fn run(ctx: &Context, options: Options) -> CoreResult<()> {
    let Options {
        allow_empty,
        commit_type,
        scope,
        message,
        breaking,
        breaking_message,
        body,
        footer,
        ticket,
        stage_all,
        stage_tracked,
    } = options;
    let ui = ctx.ui();
    let start = Instant::now();
    let policy = &ctx.policy().commit;

    let non_interactive = !ctx.is_interactive() || (commit_type.is_some() && message.is_some());

    // A breaking description is only mandatory when the policy requires the
    // footer; otherwise it is optional, and written when provided.
    if breaking
        && policy.breaking_footer_required
        && breaking_message
            .as_ref()
            .map(|s| s.trim().is_empty())
            .unwrap_or(true)
    {
        return Err(ErrorCode::InvalidInput.error().with_context(
            "reason",
            "breaking change flag was provided but no breaking message was supplied",
        ));
    }

    // commit type
    let commit_type = match commit_type {
        Some(value) => {
            if !policy.allows_type(&value) {
                return Err(ErrorCode::InvalidInput
                    .error()
                    .with_context("field", "type")
                    .with_context("value", value));
            }
            value
        }
        None if non_interactive => {
            return Err(ErrorCode::InvalidInput
                .error()
                .with_context("field", "type")
                .with_context("reason", "type is required in non-interactive mode"));
        }
        None => {
            let request = SelectRequest::new(
                "What type of change are you committing?".to_string(),
                policy
                    .types
                    .iter()
                    .map(|t| {
                        let label = if policy.use_emojis && t.emoji.is_some() {
                            format!("{} {}", t.emoji.as_deref().unwrap(), t.key)
                        } else {
                            t.key.clone()
                        };
                        SelectOption::new(t.key.clone(), label)
                            .description(t.description.as_deref().unwrap_or_default().to_string())
                    })
                    .collect(),
            )
            .with_page_size(10);

            ui.select(&request).map_err(|_| {
                ErrorCode::InvalidInput
                    .error()
                    .with_context("field", "type")
            })?
        }
    };

    // scope
    let scope: Option<String> = match scope {
        Some(value) if policy.scope_requirement == ScopeRequirement::Disabled => {
            return Err(ErrorCode::InvalidInput
                .error()
                .with_context("field", "scope")
                .with_context("value", value)
                .with_context(
                    "reason",
                    "scopes are disabled (commit.scopes.mode = disabled)",
                ));
        }
        None if policy.scope_requirement == ScopeRequirement::Disabled => None,
        Some(value) => {
            if policy.restrict_scopes_to_defined && !policy.allows_scope(&value) {
                return Err(ErrorCode::InvalidInput
                    .error()
                    .with_context("field", "scope")
                    .with_context("value", value));
            }
            Some(value)
        }
        None if policy.header_format.require_scope && non_interactive => {
            return Err(ErrorCode::InvalidInput
                .error()
                .with_context("field", "scope")
                .with_context(
                    "reason",
                    "scope is required by policy in non-interactive mode",
                ));
        }
        None if policy.header_format.require_scope => {
            let value = ui.text("Scope", None, Some("Required by current commit policy"))?;
            let trimmed: String = value.trim().to_string();

            if trimmed.is_empty() {
                return Err(ErrorCode::InvalidInput
                    .error()
                    .with_context("field", "scope")
                    .with_context("reason", "scope is required by policy"));
            }

            if policy.restrict_scopes_to_defined && !policy.allows_scope(&trimmed) {
                return Err(ErrorCode::InvalidInput
                    .error()
                    .with_context("field", "scope")
                    .with_context("value", trimmed));
            }

            Some(trimmed)
        }
        None if non_interactive => None,
        None => {
            if ui.confirm("Add a scope?", false)? {
                let value = ui.text("Scope", None, Some("Leave empty to omit"))?;
                let trimmed: String = value.trim().to_string();

                if trimmed.is_empty() {
                    None
                } else {
                    if policy.restrict_scopes_to_defined && !policy.allows_scope(&trimmed) {
                        return Err(ErrorCode::InvalidInput
                            .error()
                            .with_context("field", "scope")
                            .with_context("value", trimmed));
                    }
                    Some(trimmed)
                }
            } else {
                None
            }
        }
    };

    // ticket
    let ticket = if policy.ticket.required
        || policy.ticket.source != TicketSource::Disabled
        || ticket.is_some()
    {
        resolve_ticket(ctx, &policy.ticket, ticket, non_interactive)?
    } else {
        None
    };

    // summary
    let summary = match message {
        Some(value) if value.trim().is_empty() => {
            return Err(ErrorCode::InvalidInput
                .error()
                .with_context("field", "message")
                .with_context("reason", "commit summary cannot be empty"));
        }
        Some(value) => value.trim().to_string(),
        None if non_interactive => {
            return Err(ErrorCode::InvalidInput
                .error()
                .with_context("field", "message")
                .with_context("reason", "message is required in non-interactive mode"));
        }
        None => loop {
            let value = ui.text(
                "Write a short summary",
                None,
                Some("Imperative tone, e.g. 'add parser'"),
            )?;
            let trimmed = value.trim().to_string();
            if !trimmed.is_empty() {
                break trimmed;
            }
            ui.logger().warn("Summary cannot be empty");
        },
    };

    // The ticket opens the subject, so it counts towards the length limit.
    let summary = match &ticket {
        Some(ticket) => ticket::with_ticket(&policy.ticket.header_format, ticket, &summary)?,
        None => summary,
    };

    let subject_max_length = usize::try_from(policy.subject_max_length).unwrap();

    if summary.len() > subject_max_length {
        return Err(ErrorCode::InvalidInput
            .error()
            .with_context("field", "message")
            .with_context("reason", "commit summary exceeds configured max length")
            .with_context("length", summary.len().to_string())
            .with_context("max", policy.subject_max_length.to_string()));
    }

    // breaking details
    let (breaking, breaking_message) = if breaking {
        (true, breaking_message.map(|s| s.trim().to_string()))
    } else if non_interactive {
        (false, None)
    } else if ui.confirm("Does this commit include breaking changes?", false)? {
        let msg = ui.text(
            "Describe the breaking change",
            None,
            Some(if policy.breaking_footer_required {
                "Required by current commit policy"
            } else {
                "Optional; leave empty to omit the BREAKING CHANGE footer"
            }),
        )?;
        let trimmed = msg.trim().to_string();
        if trimmed.is_empty() && policy.breaking_footer_required {
            return Err(ErrorCode::InvalidInput
                .error()
                .with_context("field", "breaking_message")
                .with_context("reason", "breaking change description cannot be empty"));
        }
        (true, (!trimmed.is_empty()).then_some(trimmed))
    } else {
        (false, None)
    };

    let body = match body {
        Some(value) if value.trim().is_empty() => None,
        Some(value) => Some(value.trim().to_string()),
        None if non_interactive => None,
        None => {
            if ui.confirm("Add a longer description?", false)? {
                let value = ui.text("Body", None, Some("Optional detailed description"))?;
                let trimmed = value.trim().to_string();
                if trimmed.is_empty() {
                    None
                } else {
                    Some(trimmed)
                }
            } else {
                None
            }
        }
    };

    let footers = if !footer.is_empty() || non_interactive {
        footer
            .into_iter()
            .map(|v| v.trim().to_string())
            .filter(|v| !v.is_empty())
            .collect::<Vec<_>>()
    } else {
        let mut lines = Vec::new();
        if ui.confirm("Add footer lines?", false)? {
            loop {
                let value = ui.text(
                    "Footer",
                    None,
                    Some("Examples: Closes #123, Co-authored-by: Name <email>"),
                )?;
                let trimmed = value.trim().to_string();
                if trimmed.is_empty() {
                    break;
                }
                lines.push(trimmed);
            }
        }
        lines
    };

    let type_emoji = policy
        .find_type(&commit_type)
        .and_then(|t| t.emoji.as_deref());

    let header = build_header(
        &commit_type,
        scope.as_deref(),
        &summary,
        breaking,
        type_emoji,
        policy.use_emojis,
    );
    let commit_message = build_commit_message(
        &header,
        body.as_deref(),
        if breaking {
            breaking_message.as_deref()
        } else {
            None
        },
        &footers,
        &policy.breaking_footer_key,
    );

    ui.logger().heading(&format!("{} Commit Preview", PREVIEW));
    eprintln!("-------------------------");
    for line in commit_message.lines() {
        eprintln!("{}", line);
    }
    eprintln!("-------------------------");
    eprintln!();

    let duration_ms = start.elapsed().as_millis() as u64;

    if ctx.dry_run() {
        let meta = ui
            .new_output_meta()
            .with_duration_ms(duration_ms)
            .with_timestamp(chrono::Utc::now().to_string())
            .with_command("commit".to_string())
            .with_dry_run(true);

        let content = ui
            .new_output_content()
            .title(format!("{} Commit Preview", PREVIEW))
            .subtitle("Dry run: no commit was created")
            .data(
                "staging",
                if stage_all {
                    "all"
                } else if stage_tracked {
                    "tracked"
                } else {
                    "none"
                },
            )
            .data("type", commit_type)
            .data("scope", scope.unwrap_or_default())
            .data("breaking", breaking.to_string())
            .data("message", commit_message);

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

    if !non_interactive && !ui.confirm("Create this commit?", true)? {
        ui.logger().warn(&format!("{} Commit cancelled", ERROR));
        return Ok(());
    }

    if stage_all {
        ctx.git().stage_all()?;
    } else if stage_tracked {
        ctx.git().stage_tracked()?;
    }

    ctx.git().commit(&commit_message, allow_empty)?;

    let meta = ui
        .new_output_meta()
        .with_duration_ms(duration_ms)
        .with_timestamp(chrono::Utc::now().to_string())
        .with_command("commit".to_string())
        .with_dry_run(false);

    let content = ui
        .new_output_content()
        .title(format!("{} Commit Created", SUCCESS))
        .subtitle("Git commit completed successfully")
        .data("type", commit_type)
        .data("scope", scope.unwrap_or_default())
        .data("breaking", breaking.to_string())
        .data("header", header);

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

/// Works out the ticket for a commit: the `--ticket` flag, else the branch name or a
/// question, as `commit.ticket.source` says. `None` means the commit has no ticket.
fn resolve_ticket(
    ctx: &Context,
    policy: &TicketPolicy,
    given: Option<String>,
    non_interactive: bool,
) -> CoreResult<Option<String>> {
    let pattern = policy.regex.as_deref();
    let ui = ctx.ui();

    let missing = || {
        ErrorCode::InvalidInput
            .error()
            .with_context("field", "ticket")
            .with_context(
                "reason",
                "a ticket is required (commit.ticket.required); pass --ticket",
            )
    };

    if let Some(value) = given {
        let value = value.trim().to_string();
        if !ticket::is_valid(pattern, &value)? {
            return Err(ErrorCode::InvalidInput
                .error()
                .with_context("field", "ticket")
                .with_context("value", value)
                .with_context("reason", "does not match commit.ticket.pattern"));
        }
        return Ok(Some(value));
    }

    // A required ticket needs a way to be given even when no source is configured.
    let source = if policy.source == TicketSource::Disabled && policy.required {
        TicketSource::Prompt
    } else {
        policy.source
    };

    let from_branch = match source {
        TicketSource::Branch | TicketSource::BranchOrPrompt => ctx
            .git()
            .current_branch()
            .ok()
            .map(|branch| ticket::from_branch(pattern, &branch))
            .transpose()?
            .flatten(),
        _ => None,
    };

    match source {
        TicketSource::Disabled => Ok(None),
        TicketSource::Branch => match from_branch {
            Some(found) => Ok(Some(found)),
            None if policy.required => Err(missing()),
            None => Ok(None),
        },
        TicketSource::Prompt | TicketSource::BranchOrPrompt => {
            if let Some(found) = from_branch {
                return Ok(Some(found));
            }
            if non_interactive {
                return if policy.required {
                    Err(missing())
                } else {
                    Ok(None)
                };
            }
            loop {
                let help = match (policy.required, pattern) {
                    (true, Some(p)) => format!("Required; must match {p}"),
                    (true, None) => "Required".to_string(),
                    (false, Some(p)) => format!("Leave empty for none; must match {p}"),
                    (false, None) => "Leave empty for none".to_string(),
                };
                let value = ui.text("Ticket", None, Some(&help))?;
                let value = value.trim().to_string();
                if value.is_empty() {
                    if policy.required {
                        ui.logger().warn("A ticket is required");
                        continue;
                    }
                    return Ok(None);
                }
                if ticket::is_valid(pattern, &value)? {
                    return Ok(Some(value));
                }
                ui.logger()
                    .warn("That does not match commit.ticket.pattern");
            }
        }
    }
}

fn build_commit_message(
    header: &str,
    body: Option<&str>,
    breaking_message: Option<&str>,
    footers: &[String],
    breaking_footer_key: &str,
) -> String {
    let mut out = String::from(header);

    if let Some(body) = body {
        let trimmed = body.trim();
        if !trimmed.is_empty() {
            out.push_str("\n\n");
            out.push_str(trimmed);
        }
    }

    if let Some(message) = breaking_message {
        let trimmed = message.trim();
        if !trimmed.is_empty() {
            out.push_str("\n\n");
            out.push_str(breaking_footer_key);
            out.push_str(": ");
            out.push_str(trimmed);
        }
    }

    for footer in footers {
        let trimmed = footer.trim();
        if !trimmed.is_empty() {
            out.push('\n');
            out.push_str(trimmed);
        }
    }

    out
}