cucco 4.1.0

An interactive CLI for creating conventional commits, with git hooks support.
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
//! Layered configuration loaded from defaults, config files, and CLI overrides.

use std::env::current_dir;
use std::fmt;
use std::path::PathBuf;

use anyhow::Result;
use config::FileFormat;
use dirs::config_dir;
use indexmap::IndexMap;
use serde::Deserialize;
#[cfg(any(unix, target_os = "redox"))]
use xdg::BaseDirectories;

/// Resolved cucco configuration.
#[derive(Debug, Clone)]
pub struct Config {
    /// Whether the scope prompt autocompletes from commit history.
    pub autocomplete: bool,
    /// Whether to ask about breaking changes.
    pub breaking_changes: bool,
    /// Available commit types, keyed by name.
    pub commit_types: IndexMap<String, CommitType>,
    /// Configured commit scopes, keyed by name.
    pub commit_scopes: IndexMap<String, CommitScope>,
    /// Whether to prepend the type emoji to the summary.
    pub emoji: bool,
    /// Whether to ask about related issues.
    pub issues: bool,
    /// Whether to sign the commit.
    pub sign: bool,
    /// Whether the scope must be picked from `commit_scopes`.
    pub force_config_scopes: bool,
    /// Whether the scope may be left empty.
    pub allow_empty_scope: bool,
    /// Directory cucco was started in.
    pub workdir: PathBuf,
}

/// A commit type such as `feat` or `fix`.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct CommitType {
    /// Human-readable description shown in the type prompt.
    pub description: String,
    /// Emoji prepended to the summary when emoji are enabled.
    pub emoji: Option<String>,
    /// Type name written in the commit message.
    pub name: String,
}

/// A configured commit scope, optionally with path patterns and/or an AST-grep rule
/// for automatic scope detection from staged changes.
#[derive(Clone, Deserialize)]
pub struct CommitScope {
    /// Scope name written in the commit message.
    pub name: String,
    /// Human-readable description shown in the scope prompt.
    pub description: Option<String>,

    /// Regex patterns matched against staged file paths (prefixed with `/`).
    /// In TOML this may be given as a single string or a list of strings.
    #[serde(default, deserialize_with = "string_or_seq")]
    pub patterns: Vec<String>,

    /// AST-grep rule that pre-assigns this scope when it matches staged file content.
    #[cfg(feature = "ast-grep")]
    #[serde(default)]
    pub ast_grep: Option<ast_grep_config::SerializableRuleConfig<ast_grep_language::SupportLang>>,
}

// `SerializableRuleConfig` implements neither `Debug` nor `PartialEq`, so we can't
// derive `Debug` for `CommitScope` (needed because `Config` derives it).
impl fmt::Debug for CommitScope {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CommitScope")
            .field("name", &self.name)
            .field("description", &self.description)
            .field("patterns", &self.patterns)
            .finish_non_exhaustive()
    }
}

/// Accept either a single string or a list of strings for a `Vec<String>` field.
fn string_or_seq<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum OneOrMany {
        One(String),
        Many(Vec<String>),
    }

    Ok(match OneOrMany::deserialize(deserializer)? {
        OneOrMany::One(pattern) => vec![pattern],
        OneOrMany::Many(patterns) => patterns,
    })
}

#[derive(Clone, Debug, Deserialize)]
struct ConfigTOML {
    autocomplete: bool,
    breaking_changes: bool,
    #[serde(default)]
    commit_types: Vec<CommitType>,
    #[serde(default)]
    commit_scopes: Vec<CommitScope>,
    emoji: bool,
    issues: bool,
    sign: bool,
    force_config_scopes: bool,
    allow_empty_scope: bool,
}

/// Overrides applied on top of the config files, and where to look for them.
#[derive(Debug, Default)]
pub struct ConfigArgs {
    /// Extra config file, read last among the files.
    pub path: Option<PathBuf>,
    /// Overrides `autocomplete`.
    pub autocomplete: Option<bool>,
    /// Overrides `breaking_changes`.
    pub breaking_changes: Option<bool>,
    /// Overrides `emoji`.
    pub emoji: Option<bool>,
    /// Overrides `issues`.
    pub issues: Option<bool>,
    /// Overrides `sign`.
    pub sign: Option<bool>,
    /// Overrides `force_config_scopes`.
    pub force_scope: Option<bool>,
    /// Overrides `allow_empty_scope`.
    pub allow_empty_scope: Option<bool>,
    /// Extra directory searched for `cucco/config.toml`, meant for tests.
    pub _user_config_path: Option<PathBuf>,
    /// Working directory to use instead of the process one.
    pub _current_dir: Option<PathBuf>,
}

