totokaka
totokaka

Reputation: 2324

Handling meaningless changes with git

I have some config files which I would like to version as a part of a git repo, but the order of the fields in the config changes some times when I run the software. The order of the fields does not matter whatsoever to the software, so I would like for it to not matter for git either.

It would pollute my commit history if I were to commit these changes, and the files pollute my git status otherwise. The ideal option would be something like manual tracking of files.

How should i handle files that changes in meaningless ways?

Upvotes: 0

Views: 71

Answers (1)

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

There's not a good general solution to this problem. git wants to use hashes to determine "same"-ness and a re-ordering of the properties will always change the hash.

What I suppose you could do, if the order in the file truly doesn't matter, is to use a filter to sort it when adding it to the index. This assumes that the order of the entire file is irrelevant, which may or may not suit your file format.

First you would create a .gitattributes file in your project root, with an entry like

path/to/config/file filter=sorted

You can use wildcards (with same meaning as in .gitignore), but one way or other you want to associate the filter with each of your config files where order doesn't matter. This should be added and commited.

Then you have to create the filter config, so you issue the command

git config filter.sorted.clean sort

Now, no matter what order the lines appear in your work tree, they will be in a single (sorted) order in the index (and when committed). (Of course that also means a fresh checkout of the file - such as after a clone - will have the file in sorted order.)

Again this only works for file formats that can be safely sorted - such as a key/value properties file that doesn't have comments on separate lines. But "order doesn't matter" could mean other things. Like maybe you have XML files wherein the order of top-level elements doesn't matter, but each such element spans multiple lines and must be kept together. Or something else entirely...

In those cases, you would need a tool that can accept the file content on STDIN, normalize the representation in a way that eliminates "unimportant" differences, and print the result on STDOUT. You would use that as your clean filter instead of sort.

Upvotes: 3

Related Questions