gitcc_cli/
changelog.rs

1//! `changelog` command
2
3use std::env;
4
5use clap::Parser;
6use gitcc_core::{
7    build_changelog, commit_history, ChangelogBuildOptions, Config, StatusShow,
8    TEMPLATE_CHANGELOG_STD,
9};
10
11use crate::{info, warn};
12
13/// Changelog command arguments
14#[derive(Debug, Parser)]
15pub struct ChangelogArgs {
16    /// Includes all commits
17    #[arg(long)]
18    pub all: bool,
19    /// Sets the latest version as 'Unreleased'
20    #[arg(long)]
21    pub unreleased: bool,
22}
23
24/// Generates the change log
25pub fn run(args: ChangelogArgs) -> anyhow::Result<()> {
26    let cwd = env::current_dir()?;
27    let cfg = Config::load_from_fs(&cwd)?;
28    let cfg = if let Some(c) = cfg {
29        c
30    } else {
31        info!("using default config");
32        Config::default()
33    };
34
35    // Checks that the repo is clean
36    let status = gitcc_core::git_status(&cwd, StatusShow::IndexAndWorkdir)?;
37    if !status.is_empty() {
38        warn!("repo is dirty");
39    }
40
41    // Generate the changelog
42    let history = commit_history(&cwd, &cfg)?;
43    let changelog_opts = ChangelogBuildOptions {
44        origin_name: None,
45        all: args.all,
46        next_version: if args.unreleased {
47            None
48        } else {
49            Some(history.next_version_str())
50        },
51    };
52    let changelog = build_changelog(&cwd, &cfg, &history, Some(changelog_opts))?;
53    let changelog_str = changelog.render(TEMPLATE_CHANGELOG_STD)?;
54    println!("{changelog_str}");
55
56    Ok(())
57}