Disable git fsmonitor in git_worker to fix hangs when fetching repositories

This commit adds the `-c core.fsmonitor=false` arguments to all git commands (excluding `git init`) in `git_worker.bzl`. The purpose of this is to disable git from spawning a file system monitor, when many of these are spawned on a machine that is already under load or out of file descriptors, the git commands will hang indefinitely.

Fixes https://github.com/bazelbuild/bazel/issues/21438

Closes #23626.

PiperOrigin-RevId: 675472544
Change-Id: I79e0b6d1e406a9076cdb810747c382de52face1a
diff --git a/tools/build_defs/repo/git_worker.bzl b/tools/build_defs/repo/git_worker.bzl
index 146929e..acadc82 100644
--- a/tools/build_defs/repo/git_worker.bzl
+++ b/tools/build_defs/repo/git_worker.bzl
@@ -180,14 +180,14 @@
     return _git(ctx, git_repo, "log", "-n", "1", "--pretty=format:%cd", "--date=raw")
 
 def _git(ctx, git_repo, command, *args):
-    start = ["git", command]
+    start = [command]
     st = _execute(ctx, git_repo, start + list(args))
     if st.return_code != 0:
-        _error(ctx.name, start + list(args), st.stderr)
+        _error(ctx.name, ["git"] + start + list(args), st.stderr)
     return st.stdout
 
 def _git_maybe_shallow(ctx, git_repo, command, *args):
-    start = ["git", command]
+    start = [command]
     args_list = list(args)
     if git_repo.shallow:
         st = _execute(ctx, git_repo, start + [git_repo.shallow] + args_list)
@@ -196,8 +196,11 @@
     return _execute(ctx, git_repo, start + args_list)
 
 def _execute(ctx, git_repo, args):
+    # "core.fsmonitor=false" disables git from spawning a file system monitor which can cause hangs when cloning a lot.
+    # See https://github.com/bazelbuild/bazel/issues/21438
+    start = ["git", "-c", "core.fsmonitor=false"]
     return ctx.execute(
-        args,
+        start + args,
         environment = ctx.os.environ,
         working_directory = str(git_repo.directory),
     )