ferrflow 5.32.1

Universal semantic versioning for monorepos and classic repos
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
use regex::Regex;
use std::sync::OnceLock;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum BumpType {
    None,
    Patch,
    Minor,
    Major,
}

impl std::fmt::Display for BumpType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BumpType::None => write!(f, "none"),
            BumpType::Patch => write!(f, "patch"),
            BumpType::Minor => write!(f, "minor"),
            BumpType::Major => write!(f, "major"),
        }
    }
}

static BREAKING_RE: OnceLock<Regex> = OnceLock::new();
static FEAT_RE: OnceLock<Regex> = OnceLock::new();

fn breaking_header_re() -> &'static Regex {
    BREAKING_RE.get_or_init(|| {
        Regex::new(r"^(feat|fix|refactor|perf|build|chore|docs|style|test|ci)(\(.+\))?!:").unwrap()
    })
}

fn feat_header_re() -> &'static Regex {
    FEAT_RE.get_or_init(|| Regex::new(r"^feat(\(.+\))?:").unwrap())
}

static BREAKING_FOOTER_RE: OnceLock<Regex> = OnceLock::new();

fn breaking_footer_re() -> &'static Regex {
    BREAKING_FOOTER_RE.get_or_init(|| Regex::new(r"(?m)^BREAKING[ -]CHANGE: ").unwrap())
}

pub fn determine_bump(message: &str) -> BumpType {
    match classify_commit(message) {
        CommitCategory::Breaking => BumpType::Major,
        CommitCategory::Feature => BumpType::Minor,
        CommitCategory::Fix | CommitCategory::Refactor => BumpType::Patch,
        CommitCategory::Other => BumpType::None,
    }
}

/// Author-facing categorization of a single commit. Drives both the
/// version bump (via [`determine_bump`]) and changelog section grouping,
/// so the two can't disagree the way they used to. See #525.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommitCategory {
    /// `feat!:`, `fix(scope)!:`, etc. or a `BREAKING CHANGE:` footer.
    Breaking,
    /// `feat:` or `feat(scope):` (and not breaking).
    Feature,
    /// `fix:` / `perf:` — patch-bumping bug or performance changes.
    Fix,
    /// `refactor:` — patch-bumping internal restructuring. Distinct
    /// from `Fix` so the changelog can render it as its own section
    /// (the prior code dropped refactor commits entirely from the
    /// changelog while still letting them trigger a release).
    Refactor,
    /// `chore:` / `docs:` / `ci:` / `style:` / `test:` / `build:` or any
    /// non-conventional message. Doesn't bump and shouldn't appear in
    /// the user-facing changelog.
    Other,
}

pub fn classify_commit(message: &str) -> CommitCategory {
    let header = parse_subject(message);

    if breaking_header_re().is_match(header) || breaking_footer_re().is_match(message) {
        return CommitCategory::Breaking;
    }
    if feat_header_re().is_match(header) {
        return CommitCategory::Feature;
    }
    if fix_perf_header_re().is_match(header) {
        return CommitCategory::Fix;
    }
    if refactor_header_re().is_match(header) {
        return CommitCategory::Refactor;
    }
    CommitCategory::Other
}

static FIX_PERF_RE: OnceLock<Regex> = OnceLock::new();
static REFACTOR_RE: OnceLock<Regex> = OnceLock::new();

fn fix_perf_header_re() -> &'static Regex {
    FIX_PERF_RE.get_or_init(|| Regex::new(r"^(fix|perf)(\(.+\))?:").unwrap())
}

fn refactor_header_re() -> &'static Regex {
    REFACTOR_RE.get_or_init(|| Regex::new(r"^refactor(\(.+\))?:").unwrap())
}

pub fn parse_subject(message: &str) -> &str {
    message.lines().next().unwrap_or("").trim()
}

static HEADER_RE: OnceLock<Regex> = OnceLock::new();

