Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 215
christiankral Avatar asked Apr 08 '18 10:04

christiankral


2 Answers

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)

like image 131
Moti Korets Avatar answered Sep 22 '22 16:09

Moti Korets


This is not possible to do with the gitignore pattern format, which is basically a slightly extended globbing syntax (as far as I know).

Also, note that files which have already been committed, but subsequently match an ignore pattern, remain in the repo, as this note explains. To stop tracking a file that is currently tracked, you have to use git rm --cached.

Therefore, you probably won't be able to avoid a shell script or somesuch implemented as a git hook to achieve what you want.

like image 20
Christoph Avatar answered Sep 21 '22 16:09

Christoph