git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] sparse-checkout: create leading directory

From
JTJonathan Tan <jonathantanmy@google.com>
Date
Jan 21, 2022, 17:44 UTC
Message-ID
<20220121174441.3991963-1-jonathantanmy@google.com>
In-Reply-To
<20220120185548.3648549-1-jonathantanmy@google.com>

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>
---
Changes from v1:
 - made test simpler
 - added attribution to Jose Lopes for finding and making the first
   draft of this patch (after confirming with them)
Ævar mentioned "git sparse-checkout add" but I think that that is a
different problem - in the "git sparse-checkout init" case, we could get
into this case with a template that does not have .git/info, but in the
"git sparse-checkout add" case, the user would have had to explicitly
remove the info directory. So I'll limit this patch to the "init" case,
for now.
---
 builtin/sparse-checkout.c          | 3 +++
 t/t1091-sparse-checkout-builtin.sh | 6 ++++++
 2 files changed, 9 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..3189d3da96 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -79,6 +79,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 &&
-- 
2.35.0.rc0.227.g00780c9af4-goog
Previous: SZEDER GáborNext: Ævar Arnfjörð Bjarmason
Message 5 of 14 in “sparse-checkout: create leading directory”
  1. sparse-checkout: create leading directoryJonathan Tan, Jan 20, 2022
  2. Junio C HamanoJan 20, 2022
  3. Ævar Arnfjörð BjarmasonJan 20, 2022
  4. SZEDER GáborJan 22, 2022
  5. sparse-checkout: create leading directoryJonathan Tan, Jan 21, 2022
  6. Ævar Arnfjörð BjarmasonJan 21, 2022
  7. Elijah NewrenJan 22, 2022
  8. Ævar Arnfjörð BjarmasonJan 22, 2022
  9. Elijah NewrenJan 22, 2022
  10. Ævar Arnfjörð BjarmasonJan 24, 2022
  11. Jonathan TanJan 24, 2022
  12. Elijah NewrenJan 25, 2022
  13. Elijah NewrenJan 22, 2022
  14. Jonathan TanJan 24, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.