@matheusbsbs / matheus-bagliano / commits / 340e4f88c0
Fork 0

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>

Junio C Hamano committed Oct 10, 2005 at 17:34 UTC 340e4f88c083b0692e6554b1c2c27fd43c7cc8d3
1 file changed +30 -1
read-tree.c
+30 -1
@@ -237,6 +237,35 @@ static void reject_merge(struct cache_entry *ce)
237 ce->name);
238 }
239
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 +
269 static void check_updates(struct cache_entry **src, int nr)
270 {
271 static struct checkout state = {
@@ -250,7 +279,7 @@ static void check_updates(struct cache_entry **src, int nr)
279 struct cache_entry *ce = *src++;
280 if (!ce->ce_mode) {
281 if (update)
253 - unlink(ce->name);
282 + unlink_entry(ce->name);
283 continue;
284 }
285 if (ce->ce_flags & mask) {