]> git.ipfire.org Git - thirdparty/git.git/commitdiff
init: refactor the template directory discovery into its own function
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 29 Mar 2024 10:45:01 +0000 (11:45 +0100)
committerJohannes Schindelin <johannes.schindelin@gmx.de>
Wed, 17 Apr 2024 20:30:10 +0000 (22:30 +0200)
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>
builtin/init-db.c
cache.h
setup.c

index dcaaf102eaf62bee5d09adabd2a5de21c8379c03..a101e7f94c119b5b1eb5da9ea359e5eb34bb37ab 100644 (file)
 #include "parse-options.h"
 #include "worktree.h"
 
-#ifndef DEFAULT_GIT_TEMPLATE_DIR
-#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
-#endif
-
 #ifdef NO_TRUSTABLE_FILEMODE
 #define TEST_FILEMODE 0
 #else
@@ -93,8 +89,9 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
        }
 }
 
-static void copy_templates(const char *template_dir, const char *init_template_dir)
+static void copy_templates(const char *option_template)
 {
+       const char *template_dir = get_template_dir(option_template);
        struct strbuf path = STRBUF_INIT;
        struct strbuf template_path = STRBUF_INIT;
        size_t template_len;
@@ -103,16 +100,8 @@ static void copy_templates(const char *template_dir, const char *init_template_d
        DIR *dir;
        char *to_free = NULL;
 
-       if (!template_dir)
-               template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
-       if (!template_dir)
-               template_dir = init_template_dir;
-       if (!template_dir)
-               template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
-       if (!template_dir[0]) {
-               free(to_free);
+       if (!template_dir || !*template_dir)
                return;
-       }
 
        strbuf_addstr(&template_path, template_dir);
        strbuf_complete(&template_path, '/');
@@ -200,7 +189,6 @@ static int create_default_files(const char *template_path,
        int reinit;
        int filemode;
        struct strbuf err = STRBUF_INIT;
-       const char *init_template_dir = NULL;
        const char *work_tree = get_git_work_tree();
 
        /*
@@ -212,9 +200,7 @@ static int create_default_files(const char *template_path,
         * values (since we've just potentially changed what's available on
         * disk).
         */
-       git_config_get_pathname("init.templatedir", &init_template_dir);
-       copy_templates(template_path, init_template_dir);
-       free((char *)init_template_dir);
+       copy_templates(template_path);
        git_config_clear();
        reset_shared_repository();
        git_config(git_default_config, NULL);
diff --git a/cache.h b/cache.h
index a46a3e4b6b1ebc48fe42489edf77701a2a1ac738..8c5fb1e1ba14f613483a359deff145c20d80ed84 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -656,6 +656,7 @@ int path_inside_repo(const char *prefix, const char *path);
 #define INIT_DB_QUIET 0x0001
 #define INIT_DB_EXIST_OK 0x0002
 
+const char *get_template_dir(const char *option_template);
 int init_db(const char *git_dir, const char *real_git_dir,
            const char *template_dir, int hash_algo,
            const char *initial_branch, unsigned int flags);
diff --git a/setup.c b/setup.c
index 9d401ae4c8f1cb2c0139ffe0bcbcf13c0fe5382b..e6e749ec4b4d848f6e4264de035e83c04d0185bc 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -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;
+}