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>

Johannes Schindelin committed Mar 29, 2024 at 11:45 UTC df93e407f0618e4a8265ac619dc7f4c7005155bc
3 files changed +37 -18
builtin/init-db.c
+4 -18
@@ -11,10 +11,6 @@
11 #include "parse-options.h"
12 #include "worktree.h"
13
14 -#ifndef DEFAULT_GIT_TEMPLATE_DIR
15 -#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
16 -#endif
17 -
14 #ifdef NO_TRUSTABLE_FILEMODE
15 #define TEST_FILEMODE 0
16 #else
@@ -93,8 +89,9 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
89 }
90 }
91
96 -static void copy_templates(const char *template_dir, const char *init_template_dir)
92 +static void copy_templates(const char *option_template)
93 {
94 + const char *template_dir = get_template_dir(option_template);
95 struct strbuf path = STRBUF_INIT;
96 struct strbuf template_path = STRBUF_INIT;
97 size_t template_len;
@@ -103,16 +100,8 @@ static void copy_templates(const char *template_dir, const char *init_template_d
100 DIR *dir;
101 char *to_free = NULL;
102
106 - if (!template_dir)
107 - template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
108 - if (!template_dir)
109 - template_dir = init_template_dir;
110 - if (!template_dir)
111 - template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
112 - if (!template_dir[0]) {
113 - free(to_free);
103 + if (!template_dir || !*template_dir)
104 return;
115 - }
105
106 strbuf_addstr(&template_path, template_dir);
107 strbuf_complete(&template_path, '/');
@@ -200,7 +189,6 @@ static int create_default_files(const char *template_path,
189 int reinit;
190 int filemode;
191 struct strbuf err = STRBUF_INIT;
203 - const char *init_template_dir = NULL;
192 const char *work_tree = get_git_work_tree();
193
194 /*
@@ -212,9 +200,7 @@ static int create_default_files(const char *template_path,
200 * values (since we've just potentially changed what's available on
201 * disk).
202 */
215 - git_config_get_pathname("init.templatedir", &init_template_dir);
216 - copy_templates(template_path, init_template_dir);
217 - free((char *)init_template_dir);
203 + copy_templates(template_path);
204 git_config_clear();
205 reset_shared_repository();
206 git_config(git_default_config, NULL);
cache.h
+1
@@ -656,6 +656,7 @@ int path_inside_repo(const char *prefix, const char *path);
656 #define INIT_DB_QUIET 0x0001
657 #define INIT_DB_EXIST_OK 0x0002
658
659 +const char *get_template_dir(const char *option_template);
660 int init_db(const char *git_dir, const char *real_git_dir,
661 const char *template_dir, int hash_algo,
662 const char *initial_branch, unsigned int flags);
setup.c
+32
@@ -6,6 +6,7 @@
6 #include "chdir-notify.h"
7 #include "promisor-remote.h"
8 #include "quote.h"
9 +#include "exec-cmd.h"
10
11 static int inside_git_dir = -1;
12 static int inside_work_tree = -1;
@@ -1720,3 +1721,34 @@ int daemonize(void)
1721 return 0;
1722 #endif
1723 }
1724 +
1725 +#ifndef DEFAULT_GIT_TEMPLATE_DIR
1726 +#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
1727 +#endif
1728 +
1729 +const char *get_template_dir(const char *option_template)
1730 +{
1731 + const char *template_dir = option_template;
1732 +
1733 + if (!template_dir)
1734 + template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
1735 + if (!template_dir) {
1736 + static const char *init_template_dir;
1737 + static int initialized;
1738 +
1739 + if (!initialized) {
1740 + git_config_get_pathname("init.templatedir",
1741 + &init_template_dir);
1742 + initialized = 1;
1743 + }
1744 + template_dir = init_template_dir;
1745 + }
1746 + if (!template_dir) {
1747 + static char *dir;
1748 +
1749 + if (!dir)
1750 + dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
1751 + template_dir = dir;
1752 + }
1753 + return template_dir;
1754 +}