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

feat: Introduce tag_regex option with smart default #692

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

robertschweizer
Copy link
Contributor

Description

Closes #519

CLI flag name: --tag-regex

Heavily inspired by #537, but extends it with a smart default value to exclude non-release tags. This was suggested in #519 (comment)

I separated commits to make review easier.

Checklist

  • Add test cases to all the changes you introduce
  • Run ./scripts/format and ./scripts/test locally to ensure this change passes linter check and test
  • Test the changes on the local machine manually
  • Update the documentation for the changes

Expected behavior

Steps to Test This Pull Request

Additional context

@codecov
Copy link

codecov bot commented Mar 23, 2023

Codecov Report

Patch coverage: 97.72% and project coverage change: +0.69 🎉

Comparison is base (f04a719) 97.35% compared to head (8eea9ea) 98.04%.

❗ Current head 8eea9ea differs from pull request most recent head e58e56f. Consider uploading reports for the commit e58e56f to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #692      +/-   ##
==========================================
+ Coverage   97.35%   98.04%   +0.69%     
==========================================
  Files          42       41       -1     
  Lines        2041     1742     -299     
==========================================
- Hits         1987     1708     -279     
+ Misses         54       34      -20     
Flag Coverage Δ
unittests 98.04% <97.72%> (+0.69%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
commitizen/bump.py 100.00% <ø> (ø)
commitizen/cli.py 93.93% <ø> (-0.27%) ⬇️
commitizen/commands/init.py 88.03% <66.66%> (+0.59%) ⬆️
commitizen/changelog.py 99.43% <100.00%> (-0.07%) ⬇️
commitizen/commands/bump.py 97.48% <100.00%> (-0.67%) ⬇️
commitizen/commands/changelog.py 98.94% <100.00%> (ø)
commitizen/defaults.py 100.00% <100.00%> (ø)
commitizen/git.py 98.61% <100.00%> (+0.01%) ⬆️
commitizen/tags.py 100.00% <100.00%> (ø)

... and 9 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

We've been using this default already in `normalize_tag`,
but setting this value in the settings dict is cleaner.
config_path and changelog_path rely on the modified CWD provided by
tmp_commitizen_project, so they should use this fixture.
Closes commitizen-tools#519

CLI flag name: --tag-regex

Heavily inspired by
commitizen-tools#537, but extends
it with a smart default value to exclude non-release tags. This was
suggested in
commitizen-tools#519 (comment)
@robertschweizer
Copy link
Contributor Author

It looks like these changes need some reworking because they partially duplicate what was done in 3a1af6a.

Would you merge this if I rework it? It didn't get a review before.

VersionProtocol = Any


def tag_from_version(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version_to_tag looks more straightforward to me

Suggested change
def tag_from_version(
def veresion_to_tag(

@@ -163,7 +164,9 @@ def get_tags(dateformat: str = "%Y-%m-%d") -> List[GitTag]:
for line in c.out.split("\n")[:-1]
]

return git_tags
filtered_git_tags = [t for t in git_tags if pattern.fullmatch(t.name)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can use git command to filter . but it's not requried.

@Lee-W
Copy link
Member

Lee-W commented Jun 23, 2023

Hi @robertschweizer sorry for the late reply. I briefly walk through it. I like the idea and will definitely take a look after reworked 🙂

@robertschweizer
Copy link
Contributor Author

Sorry, I don't have much time at the moment. I started rebasing the first commit and opened #775 to get that merged.

To continue here (not sure if I will find the time myself), it's important to consider:

  • My use-case is to ignore tags in the changelog that we don't care about when running cz bump --changelog. I probably don't need a tag_regex.
  • cz bump --changelog sets the --incremental flag. This finds the previous version with a different logic than cz bump. The first line matching version_schema's parser in the existing changelog is used as previous version.
    This version is then formatted with tag_format and searched for in existing tags with a similarity threshold of 0.89.
  • Suspected bugs in cz changelog:
    • Existing tags (get_tags() function) and --rev-range values are validated by constructing the configured version_scheme on them.
      This means it matches packaging.Version._regex instead of BaseVersion.parser.
      Both SemVer and Pep440 version providers thus use packaging's PEP440 version matcher here.
      • The two standards require different pattern matching
    • --incremental uses version_schema but ignores tag_format to search changelog.
      • Probably cz bump --changelog should pass the current (previous) release version explicitly, to make searching of the existing changelog unnecessary.
        • This would probably fix my use-case already.
      • It's probably rather restrictive to require the changelog to contain the used Git tag names.
  • ScmProvider should really use some smarter default regex matching when a non-standard tag_format is set. Maybe using placeholders other than $version in tag_format should be deprecated to make this easier. The $version string can now be configured using version_scheme.

@Lee-W @noirbizarre

@woile
Copy link
Member

woile commented Sep 14, 2023

Looks good, hopefully we can merge it soon

Copy link
Member

@noirbizarre noirbizarre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to go the right way.
I added a few suggestions.

self.tag_format = args.get("tag_format") or self.config.settings.get(
"tag_format"
self.tag_format: str = args.get("tag_format") or self.config.settings.get(
"tag_format", DEFAULT_SETTINGS["tag_format"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to inject default there, default are already loaded and overwritten by actual settings


Type: `str`

Default: Based on `tag_format`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Default: Based on `tag_format`
Default: Computed from `tag_format`

or

Suggested change
Default: Based on `tag_format`
Default: Extrapolated from `tag_format`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, can you remove this ?

@@ -140,7 +141,7 @@ def get_filenames_in_commit(git_reference: str = ""):
raise GitCommandError(c.err)


def get_tags(dateformat: str = "%Y-%m-%d") -> List[GitTag]:
def get_tags(dateformat: str = "%Y-%m-%d", *, pattern: re.Pattern) -> List[GitTag]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the pattern filtering optional:

Suggested change
def get_tags(dateformat: str = "%Y-%m-%d", *, pattern: re.Pattern) -> List[GitTag]:
def get_tags(dateformat: str = "%Y-%m-%d", *, pattern: re.Pattern | None = None) -> List[GitTag]:


from commitizen.tags import make_tag_pattern, tag_from_version

TAG_FORMATS: Dict[str, Dict[str, list]] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a failsafe, add a case for the minimal $version that should always work:

Suggested change
TAG_FORMATS: Dict[str, Dict[str, list]] = {
TAG_FORMATS: Dict[str, Dict[str, list]] = {
"$version": {
"tags_per_version": [
("1.2.3", "1.2.3"),
("1.2.3a2", "1.2.3a2"),
("1.2.3b2", "1.2.3b2"),
("1.2.3+1.0.0", "1.2.3+1.0.0"),
],
"invalid_tags": ["v1.2.3", "unknown-tag", "1-2-3", "ver1.0.0"]
},

@AlexeySanko
Copy link

Looks as great feature for monorepo. Any update on it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

generate changelog only from tags matching specific patterns
5 participants