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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{fmt, fs};
use glob::Pattern;
use regex::{Regex, RegexBuilder};
use secrecy::SecretString;
use serde::{Deserialize, Serialize};
use crate::embed::EmbeddedConfig;
use crate::error::Result;
use crate::{DEFAULT_CONFIG, command, error};
/// Default initial tag.
const DEFAULT_INITIAL_TAG: &str = "0.1.0";
/// Manifest file information and regex for matching contents.
#[derive(Debug)]
struct ManifestInfo {
/// Path of the manifest.
path: PathBuf,
/// Regular expression for matching metadata in the manifest.
regex: Regex,
}
lazy_static::lazy_static! {
/// Array containing manifest information for Rust and Python projects.
static ref MANIFEST_INFO: Vec<ManifestInfo> = vec![
ManifestInfo {
path: PathBuf::from("Cargo.toml"),
regex: RegexBuilder::new(
r"^\[(?:workspace|package)\.metadata\.git\-cliff\.",
)
.multi_line(true)
.build()
.expect("failed to build regex"),
},
ManifestInfo {
path: PathBuf::from("pyproject.toml"),
regex: RegexBuilder::new(r"^\[(?:tool)\.git\-cliff\.")
.multi_line(true)
.build()
.expect("failed to build regex"),
},
];
}
/// Configuration values.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
/// Configuration values about changelog generation.
#[serde(default)]
pub changelog: ChangelogConfig,
/// Configuration values about git.
#[serde(default)]
pub git: GitConfig,
/// Configuration values about remote.
#[serde(default)]
pub remote: RemoteConfig,
/// Configuration values about bump version.
#[serde(default)]
pub bump: Bump,
}
/// Changelog configuration.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct ChangelogConfig {
/// Changelog header.
pub header: Option<String>,
/// Changelog body, template.
pub body: String,
/// Changelog footer.
pub footer: Option<String>,
/// Trim the template.
pub trim: bool,
/// Always render the body template.
pub render_always: bool,
/// Changelog postprocessors.
pub postprocessors: Vec<TextProcessor>,
/// Output file path.
pub output: Option<PathBuf>,
}
/// Git configuration
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct GitConfig {
/// Parse commits according to the conventional commits specification.
pub conventional_commits: bool,
/// Require all commits to be conventional.
/// Takes precedence over `filter_unconventional`.
pub require_conventional: bool,
/// Exclude commits that do not match the conventional commits specification
/// from the changelog.
pub filter_unconventional: bool,
/// Split commits on newlines, treating each line as an individual commit.
pub split_commits: bool,
/// An array of regex based parsers to modify commit messages prior to
/// further processing.
pub commit_preprocessors: Vec<TextProcessor>,
/// An array of regex based parsers for extracting data from the commit
/// message.
pub commit_parsers: Vec<CommitParser>,
/// Prevent commits having the `BREAKING CHANGE:` footer from being excluded
/// by commit parsers.
pub protect_breaking_commits: bool,
/// An array of regex based parsers to extract links from the commit message
/// and add them to the commit's context.
pub link_parsers: Vec<LinkParser>,
/// Exclude commits that are not matched by any commit parser.
pub filter_commits: bool,
/// Fail on a commit that is not matched by any commit parser.
pub fail_on_unmatched_commit: bool,
/// Regex to select git tags that represent releases.
#[serde(with = "serde_regex", default)]
pub tag_pattern: Option<Regex>,
/// Regex to select git tags that do not represent proper releases.
#[serde(with = "serde_regex", default)]
pub skip_tags: Option<Regex>,
/// Regex to exclude git tags after applying the `tag_pattern`.
#[serde(with = "serde_regex", default)]
pub ignore_tags: Option<Regex>,
/// Regex to count matched tags.
#[serde(with = "serde_regex", default)]
pub count_tags: Option<Regex>,
/// Include only the tags that belong to the current branch.
pub use_branch_tags: bool,
/// Order releases topologically instead of chronologically.
pub topo_order: bool,
/// Order commits chronologically instead of topologically.
pub topo_order_commits: bool,
/// How to order commits in each group/release within the changelog.
pub sort_commits: String,
/// Limit the total number of commits included in the changelog.
pub limit_commits: Option<usize>,
/// Read submodule commits.
pub recurse_submodules: Option<bool>,
/// Include related commits with changes at the specified paths.
#[serde(with = "serde_pattern", default)]
pub include_paths: Vec<Pattern>,
/// Exclude unrelated commits with changes at the specified paths.
#[serde(with = "serde_pattern", default)]
pub exclude_paths: Vec<Pattern>,
}
/// Serialize and deserialize implementation for [`glob::Pattern`].
mod serde_pattern {
use glob::Pattern;
use serde::Deserialize;
use serde::de::Error;
use serde::ser::SerializeSeq;
pub fn serialize<S>(patterns: &[Pattern], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(Some(patterns.len()))?;
for pattern in patterns {
seq.serialize_element(pattern.as_str())?;
}
seq.end()
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Pattern>, D::Error>
where
D: serde::Deserializer<'de>,
{
let patterns = Vec::<String>::deserialize(deserializer)?;
patterns
.into_iter()
.map(|pattern| pattern.parse().map_err(D::Error::custom))
.collect()
}
}
/// Remote configuration.
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct RemoteConfig {
/// Run in offline mode.
#[serde(default)]
pub offline: bool,
/// GitHub remote.
#[serde(default)]
pub github: Remote,
/// GitLab remote.
#[serde(default)]
pub gitlab: Remote,
/// Gitea remote.
#[serde(default)]
pub gitea: Remote,
/// Bitbucket remote.
#[serde(default)]
pub bitbucket: Remote,
/// Azure DevOps remote.
#[serde(default)]
pub azure_devops: Remote,
}
impl RemoteConfig {
/// Returns `true` if any remote is set.
#[must_use]
pub fn is_any_set(&self) -> bool {
#[cfg(feature = "github")]
if self.github.is_set() {
return true;
}
#[cfg(feature = "gitlab")]
if self.gitlab.is_set() {
return true;
}
#[cfg(feature = "gitea")]
if self.gitea.is_set() {
return true;
}
#[cfg(feature = "bitbucket")]
if self.bitbucket.is_set() {
return true;
}
#[cfg(feature = "azure_devops")]
if self.azure_devops.is_set() {
return true;
}
false
}
/// Enables the native TLS for all remotes.
pub fn enable_native_tls(&mut self) {
#[cfg(feature = "github")]
{
self.github.native_tls = Some(true);
}
#[cfg(feature = "gitlab")]
{
self.gitlab.native_tls = Some(true);
}
#[cfg(feature = "gitea")]
{
self.gitea.native_tls = Some(true);
}
#[cfg(feature = "bitbucket")]
{
self.bitbucket.native_tls = Some(true);
}
#[cfg(feature = "azure_devops")]
{
self.azure_devops.native_tls = Some(true);
}
}
}
/// A single remote.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Remote {
/// Owner of the remote.
pub owner: String,
/// Repository name.
pub repo: String,
/// Access token.
#[serde(skip_serializing)]
pub token: Option<SecretString>,
/// Whether if the remote is set manually.
#[serde(skip_deserializing, default = "default_true")]
pub is_custom: bool,
/// Remote API URL.
pub api_url: Option<String>,
/// Whether to use native TLS.
pub native_tls: Option<bool>,
}
/// Returns `true` for serde's `default` attribute.
fn default_true() -> bool {
true
}
impl fmt::Display for Remote {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.owner, self.repo)
}
}
impl PartialEq for Remote {
fn eq(&self, other: &Self) -> bool {
self.to_string() == other.to_string()
}
}
impl Remote {
/// Constructs a new instance.
pub fn new<S: Into<String>>(owner: S, repo: S) -> Self {
Self {
owner: owner.into(),
repo: repo.into(),
token: None,
is_custom: false,
api_url: None,
native_tls: None,
}
}
/// Returns `true` if the remote has an owner and repo.
#[must_use]
pub fn is_set(&self) -> bool {
!self.owner.is_empty() && !self.repo.is_empty()
}
}
/// Version bump type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum BumpType {
/// Bump major version.
Major,
/// Bump minor version.
Minor,
/// Bump patch version.
Patch,
}
/// Bump version configuration.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Bump {
/// Configures automatic minor version increments for feature changes.
///
/// When `true`, a feature will always trigger a minor version update.
/// When `false`, a feature will trigger:
///
/// - A patch version update if the major version is 0.
/// - A minor version update otherwise.
pub features_always_bump_minor: Option<bool>,
/// Configures 0 -> 1 major version increments for breaking changes.
///
/// When `true`, a breaking change commit will always trigger a major
/// version update (including the transition from version 0 to 1)
/// When `false`, a breaking change commit will trigger:
///
/// - A minor version update if the major version is 0.
/// - A major version update otherwise.
pub breaking_always_bump_major: Option<bool>,
/// Configures the initial version of the project.
///
/// When set, the version will be set to this value if no tags are found.
pub initial_tag: Option<String>,
/// Configure a custom regex pattern for major version increments.
///
/// This will check only the type of the commit against the given pattern.
///
/// ### Note
///
/// `commit type` according to the spec is only `[a-zA-Z]+`
pub custom_major_increment_regex: Option<String>,
/// Configure a custom regex pattern for minor version increments.
///
/// This will check only the type of the commit against the given pattern.
///
/// ### Note
///
/// `commit type` according to the spec is only `[a-zA-Z]+`
pub custom_minor_increment_regex: Option<String>,
/// Force to always bump in major, minor or patch.
pub bump_type: Option<BumpType>,
}
impl Bump {
/// Returns the initial tag.
///
/// This function also logs the returned value.
#[must_use]
pub fn get_initial_tag(&self) -> String {
if let Some(tag) = self.initial_tag.clone() {
log::warn!("No releases found, using initial tag '{tag}' as the next version");
tag
} else {
log::warn!("No releases found, using {DEFAULT_INITIAL_TAG} as the next version");
DEFAULT_INITIAL_TAG.into()
}
}
}
/// Parser for grouping commits.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct CommitParser {
/// SHA1 of the commit.
pub sha: Option<String>,
/// Regex for matching the commit message.
#[serde(with = "serde_regex", default)]
pub message: Option<Regex>,
/// Regex for matching the commit body.
#[serde(with = "serde_regex", default)]
pub body: Option<Regex>,
/// Regex for matching the commit footer.
#[serde(with = "serde_regex", default)]
pub footer: Option<Regex>,
/// Group of the commit.
pub group: Option<String>,
/// Default scope of the commit.
pub default_scope: Option<String>,
/// Commit scope for overriding the default scope.
pub scope: Option<String>,
/// Whether to skip this commit group.
pub skip: Option<bool>,
/// Field name of the commit to match the regex against.
pub field: Option<String>,
/// Regex for matching the field value.
#[serde(with = "serde_regex", default)]
pub pattern: Option<Regex>,
}
/// `TextProcessor`, e.g. for modifying commit messages.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextProcessor {
/// Regex for matching a text to replace.
#[serde(with = "serde_regex")]
pub pattern: Regex,
/// Replacement text.
pub replace: Option<String>,
/// Command that will be run for replacing the commit message.
pub replace_command: Option<String>,
}
impl TextProcessor {
/// Replaces the text with using the given pattern or the command output.
pub fn replace(&self, rendered: &mut String, command_envs: Vec<(&str, &str)>) -> Result<()> {
if let Some(text) = &self.replace {
*rendered = self.pattern.replace_all(rendered, text).to_string();
} else if let Some(command) = &self.replace_command {
if self.pattern.is_match(rendered) {
*rendered = command::run(command, Some(rendered.clone()), command_envs)?;
}
}
Ok(())
}
}
/// Parser for extracting links in commits.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinkParser {
/// Regex for finding links in the commit message.
#[serde(with = "serde_regex")]
pub pattern: Regex,
/// The string used to generate the link URL.
pub href: String,
/// The string used to generate the link text.
pub text: Option<String>,
}
impl Config {
/// Reads the config file contents from project manifest (e.g. Cargo.toml,
/// pyproject.toml)
pub fn read_from_manifest() -> Result<Option<String>> {
for info in &(*MANIFEST_INFO) {
if info.path.exists() {
let contents = fs::read_to_string(&info.path)?;
if info.regex.is_match(&contents) {
return Ok(Some(info.regex.replace_all(&contents, "[").to_string()));
}
}
}
Ok(None)
}
/// Parses the config file and returns the values.
pub fn load(path: &Path) -> Result<Config> {
if MANIFEST_INFO
.iter()
.any(|v| path.file_name() == v.path.file_name())
{
if let Some(contents) = Self::read_from_manifest()? {
return contents.parse();
}
}
// Adding sources one after another overwrites the previous values.
// Thus adding the default config initializes the config with default values.
let default_config_str = EmbeddedConfig::get_config()?;
Ok(config::Config::builder()
.add_source(config::File::from_str(
&default_config_str,
config::FileFormat::Toml,
))
.add_source(config::File::from(path))
.add_source(config::Environment::with_prefix("GIT_CLIFF").separator("__"))
.build()?
.try_deserialize()?)
}
/// Find a special config path on macOS.
///
/// The `dirs` crate ignores the `XDG_CONFIG_HOME` env var on macOS and only considers
/// `Library/Application Support` as the config dir, which is primarily used by GUI apps.
///
/// This function determines the config path and honors the `XDG_CONFIG_HOME` env var.
/// If it is not set, it will fall back to `~/.config`
#[cfg(target_os = "macos")]
pub fn retrieve_xdg_config_on_macos() -> PathBuf {
let config_dir = std::env::var("XDG_CONFIG_HOME").map_or_else(
|_| dirs::home_dir().unwrap_or_default().join(".config"),
PathBuf::from,
);
config_dir.join("git-cliff")
}
/// Find the path of the config file.
///
/// If the config file is not found in its standard locations, [`None`] is returned.
#[must_use]
pub fn retrieve_config_path() -> Option<PathBuf> {
for supported_path in [
#[cfg(target_os = "macos")]
Some(Config::retrieve_xdg_config_on_macos().join(DEFAULT_CONFIG)),
dirs::config_dir().map(|dir| dir.join("git-cliff").join(DEFAULT_CONFIG)),
]
.iter()
.filter_map(|v| v.as_ref())
{
if supported_path.exists() {
log::debug!("Using configuration file from: {supported_path:?}");
return Some(supported_path.clone());
}
}
None
}
}
impl FromStr for Config {
type Err = error::Error;
/// Parses the config file from string and returns the values.
fn from_str(contents: &str) -> Result<Self> {
// Adding sources one after another overwrites the previous values.
// Thus adding the default config initializes the config with default
// values.
let default_config_str = EmbeddedConfig::get_config()?;
Ok(config::Config::builder()
.add_source(config::File::from_str(
&default_config_str,
config::FileFormat::Toml,
))
.add_source(config::File::from_str(contents, config::FileFormat::Toml))
.add_source(config::Environment::with_prefix("GIT_CLIFF").separator("__"))
.build()?
.try_deserialize()?)
}
}
#[cfg(test)]
mod test {
use std::env;
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn load() -> Result<()> {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("parent directory not found")
.to_path_buf()
.join("config")
.join(crate::DEFAULT_CONFIG);
const FOOTER_VALUE: &str = "test";
const TAG_PATTERN_VALUE: &str = ".*[0-9].*";
const IGNORE_TAGS_VALUE: &str = "v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+";
unsafe {
env::set_var("GIT_CLIFF__CHANGELOG__FOOTER", FOOTER_VALUE);
env::set_var("GIT_CLIFF__GIT__TAG_PATTERN", TAG_PATTERN_VALUE);
env::set_var("GIT_CLIFF__GIT__IGNORE_TAGS", IGNORE_TAGS_VALUE);
};
let config = Config::load(&path)?;
assert_eq!(Some(String::from(FOOTER_VALUE)), config.changelog.footer);
assert_eq!(
Some(String::from(TAG_PATTERN_VALUE)),
config
.git
.tag_pattern
.map(|tag_pattern| tag_pattern.to_string())
);
assert_eq!(
Some(String::from(IGNORE_TAGS_VALUE)),
config
.git
.ignore_tags
.map(|ignore_tags| ignore_tags.to_string())
);
Ok(())
}
#[test]
fn remote_config() {
let remote1 = Remote::new("abc", "xyz1");
let remote2 = Remote::new("abc", "xyz2");
assert!(!remote1.eq(&remote2));
assert_eq!("abc/xyz1", remote1.to_string());
assert!(remote1.is_set());
assert!(!Remote::new("", "test").is_set());
assert!(!Remote::new("test", "").is_set());
assert!(!Remote::new("", "").is_set());
}
}