Skip to content

Commit aea1945

Browse files
author
Junio C Hamano
committed
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 <[email protected]>
1 parent 700ea47 commit aea1945

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

builtin-apply.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,15 +2232,15 @@ static void patch_stats(struct patch *patch)
22322232
}
22332233
}
22342234

2235-
static void remove_file(struct patch *patch)
2235+
static void remove_file(struct patch *patch, int rmdir_empty)
22362236
{
22372237
if (write_index) {
22382238
if (remove_file_from_cache(patch->old_name) < 0)
22392239
die("unable to remove %s from index", patch->old_name);
22402240
cache_tree_invalidate_path(active_cache_tree, patch->old_name);
22412241
}
22422242
if (!cached) {
2243-
if (!unlink(patch->old_name)) {
2243+
if (!unlink(patch->old_name) && rmdir_empty) {
22442244
char *name = xstrdup(patch->old_name);
22452245
char *end = strrchr(name, '/');
22462246
while (end) {
@@ -2373,7 +2373,7 @@ static void write_out_one_result(struct patch *patch, int phase)
23732373
{
23742374
if (patch->is_delete > 0) {
23752375
if (phase == 0)
2376-
remove_file(patch);
2376+
remove_file(patch, 1);
23772377
return;
23782378
}
23792379
if (patch->is_new > 0 || patch->is_copy) {
@@ -2386,7 +2386,7 @@ static void write_out_one_result(struct patch *patch, int phase)
23862386
* thing: remove the old, write the new
23872387
*/
23882388
if (phase == 0)
2389-
remove_file(patch);
2389+
remove_file(patch, 0);
23902390
if (phase == 1)
23912391
create_file(patch);
23922392
}

0 commit comments

Comments
 (0)