impl Config {
    /// Finds the config files, merges them, and applies the overrides in `args`.
    ///
    /// # Errors
    ///
    /// Returns an error if the working directory cannot be determined, a config
    /// file is malformed, or a scope pattern or ast-grep rule is invalid.
    pub fn new(args: Option<ConfigArgs>) -> Result<Self> {
        let ConfigArgs {
            path,
            autocomplete,
            breaking_changes,
            emoji,
            issues,
            sign,
            force_scope,
            allow_empty_scope,
            _user_config_path,
            _current_dir,
        } = args.unwrap_or_default();

        let mut settings = config::Config::builder();

        let workdir = match _current_dir {
            Some(dir) => dir,
            None => current_dir()?,
        };

        // Get the default config
        let default_str = include_str!("../../meta/config/default.toml");
        settings = settings.add_source(config::File::from_str(default_str, FileFormat::Toml));

        // Define the order in which configuration directories will be loaded
        let mut config_dirs = vec![config_dir()];
        #[cfg(any(unix, target_os = "redox"))]
        config_dirs.push(BaseDirectories::new().get_config_home());
        config_dirs.push(_user_config_path);

        settings = config_dirs
            .into_iter()
            .flatten()
            .map(|d| d.join("cucco/config.toml"))
            .map(|d| config::File::from(d).required(false))
            .fold(settings, |prev, cfg| prev.add_source(cfg));

        // Try to get config from working directory
        let working_dir_path = workdir.join(".cucco.toml");
        settings = settings.add_source(config::File::from(working_dir_path).required(false));

        // Try to get config from the file passed with --config
        if let Some(path) = path {
            settings = settings.add_source(config::File::from(path).required(false));
        }

        let config: ConfigTOML = settings.build()?.try_deserialize()?;

        // Gather up commit types
        let commit_types = config
            .commit_types
            .into_iter()
            .map(|commit_type| (commit_type.name.clone(), commit_type))
            .collect();

        // Gather up commit scopes (patterns and ast_grep are inline on each scope)
        let commit_scopes = config
            .commit_scopes
            .into_iter()
            .map(|commit_scope| (commit_scope.name.clone(), commit_scope))
            .collect();

        let config = Config {
            autocomplete: autocomplete.unwrap_or(config.autocomplete),
            breaking_changes: breaking_changes.unwrap_or(config.breaking_changes),
            commit_types,
            commit_scopes,
            emoji: emoji.unwrap_or(config.emoji),
            issues: issues.unwrap_or(config.issues),
            sign: sign.unwrap_or(config.sign),
            force_config_scopes: force_scope.unwrap_or(config.force_config_scopes),
            allow_empty_scope: allow_empty_scope.unwrap_or(config.allow_empty_scope),
            workdir,
        };

        config.validate_scope_patterns()?;
        #[cfg(feature = "ast-grep")]
        config.validate_ast_grep_rules()?;

        Ok(config)
    }
}

#[cfg(test)]
mod tests {
    use std::error::Error;

    use super::*;

    #[test]
    fn test_from_path() -> Result<(), Box<dyn Error>> {
        let tempdir = tempfile::tempdir()?;
        std::fs::write(
            tempdir.path().join("my-cucco.toml"),
            "[[commit_types]]\nname=\"1234\"\ndescription=\"test\"",
        )?;

        let config = Config::new(Some(ConfigArgs {
            path: Some(tempdir.path().join("my-cucco.toml")),
            ..ConfigArgs::default()
        }));

        assert!(config.is_ok());
        assert!(config?.commit_types.get("1234").is_some());

        tempdir.close()?;

        Ok(())
    }

