-2

Git is able to detect change in file permissions (for your reference https://medium.com/@tahteche/how-git-treats-changes-in-file-permissions-f71874ca239d). below is my code for pre-commit (git hooks).

git diff --summary | grep "mode change"
if [ $? -eq 0 ]
then
  echo "file permission changed. please verify and restore the permissions"
  exit 1
else
  echo "no file permissions changed. Good to proceed. Keep it up"
fi

I want to detect before commit if any file permission have been changed. If file permission change is detected, then it should not commit the changes ad should throw an error.

  • 2
  • 1
    Also, what problem are you trying to fix? Do you find yourself accidentally committing unwanted permission changes? If so, I recommend setting `core.filemode` to false in your config (https://stackoverflow.com/a/6476550/3216427). On Windows, files are often made "executable" by default in a way that is really not useful. Running `git config --global core.filemode false` will make Git ignore that. And then when there is a file you really want to make executable, you can use `git add --chmod +x ` on just that file. – joanis Sep 21 '22 at 13:01
  • 1
    Tangentialy, [Why is testing “$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Sep 21 '22 at 13:09

1 Answers1

0

I believe this is a case of an XY problem: you are asking about a script to detect filemode changes, but what you really want to solve is to get Git on Windows to stop making all your files executable in your repos.

On Windows, files are often made "executable" by default in a way that is really not useful. This is mostly due to the fact that "executable" is not a permission bit on a file, but some other inferred property, unlike on *nix OSes.

To fix this problem, I recommend setting core.filemode to false in your config (see, e.g., Git file permissions on Windows):

git config --global core.filemode false

This will make Git ignore files that are spuriously "executable".

And then when there is a file you really want to make executable, you can use:

git add --chmod +x <filename>

on just that file.

joanis
  • 10,635
  • 14
  • 30
  • 40