Skip to content

Commit

Permalink
merge-recursive: do not clobber untracked working tree garbage
Browse files Browse the repository at this point in the history
When merge-recursive wanted to create a new file in the work tree (either
as the final result, or a hint for reference purposes while delete/modify
conflicts), it unconditionally overwrote an untracked file in the working
tree.  Be careful not to lose whatever the user has that is not tracked.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Dec 15, 2008
1 parent 7bb1fcc commit c5ab03f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions builtin-merge-recursive.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,30 @@ static void flush_buffer(int fd, const char *buf, unsigned long size)
}
}

static int would_lose_untracked(const char *path)
{
int pos = cache_name_pos(path, strlen(path));

if (pos < 0)
pos = -1 - pos;
while (pos < active_nr &&
!strcmp(path, active_cache[pos]->name)) {
/*
* If stage #0, it is definitely tracked.
* If it has stage #2 then it was tracked
* before this merge started. All other
* cases the path was not tracked.
*/
switch (ce_stage(active_cache[pos])) {
case 0:
case 2:
return 0;
}
pos++;
}
return file_exists(path);
}

static int make_room_for_path(const char *path)
{
int status;
Expand All @@ -486,6 +510,14 @@ static int make_room_for_path(const char *path)
die(msg, path, "");
}

/*
* Do not unlink a file in the work tree if we are not
* tracking it.
*/
if (would_lose_untracked(path))
return error("refusing to lose untracked file at '%s'",
path);

/* Successful unlink is good.. */
if (!unlink(path))
return 0;
Expand Down
2 changes: 1 addition & 1 deletion t/t7607-merge-overwrite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test_expect_success 'will not overwrite staged changes' '
test_cmp important c2.c
'

test_expect_failure 'will not overwrite removed file' '
test_expect_success 'will not overwrite removed file' '
git reset --hard c1 &&
git rm c1.c &&
git commit -m "rm c1.c" &&
Expand Down

0 comments on commit c5ab03f

Please sign in to comment.