init: refactor the template directory discovery into its own function

We will need to call this function from `hook.c` to be able to prevent
hooks from running that were written as part of a `clone` but did not
originate from the template directory.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin
2024-03-29 11:45:01 +01:00
parent 48c171d927
commit df93e407f0
3 changed files with 37 additions and 18 deletions

32
setup.c
View File

@ -6,6 +6,7 @@
#include "chdir-notify.h"
#include "promisor-remote.h"
#include "quote.h"
#include "exec-cmd.h"
static int inside_git_dir = -1;
static int inside_work_tree = -1;
@ -1720,3 +1721,34 @@ int daemonize(void)
return 0;
#endif
}
#ifndef DEFAULT_GIT_TEMPLATE_DIR
#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
#endif
const char *get_template_dir(const char *option_template)
{
const char *template_dir = option_template;
if (!template_dir)
template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
if (!template_dir) {
static const char *init_template_dir;
static int initialized;
if (!initialized) {
git_config_get_pathname("init.templatedir",
&init_template_dir);
initialized = 1;
}
template_dir = init_template_dir;
}
if (!template_dir) {
static char *dir;
if (!dir)
dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
template_dir = dir;
}
return template_dir;
}