mit-commit-message-lints 6.4.2

Check the correctness of a specific commit message. Designed to be used in tools providing commit-msg style hooks
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
use miette::Result;
use rand::seq::SliceRandom;

use crate::external::Vcs;
use crate::mit::cmd::set_commit_authors::{remove_coauthors, set_vcs_coauthor, set_vcs_user};
use crate::mit::{Author, cmd::vcs::get_vcs_coauthors_config};

/// Rotate the primary author among configured authors
///
/// Moves the first coauthor to become the primary author,
/// and demotes the current primary to be the last coauthor.
/// This affects the NEXT commit (git reads user.name/email
/// before the prepare-commit-msg hook runs).
///
/// # Errors
///
/// Returns an error if:
/// - Reading git config (user.name, user.email, or coauthors) fails
/// - Writing git config (removing old coauthors or setting new authors) fails
///
/// # Panics
///
/// This function will panic if the primary author (constructed from user.name
/// and user.email) is None. However, this is prevented by an early return check:
/// if either user.name or user.email is missing, the function returns Ok(())
/// without attempting to unwrap. The unwrap on line 47 is safe because at that
/// point we've confirmed both name and email exist.
pub fn rotate_authors(config: &mut dyn Vcs, strategy: crate::mit::RotationOption) -> Result<()> {
    // Read the current primary author
    let primary_name = config.get_str("user.name")?.map(String::from);
    let primary_email = config.get_str("user.email")?.map(String::from);
    let primary_signingkey = config.get_str("user.signingkey")?.map(String::from);

    let primary = match (primary_name, primary_email, primary_signingkey) {
        (Some(name), Some(email), signingkey) => Some(Author::new(
            name.into(),
            email.into(),
            signingkey.map(Into::into),
        )),
        _ => return Ok(()), // No primary author, nothing to rotate
    };

    // Read coauthors
    let coauthor_emails: Vec<String> = get_vcs_coauthors_config(config, "email")?
        .into_iter()
        .filter_map(|x| x.map(|s| s.to_string()))
        .collect();

    let coauthors: Vec<Author> = get_vcs_coauthors_config(config, "name")?
        .into_iter()
        .filter_map(|x| x.map(|s| s.to_string()))
        .zip(coauthor_emails)
        .filter_map(|(name, email)| {
            if name.is_empty() || email.is_empty() {
                None
            } else {
                Some(Author::new(name.into(), email.into(), None))
            }
        })
        .collect();

    // Build full author list
    let mut all_authors: Vec<Author> = vec![primary.unwrap()];
    all_authors.extend(coauthors);

    // If only 0 or 1 author, nothing to rotate
    if all_authors.len() <= 1 {
        return Ok(());
    }

    // Apply the rotation strategy
    match strategy {
        crate::mit::RotationOption::Off => return Ok(()),
        crate::mit::RotationOption::RoundRobin => {
            all_authors.rotate_left(1);
        }
        crate::mit::RotationOption::Random => {
            all_authors.shuffle(&mut rand::rng());
        }
    }

    // Write back
    remove_coauthors(config)?;
    set_vcs_user(config, &all_authors[0])?;
    all_authors[1..]
        .iter()
        .enumerate()
        .try_for_each(|(index, author)| set_vcs_coauthor(config, index, author))?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::time::Duration;

    use miette::Result;

    use crate::external::InMemory;
    use crate::mit::{Author, set_commit_authors};

    #[test]
    fn rotate_authors_rotates_three_authors() -> Result<()> {
        let mut buffer = BTreeMap::new();
        {
            let mut vcs_config = InMemory::new(&mut buffer);

            let author_1 = Author::new("Billie Thompson".into(), "billie@example.com".into(), None);
            let author_2 = Author::new("Somebody Else".into(), "someone@example.com".into(), None);
            let author_3 = Author::new("Annie Example".into(), "annie@example.com".into(), None);

            set_commit_authors(
                &mut vcs_config,
                &[&author_1, &author_2, &author_3],
                Duration::from_hours(1),
            )?;
        }

        // Initial state: A is primary, B & C are coauthors
        assert_eq!(
            buffer.get("user.name").map(String::as_str),
            Some("Billie Thompson"),
            "Expected the initial primary author to be Billie Thompson"
        );
        assert_eq!(
            buffer
                .get("mit.author.coauthors.0.name")
                .map(String::as_str),
            Some("Somebody Else"),
            "Expected the first coauthor to be Somebody Else before rotation"
        );
        assert_eq!(
            buffer
                .get("mit.author.coauthors.1.name")
                .map(String::as_str),
            Some("Annie Example"),
            "Expected the second coauthor to be Annie Example before rotation"
        );

        // Rotate: B becomes primary, C & A are coauthors
        {
            let mut vcs_config = InMemory::new(&mut buffer);
            crate::mit::cmd::rotate_authors::rotate_authors(
                &mut vcs_config,
                crate::mit::RotationOption::RoundRobin,
            )?;
        }

        assert_eq!(
            buffer.get("user.name").map(String::as_str),
            Some("Somebody Else"),
            "Expected the primary author to be Somebody Else after rotation"
        );
        assert_eq!(
            buffer
                .get("mit.author.coauthors.0.name")
                .map(String::as_str),
            Some("Annie Example"),
            "Expected the first coauthor to be Annie Example after rotation"
        );
        assert_eq!(
            buffer
                .get("mit.author.coauthors.1.name")
                .map(String::as_str),
            Some("Billie Thompson"),
            "Expected the second coauthor to be Billie Thompson after rotation"
        );

        Ok(())
    }

    #[test]
    fn rotate_authors_noops_with_single_author() -> Result<()> {
        let mut buffer = BTreeMap::new();
        {
            let mut vcs_config = InMemory::new(&mut buffer);
            let author = Author::new("Billie Thompson".into(), "billie@example.com".into(), None);
            set_commit_authors(&mut vcs_config, &[&author], Duration::from_hours(1))?;
        }

        {
            let mut vcs_config = InMemory::new(&mut buffer);
            crate::mit::cmd::rotate_authors::rotate_authors(
                &mut vcs_config,
                crate::mit::RotationOption::RoundRobin,
            )?;
        }

        // Should be unchanged
        assert_eq!(
            buffer.get("user.name").map(String::as_str),
            Some("Billie Thompson"),
            "Expected user.name to be unchanged with a single author"
        );
        assert_eq!(
            buffer.get("user.email").map(String::as_str),
            Some("billie@example.com"),
            "Expected user.email to be unchanged with a single author"
        );
        assert!(
            !buffer.contains_key("mit.author.coauthors.0.name"),
            "Expected no coauthors to be set with a single author"
        );

        Ok(())
    }

    #[test]
    fn rotate_authors_noops_with_zero_authors() -> Result<()> {
        let mut buffer = BTreeMap::new();

        {
            let mut vcs_config = InMemory::new(&mut buffer);
            crate::mit::cmd::rotate_authors::rotate_authors(
                &mut vcs_config,
                crate::mit::RotationOption::RoundRobin,
            )?;
        }

        // Buffer should be unchanged
        assert!(
            !buffer.contains_key("user.name"),
            "Expected no user.name to be set when there are no authors"
        );

        Ok(())
    }

    #[test]
    fn rotate_authors_rotates_two_authors() -> Result<()> {
        let mut buffer = BTreeMap::new();
        {
            let mut vcs_config = InMemory::new(&mut buffer);

            let author_1 = Author::new("Billie Thompson".into(), "billie@example.com".into(), None);
            let author_2 = Author::new("Somebody Else".into(), "someone@example.com".into(), None);

            set_commit_authors(
                &mut vcs_config,
                &[&author_1, &author_2],
                Duration::from_hours(1),
            )?;
        }

        // Initial: A is primary, B is coauthor
        assert_eq!(
            buffer.get("user.name").map(String::as_str),
            Some("Billie Thompson"),
            "Expected the initial primary author to be Billie Thompson"
        );
        assert_eq!(
            buffer
                .get("mit.author.coauthors.0.name")
                .map(String::as_str),
            Some("Somebody Else"),
            "Expected the first coauthor to be Somebody Else before rotation"
        );

        // Rotate: B becomes primary, A becomes coauthor
        {
            let mut vcs_config = InMemory::new(&mut buffer);
            crate::mit::cmd::rotate_authors::rotate_authors(
                &mut vcs_config,
                crate::mit::RotationOption::RoundRobin,
            )?;
        }

        assert_eq!(
            buffer.get("user.name").map(String::as_str),
            Some("Somebody Else"),
            "Expected the primary author to be Somebody Else after first rotation"
        );
        assert_eq!(
            buffer
                .get("mit.author.coauthors.0.name")
                .map(String::as_str),
            Some("Billie Thompson"),
            "Expected the first coauthor to be Billie Thompson after first rotation"
        );

        // Rotate again: back to A primary, B coauthor
        {
            let mut vcs_config = InMemory::new(&mut buffer);
            crate::mit::cmd::rotate_authors::rotate_authors(
                &mut vcs_config,
                crate::mit::RotationOption::RoundRobin,
            )?;
        }

        assert_eq!(
            buffer.get("user.name").map(String::as_str),
            Some("Billie Thompson"),
            "Expected the primary author to be Billie Thompson after second rotation"
        );
        assert_eq!(
            buffer
                .get("mit.author.coauthors.0.name")
                .map(String::as_str),
            Some("Somebody Else"),
            "Expected the first coauthor to be Somebody Else after second rotation"
        );

        Ok(())
    }

    #[test]
    fn rotate_authors_random_produces_valid_permutation() -> Result<()> {
        let mut buffer = BTreeMap::new();
        {
            let mut vcs_config = InMemory::new(&mut buffer);

            let author_1 = Author::new("Billie Thompson".into(), "billie@example.com".into(), None);
            let author_2 = Author::new("Somebody Else".into(), "someone@example.com".into(), None);
            let author_3 = Author::new("Annie Example".into(), "annie@example.com".into(), None);

            set_commit_authors(
                &mut vcs_config,
                &[&author_1, &author_2, &author_3],
                Duration::from_hours(1),
            )?;
        }

        // Collect original author set (sorted)
        let mut original: Vec<String> = vec![buffer.get("user.name").cloned().unwrap_or_default()];
        for i in 0..3 {
            if let Some(name) = buffer.get(&format!("mit.author.coauthors.{i}.name")) {
                original.push(name.clone());
            }
        }
        original.sort();

        // Rotate randomly
        {
            let mut vcs_config = InMemory::new(&mut buffer);
            crate::mit::cmd::rotate_authors::rotate_authors(
                &mut vcs_config,
                crate::mit::RotationOption::Random,
            )?;
        }

        // Collect result author set (sorted)
        let mut result: Vec<String> = vec![buffer.get("user.name").cloned().unwrap_or_default()];
        for i in 0..3 {
            if let Some(name) = buffer.get(&format!("mit.author.coauthors.{i}.name")) {
                result.push(name.clone());
            }
        }
        result.sort();

        // The multiset of authors must be preserved (it's a permutation)
        assert_eq!(
            result, original,
            "Expected random rotation to produce a valid permutation of the original authors"
        );

        Ok(())
    }

    #[test]
    fn rotate_authors_random_noops_with_single_author() -> Result<()> {
        let mut buffer = BTreeMap::new();
        {
            let mut vcs_config = InMemory::new(&mut buffer);
            let author = Author::new("Billie Thompson".into(), "billie@example.com".into(), None);
            set_commit_authors(&mut vcs_config, &[&author], Duration::from_hours(1))?;
        }

        {
            let mut vcs_config = InMemory::new(&mut buffer);
            crate::mit::cmd::rotate_authors::rotate_authors(
                &mut vcs_config,
                crate::mit::RotationOption::Random,
            )?;
        }

        assert_eq!(
            buffer.get("user.name").map(String::as_str),
            Some("Billie Thompson"),
            "Expected user.name to be unchanged with a single author under random rotation"
        );
        assert_eq!(
            buffer.get("user.email").map(String::as_str),
            Some("billie@example.com"),
            "Expected user.email to be unchanged with a single author under random rotation"
        );

        Ok(())
    }

    #[test]
    fn rotate_authors_ignores_coauthor_with_empty_name() -> Result<()> {
        let mut buffer = BTreeMap::new();
        buffer.insert("user.name".into(), "Billie Thompson".into());
        buffer.insert("user.email".into(), "billie@example.com".into());
        // Coauthor with empty name but non-empty email — should be filtered out
        buffer.insert("mit.author.coauthors.0.name".into(), String::new());
        buffer.insert(
            "mit.author.coauthors.0.email".into(),
            "ghost@example.com".into(),
        );

        {
            let mut vcs_config = InMemory::new(&mut buffer);
            crate::mit::cmd::rotate_authors::rotate_authors(
                &mut vcs_config,
                crate::mit::RotationOption::RoundRobin,
            )?;
        }

        // With the empty-name coauthor filtered, only the primary remains,
        // so rotation should be a no-op.
        assert_eq!(
            buffer.get("user.name").map(String::as_str),
            Some("Billie Thompson"),
            "Expected user.name to be unchanged when the only coauthor has an empty name"
        );

        Ok(())
    }
}