11

How can I ignore "code style" changes while using WinMerge as a difftool? Specifically across two commits.

So that

thing
{
  a,
  b
}

and

thing { a, b }

would be treated as identical.

Essentially this question, but for winmerge rather than diff.

.gitconfig:

[diff]
    tool = winmerge
[difftool]
    prompt = false
[difftool "winmerge"]
    cmd = "$HOME/scripts/winmerge.sh" "$LOCAL" "$REMOTE"
[mergetool]
    prompt = false
    keepBackup = false
    keepTemporaries = false
[merge]
    tool = winmerge
[mergetool "winmerge"]
    cmd = "'C:/Program Files (x86)/WinMerge/WinMergeU.exe'" -e -u -fm -dl \"Local\" -dr \"Remote\" "$LOCAL" "$MERGED" "$REMOTE"

(winmerge.sh just calls WinMergeU.exe -e -u -wr "$1" "$2")

None of the command line options seem to fit, and I think line filters won't work because they are per-line.

MHebes
  • 2,290
  • 1
  • 16
  • 29
  • Diff tools are traditionally line-based as programs are written as text in lines. You're basically asking for a diff at the tokenized & normalized level, not at the textual level. See [here](https://stackoverflow.com/a/75800278/269126), but it's not directly usable answer, just an explanation of the conceptual and practical problem. – Lumi Mar 21 '23 at 11:06
  • @Lumi yes, and [JD Frias's answer](https://stackoverflow.com/a/62825323/3554391) gets closest to that idea. My "solution" since I asked this question has been to enforce clang-format rules in my team :). I could envision a theoretical tool which runs a formatter but keeps track of the original source locations, and then does a diff on the formatted text but shows the user the source-mapped originals for editing. Similar to how you can debug transpiled javascript by stepping through the typescript. I'm not sure that tool exists currently, as you said. – MHebes Mar 21 '23 at 15:20
  • Coming back to this two years later: There is no option in WinMerge to do this. I have found [difftastic](https://difftastic.wilfred.me.uk/) to do what I was actually asking here, which is syntax-tree-based diffs. It does so at the cost of speed and memory, so I end up having both winmerge and difftastic in my gitconfig and switch between them depending on my needs. For my given example (with .cpp extensions), difftastic prints "No syntactic changes". – MHebes Jun 14 '23 at 15:48

2 Answers2

14

This option works for me:

WinMerge -> Edit -> Options -> Compare -> General: Ignore blank lines; Ignore carriage return differences (Windows/Unix/Mac)

etc.

The answer was taken from https://superuser.com/questions/174275/can-i-compare-only-file-contents

Charlie
  • 639
  • 9
  • 19
  • 1
    This doesn't do it if you have one file with some blank lines inserted and the other one doesn't AND there are character-level differences between the non-blank lines. In that case, "Align similar lines" is needed. And these other options seem redundant to that one (except the line conversion probably). – Fizz May 30 '22 at 10:33
  • 1
    The question is how to do get the identitcal diff result when comparing the same text where in one file it is in a long line and in the other with separated line (to fit a column length for example). – obeliksz Sep 28 '22 at 07:17
1

You could add a .gitattributes for your file. This would run a tool to normalize/beautify/prettify both files before comparison.

This will run .json files through json_pp before compare:

echo "*.json diff=json" >> .gitattributes
git config diff.json.textconv json_pp

Check out git documentation for details: https://git-scm.com/docs/gitattributes

Source: https://t-a-w.blogspot.com/2016/05/sensible-git-diff-for-json-files.html

JD Frias
  • 4,418
  • 3
  • 21
  • 24
  • This definitely does work to show semantic diffs, but I would still rather a winmerge-specific option that still shows the original files, but doesn't highlight the code style differences. Running a formatter can get a little slow for larger cpp files, and I'm also worried about the issues it could cause when editing diffs and saving them. I don't want to accidentally commit formatting changes if I don't need to. – MHebes Jul 10 '20 at 17:07
  • I use Beyond Compare has custom rules for comparison, and tons of other features. It is paid though, well wroth it in my opinion #notsponsored – JD Frias Jul 28 '20 at 21:32