Skip to content

Commit 340e4f8

Browse files
author
Junio C Hamano
committed
Remove empty directories after read-tree -u.
This fixes everybody's favorite gripe that switching branche with 'git checkout' leaves empty directories. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 1771299 commit 340e4f8

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

read-tree.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,35 @@ static void reject_merge(struct cache_entry *ce)
237237
ce->name);
238238
}
239239

240+
/* Unlink the last component and attempt to remove leading
241+
* directories, in case this unlink is the removal of the
242+
* last entry in the directory -- empty directories are removed.
243+
*/
244+
static void unlink_entry(char *name)
245+
{
246+
char *cp, *prev;
247+
248+
if (unlink(name))
249+
return;
250+
prev = NULL;
251+
while (1) {
252+
int status;
253+
cp = strrchr(name, '/');
254+
if (prev)
255+
*prev = '/';
256+
if (!cp)
257+
break;
258+
259+
*cp = 0;
260+
status = rmdir(name);
261+
if (status) {
262+
*cp = '/';
263+
break;
264+
}
265+
prev = cp;
266+
}
267+
}
268+
240269
static void check_updates(struct cache_entry **src, int nr)
241270
{
242271
static struct checkout state = {
@@ -250,7 +279,7 @@ static void check_updates(struct cache_entry **src, int nr)
250279
struct cache_entry *ce = *src++;
251280
if (!ce->ce_mode) {
252281
if (update)
253-
unlink(ce->name);
282+
unlink_entry(ce->name);
254283
continue;
255284
}
256285
if (ce->ce_flags & mask) {

0 commit comments

Comments
 (0)