git-apply: do not lose cwd when run from a subdirectory.

When a patch modifies (not deletes) the last file in a
directory, because we treat a modification just as deletion
followed by creation, and deleting the last file in a directory
automatically rmdir(2)'s that directory, we ended up removing
the directory, which can potentially be the cwd, and then
recreating the same directory to create the patch result.

Avoid the rmdir step when remove_file() is called only because
we are replacing it with the result by later calling
create_file().

Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Junio C Hamano 2007-02-19 17:58:58 -08:00
parent 700ea47936
commit aea1945744

View File

@ -2232,7 +2232,7 @@ static void patch_stats(struct patch *patch)
} }
} }
static void remove_file(struct patch *patch) static void remove_file(struct patch *patch, int rmdir_empty)
{ {
if (write_index) { if (write_index) {
if (remove_file_from_cache(patch->old_name) < 0) if (remove_file_from_cache(patch->old_name) < 0)
@ -2240,7 +2240,7 @@ static void remove_file(struct patch *patch)
cache_tree_invalidate_path(active_cache_tree, patch->old_name); cache_tree_invalidate_path(active_cache_tree, patch->old_name);
} }
if (!cached) { if (!cached) {
if (!unlink(patch->old_name)) { if (!unlink(patch->old_name) && rmdir_empty) {
char *name = xstrdup(patch->old_name); char *name = xstrdup(patch->old_name);
char *end = strrchr(name, '/'); char *end = strrchr(name, '/');
while (end) { while (end) {
@ -2373,7 +2373,7 @@ static void write_out_one_result(struct patch *patch, int phase)
{ {
if (patch->is_delete > 0) { if (patch->is_delete > 0) {
if (phase == 0) if (phase == 0)
remove_file(patch); remove_file(patch, 1);
return; return;
} }
if (patch->is_new > 0 || patch->is_copy) { if (patch->is_new > 0 || patch->is_copy) {
@ -2386,7 +2386,7 @@ static void write_out_one_result(struct patch *patch, int phase)
* thing: remove the old, write the new * thing: remove the old, write the new
*/ */
if (phase == 0) if (phase == 0)
remove_file(patch); remove_file(patch, 0);
if (phase == 1) if (phase == 1)
create_file(patch); create_file(patch);
} }