How to create a patch ignoring indentation differences in the code?
Solution 1
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.
Solution 2
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...
Related videos on Youtube
Ram
Updated on September 18, 2022Comments
-
Ram over 1 year
I'm trying to create patch a file using diff tool.But facing an issues.The way I am doing is below.
I've created one Directory named a and put original file in to it.
a/original_file.c
Now I have created other Directory named b and put same file with modified content in to it.
b/original_file.c
Now content of
b/original_file.c
file I have copied from internet and put it into some text editor.After giving command:
diff -Naur a b > patch_file.patch
, the filepatch_file.patch
is generated and it has some unwanted changes (its related to indentation).For example:
return mg_nw (MG_READY_NOY, &rmsg, seqnr, - sizeof (struct mg_rdy_notify)); + sizeof (struct mg_rdy_notify));
Now you can see there are changed related to indentation where
sizeof (struct mg_rdy_notify))
is replaced by samesizeof (struct mg_rdy_notify))
but one basis of indentation which is what we don't want. -
Wildcard about 8 yearsI was astonished this had fewer upvotes until I noticed the timestamps. Thanks for taking the time; this was very helpful. :)
-
Marius about 8 yearsno problem - I've been working through the backlog of questions which I find interesting...