{"thread":{"id":"2066","subject":"[PATCH] Remove empty directories after read-tree -u.","startedAt":"2005-10-11T00:35:01Z","lastAt":"2005-10-11T00:35:01Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"9942","messageId":"7vu0fo28ne.fsf@assigned-by-dhcp.cox.net","threadId":"2066","inReplyTo":null,"subject":"[PATCH] Remove empty directories after read-tree -u.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-10-11T00:35:01Z","receivedAt":"2005-10-11T00:35:01Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This fixes everybody's favorite gripe that switching branche with\n'git checkout' leaves empty directories.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n\n---\n\n read-tree.c |   31 ++++++++++++++++++++++++++++++-\n 1 files changed, 30 insertions(+), 1 deletions(-)\n\napplies-to: 0c8b9e1023f9c5fbaaa7151bcc105783b98c9f10\n340e4f88c083b0692e6554b1c2c27fd43c7cc8d3\ndiff --git a/read-tree.c b/read-tree.c\nindex 5fdf58d..6a456ae 100644\n--- a/read-tree.c\n+++ b/read-tree.c\n@@ -237,6 +237,35 @@ static void reject_merge(struct cache_en\n \t    ce->name);\n }\n \n+/* Unlink the last component and attempt to remove leading\n+ * directories, in case this unlink is the removal of the\n+ * last entry in the directory -- empty directories are removed.\n+ */\n+static void unlink_entry(char *name)\n+{\n+\tchar *cp, *prev;\n+\n+\tif (unlink(name))\n+\t\treturn;\n+\tprev = NULL;\n+\twhile (1) {\n+\t\tint status;\n+\t\tcp = strrchr(name, '/');\n+\t\tif (prev)\n+\t\t\t*prev = '/';\n+\t\tif (!cp)\n+\t\t\tbreak;\n+\n+\t\t*cp = 0;\n+\t\tstatus = rmdir(name);\n+\t\tif (status) {\n+\t\t\t*cp = '/';\n+\t\t\tbreak;\n+\t\t}\n+\t\tprev = cp;\n+\t}\n+}\n+\n static void check_updates(struct cache_entry **src, int nr)\n {\n \tstatic struct checkout state = {\n@@ -250,7 +279,7 @@ static void check_updates(struct cache_e\n \t\tstruct cache_entry *ce = *src++;\n \t\tif (!ce->ce_mode) {\n \t\t\tif (update)\n-\t\t\t\tunlink(ce->name);\n+\t\t\t\tunlink_entry(ce->name);\n \t\t\tcontinue;\n \t\t}\n \t\tif (ce->ce_flags & mask) {\n---\n0.99.8.GIT\n"}]}