17

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?

Ricky Morse
  • 458
  • 4
  • 8
  • You could set up a template directory. That way you could propagate your config settings. But it would be to all git repos. http://stackoverflow.com/questions/16658087/automatically-add-gitignore-and-hooks-on-git-init/16658321#16658321 – Klas Mellbourn Sep 30 '14 at 18:17
  • Another good use case are [putting Jupyter (formerly IPython) notebooks in git](https://github.com/jond3k/ipynb_stripout), which require filters to avoid committing loads of binary data as output changes. Having the filter scripts be part of the repo (or at least referenced without requiring any extra elbow grease) makes it a lot easier to share and modify these notebooks. – Andrew Mao Jun 15 '16 at 03:47

2 Answers2

4

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)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

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.

jthill
  • 55,082
  • 5
  • 77
  • 137