Skip to content

Commit df93e40

Browse files
committed
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 <[email protected]>
1 parent 48c171d commit df93e40

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

builtin/init-db.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include "parse-options.h"
1212
#include "worktree.h"
1313

14-
#ifndef DEFAULT_GIT_TEMPLATE_DIR
15-
#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
16-
#endif
17-
1814
#ifdef NO_TRUSTABLE_FILEMODE
1915
#define TEST_FILEMODE 0
2016
#else
@@ -93,8 +89,9 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
9389
}
9490
}
9591

96-
static void copy_templates(const char *template_dir, const char *init_template_dir)
92+
static void copy_templates(const char *option_template)
9793
{
94+
const char *template_dir = get_template_dir(option_template);
9895
struct strbuf path = STRBUF_INIT;
9996
struct strbuf template_path = STRBUF_INIT;
10097
size_t template_len;
@@ -103,16 +100,8 @@ static void copy_templates(const char *template_dir, const char *init_template_d
103100
DIR *dir;
104101
char *to_free = NULL;
105102

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)
114104
return;
115-
}
116105

117106
strbuf_addstr(&template_path, template_dir);
118107
strbuf_complete(&template_path, '/');
@@ -200,7 +189,6 @@ static int create_default_files(const char *template_path,
200189
int reinit;
201190
int filemode;
202191
struct strbuf err = STRBUF_INIT;
203-
const char *init_template_dir = NULL;
204192
const char *work_tree = get_git_work_tree();
205193

206194
/*
@@ -212,9 +200,7 @@ static int create_default_files(const char *template_path,
212200
* values (since we've just potentially changed what's available on
213201
* disk).
214202
*/
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);
218204
git_config_clear();
219205
reset_shared_repository();
220206
git_config(git_default_config, NULL);

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ int path_inside_repo(const char *prefix, const char *path);
656656
#define INIT_DB_QUIET 0x0001
657657
#define INIT_DB_EXIST_OK 0x0002
658658

659+
const char *get_template_dir(const char *option_template);
659660
int init_db(const char *git_dir, const char *real_git_dir,
660661
const char *template_dir, int hash_algo,
661662
const char *initial_branch, unsigned int flags);

setup.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "chdir-notify.h"
77
#include "promisor-remote.h"
88
#include "quote.h"
9+
#include "exec-cmd.h"
910

1011
static int inside_git_dir = -1;
1112
static int inside_work_tree = -1;
@@ -1720,3 +1721,34 @@ int daemonize(void)
17201721
return 0;
17211722
#endif
17221723
}
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+
}

0 commit comments

Comments
 (0)