Discussion:
Revert only whitespace changes
Markus Schaber
2011-06-14 09:47:48 UTC
Permalink
Hi,

Occasionally, we have source files which contain - apart from the
changes made by the developer - a lot of whitespace changes introduced
by reformatting the code. (Mainly tabs<->spaces conversions and trailing
whitespace.)

While Subversion and TortoiseSVN offer the possibility to ignore those
white space changes when diffing the files, a commit still includes
those changes, and thus the diff in the SVN history is cluttered, which
causes problems for reviewers.

I found no possibility to selectively revert those lines to their
original state via SVN but manually reverting all those lines in a diff
viewer.

Is there any tool which reverts all those whitespace-only changes,
keeping only the "real" changes in the file?

Bonus points for a tool which keeps real indentation changes, and only
discards tab<->space conversions which do not change the indentation
level. :-)

(I know that a project-wide style guide and rejecting commits which do
not conform to that style guide are the long term solution to that
problem, and we're aiming towards that.)

Best regards

Markus Schaber

___________________________
We software Automation.

3S-Smart Software Solutions GmbH
Markus Schaber | Developer
Memminger Str. 151 | 87439 Kempten | Germany | Tel. +49-831-54031-0 |
Fax +49-831-54031-50

Email: ***@3s-software.com | Web: http://www.3s-software.com
CoDeSys internet forum: http://forum.3s-software.com
Download CoDeSys sample projects:
http://www.3s-software.com/index.shtml?sample_projects

Managing Directors: Dipl.Inf. Dieter Hess, Dipl.Inf. Manfred Werner |
Trade register: Kempten HRB 6186 | Tax ID No.: DE 167014915
Konstantin Kolinko
2011-06-14 11:37:33 UTC
Permalink
Post by Markus Schaber
While Subversion and TortoiseSVN offer the possibility to ignore those
white space changes when diffing the files, a commit still includes
those changes, and thus the diff in the SVN history is cluttered, which
causes problems for reviewers.
Maybe you can fine-tune the tools that are being used for the review?

If you are talking about commit e-mails, the script that generates
them can use svn diff option to ignore whitespace changes.

If you are talking about some web software, the viewvc instance used
at ASF ignores whitespace changes when I select "Colored Diff"
command. E.g. [1]


[1]: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jk/native/common/jk_lb_worker.c?r1=649041&r2=649040&pathrev=649041&diff_format=h
Post by Markus Schaber
Is there any tool which reverts all those whitespace-only changes,
keeping only the "real" changes in the file?
I think that you can generate two diffs, one with all the changes and
second one with non-whitespace ones.

Then, use the first diff to undo the changes and the second one to reapply them.
Post by Markus Schaber
(I know that a project-wide style guide and rejecting commits which do
not conform to that style guide are the long term solution to that
problem, and we're aiming towards that.)
A good policy is to educate the developers to commit whitespace-only
changes in a separate commit.

Best regards,
Konstantin Kolinko
Markus Schaber
2011-06-14 14:32:50 UTC
Permalink
Hi, Konstantin,
Post by Konstantin Kolinko
Post by Markus Schaber
While Subversion and TortoiseSVN offer the possibility to ignore those
white space changes when diffing the files, a commit still includes
those changes, and thus the diff in the SVN history is cluttered,
which causes problems for reviewers.
Maybe you can fine-tune the tools that are being used for the review?
Most people usually use TortoiseDiff for the review - but they get
suspicious when one tells them to check one of the "ignore"-Flags. :-)
Post by Konstantin Kolinko
Post by Markus Schaber
Is there any tool which reverts all those whitespace-only changes,
keeping only the "real" changes in the file?
I think that you can generate two diffs, one with all the changes and
second one with non-whitespace ones.
Then, use the first diff to undo the changes and the second one to
reapply
Post by Konstantin Kolinko
them.
I think that idea could work, I'll see whether I can come up with a
script doing that.

However, I wonder why - seemingly - no one else ever needed such a
script and provided it to the community.
Post by Konstantin Kolinko
Post by Markus Schaber
(I know that a project-wide style guide and rejecting commits which do
not conform to that style guide are the long term solution to that
problem, and we're aiming towards that.)
A good policy is to educate the developers to commit whitespace-only
changes in a separate commit.
I know that. And I'm searching for a tool allowing me to do just that.

Especially when you make a self-contained change (e. G. pure addition of
new methods or a new class, not touching any existing code pathes) to a
large "legacy" code file, and accidentally hit "Reformat code" in
ignorance of the fact that this file does not yet conform to the style
guide, you're busted.

Best Regards,

Markus Schaber
Stefan Sperling
2011-06-14 14:46:49 UTC
Permalink
Post by Markus Schaber
Hi, Konstantin,
Post by Konstantin Kolinko
A good policy is to educate the developers to commit whitespace-only
changes in a separate commit.
I know that. And I'm searching for a tool allowing me to do just that.
Especially when you make a self-contained change (e. G. pure addition of
new methods or a new class, not touching any existing code pathes) to a
large "legacy" code file, and accidentally hit "Reformat code" in
ignorance of the fact that this file does not yet conform to the style
guide, you're busted.
So why doesn't the IDE have an undo button that restores the previous
format? I don't think this warrants introducing a new feature in the
version control system. Rather, the IDE should be fixed.
Markus Schaber
2011-06-15 06:27:22 UTC
Permalink
Hi,
Post by Stefan Sperling
Post by Markus Schaber
Hi, Konstantin,
Post by Konstantin Kolinko
A good policy is to educate the developers to commit
whitespace-only
Post by Stefan Sperling
Post by Markus Schaber
Post by Konstantin Kolinko
changes in a separate commit.
I know that. And I'm searching for a tool allowing me to do just that.
Especially when you make a self-contained change (e. G. pure
addition
Post by Stefan Sperling
Post by Markus Schaber
of new methods or a new class, not touching any existing code
pathes)
Post by Stefan Sperling
Post by Markus Schaber
to a large "legacy" code file, and accidentally hit "Reformat code"
in
Post by Stefan Sperling
Post by Markus Schaber
ignorance of the fact that this file does not yet conform to the style
guide, you're busted.
So why doesn't the IDE have an undo button that restores the previous
format? I don't think this warrants introducing a new feature in the
version control system. Rather, the IDE should be fixed.
The IDE has such an undo button, which works fine if I notice that in
the moment of reformatting. However, as I am used to have the large
majority of code files (including all my own files) formatted properly,
I press that button frequently, and usually notice the "accident" hours
later when I want to actually commit the changes.

I know that the "real" fix is to change all the existing files in the
repository to proper formatting (which is in the works, but it will take
some time), and the second real fix is to not press the reformat button
in those files.

And I also do not want this as a feature included in SVN, but just
wondered whether there is a ready-made script or external tool which
does that work.

Thanks,
Markus Schaber
Daniel Shahaf
2011-06-14 23:07:00 UTC
Permalink
Post by Markus Schaber
Is there any tool which reverts all those whitespace-only changes,
keeping only the "real" changes in the file?
svn diff foo > 1
svn revert foo
patch < ./1 || svn patch ./1
Post by Markus Schaber
Bonus points for a tool which keeps real indentation changes, and only
discards tab<->space conversions which do not change the indentation
level. :-)
vim -c 'source /dev/mind-reader' -c 'wq'

Not all OS's provide the necessary device file, though.
Daniel Shahaf
2011-06-14 23:16:25 UTC
Permalink
Post by Daniel Shahaf
Post by Markus Schaber
Bonus points for a tool which keeps real indentation changes, and only
discards tab<->space conversions which do not change the indentation
level. :-)
vim -c 'source /dev/mind-reader' -c 'wq'
Not all OS's provide the necessary device file, though.
Okay, more seriously, here's a Vim script...

