0

I'm looking to write a script that monitors all files/folders tracked by git to notify me if any of them have been changed. I'm looking for file changes and not necessarily staged changes.

I've been combing through various SO questions and documentations, but I can't find any relevant examples for implementing a file system watcher based off of git that would be efficient, since I might want to implement this across a multitude of repositories and possibly hundreds of files all on one system without being extremely inefficient.

Is git even the right solution for this? Would I be better off using fswatch even across many, many files?

Many thanks

SSBakh
  • 1,487
  • 1
  • 14
  • 27
  • 1
    Git itself doesn't watch the file system and as such also doesn't provide any hooks when files change. It only inspects the file system whenever a git command is executed. If you need to be notified about changes proactively, you'll need to use another tool. Many IDEs will watch the project directory like you describe though, so maybe that's a possible approach? What are you trying to achieve, it's possible that this is a [XY problem](https://xyproblem.info). – Joachim Sauer May 30 '21 at 12:44
  • @JoachimSauer So you're implying that unstaged changes are not detected on file write and only when another git command is executed? – SSBakh May 30 '21 at 12:45
  • 1
    Yes, that's what I'm saying. – Joachim Sauer May 30 '21 at 12:46

1 Answers1

2

Git itself doesn't watch the file system (*) and as such also doesn't provide any hooks when files change. It only inspects the file system whenever a git command is executed.

Therefore there aren't (and can't be) any hooks that execute on file change. You'll need to find another way to watch the file system, if you need this.

(*) Apparently there is a way to actually use file system monitoring in git, but that only seems to be used to speed up git status (and other operations) and still doesn't mean that git actively monitors changes. It just replaces "check each file for modification" with "check the list of changed files since the last time for modification".

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 1
    There are some file system monitoring things for Git, which are still somewhat experimental. They probably won't help the OP any though. – torek May 31 '21 at 09:45
  • @torek: if you mean the `core.fsmonitor` feature: I didn't know of that and added a mention to my answer. But from what I read this does indeed not do what OP wants. – Joachim Sauer May 31 '21 at 10:09
  • Yes. The fsmonitor stuff is ... not ready for prime time, I would say. :-) It's really hard to do this well: different OSes have different capabilities and methods of doing FS monitoring, and if you have multiple repositories, it would be nice to have a single monitor for multiple repos. There are potential security / privacy issues as well. It's a nice idea that maybe we can avoid stat-ing a million files in a big checkout, but it's hard. – torek May 31 '21 at 10:38