Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use git filter.gitignore.clean to filter both branches during a diff

Tags:

git

sed

filter

Situation

I am putting htm files into git. Then I'm using git diff to compare them. There are differences between the files that I'm not interested in, such as meta or comments tags.

I'm using this approach generally: How to tell git to ignore individual lines, i.e. gitignore for specific lines of code

What I've Done So Far

Here are the steps I've taken:

  1. Created gitattributes file in <project root>/.gitattributes
  2. Added a line defining the files to be filtered:
    • *.htm filter=gitignore, i.e. run filter gitignore on all .htm files
  3. Defined filter in my gitconfig:
    • $ git config --global filter.gitignore.clean "sed 's/<meta.*>//g'", i.e. delete these lines
    • $ git config --global filter.gitignore.smudge cat, i.e. do nothing when pulling file from repo
  4. Then I do a diff on an arbitrary file: git diff A..B -- file.htm > diff.txt

Depending on which branch I'm currently on, I get different results:

  • On branch A, I see all meta tags of branch B still, and they appear as additions.
  • On branch B, I see all meta tags of branch A still, and they appear as deletions.

The Question

How can I make it so that regardless of which branch I'm on ALL branches get that filter applied to them?

like image 289
raiderrobert Avatar asked Nov 10 '22 04:11

raiderrobert


1 Answers

These filter do not care about the branch you are on. Similarly, git diff A..B -- file.htm will not depend on the current branch, as you are specifying git repository trees via the branch heads (commits) A and B directly.

Quoting man git-config

filter.<driver>.clean: The command which is used to convert the content of a worktree file to a blob upon checkin.

filter.<driver>.smudge: The command which is used to convert the content of a blob object to a worktree file upon checkout.

So your sed script is only run when you "add a file to git". It does not alter existing commits and should not affect a diff between them. It looks like the meta tags are still present in the files of your branch B, because they were added before you configured your filters.

If you can still modify branch B, you might try the following after making a backup.

find . -name \*.htm -exec rm {}
git checkout B
git reset --hard

Your repository should now show removals of meta tags to commit.

like image 100
andi5 Avatar answered Nov 12 '22 17:11

andi5