Skip to content

Commit 3c75406

Browse files
derrickstoleegitster
authored andcommitted
sparse-checkout: create leading directories
The 'git init' command creates the ".git/info" directory and fills it with some default files. However, 'git worktree add' does not create the info directory for that worktree. This causes a problem when running "git sparse-checkout init" inside a worktree. While care was taken to allow the sparse-checkout config to be specific to a worktree, this initialization was untested. Safely create the leading directories for the sparse-checkout file. This is the safest thing to do even without worktrees, as a user could delete their ".git/info" directory and expect Git to recover safely. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d622c34 commit 3c75406

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

builtin/sparse-checkout.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ static int write_patterns_and_update(struct pattern_list *pl)
199199
int result;
200200

201201
sparse_filename = get_sparse_checkout_filename();
202+
203+
if (safe_create_leading_directories(sparse_filename))
204+
die(_("failed to create directory for sparse-checkout file"));
205+
202206
fd = hold_lock_file_for_update(&lk, sparse_filename,
203207
LOCK_DIE_ON_ERROR);
204208

t/t1091-sparse-checkout-builtin.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,14 @@ test_expect_success 'interaction with submodules' '
295295
check_files super/modules/child a deep folder1 folder2
296296
'
297297

298+
test_expect_success 'different sparse-checkouts with worktrees' '
299+
git -C repo worktree add --detach ../worktree &&
300+
check_files worktree "a deep folder1 folder2" &&
301+
git -C worktree sparse-checkout init --cone &&
302+
git -C repo sparse-checkout set folder1 &&
303+
git -C worktree sparse-checkout set deep/deeper1 &&
304+
check_files repo a folder1 &&
305+
check_files worktree a deep
306+
'
307+
298308
test_done

0 commit comments

Comments
 (0)