]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sparse-checkout: create leading directory
authorJonathan Tan <jonathantanmy@google.com>
Fri, 21 Jan 2022 17:44:41 +0000 (09:44 -0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 21 Jan 2022 20:37:36 +0000 (12:37 -0800)
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>
builtin/sparse-checkout.c
t/t1091-sparse-checkout-builtin.sh

index d0f5c4702be69d0c9fade08ffbf5183c5f3dbe7e..7a2be2a43e8e8b441b53ebd3cec06a7439ea644d 100644 (file)
@@ -450,6 +450,9 @@ static int sparse_checkout_init(int argc, const char **argv)
                FILE *fp;
 
                /* assume we are in a fresh repo, but update the sparse-checkout file */
+               if (safe_create_leading_directories(sparse_filename))
+                       die(_("unable to create leading directories of %s"),
+                           sparse_filename);
                fp = xfopen(sparse_filename, "w");
                if (!fp)
                        die(_("failed to open '%s'"), sparse_filename);
index 272ba1b566b3eaf43798f9ecfb1a77c26db3f59b..41b7984c594b5ddfb11c198425d62938699485e4 100755 (executable)
@@ -71,6 +71,12 @@ test_expect_success 'git sparse-checkout init' '
        check_files repo a
 '
 
+test_expect_success 'git sparse-checkout init in empty repo' '
+       test_when_finished rm -rf empty-repo blank-template &&
+       git init --template= empty-repo &&
+       git -C empty-repo sparse-checkout init
+'
+
 test_expect_success 'git sparse-checkout list after init' '
        git -C repo sparse-checkout list >actual &&
        cat >expect <<-\EOF &&