Skip to content

Commit fbbccd0

Browse files
committed
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 <[email protected]>
1 parent ec014ea commit fbbccd0

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

builtin/checkout.c

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,21 @@ static int check_stage(int stage, struct cache_entry *ce, int pos)
110110
return error(_("path '%s' does not have their version"), ce->name);
111111
}
112112

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)
114114
{
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);
123128
return 0;
124129
}
125130

@@ -146,18 +151,27 @@ static int checkout_merged(int pos, struct checkout *state)
146151
int status;
147152
unsigned char sha1[20];
148153
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);
149171

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]);
161175

162176
/*
163177
* NEEDSWORK: re-create conflicts from merges with
@@ -188,9 +202,7 @@ static int checkout_merged(int pos, struct checkout *state)
188202
if (write_sha1_file(result_buf.ptr, result_buf.size,
189203
blob_type, sha1))
190204
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);
194206
if (!ce)
195207
die(_("make_cache_entry failed for path '%s'"), path);
196208
status = checkout_entry(ce, state, NULL);
@@ -246,7 +258,7 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
246258
} else if (stage) {
247259
errs |= check_stage(stage, ce, pos);
248260
} else if (opts->merge) {
249-
errs |= check_all_stages(ce, pos);
261+
errs |= check_stages((1<<2) | (1<<3), ce, pos);
250262
} else {
251263
errs = 1;
252264
error(_("path '%s' is unmerged"), ce->name);

0 commit comments

Comments
 (0)