peritus / bumpversion

Version-bump your software with a single command
https://pypi.python.org/pypi/bumpversion
MIT License
1.5k stars 147 forks source link

Multi-line `replace` doesn't work for lines starting with `#` #199

Open marionlb opened 5 years ago

marionlb commented 5 years ago

Using python 3.6 and bumpversion v0.5.3.

Great tool!

Summary

  1. There seems to be no way to escape the # character to avoid the line being treated as a comment
  2. Using \n in the replace field does not replace the sequence with a newline but with the literal \n

Detail

The following blurb in .bumpversion.cfg should replace the ## vNEXT header in the CHANGELOG.md file with the same header, a new line and a ## v{new_version} line.

[bumpversion:file:CHANGELOG.md]
search = ## vNEXT
replace = ## vNEXT 

  ## v{new_version}

However the CHANGELOG file is not modified and the 2nd and 3rd lines of the replace field in the .bumpversion.cfg file are deleted.

The same .bumpversion.cfg section after running bumpversion minor --no-tag:

[bumpversion:file:CHANGELOG.md]
search = ## vNEXT
replace = ## vNEXT

It's not a multi-line problem since using * instead of # works as expected, e.g. with the following:

[bumpversion:file:CHANGELOG.md]
search = ## vNEXT
replace = ## vNEXT 

  **v{new_version}**

I've tried escaping the # using \# to but that writes the actual \#\# characters in the CHANGELOG.

replace = ## vNEXT

  \#\# v{new_version}

I've tried using a single-line replace with \n for the new line but as before, the actual character \n is what appears in the CHANGELOG.

replace = ## vNEXT \n## v{new_version}

I've tried using {\n} instead but that just raises a KeyError: '\\n'.

Full log:

$ bumpversion --verbose minor --no-tag
Reading config file .bumpversion.cfg:
[bumpversion]
commit = True
tag = True
current_version = 3.6-dev
parse = (?P<major>\d+)\.(?P<minor>\d+)(\-(?P<release>[a-z]+))?
serialize =
    {major}.{minor}-{release}
    {major}.{minor}

[bumpversion:file:src/__init__.py]

[bumpversion:file:CHANGELOG.md]
search = ## vNEXT
replace = ## vNEXT

  ## v{new_version}

[bumpversion:part:release]
optional_value = gamma
values =
    dev
    gamma

Parsing version '3.6-dev' using regexp '(?P<major>\d+)\.(?P<minor>\d+)(\-(?P<release>[a-z]+))?'
Parsed the following values: major=3, minor=6, release=dev
Attempting to increment part 'minor'
Values are now: major=3, minor=7, release=dev
Parsing version '3.7-dev' using regexp '(?P<major>\d+)\.(?P<minor>\d+)(\-(?P<release>[a-z]+))?'
Parsed the following values: major=3, minor=7, release=dev
New version will be '3.7-dev'
Asserting files src/__init__.py, CHANGELOG.md contain the version string:
Found '3.6-dev' in src/__init__.py at line 0: __version__ = "3.6-dev"
Found '## vNEXT' in CHANGELOG.md at line 2: ## vNEXT
Changing file src/__init__.py:
--- a/src/__init__.py
+++ b/src/__init__.py
@@ -1 +1 @@
-__version__ = "3.6-dev"
+__version__ = "3.7-dev"
Not changing file CHANGELOG.md
Writing to config file .bumpversion.cfg:
[bumpversion]
commit = True
tag = True
current_version = 3.7-dev
parse = (?P<major>\d+)\.(?P<minor>\d+)(\-(?P<release>[a-z]+))?
serialize =
    {major}.{minor}-{release}
    {major}.{minor}

[bumpversion:file:src/__init__.py]

[bumpversion:file:CHANGELOG.md]
search = ## vNEXT
replace = ## vNEXT

[bumpversion:part:release]
optional_value = gamma
values =
    dev
    gamma

Preparing Git commit
Adding changes in file 'src/__init__.py' to Git
Adding changes in file 'CHANGELOG.md' to Git
Adding changes in file '.bumpversion.cfg' to Git
Committing to Git with message 'Bump version: 3.6-dev → 3.7-dev'
Would tag 'v3.7-dev' in Git
jaredsampson commented 4 years ago

I've run into the same issue. Trying to adopt the CHANGELOG.md format from Keep A Changelog and using Markdown headings to delineate versions.

For the time being, I'm using the following as a workaround:

[bumpversion:file:CHANGELOG.md]
search = ## [Unreleased]
replace = ## [Unreleased]
    -
    -## [v{new_version}]

and will plan to simply delete the leading dash manually upon the next edit.

ichrysou commented 1 year ago

This seems to be due to configparser module used by bumpversion. configparser kindly discards all new lines in the replace string beginning with # as comments. For "Keep a Changelog" to be supported as is, one might need a remedy for that.