How to setup gitattributes to filter part of a file?

白昼怎懂夜的黑 提交于 2019-12-01 09:43:58

问题


I am using Visual Studio 2010 and git (git svn). My coworker uses pure svn with the Ankhsvn plugin.

When i try to open the file with the Git Source Control Provider enabled, Visual Studion complains:

Source Control Plug-in
The active solution or project is controlled by adifferen source control plug-in than the one you have selected. If you change the source control plu-in, the active solution or project will be closed.
Do you want to continue?

When i click yes an empty solution is opened. When i click no, all the git specific icons next to the solution items are missing (i still see the branch name in the solution explorer) and i get prompted about the Source Control Plug-in every time i open the solution.

When i clone the svn repository which my coworker created the solution sln file contains the following

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

I have deleted this section and now the solution opens without problems when the Git Source Control Provider is activated but complains when you select the Ankhsvn Provider - the situation is reversed.

I would like to tell git to remove this part of the .sln file during a pull (git svn fetch) and add it when i commit (git svn dcommit). Is this possible through gitattributes and how to do it?

EDIT:

I have now added

solutionname.sln filter=ankhsvn

to $GIT_DIR/info/attributes

and

[filter "ankhsvn"]
  clean=sed '/^Global$/ r ../ankhsvnsection '
  smudge=sed '/GlobalSection(SubversionScc)/,/EndGlobalSection/d '

to my .git/config file.

ankhsvnsection contains the secton that is removed by the smudge operation. I does not seem to do anything?!


回答1:


I do not know how to do it exactly on windows, but I do know how I would do it on *nix.

I would write a sed/bash script (called myscript) which removes those lines from the file when they are checked-out by git using a git "smudge" filter.

So in "$GIT_DIR/info/attributes" I would add:

{sln filename} filter=myfilter

And define the smudge-part of the filter in the "$GIT_DIR/config" file:

[filter "myfilter"]
    smudge={path to myscript}

For Linux I would even just call sed directly like this:

[filter "myfilter"]
    smudge=sed '/GlobalSection(SubversionScc)/,/EndGlobalSection/ d'

This is maybe something you can take over in windows using git bash.

Be careful when you want to checkin changes to the file, as it will checkin the "smudged" version. You can define a "clean" filter for this which adds the svn statements again.



来源:https://stackoverflow.com/questions/5270243/how-to-setup-gitattributes-to-filter-part-of-a-file

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