Repository: orhun/git-cliff
Pull request #858 (2024-09-15 19:54:06)

refactor(clippy): apply case_sensitive_file_extension_comparisons lint
## Description

Apply [case_sensitive_file_extension_comparisons](https://rust-lang.github.io/rust-clippy/master/index.html#/case_sensitive_file_extension_comparisons) clippy lint from the pedantic group.

## Motivation and Context

Some of the checks from pedantic group are very useful to apply into the project. I will select the useful ones, and apply each to own MR, for maintainer to easy see the changes and easier decided which he want to apply and which not.
I think it's useful to apply them every once in a while, but not to use them by default.

I use the refactor(clippy) format for my commits, which is omitted from the changelog report.

I also apply those changes to get more familiar with the project and later I will take some of the open issues.

## How Has This Been Tested?

I ran the specific lint checks and solved the problem until the linter no longer told me any issue.
Command:
```sh
cargo clippy --all-targets -- -D warnings -W clippy::case_sensitive_file_extension_comparisons
```

## Types of Changes

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation (no code change)
- [x] Refactor (refactoring production code)
- [ ] Other <!--- (provide information) -->

## Checklist:

- [x] My code follows the code style of this project.
- [x] I have updated the documentation accordingly.
- [x] I have formatted the code with [rustfmt](https://github.com/rust-lang/rustfmt).
- [x] I checked the lints with [clippy](https://github.com/rust-lang/rust-clippy).
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.


codecov-commenter: :warning: Please install the !['codecov app svg image'](https://github.com/codecov/engineering-team/assets/152432831/e90313f4-9d3a-4b63-8b54-cfe14e7ec20d) to ensure uploads and comments are reliably processed by Codecov.

## [Codecov](https://app.codecov.io/gh/orhun/git-cliff/pull/858?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z) Report
Attention: Patch coverage is `0%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 40.27%. Comparing base [(`1d8a2fe`)](https://app.codecov.io/gh/orhun/git-cliff/commit/1d8a2fe84917d7bc7c0cb26afc559198ad806184?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z) to head [(`63a0e2f`)](https://app.codecov.io/gh/orhun/git-cliff/commit/63a0e2fd796d3f18b91bd84fcd7b6313a4c847bd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z).

| [Files with missing lines](https://app.codecov.io/gh/orhun/git-cliff/pull/858?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z) | Patch % | Lines |
|---|---|---|
| [git-cliff-core/src/embed.rs](https://app.codecov.io/gh/orhun/git-cliff/pull/858?src=pr&el=tree&filepath=git-cliff-core%2Fsrc%2Fembed.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z#diff-Z2l0LWNsaWZmLWNvcmUvc3JjL2VtYmVkLnJz) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/orhun/git-cliff/pull/858?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z) |

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #858      +/-   ##
==========================================
- Coverage   40.29%   40.27%   -0.02%     
==========================================
  Files          21       21              
  Lines        1663     1664       +1     
==========================================
  Hits          670      670              
- Misses        993      994       +1     
```

| [Flag](https://app.codecov.io/gh/orhun/git-cliff/pull/858/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z) | Coverage Δ | |
|---|---|---|
| [unit-tests](https://app.codecov.io/gh/orhun/git-cliff/pull/858/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z) | `40.27% <0.00%> (-0.02%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z#carryforward-flags-in-the-pull-request-comment) to find out more.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/orhun/git-cliff/pull/858?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z).


Patch:
diff --git a/git-cliff-core/src/changelog.rs b/git-cliff-core/src/changelog.rs
index 0641ee6..36f1f5e 100644
--- a/git-cliff-core/src/changelog.rs
+++ b/git-cliff-core/src/changelog.rs
@@ -55,12 +55,16 @@ impl<'a> Changelog<'a> {
 		Ok(Self {
 			releases,
 			header_template: match &config.changelog.header {
-				Some(header) => Some(Template::new(header.to_string(), trim)?),
+				Some(header) => {
+					Some(Template::new("header", header.to_string(), trim)?)
+				}
 				None => None,
 			},
 			body_template: get_body_template(config, trim)?,
 			footer_template: match &config.changelog.footer {
-				Some(footer) => Some(Template::new(footer.to_string(), trim)?),
+				Some(footer) => {
+					Some(Template::new("footer", footer.to_string(), trim)?)
+				}
 				None => None,
 			},
 			config,
@@ -154,7 +158,7 @@ impl<'a> Changelog<'a> {
 			.rev()
 			.filter(|release| {
 				if release.commits.is_empty() {
-					if let Some(version) = release.version.as_ref().cloned() {
+					if let Some(version) = release.version.clone() {
 						trace!("Release doesn't have any commits: {}", version);
 					}
 					false
@@ -612,7 +616,7 @@ fn get_body_template(config: &Config, trim: bool) -> Result<Template> {
 		.as_deref()
 		.unwrap_or_default()
 		.to_string();
-	let template = Template::new(template_str, trim)?;
+	let template = Template::new("body", template_str, trim)?;
 	let deprecated_vars = [
 		"commit.github",
 		"commit.gitea",
diff --git a/git-cliff-core/src/commit.rs b/git-cliff-core/src/commit.rs
index 3ff83ca..19b9a0c 100644
--- a/git-cliff-core/src/commit.rs
+++ b/git-cliff-core/src/commit.rs
@@ -347,11 +347,9 @@ impl Commit<'_> {
 							}
 							value
 						};
-						self.group =
-							parser.group.as_ref().cloned().map(regex_replace);
-						self.scope =
-							parser.scope.as_ref().cloned().map(regex_replace);
-						self.default_scope = parser.default_scope.as_ref().cloned();
+						self.group = parser.group.clone().map(regex_replace);
+						self.scope = parser.scope.clone().map(regex_replace);
+						self.default_scope = parser.default_scope.clone();
 						return Ok(self);
 					}
 				}
diff --git a/git-cliff-core/src/embed.rs b/git-cliff-core/src/embed.rs
index 0cf2d29..79450df 100644
--- a/git-cliff-core/src/embed.rs
+++ b/git-cliff-core/src/embed.rs
@@ -4,6 +4,7 @@ use crate::error::{
 	Result,
 };
 use rust_embed::RustEmbed;
+use std::path::Path;
 use std::str;
 
 /// Default configuration file embedder/extractor.
@@ -44,7 +45,10 @@ pub struct BuiltinConfig;
 impl BuiltinConfig {
 	/// Extracts the embedded content.
 	pub fn get_config(mut name: String) -> Result<String> {
-		if !name.ends_with(".toml") {
+		if !Path::new(&name)
+			.extension()
+			.map_or(false, |ext| ext.eq_ignore_ascii_case("toml"))
+		{
 			name = format!("{name}.toml");
 		}
 		let contents = match Self::get(&name) {
diff --git a/git-cliff-core/src/template.rs b/git-cliff-core/src/template.rs
index 6a14946..4cbe496 100644
--- a/git-cliff-core/src/template.rs
+++ b/git-cliff-core/src/template.rs
@@ -22,6 +22,9 @@ use tera::{
 /// Wrapper for [`Tera`].
 #[derive(Debug)]
 pub struct Template {
+	/// Template name.
+	name:          String,
+	/// Internal Tera instance.
 	tera:          Tera,
 	/// Template variables.
 	#[cfg_attr(not(feature = "github"), allow(dead_code))]
@@ -30,16 +33,16 @@ pub struct Template {
 
 impl Template {
 	/// Constructs a new instance.
-	pub fn new(mut template: String, trim: bool) -> Result<Self> {
+	pub fn new(name: &str, mut content: String, trim: bool) -> Result<Self> {
 		if trim {
-			template = template
+			content = content
 				.lines()
 				.map(|v| v.trim())
 				.collect::<Vec<&str>>()
 				.join("\n");
 		}
 		let mut tera = Tera::default();
-		if let Err(e) = tera.add_raw_template("template", &template) {
+		if let Err(e) = tera.add_raw_template(name, &content) {
 			return if let Some(error_source) = e.source() {
 				Err(Error::TemplateParseError(error_source.to_string()))
 			} else {
@@ -48,7 +51,8 @@ impl Template {
 		}
 		tera.register_filter("upper_first", Self::upper_first_filter);
 		Ok(Self {
-			variables: Self::get_template_variables(&tera)?,
+			name: name.to_string(),
+			variables: Self::get_template_variables(name, &tera)?,
 			tera,
 		})
 	}
@@ -129,13 +133,13 @@ impl Template {
 	}
 
 	/// Returns the variable names that are used in the template.
-	fn get_template_variables(tera: &Tera) -> Result<Vec<String>> {
+	fn get_template_variables(name: &str, tera: &Tera) -> Result<Vec<String>> {
 		let mut variables = HashSet::new();
-		let ast = &tera.get_template("template")?.ast;
+		let ast = &tera.get_template(name)?.ast;
 		for node in ast {
 			Self::find_identifiers(node, &mut variables);
 		}
-		trace!("Template variables: {variables:?}");
+		trace!("Template variables for {name}: {variables:?}");
 		Ok(variables.into_iter().collect())
 	}
 
@@ -159,7 +163,7 @@ impl Template {
 				context.insert(key.clone(), &value);
 			}
 		}
-		match self.tera.render("template", &context) {
+		match self.tera.render(&self.name, &context) {
 			Ok(mut v) => {
 				for postprocessor in postprocessors {
 					postprocessor.replace(&mut v, vec![])?;
@@ -242,7 +246,7 @@ mod test {
 		### {{ commit.group }}
 		- {{ commit.message | upper_first }}
 		{% endfor %}"#;
-		let mut template = Template::new(template.to_string(), false)?;
+		let mut template = Template::new("test", template.to_string(), false)?;
 		let release = get_fake_release_data();
 		assert_eq!(
 			"\n\t\t## 1.0 - 2023\n\t\t\n\t\t### feat\n\t\t- Add xyz\n\t\t\n\t\t### \
@@ -281,7 +285,7 @@ mod test {
 		let template = r#"
 		##  {{ version }}
 		"#;
-		let template = Template::new(template.to_string(), true)?;
+		let template = Template::new("test", template.to_string(), true)?;
 		let release = get_fake_release_data();
 		assert_eq!(
 			"\n##  1.0\n",
@@ -300,7 +304,7 @@ mod test {
 		let template =
 			"{% set hello_variable = 'hello' %}{{ hello_variable | upper_first }}";
 		let release = get_fake_release_data();
-		let template = Template::new(template.to_string(), true)?;
+		let template = Template::new("test", template.to_string(), true)?;
 		let r = template.render(
 			&release,
 			Option::<HashMap<&str, String>>::None.as_ref(),


Test patch:
diff --git a/git-cliff-core/tests/integration_test.rs b/git-cliff-core/tests/integration_test.rs
index 6952eac..45add26 100644
--- a/git-cliff-core/tests/integration_test.rs
+++ b/git-cliff-core/tests/integration_test.rs
@@ -261,7 +261,7 @@ fn generate_changelog() -> Result<()> {
 	];
 
 	let out = &mut String::new();
-	let template = Template::new(changelog_config.body.unwrap(), false)?;
+	let template = Template::new("test", changelog_config.body.unwrap(), false)?;
 
 	writeln!(out, "{}", changelog_config.header.unwrap()).unwrap();
 	for release in releases {