Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git clean and smudge filters don't do anything

I defined smudge and clean filters for my git repository. I tested the scripts individually and I'm pretty sure they work correctly.

But when I git commit && git push, the remote version is un-filtered.

What am I doing wrong? Also, is there a way to test whether the filter works without pushing it to a remote repository?

The repository looks like:

zsh/
|- zshrc
git/
|- gitconfig
.gitattributes
.gitconfig
config
zshrc-clean.zsh
zshrc-smudge.zsh
gitconfig-clean.zsh
gitconfig-smudge.zsh

zsh/zshrc

export HOMEBREW_GITHUB_API_TOKEN = abcdefg

git/gitconfig

[user]
    email = [email protected]

.gitattributes

zsh/zshrc   filter=zshrc
git/gitconfig   filter=gitconfig

.gitconfig

[filter "zshrc"]
    clean = zsh zshrc-clean.zsh
    smudge = zsh zshrc-smudge.zsh
[filter "gitconfig"]
    clean = zsh gitconfig-clean.zsh
    smudge = zsh gitconfig-smudge.zsh

config

git:user:email = [email protected]
zsh:HOMEBREW_GITHUB_API_TOKEN = abcdefg

configuration-scripts/gitconfig-clean.zsh

sed '/email/ s/= .*/= REPLACEME:git:user:email/' /dev/stdin

gitconfig-smudge.zsh

user_email=$(sed -n '/git:user:email/ { s/.* = //; p; }' ~/dotfiles/config)
sed "s/REPLACEME:git:user:email/$user_email/" /dev/stdin

zshrc-clean.zsh

sed '/export HOMEBREW_GITHUB_API_TOKEN/ s/=.*/=REPLACEME:zsh:HOMEBREW_GITHUB_API_TOKEN/' /dev/stdin

zshrc-smudge.zsh

HOMEBREW_GITHUB_API_TOKEN=$(sed -n '/HOMEBREW_GITHUB_API_TOKEN/ { s/.* = //; p; }' ~/dotfiles/config)
sed "s/REPLACEME:zsh:HOMEBREW_GITHUB_API_TOKEN/$HOMEBREW_GITHUB_API_TOKEN/" /dev/stdin

Test the filters

zsh zshrc-clean.zsh < zsh/zshrc > zshrc-temp
cat zshrc-temp
zsh zshrc-smudge.zsh < zshrc-temp

zsh gitconfig-clean.zsh < git/gitconfig > gitconfig-temp
cat gitconfig-temp
zsh gitconfig-smudge.zsh < gitconfig-temp
like image 531
shadowtalker Avatar asked Nov 11 '15 07:11

shadowtalker


1 Answers

From the visible information I can only assume that the problem is in trying to configure clean and smudge filters in the wrong place.
I see the lines in .gitconfig file, but unless it is also a home directory, it is not the same as .git/config, where Git looks for them.

Try executing this command to see if Git sees the filter:

$ git config filter.zshrc.clean
zsh zshrc-clean.zsh

If you see nothing instead, the filter is not actually configured. You can use git config filter.zshrc.clean "zsh zshrc-clean.zsh" instead of editing config file manually.

Unfortunately, if a filter mentioned in .gitattributes is missing from actual git config, it is silently ignored.

Here is a direct way to inspect if the filter is being called while adding new or changed file (remove and re-add if it is already in index); Linux-only:

$ strace -qqqqqqq -fe execve -e signal=none git add  zsh/zshrc
execve("/home/vi/bin/git", ["git", "add", "zsh/zshrc"], [/* 29 vars */]) = 0
[pid  7061] execve("/bin/sh", ["/bin/sh", "-c", "zsh zshrc-clean.zsh", "zsh zshrc-clean.zsh"], [/* 31 vars */]) = 0
[pid  7062] execve("/usr/bin/zsh", ["zsh", "zshrc-clean.zsh"], [/* 31 vars */]) = 0
[pid  7063] execve("/bin/sed", ["sed", "/export HOMEBREW_GITHUB_API_TOKE"..., "/dev/stdin"], [/* 31 vars */]) = 0

One can inspect the resulting blob after the adding:

$ git ls-files -s
100644 4138315597d69f0da1deae1b6eff0c30dc447e9c 0   zsh/zshrc
$ git cat-file blob 4138315597d69f0da1deae1b6eff0c30dc447e9c
export HOMEBREW_GITHUB_API_TOKEN =REPLACEME:zsh:HOMEBREW_GITHUB_API_TOKEN

If the suspected problem is actually in .gitattributes, you can check if the attribute is actually applied to the file:

$ git check-attr -a zsh/zshrc 
zsh/zshrc: filter: zshrc

To get further help, you can:

  1. Specify git --version and the operating system;
  2. Publish and link entire or partial project directory (including .git) if possible, or try to reproduce the failure with filter on some simple test repository (which can be published).
  3. Try to set up (or inspect the setup) and use filters completely from console and include the entire output in the question.

After ensuring the clean filter works for one file, you can continue with the other file and smudge filters.

like image 105
Vi. Avatar answered Sep 20 '22 13:09

Vi.