Skip to content

Commit

Permalink
refactor: simplify changelog construction
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Dec 9, 2024
1 parent 8bdb469 commit 9f6c46a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
36 changes: 16 additions & 20 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ pub struct Changelog<'a> {

impl<'a> Changelog<'a> {
/// Constructs a new instance.
///
/// Processes the commits/releases and fetches the remote data
/// if `process_data` is set to `true`.
pub fn new(
releases: Vec<Release<'a>>,
config: Config,
process_data: bool,
) -> Result<Self> {
pub fn new(releases: Vec<Release<'a>>, config: Config) -> Result<Self> {
let mut changelog = Changelog::build(releases, config)?;
changelog.add_remote_data()?;
changelog.process_commits();
changelog.process_releases();
Ok(changelog)
}

/// Builds a changelog from releases and config.
fn build(releases: Vec<Release<'a>>, config: Config) -> Result<Self> {
let trim = config.changelog.trim.unwrap_or(true);
let mut changelog = Self {
Ok(Self {
releases,
header_template: match &config.changelog.header {
Some(header) => {
Expand All @@ -68,18 +70,12 @@ impl<'a> Changelog<'a> {
},
config,
additional_context: HashMap::new(),
};
if process_data {
changelog.add_remote_data()?;
changelog.process_commits();
changelog.process_releases();
}
Ok(changelog)
})
}

/// Constructs an instance from a serialized context object.
pub fn from_context<R: Read>(input: &mut R, config: Config) -> Result<Self> {
Changelog::new(serde_json::from_reader(input)?, config, false)
Changelog::build(serde_json::from_reader(input)?, config)
}

/// Adds a key value pair to the template context.
Expand Down Expand Up @@ -1030,7 +1026,7 @@ mod test {
#[test]
fn changelog_generator() -> Result<()> {
let (config, releases) = get_test_data();
let mut changelog = Changelog::new(releases, config, true)?;
let mut changelog = Changelog::new(releases, config)?;
changelog.bump_version()?;
changelog.releases[0].timestamp = 0;
let mut out = Vec::new();
Expand Down Expand Up @@ -1149,7 +1145,7 @@ chore(deps): fix broken deps
",
),
));
let changelog = Changelog::new(releases, config, true)?;
let changelog = Changelog::new(releases, config)?;
let mut out = Vec::new();
changelog.generate(&mut out)?;
assert_eq!(
Expand Down Expand Up @@ -1253,7 +1249,7 @@ chore(deps): fix broken deps
{% endfor %}{% endfor %}"#
.to_string(),
);
let mut changelog = Changelog::new(releases, config, true)?;
let mut changelog = Changelog::new(releases, config)?;
changelog.add_context("custom_field", "Hello")?;
let mut out = Vec::new();
changelog.generate(&mut out)?;
Expand Down
5 changes: 3 additions & 2 deletions git-cliff-tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ impl<'a> State<'a> {
.clone(),
)?
.0;
let changelog =
Changelog::new(changelog.releases.clone(), config.clone(), false)?;
let mut changelog =
Changelog::new(changelog.releases.clone(), config.clone())?;
changelog.add_remote_context()?;
let mut output = Vec::new();
git_cliff::write_changelog(
self.args.clone(),
Expand Down
2 changes: 1 addition & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ pub fn generate_changelog(args: &mut Args) -> Result<Changelog<'static>> {
&args,
)?);
}
Changelog::new(releases, config.clone(), true)?
Changelog::new(releases, config.clone())?
};

Ok(changelog)
Expand Down

0 comments on commit 9f6c46a

Please sign in to comment.