Skip to content

Commit

Permalink
Merge branch 'minor-fixes' into pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt authored Nov 4, 2023
2 parents 7e5d1bc + 0034716 commit 3c7afb8
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 103 deletions.
57 changes: 28 additions & 29 deletions .github/workflows/bumpversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,6 @@ on:
types: [closed]
branches: [master]
workflow_dispatch:
inputs:
dry_run:
description: Don't actually do the work, just describe it
default: true
type: boolean
new_version:
description: Set the version to a specific value
required: false
type: string
verbose:
description: The amount of output detail
default: 0
type: choice
options:
- "0"
- "1"
- "2"

jobs:
bumpversion:
Expand All @@ -38,8 +21,7 @@ jobs:

- name: Install requirements
run: |
python -m pip install generate-changelog
python -m pip install -e .
python -m pip install generate-changelog bump-my-version
- name: Git check
run: |
Expand All @@ -55,27 +37,44 @@ jobs:
echo "::notice::Suggested release type is: ${RELEASE_KIND}"
echo "RELEASE_KIND=$RELEASE_KIND" >> $GITHUB_ENV
echo "release-kind=$RELEASE_KIND" >> $GITHUB_OUTPUT
echo "PACKAGE=false" >> $GITHUB_ENV
- name: Bump Version auto
if: ${{ github.event_name != 'workflow_dispatch' }}
shell: bash
run: |
if [[ $RELEASE_KIND != "no-release" ]]; then
bump-my-version -v "$RELEASE_KIND"
bump-my-version bump -v "$RELEASE_KIND"
git push
git push --tags
echo "PACKAGE=true" >> $GITHUB_ENV
fi
- name: Bump Version manual
if: ${{ github.event_name == 'workflow_dispatch' }}
shell: bash
env:
BUMPVERSION_DRY_RUN: ${{ inputs.dry_run }}
BUMPVERSION_NEW_VERSION: ${{ inputs.tags }}
BUMPVERSION_VERBOSE: ${{ inputs.verbose }}
run: |
bump-my-version -v "$RELEASE_KIND"
if [[ BUMPVERSION_DRY_RUN == "false" ]]; then
git push
git push --tags
fi
case "$RELEASE_KIND" in
major|minor|patch)
bump-my-version bump "$RELEASE_KIND"
if [[ BUMPVERSION_DRY_RUN == "false" ]]; then
git push
git push --tags
echo "PACKAGE=true" >> $GITHUB_ENV
fi
;;
dev)
bump-my-version bump -v --no-commit "$RELEASE_KIND"
echo "PACKAGE=true" >> $GITHUB_ENV
;;
esac
- name: Package
if: ${{ env.PACKAGE == 'true' }}
uses: hynek/build-and-inspect-python-package@v1

- name: Upload dev release to PyPI
if: ${{ env.PACKAGE == 'true' && env.RELEASE_KIND == 'dev' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
8 changes: 6 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,16 @@ jobs:
- name: Set dev version
shell: bash
run: |
bumpversion dev bumpversion/__init__.py --no-commit --no-configured-files -v
export PR_NUMBER=$(gh pr view --json number -q .number || echo "")
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
echo "::notice::PR_NUMBER is: ${PR_NUMBER}"
bump-my-version bump dev bumpversion/__init__.py --no-commit --no-configured-files -v
env:
GH_TOKEN: ${{ secrets.PAT }}

- name: Package
shell: bash
run: |
python -m pip install --disable-pip-version-check --no-python-version-warning build
python -m build
- name: Upload package to Test PyPI
Expand Down
20 changes: 10 additions & 10 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ on:

jobs:
# Package when a new tag is pushed
build-package:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.PAT }}

- name: Package
uses: hynek/build-and-inspect-python-package@v1
# build-package:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# with:
# fetch-depth: 0
# token: ${{ secrets.PAT }}
#
# - name: Package
# uses: hynek/build-and-inspect-python-package@v1

