Skip to content

Commit

Permalink
do not overwrite untracked during merge from unborn branch
Browse files Browse the repository at this point in the history
In case HEAD does not point to a valid commit yet, merge is
implemented as a hard reset. This will cause untracked files to be
overwritten.

Instead, assume the empty tree for HEAD and do a regular merge. An
untracked file will cause the merge to abort and do nothing. If no
conflicting files are present, the merge will have the same effect
as a hard reset.

Signed-off-by: Clemens Buchacher <drizzd@aon.at>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Clemens Buchacher authored and Junio C Hamano committed Nov 15, 2010
1 parent 2caf20c commit 172b642
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
20 changes: 19 additions & 1 deletion builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ static void save_state(void)
die("not a valid object: %s", buffer.buf);
}

static void read_empty(unsigned const char *sha1, int verbose)
{
int i = 0;
const char *args[7];

args[i++] = "read-tree";
if (verbose)
args[i++] = "-v";
args[i++] = "-m";
args[i++] = "-u";
args[i++] = EMPTY_TREE_SHA1_HEX;
args[i++] = sha1_to_hex(sha1);
args[i] = NULL;

if (run_command_v_opt(args, RUN_GIT_CMD))
die("read-tree failed");
}

static void reset_hard(unsigned const char *sha1, int verbose)
{
int i = 0;
Expand Down Expand Up @@ -979,7 +997,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
die("%s - not something we can merge", argv[0]);
update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
DIE_ON_ERR);
reset_hard(remote_head->sha1, 0);
read_empty(remote_head->sha1, 0);
return 0;
} else {
struct strbuf merge_names = STRBUF_INIT;
Expand Down
16 changes: 16 additions & 0 deletions t/t7607-merge-overwrite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,20 @@ test_expect_success 'will not overwrite removed file with staged changes' '
test_cmp important c1.c
'

cat >expect <<\EOF
error: Untracked working tree file 'c0.c' would be overwritten by merge.
fatal: read-tree failed
EOF

test_expect_success 'will not overwrite untracked file on unborn branch' '
git reset --hard c0 &&
git rm -fr . &&
git checkout --orphan new &&
cp important c0.c &&
test_must_fail git merge c0 2>out &&
test_cmp out expect &&
test_path_is_missing .git/MERGE_HEAD &&
test_cmp important c0.c
'

test_done

0 comments on commit 172b642

Please sign in to comment.