3

I need to ignore some lines from some files which contains timestamp when thoses files are generated. Sometimes we generates thoses files but the only change in the file is the timestamp which we don't care and the file should not be targeted to be commited by the git status.

I know I can use git filters like on this topic : How to tell git to ignore individual lines, i.e. gitignore for specific lines of code

But in that case it's deleting lines when I do git add. I want thoses lines to be commited if there is another change in the file than the timestamp (timestamp line will be ignored). But if the change is only the timestamp line I don't want the file to be commited.

How I can do that using git filters ?

Thanks

1 Answers1

5

Use text conversion to exclude noise when looking for differences, then you can add only the files with changes you care about. git add doesn't automate running diffs to detect changes, it's done often and needs to be fast, but scripting it's easy.

Set a custom diff attribute value on files where you're (un)interested in specific differences, configure that diff's text conversion, run your diffs to detect files that have changes you care about, add those.

Say sed 's,^#Timestamp:.*',#Timestamp:<whatever>,' will make all timestamp lines identical, so if you run that on content before checking for differences, timestamp changes won't show up, even though they're there in the files.

Put

*.xyz diff=notimestamp

in an attributes file, for repo-local work like initial testing and such .git/info/attributes is the right spot.

Then configure the notimestamp diff preprocessing in .git/config:

[diff "notimestamp"]
        textconv = "sed 's,^#Timestamp:.*,#Timestamp:<whatever>,'"

and now whenever git runs a full diff on a *.xyz file it won't see timestamp changes. So you can

git diff | sed -n '/^diff/s,.* b/,,p'

to see a list of files that have changes you do care about, and add those (you could for instance replace the diff header with git add rather than nothing and just run the generated commands).

jthill
  • 55,082
  • 5
  • 77
  • 137
  • This sounds nice but I really need thoses change to be transparent and automatic. Because I am developing a tool which generates airflow dags (which countains timestamp) and automatically push changes and new dags, if they have changed, in the right repository (there is more than one repo). Is there a way to be sure git ignore change on some lines without deletings them when we commit them ? I hope I am more clear. – David Tarum Sep 09 '20 at 20:51
  • The entire point of scripts is to automate, this procedure trivally reduces to `git diff | sh` as it stands, and you can add a git alias to make say `git publish` do that or anything else you want. Can you explain what "transparent" means? – jthill Sep 09 '20 at 21:00
  • 1
    To answer the question in your comment directly, "Is there a way to be sure git ignore change on some lines without deletings them when we commit them", yes, there is: ask it to do the above procedure, in whatever way's most convenient. Have your tool do the above `git diff | sed` and add only the files that have changes you care about. – jthill Sep 09 '20 at 21:14
  • By transparent I mean that end users should not have to take care about those timestamp. And their repository should not have commit which unclude file with only timestamp change. – David Tarum Sep 09 '20 at 21:26
  • Seems to me they can just use your tool, that you've built to do what they want. Clearly you're seeing some problem with that, but I can't figure what it is. – jthill Sep 09 '20 at 21:31
  • So I have test your solution, I have just changed few things. I have a filter : [diff "gitignore"] textconv = "sed '/#gitignore/c\\changeToBeIgnored'" So when I call git diff it's change my lines I want to be ignored by changeToBeIgnored. If I run your command "git diff | sed -n '/^diff/s,.* b/,,p'" I only see files without this change. But, if there is a change in another line than the timestamp the file will not appear in the git diff ... – David Tarum Sep 11 '20 at 15:17
  • That's right, if you change what diff sees, you'll change what it shows you in its results. It's still in the file. If this turns out to be a real problem, you can pull the source to git's (or any other) diff and make your own based on that, and configure git to use your diff-with-internalized-blinding as an external diff driver. – jthill Sep 11 '20 at 15:52