清洁并弄脏过滤器无济于事 [英] Git clean and smudge filters don't do anything
问题描述
我为git储存库定义了污迹和清洁过滤器.我对脚本进行了单独测试,我很确定它们可以正常工作.
I defined smudge and clean filters for my git repository. I tested the scripts individually and I'm pretty sure they work correctly.
但是当我git commit && git push
时,远程版本未过滤.
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?
存储库如下:
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
测试过滤器
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
推荐答案
从可见信息中,我只能认为问题出在试图在错误的位置配置清洁和弄脏的过滤器.
我看到了.gitconfig
文件中的行,但是除非它也是主目录,否则它与.git/config
不同,Git在其中查找它们.
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.
尝试执行此命令以查看Git是否看到过滤器:
Try executing this command to see if Git sees the filter:
$ git config filter.zshrc.clean
zsh zshrc-clean.zsh
如果您什么也没看到,则说明该过滤器并未实际配置.您可以使用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.
不幸的是,如果实际git config中缺少.gitattributes
中提到的过滤器,它将被静默忽略.
Unfortunately, if a filter mentioned in .gitattributes
is missing from actual git config, it is silently ignored.
这里是检查添加新文件或更改文件时是否正在调用过滤器的直接方法(如果已在索引中,则删除并重新添加);仅限Linux:
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
添加后,您可以检查生成的blob:
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
如果怀疑的问题确实存在于.gitattributes
中,则可以检查该属性是否确实应用于文件:
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:
- 指定
git --version
和操作系统; - 如果可能,发布并链接整个或部分项目目录(包括
.git
),或者尝试在一些简单的测试存储库(可以发布)上使用过滤器重现故障. - 尝试设置(或检查设置)并完全从控制台使用过滤器,并将整个输出包含在问题中.
- Specify
git --version
and the operating system; - 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). - 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.
这篇关于清洁并弄脏过滤器无济于事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!