threads / discuss / 49611

Shouldn't git be able to apply diffs that it created with --ignore-whitespace?

Subject: Shouldn't git be able to apply diffs that it created with --ignore-whitespace?

## tl;dr

2 messages between Oct 18, 2018 and Oct 20, 2018.

replies: 1people: 2as markdown or json

Mahmoud Al-Qudsi· Oct 18, 2018, 20:12 UTC · lore
Hello again all,

I think I've previously broached this subject before, but I think I perhaps wasn't clear enough about what I was trying to do or why I feel that git is at fault here.

(I'm running git 2.19.1)

Starting with a fully-committed, not-dirty codebase, I open(ed) a poorly formatted, mixed-whitespace file (that I absolutely did not author!) under version control and make some very localized changes. My editor, being very smart and helpful, fixes up the line ending on save, and I exit.

At this point, my source file contains a) deliberate changes I want, and b) whitespace changes I wish I could commit but that should not be a part of my patch.

Shouldn't the following workflow be supported:

~> git diff -w > foo.diff ~> git reset --hard ~> git apply [--ignore-whitespace] < foo.diff

Because that throws an error in this case:
> error: patch failed: includes/helpers/class.phpmailer.php:1182
> error: includes/helpers/class.phpmailer.php: patch does not apply

I feel like this did work, once upon a time. Perhaps prior to the same that broke `git add -p` when whitespace was mangled on editor exit/save (2b8ea7f3c7)?

To help debug this, I'm attaching the output of the following taken after I've made my changes to the file and wish to generate a clean diff, the former of which applies just fine after `git reset --hard`, while the latter does not:

~> git diff > with_whitespace.diff ~> git diff -w > without_whitespace.diff

(I can also privately share the original file off-list if needed.)

I don't believe the list allows binary attachments and because we're dealing with line-ending mangling I definitely do not want to include them inline, so I've uploaded them here:

* http://share.neosmart.net/View/Index/nGujqm.diff
* http://share.neosmart.net/View/Index/f4dkVF.diff
(You can download them as-is by clicking the floppy icon in the top-right)
With thanks,

Mahmoud Al-Qudsi NeoSmart Technologies

brian m. carlson· Oct 20, 2018, 02:36 UTC · re: Mahmoud Al-Qudsi · lore

Re: Shouldn't git be able to apply diffs that it created with --ignore-whitespace?

On Thu, Oct 18, 2018 at 03:12:09PM -0500, Mahmoud Al-Qudsi wrote:
Show 22 quoted lines
> Hello again all,
> 
> I think I've previously broached this subject before, but I think I perhaps
> wasn't clear enough about what I was trying to do or why I feel that git is at
> fault here.
> 
> (I'm running git 2.19.1)
> 
> Starting with a fully-committed, not-dirty codebase, I open(ed) a poorly
> formatted, mixed-whitespace file (that I absolutely did not author!) under
> version control and make some very localized changes. My editor, being very
> smart and helpful, fixes up the line ending on save, and I exit.
> 
> At this point, my source file contains a) deliberate changes I want, and b)
> whitespace changes I wish I could commit but that should not be a part of my
> patch.
> 
> Shouldn't the following workflow be supported:
> 
> ~> git diff -w > foo.diff
> ~> git reset --hard
> ~> git apply [--ignore-whitespace] < foo.diff

In general, git diff -w doesn't produce a patch that can be applied. That's because it ignores all whitespace changes in a particular way. The diff output is rendered as if the whitespace in the lines were written as it is in the postimage (the changed file), not the preimage (the original file).

This is useful because usually if you're, say, indenting a block of code, you want to see the output properly indented with the new lines of code (say, the loop or conditional) you wrote around it. And since this feature is designed for visual inspection, it makes sense to do it this way. However, it essentially means that your changes can't, in general, be applied.

There are, of course, situations in which this might have worked in the past, and it may indeed work for some situations still, but it won't in the general case. git apply --ignore-whitespace only modifies context lines, so it doesn't affect the actual content lines in the diff.

-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

← back to recent threads