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

[RFC/PATCH v2 2/3] run-command: make sure hooks have always GIT_DIR

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Apr 23, 2014, 19:42 UTC
Message-ID
<1398282160-26151-3-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1398282160-26151-1-git-send-email-felipe.contreras@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 run-command.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/run-command.c b/run-command.c
index 75abc47..8e188f6 100644
--- a/run-command.c
+++ b/run-command.c
@@ -765,12 +765,29 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
 	struct child_process hook;
 	struct argv_array argv = ARGV_ARRAY_INIT;
 	const char *p;
-	int ret;
+	const char **mod_env = NULL;
+	int ret, i = 0;
+	struct strbuf buf = STRBUF_INIT;
 
 	p = find_hook(name);
 	if (!p)
 		return 0;
 
+	if (!getenv(GIT_DIR_ENVIRONMENT)) {
+		if (env)
+			for (i = 0; env[i]; i++);
+
+		mod_env = xcalloc(i + 2, sizeof(*mod_env));
+
+		if (env)
+			for (i = 0; env[i]; i++)
+				mod_env[i] = env[i];
+
+		strbuf_addf(&buf, "GIT_DIR=%s", get_git_dir());
+		mod_env[i++] = buf.buf;
+		mod_env[i++] = NULL;
+	}
+
 	argv_array_push(&argv, p);
 
 	while ((p = va_arg(args, const char *)))
@@ -778,12 +795,15 @@ int run_hook_ve(const char *const *env, const char *name, va_list args)
 
 	memset(&hook, 0, sizeof(hook));
 	hook.argv = argv.argv;
-	hook.env = env;
+	hook.env = mod_env ? mod_env : env;
 	hook.no_stdin = 1;
 	hook.stdout_to_stderr = 1;
 
 	ret = run_command(&hook);
 	argv_array_clear(&argv);
+	strbuf_release(&buf);
+	free(mod_env);
+
 	return ret;
 }
 
-- 
1.9.2+fc1.1.g5c924db
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 12 in “New 'update-branch' hook”
  1. 0/3 New 'update-branch' hookFelipe Contreras, Apr 23, 2014
  2. 1/3 sh-setup: export GIT_DIRFelipe Contreras, Apr 23, 2014
  3. Jeff KingApr 23, 2014
  4. Junio C HamanoApr 23, 2014
  5. 2/3 run-command: make sure hooks have always GIT_DIRFelipe Contreras, Apr 23, 2014
  6. Junio C HamanoApr 23, 2014
  7. Felipe ContrerasApr 23, 2014
  8. 3/3 Add 'update-branch' hookFelipe Contreras, Apr 23, 2014
  9. Junio C HamanoApr 23, 2014
  10. Felipe ContrerasApr 23, 2014
  11. Junio C HamanoApr 23, 2014
  12. Felipe ContrerasApr 23, 2014

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.