这个 git smudge/clean 过滤器有什么问题?

发布于 2024-10-24 18:10:38 字数 703 浏览 12 评论 0原文

我想在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赴月观长安 2024-10-31 18:10:38

我在这里看到一些问题:

  1. 两个过滤器的末尾都有 %
    这没有特殊含义,将作为额外参数传递给 sed,这可能会生成错误(除非您有一个名为 % 的文件)。
    过滤器应该是“流式”的(从标准输入读取并写入标准输出)。它们的定义可以包含 %f,但它不应该真正被视为一个文件来读取或写入; Git 完成这部分工作,过滤器应该只从 stdin 读取并写入 stdout。

  2. 您的干净过滤器尝试从 %f 重定向标准输入。
    输入数据已经在标准输入上,无需重定向。

  3. clean 过滤器中的sed 程序使用r 命令访问另一个文件。
    过滤器似乎是从工作树的根运行的,但我不确定这是否得到保证。

  4. 干净过滤器中的sed命令使用-n。它唯一的输出将是 ankhsvnsection 文件的内容(假设输入有一个 Global 行)。

  5. 某些版本的sed(至少是 Mac OS X 中的(旧)BSD 版本)不允许在 r 命令的文件名后有空格(即空格)在干净过滤器的 sed 程序中的 ankhsvnsection 之后)。

添加、更改或删除过滤器后,您可能需要触摸、修改或删除工作树文件,然后 Git 应用该过滤器。 Git的索引记录了工作树文件的修改时间;如果它们没有改变,那么 Git 会将 git checkout -- filegit add file 转换为无操作。

如果您想查看索引的实际内容(例如检查干净的过滤器生成的内容),您可以使用 git show :0:path/from/repo/root/to/file。您通常不能为此使用 git diff ,因为它也会应用过滤器。

这些对我有用:

git config filter.SourceControlProvider.smudge "sed -e '/GlobalSection(SubversionScc)/,/EndGlobalSection/d'"
git config filter.SourceControlProvider.clean "sed -e '/^Global\$/ r ankhsvnsection'"

I see a few issues here:

  1. 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.

  2. Your clean filter tries to redirect stdin from %f.
    The input data will already be on stdin, there is no need to redirect.

  3. 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.

  4. The sed command in the clean filter uses -n. Its only output will be the contents of the ankhsvnsection file (assuming the input has a Global line).

  5. 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 after ankhsvnsection 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 and git 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 use git diff for this since it also applies the filters.

These worked for me:

git config filter.SourceControlProvider.smudge "sed -e '/GlobalSection(SubversionScc)/,/EndGlobalSection/d'"
git config filter.SourceControlProvider.clean "sed -e '/^Global\$/ r ankhsvnsection'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文