@matheusbsbs / matheus-bagliano / commits / aea1945744
Fork 0

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>

Junio C Hamano committed Feb 19, 2007 at 17:58 UTC aea1945744214bf84908586af8be1c098a6f346d
1 file changed +4 -4
builtin-apply.c
+4 -4
@@ -2232,7 +2232,7 @@ static void patch_stats(struct patch *patch)
2232 }
2233 }
2234
2235 -static void remove_file(struct patch *patch)
2235 +static void remove_file(struct patch *patch, int rmdir_empty)
2236 {
2237 if (write_index) {
2238 if (remove_file_from_cache(patch->old_name) < 0)
@@ -2240,7 +2240,7 @@ static void remove_file(struct patch *patch)
2240 cache_tree_invalidate_path(active_cache_tree, patch->old_name);
2241 }
2242 if (!cached) {
2243 - if (!unlink(patch->old_name)) {
2243 + if (!unlink(patch->old_name) && rmdir_empty) {
2244 char *name = xstrdup(patch->old_name);
2245 char *end = strrchr(name, '/');
2246 while (end) {
@@ -2373,7 +2373,7 @@ static void write_out_one_result(struct patch *patch, int phase)
2373 {
2374 if (patch->is_delete > 0) {
2375 if (phase == 0)
2376 - remove_file(patch);
2376 + remove_file(patch, 1);
2377 return;
2378 }
2379 if (patch->is_new > 0 || patch->is_copy) {
@@ -2386,7 +2386,7 @@ static void write_out_one_result(struct patch *patch, int phase)
2386 * thing: remove the old, write the new
2387 */
2388 if (phase == 0)
2389 - remove_file(patch);
2389 + remove_file(patch, 0);
2390 if (phase == 1)
2391 create_file(patch);
2392 }