0

I've got a large collection of files I want to keep in git. Each of these files has a header that can be automatically updated containing time-stamps and other details about when the file was last exported (they're code files exported from a specific tool)

Since the changes to the header aren't particularly meaningful on their own, I'd like to use a git pre-commit hook to exclude files where there's no change in the actual body of the code. What's the recommended way for me to perform a comparison and discard files where only the first n lines have changes.

Adam Luchjenbroers
  • 4,917
  • 2
  • 30
  • 35

1 Answers1

1

A better approach (than trying use a hook) is:

  • either to not track those files if they can be regerated
  • or track them, but use a content clean filter driver to restore their content if the changes detected are not significant enough.

For the second option, you need a script able to detect if the git diff includes other changes that in the header. If not, the script would perform a git checkout -- afile, in order to restore its content to HEAD (discarding any local changes).

The restoration of the file through that script is automated through a content filter driver, using a .gitattributes declaration.

https://i.stack.imgur.com/tumAc.png
(image from "Customizing Git - Git Attributes" from "Pro Git book"))

Once you declare that content filer driver in your local config, it will automatically, on git commit, inspect your file for you.
See a complete example in "Best practice - Git + Build automation - Keeping configs separate".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Option 2 work better in this case (the files are source code exported from a particular tool (DataStage) that includes a header with export date / time. – Adam Luchjenbroers Sep 29 '17 at 06:01