gitpoulpi-core 1.3.0

GitPoulpi domain model, GitBackend and trait. No I/O-framework dependencies.
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
//! Classifying branches against a workflow, and finding their parents.
//!
//! Order, in full:
//! 1. A branch whose name is a base's name (or one of its alternative names)
//!    is that **base**.
//! 2. Otherwise every topic pattern (`pattern`, then `patterns`, then `regex`,
//!    of each topic in declaration order) that matches is a candidate; the
//!    candidate fixing the most literal characters wins (`release/{version}`
//!    beats `{slug}`; a regex counts its literal prefix), ties go to the
//!    earlier declaration.
//! 3. Otherwise **unclassified**.

use regex::Regex;
use serde::{Deserialize, Serialize};

use super::pattern::{BranchPattern, Fields};
use super::{BaseBranch, TopicType, Workflow};

/// What a branch is.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Classification {
    /// A long-lived base; carries the configured (preferred) name.
    Base {
        name: String,
    },
    /// A topic branch of a type, with the fields its name carried.
    Topic {
        type_id: String,
        fields: Fields,
    },
    Unclassified,
}

/// Where a parent came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ParentSource {
    /// Recorded when GitPoulpi created the branch (`branch.<b>.gitpoulpi-parent`).
    Recorded,
    /// Read from Git Town's `git-town-branch.<b>.parent`.
    GitTown,
    /// The topic type's parent.
    Type,
    /// The base's parent.
    Base,
}

/// The branch a branch starts from and is finished into.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParentInfo {
    pub name: String,
    pub source: ParentSource,
}

enum Matcher {
    Pattern(BranchPattern),
    Regex(Regex, usize),
}

impl Matcher {
    fn specificity(&self) -> usize {
        match self {
            Matcher::Pattern(p) => p.specificity(),
            Matcher::Regex(_, s) => *s,
        }
    }

    fn fields(&self, name: &str) -> Option<Fields> {
        match self {
            Matcher::Pattern(p) => p.parse_name(name),
            Matcher::Regex(re, _) => {
                let caps = re.captures(name)?;
                Some(
                    re.capture_names()
                        .flatten()
                        .filter_map(|n| Some((n.to_string(), caps.name(n)?.as_str().to_string())))
                        .collect(),
                )
            }
        }
    }
}

/// Literal characters at the start of a regex (after `^`).
fn regex_literal_prefix(source: &str) -> usize {
    source
        .trim_start_matches('^')
        .chars()
        .take_while(|c| !"\\.[](){}*+?|$^".contains(*c))
        .count()
}

struct CompiledTopic {
    matchers: Vec<Matcher>,
    /// The rendering pattern (the first valid of `pattern`).
    render: Option<BranchPattern>,
}

/// A [`Workflow`] with its patterns parsed, ready to classify many branches.
pub struct CompiledWorkflow {
    workflow: Workflow,
    topics: Vec<CompiledTopic>,
    warnings: Vec<String>,
}

impl CompiledWorkflow {
    pub fn new(workflow: &Workflow) -> Self {
        let mut warnings = Vec::new();
        let mut topics = Vec::new();
        for t in &workflow.topics {
            let mut matchers = Vec::new();
            let mut render = None;
            if t.id.trim().is_empty() {
                warnings.push("a topic type has no `id`".to_string());
            }
            let sources = std::iter::once((true, t.pattern.as_str()))
                .chain(t.patterns.iter().map(|p| (false, p.as_str())))
                .filter(|(primary, p)| !(*primary && p.is_empty()));
            if t.pattern.is_empty() && t.patterns.is_empty() && t.regex.is_none() {
                warnings.push(format!("topic `{}` has no pattern or regex", t.id));
            }
            for (primary, source) in sources {
                match BranchPattern::parse(source) {
                    Ok(p) => {
                        if primary {
                            render = Some(p.clone());
                        }
                        matchers.push(Matcher::Pattern(p));
                    }
                    Err(e) => warnings.push(format!(
                        "topic `{}`: pattern `{source}` is invalid: {e}",
                        t.id
                    )),
                }
            }
            if let Some(src) = &t.regex {
                match Regex::new(&format!(
                    "^(?:{})$",
                    src.trim_start_matches('^').trim_end_matches('$')
                )) {
                    Ok(re) => matchers.push(Matcher::Regex(re, regex_literal_prefix(src))),
                    Err(e) => {
                        warnings.push(format!("topic `{}`: regex `{src}` is invalid: {e}", t.id))
                    }
                }
            }
            topics.push(CompiledTopic { matchers, render });
        }
        for b in &workflow.bases {
            if b.name.trim().is_empty() {
                warnings.push("a base branch has no `name`".to_string());
            }
            if let Some(p) = &b.parent {
                if !workflow.bases.iter().any(|o| o.is_named(p)) {
                    warnings.push(format!(
                        "base `{}`: parent `{p}` is not a defined base",
                        b.name
                    ));
                }
            }
        }
        for t in &workflow.topics {
            if let Some(p) = &t.parent {
                if !workflow.bases.iter().any(|o| o.is_named(p)) {
                    warnings.push(format!(
                        "topic `{}`: parent `{p}` is not a defined base",
                        t.id
                    ));
                }
            }
        }
        Self {
            workflow: workflow.clone(),
            topics,
            warnings,
        }
    }

