Commit a025907e0d751ed1022e65365243ae97acf3f598

Linquize 2013-09-17T23:55:11

Can load default template directory

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