.gitattributes smudge and clean filters as a part of the repository

最后都变了- 提交于 2019-12-04 17:30:20

问题


I have a number of template files in my git repository which change at different rates. These are then used to generate HTML pages. If someone reports a problem, I want them to be able to tell me which version of the template file has the problem. Currently, I manually enter the date into the file when I change it. Or, well, I try to. I forget most of the time.

At least in theory, I should be able to use git smudge and clean filters to fix the files and insert the date of last update automatically. This would be great.

Except that I develop on one machine, and when I'm ready, I push/pull to a different machine.

How do I get the smudge and clean filters to show up on the other machine? I don't want to have to add odd scripts to the path; this is only needed for this one repository, so I want to make it entirely self-contained. Everything online says "add this filter definition to your ~/.gitconfig, then add these scripts to your path, then set up the repository .gitattributes file". I want it so that if I go to a new computer and clone the repository, all of the clean and smudge is configured automatically.

Has anyone done this?


回答1:


As mentioned in git clone, you can setup a template directory which will declare those configuration.
Ie that template folder would have a minimal .gitconfig with those smudge/clean directives:

filter.manage_date.smudge ${GIT_TEMPLATE_DIR}/smudge-script
filter.manage_date.clean ${GIT_TEMPLATE_DIR}/clean-script

Simply set (export) a GIT_TEMPLATE_DIR environment variable referencing that template folder.

By using an absolute path starting with ${GIT_TEMPLATE_DIR}, you won't need to add those scripts to a $PATH. And that script path can differ from machine to machine: each one can have its own ${GIT_TEMPLATE_DIR} path.

Finally, the .gitattributes is part of your repo, so nothing to do there.

With that setup, you can "go to a new computer and clone the repository, all of the clean and smudge is configured automatically."
(provided that new computer has its template-dir pre-populated)




回答2:


Specify a template that does what you want when cloning, or supply a repo-setup script and type sh repo-setup after cloning, or put the necessary commands in a paragraph in the README and say V}!sh or however your editor pipes commands through the shell, or use git archive for distribution rather than git push, since that supports arbitrary keyword substitution. Turning a bare git clone into a code-injection vector seems like a bad price to pay to avoid trivial inconveniences like this.



来源:https://stackoverflow.com/questions/26125573/gitattributes-smudge-and-clean-filters-as-a-part-of-the-repository

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