Skip to content

Commit c5e558a

Browse files
pccgitster
authored andcommitted
Remove empty directories when checking out a commit with fewer submodules
Change the unlink_entry function to use rmdir to remove submodule directories. Currently we try to use unlink, which will never succeed. Of course rmdir will only succeed for empty (i.e. not checked out) submodule directories. Behaviour if a submodule is checked out stays essentially the same: print a warning message and keep the submodule directory. Signed-off-by: Peter Collingbourne <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 902f235 commit c5e558a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

t/t7400-submodule-basic.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ test_expect_success 'ls-files gracefully handles trailing slash' '
299299
300300
'
301301

302+
test_expect_success 'moving to a commit without submodule does not leave empty dir' '
303+
rm -rf init &&
304+
mkdir init &&
305+
git reset --hard &&
306+
git checkout initial &&
307+
test ! -d init &&
308+
git checkout second
309+
'
310+
302311
test_expect_success 'submodule <invalid-path> warns' '
303312
304313
git submodule no-such-submodule 2> output.err &&

unpack-trees.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,16 @@ static void unlink_entry(struct cache_entry *ce)
6161
{
6262
if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
6363
return;
64-
if (unlink_or_warn(ce->name))
65-
return;
64+
if (S_ISGITLINK(ce->ce_mode)) {
65+
if (rmdir(ce->name)) {
66+
warning("unable to rmdir %s: %s",
67+
ce->name, strerror(errno));
68+
return;
69+
}
70+
}
71+
else
72+
if (unlink_or_warn(ce->name))
73+
return;
6674
schedule_dir_for_removal(ce->name, ce_namelen(ce));
6775
}
6876

0 commit comments

Comments
 (0)