Home > Back-end >  Dynamic .gitignore configs to follow the same pattern over and over
Dynamic .gitignore configs to follow the same pattern over and over

Time:03-02

git here.

My project structure will look something like this:

app/
    README.md
    .gitignore
    src/
        <all source code goes here>
    docs/
        <docs go here>
    misc/
        examples/
            <examples go here>
        fizzbuzz/
            properties/
            configs/

The misc/fizzbuzz/properties/ directory is very special and I am hoping to be able to set up some special .gitignore configurations for it. In this directory I will have many child directories, like so:

app/
    README.md
    .gitignore
    src/
        <all source code goes here>
    docs/
        <docs go here>
    misc/
        examples/
            <examples go here>
        fizzbuzz/
            properties/
                abc/
                def/
                ghi/
                ...etc.
            configs/

Each of these child directories (of properties/) will follow the same pattern:

abc/
    abc.properties
    abc.txt
    many many other files...
def/
    def.properties
    def.txt
    many many other files...
ghi/
    ghi.properties
    ghi.txt
    many many other files...

So in each child directory there will be:

  1. A properties file whose name is the exact same as the name of the child directory it is a member of (ghi.properties under properties/ghi/, etc.); and
  2. A txt file whose name is also the exact same as the name of the child directory it is a member of (ghi.txt under properties/ghi/, etc.); and
  3. Many, many other files. Some will be ghi.* (a "ghi" file, just not of type properties or txt). Some will be *.properties or *.txt (other properties files or txt files, just not ghi.properties or ghi.txt, etc.). Some will be named all over the place (foobar.mp4, flam.csv, etc.). But the thing these files have in common is that they are not of the form "<parent-dir-name>.properties" or "<parent-dir-name>.txt".

I am trying to configure .gitignore to ignore everything in these child directories except for the properties and txt file that matches the name of the directory they live inside of.

Can anyone help nudge me in the right direction?

CodePudding user response:

Composing a simple .gitignore file by hand seems simple:

abc
!abc/abc.properties
!abc/abc.txt
def
!def/def.properties
!def/def.txt
# ...etc.

A short Bash shell script to generate such .gitignore file could look like this, but a more readable version with basename would maybe be clearer:

cd app/...../properties
for i in */; do printf "%s\n" "${i%/}" '!'"$i${i%/}."{properties,txt}; done > .gitignore

Such a script could be added to git-commit hooks or to the automation that generates the folders.

  • Related