这个 git smudge/clean 过滤器有什么问题?
我想在我的 git 存储库中应用此过滤器,以便在签出期间从解决方案文件中删除一个部分,并在提交期间添加此部分。
这是我要删除或添加的部分:
GlobalSection(SubversionScc) = preSolution
Svn-Managed = True
Manager = AnkhSVN - Subversion Support for Visual Studio
EndGlobalSection
我已在 .git/info/attributes 中设置了此过滤器
*.sln 过滤器=SourceControlProvider
并且我已将这些命令添加到我的配置中
$ git config filter.SourceControlProvider.smudge "sed -e '/GlobalSection(SubversionScc)/,/EndGlobalSection/d' %"
$ git config filter.SourceControlProvider.clean "sed -n -e '/^Global$/ r ankhsvnsection ' < %"
,但是它不起作用。我做错了什么?
ankhsvnsection 是一个文本文件,与 *.sln 文件位于同一目录中
I want to apply this filter in my git repository to remove a section from a solution file during checkout and to add this section during commit.
This is the section i want to remove or add:
GlobalSection(SubversionScc) = preSolution
Svn-Managed = True
Manager = AnkhSVN - Subversion Support for Visual Studio
EndGlobalSection
I have setup this filter in my .git/info/attributes
*.sln filter=SourceControlProvider
and i have added these commands to my config
$ git config filter.SourceControlProvider.smudge "sed -e '/GlobalSection(SubversionScc)/,/EndGlobalSection/d' %"
$ git config filter.SourceControlProvider.clean "sed -n -e '/^Global$/ r ankhsvnsection ' < %"
Well, it does not work. What have i done wrong?
ankhsvnsection is a text file that lies in the same directory as the *.sln file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里看到一些问题:
两个过滤器的末尾都有
%
。这没有特殊含义,将作为额外参数传递给 sed,这可能会生成错误(除非您有一个名为
%
的文件)。过滤器应该是“流式”的(从标准输入读取并写入标准输出)。它们的定义可以包含
%f
,但它不应该真正被视为一个文件来读取或写入; Git 完成这部分工作,过滤器应该只从 stdin 读取并写入 stdout。您的干净过滤器尝试从
%f
重定向标准输入。输入数据已经在标准输入上,无需重定向。
clean 过滤器中的sed 程序使用
r
命令访问另一个文件。过滤器似乎是从工作树的根运行的,但我不确定这是否得到保证。
干净过滤器中的sed命令使用
-n
。它唯一的输出将是ankhsvnsection
文件的内容(假设输入有一个Global
行)。某些版本的sed(至少是 Mac OS X 中的(旧)BSD 版本)不允许在
r
命令的文件名后有空格(即空格)在干净过滤器的 sed 程序中的ankhsvnsection
之后)。添加、更改或删除过滤器后,您可能需要触摸、修改或删除工作树文件,然后 Git 应用该过滤器。 Git的索引记录了工作树文件的修改时间;如果它们没有改变,那么 Git 会将
git checkout -- file
和git add file
转换为无操作。如果您想查看索引的实际内容(例如检查干净的过滤器生成的内容),您可以使用 git show :0:path/from/repo/root/to/file。您通常不能为此使用 git diff ,因为它也会应用过滤器。
这些对我有用:
I see a few issues here:
You have
%
at the end of both filters.This has no special meaning and will be passed as an extra argument to sed, which will probably generate an error (unless you have a file named
%
).Filters should be “streaming” (read from stdin and write to stdout). Their definition can include
%f
, but it should not really be treated as a file to read or write; Git does that part, filters should just read from stdin and write to stdout.Your clean filter tries to redirect stdin from
%f
.The input data will already be on stdin, there is no need to redirect.
The sed program in the clean filter uses the
r
command to access another file.Filters seem to be run from root of the working tree, but I am not sure if that is guaranteed.
The sed command in the clean filter uses
-n
. Its only output will be the contents of theankhsvnsection
file (assuming the input has aGlobal
line).Some versions of sed (at least the (old) BSD version in Mac OS X) do not allow whitespace after the filename of the
r
command (i.e. the space afterankhsvnsection
in the clean filter’s sed program).After adding, changing, or removing a filter you will probably need to touch, modify, or delete your working tree files before Git will apply the filter. Git’s index records the modification time of working tree files; if they have not changed, then Git will translate
git checkout -- file
andgit add file
into a no-op.If you want to see the actual contents of the index (e.g. to check what the clean filter produced), you can use
git show :0:path/from/repo/root/to/file
. You can not usually usegit diff
for this since it also applies the filters.These worked for me: