Skip to content

Commit

Permalink
refactor: clean up some code
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jun 15, 2024
1 parent dabe716 commit 1c090ea
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 124 deletions.
17 changes: 7 additions & 10 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,14 @@ impl<'a> Changelog<'a> {
serde_json::to_value(self.config.remote.clone())?,
);
#[cfg(feature = "github")]
let (github_commits, github_pull_requests) = if self.config.remote.github.is_set()
{
let (github_commits, github_pull_requests) = if self.config.remote.github.is_set() {
self.get_github_metadata()
.expect("Could not get github metadata")
} else {
(vec![], vec![])
};
#[cfg(feature = "gitlab")]
let (gitlab_commits, gitlab_merge_request) = if self.config.remote.gitlab.is_set()
{
let (gitlab_commits, gitlab_merge_request) = if self.config.remote.gitlab.is_set() {
self.get_gitlab_metadata()
.expect("Could not get gitlab metadata")
} else {
Expand Down Expand Up @@ -494,7 +492,7 @@ impl<'a> Changelog<'a> {
}

/// Generates the changelog and writes it to the given output.
pub fn generate<W: Write>(&self, out: &mut W) -> Result<()> {
pub fn generate<W: Write + ?Sized>(&self, out: &mut W) -> Result<()> {
debug!("Generating changelog...");
let postprocessors = self
.config
Expand All @@ -510,8 +508,7 @@ impl<'a> Changelog<'a> {
}
}
}
let mut releases = self.releases.clone();
for release in releases.iter_mut() {
for release in &self.releases {
let write_result = write!(
out,
"{}",
Expand All @@ -533,7 +530,7 @@ impl<'a> Changelog<'a> {
"{}",
footer_template.render(
&Releases {
releases: &releases,
releases: &self.releases,
},
Some(&self.additional_context),
&postprocessors,
Expand All @@ -549,7 +546,7 @@ impl<'a> Changelog<'a> {
}

/// Generates a changelog and prepends it to the given changelog.
pub fn prepend<W: Write>(
pub fn prepend<W: Write + ?Sized>(
&self,
mut changelog: String,
out: &mut W,
Expand All @@ -564,7 +561,7 @@ impl<'a> Changelog<'a> {
}

/// Prints the changelog context to the given output.
pub fn write_context<W: Write>(&self, out: &mut W) -> Result<()> {
pub fn write_context<W: Write + ?Sized>(&self, out: &mut W) -> Result<()> {
let output = Releases {
releases: &self.releases,
}
Expand Down
58 changes: 29 additions & 29 deletions git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ impl Repository {
/// Sorts the commits by their time.
pub fn commits(
&self,
range: Option<String>,
include_path: Option<Vec<Pattern>>,
exclude_path: Option<Vec<Pattern>>,
range: Option<&str>,
include_path: Option<&[Pattern]>,
exclude_path: Option<&[Pattern]>,
) -> Result<Vec<Commit>> {
let mut revwalk = self.inner.revwalk()?;
revwalk.set_sorting(Sort::TOPOLOGICAL)?;
if let Some(range) = range {
revwalk.push_range(&range)?;
revwalk.push_range(range)?;
} else {
revwalk.push_head()?;
}
Expand All @@ -62,31 +62,31 @@ impl Repository {
.collect();
if include_path.is_some() || exclude_path.is_some() {
commits.retain(|commit| {
if let Ok(prev_commit) = commit.parent(0) {
if let Ok(diff) = self.inner.diff_tree_to_tree(
commit.tree().ok().as_ref(),
prev_commit.tree().ok().as_ref(),
None,
) {
return diff
.deltas()
.filter_map(|delta| delta.new_file().path())
.any(|new_file_path| {
if let Some(include_path) = &include_path {
include_path
.iter()
.any(|glob| glob.matches_path(new_file_path))
} else if let Some(exclude_path) = &exclude_path {
!exclude_path
.iter()
.any(|glob| glob.matches_path(new_file_path))
} else {
false
}
});
}
}
false
let Ok(prev_commit) = commit.parent(0) else {
return false;
};
let Ok(diff) = self.inner.diff_tree_to_tree(
commit.tree().ok().as_ref(),
prev_commit.tree().ok().as_ref(),
None,
) else {
return false;
};
diff.deltas()
.filter_map(|delta| delta.new_file().path())
.any(|new_file_path| {
if let Some(include_path) = include_path {
return include_path
.iter()
.any(|glob| glob.matches_path(new_file_path));
}
if let Some(exclude_path) = exclude_path {
return !exclude_path
.iter()
.any(|glob| glob.matches_path(new_file_path));
}
unreachable!()
})
});
}
Ok(commits)
Expand Down
152 changes: 67 additions & 85 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ use std::fs::{
self,
File,
};
use std::io::{
self,
Write,
};
use std::path::PathBuf;
use std::io;
use std::path::Path;
use std::time::{
SystemTime,
UNIX_EPOCH,
Expand Down Expand Up @@ -88,29 +85,26 @@ fn process_repository<'a>(
let mut tags = repository.tags(&config.git.tag_pattern, args.topo_order)?;
let skip_regex = config.git.skip_tags.as_ref();
let ignore_regex = config.git.ignore_tags.as_ref();
tags = tags
.into_iter()
.filter(|(_, name)| {
// Keep skip tags to drop commits in the later stage.
let skip = skip_regex.map(|r| r.is_match(name)).unwrap_or_default();

let ignore = ignore_regex
.map(|r| {
if r.as_str().trim().is_empty() {
return false;
}
tags.retain(|_, name| {
// Keep skip tags to drop commits in the later stage.
let skip = skip_regex.is_some_and(|r| r.is_match(name));
if skip {
return true;
}

let ignore_tag = r.is_match(name);
if ignore_tag {
trace!("Ignoring release: {}", name)
}
ignore_tag
})
.unwrap_or_default();
let ignore = ignore_regex.is_some_and(|r| {
if r.as_str().trim().is_empty() {
return false;
}

skip || !ignore
})
.collect();
let ignore_tag = r.is_match(name);
if ignore_tag {
trace!("Ignoring release: {}", name)
}
ignore_tag
});
!ignore
});

if !config.remote.github.is_set() {
match repository.upstream_remote() {
Expand Down Expand Up @@ -211,14 +205,12 @@ fn process_repository<'a>(
}
}
let mut commits = repository.commits(
commit_range,
args.include_path.clone(),
args.exclude_path.clone(),
commit_range.as_deref(),
args.include_path.as_deref(),
args.exclude_path.as_deref(),
)?;
if let Some(commit_limit_value) = config.git.limit_commits {
commits = commits
.drain(..commits.len().min(commit_limit_value))
.collect();
commits.truncate(commit_limit_value);
}

// Update tags.
Expand All @@ -237,21 +229,21 @@ fn process_repository<'a>(

// Process releases.
let mut releases = vec![Release::default()];
let mut release_index = 0;
let mut previous_release = Release::default();
let mut first_processed_tag = None;
for git_commit in commits.iter().rev() {
let release = releases.last_mut().unwrap();
let commit = Commit::from(git_commit);
let commit_id = commit.id.to_string();
if args.sort == Sort::Newest {
releases[release_index].commits.insert(0, commit);
release.commits.insert(0, commit);
} else {
releases[release_index].commits.push(commit);
release.commits.push(commit);
}
if let Some(tag) = tags.get(&commit_id) {
releases[release_index].version = Some(tag.to_string());
releases[release_index].commit_id = Some(commit_id);
releases[release_index].timestamp = if args.tag.as_deref() == Some(tag) {
release.version = Some(tag.to_string());
release.commit_id = Some(commit_id);
release.timestamp = if args.tag.as_deref() == Some(tag) {
SystemTime::now()
.duration_since(UNIX_EPOCH)?
.as_secs()
Expand All @@ -263,36 +255,34 @@ fn process_repository<'a>(
first_processed_tag = Some(tag);
}
previous_release.previous = None;
releases[release_index].previous = Some(Box::new(previous_release));
previous_release = releases[release_index].clone();
release.previous = Some(Box::new(previous_release));
previous_release = release.clone();
releases.push(Release::default());
release_index += 1;
}
}

if release_index > 0 {
debug_assert!(!releases.is_empty());

if releases.len() > 1 {
previous_release.previous = None;
releases[release_index].previous = Some(Box::new(previous_release));
releases.last_mut().unwrap().previous = Some(Box::new(previous_release));
}

// Add custom commit messages to the latest release.
if let Some(custom_commits) = &args.with_commit {
if let Some(latest_release) = releases.iter_mut().last() {
custom_commits.iter().for_each(|message| {
latest_release
.commits
.push(Commit::from(message.to_string()))
});
}
releases
.last_mut()
.unwrap()
.commits
.extend(custom_commits.iter().cloned().map(Commit::from));
}

// Set the previous release if the first release does not have one set.
if !releases.is_empty() &&
releases
.first()
.and_then(|r| r.previous.as_ref())
.and_then(|p| p.version.as_ref())
.is_none()
if releases[0]
.previous
.as_ref()
.and_then(|p| p.version.as_ref())
.is_none()
{
// Get the previous tag of the first processed tag in the release loop.
let first_tag = first_processed_tag
Expand Down Expand Up @@ -532,6 +522,16 @@ pub fn run(mut args: Opt) -> Result<()> {
let mut changelog = Changelog::new(releases, &config)?;

// Print the result.
let mut out: Box<dyn io::Write> = if let Some(path) = &args.output {
if path == Path::new("-") {
Box::new(io::stdout())
} else {
Box::new(io::BufWriter::new(File::create(path)?))
}
} else {
Box::new(io::stdout())
};
let out = &mut *out;
if args.bump || args.bumped_version {
let next_version = if let Some(next_version) = changelog.bump_version()? {
next_version
Expand All @@ -544,40 +544,22 @@ pub fn run(mut args: Opt) -> Result<()> {
return Ok(());
};
if args.bumped_version {
if let Some(path) = args.output {
let mut output = File::create(path)?;
output.write_all(next_version.as_bytes())?;
} else {
println!("{next_version}");
}
out.write_all(next_version.as_bytes())?;
return Ok(());
}
}

if args.context {
return if let Some(path) = args.output {
let mut output = File::create(path)?;
changelog.write_context(&mut output)
} else {
changelog.write_context(&mut io::stdout())
};
changelog.write_context(out)?;
return Ok(());
}
if let Some(ref path) = args.prepend {
changelog.prepend(fs::read_to_string(path)?, &mut File::create(path)?)?;

if let Some(path) = &args.prepend {
changelog.prepend(fs::read_to_string(path)?, out)?;
}
if let Some(path) = args.output {
let mut output: Box<dyn Write> = if path == PathBuf::from("-") {
Box::new(io::stdout())
} else {
Box::new(File::create(path)?)
};
if args.context {
changelog.write_context(&mut output)
} else {
changelog.generate(&mut output)
}
} else if args.prepend.is_none() {
changelog.generate(&mut io::stdout())
} else {
Ok(())
if args.output.is_some() || args.prepend.is_none() {
changelog.generate(out)?;
}

Ok(())
}

0 comments on commit 1c090ea

Please sign in to comment.