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

[PATCH] Allow environment variables to be unset in the processes started by run_command

From
Alex Riesen <raa.lkml@gmail.com>
Date
May 23, 2007, 20:21 UTC
Message-ID
<20070523202139.GC2554@steel.home>
In-Reply-To
<20070522231442.GM30871@steel.home>
To unset a variable, just specify its name, without "=". For example:
    const char *env[] = {"GIT_DIR=.git", "PWD", NULL};
    const char *argv[] = {"git-ls-files", "-s", NULL};
    int err = run_command_v_opt_cd_env(argv, RUN_GIT_CMD, ".", env);
The PWD will be unset before executing git-ls-files.
Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
Alex Riesen, Wed, May 23, 2007 01:14:42 +0200:
Show 12 quoted lines
> > Because you _scan_ the whole string in your patch to see if it
> > ends with = anyway, a trivial improvement would be to do:
> > 
> > 	if (strchr(cmd->env, '='))
> >                 putenv(cmd->env);
> > 	else
> >         	unsetenv(cmd->env);
> 
> I like this one. The env field in struct child_process and run_command
> will have to mention it in comments (in run-command.h), it's kind of
> special.
> 
 run-command.c |    8 ++++++--
 run-command.h |    5 +++++
 2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/run-command.c b/run-command.c
index 605aa1e..3b1899e 100644
--- a/run-command.c
+++ b/run-command.c
@@ -77,8 +77,12 @@ int start_command(struct child_process *cmd)
 			die("exec %s: cd to %s failed (%s)", cmd->argv[0],
 			    cmd->dir, strerror(errno));
 		if (cmd->env) {
-			for (; *cmd->env; cmd->env++)
-				putenv((char*)*cmd->env);
+			for (; *cmd->env; cmd->env++) {
+				if (strchr(*cmd->env, '='))
+					putenv((char*)*cmd->env);
+				else
+					unsetenv(*cmd->env);
+			}
 		}
 		if (cmd->git_cmd) {
 			execv_git_cmd(cmd->argv);
diff --git a/run-command.h b/run-command.h
index af1e0bf..7958eb1 100644
--- a/run-command.h
+++ b/run-command.h
@@ -35,6 +35,11 @@ int run_command(struct child_process *);
 #define RUN_COMMAND_STDOUT_TO_STDERR 4
 int run_command_v_opt(const char **argv, int opt);
 int run_command_v_opt_cd(const char **argv, int opt, const char *dir);
+
+/*
+ * env (the environment) is to be formatted like environ: "VAR=VALUE".
+ * To unset an environment variable use just "VAR".
+ */
 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env);
 
 #endif
-- 
1.5.2.67.gbd3c2
Previous: Alex Riesen
Message 20 of 20 in “allow commands to be executed in submodules”
  1. allow commands to be executed in submodulesMartin Waitz, May 20, 2007
  2. Alex RiesenMay 20, 2007
  3. Junio C HamanoMay 20, 2007
  4. Martin WaitzMay 20, 2007
  5. Alex RiesenMay 20, 2007
  6. Martin WaitzMay 20, 2007
  7. Add ability to specify environment extension to run_commandAlex Riesen, May 21, 2007
  8. Junio C HamanoMay 21, 2007
  9. Martin WaitzMay 22, 2007
  10. Junio C HamanoMay 22, 2007
  11. Shawn O. PearceMay 22, 2007
  12. Sven VerdoolaegeMay 22, 2007
  13. Alex RiesenMay 22, 2007
  14. Alex RiesenMay 22, 2007
  15. Add run_command_v_opt_cd: chdir into a directory before execAlex Riesen, May 22, 2007
  16. Add ability to specify environment extension to run_commandAlex Riesen, May 22, 2007
  17. Allow environment variables to be unset in the processes started by run_commandAlex Riesen, May 22, 2007
  18. Junio C HamanoMay 22, 2007
  19. Alex RiesenMay 22, 2007
  20. Allow environment variables to be unset in the processes started by run_commandAlex Riesen, May 23, 2007

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.