summaryrefslogtreecommitdiff
path: root/read-tree.c
diff options
context:
space:
mode:
authorJunio C Hamano <junkio@cox.net>2005-10-11 00:34:08 (GMT)
committerJunio C Hamano <junkio@cox.net>2005-10-11 00:34:08 (GMT)
commit340e4f88c083b0692e6554b1c2c27fd43c7cc8d3 (patch)
tree2bab3caf63fc29f249c792289365abaf1cfa071b /read-tree.c
parent17712991a59824a8d22d5115c0c154d3122fc17b (diff)
downloadgit-340e4f88c083b0692e6554b1c2c27fd43c7cc8d3.zip
git-340e4f88c083b0692e6554b1c2c27fd43c7cc8d3.tar.gz
git-340e4f88c083b0692e6554b1c2c27fd43c7cc8d3.tar.bz2
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>
Diffstat (limited to 'read-tree.c')
-rw-r--r--read-tree.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/read-tree.c b/read-tree.c
index 5fdf58d..6a456ae 100644
--- a/read-tree.c
+++ b/read-tree.c
@@ -237,6 +237,35 @@ static void reject_merge(struct cache_entry *ce)
ce->name);
}
+/* Unlink the last component and attempt to remove leading
+ * directories, in case this unlink is the removal of the
+ * last entry in the directory -- empty directories are removed.
+ */
+static void unlink_entry(char *name)
+{
+ char *cp, *prev;
+
+ if (unlink(name))
+ return;
+ prev = NULL;
+ while (1) {
+ int status;
+ cp = strrchr(name, '/');
+ if (prev)
+ *prev = '/';
+ if (!cp)
+ break;
+
+ *cp = 0;
+ status = rmdir(name);
+ if (status) {
+ *cp = '/';
+ break;
+ }
+ prev = cp;
+ }
+}
+
static void check_updates(struct cache_entry **src, int nr)
{
static struct checkout state = {
@@ -250,7 +279,7 @@ static void check_updates(struct cache_entry **src, int nr)
struct cache_entry *ce = *src++;
if (!ce->ce_mode) {
if (update)
- unlink(ce->name);
+ unlink_entry(ce->name);
continue;
}
if (ce->ce_flags & mask) {