run-command: extract sanitize_repo_env helper
The current prepare_other_repo_env() does two distinct things: 1. Strip certain known environment variables that should be set by a child process based on a different repository. 2. Set the GIT_DIR variable to avoid repository discovery. The second item is valuable for child processes that operate on submodules, where the repo discovery could be mistaken for the parent repository. In the next change, we will see an important case where only the first item is required as the GIT_DIR discovery should happen naturally from the '-C' parameter in the child process. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Derrick Stolee committed
Mar 3, 2026 at 17:31 UTC
5f031fe4f14b5cc754daaf24534dbe0c6647fcca
2 files changed
+16
-6
run-command.c
+6
-1
@@ -1847,7 +1847,7 @@ int run_auto_maintenance(int quiet)
1847
return run_command(&maint);
1848
}
1849
1850
-void prepare_other_repo_env(struct strvec *env, const char *new_git_dir)
1850
+void sanitize_repo_env(struct strvec *env)
1851
{
1852
const char * const *var;
1853
@@ -1856,6 +1856,11 @@ void prepare_other_repo_env(struct strvec *env, const char *new_git_dir)
1856
strcmp(*var, CONFIG_COUNT_ENVIRONMENT))
1857
strvec_push(env, *var);
1858
}
1859
+}
1860
+
1861
+void prepare_other_repo_env(struct strvec *env, const char *new_git_dir)
1862
+{
1863
+ sanitize_repo_env(env);
1864
strvec_pushf(env, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir);
1865
}
1866
run-command.h
+10
-5
@@ -509,13 +509,18 @@ struct run_process_parallel_opts
509
*/
510
void run_processes_parallel(const struct run_process_parallel_opts *opts);
511
512
+/**
513
+ * Unset all local-repo GIT_* variables in env; see local_repo_env in
514
+ * environment.h. GIT_CONFIG_PARAMETERS and GIT_CONFIG_COUNT are preserved
515
+ * to pass -c and --config-env options from the parent process.
516
+ */
517
+void sanitize_repo_env(struct strvec *env);
518
+
519
/**
520
* Convenience function which prepares env for a command to be run in a
514
- * new repo. This adds all GIT_* environment variables to env with the
515
- * exception of GIT_CONFIG_PARAMETERS and GIT_CONFIG_COUNT (which cause the
516
- * corresponding environment variables to be unset in the subprocess) and adds
517
- * an environment variable pointing to new_git_dir. See local_repo_env in
518
- * environment.h for more information.
521
+ * new repo. This removes variables pointing to the local repository (using
522
+ * sanitize_repo_env() above), and adds an environment variable pointing to
523
+ * new_git_dir.
524
*/
525
void prepare_other_repo_env(struct strvec *env, const char *new_git_dir);
526