    pub fn workflow(&self) -> &Workflow {
        &self.workflow
    }

    /// Problems found while compiling (bad patterns, unknown parents).
    pub fn warnings(&self) -> &[String] {
        &self.warnings
    }

    /// The base a branch name denotes, by any of its names.
    pub fn base_named(&self, name: &str) -> Option<&BaseBranch> {
        self.workflow.bases.iter().find(|b| b.is_named(name))
    }

    pub fn topic(&self, id: &str) -> Option<&TopicType> {
        self.workflow.topics.iter().find(|t| t.id == id)
    }

    /// The pattern that renders names of topic `id`.
    pub fn render_pattern(&self, id: &str) -> Option<&BranchPattern> {
        let idx = self.workflow.topics.iter().position(|t| t.id == id)?;
        self.topics.get(idx)?.render.as_ref()
    }

    /// Classifies a local branch name (for a remote branch, pass the name
    /// without the remote).
    pub fn classify(&self, branch: &str) -> Classification {
        if let Some(base) = self.base_named(branch) {
            return Classification::Base {
                name: base.name.clone(),
            };
        }
        let mut best: Option<(usize, usize, Fields)> = None;
        for (idx, topic) in self.topics.iter().enumerate() {
            for m in &topic.matchers {
                let Some(fields) = m.fields(branch) else {
                    continue;
                };
                let spec = m.specificity();
                // Strictly greater: an earlier declaration wins a tie.
                if best.as_ref().is_none_or(|(s, _, _)| spec > *s) {
                    best = Some((spec, idx, fields));
                }
            }
        }
        match best {
            Some((_, idx, fields)) => Classification::Topic {
                type_id: self.workflow.topics[idx].id.clone(),
                fields,
            },
            None => Classification::Unclassified,
        }
    }

    /// The existing branch a base reference (`develop`, or any of its names)
    /// stands for: the first of the base's names `exists` accepts.
    pub fn concrete_base(&self, reference: &str, exists: &dyn Fn(&str) -> bool) -> Option<String> {
        let base = self.base_named(reference)?;
        base.all_names().find(|n| exists(n)).map(str::to_string)
    }

