Skip to content

Commit 8722a0f

Browse files
committed
Added replace subcommand.
- Works just like `bump` but - doesn't do any version incrementing - Will not change the configuration file - Will not commit or tag - Can use `bumpversion show new_version --increment <versionpart>` to see what the new version would be
1 parent 59b2e41 commit 8722a0f

File tree

2 files changed

+156
-2
lines changed

2 files changed

+156
-2
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ repos:
4343
hooks:
4444
- id: mypy
4545
args: [--no-strict-optional, --ignore-missing-imports]
46-
additional_dependencies: ["pydantic", "toml", "types-all"]
46+
additional_dependencies: ["pydantic<2.0", "toml", "types-all"]
4747
- repo: https://github.com/terrencepreilly/darglint
4848
rev: v1.8.1
4949
hooks:

bumpversion/cli.py

+155-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from bumpversion.aliases import AliasedGroup
1010
from bumpversion.bump import do_bump
1111
from bumpversion.config import find_config_file, get_configuration
12+
from bumpversion.files import modify_files, resolve_file_config
1213
from bumpversion.logging import setup_logging
1314
from bumpversion.show import do_show, log_list
14-
from bumpversion.utils import get_overrides
15+
from bumpversion.utils import get_context, get_overrides
1516

1617
logger = logging.getLogger(__name__)
1718

@@ -290,3 +291,156 @@ def show(args: List[str], config_file: Optional[str], format_: str, increment: O
290291
do_show("all", config=config, format_=format_, increment=increment)
291292
else:
292293
do_show(*args, config=config, format_=format_, increment=increment)
294+
295+
296+
@cli.command()
297+
@click.argument("files", nargs=-1, type=str)
298+
@click.option(
299+
"--config-file",
300+
metavar="FILE",
301+
required=False,
302+
envvar="BUMPVERSION_CONFIG_FILE",
303+
type=click.Path(exists=True),
304+
help="Config file to read most of the variables from.",
305+
)
306+
@click.option(
307+
"-v",
308+
"--verbose",
309+
count=True,
310+
required=False,
311+
envvar="BUMPVERSION_VERBOSE",
312+
help="Print verbose logging to stderr. Can specify several times for more verbosity.",
313+
)
314+
@click.option(
315+
"--allow-dirty/--no-allow-dirty",
316+
default=None,
317+
required=False,
318+
envvar="BUMPVERSION_ALLOW_DIRTY",
319+
help="Don't abort if working directory is dirty, or explicitly abort if dirty.",
320+
)
321+
@click.option(
322+
"--current-version",
323+
metavar="VERSION",
324+
required=False,
325+
envvar="BUMPVERSION_CURRENT_VERSION",
326+
help="Version that needs to be updated",
327+
)
328+
@click.option(
329+
"--new-version",
330+
metavar="VERSION",
331+
required=False,
332+
envvar="BUMPVERSION_NEW_VERSION",
333+
help="New version that should be in the files",
334+
)
335+
@click.option(
336+
"--parse",
337+
metavar="REGEX",
338+
required=False,
339+
envvar="BUMPVERSION_PARSE",
340+
help="Regex parsing the version string",
341+
)
342+
@click.option(
343+
"--serialize",
344+
metavar="FORMAT",
345+
multiple=True,
346+
required=False,
347+
envvar="BUMPVERSION_SERIALIZE",
348+
help="How to format what is parsed back to a version",
349+
)
350+
@click.option(
351+
"--search",
352+
metavar="SEARCH",
353+
required=False,
354+
envvar="BUMPVERSION_SEARCH",
355+
help="Template for complete string to search",
356+
)
357+
@click.option(
358+
"--replace",
359+
metavar="REPLACE",
360+
required=False,
361+
envvar="BUMPVERSION_REPLACE",
362+
help="Template for complete string to replace",
363+
)
364+
@click.option(
365+
"--no-configured-files",
366+
is_flag=True,
367+
envvar="BUMPVERSION_NO_CONFIGURED_FILES",
368+
help=(
369+
"Only replace the version in files specified on the command line, "
370+
"ignoring the files from the configuration file."
371+
),
372+
)
373+
@click.option(
374+
"--dry-run",
375+
"-n",
376+
is_flag=True,
377+
envvar="BUMPVERSION_DRY_RUN",
378+
help="Don't write any files, just pretend.",
379+
)
380+
def replace(
381+
files: list,
382+
config_file: Optional[str],
383+
verbose: int,
384+
allow_dirty: Optional[bool],
385+
current_version: Optional[str],
386+
new_version: Optional[str],
387+
parse: Optional[str],
388+
serialize: Optional[List[str]],
389+
search: Optional[str],
390+
replace: Optional[str],
391+
no_configured_files: bool,
392+
dry_run: bool,
393+
) -> None:
394+
"""
395+
Replace the version in files.
396+
397+
FILES are additional file(s) to modify.
398+
If you want to rewrite only files specified on the command line, use with the
399+
`--no-configured-files` option.
400+
"""
401+
setup_logging(verbose)
402+
403+
logger.info("Starting BumpVersion %s", __version__)
404+
405+
overrides = get_overrides(
406+
allow_dirty=allow_dirty,
407+
current_version=current_version,
408+
new_version=new_version,
409+
parse=parse,
410+
serialize=serialize or None,
411+
search=search,
412+
replace=replace,
413+
commit=False,
414+
tag=False,
415+
sign_tags=False,
416+
tag_name=None,
417+
tag_message=None,
418+
message=None,
419+
commit_args=None,
420+
)
421+
422+
found_config_file = find_config_file(config_file)
423+
config = get_configuration(found_config_file, **overrides)
424+
425+
config.allow_dirty = allow_dirty if allow_dirty is not None else config.allow_dirty
426+
if not config.allow_dirty and config.scm_info.tool:
427+
config.scm_info.tool.assert_nondirty()
428+
429+
if no_configured_files:
430+
config.files = []
431+
432+
if files:
433+
config.add_files(files)
434+
435+
version = config.version_config.parse(config.current_version)
436+
437+
if new_version:
438+
next_version = config.version_config.parse(new_version)
439+
else:
440+
next_version = None
441+
442+
ctx = get_context(config, version, next_version)
443+
444+
configured_files = resolve_file_config(config.files, config.version_config)
445+
446+
modify_files(configured_files, version, next_version, ctx, dry_run)

0 commit comments

Comments
 (0)