diff --git a/git-cliff/src/args.rs b/git-cliff/src/args.rs index ba0123840d..c18eb001ed 100644 --- a/git-cliff/src/args.rs +++ b/git-cliff/src/args.rs @@ -219,9 +219,18 @@ pub struct Opt { long, env = "GIT_CLIFF_TEMPLATE", value_name = "TEMPLATE", - allow_hyphen_values = true + allow_hyphen_values = true, + conflicts_with = "body_file" )] pub body: Option, + /// Sets the template for the changelog body from a file. + #[arg( + long, + env = "GIT_CLIFF_TEMPLATE_FILE", + value_name = "PATH", + value_parser = Opt::parse_dir + )] + pub body_file: Option, /// Processes the commits starting from the latest tag. #[arg(short, long, help_heading = Some("FLAGS"))] pub latest: bool, @@ -585,6 +594,30 @@ mod tests { Ok(()) } + #[test] + fn body_file_is_parsed_as_path() -> Result<(), Box> { + let opt = Opt::try_parse_from(["git-cliff", "--body-file", "template.tera"])?; + assert_eq!(Some(PathBuf::from("template.tera")), opt.body_file); + assert_eq!(None, opt.body); + Ok(()) + } + + #[test] + fn body_and_body_file_conflict() { + let result = Opt::try_parse_from([ + "git-cliff", + "--body", + "{{ version }}", + "--body-file", + "template.tera", + ]); + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().kind(), + clap::error::ErrorKind::ArgumentConflict + ); + } + // Environment variables are process-global, so tests that modify them must run exclusively and // restore the original state after execution. For this reason, we use the `serial` macro // from the `serial_test` crate to guarantee exclusive execution. See: https://crates.io/crates/serial_test diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs index bcb5b62bbf..602c10db2b 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -637,6 +637,8 @@ pub fn run_with_changelog_modifier<'a>( } if let Some(body) = args.body.clone() { config.changelog.body = body; + } else if let Some(body_file) = args.body_file.clone() { + config.changelog.body = fs::read_to_string(&body_file)?; } if args.sort == Sort::Oldest { args.sort = Sort::from_str(&config.git.sort_commits, true) diff --git a/website/docs/usage/args.md b/website/docs/usage/args.md index 8846bb682f..6946cbed29 100644 --- a/website/docs/usage/args.md +++ b/website/docs/usage/args.md @@ -47,6 +47,7 @@ git-cliff [FLAGS] [OPTIONS] [--] [RANGE] -o, --output [] Writes output to the given file [env: GIT_CLIFF_OUTPUT=] -t, --tag Sets the tag for the latest version [env: GIT_CLIFF_TAG=] -b, --body