Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commit_range to template context #1041

Open
jdrst opened this issue Feb 7, 2025 · 11 comments
Open

Add commit_range to template context #1041

jdrst opened this issue Feb 7, 2025 · 11 comments
Assignees
Labels
feature/request New feature or request good first issue Good for newcomers

Comments

@jdrst
Copy link

jdrst commented Feb 7, 2025

Hey,

i would like to add links to diffs of certain files to the changelog.
Is there a way to get the commit range git cliff uses or was called with (in my case git cliff commitA..commitB)? As far as i could see it's not part of the context.

Thanks

Copy link

welcome bot commented Feb 7, 2025

Thanks for opening your first issue at git-cliff! Be sure to follow the issue template! ⛰️

@orhun
Copy link
Owner

orhun commented Feb 20, 2025

Good idea! We simply need to add commit_range to Release struct and update the other parts accordingly. See #721 for an example.

Marking this as good-first-issue, would you be interested in contributing?

@orhun orhun added feature/request New feature or request good first issue Good for newcomers labels Feb 20, 2025
@orhun orhun changed the title Get commit range in context Add commit_range to template context Feb 20, 2025
@jdrst
Copy link
Author

jdrst commented Feb 21, 2025

i'll try to make some time next week and have a look.

@orhun
Copy link
Owner

orhun commented Feb 21, 2025

Perfect, assigned to you! Let me know if you need any assistance.

@jdrst
Copy link
Author

jdrst commented Mar 3, 2025

Hey,

i finally could make the time to have a look. Unless i'm overlooking something this might not be as straight forward as thought. Here are my observations:

Currently the commit range is passed when getting the commits for changelog creation, we don't have ownership of the repo there.
As far as i could tell, we might not even "know" the actual commits used, at that time, because they might be internal to the revwalker. What we (afterwards) do have, is a list of sorted commits that is produced. So it might be possible to add something here akin to

//...
	let first_commit = commits.first().unwrap().id();
	let last_commit = commits.last().unwrap().id();
	let commit_range =
		CommitRange::new(&first_commit.to_string(), &last_commit.to_string());
	for git_commit in commits.iter().rev() {
		let release = releases.last_mut().unwrap();
                //..
		release.commit_range = Some(commit_range.clone());
// ...

with

/// Commit range (from..to)
pub struct CommitRange {
	from: String,
	to:   String,
}

From what i could tell this is currently not easily testable though, right?

Let me know your thoughts.

@Cyclonit
Copy link
Contributor

Cyclonit commented Mar 3, 2025

Can you elaborate on the use case for this request? Do you want to generate a diff between a change's commit and the previous release?

@orhun
Copy link
Owner

orhun commented Mar 3, 2025

Do you want to generate a diff between a change's commit and the previous release?

I think yes... I cannot think of another use case.

As far as i could tell, we might not even "know" the actual commits used, at that time, because they might be internal to the revwalker.

Hmm yeah, good point.

let commit_range =
CommitRange::new(&first_commit.to_string(), &last_commit.to_string());

Yup, that looks good to me. I think that would be the most correct way of getting the range.

From what i could tell this is currently not easily testable though, right?

Can you elaborate this a bit? What do you want to do exactly?

@jdrst
Copy link
Author

jdrst commented Mar 4, 2025

Can you elaborate on the use case for this request? Do you want to generate a diff between a change's commit and the previous release?

yes, as stated:

i would like to add links to diffs of certain files to the changelog.

in my case this would be achieveable via the commit hashes

Can you elaborate this a bit? What do you want to do exactly?

my proposal would put the logic in fn process_repository - looking at references i couldn't find a test that runs that logic.
a quick check locally with an actual project produced results that were not what i was expecting and i'd like to look at it more isolated

@orhun
Copy link
Owner

orhun commented Mar 4, 2025

I think we can use test fixtures for testing this functionality in the first phase. And yes, I think process_repository is a good place to implement it 👍🏼

@jdrst
Copy link
Author

jdrst commented Mar 4, 2025

Ah, missed those. It looks like commits are created "on the fly" there. Any quick idea how i can deterministically create the same commit sha every time? Apply patches?
Or is there another way to put those in the expected.md?

I'll try to get a pr going some time soon.

@orhun
Copy link
Owner

orhun commented Mar 4, 2025

Good question. I think applying patches might work. Or we can install a tool like lucky_commit in CI to generate hashes like 1010101 etc. It might be a bit of an overkill though.

Edit: Just do this in commit.sh to use an already existing repo:

#!/usr/bin/env bash
set -e

git remote add origin https://github.com/orhun/git-cliff-readme-example
git pull origin master
git fetch --tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/request New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants