Repository: orhun/git-cliff
Pull request #721 (2024-06-19 22:26:57)

feat(context): add repository path to template context
## Description

You can now use `{{ repository }}` variable in the template to get the repository path.

This is especially useful when you use `git-cliff` with multiple repositories. (e.g. `git-cliff -r repo1 -r repo2`)

## Motivation and Context

see #685

## How Has This Been Tested?

Unit tests.

## Types of Changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation (no code change)
- [ ] Refactor (refactoring production code)
- [ ] Other <!--- (provide information) -->

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [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.

<!--- Thank you for contributing to git-cliff! ⛰️  -->


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/orhun/git-cliff/pull/721?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 `50.00000%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 36.51%. Comparing base [(`94891c2`)](https://app.codecov.io/gh/orhun/git-cliff/commit/94891c2c27b1f46cb840fbea1e167c161609cbcd?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 [(`e5b31a2`)](https://app.codecov.io/gh/orhun/git-cliff/commit/e5b31a26e501294cd5c32388af1739cdb14e8a9e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z).
> Report is 3 commits behind head on main.

| [Files](https://app.codecov.io/gh/orhun/git-cliff/pull/721?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/src/lib.rs](https://app.codecov.io/gh/orhun/git-cliff/pull/721?src=pr&el=tree&filepath=git-cliff%2Fsrc%2Flib.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z#diff-Z2l0LWNsaWZmL3NyYy9saWIucnM=) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/orhun/git-cliff/pull/721?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     #721      +/-   ##
==========================================
- Coverage   36.59%   36.51%   -0.07%     
==========================================
  Files          19       19              
  Lines        1468     1471       +3     
==========================================
  Hits          537      537              
- Misses        931      934       +3     
```

| [Flag](https://app.codecov.io/gh/orhun/git-cliff/pull/721/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/721/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Orhun+Parmaks%C4%B1z) | `36.51% <50.00%> (-0.07%)` | :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/721?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 5680d49..c6a84d9 100644
--- a/git-cliff-core/src/changelog.rs
+++ b/git-cliff-core/src/changelog.rs
@@ -613,7 +613,7 @@ mod test {
 				header:         Some(String::from("# Changelog")),
 				body:           Some(String::from(
 					r#"{% if version %}
-				## Release [{{ version }}] - {{ timestamp | date(format="%Y-%m-%d") }}
+				## Release [{{ version }}] - {{ timestamp | date(format="%Y-%m-%d") }} - ({{ repository }})
 				{% if commit_id %}({{ commit_id }}){% endif %}{% else %}
 				## Unreleased{% endif %}
 				{% for group, commits in commits | group_by(attribute="group") %}
@@ -881,6 +881,7 @@ mod test {
 			commit_id: Some(String::from("0bc123")),
 			timestamp: 50000000,
 			previous: None,
+			repository: Some(String::from("/root/repo")),
 			#[cfg(feature = "github")]
 			github: crate::remote::RemoteReleaseMetadata {
 				contributors: vec![],
@@ -941,6 +942,7 @@ mod test {
 				commit_id: None,
 				timestamp: 1000,
 				previous: Some(Box::new(test_release)),
+				repository: Some(String::from("/root/repo")),
 				#[cfg(feature = "github")]
 				github: crate::remote::RemoteReleaseMetadata {
 					contributors: vec![],
@@ -974,7 +976,7 @@ mod test {
 			String::from(
 				r#"# Changelog
 
-			## Release [v1.1.0] - 1970-01-01
+			## Release [v1.1.0] - 1970-01-01 - (/root/repo)
 
 
 			### Bug Fixes
@@ -992,7 +994,7 @@ mod test {
 			#### ui
 			- do exciting stuff
 
-			## Release [v1.0.0] - 1971-08-02
+			## Release [v1.0.0] - 1971-08-02 - (/root/repo)
 			(0bc123)
 
 			### Bug Fixes
@@ -1117,7 +1119,7 @@ chore(deps): fix broken deps
 			#### app
 			- merge #5
 
-			## Release [v1.0.0] - 1971-08-02
+			## Release [v1.0.0] - 1971-08-02 - (/root/repo)
 			(0bc123)
 
 			### Bug Fixes
diff --git a/git-cliff-core/src/release.rs b/git-cliff-core/src/release.rs
index 05720d2..9ee2383 100644
--- a/git-cliff-core/src/release.rs
+++ b/git-cliff-core/src/release.rs
@@ -20,30 +20,32 @@ use serde::{
 #[serde(rename_all = "camelCase")]
 pub struct Release<'a> {
 	/// Release version, git tag.
-	pub version:   Option<String>,
+	pub version:    Option<String>,
 	/// git tag's message.
-	pub message:   Option<String>,
+	pub message:    Option<String>,
 	/// Commits made for the release.
-	pub commits:   Vec<Commit<'a>>,
+	pub commits:    Vec<Commit<'a>>,
 	/// Commit ID of the tag.
 	#[serde(rename = "commit_id")]
-	pub commit_id: Option<String>,
+	pub commit_id:  Option<String>,
 	/// Timestamp of the release in seconds, from epoch.
-	pub timestamp: i64,
+	pub timestamp:  i64,
 	/// Previous release.
-	pub previous:  Option<Box<Release<'a>>>,
+	pub previous:   Option<Box<Release<'a>>>,
+	/// Repository path.
+	pub repository: Option<String>,
 	/// Contributors.
 	#[cfg(feature = "github")]
-	pub github:    RemoteReleaseMetadata,
+	pub github:     RemoteReleaseMetadata,
 	/// Contributors.
 	#[cfg(feature = "gitlab")]
-	pub gitlab:    RemoteReleaseMetadata,
+	pub gitlab:     RemoteReleaseMetadata,
 	/// Contributors.
 	#[cfg(feature = "gitea")]
-	pub gitea:     RemoteReleaseMetadata,
+	pub gitea:      RemoteReleaseMetadata,
 	/// Contributors.
 	#[cfg(feature = "bitbucket")]
-	pub bitbucket: RemoteReleaseMetadata,
+	pub bitbucket:  RemoteReleaseMetadata,
 }
 
 #[cfg(feature = "github")]
@@ -187,6 +189,7 @@ mod test {
 					version: Some(String::from(version)),
 					..Default::default()
 				})),
+				repository: Some(String::from("/root/repo")),
 				#[cfg(feature = "github")]
 				github: crate::remote::RemoteReleaseMetadata {
 					contributors: vec![],
@@ -395,6 +398,7 @@ mod test {
 				version: Some(String::from("1.0.0")),
 				..Default::default()
 			})),
+			repository: Some(String::from("/root/repo")),
 			github: RemoteReleaseMetadata {
 				contributors: vec![],
 			},
@@ -681,6 +685,7 @@ mod test {
 				version: Some(String::from("1.0.0")),
 				..Default::default()
 			})),
+			repository: Some(String::from("/root/repo")),
 			#[cfg(feature = "github")]
 			github: RemoteReleaseMetadata {
 				contributors: vec![],
@@ -1025,6 +1030,7 @@ mod test {
 				version: Some(String::from("1.0.0")),
 				..Default::default()
 			})),
+			repository: Some(String::from("/root/repo")),
 			#[cfg(feature = "github")]
 			github: RemoteReleaseMetadata {
 				contributors: vec![],
diff --git a/git-cliff-core/src/repo.rs b/git-cliff-core/src/repo.rs
index 50645c8..25d9a23 100644
--- a/git-cliff-core/src/repo.rs
+++ b/git-cliff-core/src/repo.rs
@@ -32,7 +32,9 @@ static TAG_SIGNATURE_REGEX: Lazy<Regex> = lazy_regex!(
 ///
 /// [`Repository`]: GitRepository
 pub struct Repository {
-	inner: GitRepository,
+	inner:    GitRepository,
+	/// Repository path.
+	pub path: PathBuf,
 }
 
 impl Repository {
@@ -40,7 +42,8 @@ impl Repository {
 	pub fn init(path: PathBuf) -> Result<Self> {
 		if path.exists() {
 			Ok(Self {
-				inner: GitRepository::open(path)?,
+				inner: GitRepository::open(&path)?,
+				path,
 			})
 		} else {
 			Err(Error::IoError(io::Error::new(
diff --git a/git-cliff-core/src/template.rs b/git-cliff-core/src/template.rs
index 3fe0e8a..6d2e611 100644
--- a/git-cliff-core/src/template.rs
+++ b/git-cliff-core/src/template.rs
@@ -206,6 +206,7 @@ mod test {
 			commit_id: None,
 			timestamp: 0,
 			previous: None,
+			repository: Some(String::from("/root/repo")),
 			#[cfg(feature = "github")]
 			github: crate::remote::RemoteReleaseMetadata {
 				contributors: vec![],
diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs
index 6fc8e8c..a42da54 100644
--- a/git-cliff/src/lib.rs
+++ b/git-cliff/src/lib.rs
@@ -245,6 +245,8 @@ fn process_repository<'a>(
 	for git_commit in commits.iter().rev() {
 		let commit = Commit::from(git_commit);
 		let commit_id = commit.id.to_string();
+		releases[release_index].repository =
+			Some(repository.path.to_string_lossy().to_string());
 		if args.sort == Sort::Newest {
 			releases[release_index].commits.insert(0, commit);
 		} else {
diff --git a/website/docs/templating/context.md b/website/docs/templating/context.md
index 9d61db8..c979659 100644
--- a/website/docs/templating/context.md
+++ b/website/docs/templating/context.md
@@ -62,6 +62,7 @@ following context is generated to use for templating:
   ],
   "commit_id": "a440c6eb26404be4877b7e3ad592bfaa5d4eb210 (release commit)",
   "timestamp": 1625169301,
+  "repository": "/path/to/repository",
   "previous": {
     "version": "previous release"
   }
@@ -156,6 +157,7 @@ If [`conventional_commits`](/docs/configuration/git#conventional_commits) is set
   ],
   "commit_id": "a440c6eb26404be4877b7e3ad592bfaa5d4eb210 (release commit)",
   "timestamp": 1625169301,
+  "repository": "/path/to/repository",
   "previous": {
     "version": "previous release"
   }
diff --git a/website/docs/usage/multiple-repos.md b/website/docs/usage/multiple-repos.md
index d6fe294..0e4a6bd 100644
--- a/website/docs/usage/multiple-repos.md
+++ b/website/docs/usage/multiple-repos.md
@@ -11,3 +11,11 @@ git cliff --repository path1 path2
 ```
 
 Note that the changelog will be generated using the merged history of the given repositories.
+
+:::tip
+
+You can use the `{{ repository }}` variable in the template to display which release belongs to which repository.
+
+See [context](/docs/templating/context) for more information.
+
+:::


Test patch:
diff --git a/git-cliff-core/tests/integration_test.rs b/git-cliff-core/tests/integration_test.rs
index 3e960fa..47f2791 100644
--- a/git-cliff-core/tests/integration_test.rs
+++ b/git-cliff-core/tests/integration_test.rs
@@ -194,6 +194,7 @@ fn generate_changelog() -> Result<()> {
 			commit_id: None,
 			timestamp: 0,
 			previous:  None,
+			repository: Some(String::from("/root/repo")),
 			#[cfg(feature = "github")]
 			github: git_cliff_core::remote::RemoteReleaseMetadata {
 				contributors: vec![],
@@ -235,6 +236,7 @@ fn generate_changelog() -> Result<()> {
 			commit_id: None,
 			timestamp: 0,
 			previous:  None,
+			repository: Some(String::from("/root/repo")),
 			#[cfg(feature = "github")]
 			github: git_cliff_core::remote::RemoteReleaseMetadata {
 				contributors: vec![],