What is wrong with this git smudge / clean filter?

I want to apply this filter in my git repository to remove a section from a solution file during validation and add this section during commit.

This is the section I want to delete or add:

GlobalSection(SubversionScc) = preSolution Svn-Managed = True Manager = AnkhSVN - Subversion Support for Visual Studio EndGlobalSection 

I set this filter in my .git / info / attributes

*. sln filter = SourceControlProvider

and I added these commands to my configuration

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

Well, that doesn't work. What did I do wrong?

ankhsvnsection - a text file that is in the same directory as the * .sln file

+6
git filter sed gitattributes
source share
1 answer

I see several problems here:

  • You have % at the end of both filters.
    This doesn't really matter and will be passed as an optional sed argument, which is likely to result in an error (if you don't have a file named % ).
    Filters should be "streaming" (reading from stdin and writing to stdout). Their definition may include %f , but in fact it should not be construed as a file for reading or writing; Git does this part, filters should just read from stdin and write to stdout.

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

  • The sed program, in a clean filter, uses the r command to access another file.
    Filters seem to run from the root of the working tree, but I'm not sure if this is guaranteed.

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

  • Some versions of sed (at least the (old) version of BSD on Mac OS X) do not allow spaces after the r command file name (i.e. the space after ankhsvnsection in pure sed program filters).

After adding, modifying, or deleting a filter, you may have to touch, modify, or delete work tree files before Git applies the filter. The Git s index records the time that the working tree files changed; if they have not changed, then Git will translate git checkout -- file and git add file to no-op.

If you want to see the actual contents of the index (for example, to check what a clean filter did), you can use git show :0:path/from/repo/root/to/file . Normally you cannot use git diff since it also applies filters.

They worked for me:

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

All Articles