Chris Maes
Chris Maes

Reputation: 37742

git diff ignore semicolon at end of line

A colleague has created a commit where his editor has appended semicolons to each line (amongst other changes).

I know that I can use the -w switch to ignore whitespace errors. Would there be some kind of git magic to make git ignore ; altogether, or even better only when at EOL ?

Something like:

git diff --ignore=; -w

Upvotes: 6

Views: 1187

Answers (2)

Marco Luzzara
Marco Luzzara

Reputation: 6036

Maybe you can solve that with the --word-diff-regex option. This is what I did: I created a simple file and committed it with the following content.

first line
second line
third line

Then I modified it like this:

first line;
second; line;
third changed line;

If I have correctly understood, you need to show only the following differences: second -> second; third line -> third changed line

You can partially do this executing:

git diff  --word-diff-regex='[^ \\n;]+' HEAD..HEAD~1

And this is the output:

first line
second line
third[-changed-] line

I said partially because even if I found a regex to detect also the first change ('[^ \\n]+(?!\\n|$)'), git does not seem to accept it, for some reason I am not aware of (I am still working on it).

Anyway, the logic behind it is that this option "overrides" how git considers a word. I know this is not the right regex since it is not covering several cases, change it based on your needs (for example if you consider test1;test2 a word).

Upvotes: 5

LeGEC
LeGEC

Reputation: 51850

The easiest way I can think of is :

create a file, with your colleague's version, where you remove the semicolons at EOL, and compare that with the file you want :

git show modifiedcommit:the/file | sed -e 's/;$//' > /tmp/theFile
git diff originalcommit:the/file /tmp/theFile 

Upvotes: 2

Related Questions