sparse-checkout: create leading directory

When creating the sparse-checkout file, Git does not create the leading directory, "$GIT_DIR/info", if it does not exist. This causes problems if the repository does not have that directory. Therefore, ensure that the leading directory is created. This is the only "open" in builtin/sparse-checkout.c that does not have a leading directory check. (The other one in write_patterns_and_update() does.) Note that the test needs to explicitly specify a template when running "git init" because the default template used in the tests has the "info/" directory included. Helped-by: Jose Lopes <jabolopes@google.com> Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jan 21, 2022 at 09:44 UTC 7f44842ac198c8fbf76c513cb69b66bdc54d59e8
2 files changed +9
builtin/sparse-checkout.c
+3
@@ -450,6 +450,9 @@ static int sparse_checkout_init(int argc, const char **argv)
450 FILE *fp;
451
452 /* assume we are in a fresh repo, but update the sparse-checkout file */
453 + if (safe_create_leading_directories(sparse_filename))
454 + die(_("unable to create leading directories of %s"),
455 + sparse_filename);
456 fp = xfopen(sparse_filename, "w");
457 if (!fp)
458 die(_("failed to open '%s'"), sparse_filename);
t/t1091-sparse-checkout-builtin.sh
+6
@@ -71,6 +71,12 @@ test_expect_success 'git sparse-checkout init' '
71 check_files repo a
72 '
73
74 +test_expect_success 'git sparse-checkout init in empty repo' '
75 + test_when_finished rm -rf empty-repo blank-template &&
76 + git init --template= empty-repo &&
77 + git -C empty-repo sparse-checkout init
78 +'
79 +
80 test_expect_success 'git sparse-checkout list after init' '
81 git -C repo sparse-checkout list >actual &&
82 cat >expect <<-\EOF &&