Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure Git to ignore trivial changes (e.g. timestamp) in auto-generated code?

Tags:

git

c

I am working with a tool which auto-generates a large amount of C code. The tool generates code for a batch of .c and .h files at each run. For some reason, the tool isn't smart enough to recognize when the files have no substantial changes, so in many cases it simply updates a timestamp in the comments at the top of each file. Otherwise, the file remains unaltered.

When I run git status in that scenario, I sometimes see dozens or hundreds of files changed. But as I review the changes to the individual files, most of them have no real changes - just an update to the timestamp. I have to go through each file one-by-one to determine if there are any actual changes to be committed.

Is there a way to configure Git so that it can ignore inconsequential changes such as the timestamp in the header comments? Or how might I otherwise deal with this situation?

Thanks for your help.

like image 546
Ian Anderson Avatar asked Oct 17 '22 06:10

Ian Anderson


1 Answers

Is there a way to configure Git so that it can ignore inconsequential changes such as the timestamp in the header comments? Or how might I otherwise deal with this situation?

Yes; this is the purpose of a filter.

You might be familiar with git's notion of "clean" and "smudge" filters already, that's how it handles line ending conversion. When you are on a Windows computer and have Windows-style line endings in your working directory, you might set a .gitattribute like * text=auto indicating that you want files checked into the repository with "normalized" Unix-style line endings. In this case, the files will have the "clean" filter applied to convert \r\n line endings to \n style line endings. Similarly, the files will be "smudged" on checkout to convert from \n to \r\n on-disk.

You can create your own clean and smudge filters to remove (or add) data when translating between the working directory and the repository. For these files you can add an attribute:

*.c filter=autogen

And then you can configure your autogen filter, with commands to run in the "clean" (into the repository) and "smudge" (into the working directory) directions.

git config --global filter.autogen.clean remove_metadata
git config --global filter.autogen.smudge cat

(Using cat is a "noop" as far as filters are concerned).

The Pro Git book has more detailed examples of creating your own filters.

like image 180
Edward Thomson Avatar answered Oct 31 '22 16:10

Edward Thomson