From 03c54fff3c358876eb77b3e18fcf4b77900f0cad Mon Sep 17 00:00:00 2001 From: maimul Date: Sat, 30 May 2026 17:22:46 +0800 Subject: [PATCH] fix: add --ignore-missing-version flag for glob patterns (#267) When using glob patterns, some matched files may not contain the version string. Previously bumpversion would crash with VersionNotFoundException for any such file. This adds --ignore-missing-version (also settable via ignore_missing_version = True in config) which logs a warning and skips files that don't contain the expected version string. Fixes #267 --- bumpversion/cli.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/bumpversion/cli.py b/bumpversion/cli.py index d627d702..c81c3cc8 100644 --- a/bumpversion/cli.py +++ b/bumpversion/cli.py @@ -24,6 +24,7 @@ from bumpversion.exceptions import ( IncompleteVersionRepresentationException, MissingValueForSerializationException, + VersionNotFoundException, WorkingDirectoryIsDirtyException, ) @@ -121,7 +122,7 @@ def main(original_args=None): for file_name in (file_names or positionals[1:]) ) - _check_files_contain_version(files, current_version, context) + _check_files_contain_version(files, current_version, context, args.ignore_missing_version) _replace_version_in_files(files, current_version, new_version, args.dry_run, context) _log_list(config, args.new_version) @@ -294,7 +295,7 @@ def _load_configuration(config_file, explicit_config, defaults): except NoOptionError: pass # no default value then ;) - for boolvaluename in ("commit", "tag", "dry_run"): + for boolvaluename in ("commit", "tag", "dry_run", "ignore_missing_version"): try: defaults[boolvaluename] = config.getboolean( "bumpversion", boolvaluename @@ -473,6 +474,13 @@ def _parse_arguments_phase_3(remaining_argv, positionals, defaults, parser2): help="Version that needs to be updated", required="current_version" not in defaults, ) + parser3.add_argument( + "--ignore-missing-version", + action="store_true", + default=False, + dest="ignore_missing_version", + help="Ignore any missing version strings in files and skip those files.", + ) parser3.add_argument( "--no-configured-files", action="store_true", @@ -611,14 +619,20 @@ def _determine_vcs_dirty(possible_vcses, defaults): return None -def _check_files_contain_version(files, current_version, context): +def _check_files_contain_version(files, current_version, context, ignore_missing=False): # make sure files exist and contain version string logger.info( "Asserting files %s contain the version string...", ", ".join([str(f) for f in files]), ) for f in files: - f.should_contain_version(current_version, context) + try: + f.should_contain_version(current_version, context) + except VersionNotFoundException as e: + if ignore_missing: + logger.warning("Skipping file %s: %s", f, e) + else: + raise def _replace_version_in_files(files, current_version, new_version, dry_run, context):