opencodecommit 1.4.2

AI-powered git commit message generator that delegates to terminal AI agents
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
use std::sync::LazyLock;

use crate::config::{Config, DEFAULT_EMOJIS};

/// Parsed conventional commit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedCommit {
    pub type_name: String,
    pub message: String,
    pub description: Option<String>,
}

static TYPE_PATTERN: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(
        r"^(feat|fix|docs|style|refactor|test|chore|perf|security|revert)(\(.*?\))?:\s*(.+)",
    )
    .unwrap()
});

static ANSI_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap());

static PREAMBLE_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(
        r"(?i)^(?:(?:Here(?:'s| is)|Sure[,.].*?(?:here|is)|I(?:'ll| will).*?:?)\s*(?:your |the |a )?(?:commit )?(?:message|response)?[:\s]*\n+)",
    )
    .unwrap()
});

static CODE_BLOCK_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r"(?s)^```(?:\w*)\n(.*?)\n```$").unwrap());

/// Sanitize an AI response: strip ANSI codes, preamble, code blocks, formatting.
pub fn sanitize_response(response: &str) -> String {
    let mut result = response.trim().to_owned();

    // Strip ANSI codes
    result = ANSI_RE.replace_all(&result, "").to_string();

    // Strip preamble
    result = PREAMBLE_RE.replace(&result, "").trim().to_owned();

    // Strip code blocks: ```\n...\n``` or ```lang\n...\n```
    result = CODE_BLOCK_RE.replace(&result, "$1").trim().to_owned();

    // Strip inline backticks wrapping entire response
    if result.starts_with('`') && result.ends_with('`') && !result.contains('\n') {
        result = result[1..result.len() - 1].trim().to_owned();
    }

    // Strip wrapping quotes
    if (result.starts_with('"') && result.ends_with('"'))
        || (result.starts_with('\'') && result.ends_with('\''))
    {
        result = result[1..result.len() - 1].trim().to_owned();
    }

    // Strip markdown bold/italic wrapping entire response
    if result.starts_with("**") && result.ends_with("**") {
        result = result[2..result.len() - 2].trim().to_owned();
    } else if result.starts_with('*') && result.ends_with('*') && !result.starts_with("**") {
        result = result[1..result.len() - 1].trim().to_owned();
    }

    result
}

/// Parse a response into a conventional commit structure.
pub fn parse_response(response: &str) -> ParsedCommit {
    let sanitized = sanitize_response(response);
    let lines: Vec<&str> = sanitized.lines().collect();
    let first_line = lines.first().map(|l| l.trim()).unwrap_or("");

    if let Some(caps) = TYPE_PATTERN.captures(first_line) {
        let type_name = caps.get(1).unwrap().as_str().to_owned();
        let message = caps.get(3).unwrap().as_str().to_owned();
        let remaining: Vec<&str> = lines[1..]
            .iter()
            .filter(|l| !l.trim().is_empty())
            .copied()
            .collect();
        let description = if remaining.is_empty() {
            None
        } else {
            Some(remaining.join("\n"))
        };
        return ParsedCommit {
            type_name,
            message,
            description,
        };
    }

    ParsedCommit {
        type_name: infer_type(first_line),
        message: if first_line.is_empty() {
            "update code".to_owned()
        } else {
            first_line.to_owned()
        },
        description: None,
    }
}

/// Infer a conventional commit type from an unstructured message.
fn infer_type(message: &str) -> String {
    let lower = message.to_lowercase();
    static DOCS_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"\b(readme|docs?|documentation|changelog|comment|jsdoc|rustdoc)\b")
            .unwrap()
    });
    static FIX_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"\b(fix|bug|patch|resolve|issue|error|crash|repair)\b").unwrap()
    });
    static FEAT_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"\b(add|implement|feature|new|introduce|support|create)\b").unwrap()
    });
    static REFACTOR_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"\b(refactor|restructure|reorganize|rename|move|extract|simplify)\b")
            .unwrap()
    });
    static TEST_RE: LazyLock<regex::Regex> =
        LazyLock::new(|| regex::Regex::new(r"\b(tests?|spec|assert|coverage)\b").unwrap());
    static STYLE_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"\b(style|format|whitespace|indent|lint|prettier|biome)\b").unwrap()
    });
    static PERF_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"\b(perf|performance|optimiz|speed|faster|cache)\b").unwrap()
    });
    static REVERT_RE: LazyLock<regex::Regex> =
        LazyLock::new(|| regex::Regex::new(r"\b(revert|undo|rollback)\b").unwrap());

    if DOCS_RE.is_match(&lower) {
        return "docs".to_owned();
    }
    if FIX_RE.is_match(&lower) {
        return "fix".to_owned();
    }
    if FEAT_RE.is_match(&lower) {
        return "feat".to_owned();
    }
    if REFACTOR_RE.is_match(&lower) {
        return "refactor".to_owned();
    }
    if TEST_RE.is_match(&lower) {
        return "test".to_owned();
    }
    if STYLE_RE.is_match(&lower) {
        return "style".to_owned();
    }
    if PERF_RE.is_match(&lower) {
        return "perf".to_owned();
    }
    static SECURITY_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
        regex::Regex::new(r"\b(security|vulnerab|auth|cve|xss|csrf|injection|sanitiz)\b").unwrap()
    });
    if REVERT_RE.is_match(&lower) {
        return "revert".to_owned();
    }
    if SECURITY_RE.is_match(&lower) {
        return "security".to_owned();
    }
    "chore".to_owned()
}

