git-z 0.2.4

A Git extension to go beyond.
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
// git-z - A Git extension to go beyond.
// Copyright (C) 2023-2024 Jean-Philippe Cugnet <jean-philippe@cugnet.eu>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Configuration updater from version 0.1.

#![expect(
    clippy::expect_used,
    clippy::missing_panics_doc,
    reason = "Updaters make a heavy usage of `expect` instead of proper error \
        handling. This is because `ConfigUpdater::load` already validates the \
        configuration by parsing it to a `Config`. Any error occurring here is \
        a bug, hence should lead to a panic."
)]

use indoc::indoc;
use regex::Regex;
use toml_edit::{DocumentMut, Item, Table};

use super::{super::split_type_and_doc, AskForTicket, common};

/// The old configuration for `types`.
const OLD_TYPES_DOC: &str = indoc! {"

    # The available types of commits.
    #
    # This is a list of types (1 word) and their description, separated by one or
    # more spaces.
"};

/// The old configuration for `scopes`.
const OLD_SCOPES_DOC: &str = indoc! {"

    # The list of valid scopes.
"};

/// The old documentation for `ticket.prefixes`.
pub const OLD_TICKET_PREFIXES_DOC: &str = indoc! {"
    # The list of valid ticket prefixes.
"};

/// The old documentation for `templates.commit`.
pub const OLD_TEMPLATES_COMMIT_DOC: &str = indoc! {"
    # The commit message template, written with the Tera [1] templating engine.
    # [1] https://tera.netlify.app/
"};

/// Updates the configuration from version 0.1.
pub fn update(
    toml_config: &mut DocumentMut,
    switch_scopes_to_any: bool,
    ask_for_ticket: AskForTicket,
    empty_prefix_to_hash: bool,
) {
    common::update_version(toml_config);
    update_types(toml_config);
    update_scopes(toml_config, switch_scopes_to_any);

    match ask_for_ticket {
        AskForTicket::Ask { require } => {
            update_ticket(toml_config, require, empty_prefix_to_hash);
        }
        AskForTicket::DontAsk => remove_ticket(toml_config),
    }

    update_templates(toml_config, empty_prefix_to_hash);
}

/// Updates the configuration for the types.
fn update_types(toml_config: &mut DocumentMut) {
    let (key, value) =
        toml_config.get_key_value("types").expect("No `types` key");

    let doc = key
        .leaf_decor()
        .prefix()
        .expect("No prefix decorator for key `types`")
        .as_str()
        .expect("Improper string in the prefix decorator of the `types` key");

    // Update the configuration format.
    let mut types: Table = value
        .as_array()
        .expect("The `types` key is not an array")
        .iter()
        .map(|ty| {
            ty.as_str()
                .expect("Values of the `types` array are not strings")
        })
        .map(split_type_and_doc)
        .collect();

    // Update the documentation.
    types
        .decor_mut()
        .set_prefix(doc.replace(OLD_TYPES_DOC, common::TYPES_DOC));

    // Replace the old configuration with the new one.
    toml_config.insert("types", Item::Table(types));
}

/// Updates the configuration for scopes.
fn update_scopes(toml_config: &mut DocumentMut, switch_scopes_to_any: bool) {
    let (key, value) = toml_config
        .get_key_value("scopes")
        .expect("No `scopes` key");

    let doc = key
        .leaf_decor()
        .prefix()
        .expect("No prefix decorator for key `scopes`")
        .as_str()
        .expect("Improper string in the prefix decorator of the `scopes` key");

    // Update the configuration format.
    let mut scopes = Table::new();

    if switch_scopes_to_any {
        scopes.insert("accept", Item::Value("any".into()));
    } else {
        scopes.insert("accept", Item::Value("list".into()));
        scopes.insert("list", value.clone());
    }

    // Update the documentation.
    scopes
        .decor_mut()
        .set_prefix(doc.replace(OLD_SCOPES_DOC, common::SCOPES_DOC));
    scopes
        .key_mut("accept")
        .expect("No `scopes.accept` key")
        .leaf_decor_mut()
        .set_prefix(common::SCOPES_ACCEPT_DOC);

    // Replace the old configuration with the new one.
    toml_config.insert("scopes", Item::Table(scopes));
}

/// Updates the configuration for ticket references.
fn update_ticket(
    toml_config: &mut DocumentMut,
    required: bool,
    empty_prefix_to_hash: bool,
) {
    let (key, value) = toml_config
        .get_key_value("ticket_prefixes")
        .expect("No `ticket_prefixes` key");

    let doc = key
        .leaf_decor()
        .prefix()
        .expect("No prefix decorator for key `ticket_prefixes`")
        .as_str()
        .expect("Improper string in the prefix decorator of the `ticket_prefixes` key");

    // Update the value itself.
    let mut prefixes = value.clone();
    if empty_prefix_to_hash {
        replace_empty_prefix_with_hash(&mut prefixes);
    }

    // Update the configuration format.
    let mut ticket = Table::new();
    ticket.insert("required", Item::Value(required.into()));
    ticket.insert("prefixes", prefixes);

    // Update the documentation.
    ticket.decor_mut().set_prefix(common::TICKET_DOC);
    ticket
        .key_mut("required")
        .expect("No `ticket.required` key")
        .leaf_decor_mut()
        .set_prefix(common::TICKET_REQUIRED_DOC);
    ticket
        .key_mut("prefixes")
        .expect("No `ticket.prefixes` key")
        .leaf_decor_mut()
        .set_prefix(
            doc.trim_start()
                .replace(OLD_TICKET_PREFIXES_DOC, common::TICKET_PREFIXES_DOC),
        );

    // Replace the old configuration with the new one.
    toml_config.remove("ticket_prefixes");
    toml_config.insert("ticket", Item::Table(ticket));
}

/// Replaces an empty ticket prefix by `#`.
fn replace_empty_prefix_with_hash(prefixes: &mut Item) {
    let empty_prefix = prefixes
        .as_array_mut()
        .expect("The `ticket.prefixes` key is not an array")
        .iter_mut()
        .find(|item| {
            item.as_str()
                .expect("Items in `ticket.prefixes are not strings")
                .is_empty()
        });

    if let Some(value) = empty_prefix {
        *value = "#".into();
    }
}

/// Removes the configuration for ticket references.
fn remove_ticket(toml_config: &mut DocumentMut) {
    toml_config.remove("ticket_prefixes");
}

/// Updates the configuration for templates.
fn update_templates(toml_config: &mut DocumentMut, remove_hash_prefix: bool) {
    let (key, value) = toml_config
        .get_key_value("template")
        .expect("No `template` key");

    let doc = key
        .leaf_decor()
        .prefix()
        .expect("No prefix decorator for key `template`")
        .as_str()
        .expect(
            "Improper string in the prefix decorator of the `template` key",
        );

    // Update the template itself.
    let template = value.as_str().expect("The `template` key is not a string");
    let template = add_ticket_condition_to_commit_template(template);
    let template = if remove_hash_prefix {
        remove_hash_ticket_prefix_from_commit_template(&template)
    } else {
        template
    };

    // Update the configuration format.
    let mut templates = Table::new();
    templates.insert("commit", Item::Value(template.into()));

    // Update the documentation.
    templates.decor_mut().set_prefix(common::TEMPLATES_DOC);
    templates
        .key_mut("commit")
        .expect("No `commit` key")
        .leaf_decor_mut()
        .set_prefix(
            doc.trim_start().replace(
                OLD_TEMPLATES_COMMIT_DOC,
                common::TEMPLATES_COMMIT_DOC,
            ),
        );

    // Replace the old configuration with the new one.
    toml_config.remove("template");
    toml_config.insert("templates", Item::Table(templates));
}

/// Adds a condition around the usage of the `ticket` variable.
fn add_ticket_condition_to_commit_template(template: &str) -> String {
    #[expect(clippy::unwrap_used, reason = "This regex is known to be valid.")]
    let re = Regex::new(r"(.*\{\{ ticket \}\}.*)").unwrap();
    re.replace(template, "{% if ticket %}$1{% endif %}")
        .to_string()
}

/// Removes the `#` prefix before the `ticket` variable.
fn remove_hash_ticket_prefix_from_commit_template(template: &str) -> String {
    template.replace("#{{ ticket }}", "{{ ticket }}")
}

#[cfg(test)]
mod tests {
    #![allow(clippy::pedantic, clippy::restriction)]

    use super::*;

    const V0_1_STANDARD: &str =
        include_str!("../../../tests/res/config/v0_1_standard.toml");

    const V0_1_USER_COMMENTS: &str =
        include_str!("../../../tests/res/config/v0_1_user-comments.toml");

    const V0_1_DOC_AND_USER_COMMENTS: &str = include_str!(
        "../../../tests/res/config/v0_1_doc-and-user-comments.toml"
    );

    const V0_2_STANDARD: &str =
        include_str!("../../../tests/res/config/v0_2_standard.toml");

    const V0_2_SCOPES_ANY: &str =
        include_str!("../../../tests/res/config/v0_2_scopes-any.toml");

    const V0_2_TICKET_NOT_REQUIRED: &str =
        include_str!("../../../tests/res/config/v0_2_ticket-not-required.toml");

    const V0_2_TICKET_NOT_ASKED_FOR: &str = include_str!(
        "../../../tests/res/config/v0_2_ticket-not-asked-for.toml"
    );

    const V0_2_KEEP_EMPTY_PREFIX: &str =
        include_str!("../../../tests/res/config/v0_2_keep-empty-prefix.toml");

    const V0_2_USER_COMMENTS: &str =
        include_str!("../../../tests/res/config/v0_2_user-comments.toml");

    const V0_2_DOC_AND_USER_COMMENTS: &str = include_str!(
        "../../../tests/res/config/v0_2_doc-and-user-comments.toml"
    );

    #[test]
    fn update_works_with_standard_config() {
        let source = V0_1_STANDARD;
        let expected = V0_2_STANDARD;

        let mut document = source.parse().unwrap();
        update(
            &mut document,
            false,
            AskForTicket::Ask { require: true },
            true,
        );

        let actual = document.to_string();
        assert_eq!(actual, expected);
    }

    #[test]
    fn update_can_switch_scopes_to_any() {
        let source = V0_1_STANDARD;
        let expected = V0_2_SCOPES_ANY;

        let mut document = source.parse().unwrap();
        update(
            &mut document,
            true,
            AskForTicket::Ask { require: true },
            true,
        );

        let actual = document.to_string();
        assert_eq!(actual, expected);
    }

    #[test]
    fn update_can_ask_for_ticket_without_requiring_it() {
        let source = V0_1_STANDARD;
        let expected = V0_2_TICKET_NOT_REQUIRED;

        let mut document = source.parse().unwrap();
        update(
            &mut document,
            false,
            AskForTicket::Ask { require: false },
            true,
        );

        let actual = document.to_string();
        assert_eq!(actual, expected);
    }

    #[test]
    fn update_can_omit_to_ask_for_a_ticket() {
        let source = V0_1_STANDARD;
        let expected = V0_2_TICKET_NOT_ASKED_FOR;

        let mut document = source.parse().unwrap();
        update(&mut document, false, AskForTicket::DontAsk, true);

        let actual = document.to_string();
        assert_eq!(actual, expected);
    }

    #[test]
    fn update_can_skip_updating_an_empty_ticket_prefix_to_hash() {
        let source = V0_1_STANDARD;
        let expected = V0_2_KEEP_EMPTY_PREFIX;

        let mut document = source.parse().unwrap();
        update(
            &mut document,
            false,
            AskForTicket::Ask { require: true },
            false,
        );

        let actual = document.to_string();
        assert_eq!(actual, expected);
    }

    #[test]
    fn update_preserves_user_comments() {
        let source = V0_1_USER_COMMENTS;
        let expected = V0_2_USER_COMMENTS;

        let mut document = source.parse().unwrap();
        update(
            &mut document,
            false,
            AskForTicket::Ask { require: true },
            true,
        );

        let actual = document.to_string();
        assert_eq!(actual, expected);
    }

    #[test]
    fn update_updates_default_doc_when_mixed_with_user_comments() {
        let source = V0_1_DOC_AND_USER_COMMENTS;
        let expected = V0_2_DOC_AND_USER_COMMENTS;

        let mut document = source.parse().unwrap();
        update(
            &mut document,
            false,
            AskForTicket::Ask { require: true },
            true,
        );

        let actual = document.to_string();
        assert_eq!(actual, expected);
    }

    #[test]
    fn add_ticket_condition_makes_reference_conditional_on_refs_footer() {
        let source = indoc! {"
            {{ type }}{% if scope %}({{ scope }}){% endif %}{% if breaking_change %}!{% endif %}: {{ description }}

            # Feel free to enter a longer description here.

            Refs: {{ ticket }}

            {% if breaking_change %}BREAKING CHANGE: {{ breaking_change }}{% endif %}
        "};

        let expected = indoc! {"
            {{ type }}{% if scope %}({{ scope }}){% endif %}{% if breaking_change %}!{% endif %}: {{ description }}

            # Feel free to enter a longer description here.

            {% if ticket %}Refs: {{ ticket }}{% endif %}

            {% if breaking_change %}BREAKING CHANGE: {{ breaking_change }}{% endif %}
        "};

        let actual = add_ticket_condition_to_commit_template(source);

        assert_eq!(actual, expected);
    }
}