git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] githooks: discuss Git operations in foreign repositories

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 9, 2023, 04:58 UTC
Message-ID
<xmqqwn5wuwvs.fsf@gitster.g>
In-Reply-To
<pull.1457.git.1673171924727.gitgitgadget@gmail.com>
"Eric Sunshine via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 19 quoted lines
> diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt
> index a16e62bc8c8..6e9a5420b7c 100644
> --- a/Documentation/githooks.txt
> +++ b/Documentation/githooks.txt
> @@ -31,6 +31,17 @@ Hooks can get their arguments via the environment, command-line
>  arguments, and stdin. See the documentation for each hook below for
>  details.
>  
> +If your hook needs to invoke Git commands in a foreign repository or in a
> +different working tree of the same repository, then it should clear local Git
> +environment variables, such as `GIT_DIR`, `GIT_WORK_TREE`, etc., which could
> +interfere with Git operations in the foreign repository since those variables
> +will be referencing the local repository and working tree. For example:
> +
> +------------
> +local_desc=$(git describe)
> +foreign_desc=$(unset $(git rev-parse --local-env-vars); git -C ../foreign-repo describe)
> +------------
> +
It is an excellent idea to add the above, but
 * I think adding it one paragraph earlier may make it fit better.
 * The paragraph, after which the above gets inserted, can use a bit
   of enhancement.
That is, something like this?
 Documentation/githooks.txt | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
diff --git c/Documentation/githooks.txt w/Documentation/githooks.txt
index a16e62bc8c..f3d0404164 100644
--- c/Documentation/githooks.txt
+++ w/Documentation/githooks.txt
@@ -25,7 +25,20 @@ Before Git invokes a hook, it changes its working directory to either
 $GIT_DIR in a bare repository or the root of the working tree in a non-bare
 repository. An exception are hooks triggered during a push ('pre-receive',
 'update', 'post-receive', 'post-update', 'push-to-checkout') which are always
-executed in $GIT_DIR.
+executed in $GIT_DIR.  Environment variables like GIT_DIR and GIT_WORK_TREE
+are exported so that the hook can easily learn which repository it is
+working with.
+
+If your hook needs to invoke Git commands in a foreign repository or in a
+different working tree of the same repository, then it should clear local Git
+environment variables, such as `GIT_DIR`, `GIT_WORK_TREE`, etc., which could
+interfere with Git operations in the foreign repository since those variables
+will be referencing the local repository and working tree. For example:
+
+------------
+local_desc=$(git describe)
+foreign_desc=$(unset $(git rev-parse --local-env-vars); git -C ../foreign-repo describe)
+------------
 
 Hooks can get their arguments via the environment, command-line
 arguments, and stdin. See the documentation for each hook below for
Previous: Eric SunshineNext: Eric Sunshine
Message 6 of 11 in “githooks: discuss Git operations in foreign repositories”
  1. githooks: discuss Git operations in foreign repositoriesEric Sunshine via GitGitGadget, Jan 8, 2023
  2. Preston Tunnell WilsonJan 8, 2023
  3. Eric SunshineJan 8, 2023
  4. Preston Tunnell WilsonJan 9, 2023
  5. Eric SunshineJan 9, 2023
  6. Junio C HamanoJan 9, 2023
  7. Eric SunshineJan 9, 2023
  8. Junio C HamanoJan 9, 2023
  9. githooks: discuss Git operations in foreign repositoriesEric Sunshine via GitGitGadget, Jan 9, 2023
  10. Preston Tunnell WilsonJan 9, 2023
  11. Jeff KingJan 11, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.