Skip to content

Commit 9f6c46a

Browse files
committed
refactor: simplify changelog construction
1 parent 8bdb469 commit 9f6c46a

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

git-cliff-core/src/changelog.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,18 @@ pub struct Changelog<'a> {
4242

4343
impl<'a> Changelog<'a> {
4444
/// Constructs a new instance.
45-
///
46-
/// Processes the commits/releases and fetches the remote data
47-
/// if `process_data` is set to `true`.
48-
pub fn new(
49-
releases: Vec<Release<'a>>,
50-
config: Config,
51-
process_data: bool,
52-
) -> Result<Self> {
45+
pub fn new(releases: Vec<Release<'a>>, config: Config) -> Result<Self> {
46+
let mut changelog = Changelog::build(releases, config)?;
47+
changelog.add_remote_data()?;
48+
changelog.process_commits();
49+
changelog.process_releases();
50+
Ok(changelog)
51+
}
52+
53+
/// Builds a changelog from releases and config.
54+
fn build(releases: Vec<Release<'a>>, config: Config) -> Result<Self> {
5355
let trim = config.changelog.trim.unwrap_or(true);
54-
let mut changelog = Self {
56+
Ok(Self {
5557
releases,
5658
header_template: match &config.changelog.header {
5759
Some(header) => {
@@ -68,18 +70,12 @@ impl<'a> Changelog<'a> {
6870
},
6971
config,
7072
additional_context: HashMap::new(),
71-
};
72-
if process_data {
73-
changelog.add_remote_data()?;
74-
changelog.process_commits();
75-
changelog.process_releases();
76-
}
77-
Ok(changelog)
73+
})
7874
}
7975

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

8581
/// Adds a key value pair to the template context.
@@ -1030,7 +1026,7 @@ mod test {
10301026
#[test]
10311027
fn changelog_generator() -> Result<()> {
10321028
let (config, releases) = get_test_data();
1033-
let mut changelog = Changelog::new(releases, config, true)?;
1029+
let mut changelog = Changelog::new(releases, config)?;
10341030
changelog.bump_version()?;
10351031
changelog.releases[0].timestamp = 0;
10361032
let mut out = Vec::new();
@@ -1149,7 +1145,7 @@ chore(deps): fix broken deps
11491145
",
11501146
),
11511147
));
1152-
let changelog = Changelog::new(releases, config, true)?;
1148+
let changelog = Changelog::new(releases, config)?;
11531149
let mut out = Vec::new();
11541150
changelog.generate(&mut out)?;
11551151
assert_eq!(
@@ -1253,7 +1249,7 @@ chore(deps): fix broken deps
12531249
{% endfor %}{% endfor %}"#
12541250
.to_string(),
12551251
);
1256-
let mut changelog = Changelog::new(releases, config, true)?;
1252+
let mut changelog = Changelog::new(releases, config)?;
12571253
changelog.add_context("custom_field", "Hello")?;
12581254
let mut out = Vec::new();
12591255
changelog.generate(&mut out)?;

git-cliff-tui/src/state.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ impl<'a> State<'a> {
109109
.clone(),
110110
)?
111111
.0;
112-
let changelog =
113-
Changelog::new(changelog.releases.clone(), config.clone(), false)?;
112+
let mut changelog =
113+
Changelog::new(changelog.releases.clone(), config.clone())?;
114+
changelog.add_remote_context()?;
114115
let mut output = Vec::new();
115116
git_cliff::write_changelog(
116117
self.args.clone(),

git-cliff/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ pub fn generate_changelog(args: &mut Args) -> Result<Changelog<'static>> {
589589
&args,
590590
)?);
591591
}
592-
Changelog::new(releases, config.clone(), true)?
592+
Changelog::new(releases, config.clone())?
593593
};
594594

595595
Ok(changelog)

0 commit comments

Comments
 (0)