Can load default template directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
diff --git a/src/fileops.c b/src/fileops.c
index b7ae94b..ba88dfe 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -763,7 +763,8 @@ static int git_futils_find_in_dirlist(
continue;
GITERR_CHECK_ERROR(git_buf_set(path, scan, len));
- GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
+ if (name)
+ GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
if (git_path_exists(path->ptr))
return 0;
@@ -792,6 +793,12 @@ int git_futils_find_xdg_file(git_buf *path, const char *filename)
path, filename, GIT_FUTILS_DIR_XDG, "global/xdg");
}
+int git_futils_find_template_dir(git_buf *path)
+{
+ return git_futils_find_in_dirlist(
+ path, NULL, GIT_FUTILS_DIR_TEMPLATE, "template");
+}
+
int git_futils_fake_symlink(const char *old, const char *new)
{
int retcode = GIT_ERROR;
diff --git a/src/fileops.h b/src/fileops.h
index 0ac9b90..64d3da7 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -306,6 +306,14 @@ extern int git_futils_find_xdg_file(git_buf *path, const char *filename);
*/
extern int git_futils_find_system_file(git_buf *path, const char *filename);
+/**
+ * Find template directory.
+ *
+ * @param path buffer to write the full path into
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_futils_find_template_dir(git_buf *path);
+
typedef enum {
GIT_FUTILS_DIR_SYSTEM = 0,
GIT_FUTILS_DIR_GLOBAL = 1,
diff --git a/src/repository.c b/src/repository.c
index eead412..e67d1f0 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -33,8 +33,6 @@
#define GIT_REPO_VERSION 0
-#define GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
-
static void set_odb(git_repository *repo, git_odb *odb)
{
if (odb) {
@@ -1136,6 +1134,9 @@ static int repo_init_structure(
if (external_tpl) {
git_config *cfg;
const char *tdir;
+ git_buf template_buf = GIT_BUF_INIT;
+
+ git_futils_find_template_dir(&template_buf);
if (opts->template_path)
tdir = opts->template_path;
@@ -1150,7 +1151,7 @@ static int repo_init_structure(
return error;
giterr_clear();
- tdir = GIT_TEMPLATE_DIR;
+ tdir = template_buf.ptr;
}
error = git_futils_cp_r(tdir, repo_dir,
@@ -1158,14 +1159,17 @@ static int repo_init_structure(
GIT_CPDIR_SIMPLE_TO_MODE, dmode);
if (error < 0) {
- if (strcmp(tdir, GIT_TEMPLATE_DIR) != 0)
+ if (strcmp(tdir, template_buf.ptr) != 0) {
+ git_buf_free(&template_buf);
return error;
+ }
/* if template was default, ignore error and use internal */
giterr_clear();
external_tpl = false;
error = 0;
}
+ git_buf_free(&template_buf);
}
/* Copy internal template