Skip to content

Commit

Permalink
feat(version-config): add option to disable 0.x.x versions
Browse files Browse the repository at this point in the history
  • Loading branch information
codejedi365 authored and relekang committed Mar 18, 2024
1 parent a040aa4 commit dedb3b7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions semantic_release/cli/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ def version( # noqa: C901
commit_parser=parser,
prerelease=prerelease,
major_on_zero=major_on_zero,
allow_zero_version=runtime.allow_zero_version,
)

if build_metadata:
Expand Down
3 changes: 3 additions & 0 deletions semantic_release/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class RawConfig(BaseModel):
commit_parser_options: Dict[str, Any] = {}
logging_use_named_masks: bool = False
major_on_zero: bool = True
allow_zero_version: bool = True
remote: RemoteConfig = RemoteConfig()
tag_format: str = "v{version}"
publish: PublishConfig = PublishConfig()
Expand Down Expand Up @@ -229,6 +230,7 @@ class RuntimeContext:
commit_parser: CommitParser[ParseResult, ParserOptions]
version_translator: VersionTranslator
major_on_zero: bool
allow_zero_version: bool
prerelease: bool
assets: List[str]
commit_author: Actor
Expand Down Expand Up @@ -414,6 +416,7 @@ def from_raw_config(
commit_parser=commit_parser,
version_translator=version_translator,
major_on_zero=raw.major_on_zero,
allow_zero_version=raw.allow_zero_version,
build_command=raw.build_command,
version_declarations=tuple(version_declarations),
hvcs_client=hvcs_client,
Expand Down
38 changes: 26 additions & 12 deletions semantic_release/version/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def _increment_version(
prerelease: bool,
prerelease_token: str,
major_on_zero: bool,
allow_zero_version: bool,
) -> Version:
"""
Using the given versions, along with a given `level_bump`, increment to
Expand All @@ -158,19 +159,29 @@ def _increment_version(
`latest_full_version_in_history`, correspondingly, is the latest full release which
is in this branch's history.
"""
local_vars = list(locals().items())
log.debug(
"_increment_version: %s", ", ".join(f"{k} = {v}" for k, v in locals().items())
"_increment_version: %s", ", ".join(f"{k} = {v}" for k, v in local_vars)
)
if not major_on_zero and latest_version.major == 0:
# if we are a 0.x.y release and have set `major_on_zero`,
# breaking changes should increment the minor digit
# Correspondingly, we reduce the level that we increment the
# version by.
log.debug(
"reducing version increment due to 0. version and major_on_zero=False"
)
if latest_version.major == 0:
if not allow_zero_version:
# Set up default version to be 1.0.0 if currently 0.x.x which means a commented
# breaking change is not required to bump to 1.0.0
log.debug(
"Bumping major version as 0.x.x versions are disabled because of allow_zero_version=False"
)
level_bump = LevelBump.MAJOR

elif not major_on_zero:
# if we are a 0.x.y release and have set `major_on_zero`,
# breaking changes should increment the minor digit
# Correspondingly, we reduce the level that we increment the
# version by.
log.debug(
"reducing version increment due to 0. version and major_on_zero=False"
)

level_bump = min(level_bump, LevelBump.MINOR)
level_bump = min(level_bump, LevelBump.MINOR)

if prerelease:
log.debug("prerelease=true")
Expand Down Expand Up @@ -261,6 +272,7 @@ def next_version(
commit_parser: CommitParser[ParseResult, ParserOptions],
prerelease: bool = False,
major_on_zero: bool = True,
allow_zero_version: bool = True,
) -> Version:
"""
Evaluate the history within `repo`, and based on the tags and commits in the repo
Expand Down Expand Up @@ -398,8 +410,9 @@ def next_version(
level_bump = max(parsed_levels, default=LevelBump.NO_RELEASE)
log.info("The type of the next release release is: %s", level_bump)
if level_bump is LevelBump.NO_RELEASE:
log.info("No release will be made")
return latest_version
if latest_version.major != 0 or allow_zero_version:
log.info("No release will be made")
return latest_version

return _increment_version(
latest_version=latest_version,
Expand All @@ -418,4 +431,5 @@ def next_version(
prerelease=prerelease,
prerelease_token=translator.prerelease_token,
major_on_zero=major_on_zero,
allow_zero_version=allow_zero_version,
)

0 comments on commit dedb3b7

Please sign in to comment.