Skip to content

Commit

Permalink
feat(args): add --context flag for outputting context (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Mar 26, 2022
1 parent 1bacc7f commit 95ad55d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ git-cliff [FLAGS] [OPTIONS] [--] [RANGE]
--current Processes the commits that belong to the current tag
-u, --unreleased Processes the commits that do not belong to a tag
--date-order Sorts the tags chronologically
--context Prints changelog context as JSON
-h, --help Prints help information
-V, --version Prints version information
```
Expand Down Expand Up @@ -260,6 +261,16 @@ Save the changelog file to the specified file:
git cliff --output CHANGELOG.md
```

Print the changelog [context](#context) as JSON:

```sh
# print context to stdout
git cliff --context

# save context to a file
git cliff --context --output context.json
```

Prepend new changes to an existing changelog file:

```sh
Expand Down
1 change: 1 addition & 0 deletions git-cliff-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2021"
[dependencies]
thiserror = "1.0.30"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.78"
tera = "1.15.0"
regex = "1.5.5"
serde_regex = "1.1.0"
Expand Down
3 changes: 3 additions & 0 deletions git-cliff-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub enum Error {
/// Errors that may occur when deserializing types from TOML format.
#[error("Cannot parse TOML: `{0}`")]
DeserializeError(#[from] toml::de::Error),
/// Errors that may occur while de/serializing JSON format.
#[error("Cannot de/serialize JSON: `{0}`")]
JsonError(#[from] serde_json::Error),
}

/// Result type of the core library.
Expand Down
11 changes: 11 additions & 0 deletions git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::commit::Commit;
use crate::error::Result;

/// Representation of a release.
#[derive(Default, Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
Expand All @@ -16,3 +17,13 @@ pub struct Release<'a> {
/// Previous release.
pub previous: Option<Box<Release<'a>>>,
}

/// Representation of a list of releases.
pub struct Releases<'a>(pub &'a Vec<Release<'a>>);

impl<'a> Releases<'a> {
/// Returns the list of releases as JSON.
pub fn as_json(&self) -> Result<String> {
Ok(serde_json::to_string(self.0)?)
}
}
3 changes: 3 additions & 0 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ pub struct Opt {
/// Sorts the tags chronologically.
#[clap(long, help_heading = Some("FLAGS"))]
pub date_order: bool,
/// Prints changelog context as JSON.
#[clap(long, help_heading = Some("FLAGS"))]
pub context: bool,
/// Strips the given parts from the changelog.
#[clap(short, long, value_name = "PART", arg_enum)]
pub strip: Option<Strip>,
Expand Down
12 changes: 11 additions & 1 deletion git-cliff/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use git_cliff_core::error::{
Error,
Result,
};
use git_cliff_core::release::Release;
use git_cliff_core::release::{
Release,
Releases,
};
use git_cliff_core::template::Template;
use std::io::Write;

Expand Down Expand Up @@ -153,6 +156,13 @@ impl<'a> Changelog<'a> {
write!(out, "{}", changelog)?;
Ok(())
}

/// Prints the changelog context to the given output.
pub fn write_context<W: Write>(&self, out: &mut W) -> Result<()> {
let output = Releases(&self.releases).as_json()?;
writeln!(out, "{output}")?;
Ok(())
}
}

#[cfg(test)]
Expand Down
9 changes: 8 additions & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,14 @@ pub fn run(mut args: Opt) -> Result<()> {
if let Some(path) = args.prepend {
changelog.prepend(fs::read_to_string(&path)?, &mut File::create(path)?)
} else if let Some(path) = args.output {
changelog.generate(&mut File::create(path)?)
let mut output = File::create(path)?;
if args.context {
changelog.write_context(&mut output)
} else {
changelog.generate(&mut output)
}
} else if args.context {
changelog.write_context(&mut io::stdout())
} else {
changelog.generate(&mut io::stdout())
}
Expand Down

0 comments on commit 95ad55d

Please sign in to comment.