    #[test]
    fn test_local_config() -> Result<(), Box<dyn Error>> {
        let tempdir = tempfile::tempdir()?;
        std::fs::write(
            tempdir.path().join(".cucco.toml"),
            "[[commit_types]]\nname=\"123\"\ndescription=\"test\"",
        )?;

        let config = Config::new(Some(ConfigArgs {
            _current_dir: Some(tempdir.path().to_path_buf()),
            ..Default::default()
        }));

        assert!(config.is_ok());
        assert!(config?.commit_types.get("123").is_some());

        tempdir.close()?;

        Ok(())
    }

    #[test]
    fn test_user_config_config() -> Result<(), Box<dyn Error>> {
        let tempdir_current = tempfile::tempdir()?;
        let tempdir_config = tempfile::tempdir()?;
        std::fs::create_dir(tempdir_config.path().join("cucco"))?;
        std::fs::write(
            tempdir_config.path().join("cucco").join("config.toml"),
            "[[commit_types]]\nname=\"12345\"\ndescription=\"test\"",
        )?;

        let config = Config::new(Some(ConfigArgs {
            _user_config_path: Some(tempdir_config.path().to_path_buf()),
            _current_dir: Some(tempdir_current.path().to_path_buf()),
            ..Default::default()
        }));

        assert!(config.is_ok());
        assert!(config?.commit_types.get("12345").is_some());

        tempdir_current.close()?;
        tempdir_config.close()?;

        Ok(())
    }

    #[test]
    fn test_all_config_sources() -> Result<(), Box<dyn Error>> {
        let tempdir_config = tempfile::tempdir()?;
        std::fs::create_dir(tempdir_config.path().join("cucco"))?;
        std::fs::write(
            tempdir_config.path().join("cucco").join("config.toml"),
            "[[commit_types]]\nname=\"12345\"\ndescription=\"test\"",
        )?;
        let tempdir_current = tempfile::tempdir()?;
        std::fs::write(tempdir_current.path().join(".cucco.toml"), "emoji=\"true\"")?;
        let tempdir_path = tempfile::tempdir()?;
        std::fs::write(tempdir_path.path().join("custom.toml"), "autocomplete=true")?;

        let config = Config::new(Some(ConfigArgs {
            _user_config_path: Some(tempdir_config.path().to_path_buf()),
            _current_dir: Some(tempdir_current.path().to_path_buf()),
            path: Some(tempdir_path.path().join("custom.toml").to_path_buf()),
            emoji: Some(false),
            ..Default::default()
        }))?;

        // from user config dir
        assert!(config.commit_types.get("12345").is_some());
        assert!(config.commit_types.len() == 1);
        // set by current dir config and directly, which overwrites the former
        assert!(!config.emoji);
        // set by passed config path
        assert!(config.autocomplete);
        // a default
        assert!(!config.sign);

        tempdir_current.close()?;
        tempdir_config.close()?;
        tempdir_path.close()?;

        Ok(())
    }

    #[test]
    fn test_non_custom_use_defaults() -> Result<(), Box<dyn Error>> {
        let tempdir = tempfile::tempdir()?;

        let config = Config::new(Some(ConfigArgs {
            _current_dir: Some(tempdir.path().to_path_buf()),
            _user_config_path: Some(tempdir.path().to_path_buf()),
            ..Default::default()
        }));

        assert!(config.is_ok());
        assert!(!config?.commit_types.is_empty());

        tempdir.close()?;

        Ok(())
    }

    #[test]
    fn test_breaking_changes() -> Result<(), Box<dyn Error>> {
        let config = Config::new(None)?;
        assert!(config.breaking_changes);

        let config = Config::new(Some(ConfigArgs {
            breaking_changes: Some(false),
            ..Default::default()
        }))?;
        assert!(!config.breaking_changes);

        Ok(())
    }

    #[test]
    fn test_issues() -> Result<(), Box<dyn Error>> {
        let config = Config::new(None)?;
        assert!(config.issues);

        let config = Config::new(Some(ConfigArgs {
            issues: Some(false),
            ..Default::default()
        }))?;
        assert!(!config.issues);

        Ok(())
    }

    #[test]
    fn test_commit_types() -> Result<(), Box<dyn Error>> {
        let config = Config::new(None)?;
        let commit_types = config.commit_types;

        assert_eq!(
            commit_types.get("feat"),
            Some(&CommitType {
                name: "feat".into(),
                emoji: Some("✨".into()),
                description: "A new feature".into()
            })
        );

        Ok(())
    }

