read-tree -m -u: do not overwrite or remove untracked working tree files.
When a merge results in a creation of a path that did not exist in HEAD, and if you already have that path on the working tree, because the index has not been told about the working tree file, read-tree happily removes it. The issue was brought up by Santi Béjar on the list. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
May 17, 2006 at 01:17 UTC
fcc387db9bc453dc7e07a262873481af2ee9e5c8
5 files changed
+88
-31
read-tree.c
+40
-5
@@ -408,7 +408,7 @@ static void verify_uptodate(struct cache_entry *ce)
408
{
409
struct stat st;
410
411
- if (index_only)
411
+ if (index_only || reset)
412
return;
413
414
if (!lstat(ce->name, &st)) {
@@ -426,6 +426,21 @@ static void verify_uptodate(struct cache_entry *ce)
426
die("Entry '%s' not uptodate. Cannot merge.", ce->name);
427
}
428
429
+/*
430
+ * We do not want to remove or overwrite a working tree file that
431
+ * is not tracked.
432
+ */
433
+static void verify_absent(const char *path, const char *action)
434
+{
435
+ struct stat st;
436
+
437
+ if (index_only || reset || !update)
438
+ return;
439
+ if (!lstat(path, &st))
440
+ die("Untracked working tree file '%s' "
441
+ "would be %s by merge.", path, action);
442
+}
443
+
444
static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
445
{
446
merge->ce_flags |= htons(CE_UPDATE);
@@ -443,6 +458,9 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
458
verify_uptodate(old);
459
}
460
}
461
+ else
462
+ verify_absent(merge->name, "overwritten");
463
+
464
merge->ce_flags &= ~htons(CE_STAGEMASK);
465
add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
466
return 1;
@@ -452,6 +470,8 @@ static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
470
{
471
if (old)
472
verify_uptodate(old);
473
+ else
474
+ verify_absent(ce->name, "removed");
475
ce->ce_mode = 0;
476
add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
477
return 1;
@@ -487,6 +507,7 @@ static int threeway_merge(struct cache_entry **stages)
507
int count;
508
int head_match = 0;
509
int remote_match = 0;
510
+ const char *path = NULL;
511
512
int df_conflict_head = 0;
513
int df_conflict_remote = 0;
@@ -498,8 +519,11 @@ static int threeway_merge(struct cache_entry **stages)
519
for (i = 1; i < head_idx; i++) {
520
if (!stages[i])
521
any_anc_missing = 1;
501
- else
522
+ else {
523
+ if (!path)
524
+ path = stages[i]->name;
525
no_anc_exists = 0;
526
+ }
527
}
528
529
index = stages[0];
@@ -515,8 +539,15 @@ static int threeway_merge(struct cache_entry **stages)
539
remote = NULL;
540
}
541
542
+ if (!path && index)
543
+ path = index->name;
544
+ if (!path && head)
545
+ path = head->name;
546
+ if (!path && remote)
547
+ path = remote->name;
548
+
549
/* First, if there's a #16 situation, note that to prevent #13
519
- * and #14.
550
+ * and #14.
551
*/
552
if (!same(remote, head)) {
553
for (i = 1; i < head_idx; i++) {
@@ -575,6 +606,8 @@ static int threeway_merge(struct cache_entry **stages)
606
(remote_deleted && head && head_match)) {
607
if (index)
608
return deleted_entry(index, index);
609
+ else if (path)
610
+ verify_absent(path, "removed");
611
return 0;
612
}
613
/*
@@ -592,6 +625,8 @@ static int threeway_merge(struct cache_entry **stages)
625
if (index) {
626
verify_uptodate(index);
627
}
628
+ else if (path)
629
+ verify_absent(path, "overwritten");
630
631
nontrivial_merge = 1;
632
@@ -689,7 +724,7 @@ static int oneway_merge(struct cache_entry **src)
724
merge_size);
725
726
if (!a)
692
- return deleted_entry(old, NULL);
727
+ return deleted_entry(old, old);
728
if (old && same(old, a)) {
729
if (reset) {
730
struct stat st;
@@ -699,7 +734,7 @@ static int oneway_merge(struct cache_entry **src)
734
}
735
return keep_entry(old);
736
}
702
- return merged_entry(a, NULL);
737
+ return merged_entry(a, old);
738
}
739
740
static int read_cache_unmerged(void)
t/t1002-read-tree-m-u-2way.sh
+43
-23
@@ -39,7 +39,6 @@ test_expect_success \
39
echo nitfol >nitfol &&
40
echo bozbar >bozbar &&
41
echo rezrov >rezrov &&
42
- echo yomin >yomin &&
42
git-update-index --add nitfol bozbar rezrov &&
43
treeH=`git-write-tree` &&
44
echo treeH $treeH &&
@@ -56,7 +55,8 @@ test_expect_success \
55
56
test_expect_success \
57
'1, 2, 3 - no carry forward' \
59
- 'rm -f .git/index &&
58
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
59
+ git-read-tree --reset -u $treeH &&
60
git-read-tree -m -u $treeH $treeM &&
61
git-ls-files --stage >1-3.out &&
62
cmp M.out 1-3.out &&
@@ -66,11 +66,12 @@ test_expect_success \
66
check_cache_at frotz clean &&
67
check_cache_at nitfol clean'
68
69
-echo '+100644 X 0 yomin' >expected
70
-
69
test_expect_success \
70
'4 - carry forward local addition.' \
73
- 'rm -f .git/index &&
71
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
72
+ git-read-tree --reset -u $treeH &&
73
+ echo "+100644 X 0 yomin" >expected &&
74
+ echo yomin >yomin &&
75
git-update-index --add yomin &&
76
git-read-tree -m -u $treeH $treeM &&
77
git-ls-files --stage >4.out || return 1
@@ -85,7 +86,9 @@ test_expect_success \
86
87
test_expect_success \
88
'5 - carry forward local addition.' \
88
- 'rm -f .git/index &&
89
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
90
+ git-read-tree --reset -u $treeH &&
91
+ git-read-tree -m -u $treeH &&
92
echo yomin >yomin &&
93
git-update-index --add yomin &&
94
echo yomin yomin >yomin &&
@@ -103,7 +106,9 @@ test_expect_success \
106
107
test_expect_success \
108
'6 - local addition already has the same.' \
106
- 'rm -f .git/index &&
109
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
110
+ git-read-tree --reset -u $treeH &&
111
+ echo frotz >frotz &&
112
git-update-index --add frotz &&
113
git-read-tree -m -u $treeH $treeM &&
114
git-ls-files --stage >6.out &&
@@ -117,7 +122,8 @@ test_expect_success \
122
123
test_expect_success \
124
'7 - local addition already has the same.' \
120
- 'rm -f .git/index &&
125
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
126
+ git-read-tree --reset -u $treeH &&
127
echo frotz >frotz &&
128
git-update-index --add frotz &&
129
echo frotz frotz >frotz &&
@@ -134,14 +140,16 @@ test_expect_success \
140
141
test_expect_success \
142
'8 - conflicting addition.' \
137
- 'rm -f .git/index &&
143
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
144
+ git-read-tree --reset -u $treeH &&
145
echo frotz frotz >frotz &&
146
git-update-index --add frotz &&
147
if git-read-tree -m -u $treeH $treeM; then false; else :; fi'
148
149
test_expect_success \
150
'9 - conflicting addition.' \
144
- 'rm -f .git/index &&
151
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
152
+ git-read-tree --reset -u $treeH &&
153
echo frotz frotz >frotz &&
154
git-update-index --add frotz &&
155
echo frotz >frotz &&
@@ -149,7 +157,8 @@ test_expect_success \
157
158
test_expect_success \
159
'10 - path removed.' \
152
- 'rm -f .git/index &&
160
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
161
+ git-read-tree --reset -u $treeH &&
162
echo rezrov >rezrov &&
163
git-update-index --add rezrov &&
164
git-read-tree -m -u $treeH $treeM &&
@@ -160,7 +169,8 @@ test_expect_success \
169
170
test_expect_success \
171
'11 - dirty path removed.' \
163
- 'rm -f .git/index &&
172
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
173
+ git-read-tree --reset -u $treeH &&
174
echo rezrov >rezrov &&
175
git-update-index --add rezrov &&
176
echo rezrov rezrov >rezrov &&
@@ -168,14 +178,16 @@ test_expect_success \
178
179
test_expect_success \
180
'12 - unmatching local changes being removed.' \
171
- 'rm -f .git/index &&
181
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
182
+ git-read-tree --reset -u $treeH &&
183
echo rezrov rezrov >rezrov &&
184
git-update-index --add rezrov &&
185
if git-read-tree -m -u $treeH $treeM; then false; else :; fi'
186
187
test_expect_success \
188
'13 - unmatching local changes being removed.' \
178
- 'rm -f .git/index &&
189
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
190
+ git-read-tree --reset -u $treeH &&
191
echo rezrov rezrov >rezrov &&
192
git-update-index --add rezrov &&
193
echo rezrov >rezrov &&
@@ -188,7 +200,8 @@ EOF
200
201
test_expect_success \
202
'14 - unchanged in two heads.' \
191
- 'rm -f .git/index &&
203
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
204
+ git-read-tree --reset -u $treeH &&
205
echo nitfol nitfol >nitfol &&
206
git-update-index --add nitfol &&
207
git-read-tree -m -u $treeH $treeM &&
@@ -207,7 +220,8 @@ test_expect_success \
220
221
test_expect_success \
222
'15 - unchanged in two heads.' \
210
- 'rm -f .git/index &&
223
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
224
+ git-read-tree --reset -u $treeH &&
225
echo nitfol nitfol >nitfol &&
226
git-update-index --add nitfol &&
227
echo nitfol nitfol nitfol >nitfol &&
@@ -227,14 +241,16 @@ test_expect_success \
241
242
test_expect_success \
243
'16 - conflicting local change.' \
230
- 'rm -f .git/index &&
244
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
245
+ git-read-tree --reset -u $treeH &&
246
echo bozbar bozbar >bozbar &&
247
git-update-index --add bozbar &&
248
if git-read-tree -m -u $treeH $treeM; then false; else :; fi'
249
250
test_expect_success \
251
'17 - conflicting local change.' \
237
- 'rm -f .git/index &&
252
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
253
+ git-read-tree --reset -u $treeH &&
254
echo bozbar bozbar >bozbar &&
255
git-update-index --add bozbar &&
256
echo bozbar bozbar bozbar >bozbar &&
@@ -242,7 +258,8 @@ test_expect_success \
258
259
test_expect_success \
260
'18 - local change already having a good result.' \
245
- 'rm -f .git/index &&
261
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
262
+ git-read-tree --reset -u $treeH &&
263
echo gnusto >bozbar &&
264
git-update-index --add bozbar &&
265
git-read-tree -m -u $treeH $treeM &&
@@ -254,7 +271,8 @@ test_expect_success \
271
272
test_expect_success \
273
'19 - local change already having a good result, further modified.' \
257
- 'rm -f .git/index &&
274
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
275
+ git-read-tree --reset -u $treeH &&
276
echo gnusto >bozbar &&
277
git-update-index --add bozbar &&
278
echo gnusto gnusto >bozbar &&
@@ -273,7 +291,8 @@ test_expect_success \
291
292
test_expect_success \
293
'20 - no local change, use new tree.' \
276
- 'rm -f .git/index &&
294
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
295
+ git-read-tree --reset -u $treeH &&
296
echo bozbar >bozbar &&
297
git-update-index --add bozbar &&
298
git-read-tree -m -u $treeH $treeM &&
@@ -285,7 +304,8 @@ test_expect_success \
304
305
test_expect_success \
306
'21 - no local change, dirty cache.' \
288
- 'rm -f .git/index &&
307
+ 'rm -f .git/index nitfol bozbar rezrov frotz &&
308
+ git-read-tree --reset -u $treeH &&
309
echo bozbar >bozbar &&
310
git-update-index --add bozbar &&
311
echo gnusto gnusto >bozbar &&
@@ -294,7 +314,7 @@ test_expect_success \
314
# Also make sure we did not break DF vs DF/DF case.
315
test_expect_success \
316
'DF vs DF/DF case setup.' \
297
- 'rm -f .git/index &&
317
+ 'rm -f .git/index
318
echo DF >DF &&
319
git-update-index --add DF &&
320
treeDF=`git-write-tree` &&
t/t3500-cherry.sh
+1
@@ -30,6 +30,7 @@ test_expect_success \
30
git-commit -m "Add C." &&
31
32
git-checkout -f master &&
33
+ rm -f B C &&
34
35
echo Third >> A &&
36
git-update-index A &&
t/t4002-diff-basic.sh
+3
-3
@@ -191,7 +191,7 @@ test_expect_success \
191
'rm -fr Z [A-Z][A-Z] &&
192
git-read-tree $tree_A &&
193
git-checkout-index -f -a &&
194
- git-read-tree -m $tree_O || return 1
194
+ git-read-tree --reset $tree_O || return 1
195
git-update-index --refresh >/dev/null ;# this can exit non-zero
196
git-diff-files >.test-a &&
197
cmp_diff_files_output .test-a .test-recursive-OA'
@@ -201,7 +201,7 @@ test_expect_success \
201
'rm -fr Z [A-Z][A-Z] &&
202
git-read-tree $tree_B &&
203
git-checkout-index -f -a &&
204
- git-read-tree -m $tree_O || return 1
204
+ git-read-tree --reset $tree_O || return 1
205
git-update-index --refresh >/dev/null ;# this can exit non-zero
206
git-diff-files >.test-a &&
207
cmp_diff_files_output .test-a .test-recursive-OB'
@@ -211,7 +211,7 @@ test_expect_success \
211
'rm -fr Z [A-Z][A-Z] &&
212
git-read-tree $tree_B &&
213
git-checkout-index -f -a &&
214
- git-read-tree -m $tree_A || return 1
214
+ git-read-tree --reset $tree_A || return 1
215
git-update-index --refresh >/dev/null ;# this can exit non-zero
216
git-diff-files >.test-a &&
217
cmp_diff_files_output .test-a .test-recursive-AB'
t/t6022-merge-rename.sh
+1
@@ -111,6 +111,7 @@ test_expect_success 'pull renaming branch into unrenaming one' \
111
112
test_expect_success 'pull renaming branch into another renaming one' \
113
'
114
+ rm -f B
115
git reset --hard
116
git checkout red
117
git pull . white && {