The first two lines are the configuration, you shouldn't have to change
anything below them.


:set tabstop=8 shiftwidth=8
:set expandtab

:g/^/call MaybeRetab()
func! MaybeRetab()
normal! ^
let before = virtcol('.')
.retab
let after = virtcol('.')
if before != after
undo
endif
endfunction
Markus Schaber
2011-06-15 06:34:31 UTC
Permalink
Hi, Daniel,
Post by Daniel Shahaf
Okay, more seriously, here's a Vim script...
The first two lines are the configuration, you shouldn't have to change
anything below them.
[snap]
Thanks!

Best regards

Markus Schaber

___________________________
We software Automation.

3S-Smart Software Solutions GmbH
Markus Schaber | Developer
Memminger Str. 151 | 87439 Kempten | Germany | Tel. +49-831-54031-0 |
Fax +49-831-54031-50

Email: ***@3s-software.com | Web: http://www.3s-software.com
CoDeSys internet forum: http://forum.3s-software.com
Download CoDeSys sample projects:
http://www.3s-software.com/index.shtml?sample_projects

Managing Directors: Dipl.Inf. Dieter Hess, Dipl.Inf. Manfred Werner |
Trade register: Kempten HRB 6186 | Tax ID No.: DE 167014915

Continue reading on narkive:
Loading...