6

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
2

2 Answers 2

5

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

4
  • thanks for the hint. You can make your answer better: \\n is not working. What is working: --word-diff-regex='[^;[:space:]]+'. But that is still a partial answer as you say; it ignores all semicolons, but it is a nice help already
    – Chris Maes
    Commented May 27, 2019 at 6:36
  • Thank you for the improvement. However, after some tries, I am afraid regex lookahead is not supported. The partial answer will probably remain partial. Sorry for that. Commented May 27, 2019 at 7:24
  • Yes I understood that, you have my upvote already, but I'm not accepting the answer as for now...
    – Chris Maes
    Commented May 27, 2019 at 7:27
  • Do not worry, I totally understand that. I just hope someone else will come up with a complete answer. Commented May 27, 2019 at 7:42
2

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 

Not the answer you're looking for? Browse other questions tagged or ask your own question.