    /// Where `class` starts from: the recorded parent if that branch still
    /// exists, else the type's (or base's) parent as an existing branch.
    pub fn resolve_parent(
        &self,
        class: &Classification,
        recorded: Option<(&str, ParentSource)>,
        exists: &dyn Fn(&str) -> bool,
    ) -> Option<ParentInfo> {
        if let Some((name, source)) = recorded.filter(|(n, _)| exists(n)) {
            return Some(ParentInfo {
                name: name.to_string(),
                source,
            });
        }
        let (reference, source) = match class {
            Classification::Base { name } => (
                self.base_named(name)?.parent.as_deref()?,
                ParentSource::Base,
            ),
            Classification::Topic { type_id, .. } => {
                (self.topic(type_id)?.parent.as_deref()?, ParentSource::Type)
            }
            Classification::Unclassified => return None,
        };
        let name = self.concrete_base(reference, exists)?;
        Some(ParentInfo { name, source })
    }
}

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

    fn topic(id: &str, pattern: &str, parent: &str) -> TopicType {
        TopicType {
            id: id.into(),
            pattern: pattern.into(),
            parent: Some(parent.into()),
            ..Default::default()
        }
    }

    fn base(name: &str) -> BaseBranch {
        BaseBranch {
            name: name.into(),
            ..Default::default()
        }
    }

    fn type_of(w: &Workflow, b: &str) -> String {
        match w.classify(b) {
            Classification::Base { name } => format!("base:{name}"),
            Classification::Topic { type_id, .. } => type_id,
            Classification::Unclassified => "-".into(),
        }
    }

    #[test]
    fn bases_win_over_topic_patterns() {
        let w = Workflow {
            bases: vec![base("main")],
            topics: vec![topic("any", "{slug}", "main")],
            ..Default::default()
        };
        assert_eq!(type_of(&w, "main"), "base:main");
        assert_eq!(type_of(&w, "other"), "any");
    }

    #[test]
    fn alias_names_classify_as_the_configured_base() {
        let w = Workflow {
            bases: vec![BaseBranch {
                name: "main".into(),
                names: vec!["master".into()],
                ..Default::default()
            }],
            ..Default::default()
        };
        assert_eq!(type_of(&w, "master"), "base:main");
    }

    #[test]
    fn the_most_specific_pattern_wins_whatever_the_order() {
        let w = Workflow {
            bases: vec![base("main")],
            topics: vec![
                topic("any", "{slug}", "main"),
                topic("user", "{user}/{slug}", "main"),
                topic("feature", "feature/{slug}", "main"),
                topic("release", "release/{version}", "main"),
            ],
            ..Default::default()
        };
        assert_eq!(type_of(&w, "feature/x"), "feature");
        assert_eq!(type_of(&w, "release/1.0"), "release");
        // not a version: the release pattern does not match, `{user}/{slug}` does
        assert_eq!(type_of(&w, "release/next"), "user");
        assert_eq!(type_of(&w, "ana/x"), "user");
        assert_eq!(type_of(&w, "plain"), "any");
    }

    #[test]
    fn a_tie_goes_to_the_earlier_declaration() {
        let w = Workflow {
            topics: vec![topic("a", "x/{slug}", "m"), topic("b", "x/{issue}", "m")],
            ..Default::default()
        };
        assert_eq!(type_of(&w, "x/12"), "a");
        let w = Workflow {
            topics: vec![topic("b", "x/{issue}", "m"), topic("a", "x/{slug}", "m")],
            ..Default::default()
        };
        assert_eq!(type_of(&w, "x/12"), "b");
        assert_eq!(type_of(&w, "x/ab"), "a");
    }

    #[test]
    fn regex_topics_match_and_extract_named_groups() {
        let w = Workflow {
            topics: vec![TopicType {
                id: "feat".into(),
                pattern: "feat/{slug}".into(),
                regex: Some("^feat-(?P<slug>.+)$".into()),
                ..Default::default()
            }],
            ..Default::default()
        };
        match w.classify("feat-login") {
            Classification::Topic { type_id, fields } => {
                assert_eq!(type_id, "feat");
                assert_eq!(fields["slug"], "login");
            }
            other => panic!("{other:?}"),
        }
        assert_eq!(type_of(&w, "xfeat-login"), "-", "regex is anchored");
        assert_eq!(type_of(&w, "feat/ok"), "feat");
    }

    #[test]
    fn a_regex_only_topic_has_no_render_pattern() {
        let w = Workflow {
            topics: vec![TopicType {
                id: "bot".into(),
                regex: Some("bot/.*".into()),
                ..Default::default()
            }],
            ..Default::default()
        };
        let c = w.compile();
        assert!(c.render_pattern("bot").is_none());
        assert!(c.warnings().is_empty(), "{:?}", c.warnings());
        assert_eq!(type_of(&w, "bot/1"), "bot");
    }

    #[test]
    fn broken_definitions_warn_and_are_skipped() {
        let w = Workflow {
            bases: vec![BaseBranch {
                name: "d".into(),
                parent: Some("ghost".into()),
                ..Default::default()
            }],
            topics: vec![
                topic("bad", "x/{nope}", "d"),
                TopicType {
                    id: "re".into(),
                    pattern: "re/{slug}".into(),
                    regex: Some("(".into()),
                    parent: Some("zzz".into()),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        let c = w.compile();
        let all = c.warnings().join("\n");
        assert!(all.contains("x/{nope}"), "{all}");
        assert!(all.contains("regex `(`"), "{all}");
        assert!(all.contains("parent `ghost`"), "{all}");
        assert!(all.contains("parent `zzz`"), "{all}");
        assert_eq!(c.classify("re/a"), w.classify("re/a"));
        assert_eq!(type_of(&w, "re/a"), "re", "the valid pattern still works");
        assert_eq!(type_of(&w, "x/y"), "-");
    }

    #[test]
    fn parent_resolution_prefers_recorded_then_type() {
        let w = WorkflowTemplate::GitFlow.workflow();
        let c = w.compile();
        let existing = ["master", "dev", "feature/a", "other"];
        let exists = |n: &str| existing.contains(&n);
        let class = c.classify("feature/a");
        // type parent: develop resolves to the existing alias `dev`
        assert_eq!(
            c.resolve_parent(&class, None, &exists),
            Some(ParentInfo {
                name: "dev".into(),
                source: ParentSource::Type
            })
        );
        // recorded wins when it exists
        assert_eq!(
            c.resolve_parent(&class, Some(("other", ParentSource::Recorded)), &exists),
            Some(ParentInfo {
                name: "other".into(),
                source: ParentSource::Recorded
            })
        );
        // a recorded parent that is gone falls back to the type's
        assert_eq!(
            c.resolve_parent(&class, Some(("gone", ParentSource::Recorded)), &exists)
                .unwrap()
                .source,
            ParentSource::Type
        );
        // a base's parent
        let dev = c.classify("dev");
        assert_eq!(
            c.resolve_parent(&dev, None, &exists),
            Some(ParentInfo {
                name: "master".into(),
                source: ParentSource::Base
            })
        );
        // trunk and unclassified have none
        assert_eq!(c.resolve_parent(&c.classify("master"), None, &exists), None);
        assert_eq!(
            c.resolve_parent(&Classification::Unclassified, None, &exists),
            None
        );
        // type parent that does not exist at all
        assert_eq!(c.resolve_parent(&class, None, &|n| n == "feature/a"), None);
    }
}