Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git diff ignore semicolon at end of line

Tags:

git

git-diff

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
like image 215
Chris Maes Avatar asked May 26 '19 15:05

Chris Maes


People also ask

Does git ignore line endings?

By default, core. autocrlf is set to false on a fresh install of Git, meaning Git won't perform any line ending normalization. Instead, Git will defer to the core. eol setting to decide what line endings should be used; core.

Why do I see M in git diff?

When I changed some code, I saw ^M at the end of the lines I added in git diff . I think the ^M were showing up because they were different line endings than the rest of the file. Because the rest of the file was developed in Windows it used CRLF line endings, and in Linux it uses LF line endings.

What does git diff -- staged do?

git diff --staged will only show changes to files in the "staged" area. git diff HEAD will show all changes to tracked files. If you have all changes staged for commit, then both commands will output the same.


2 Answers

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).

like image 69
Marco Luzzara Avatar answered Oct 31 '22 21:10

Marco Luzzara


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 
like image 2
LeGEC Avatar answered Oct 31 '22 21:10

LeGEC