# Create a GitHub release
release:
Expand Down
3 changes: 2 additions & 1 deletion bumpversion/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def resolve_command(self, ctx: Context, args: List[str]) -> tuple:
original_args.remove("bump")
else:
print_warning(
"Calling bumpversion without a subcommand is deprecated. " "Please use `bumpversion bump` instead"
"Calling bumpversion without a subcommand is deprecated. "
"Please use `bump-my-version bump` instead"
)
return cmd.name, cmd, original_args
return cmd.name, cmd, args
25 changes: 13 additions & 12 deletions bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
context_settings={
"ignore_unknown_options": True,
"allow_interspersed_args": True,
"help_option_names": ["-h", "--help"],
},
add_help_option=False,
)
Expand Down Expand Up @@ -140,10 +141,10 @@ def cli(ctx: Context) -> None:
help="Template for complete string to replace",
)
@click.option(
"--no-regex",
is_flag=True,
envvar="BUMPVERSION_NO_REGEX",
help="Do not treat the search parameter as a regular expression",
"--regex/--no-regex",
default=False,
envvar="BUMPVERSION_REGEX",
help="Treat the search parameter as a regular expression or explicitly do not treat it as a regular expression.",
)
@click.option(
"--no-configured-files",
Expand Down Expand Up @@ -232,7 +233,7 @@ def bump(
serialize: Optional[List[str]],
search: Optional[str],
replace: Optional[str],
no_regex: bool,
regex: bool,
no_configured_files: bool,
ignore_missing_version: bool,
dry_run: bool,
Expand Down Expand Up @@ -276,7 +277,7 @@ def bump(
message=message,
commit_args=commit_args,
ignore_missing_version=ignore_missing_version,
no_regex=no_regex,
regex=regex,
)

found_config_file = find_config_file(config_file)
Expand Down Expand Up @@ -417,10 +418,10 @@ def show(args: List[str], config_file: Optional[str], format_: str, increment: O
help="Template for complete string to replace",
)
@click.option(
"--no-regex",
is_flag=True,
envvar="BUMPVERSION_NO_REGEX",
help="Do not treat the search parameter as a regular expression",
"--regex/--no-regex",
default=False,
envvar="BUMPVERSION_REGEX",
help="Treat the search parameter as a regular expression or explicitly do not treat it as a regular expression.",
)
@click.option(
"--no-configured-files",
Expand Down Expand Up @@ -455,7 +456,7 @@ def replace(
serialize: Optional[List[str]],
search: Optional[str],
replace: Optional[str],
no_regex: bool,
regex: bool,
no_configured_files: bool,
ignore_missing_version: bool,
dry_run: bool,
Expand Down Expand Up @@ -485,7 +486,7 @@ def replace(
message=None,
commit_args=None,
ignore_missing_version=ignore_missing_version,
no_regex=no_regex,
regex=regex,
)

found_config_file = find_config_file(config_file)
Expand Down
16 changes: 9 additions & 7 deletions bumpversion/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FileConfig(BaseModel):
serialize: Optional[List[str]] = None # If different from outer scope
search: Optional[str] = None # If different from outer scope
replace: Optional[str] = None # If different from outer scope
no_regex: Optional[bool] = None # If different from outer scope
regex: Optional[bool] = None # If different from outer scope
ignore_missing_version: Optional[bool] = None


Expand All @@ -55,7 +55,7 @@ class Config(BaseSettings):
serialize: List[str] = Field(min_length=1)
search: str
replace: str
no_regex: bool
regex: bool
ignore_missing_version: bool
tag: bool
sign_tags: bool
Expand Down Expand Up @@ -86,7 +86,7 @@ def add_files(self, filename: Union[str, List[str]]) -> None:
serialize=self.serialize,
search=self.search,
replace=self.replace,
no_regex=self.no_regex,
regex=self.regex,
ignore_missing_version=self.ignore_missing_version,
)
)
Expand All @@ -107,10 +107,12 @@ def resolved_filemap(self) -> Dict[str, FileConfig]:
def files_to_modify(self) -> List[FileConfig]:
"""Return a list of files to modify."""
files_not_excluded = [
file_cfg.filename for file_cfg in self.files if file_cfg.filename not in self.excluded_paths
file_cfg.filename
for file_cfg in self.resolved_filemap.values()
if file_cfg.filename not in self.excluded_paths
]
inclusion_set = set(self.included_paths) | set(files_not_excluded)
return [file_cfg for file_cfg in self.files if file_cfg.filename in inclusion_set]
return [file_cfg for file_cfg in self.resolved_filemap.values() if file_cfg.filename in inclusion_set]

@property
def version_config(self) -> "VersionConfig":
Expand All @@ -126,7 +128,7 @@ def version_config(self) -> "VersionConfig":
"serialize": ["{major}.{minor}.{patch}"],
"search": "{current_version}",
"replace": "{new_version}",
"no_regex": False,
"regex": False,
"ignore_missing_version": False,
"tag": False,
"sign_tags": False,
Expand Down Expand Up @@ -157,7 +159,7 @@ def get_all_file_configs(config_dict: dict) -> List[FileConfig]:
"search": config_dict["search"],
"replace": config_dict["replace"],
"ignore_missing_version": config_dict["ignore_missing_version"],
"no_regex": config_dict["no_regex"],
"regex": config_dict["regex"],
}
files = [{k: v for k, v in filecfg.items() if v is not None} for filecfg in config_dict["files"]]
for f in files:
Expand Down
27 changes: 14 additions & 13 deletions bumpversion/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
from copy import deepcopy
from difflib import context_diff
from typing import List, MutableMapping, Optional
from typing import List, MutableMapping, Optional, Tuple

from bumpversion.config import FileConfig
from bumpversion.exceptions import VersionNotFoundError
Expand All @@ -27,7 +27,7 @@ def __init__(
self.serialize = file_cfg.serialize or version_config.serialize_formats
self.search = search or file_cfg.search or version_config.search
self.replace = replace or file_cfg.replace or version_config.replace
self.no_regex = file_cfg.no_regex or False
self.regex = file_cfg.regex or False
self.ignore_missing_version = file_cfg.ignore_missing_version or False
self.version_config = VersionConfig(
self.parse, self.serialize, self.search, self.replace, version_config.part_configs
Expand Down Expand Up @@ -63,7 +63,7 @@ def contains_version(self, version: Version, context: MutableMapping) -> bool:
Returns:
True if the version number is in fact present.
"""
search_expression = self.get_search_pattern(context)
search_expression, raw_search_expression = self.get_search_pattern(context)

if self.contains(search_expression):
return True
Expand All @@ -84,7 +84,7 @@ def contains_version(self, version: Version, context: MutableMapping) -> bool:
# version not found
if self.ignore_missing_version:
return False
raise VersionNotFoundError(f"Did not find '{search_expression.pattern}' in file: '{self.path}'")
raise VersionNotFoundError(f"Did not find '{raw_search_expression}' in file: '{self.path}'")

def contains(self, search: re.Pattern) -> bool:
"""Does the work of the contains_version method."""
Expand Down Expand Up @@ -115,15 +115,15 @@ def replace_version(
if new_version:
context["new_version"] = self.version_config.serialize(new_version, context)

search_for = self.get_search_pattern(context)
search_for, raw_search_pattern = self.get_search_pattern(context)
replace_with = self.version_config.replace.format(**context)

file_content_after = search_for.sub(replace_with, file_content_before)

if file_content_before == file_content_after and current_version.original:
og_context = deepcopy(context)
og_context["current_version"] = current_version.original
search_for_og = self.get_search_pattern(og_context)
search_for_og, og_raw_search_pattern = self.get_search_pattern(og_context)
file_content_after = search_for_og.sub(replace_with, file_content_before)

if file_content_before != file_content_after:
Expand All @@ -147,25 +147,26 @@ def replace_version(
if not dry_run: # pragma: no-coverage
self.write_file_contents(file_content_after)

def get_search_pattern(self, context: MutableMapping) -> re.Pattern:
def get_search_pattern(self, context: MutableMapping) -> Tuple[re.Pattern, str]:
"""Compile and return the regex if it is valid, otherwise return the string."""
# the default search pattern is escaped, so we can still use it in a regex
default = re.compile(re.escape(self.version_config.search.format(**context)), re.MULTILINE | re.DOTALL)
if self.no_regex:
raw_pattern = self.version_config.search.format(**context)
default = re.compile(re.escape(raw_pattern), re.MULTILINE | re.DOTALL)
if not self.regex:
logger.debug("No RegEx flag detected. Searching for the default pattern: '%s'", default.pattern)
return default
return default, raw_pattern

re_context = {key: re.escape(str(value)) for key, value in context.items()}
regex_pattern = self.version_config.search.format(**re_context)
try:
search_for_re = re.compile(regex_pattern, re.MULTILINE | re.DOTALL)
logger.debug("Searching for the regex: '%s'", search_for_re.pattern)
return search_for_re
return search_for_re, raw_pattern
except re.error as e:
logger.error("Invalid regex '%s' for file %s: %s.", default, self.path, e)

logger.debug("Searching for the default pattern: '%s'", default.pattern)
return default
logger.debug("Searching for the default pattern: '%s'", raw_pattern)
return default, raw_pattern

def __str__(self) -> str: # pragma: no-coverage
return self.path
Expand Down
2 changes: 0 additions & 2 deletions bumpversion/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ def latest_tag_info(cls, tag_pattern: str) -> SCMInfo:

try:
# get info about the latest tag in git
# TODO: This only works if the tag name is prefixed with `v`.
# Should allow for the configured format for the tag name.
git_cmd = [
"git",
"describe",
Expand Down
2 changes: 1 addition & 1 deletion docsrc/reference/search-and-replace-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Bump-my-version uses a combination of [template strings](https://docs.python.org/3/library/string.html#format-string-syntax) using a [formatting context](formatting-context.md) and regular expressions to search the configured files for the old or current version and replace the text with the new version.

Bump My Version falls back to using a simple string search if the search template is not a valid regular expression or if the `no-regex` flag is `True`. The search template is always rendered using the formatting context. The basic logic is:
Bump My Version defaults to using a simple string search. If the search template is not a valid regular expression or if the `no-regex` flag is `True`. The search template is always rendered using the formatting context. The basic logic is:

1. Escape the formatting context for use in a regular expression.
2. Render the search string using the escaped formatting context.
Expand Down
Loading

0 comments on commit 3c7afb8

Please sign in to comment.