checkout -m: no need to insist on having all 3 stages
The content level merge machinery ll_merge() is prepared to merge correctly in "both sides added differently" case by using an empty blob as if it were the common ancestor. "checkout -m" could do the same, but didn't bother supporting it and instead insisted on having all three stages. Reported-by: Pete Harlan Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Dec 5, 2011 at 10:58 UTC
fbbccd0a10f314d9cce4427a4135efd051610880
1 file changed
+36
-24
builtin/checkout.c
+36
-24
@@ -110,16 +110,21 @@ static int check_stage(int stage, struct cache_entry *ce, int pos)
110
return error(_("path '%s' does not have their version"), ce->name);
111
}
112
113
-static int check_all_stages(struct cache_entry *ce, int pos)
113
+static int check_stages(unsigned stages, struct cache_entry *ce, int pos)
114
{
115
- if (ce_stage(ce) != 1 ||
116
- active_nr <= pos + 2 ||
117
- strcmp(active_cache[pos+1]->name, ce->name) ||
118
- ce_stage(active_cache[pos+1]) != 2 ||
119
- strcmp(active_cache[pos+2]->name, ce->name) ||
120
- ce_stage(active_cache[pos+2]) != 3)
121
- return error(_("path '%s' does not have all three versions"),
122
- ce->name);
115
+ unsigned seen = 0;
116
+ const char *name = ce->name;
117
+
118
+ while (pos < active_nr) {
119
+ ce = active_cache[pos];
120
+ if (strcmp(name, ce->name))
121
+ break;
122
+ seen |= (1 << ce_stage(ce));
123
+ pos++;
124
+ }
125
+ if ((stages & seen) != stages)
126
+ return error(_("path '%s' does not have all necessary versions"),
127
+ name);
128
return 0;
129
}
130
@@ -146,18 +151,27 @@ static int checkout_merged(int pos, struct checkout *state)
151
int status;
152
unsigned char sha1[20];
153
mmbuffer_t result_buf;
154
+ unsigned char threeway[3][20];
155
+ unsigned mode;
156
+
157
+ memset(threeway, 0, sizeof(threeway));
158
+ while (pos < active_nr) {
159
+ int stage;
160
+ stage = ce_stage(ce);
161
+ if (!stage || strcmp(path, ce->name))
162
+ break;
163
+ hashcpy(threeway[stage - 1], ce->sha1);
164
+ if (stage == 2)
165
+ mode = create_ce_mode(ce->ce_mode);
166
+ pos++;
167
+ ce = active_cache[pos];
168
+ }
169
+ if (is_null_sha1(threeway[1]) || is_null_sha1(threeway[2]))
170
+ return error(_("path '%s' does not have necessary versions"), path);
171
150
- if (ce_stage(ce) != 1 ||
151
- active_nr <= pos + 2 ||
152
- strcmp(active_cache[pos+1]->name, path) ||
153
- ce_stage(active_cache[pos+1]) != 2 ||
154
- strcmp(active_cache[pos+2]->name, path) ||
155
- ce_stage(active_cache[pos+2]) != 3)
156
- return error(_("path '%s' does not have all 3 versions"), path);
157
-
158
- read_mmblob(&ancestor, active_cache[pos]->sha1);
159
- read_mmblob(&ours, active_cache[pos+1]->sha1);
160
- read_mmblob(&theirs, active_cache[pos+2]->sha1);
172
+ read_mmblob(&ancestor, threeway[0]);
173
+ read_mmblob(&ours, threeway[1]);
174
+ read_mmblob(&theirs, threeway[2]);
175
176
/*
177
* NEEDSWORK: re-create conflicts from merges with
@@ -188,9 +202,7 @@ static int checkout_merged(int pos, struct checkout *state)
202
if (write_sha1_file(result_buf.ptr, result_buf.size,
203
blob_type, sha1))
204
die(_("Unable to add merge result for '%s'"), path);
191
- ce = make_cache_entry(create_ce_mode(active_cache[pos+1]->ce_mode),
192
- sha1,
193
- path, 2, 0);
205
+ ce = make_cache_entry(mode, sha1, path, 2, 0);
206
if (!ce)
207
die(_("make_cache_entry failed for path '%s'"), path);
208
status = checkout_entry(ce, state, NULL);
@@ -246,7 +258,7 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
258
} else if (stage) {
259
errs |= check_stage(stage, ce, pos);
260
} else if (opts->merge) {
249
- errs |= check_all_stages(ce, pos);
261
+ errs |= check_stages((1<<2) | (1<<3), ce, pos);
262
} else {
263
errs = 1;
264
error(_("path '%s' is unmerged"), ce->name);