X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=read-tree.c;h=e3b9c0d9fa1cb5a03d1a3a08f992074293285904;hb=104f3e03c03a83617bac9d120b5e536c5f29d9ef;hp=5fdf58d240e68f2c4b332dbdd02be357641127f8;hpb=79a9d8ea0d88a3667ad19be8e705405ab5d896f1;p=git.git diff --git a/read-tree.c b/read-tree.c index 5fdf58d2..e3b9c0d9 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) { @@ -600,10 +629,14 @@ int main(int argc, char **argv) unsigned char sha1[20]; merge_fn_t fn = NULL; + setup_git_directory(); + newfd = hold_index_file_for_update(&cache_file, get_index_file()); if (newfd < 0) die("unable to create new cachefile"); + git_config(git_default_config); + merge = 0; reset = 0; for (i = 1; i < argc; i++) {