Search code examples
gitshellsmartywildcardgitattributes

gitattributes: Match two files with almost identical names, differing only in an additional fixed string


Background

When working with smarty templates, you can override files by creating a copy of a file and add the string -USERMOD to it, like this:

original_file.html
original_file-USERMOD.html

Is there a way to target both files in my .gitattributes file?

Just targeting the 2nd type of file is easy:

*-USERMOD* export-ignore

Literally speaking, the rule would be like this:
"If there is a copy of any file that ends in -USERMOD.original_extension, ignore both, the copy and the original"

I can always add those files manually of course, but an automated solution would be nice.

The man page for gitignore(5) (which is referred to by gitattributes(5) for explanation of pattern matching) says:

Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag

As far as I know, shell globs can't do what I need. The final goal however is to tell git to export-ignore USERMOD-files and their originals, thus any solution that solves this will be accepted.


Solution

  • You're going to have to script this somehow, I'd strongly recommend doing it as part of archive creation. Simplest might be

    USERMOD-aware-archive() {
            git ls-files '*-USERMOD*' \
            | sed 's/$/ export-ignore/;p;s/-USERMOD//' >.git/info/attributes
            git archive "$@"
    }