-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strip trailing white spaces before saving the config #161
base: master
Are you sure you want to change the base?
Strip trailing white spaces before saving the config #161
Conversation
Implemented the test for this into test_retain_newline since it can affect the newline character. Added test case for mac line ending '\r'.
Does this mean that all existing users will see spaces disappear in their config file? That would introduce a lot of unwanted diffs... |
@florisla Yes, this change would remove trailing spaces from existing configs and thus create additional diffs.
So IMHO the current behavior creates possibly more unwanted diffs than the changed one. Another noteworthy point in respect to formatting only diffs is that Lastly, this would allow using |
I agree that stripping the trailing whitespace is better. The question is, will all users be happy (or don't care) if we change this. Also, we have to decide if we still want to re-write the config file at all, but that's another issue (#185). I have a theory on why text with return characters ( Do note that the current newline detection has a bug... If a file contains both |
Reason I am a big fan of this change: Projects that use pre-commit's "Trim Trailing Whitespace" hook will see the version-bump commits fail because setup.cfg otherwise contains whitespace prior to the commit (after its rewrite).
|
I think I finally found the black magic that puzzled me back then 😄 In [1]: with open("dummy.txt", "wb") as dummy:
: dummy.write(b"foo\r\nbar")
:
In [2]: with open("dummy.txt", "rb") as dummy:
: print(dummy.read())
:
b'foo\r\nbar'
In [3]: with open("dummy.txt", "r") as dummy:
: print(dummy.read().encode())
:
b'foo\nbar' Same result on windows ( |
There is another complication: files which use multiple newline styles intermixed! When calling bump2version already checks Edit: |
There are some questions I have regarding raising an error on intermixed newline, first and foremost should it be done in this PR?
|
Just want to hop in here and give my +1. I find the trailing whitespace to be annoying, and I would much rather have a one-time diff that removes trailing whitespace than continuous diffs that pop up every time I have to adjust the config file. I would love for this PR to get released! Not that I'm qualified to answer your question, but I'll give my 2 cents while I'm here: I think fixing the line ending bug is outside the scope of this PR. You haven't touched any of the code that affects which newline(s) the file uses, and it's not really relevant to the conceptual change either. |
First of all thanks for the nice tool, I have used it for 3 years and together with a CD system, releasing a new version of your package is a breeze 😄
Recently I made my first
setup.cfg
only package (bare minimumsetup.py
) and when I wanted to bump the version with (commit = True
andtag = True
settings as usual), my pre-commit hook removing trailing white spaces failed, rendering me unable to make a commit.After some investigating I found that
bump2version
generates the following unwanted diff:Since many people have editor settings and/or other measures to remove trailing white spaces, and I don't know of any reason to add them, I decided to make this ninja PR, which makes
bump2version
remove them by default.Since the line spitting is closely related to the used newline character, I integrated the test into
test_retain_newline
.Confusing black magic
Even so splitting lines which have
\r
as newline character, with\n
shouldn't work in theory it somehow does.Also, I found that the passed value of
config_newlines
in the tests is oftenNone
, which is why tests failed when I tried to split onconfig_newlines
or simply use.splitlines()
.This is also the reason why I had to manually add back the
\n
, instead of.writelines
taking care of that.The only way for
config_newlines
to beNone
, without any other issues, would be if the file wasn't read before retrievingnewlines
from the file descriptor.But it is read before:
bump2version/bumpversion/cli.py
Lines 270 to 272 in 8e6db23
And the function where it is read is always called:
bump2version/bumpversion/cli.py
Lines 89 to 91 in 8e6db23
So I'm very confused and have the 2nd biggest problem in programming, 'It works and I don't know why!'.
Maybe you can enlighten me, about what kind of black magic is going on here 😅