    #[test]
    fn test_commit_scopes() -> Result<(), Box<dyn Error>> {
        let tempdir = tempfile::tempdir()?;
        std::fs::write(
            tempdir.path().join(".cucco.toml"),
            "[[commit_scopes]]\nname=\"app\"\ndescription=\"Application code\"",
        )?;
        let config = Config::new(Some(ConfigArgs {
            _current_dir: Some(tempdir.path().to_path_buf()),
            ..Default::default()
        }))?;
        assert!(config.commit_scopes.get("app").is_some());
        let scope = config.commit_scopes.get("app").unwrap();
        assert_eq!(scope.name, "app");
        assert_eq!(scope.description, Some("Application code".into()));
        tempdir.close()?;
        Ok(())
    }

    #[test]
    fn test_commit_scopes_from_config() -> Result<(), Box<dyn Error>> {
        let tempdir_config = tempfile::tempdir()?;
        std::fs::create_dir(tempdir_config.path().join("cucco"))?;
        std::fs::write(
            tempdir_config.path().join("cucco").join("config.toml"),
            "[[commit_scopes]]\nname=\"server\"\ndescription=\"Server code\"\n[[commit_scopes]]\nname=\"shared\"",
        )?;
        let tempdir_current = tempfile::tempdir()?;
        let config = Config::new(Some(ConfigArgs {
            _user_config_path: Some(tempdir_config.path().to_path_buf()),
            _current_dir: Some(tempdir_current.path().to_path_buf()),
            ..Default::default()
        }))?;
        assert!(config.commit_scopes.get("server").is_some());
        assert!(config.commit_scopes.get("shared").is_some());
        assert_eq!(config.commit_scopes.len(), 2);
        tempdir_current.close()?;
        tempdir_config.close()?;
        Ok(())
    }

    #[test]
    fn test_scope_patterns_inline() -> Result<(), Box<dyn Error>> {
        let tempdir = tempfile::tempdir()?;
        std::fs::write(
            tempdir.path().join(".cucco.toml"),
            "[[commit_scopes]]\nname=\"core\"\ndescription=\"Core crate\"\npatterns = \"/crates/core/**/*.rs\"\n[[commit_scopes]]\nname=\"build\"\npatterns = [\"^/build\\\\.rs$\", \"/justfile\"]",
        )?;

        let config = Config::new(Some(ConfigArgs {
            _current_dir: Some(tempdir.path().to_path_buf()),
            ..Default::default()
        }))?;

        assert!(config.commit_scopes.contains_key("core"));
        assert!(config.commit_scopes.contains_key("build"));

        let core = config.commit_scopes.get("core").unwrap();
        assert_eq!(core.description, Some("Core crate".into()));
        assert_eq!(core.patterns, vec!["/crates/core/**/*.rs".to_string()]);

        let build = config.commit_scopes.get("build").unwrap();
        assert_eq!(build.patterns, vec!["^/build\\.rs$", "/justfile"]);

        tempdir.close()?;
        Ok(())
    }

    #[cfg(feature = "ast-grep")]
    #[test]
    fn test_scope_ast_grep_inline() -> Result<(), Box<dyn Error>> {
        let tempdir = tempfile::tempdir()?;
        // Use a subtable for ast_grep within the commit_scopes array entry
        std::fs::write(
            tempdir.path().join(".cucco.toml"),
            "[[commit_scopes]]\nname = \"test\"\ndescription = \"Test functions\"\n\n[commit_scopes.ast_grep]\nlanguage = \"Rust\"\nrule = { kind = \"function_item\" }\nfiles = [\"**/*.rs\"]\n",
        )?;

        let config = Config::new(Some(ConfigArgs {
            _current_dir: Some(tempdir.path().to_path_buf()),
            ..Default::default()
        }))?;

        assert!(config.commit_scopes.contains_key("test"));
        let scope = config.commit_scopes.get("test").unwrap();
        assert_eq!(scope.description, Some("Test functions".into()));
        assert!(scope.ast_grep.is_some());

        tempdir.close()?;
        Ok(())
    }
}