Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git apply: Ignore non-leading/trailing whitespace

Tags:

git

whitespace

I want to split a commit into two commits: contentful changes & whitespace changes, though every solution I've found only applies if a line contains exclusively whitespace changes.

In the following there's an irrelevant whitespace change and a new argument added to a function:

$ git diff -U0
diff --git a/blah.h b/blah.h
--- a/blah.h
+++ b/blah.h
@@ -1, +1 @@
-void  blah();
+void blah(int x);

The best I've found is this:

$ git diff -w --word-diff-regex=[^[:space:]]
diff --git a/blah.h b/blah.h
--- a/blah.h
+++ b/blah.h
@@ -1, +1 @@
void blah({+int x+});

... but so far as I can tell git-apply cannot consume that as input.

I almost had myself convinced this may not be possible, arguing that git would need contextual information to know whether it's a meaninful change, eg:

  • Inside a literal string (especially if it spans multiple lines)
  • Whether the whitespace has meaning (eg, makefiles)

... but git will happily ignore all whitespace changes as long as you don't introduce any other changes (in some neighborhood of the relevant lines).

I'm sure there exist other tools capable of doing what I want (or something close to it), though in this situation I cannot just download whatever I want to take care of the problem. Nevertheless, feel free to post answers of that stripe as they may help others in the future.

like image 827
Brian Vandenberg Avatar asked Oct 16 '22 15:10

Brian Vandenberg


1 Answers

I believe my best option is to reformat the code before the changes get applied / commit / reformat the changed code / commit and live with it.

like image 166
Brian Vandenberg Avatar answered Oct 31 '22 12:10

Brian Vandenberg