Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 853
Ricky Morse Avatar asked Sep 30 '14 16:09

Ricky Morse


People also ask

What is Git smudge filter?

In git lfs terms, 'smudge' is the process of getting the actual data for the pointers that are stored locally. By installing git lfs with the --skip-smudge option, you are setting the filter smudge = git-lfs smudge --skip -- %f in the global . gitconfig file in your home directory.

What is a .gitattributes file?

DESCRIPTION. A gitattributes file is a simple text file that gives attributes to pathnames. Each line in gitattributes file is of form: pattern attr1 attr2 ... That is, a pattern followed by an attributes list, separated by whitespaces.

What is LFS smudge?

The Git smudge filter is what converts the LFS pointer stored in Git with the actual large file from the LFS server. If your local repository does not have the LFS object, the smudge filter will have to download it. This means that network issues could affect the smudge filter.

Where is the .gitattributes file?

These path-specific settings are called Git attributes and are set either in a . gitattributes file in one of your directories (normally the root of your project) or in the . git/info/attributes file if you don't want the attributes file committed with your project.


1 Answers

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)

like image 88
VonC Avatar answered Oct 18 '22 20:10

VonC