Search code examples
gitgitignoremodelicaopenmodelica

Exclude directories in .gitignore which contain a particular file


When a package, e.g., Examples, is deleted in OpenModelica, the underlying directory is not removed. Instead, directory Examples contains a file named package.bak-mo to indicate that the directory is not used any more. So a backup is kept. If a file package.bak-mo exists in a folder, this folder plus its files shall be excluded from the git commit process.

├── Sub1
│   └── package.mo
│   └── package.order
│   └── Examples
│       └── package.bak-mo
│       └── otherfile.mo
│       └── package.order
├── Sub2
│   └── package.mo
│   └── package.order
│   └── Demo
│       └── otherfile.mo
│       └── package.mo
│       └── package.order

In this example the following directory plus files shall be excluded from git commit:

│   └── Examples
│       └── package.bak-mo
│       └── otherfile.mo
│       └── package.order

All other files and directories shall still be included in a possible git commit.

Note: This issue is triggered by https://trac.openmodelica.org/OpenModelica/ticket/4854

I could not figure out how to make this working. Any ideas here?


Solution

  • This can be done with pre-commit hook . To create the hook run

    $ touch .git/hooks/pre-commit  
    $ chmod a+x .git/hooks/pre-commit   
    

    Add the following code

    #!/bin/bash
    path_with_pattern=$(git diff --cached --find-copies --find-renames --name-only --diff-filter=ACR | grep "\.bak-mo$")
    if [[ -n $path_with_pattern  ]];
    then
      for path in $path_with_pattern; do
        folder=$(echo $path | sed 's/\/[^\/]*\.bak-mo$//')
        echo "Found OpenModelica backup folder: $folder"
        git reset HEAD $folder
        echo "Unstaged: $folder"
        rm -rf $folder  # Consider adding some confirmation here (use at your own risk)
        echo "Removed: $folder"
        # or safer option add to .gitignore instead of removing
        # echo $folder >> .gitignore
      done
    fi
    

    Caution: This code needs more testing i haven't tested this code as much as needed (only against the example you provided)