init: make --template path relative to $CWD

During git-init we chdir() to the target directory, but --template is not adjusted. So it's relative to the target directory instead of current directory. It would be ok if it's documented, but --template in git-init.txt mentions nothing about this behavior. Change it to be relative to $CWD, which is much more intuitive. The changes in the test suite show that this relative-to-target behavior is actually used. I just hope that it's only used in the test suite and it's safe to change. Otherwise, the other option is just document it (i.e. relative to target dir) and move on. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed May 10, 2019 at 17:46 UTC e1df7fe43f4e2e607ed24668e2942d45df8d2743
3 files changed +7 -4
builtin/init-db.c
+3
@@ -494,6 +494,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
494 if (real_git_dir && !is_absolute_path(real_git_dir))
495 real_git_dir = real_pathdup(real_git_dir, 1);
496
497 + if (template_dir && *template_dir && !is_absolute_path(template_dir))
498 + template_dir = absolute_pathdup(template_dir);
499 +
500 if (argc == 1) {
501 int mkdir_tried = 0;
502 retry:
t/t0001-init.sh
+1 -1
@@ -174,7 +174,7 @@ test_expect_success 'reinit' '
174 test_expect_success 'init with --template' '
175 mkdir template-source &&
176 echo content >template-source/file &&
177 - git init --template=../template-source template-custom &&
177 + git init --template=template-source template-custom &&
178 test_cmp template-source/file template-custom/.git/file
179 '
180
t/t1301-shared-repo.sh
+3 -3
@@ -136,7 +136,7 @@ test_expect_success POSIXPERM 'forced modes' '
136 (
137 cd new &&
138 umask 002 &&
139 - git init --shared=0660 --template=../templates &&
139 + git init --shared=0660 --template=templates &&
140 >frotz &&
141 git add frotz &&
142 git commit -a -m initial &&
@@ -192,7 +192,7 @@ test_expect_success POSIXPERM 're-init respects core.sharedrepository (remote)'
192 umask 0022 &&
193 git init --bare --shared=0666 child.git &&
194 test_path_is_missing child.git/foo &&
195 - git init --bare --template=../templates child.git &&
195 + git init --bare --template=templates child.git &&
196 echo "-rw-rw-rw-" >expect &&
197 test_modebits child.git/foo >actual &&
198 test_cmp expect actual
@@ -203,7 +203,7 @@ test_expect_success POSIXPERM 'template can set core.sharedrepository' '
203 umask 0022 &&
204 git config core.sharedrepository 0666 &&
205 cp .git/config templates/config &&
206 - git init --bare --template=../templates child.git &&
206 + git init --bare --template=templates child.git &&
207 echo "-rw-rw-rw-" >expect &&
208 test_modebits child.git/HEAD >actual &&
209 test_cmp expect actual