How to create a patch ignoring indentation differences in the code?
diff
has an option to ignore whitespace changes (-w
), same for patch
(-l
). In general, it's a bad idea to ignore whitespace though, so you should reserve its use for special cases, when someone's editor did something horrible...
diff
has more than one option related to whitespace. However, one is less useful for patches. The manual page gives an obscure hint, referring to both GNU:
-B, --ignore-blank-lines
ignore changes where lines are all blank
-b, --ignore-space-change
ignore changes in the amount of white space
-w, --ignore-all-space
ignore all white space
and FreeBSD
-b Ignore changes in amount of white space.
-B Ignore changes that just insert or delete blank lines.
-w Ignore white space when comparing lines.
Usually one uses -b
, because that is less likely to overlook significant changes. If you have changed only indentation, then both -b
and -w
give the same result. On the other hand, if you inserted spaces where there were none, or deleted existing spaces (leaving none), that could be a change in your program. Here is an example:
$ diff foo.c foo2.c
4c4
< setlocale(LC_ALL, "");
---
> setlocale(LC_ALL, " ");
6,7c6,7
< printw("\U0001F0A1");
< getch();
---
> printw ("\U0001F0A1");
> getch(); /* comment */
$ diff -b foo.c foo2.c
4c4
< setlocale(LC_ALL, "");
---
> setlocale(LC_ALL, " ");
6,7c6,7
< printw("\U0001F0A1");
< getch();
---
> printw ("\U0001F0A1");
> getch(); /* comment */
$ diff -w foo.c foo2.c
7c7
< getch();
---
> getch(); /* comment */
In this case, the -w
option allows you to ignore the change to the setlocale
parameter (perhaps not what was intended).
POSIX diff, by the way, has only the -b
option.
For patch
, POSIX documents the -l
option:
-l
(The letter ell.) Cause any sequence of<blank>
characters in the difference script to match any sequence of<blank>
characters in the input file. Other characters shall be matched exactly.