run-command: refactor subprocess env preparation

submodule.c has functionality that prepares the environment for running a subprocess in a new repo. The lazy-fetching code (used in partial clones) will need this in a subsequent commit, so move it to a more central location. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jun 17, 2021 at 10:13 UTC d1fa94356ddd2a81348532d49030cd08d0df6a4d
3 files changed +24 -16
run-command.c
+12
@@ -1892,3 +1892,15 @@ int run_auto_maintenance(int quiet)
1892
1893 return run_command(&maint);
1894 }
1895 +
1896 +void prepare_other_repo_env(struct strvec *env_array, const char *new_git_dir)
1897 +{
1898 + const char * const *var;
1899 +
1900 + for (var = local_repo_env; *var; var++) {
1901 + if (strcmp(*var, CONFIG_DATA_ENVIRONMENT) &&
1902 + strcmp(*var, CONFIG_COUNT_ENVIRONMENT))
1903 + strvec_push(env_array, *var);
1904 + }
1905 + strvec_pushf(env_array, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir);
1906 +}
run-command.h
+10
@@ -483,4 +483,14 @@ int run_processes_parallel_tr2(int n, get_next_task_fn, start_failure_fn,
483 task_finished_fn, void *pp_cb,
484 const char *tr2_category, const char *tr2_label);
485
486 +/**
487 + * Convenience function which prepares env_array for a command to be run in a
488 + * new repo. This adds all GIT_* environment variables to env_array with the
489 + * exception of GIT_CONFIG_PARAMETERS and GIT_CONFIG_COUNT (which cause the
490 + * corresponding environment variables to be unset in the subprocess) and adds
491 + * an environment variable pointing to new_git_dir. See local_repo_env in
492 + * cache.h for more information.
493 + */
494 +void prepare_other_repo_env(struct strvec *env_array, const char *new_git_dir);
495 +
496 #endif
submodule.c
+2 -16
@@ -484,28 +484,14 @@ static void print_submodule_diff_summary(struct repository *r, struct rev_info *
484 strbuf_release(&sb);
485 }
486
487 -static void prepare_submodule_repo_env_no_git_dir(struct strvec *out)
488 -{
489 - const char * const *var;
490 -
491 - for (var = local_repo_env; *var; var++) {
492 - if (strcmp(*var, CONFIG_DATA_ENVIRONMENT) &&
493 - strcmp(*var, CONFIG_COUNT_ENVIRONMENT))
494 - strvec_push(out, *var);
495 - }
496 -}
497 -
487 void prepare_submodule_repo_env(struct strvec *out)
488 {
500 - prepare_submodule_repo_env_no_git_dir(out);
501 - strvec_pushf(out, "%s=%s", GIT_DIR_ENVIRONMENT,
502 - DEFAULT_GIT_DIR_ENVIRONMENT);
489 + prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT);
490 }
491
492 static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
493 {
507 - prepare_submodule_repo_env_no_git_dir(out);
508 - strvec_pushf(out, "%s=.", GIT_DIR_ENVIRONMENT);
494 + prepare_other_repo_env(out, ".");
495 }
496
497 /*