Styling a C/C++ file on diffs

493 Views Asked by At

I have a requirement to style only the part of the C/C++ file which is getting committed (so the blame for the other lines are intact). Is there any tool to just do that? I know we can style the whole file and get the blame to ignore styling changes (but my team doesn't want that).

Here are a few things I tried with astyle:

  1. I write the diff to a new file and style it and replace the diff part in the original file - Why it doesn't work? It doesn't really get styled as it doesn't have the context, like the if statement to indent the rest of the lines on etc.

  2. I style the whole file and replace only the section that changed. - Why it doesn't work? If I try to change max code length and the lines get broken up then the diff lines are different.

  3. How about a 3 way merge between the original file and the file where the styled diff is included? Yet to try this.

May be there is an easier way and I am missing it :S

1

There are 1 best solutions below

1
Jesper Juhl On

You can do that with a git hook and clang-format.

For example this: https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format