SiriO
SiriO

Reputation: 95

git ignore a specified type of change to file by rule

I have a repository which includes files that are generated when a third party executable is run. I need to track the files that are generated by this executable, to allow me to sync the executable settings between users.

Let us call this file "executable_settings.json"

The file can change in 2 ways:

  1. A settings change that needs to be added to the repository
  2. A run-time change which is generated every time that the executable is run and changes the file in a specified way that can be captured by regex (e.g. "Edited On: XXXX-YY-ZZ"). I do not want these changes to be in the repo.

Is there a way to get git to automatically ignore changes to executbale_settings.json, according to a rule? I would like to find a way to have changes of type 2 not appear as changes to git unless there was also a change of type 1 at the same time.

Upvotes: 0

Views: 361

Answers (1)

bk2204
bk2204

Reputation: 76964

The best way here is to use a smudge and clean filter. These are outlined in the gitattributes(7) documentation, but to summarize, if you write an entry in .gitattributes like so:

executable_settings.json  filter=exec-settings

and then add the following to your config:

[filter "exec-settings"]
    clean = sed -e '/Edited On:/d'

then when you add the file to Git with git add, it will deleted all the lines which contain “Edited On”. Note that this won't intrinsically prevent them from being marked modified by git status, but it will prevent those lines from being checked in.

Note that you can use a different command, such as jq or a script, that would filter out the entries you don't want. Once you're happy with the filter, you should add .gitattributes, run git add --renormalize . to fix the files in the repository, and commit.

I should point out that this is generally local to your system. There's no way to include this configuration in the repository such that it's automatically invoked. This is because doing so would allow arbitrary code execution. However, you could add a setup script to your repository that installs this configuration locally as part of your project.

Upvotes: 1

Related Questions