Git clean and smudge filters don't do anything

Deadly 提交于 2020-05-25 11:24:02

问题


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 = me@example.com

.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 = me@example.com
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

回答1:


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.



来源:https://stackoverflow.com/questions/33646172/git-clean-and-smudge-filters-dont-do-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!