3

git thinks that two versions of a .json file is different, but actually their data is not:

@@ -469,9 +479,9 @@
       "integrity": "sha1-+GzWzvT1MAyOY+B6TVEvZfv/RTE=",
       "dev": true,
       "requires": {
+        "JSONStream": "1.3.1",
         "combine-source-map": "0.7.2",
         "defined": "1.0.0",
-        "JSONStream": "1.3.1",
         "through2": "2.0.3",
         "umd": "3.0.1"
       }

How can we tell git to ignore such differences for JSON files?

Note:

Following code from Tomasz Wegrzanowski's Blog:

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

is not working and causing hangs on git diff

ceremcem
  • 3,900
  • 4
  • 28
  • 66
  • Is this `.json` file being auto generated somehow and the order of the elements are dynamic based on the auto generation? – sadmicrowave Sep 12 '17 at 20:51
  • Yes, it is the `package-lock.json` file generated by `npm`. – ceremcem Sep 12 '17 at 21:09
  • I'm thinking you need to read this: https://stackoverflow.com/questions/44206782/do-i-commit-the-package-lock-json-file-created-by-npm-5, you are trying to avoid tracking these changes on this file, but by all other accounts, you should be tracking it... – sadmicrowave Sep 13 '17 at 13:12
  • If you think about it, git is intended to track changes on files (obviously), you can exclude files you don't care about, but `package-lock.json` is a file you DO care about. For all you know, NPM may be reorganizing the list of required packages based upon dependency (i.e. depending on your setup and other packages, maybe it thinks `JSONStream` should be required first). You would want to track this change. – sadmicrowave Sep 13 '17 at 13:28
  • 1
    That would be true if it was not a `Object`, but an `Array`. `{a: 1, b: 2}` is equal to `{b: 2, a: 1}` where `[1, 2]` is different from `[2, 1]`. That's why these versions are exactly identical. – ceremcem Sep 13 '17 at 13:37
  • True, I'm just trying to provide an example of a potential reason you should continue tracking changes to this file. These files aren't exactly identical, as you can see. It is true, they are 'evaluated' the same by the interpreter, but git sees them as different, and it's doing it's job properly. I'll keep researching for you though. – sadmicrowave Sep 13 '17 at 13:41
  • Thank you! I'm also searching for a way to be able to provide custom bash functions for calculating difference for a specific file extensions via `.gitattributes` or something like that. – ceremcem Sep 13 '17 at 13:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154352/discussion-between-ceremcem-and-sadmicrowave). – ceremcem Sep 13 '17 at 13:51

2 Answers2

2

I thing you config hangs cause json_pp doesnt accept the filename as parameter. That means in your config json_pp waits for inputs on std-input stream, and cause you don`t deliver any input streams your git diff hangs In my gitconfig I have implemented json_pp like this:

[diff "json"]
         textconv = "cat \"$1\" | json_pp --json_opt=canonical,pretty" 

For me this works fine. If you like you can change the --json_opt values to have you favorite output-format. But if the order of keys change this config will still show a difference.

Tested in git-version 2.16.1.windows.1

Radon8472
  • 4,285
  • 1
  • 33
  • 41
0

git thinks that two versions of a .json file is different, but actually their data is not

They are different, textually different. This is the only way git versions files, by design.

How can we tell git to ignore such differences for JSON files?

You cannot. The link you refer to is only about transforming JSON files while viewing them using the git-diff tool.

The widely accepted practice to commit JSON and other similar configuration files (xml etc.) that are often machine generated is by enforcing a sort order. In your case it looks like you used a tool that did case-sensitive sorting whereas the file was originally sorted in case-insensitive manner.

CervEd
  • 3,306
  • 28
  • 25