Skip to content

Commit

Permalink
git-apply: do not lose cwd when run from a subdirectory.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Junio C Hamano committed Feb 20, 2007
1 parent 700ea47 commit aea1945
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -2232,15 +2232,15 @@ 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 (remove_file_from_cache(patch->old_name) < 0)
die("unable to remove %s from index", patch->old_name);
cache_tree_invalidate_path(active_cache_tree, patch->old_name);
}
if (!cached) {
if (!unlink(patch->old_name)) {
if (!unlink(patch->old_name) && rmdir_empty) {
char *name = xstrdup(patch->old_name);
char *end = strrchr(name, '/');
while (end) {
Expand Down Expand Up @@ -2373,7 +2373,7 @@ static void write_out_one_result(struct patch *patch, int phase)
{
if (patch->is_delete > 0) {
if (phase == 0)
remove_file(patch);
remove_file(patch, 1);
return;
}
if (patch->is_new > 0 || patch->is_copy) {
Expand All @@ -2386,7 +2386,7 @@ static void write_out_one_result(struct patch *patch, int phase)
* thing: remove the old, write the new
*/
if (phase == 0)
remove_file(patch);
remove_file(patch, 0);
if (phase == 1)
create_file(patch);
}
Expand Down

0 comments on commit aea1945

Please sign in to comment.