6
#include "revision.h"
7
#include "run-command.h"
8
#include "diffcore.h"
9
+#include "refs.h"
10
11
static int add_submodule_odb(const char *path)
12
{
219
strbuf_release(&buf);
220
return dirty_submodule;
221
}
222
+
223
+static int find_first_merges(struct object_array *result, const char *path,
224
+ struct commit *a, struct commit *b)
225
+{
226
+ int i, j;
227
+ struct object_array merges;
228
+ struct commit *commit;
229
+ int contains_another;
230
+
231
+ char merged_revision[42];
232
+ const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
233
+ "--all", merged_revision, NULL };
234
+ struct rev_info revs;
235
+ struct setup_revision_opt rev_opts;
236
+
237
+ memset(&merges, 0, sizeof(merges));
238
+ memset(result, 0, sizeof(struct object_array));
239
+ memset(&rev_opts, 0, sizeof(rev_opts));
240
+
241
+ /* get all revisions that merge commit a */
242
+ snprintf(merged_revision, sizeof(merged_revision), "^%s",
243
+ sha1_to_hex(a->object.sha1));
244
+ init_revisions(&revs, NULL);
245
+ rev_opts.submodule = path;
246
+ setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
247
+
248
+ /* save all revisions from the above list that contain b */
249
+ if (prepare_revision_walk(&revs))
250
+ die("revision walk setup failed");
251
+ while ((commit = get_revision(&revs)) != NULL) {
252
+ struct object *o = &(commit->object);
253
+ if (in_merge_bases(b, &commit, 1))
254
+ add_object_array(o, NULL, &merges);
255
+ }
256
+
257
+ /* Now we've got all merges that contain a and b. Prune all
258
+ * merges that contain another found merge and save them in
259
+ * result.
260
+ */
261
+ for (i = 0; i < merges.nr; i++) {
262
+ struct commit *m1 = (struct commit *) merges.objects[i].item;
263
+
264
+ contains_another = 0;
265
+ for (j = 0; j < merges.nr; j++) {
266
+ struct commit *m2 = (struct commit *) merges.objects[j].item;
267
+ if (i != j && in_merge_bases(m2, &m1, 1)) {
268
+ contains_another = 1;
269
+ break;
270
+ }
271
+ }
272
+
273
+ if (!contains_another)
274
+ add_object_array(merges.objects[i].item,
275
+ merges.objects[i].name, result);
276
+ }
277
+
278
+ free(merges.objects);
279
+ return result->nr;
280
+}
281
+
282
+static void print_commit(struct commit *commit)
283
+{
284
+ struct strbuf sb = STRBUF_INIT;
285
+ struct pretty_print_context ctx = {0};
286
+ ctx.date_mode = DATE_NORMAL;
287
+ format_commit_message(commit, " %h: %m %s", &sb, &ctx);
288
+ fprintf(stderr, "%s\n", sb.buf);
289
+ strbuf_release(&sb);
290
+}
291
+
292
+#define MERGE_WARNING(path, msg) \
293
+ warning("Failed to merge submodule %s (%s)", path, msg);
294
+
295
+int merge_submodule(unsigned char result[20], const char *path,
296
+ const unsigned char base[20], const unsigned char a[20],
297
+ const unsigned char b[20])
298
+{
299
+ struct commit *commit_base, *commit_a, *commit_b;
300
+ int parent_count;
301
+ struct object_array merges;
302
+
303
+ int i;
304
+
305
+ /* store a in result in case we fail */
306
+ hashcpy(result, a);
307
+
308
+ /* we can not handle deletion conflicts */
309
+ if (is_null_sha1(base))
310
+ return 0;
311
+ if (is_null_sha1(a))
312
+ return 0;
313
+ if (is_null_sha1(b))
314
+ return 0;
315
+
316
+ if (add_submodule_odb(path)) {
317
+ MERGE_WARNING(path, "not checked out");
318
+ return 0;
319
+ }
320
+
321
+ if (!(commit_base = lookup_commit_reference(base)) ||
322
+ !(commit_a = lookup_commit_reference(a)) ||
323
+ !(commit_b = lookup_commit_reference(b))) {
324
+ MERGE_WARNING(path, "commits not present");
325
+ return 0;
326
+ }
327
+
328
+ /* check whether both changes are forward */
329
+ if (!in_merge_bases(commit_base, &commit_a, 1) ||
330
+ !in_merge_bases(commit_base, &commit_b, 1)) {
331
+ MERGE_WARNING(path, "commits don't follow merge-base");
332
+ return 0;
333
+ }
334
+
335
+ /* Case #1: a is contained in b or vice versa */
336
+ if (in_merge_bases(commit_a, &commit_b, 1)) {
337
+ hashcpy(result, b);
338
+ return 1;
339
+ }
340
+ if (in_merge_bases(commit_b, &commit_a, 1)) {
341
+ hashcpy(result, a);
342
+ return 1;
343
+ }
344
+
345
+ /*
346
+ * Case #2: There are one or more merges that contain a and b in
347
+ * the submodule. If there is only one, then present it as a
348
+ * suggestion to the user, but leave it marked unmerged so the
349
+ * user needs to confirm the resolution.
350
+ */
351
+
352
+ /* find commit which merges them */
353
+ parent_count = find_first_merges(&merges, path, commit_a, commit_b);
354
+ switch (parent_count) {
355
+ case 0:
356
+ MERGE_WARNING(path, "merge following commits not found");
357
+ break;
358
+
359
+ case 1:
360
+ MERGE_WARNING(path, "not fast-forward");
361
+ fprintf(stderr, "Found a possible merge resolution "
362
+ "for the submodule:\n");
363
+ print_commit((struct commit *) merges.objects[0].item);
364
+ fprintf(stderr,
365
+ "If this is correct simply add it to the index "
366
+ "for example\n"
367
+ "by using:\n\n"
368
+ " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
369
+ "which will accept this suggestion.\n",
370
+ sha1_to_hex(merges.objects[0].item->sha1), path);
371
+ break;
372
+
373
+ default:
374
+ MERGE_WARNING(path, "multiple merges found");
375
+ for (i = 0; i < merges.nr; i++)
376
+ print_commit((struct commit *) merges.objects[i].item);
377
+ }
378
+
379
+ free(merges.objects);
380
+ return 0;
381
+}