fn header_re() -> &'static Regex {
    HEADER_RE.get_or_init(|| {
        Regex::new(r"^(?P<type>[a-z]+)(?:\((?P<scope>[^()]+)\))?(?P<bang>!)?:\s*(?P<desc>.*)$")
            .unwrap()
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedHeader<'a> {
    pub commit_type: &'a str,
    pub scope: Option<&'a str>,
    pub breaking_bang: bool,
    pub description: &'a str,
}

pub fn parse_header(message: &str) -> Option<ParsedHeader<'_>> {
    let subject = parse_subject(message);
    let caps = header_re().captures(subject)?;
    Some(ParsedHeader {
        commit_type: caps.name("type")?.as_str(),
        scope: caps.name("scope").map(|m| m.as_str()),
        breaking_bang: caps.name("bang").is_some(),
        description: caps.name("desc").map(|m| m.as_str()).unwrap_or(""),
    })
}

pub fn is_breaking(message: &str) -> bool {
    matches!(classify_commit(message), CommitCategory::Breaking)
}

pub fn breaking_footer_body(message: &str) -> Option<String> {
    let re = breaking_footer_re();
    let mat = re.find(message)?;
    let body = message[mat.end()..].trim();
    if body.is_empty() {
        None
    } else {
        Some(body.lines().next().unwrap_or("").trim().to_string())
    }
}

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

    #[test]
    fn test_patch() {
        assert_eq!(determine_bump("fix: correct typo"), BumpType::Patch);
        assert_eq!(determine_bump("perf: faster query"), BumpType::Patch);
        assert_eq!(determine_bump("refactor: clean up"), BumpType::Patch);
    }

    #[test]
    fn test_minor() {
        assert_eq!(determine_bump("feat: add login"), BumpType::Minor);
        assert_eq!(determine_bump("feat(auth): add JWT"), BumpType::Minor);
    }

    #[test]
    fn test_major() {
        assert_eq!(determine_bump("feat!: breaking change"), BumpType::Major);
        assert_eq!(
            determine_bump("fix(api)!: remove endpoint"),
            BumpType::Major
        );
        assert_eq!(
            determine_bump("BREAKING CHANGE: removed X"),
            BumpType::Major
        );
    }

    #[test]
    fn test_none() {
        assert_eq!(determine_bump("chore: update deps"), BumpType::None);
        assert_eq!(determine_bump("docs: update readme"), BumpType::None);
        assert_eq!(determine_bump("ci: fix pipeline"), BumpType::None);
    }

    #[test]
    fn test_parse_subject() {
        assert_eq!(parse_subject("feat: add login"), "feat: add login");
        assert_eq!(
            parse_subject("feat: add login\n\nbody text"),
            "feat: add login"
        );
        assert_eq!(parse_subject("  spaced  "), "spaced");
        assert_eq!(parse_subject(""), "");
    }

    #[test]
    fn test_scoped_commits() {
        assert_eq!(determine_bump("fix(api): null check"), BumpType::Patch);
        assert_eq!(determine_bump("feat(ui): new button"), BumpType::Minor);
        assert_eq!(determine_bump("refactor(db): simplify"), BumpType::Patch);
    }

    #[test]
    fn test_breaking_change_in_body() {
        let msg = "feat: something\n\nBREAKING CHANGE: removed old API";
        assert_eq!(determine_bump(msg), BumpType::Major);
    }

    #[test]
    fn test_breaking_change_hyphen_footer() {
        let msg = "feat: something\n\nBREAKING-CHANGE: removed old API";
        assert_eq!(determine_bump(msg), BumpType::Major);
    }

    #[test]
    fn test_breaking_change_prose_is_not_major() {
        assert_eq!(
            determine_bump("docs: note that BREAKING CHANGES are coming in v2"),
            BumpType::None
        );
        let body = "feat: add flag\n\nBREAKING CHANGE will be handled later, not yet";
        assert_eq!(determine_bump(body), BumpType::Minor);
        let plural = "chore: cleanup\n\nBREAKING CHANGES: none in this one";
        assert_eq!(determine_bump(plural), BumpType::None);
    }

    #[test]
    fn test_breaking_change_footer_missing_space_after_colon() {
        let msg = "feat: x\n\nBREAKING CHANGE:no-space-description";
        assert_eq!(determine_bump(msg), BumpType::Minor);
    }

    #[test]
    fn test_bump_ordering() {
        assert!(BumpType::Major > BumpType::Minor);
        assert!(BumpType::Minor > BumpType::Patch);
        assert!(BumpType::Patch > BumpType::None);
    }

    #[test]
    fn test_empty_message() {
        assert_eq!(determine_bump(""), BumpType::None);
    }

    #[test]
    fn test_whitespace_only_message() {
        assert_eq!(determine_bump("   \n\n  "), BumpType::None);
    }

    #[test]
    fn test_non_conventional_message() {
        assert_eq!(determine_bump("update readme"), BumpType::None);
        assert_eq!(determine_bump("fixed the thing"), BumpType::None);
        assert_eq!(determine_bump("WIP"), BumpType::None);
    }

    #[test]
    fn test_all_patch_types() {
        assert_eq!(determine_bump("fix: something"), BumpType::Patch);
        assert_eq!(determine_bump("perf: something"), BumpType::Patch);
        assert_eq!(determine_bump("refactor: something"), BumpType::Patch);
    }

    #[test]
    fn test_all_none_types() {
        assert_eq!(determine_bump("chore: something"), BumpType::None);
        assert_eq!(determine_bump("docs: something"), BumpType::None);
        assert_eq!(determine_bump("ci: something"), BumpType::None);
        assert_eq!(determine_bump("style: something"), BumpType::None);
        assert_eq!(determine_bump("test: something"), BumpType::None);
        assert_eq!(determine_bump("build: something"), BumpType::None);
    }

    #[test]
    fn test_breaking_all_types() {
        assert_eq!(determine_bump("fix!: breaking fix"), BumpType::Major);
        assert_eq!(determine_bump("refactor!: breaking"), BumpType::Major);
        assert_eq!(determine_bump("perf!: breaking"), BumpType::Major);
        assert_eq!(determine_bump("chore!: breaking"), BumpType::Major);
        assert_eq!(determine_bump("docs!: breaking"), BumpType::Major);
        assert_eq!(determine_bump("style!: breaking"), BumpType::Major);
        assert_eq!(determine_bump("test!: breaking"), BumpType::Major);
        assert_eq!(determine_bump("build!: breaking"), BumpType::Major);
        assert_eq!(determine_bump("ci!: breaking"), BumpType::Major);
    }

    #[test]
    fn test_breaking_with_scope() {
        assert_eq!(determine_bump("chore(deps)!: breaking"), BumpType::Major);
        assert_eq!(determine_bump("build(npm)!: breaking"), BumpType::Major);
    }

    #[test]
    fn test_breaking_change_in_body_multiline() {
        let msg = "feat: add feature\n\nSome description.\n\nBREAKING CHANGE: removed old API";
        assert_eq!(determine_bump(msg), BumpType::Major);
    }

    #[test]
    fn test_parse_subject_multiline() {
        assert_eq!(
            parse_subject("first line\nsecond line\nthird line"),
            "first line"
        );
    }

    #[test]
    fn test_parse_subject_empty() {
        assert_eq!(parse_subject(""), "");
    }

    #[test]
    fn test_bump_type_display() {
        assert_eq!(format!("{}", BumpType::None), "none");
        assert_eq!(format!("{}", BumpType::Patch), "patch");
        assert_eq!(format!("{}", BumpType::Minor), "minor");
        assert_eq!(format!("{}", BumpType::Major), "major");
    }

    #[test]
    fn test_feat_not_in_middle_of_word() {
        assert_eq!(determine_bump("featured something"), BumpType::None);
    }

    #[test]
    fn test_deep_nested_scope() {
        assert_eq!(
            determine_bump("feat(api/auth/jwt): add token"),
            BumpType::Minor
        );
        assert_eq!(
            determine_bump("fix(ui/modal): close on escape"),
            BumpType::Patch
        );
    }

    #[test]
    fn test_uppercase_types_not_matched() {
        assert_eq!(determine_bump("FEAT: add login"), BumpType::None);
        assert_eq!(determine_bump("FIX: bug"), BumpType::None);
        assert_eq!(determine_bump("Feat: add login"), BumpType::None);
    }

    #[test]
    fn test_missing_colon() {
        assert_eq!(determine_bump("feat add login"), BumpType::None);
        assert_eq!(determine_bump("fix something"), BumpType::None);
    }

    #[test]
    fn test_extra_space_after_type() {
        assert_eq!(determine_bump("feat : add login"), BumpType::None);
    }

    #[test]
    fn test_empty_scope() {
        assert_eq!(determine_bump("feat(): add login"), BumpType::None);
        assert_eq!(determine_bump("fix(): bug"), BumpType::None);
    }

    #[test]
    fn test_breaking_change_not_at_line_start() {
        let msg = "feat: something\n\nnot a BREAKING CHANGE here";
        assert_eq!(determine_bump(msg), BumpType::Minor);
    }

    #[test]
    fn test_parse_subject_crlf() {
        assert_eq!(parse_subject("feat: add\r\nbody text"), "feat: add");
    }

    #[test]
    fn test_parse_subject_only_newlines() {
        assert_eq!(parse_subject("\n\n\n"), "");
    }

    #[test]
    fn test_multiline_body_feat_in_body_does_not_match() {
        let msg = "chore: update deps\n\nfeat: this is in the body";
        assert_eq!(determine_bump(msg), BumpType::None);
    }

    #[test]
    fn test_multiline_body_fix_in_body_does_not_match() {
        let msg = "chore: update deps\n\nfix: this is in the body";
        assert_eq!(determine_bump(msg), BumpType::None);
    }

    #[test]
    fn test_multiline_body_breaking_marker_in_body_does_not_match() {
        let msg = "chore: update deps\n\nfeat!: this is in the body";
        assert_eq!(determine_bump(msg), BumpType::None);
    }

    #[test]
    fn test_parse_header_type_scope_desc() {
        let h = parse_header("feat(api): add events endpoint").unwrap();
        assert_eq!(h.commit_type, "feat");
        assert_eq!(h.scope, Some("api"));
        assert!(!h.breaking_bang);
        assert_eq!(h.description, "add events endpoint");
    }

    #[test]
    fn test_parse_header_breaking_bang() {
        let h = parse_header("feat!: drop flag").unwrap();
        assert_eq!(h.commit_type, "feat");
        assert_eq!(h.scope, None);
        assert!(h.breaking_bang);
        assert_eq!(h.description, "drop flag");
    }

    #[test]
    fn test_parse_header_scoped_bang() {
        let h = parse_header("fix(security)!: patch").unwrap();
        assert_eq!(h.commit_type, "fix");
        assert_eq!(h.scope, Some("security"));
        assert!(h.breaking_bang);
    }

    #[test]
    fn test_parse_header_non_conventional() {
        assert!(parse_header("just a message").is_none());
        assert!(parse_header("feat add no colon").is_none());
    }

    #[test]
    fn test_breaking_footer_body_extracts_description() {
        let msg = "feat: add x\n\nBREAKING CHANGE: the old endpoint is gone";
        assert_eq!(
            breaking_footer_body(msg).as_deref(),
            Some("the old endpoint is gone")
        );
    }

    #[test]
    fn test_breaking_footer_body_none_when_absent() {
        assert_eq!(breaking_footer_body("feat: add x"), None);
        assert_eq!(breaking_footer_body("feat!: add x"), None);
    }

    #[test]
    fn test_is_breaking() {
        assert!(is_breaking("feat!: x"));
        assert!(is_breaking("feat: x\n\nBREAKING CHANGE: y"));
        assert!(!is_breaking("feat: x"));
        assert!(!is_breaking("fix: y"));
    }
}