5

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?

christiankral
  • 393
  • 2
  • 7
  • 3
    I don't think its possible with simple ignore pattern. Maybe writing a pre-commit script that ignores those folders is a solution. You think that would work for you? – Moti Korets Apr 08 '18 at 10:54
  • Some precisions about "shall be excluded from git commit" : do you want to : 1. have all other files in the directory be deleted from your next commit ? 2. keep the other existing files, just never commit any modifications on them ? – LeGEC Apr 08 '18 at 21:28
  • @MotiKorets I agree this may be a good option. The pre-commit script could even delete the unused directory so that for OpenModelica I can keep a clean repository. – christiankral Apr 09 '18 at 08:12
  • 1
    I actually requested a new OpenModelica feature in https://trac.openmodelica.org/OpenModelica/ticket/4854 which may be equivalent to using a pre-commit script – christiankral Apr 09 '18 at 08:14
  • @LeGEC Option 1. is my choice here. – christiankral Apr 09 '18 at 08:14
  • Dymola has the flag `Advanced.AutoDeleteDirectories` which seems to do just that! Maybe OpenModelica should have a similar flag/setting? – matth Nov 28 '18 at 12:31

2 Answers2

5

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)

Moti Korets
  • 3,738
  • 2
  • 26
  • 35
  • 1
    Note that adding the folder to gitignore after it's already in the repo won't have an effect, see my answer (gitignore only affect files/directories not yet tracked). – Christoph Apr 09 '18 at 08:59
  • @MotiKorets I tried your pre-commit hook using `echo $mo_backup_folders | xargs rm -rf` but it unfortunately also deletes directory `Sub1` -- which shall be kept. Only directory `Examples` shall be deleted. – christiankral Apr 09 '18 at 09:53
  • I am not that good in bash programming to get this done; @MotiKorets do you have a proposal for me? Thanks. – christiankral Apr 09 '18 at 09:54
  • It echoes `.git/hooks/pre-commit: 3: .git/hooks/pre-commit: [[: not found`; this also happens, when the script `.git/hooks/pre-commit` is started manually – christiankral Apr 09 '18 at 10:43
  • What happens when the package `Examples` is deleted. The original file `Sub1/Examples/package.mo` is renamed to `Sub1/Examples/package.bak-mo`. – christiankral Apr 09 '18 at 10:45
2

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.

Christoph
  • 5,480
  • 6
  • 36
  • 61