init: handle empty "template" parameter
If a user passes "--template=", then our template parameter is blank. Unfortunately, copy_templates() assumes it has at least one character, and does all sorts of bad things like reading from template[-1] and then proceeding to link all of '/' into the .git directory. This patch just checks for that condition in copy_templates and aborts. As a side effect, this means that --template= now has the meaning "don't copy any templates." Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jul 28, 2008 at 02:02 UTC
172035f044ebae283f23e95e3ec4d119dc8057cb
2 files changed
+28
builtin-init-db.c
+2
@@ -117,6 +117,8 @@ static void copy_templates(const char *template_dir)
117
template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
118
if (!template_dir)
119
template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
120
+ if (!template_dir[0])
121
+ return;
122
strcpy(template_path, template_dir);
123
template_len = strlen(template_path);
124
if (template_path[template_len-1] != '/') {
t/t0001-init.sh
+26
@@ -141,4 +141,30 @@ test_expect_success 'reinit' '
141
test_cmp again/empty again/err2
142
'
143
144
+test_expect_success 'init with --template' '
145
+ mkdir template-source &&
146
+ echo content >template-source/file &&
147
+ (
148
+ mkdir template-custom &&
149
+ cd template-custom &&
150
+ git init --template=../template-source
151
+ ) &&
152
+ test_cmp template-source/file template-custom/.git/file
153
+'
154
+
155
+test_expect_success 'init with --template (blank)' '
156
+ (
157
+ mkdir template-plain &&
158
+ cd template-plain &&
159
+ git init
160
+ ) &&
161
+ test -f template-plain/.git/info/exclude &&
162
+ (
163
+ mkdir template-blank &&
164
+ cd template-blank &&
165
+ git init --template=
166
+ ) &&
167
+ ! test -f template-blank/.git/info/exclude
168
+'
169
+
170
test_done