Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code formatting and source control diffs

What source control products have a "diff" facility that ignores white space, braces, etc., in calculating the difference between checked-in versions? I seem to remember that Clearcase's diff did this but Visual SourceSafe (or at least the version I used) did not.

The reason I ask is probably pretty typical. Four perfectly reasonable developers on a team have four entirely different ways of formatting their code. Upon checking out the code last changed by someone else, each will immediately run some kind of program or editor macro to format things the way they like. They make actual code changes. They check-in their changes. They go on vacation. Two days later that program, which had been running fine for two years, blows up. The developer assigned to the bug does a diff between versions and finds 204 differences, only 3 of which are of any significance, because the diff algorithm is lame.

Yes, you can have coding standards. Most everyone finds them dreadful. A solution where everyone can have their cake and eat it too seems far more preferable.

=========

EDIT: Thanks to everyone for some great suggestions.

What I take away from this is:

(1) A source control system with plug-in type diffs is preferable.

(2) Find a diff with suitable options.

(3) Use a good source formatting program and settle on a check-in standard.

Sounds like a plan. Thanks again.

like image 442
Newton Falls Avatar asked May 24 '09 19:05

Newton Falls


1 Answers

Git does have these options:

  • --ignore-space-at-eol

    Ignore changes in whitespace at EOL.

  • -b, --ignore-space-change

    Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent.

  • -w, --ignore-all-space

    Ignore whitespace when comparing lines. This ignores differences even if one line has whitespace where the other line has none.

I am not sure if brace changes can be ignored using Git's diff.

If it is C/C++ code, you can define Astyle rules and then convert the source code's brace style to the one that you want, using Astyle. A git diff will then produce sane output.

like image 191
Alan Haggai Alavi Avatar answered Sep 22 '22 09:09

Alan Haggai Alavi