/// Format a parsed commit using the config template, emojis, lowercase.
pub fn format_commit_message(parsed: &ParsedCommit, config: &Config) -> String {
    let mut message = parsed.message.clone();

    // Apply lowercase
    if config.use_lower_case && !message.is_empty() {
        let mut chars = message.chars();
        if let Some(first) = chars.next() {
            message = first.to_lowercase().to_string() + chars.as_str();
        }
    }

    // Resolve emoji
    let mut emoji = String::new();
    if config.use_emojis {
        // Check custom emojis first
        if let Some(e) = config.custom.emojis.get(&parsed.type_name) {
            emoji = e.clone();
        }
        // Fallback to defaults
        if emoji.is_empty() {
            for &(t, e) in DEFAULT_EMOJIS {
                if t == parsed.type_name {
                    emoji = e.to_owned();
                    break;
                }
            }
        }
    }

    // Apply template
    let mut result = config
        .commit_template
        .replace("{{type}}", &parsed.type_name)
        .replace("{{emoji}}", &emoji)
        .replace("{{message}}", &message);

    // Clean up: collapse multiple spaces, remove space before colon
    static MULTI_SPACE: LazyLock<regex::Regex> =
        LazyLock::new(|| regex::Regex::new(r"\s+").unwrap());
    static SPACE_COLON: LazyLock<regex::Regex> =
        LazyLock::new(|| regex::Regex::new(r"\s+:").unwrap());

    result = MULTI_SPACE.replace_all(&result, " ").to_string();
    result = SPACE_COLON.replace_all(&result, ":").to_string();
    result = result.trim().to_owned();

    // Append description
    if let Some(ref desc) = parsed.description {
        result.push_str(&format!("\n\n{desc}"));
    }

    result
}

/// Format an adaptive-mode response (sanitize only, no type parsing).
pub fn format_adaptive_message(response: &str) -> String {
    let sanitized = sanitize_response(response);
    if sanitized.is_empty() {
        "update code".to_owned()
    } else {
        sanitized
    }
}

/// Format a branch name from AI response: sanitize, slugify.
pub fn format_branch_name(response: &str) -> String {
    let sanitized = sanitize_response(response);
    let name = sanitized.lines().next().unwrap_or("").trim();
    if name.is_empty() {
        return "chore/update".to_owned();
    }
    // Already in type/slug format? Return as-is if it looks right.
    if name.contains('/') && !name.contains(' ') && name.len() <= 60 {
        return name.to_lowercase();
    }
    // Slugify: lowercase, replace non-alphanumeric with hyphens, collapse
    let slug: String = name
        .to_lowercase()
        .chars()
        .map(|c| {
            if c.is_alphanumeric() || c == '/' || c == '-' {
                c
            } else {
                '-'
            }
        })
        .collect();
    static MULTI_HYPHEN: LazyLock<regex::Regex> =
        LazyLock::new(|| regex::Regex::new(r"-{2,}").unwrap());
    let slug = MULTI_HYPHEN.replace_all(&slug, "-");
    let slug = slug.trim_matches('-');
    if slug.is_empty() {
        "chore/update".to_owned()
    } else {
        slug.to_owned()
    }
}

/// Parsed PR output.
#[derive(Debug, Clone)]
pub struct ParsedPr {
    pub title: String,
    pub body: String,
}

