Repository: pre-commit/pre-commit

Issue:
Expose an env variable that indicates if pre-commit is running
In some circumstances, it could be helpful to know inside an executable / function / script if it was invoked from pre-commit or from some other process. I wrote a hook that just printed all env variables set but I could not find an env variable like `PRECOMMIT_RUNNING` or similar. Would you consider setting such an env variable during running pre-commit? I searched old issues and documentation but I could not find anything.


Discussion:
I'd be curious to hear the concrete usecase, it seems a bit odd to need to know this information
The use case is hacky so I don't post this on GitHub to maintain good reputation 😄 (#todayInOpensource). Well, the thing is that R has a startup process that runs something like a `.bash_rc` and this may be slow, depending on the code people put in and it is in mostly not necessary for the hooks. The startup process is ran every time a hook is invoked that uses R by default. With the proposed solution above, they could run this code conditional on whether or not a hook invokes the startup process. 

Other approach is to always exclude this `.bash_rc`-like file from startup. But since shebang does [generally only take one argument](https://unix.stackexchange.com/questions/399690/multiple-arguments-in-shebang), I can't set the option for excluding (`--vanilla`) in the shebang `#!/usr/bin/env Rscript` when using the `script` language, which is the language used for [my hooks](https://github.com/lorenzwalthert/precommit). Also, this would make it impossible for people to include it again for whatever reason they have, which I may want to allow.

I understand if you don't want to support this hack 😊. It's also only a minor problem.
> The use case is hacky so I don't post this on GitHub to maintain good reputation 😄 (#todayInOpensource). 

lol, I know that feel ;)

it should be easy to add something that sets `PRE_COMMIT=1` or something -- probably would go in `pre_commit/command/run.py` if you'd like to send a patch

Resolving patch:
diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py
--- a/pre_commit/commands/run.py
+++ b/pre_commit/commands/run.py
@@ -347,6 +347,9 @@ def run(
     if args.checkout_type:
         environ['PRE_COMMIT_CHECKOUT_TYPE'] = args.checkout_type
 
+    # Set pre_commit flag
+    environ['PRE_COMMIT'] = '1'
+
     with contextlib.ExitStack() as exit_stack:
         if stash:
             exit_stack.enter_context(staged_files_only(store.directory))


Test patch:
diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py
--- a/tests/commands/run_test.py
+++ b/tests/commands/run_test.py
@@ -1028,3 +1028,12 @@ def test_skipped_without_any_setup_for_post_checkout(in_git_dir, store):
     environ = {'_PRE_COMMIT_SKIP_POST_CHECKOUT': '1'}
     opts = run_opts(hook_stage='post-checkout')
     assert run(C.CONFIG_FILE, store, opts, environ=environ) == 0
+
+
+def test_pre_commit_env_variable_set(cap_out, store, repo_with_passing_hook):
+    args = run_opts()
+    environ: EnvironT = {}
+    ret, printed = _do_run(
+        cap_out, store, repo_with_passing_hook, args, environ,
+    )
+    assert environ['PRE_COMMIT'] == '1'