git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jonathan Tan <jonathantanmy@google.com>
To: git@vger.kernel.org
Cc: Jonathan Tan <jonathantanmy@google.com>,
	gitster@pobox.com, stolee@gmail.com, avarab@gmail.com
Subject: [PATCH] sparse-checkout: create leading directory
Date: Thu, 20 Jan 2022 10:55:48 -0800	[thread overview]
Message-ID: <20220120185548.3648549-1-jonathantanmy@google.com> (raw)

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.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
This problem is being discussed in [1], and we also noticed this problem
internally at $DAYJOB. Here's a fix.

[1] https://lore.kernel.org/git/db6f47a3-0df3-505b-b391-6ca289fd61b5@gmail.com/
---
 builtin/sparse-checkout.c          | 3 +++
 t/t1091-sparse-checkout-builtin.sh | 7 +++++++
 2 files changed, 10 insertions(+)

diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index 679c107036..2b0e1db2d2 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -471,6 +471,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);
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index 42776984fe..dba0737599 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -79,6 +79,13 @@ 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 &&
+	mkdir blank-template &&
+	git init --template=blank-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 &&
-- 
2.34.1.703.g22d0c6ccf7-goog


             reply	other threads:[~2022-01-20 18:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-20 18:55 Jonathan Tan [this message]
2022-01-20 20:26 ` [PATCH] sparse-checkout: create leading directory Junio C Hamano
2022-01-20 21:30 ` Ævar Arnfjörð Bjarmason
2022-01-22  7:58   ` SZEDER Gábor
2022-01-21 17:44 ` [PATCH v2] " Jonathan Tan
2022-01-21 18:48   ` Ævar Arnfjörð Bjarmason
2022-01-22  5:21     ` Elijah Newren
2022-01-22 12:06       ` Ævar Arnfjörð Bjarmason
2022-01-22 17:29         ` Elijah Newren
2022-01-24 11:17           ` Ævar Arnfjörð Bjarmason
2022-01-24 18:25             ` Jonathan Tan
2022-01-25  5:37             ` Elijah Newren
2022-01-22  2:33   ` Elijah Newren
2022-01-24 18:22     ` Jonathan Tan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220120185548.3648549-1-jonathantanmy@google.com \
    --to=jonathantanmy@google.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=stolee@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).