/// Parse a PR response into title and body.
pub fn parse_pr_response(response: &str) -> ParsedPr {
    let sanitized = sanitize_response(response);
    let lines: Vec<&str> = sanitized.lines().collect();

    let mut title = String::new();
    let mut body_start = 0;

    for (i, line) in lines.iter().enumerate() {
        let trimmed = line.trim();
        if let Some(t) = trimmed.strip_prefix("TITLE:") {
            title = t.trim().to_owned();
            body_start = i + 1;
            break;
        }
    }

    // Skip "BODY:" line if present
    if body_start < lines.len() && lines[body_start].trim().starts_with("BODY:") {
        body_start += 1;
    }

    let body = if body_start < lines.len() {
        lines[body_start..].join("\n").trim().to_owned()
    } else {
        String::new()
    };

    if title.is_empty() {
        // Fallback: first line is title, rest is body
        ParsedPr {
            title: lines.first().unwrap_or(&"Update").to_string(),
            body: if lines.len() > 1 {
                lines[1..].join("\n").trim().to_owned()
            } else {
                String::new()
            },
        }
    } else {
        ParsedPr { title, body }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    fn make_config(overrides: impl FnOnce(&mut Config)) -> Config {
        let mut cfg = Config::default();
        overrides(&mut cfg);
        cfg
    }

    // --- sanitizeResponse tests (ported from TS) ---

    #[test]
    fn sanitize_strips_code_block() {
        assert_eq!(
            sanitize_response("```\nfeat: add login\n```"),
            "feat: add login"
        );
    }

    #[test]
    fn sanitize_strips_code_block_with_language() {
        assert_eq!(
            sanitize_response("```text\nfeat: add login\n```"),
            "feat: add login"
        );
    }

    #[test]
    fn sanitize_strips_inline_backticks() {
        assert_eq!(sanitize_response("`feat: add login`"), "feat: add login");
    }

    #[test]
    fn sanitize_strips_double_quotes() {
        assert_eq!(sanitize_response("\"feat: add login\""), "feat: add login");
    }

    #[test]
    fn sanitize_strips_single_quotes() {
        assert_eq!(sanitize_response("'feat: add login'"), "feat: add login");
    }

    #[test]
    fn sanitize_strips_markdown_bold() {
        assert_eq!(sanitize_response("**feat: add login**"), "feat: add login");
    }

    #[test]
    fn sanitize_strips_markdown_italic() {
        assert_eq!(sanitize_response("*feat: add login*"), "feat: add login");
    }

    #[test]
    fn sanitize_trims_whitespace() {
        assert_eq!(sanitize_response("  feat: add login  "), "feat: add login");
    }

    #[test]
    fn sanitize_handles_clean_input() {
        assert_eq!(sanitize_response("feat: add login"), "feat: add login");
    }

    #[test]
    fn sanitize_strips_ansi() {
        assert_eq!(
            sanitize_response("\x1b[32mfeat: add login\x1b[0m"),
            "feat: add login"
        );
    }

    #[test]
    fn sanitize_strips_preamble() {
        assert_eq!(
            sanitize_response("Here's your commit message:\nfeat: add login"),
            "feat: add login"
        );
    }

    #[test]
    fn sanitize_strips_sure_preamble() {
        assert_eq!(
            sanitize_response("Sure, here is the commit message:\nfeat: add login"),
            "feat: add login"
        );
    }

    // --- parseResponse tests (ported from TS) ---

    #[test]
    fn parse_conventional_commit() {
        let result = parse_response("feat: add login page");
        assert_eq!(result.type_name, "feat");
        assert_eq!(result.message, "add login page");
        assert!(result.description.is_none());
    }

    #[test]
    fn parse_commit_with_scope() {
        let result = parse_response("fix(auth): resolve token expiry");
        assert_eq!(result.type_name, "fix");
        assert_eq!(result.message, "resolve token expiry");
    }

    #[test]
    fn parse_multiline_response() {
        let result = parse_response(
            "feat: update authentication\n\n- add JWT tokens\n- remove session cookies",
        );
        assert_eq!(result.type_name, "feat");
        assert_eq!(result.message, "update authentication");
        let desc = result.description.unwrap();
        assert!(desc.contains("add JWT tokens"));
        assert!(desc.contains("remove session cookies"));
    }

    #[test]
    fn parse_malformed_fallback() {
        let result = parse_response("just some random text");
        assert_eq!(result.type_name, "chore");
        assert_eq!(result.message, "just some random text");
    }

    #[test]
    fn parse_infers_docs_type() {
        let result = parse_response("update README with installation instructions");
        assert_eq!(result.type_name, "docs");
        assert_eq!(
            result.message,
            "update README with installation instructions"
        );
    }

    #[test]
    fn parse_infers_feat_type() {
        let result = parse_response("add new login page");
        assert_eq!(result.type_name, "feat");
    }

    #[test]
    fn parse_infers_fix_type() {
        let result = parse_response("fix crash on startup");
        assert_eq!(result.type_name, "fix");
    }

    #[test]
    fn parse_empty_response() {
        let result = parse_response("");
        assert_eq!(result.type_name, "chore");
        assert_eq!(result.message, "update code");
    }

    #[test]
    fn parse_code_block_wrapped() {
        let result = parse_response("```\nfeat: add login\n```");
        assert_eq!(result.type_name, "feat");
        assert_eq!(result.message, "add login");
    }

    #[test]
    fn parse_all_valid_types() {
        let types = [
            "feat", "fix", "docs", "style", "refactor", "test", "chore", "perf", "revert",
        ];
        for t in types {
            let result = parse_response(&format!("{t}: some message"));
            assert_eq!(result.type_name, t, "failed for type: {t}");
        }
    }

    // --- formatCommitMessage tests (ported from TS) ---

    #[test]
    fn format_default_template() {
        let config = Config::default();
        let result = format_commit_message(
            &ParsedCommit {
                type_name: "feat".to_owned(),
                message: "Add login".to_owned(),
                description: None,
            },
            &config,
        );
        assert_eq!(result, "feat: add login");
    }

    #[test]
    fn format_applies_lowercase() {
        let config = make_config(|c| c.use_lower_case = true);
        let result = format_commit_message(
            &ParsedCommit {
                type_name: "feat".to_owned(),
                message: "Add login".to_owned(),
                description: None,
            },
            &config,
        );
        assert_eq!(result, "feat: add login");
    }

    #[test]
    fn format_preserves_case() {
        let config = make_config(|c| c.use_lower_case = false);
        let result = format_commit_message(
            &ParsedCommit {
                type_name: "feat".to_owned(),
                message: "Add login".to_owned(),
                description: None,
            },
            &config,
        );
        assert_eq!(result, "feat: Add login");
    }

    #[test]
    fn format_emoji_without_template_placeholder() {
        let config = make_config(|c| c.use_emojis = true);
        let result = format_commit_message(
            &ParsedCommit {
                type_name: "feat".to_owned(),
                message: "add login".to_owned(),
                description: None,
            },
            &config,
        );
        // Default template has no {{emoji}} placeholder
        assert_eq!(result, "feat: add login");
    }

    #[test]
    fn format_emoji_with_template() {
        let config = make_config(|c| {
            c.use_emojis = true;
            c.commit_template = "{{emoji}} {{type}}: {{message}}".to_owned();
        });
        let result = format_commit_message(
            &ParsedCommit {
                type_name: "feat".to_owned(),
                message: "add login".to_owned(),
                description: None,
            },
            &config,
        );
        assert_eq!(result, "\u{2728} feat: add login");
    }

    #[test]
    fn format_custom_emoji_override() {
        let config = make_config(|c| {
            c.use_emojis = true;
            c.commit_template = "{{emoji}} {{type}}: {{message}}".to_owned();
            c.custom.emojis = HashMap::from([("feat".to_owned(), "\u{1f680}".to_owned())]);
        });
        let result = format_commit_message(
            &ParsedCommit {
                type_name: "feat".to_owned(),
                message: "add login".to_owned(),
                description: None,
            },
            &config,
        );
        assert_eq!(result, "\u{1f680} feat: add login");
    }

    #[test]
    fn format_appends_description() {
        let config = Config::default();
        let result = format_commit_message(
            &ParsedCommit {
                type_name: "feat".to_owned(),
                message: "Update auth".to_owned(),
                description: Some("- add JWT\n- remove cookies".to_owned()),
            },
            &config,
        );
        assert!(result.starts_with("feat: update auth"));
        assert!(result.contains("- add JWT"));
        assert!(result.contains("- remove cookies"));
    }

    #[test]
    fn format_collapses_multiple_spaces() {
        let config = make_config(|c| {
            c.commit_template = "{{type}}:  {{message}}".to_owned();
        });
        let result = format_commit_message(
            &ParsedCommit {
                type_name: "feat".to_owned(),
                message: "add login".to_owned(),
                description: None,
            },
            &config,
        );
        assert!(!result.contains("  "));
    }

    // --- format_adaptive_message ---

    #[test]
    fn adaptive_returns_sanitized() {
        assert_eq!(
            format_adaptive_message("```\nfeat: add login\n```"),
            "feat: add login"
        );
    }

    #[test]
    fn adaptive_empty_fallback() {
        assert_eq!(format_adaptive_message(""), "update code");
    }
}