1

At work I'm coding in C# using Visual Studio and Resharper, which liberally change whitespace, including breaking lines, even of code I'm not editing.

This is fine for new code I'm writing, or even old code I'm editing, but for old code I'm not touching, it results in a lot of cruft in the git diffs.

The workflow process I usually follow is to create a new branch, and do my changes there, and push when it's ready for review.

But before I push, I'd like to clear out all the changes that are solely whitespace changes. i.e. lines being split like so:

a + b + c

to

a +
b +
c

and all sorts of other whitespace changes that don't change the behaviour of C# code as it doesn't give meaning to whitespace.

Of course I don't want to stop any whitespace changes, obviously between commits I might change the whitespace of new code.

But it's the code I haven't touched in my branch that I want any whitespace changes removed from.

I just want to make a commit, before the final push, that rolls back all changes that are whitespace only as compared to the point the initial branch was taken.

Is there a way to do this or a nice way to script it?

I attempted this with the following script:

#!/bin/bash

BRANCH_BASE=$(git merge-base --fork-point origin/dev)
git diff $BRANCH_BASE HEAD -w --word-diff-regex=[^[:space:]] > /tmp/no_whitespace_changes.patch
git checkout $BRANCH_BASE
git apply /tmp/no_whitespace_changes.patch

My intent was to then rebase on top of the branch head, but I get a fatal: corrupt patch at line 6 error upon running this, so I'm obviously doing something hideously wrong.

But perhaps there's a better way anyway to solve this. I feel like others would have this same problem so I don't want to reinvent the wheel.

Clinton
  • 22,361
  • 15
  • 67
  • 163
  • Wouldn't it be the "right" approach to configure the IDE's to behave in a way that suits your workflow? I don't know about Visual Studio, but I'm pretty sure that you can fine tune the auto formatting in the JetBrains IDE's in every possible way. For example, you can tell it to only auto-format the parts of the file that you edited and leave alone the rest. – anothernode Apr 05 '18 at 07:56
  • Resharper unfortunately doesn't have an option "only reformat the parts I've changed". Indeed that's complex, it would have to work out what parts of functions you've changed and apply only the changes to those parts. It would basically have to reimplement git to track changes, including remembering for example when you change parts but then change them back to the original (at which point the whitespace shouldn't show up in the git logs as there's no "real" changes). Git has more information to preform this I'm pretty sure. – Clinton Apr 05 '18 at 08:00
  • Okay, I just assumed Resharper would have this functionality because IntelliJ has and they are closely related. Of course you can easily tell git to ignore whitespace changes in commits but your requirement is that only whitespace changes done by the auto-formatter should be ignored while the one you made manually should not, right? I don't know how git should be able to make that distinction. – anothernode Apr 05 '18 at 08:05
  • I can calculate a diff between when I started the branch and the head. I just want to ignore whitespace only changes to this diff. – Clinton Apr 05 '18 at 08:08
  • 1
    Have you seen this? https://stackoverflow.com/questions/3515597/add-only-non-whitespace-changes – anothernode Apr 05 '18 at 08:35

1 Answers1

0

Personally, I would not want an extra commit to rollback changes to files I have not touched. They should never be added in the first place. This will only add confusion in the git history.

I would first look to see if there is a way to configure Resharper to be a little less excitable. Maybe only running when a files contents change, or the file is actually touched.

In the worst case, rather than add another commit to rollback the changes, I would look to reset the HEAD back and apply only those files that change. The original commit with the whitespace changes would still exist, but it would not end up on your master branch.

Xetius
  • 44,755
  • 24
  • 88
  • 123
  • There are no files which are touched which I don't want to be. The issue is with files that I have changed having whitespace changes all over them in places that I haven't changed. This bloats the diffs, not in terms of the number of files but their size. – Clinton Apr 05 '18 at 07:56