c4urself / bump2version

Version-bump your software with a single command
https://pypi.python.org/pypi/bump2version
MIT License
1.05k stars 134 forks source link

added option to disable comments in config files #221

Closed manfred-kaiser closed 2 years ago

manfred-kaiser commented 3 years ago

I'm using bum2version to update my changelog. This changelog uses markdown and headers are defined by a multiple of # The ConfigParser's default settings is, that # and ; are used as comment prefix.

This pull request introduces a new command line argument --disable-config-comments. When this argument is used, the comment prefixes for the config parser are set to None. This allows using # in multiline search and replace options.

Example Changelog:

## [Unreleased]

### Added

- new config option

## [0.5.12] - 2021-07-13

### Fixed

- handle subsystem errors in sftp server

[Unreleased]: https://github.com/ssh-mitm/ssh-mitm/compare/0.5.12...develop
[0.5.12]: https://github.com/ssh-mitm/ssh-mitm/compare/0.5.11...0.5.12

I want to use bump2version to update ## [Unreleased] to the current version and the link [Unreleased]: https://github.com/ssh-mitm/ssh-mitm/compare/0.5.12...develop to match the latest version. bum2version should also add a new unreleased link, so there is no need to add it after a version update.

Used config file:

[bumpversion]
current_version = 0.5.12

[bumpversion:file (compare vesions):CHANGELOG.md]
search = [Unreleased]: https://github.com/ssh-mitm/ssh-mitm/compare/{current_version}...develop
replace = [Unreleased]: https://github.com/ssh-mitm/ssh-mitm/compare/{new_version}...develop
    [{new_version}]: https://github.com/ssh-mitm/ssh-mitm/compare/{current_version}...{new_version}

[bumpversion:file (release changes):CHANGELOG.md]
search = ## [Unreleased]
replace = ## [Unreleased]

    ## [{new_version}] - {now:%Y-%m-%d}

Without disabling the comment prefixes, the line ## [{current_version}] - {now:%Y-%m-%d} will be removed, because of the # at the beginning, which will interpreted as comment.

When the new argument --disable-config-comments is used it works as expected.

Updated Changelog:

## [Unreleased]

## [0.5.13] - 2021-07-14

### Added

- new config option

## [0.5.12] - 2021-07-13

### Fixed

- handle subsystem errors in sftp server

[Unreleased]: https://github.com/ssh-mitm/ssh-mitm/compare/0.5.13...develop
[0.5.13]: https://github.com/ssh-mitm/ssh-mitm/compare/0.5.12...0.5.13
[0.5.12]: https://github.com/ssh-mitm/ssh-mitm/compare/0.5.11...0.5.12
florisla commented 3 years ago

I think this is a sensible feature to support comment characters. The alternative would be to replace keywords such as {hash} with a comment character #.

This will require a thorough review though.

manfred-kaiser commented 3 years ago

I know, that the new option can break config files, when you are using comments as values. If you forgot the new argument, second line will be interpreted as a comment. This is getting more worse, if you are using auto commit.

As an alternative, it's possible to use a wrapper script:

from configparser import ConfigParser, RawConfigParser
from bumpversion.cli import main

@property
def comment_prefixes(self):
    return ()

@comment_prefixes.setter
def comment_prefixes(self, value):
    pass

RawConfigParser._comment_prefixes = comment_prefixes
ConfigParser._comment_prefixes = comment_prefixes

main()
hukkin commented 3 years ago

An alternative proposal: How about allowing configuration in .bumpversion.toml (and preferring that over the cfg file if it exists, perhaps slowly deprecating the cfg)?

TOML overcomes this and other flaws of the ini format that lacks specification.

hukkin commented 3 years ago

An added note: tbump (mentioned in RELATED.md uses a tbump.toml so should already be able to do what OP wants.

EDIT: or perhaps not, haven't used it :smile: the conf structure seems to be different from bumpversion

FredM commented 2 years ago

I had the same issue but it can be alleviated by using {#} each time you need to represent # in a multiline comment. In the initial example this would give (first line can be omitted):

[bumpversion:file (release changes):CHANGELOG.md]
search = ## [Unreleased]
replace = ## [Unreleased]

    {#}{#} [{new_version}] - {now:%Y-%m-%d}

I found the mention of it in this thread: https://github.com/c4urself/bump2version/issues/199

manfred-kaiser commented 2 years ago

I had the same issue but it can be alleviated by using {#} each time you need to represent # in a multiline comment. In the initial example this would give (first line can be omitted):

[bumpversion:file (release changes):CHANGELOG.md]
search = ## [Unreleased]
replace = ## [Unreleased]

  {#}{#} [{new_version}] - {now:%Y-%m-%d}

I found the mention of it in this thread: #199

@FredM thanks for the solution, this is the better option to work with markdown files :+1: