@matheusbsbs / matheus-bagliano / commits / 172b6428d0
Fork 0

do not overwrite untracked during merge from unborn branch

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>

Clemens Buchacher committed Nov 14, 2010 at 23:07 UTC 172b6428d06ff4eb0812f087a497d50b6c878fe1
2 files changed +35 -1
builtin/merge.c
+19 -1
@@ -231,6 +231,24 @@ static void save_state(void)
231 die("not a valid object: %s", buffer.buf);
232 }
233
234 +static void read_empty(unsigned const char *sha1, int verbose)
235 +{
236 + int i = 0;
237 + const char *args[7];
238 +
239 + args[i++] = "read-tree";
240 + if (verbose)
241 + args[i++] = "-v";
242 + args[i++] = "-m";
243 + args[i++] = "-u";
244 + args[i++] = EMPTY_TREE_SHA1_HEX;
245 + args[i++] = sha1_to_hex(sha1);
246 + args[i] = NULL;
247 +
248 + if (run_command_v_opt(args, RUN_GIT_CMD))
249 + die("read-tree failed");
250 +}
251 +
252 static void reset_hard(unsigned const char *sha1, int verbose)
253 {
254 int i = 0;
@@ -979,7 +997,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
997 die("%s - not something we can merge", argv[0]);
998 update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
999 DIE_ON_ERR);
982 - reset_hard(remote_head->sha1, 0);
1000 + read_empty(remote_head->sha1, 0);
1001 return 0;
1002 } else {
1003 struct strbuf merge_names = STRBUF_INIT;
t/t7607-merge-overwrite.sh
+16
@@ -84,4 +84,20 @@ test_expect_success 'will not overwrite removed file with staged changes' '
84 test_cmp important c1.c
85 '
86
87 +cat >expect <<\EOF
88 +error: Untracked working tree file 'c0.c' would be overwritten by merge.
89 +fatal: read-tree failed
90 +EOF
91 +
92 +test_expect_success 'will not overwrite untracked file on unborn branch' '
93 + git reset --hard c0 &&
94 + git rm -fr . &&
95 + git checkout --orphan new &&
96 + cp important c0.c &&
97 + test_must_fail git merge c0 2>out &&
98 + test_cmp out expect &&
99 + test_path_is_missing .git/MERGE_HEAD &&
100 + test_cmp important c0.c
101 +'
102 +
103 test_done