Skip to content

Commit

Permalink
feat(config): allow configuring output file from config (#829)
Browse files Browse the repository at this point in the history
* feat(config): allow configuring output file from config

* test: add missing output to tests

* chore: move output to default config
  • Loading branch information
orhun authored Sep 21, 2024
1 parent c41eacb commit c2db791
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ trim = true
postprocessors = [
# { pattern = '<REPO>', replace = "https://github.com/orhun/git-cliff" }, # replace repository URL
]
# output file path
# output = "test.md"

[git]
# parse the commits based on https://www.conventionalcommits.org
Expand Down
1 change: 1 addition & 0 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ mod test {
replace: Some(String::from("exciting")),
replace_command: None,
}]),
output: None,
},
git: GitConfig {
conventional_commits: Some(true),
Expand Down
2 changes: 2 additions & 0 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub struct ChangelogConfig {
pub trim: Option<bool>,
/// Changelog postprocessors.
pub postprocessors: Option<Vec<TextProcessor>>,
/// Output file path.
pub output: Option<PathBuf>,
}

/// Git configuration
Expand Down
1 change: 1 addition & 0 deletions git-cliff-core/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn generate_changelog() -> Result<()> {
footer: Some(String::from("eoc - end of changelog")),
trim: None,
postprocessors: None,
output: None,
};
let git_config = GitConfig {
conventional_commits: Some(true),
Expand Down
9 changes: 5 additions & 4 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ pub fn run(mut args: Opt) -> Result<()> {
}

// Update the configuration based on command line arguments and vice versa.
let output = args.output.clone().or(config.changelog.output.clone());
match args.strip {
Some(Strip::Header) => {
config.changelog.header = None;
Expand All @@ -445,9 +446,9 @@ pub fn run(mut args: Opt) -> Result<()> {
)));
}
}
if args.output.is_some() &&
if output.is_some() &&
args.prepend.is_some() &&
args.output.as_ref() == args.prepend.as_ref()
output.as_ref() == args.prepend.as_ref()
{
return Err(Error::ArgumentError(String::from(
"'-o' and '-p' can only be used together if they point to different \
Expand Down Expand Up @@ -595,7 +596,7 @@ pub fn run(mut args: Opt) -> Result<()> {
};

// Print the result.
let mut out: Box<dyn io::Write> = if let Some(path) = &args.output {
let mut out: Box<dyn io::Write> = if let Some(path) = &output {
if path == Path::new("-") {
Box::new(io::stdout())
} else {
Expand Down Expand Up @@ -631,7 +632,7 @@ pub fn run(mut args: Opt) -> Result<()> {
let mut out = io::BufWriter::new(File::create(path)?);
changelog.prepend(changelog_before, &mut out)?;
}
if args.output.is_some() || args.prepend.is_none() {
if output.is_some() || args.prepend.is_none() {
changelog.generate(&mut out)?;
}

Expand Down
4 changes: 4 additions & 0 deletions website/docs/configuration/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ It is useful for adding indentation to the template for readability, as shown [i
An array of commit postprocessors for manipulating the changelog before outputting.
Can e.g. be used for replacing commit author with GitHub usernames.
Internally postprocessors and preprocessors are the same. See [commit_preprocessors](/docs/configuration/git#commit_preprocessors) for more detail and examples, it uses the same syntax.

### output

Output file path for the changelog. You can also use the `--output` argument to override this value.

0 comments on commit c2db791

Please sign in to comment.