Coverage Report

Created: 2023-11-19 07:08

/src/git/builtin/merge.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Builtin "git merge"
3
 *
4
 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
5
 *
6
 * Based on git-merge.sh by Junio C Hamano.
7
 */
8
9
#define USE_THE_INDEX_VARIABLE
10
#include "builtin.h"
11
#include "abspath.h"
12
#include "advice.h"
13
#include "config.h"
14
#include "editor.h"
15
#include "environment.h"
16
#include "gettext.h"
17
#include "hex.h"
18
#include "object-name.h"
19
#include "parse-options.h"
20
#include "lockfile.h"
21
#include "run-command.h"
22
#include "hook.h"
23
#include "diff.h"
24
#include "diff-merges.h"
25
#include "refs.h"
26
#include "refspec.h"
27
#include "commit.h"
28
#include "diffcore.h"
29
#include "path.h"
30
#include "revision.h"
31
#include "unpack-trees.h"
32
#include "cache-tree.h"
33
#include "dir.h"
34
#include "utf8.h"
35
#include "log-tree.h"
36
#include "color.h"
37
#include "rerere.h"
38
#include "help.h"
39
#include "merge.h"
40
#include "merge-recursive.h"
41
#include "merge-ort-wrappers.h"
42
#include "resolve-undo.h"
43
#include "remote.h"
44
#include "fmt-merge-msg.h"
45
#include "gpg-interface.h"
46
#include "sequencer.h"
47
#include "string-list.h"
48
#include "packfile.h"
49
#include "tag.h"
50
#include "alias.h"
51
#include "branch.h"
52
#include "commit-reach.h"
53
#include "wt-status.h"
54
#include "commit-graph.h"
55
56
0
#define DEFAULT_TWOHEAD (1<<0)
57
0
#define DEFAULT_OCTOPUS (1<<1)
58
0
#define NO_FAST_FORWARD (1<<2)
59
0
#define NO_TRIVIAL      (1<<3)
60
61
struct strategy {
62
  const char *name;
63
  unsigned attr;
64
};
65
66
static const char * const builtin_merge_usage[] = {
67
  N_("git merge [<options>] [<commit>...]"),
68
  "git merge --abort",
69
  "git merge --continue",
70
  NULL
71
};
72
73
static int show_diffstat = 1, shortlog_len = -1, squash;
74
static int option_commit = -1;
75
static int option_edit = -1;
76
static int allow_trivial = 1, have_message, verify_signatures;
77
static int check_trust_level = 1;
78
static int overwrite_ignore = 1;
79
static struct strbuf merge_msg = STRBUF_INIT;
80
static struct strategy **use_strategies;
81
static size_t use_strategies_nr, use_strategies_alloc;
82
static struct strvec xopts = STRVEC_INIT;
83
static const char *branch;
84
static char *branch_mergeoptions;
85
static int verbosity;
86
static int allow_rerere_auto;
87
static int abort_current_merge;
88
static int quit_current_merge;
89
static int continue_current_merge;
90
static int allow_unrelated_histories;
91
static int show_progress = -1;
92
static int default_to_upstream = 1;
93
static int signoff;
94
static const char *sign_commit;
95
static int autostash;
96
static int no_verify;
97
static char *into_name;
98
99
static struct strategy all_strategy[] = {
100
  { "recursive",  NO_TRIVIAL },
101
  { "octopus",    DEFAULT_OCTOPUS },
102
  { "ort",        DEFAULT_TWOHEAD | NO_TRIVIAL },
103
  { "resolve",    0 },
104
  { "ours",       NO_FAST_FORWARD | NO_TRIVIAL },
105
  { "subtree",    NO_FAST_FORWARD | NO_TRIVIAL },
106
};
107
108
static const char *pull_twohead, *pull_octopus;
109
110
enum ff_type {
111
  FF_NO,
112
  FF_ALLOW,
113
  FF_ONLY
114
};
115
116
static enum ff_type fast_forward = FF_ALLOW;
117
118
static const char *cleanup_arg;
119
static enum commit_msg_cleanup_mode cleanup_mode;
120
121
static int option_parse_message(const struct option *opt,
122
        const char *arg, int unset)
123
0
{
124
0
  struct strbuf *buf = opt->value;
125
126
0
  if (unset)
127
0
    strbuf_setlen(buf, 0);
128
0
  else if (arg) {
129
0
    strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg);
130
0
    have_message = 1;
131
0
  } else
132
0
    return error(_("switch `m' requires a value"));
133
0
  return 0;
134
0
}
135
136
static enum parse_opt_result option_read_message(struct parse_opt_ctx_t *ctx,
137
             const struct option *opt,
138
             const char *arg_not_used,
139
             int unset)
140
0
{
141
0
  struct strbuf *buf = opt->value;
142
0
  const char *arg;
143
144
0
  BUG_ON_OPT_ARG(arg_not_used);
145
0
  if (unset)
146
0
    BUG("-F cannot be negated");
147
148
0
  if (ctx->opt) {
149
0
    arg = ctx->opt;
150
0
    ctx->opt = NULL;
151
0
  } else if (ctx->argc > 1) {
152
0
    ctx->argc--;
153
0
    arg = *++ctx->argv;
154
0
  } else
155
0
    return error(_("option `%s' requires a value"), opt->long_name);
156
157
0
  if (buf->len)
158
0
    strbuf_addch(buf, '\n');
159
0
  if (ctx->prefix && !is_absolute_path(arg))
160
0
    arg = prefix_filename(ctx->prefix, arg);
161
0
  if (strbuf_read_file(buf, arg, 0) < 0)
162
0
    return error(_("could not read file '%s'"), arg);
163
0
  have_message = 1;
164
165
0
  return 0;
166
0
}
167
168
static struct strategy *get_strategy(const char *name)
169
0
{
170
0
  int i;
171
0
  struct strategy *ret;
172
0
  static struct cmdnames main_cmds, other_cmds;
173
0
  static int loaded;
174
0
  char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
175
176
0
  if (!name)
177
0
    return NULL;
178
179
0
  if (default_strategy &&
180
0
      !strcmp(default_strategy, "ort") &&
181
0
      !strcmp(name, "recursive")) {
182
0
    name = "ort";
183
0
  }
184
185
0
  for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
186
0
    if (!strcmp(name, all_strategy[i].name))
187
0
      return &all_strategy[i];
188
189
0
  if (!loaded) {
190
0
    struct cmdnames not_strategies;
191
0
    loaded = 1;
192
193
0
    memset(&not_strategies, 0, sizeof(struct cmdnames));
194
0
    load_command_list("git-merge-", &main_cmds, &other_cmds);
195
0
    for (i = 0; i < main_cmds.cnt; i++) {
196
0
      int j, found = 0;
197
0
      struct cmdname *ent = main_cmds.names[i];
198
0
      for (j = 0; !found && j < ARRAY_SIZE(all_strategy); j++)
199
0
        if (!strncmp(ent->name, all_strategy[j].name, ent->len)
200
0
            && !all_strategy[j].name[ent->len])
201
0
          found = 1;
202
0
      if (!found)
203
0
        add_cmdname(&not_strategies, ent->name, ent->len);
204
0
    }
205
0
    exclude_cmds(&main_cmds, &not_strategies);
206
0
  }
207
0
  if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
208
0
    fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
209
0
    fprintf(stderr, _("Available strategies are:"));
210
0
    for (i = 0; i < main_cmds.cnt; i++)
211
0
      fprintf(stderr, " %s", main_cmds.names[i]->name);
212
0
    fprintf(stderr, ".\n");
213
0
    if (other_cmds.cnt) {
214
0
      fprintf(stderr, _("Available custom strategies are:"));
215
0
      for (i = 0; i < other_cmds.cnt; i++)
216
0
        fprintf(stderr, " %s", other_cmds.names[i]->name);
217
0
      fprintf(stderr, ".\n");
218
0
    }
219
0
    exit(1);
220
0
  }
221
222
0
  CALLOC_ARRAY(ret, 1);
223
0
  ret->name = xstrdup(name);
224
0
  ret->attr = NO_TRIVIAL;
225
0
  return ret;
226
0
}
227
228
static void append_strategy(struct strategy *s)
229
0
{
230
0
  ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
231
0
  use_strategies[use_strategies_nr++] = s;
232
0
}
233
234
static int option_parse_strategy(const struct option *opt UNUSED,
235
         const char *name, int unset)
236
0
{
237
0
  if (unset)
238
0
    return 0;
239
240
0
  append_strategy(get_strategy(name));
241
0
  return 0;
242
0
}
243
244
static struct option builtin_merge_options[] = {
245
  OPT_SET_INT('n', NULL, &show_diffstat,
246
    N_("do not show a diffstat at the end of the merge"), 0),
247
  OPT_BOOL(0, "stat", &show_diffstat,
248
    N_("show a diffstat at the end of the merge")),
249
  OPT_BOOL(0, "summary", &show_diffstat, N_("(synonym to --stat)")),
250
  { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
251
    N_("add (at most <n>) entries from shortlog to merge commit message"),
252
    PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
253
  OPT_BOOL(0, "squash", &squash,
254
    N_("create a single commit instead of doing a merge")),
255
  OPT_BOOL(0, "commit", &option_commit,
256
    N_("perform a commit if the merge succeeds (default)")),
257
  OPT_BOOL('e', "edit", &option_edit,
258
    N_("edit message before committing")),
259
  OPT_CLEANUP(&cleanup_arg),
260
  OPT_SET_INT(0, "ff", &fast_forward, N_("allow fast-forward (default)"), FF_ALLOW),
261
  OPT_SET_INT_F(0, "ff-only", &fast_forward,
262
          N_("abort if fast-forward is not possible"),
263
          FF_ONLY, PARSE_OPT_NONEG),
264
  OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
265
  OPT_BOOL(0, "verify-signatures", &verify_signatures,
266
    N_("verify that the named commit has a valid GPG signature")),
267
  OPT_CALLBACK('s', "strategy", NULL, N_("strategy"),
268
    N_("merge strategy to use"), option_parse_strategy),
269
  OPT_STRVEC('X', "strategy-option", &xopts, N_("option=value"),
270
    N_("option for selected merge strategy")),
271
  OPT_CALLBACK('m', "message", &merge_msg, N_("message"),
272
    N_("merge commit message (for a non-fast-forward merge)"),
273
    option_parse_message),
274
  { OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
275
    N_("read message from file"), PARSE_OPT_NONEG,
276
    NULL, 0, option_read_message },
277
  OPT_STRING(0, "into-name", &into_name, N_("name"),
278
       N_("use <name> instead of the real target")),
279
  OPT__VERBOSITY(&verbosity),
280
  OPT_BOOL(0, "abort", &abort_current_merge,
281
    N_("abort the current in-progress merge")),
282
  OPT_BOOL(0, "quit", &quit_current_merge,
283
    N_("--abort but leave index and working tree alone")),
284
  OPT_BOOL(0, "continue", &continue_current_merge,
285
    N_("continue the current in-progress merge")),
286
  OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories,
287
     N_("allow merging unrelated histories")),
288
  OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1),
289
  { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
290
    N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
291
  OPT_AUTOSTASH(&autostash),
292
  OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
293
  OPT_BOOL(0, "signoff", &signoff, N_("add a Signed-off-by trailer")),
294
  OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")),
295
  OPT_END()
296
};
297
298
static int save_state(struct object_id *stash)
299
0
{
300
0
  int len;
301
0
  struct child_process cp = CHILD_PROCESS_INIT;
302
0
  struct strbuf buffer = STRBUF_INIT;
303
0
  struct lock_file lock_file = LOCK_INIT;
304
0
  int fd;
305
0
  int rc = -1;
306
307
0
  fd = repo_hold_locked_index(the_repository, &lock_file, 0);
308
0
  refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
309
0
  if (0 <= fd)
310
0
    repo_update_index_if_able(the_repository, &lock_file);
311
0
  rollback_lock_file(&lock_file);
312
313
0
  strvec_pushl(&cp.args, "stash", "create", NULL);
314
0
  cp.out = -1;
315
0
  cp.git_cmd = 1;
316
317
0
  if (start_command(&cp))
318
0
    die(_("could not run stash."));
319
0
  len = strbuf_read(&buffer, cp.out, 1024);
320
0
  close(cp.out);
321
322
0
  if (finish_command(&cp) || len < 0)
323
0
    die(_("stash failed"));
324
0
  else if (!len)   /* no changes */
325
0
    goto out;
326
0
  strbuf_setlen(&buffer, buffer.len-1);
327
0
  if (repo_get_oid(the_repository, buffer.buf, stash))
328
0
    die(_("not a valid object: %s"), buffer.buf);
329
0
  rc = 0;
330
0
out:
331
0
  strbuf_release(&buffer);
332
0
  return rc;
333
0
}
334
335
static void read_empty(const struct object_id *oid)
336
0
{
337
0
  struct child_process cmd = CHILD_PROCESS_INIT;
338
339
0
  strvec_pushl(&cmd.args, "read-tree", "-m", "-u", empty_tree_oid_hex(),
340
0
         oid_to_hex(oid), NULL);
341
0
  cmd.git_cmd = 1;
342
343
0
  if (run_command(&cmd))
344
0
    die(_("read-tree failed"));
345
0
}
346
347
static void reset_hard(const struct object_id *oid)
348
0
{
349
0
  struct child_process cmd = CHILD_PROCESS_INIT;
350
351
0
  strvec_pushl(&cmd.args, "read-tree", "-v", "--reset", "-u",
352
0
         oid_to_hex(oid), NULL);
353
0
  cmd.git_cmd = 1;
354
355
0
  if (run_command(&cmd))
356
0
    die(_("read-tree failed"));
357
0
}
358
359
static void restore_state(const struct object_id *head,
360
        const struct object_id *stash)
361
0
{
362
0
  struct child_process cmd = CHILD_PROCESS_INIT;
363
364
0
  reset_hard(head);
365
366
0
  if (is_null_oid(stash))
367
0
    goto refresh_cache;
368
369
0
  strvec_pushl(&cmd.args, "stash", "apply", "--index", "--quiet", NULL);
370
0
  strvec_push(&cmd.args, oid_to_hex(stash));
371
372
  /*
373
   * It is OK to ignore error here, for example when there was
374
   * nothing to restore.
375
   */
376
0
  cmd.git_cmd = 1;
377
0
  run_command(&cmd);
378
379
0
refresh_cache:
380
0
  discard_index(&the_index);
381
0
  if (repo_read_index(the_repository) < 0)
382
0
    die(_("could not read index"));
383
0
}
384
385
/* This is called when no merge was necessary. */
386
static void finish_up_to_date(void)
387
0
{
388
0
  if (verbosity >= 0) {
389
0
    if (squash)
390
0
      puts(_("Already up to date. (nothing to squash)"));
391
0
    else
392
0
      puts(_("Already up to date."));
393
0
  }
394
0
  remove_merge_branch_state(the_repository);
395
0
}
396
397
static void squash_message(struct commit *commit, struct commit_list *remoteheads)
398
0
{
399
0
  struct rev_info rev;
400
0
  struct strbuf out = STRBUF_INIT;
401
0
  struct commit_list *j;
402
0
  struct pretty_print_context ctx = {0};
403
404
0
  printf(_("Squash commit -- not updating HEAD\n"));
405
406
0
  repo_init_revisions(the_repository, &rev, NULL);
407
0
  diff_merges_suppress(&rev);
408
0
  rev.commit_format = CMIT_FMT_MEDIUM;
409
410
0
  commit->object.flags |= UNINTERESTING;
411
0
  add_pending_object(&rev, &commit->object, NULL);
412
413
0
  for (j = remoteheads; j; j = j->next)
414
0
    add_pending_object(&rev, &j->item->object, NULL);
415
416
0
  setup_revisions(0, NULL, &rev, NULL);
417
0
  if (prepare_revision_walk(&rev))
418
0
    die(_("revision walk setup failed"));
419
420
0
  ctx.abbrev = rev.abbrev;
421
0
  ctx.date_mode = rev.date_mode;
422
0
  ctx.fmt = rev.commit_format;
423
424
0
  strbuf_addstr(&out, "Squashed commit of the following:\n");
425
0
  while ((commit = get_revision(&rev)) != NULL) {
426
0
    strbuf_addch(&out, '\n');
427
0
    strbuf_addf(&out, "commit %s\n",
428
0
      oid_to_hex(&commit->object.oid));
429
0
    pretty_print_commit(&ctx, commit, &out);
430
0
  }
431
0
  write_file_buf(git_path_squash_msg(the_repository), out.buf, out.len);
432
0
  strbuf_release(&out);
433
0
  release_revisions(&rev);
434
0
}
435
436
static void finish(struct commit *head_commit,
437
       struct commit_list *remoteheads,
438
       const struct object_id *new_head, const char *msg)
439
0
{
440
0
  struct strbuf reflog_message = STRBUF_INIT;
441
0
  const struct object_id *head = &head_commit->object.oid;
442
443
0
  if (!msg)
444
0
    strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
445
0
  else {
446
0
    if (verbosity >= 0)
447
0
      printf("%s\n", msg);
448
0
    strbuf_addf(&reflog_message, "%s: %s",
449
0
      getenv("GIT_REFLOG_ACTION"), msg);
450
0
  }
451
0
  if (squash) {
452
0
    squash_message(head_commit, remoteheads);
453
0
  } else {
454
0
    if (verbosity >= 0 && !merge_msg.len)
455
0
      printf(_("No merge message -- not updating HEAD\n"));
456
0
    else {
457
0
      update_ref(reflog_message.buf, "HEAD", new_head, head,
458
0
           0, UPDATE_REFS_DIE_ON_ERR);
459
      /*
460
       * We ignore errors in 'gc --auto', since the
461
       * user should see them.
462
       */
463
0
      run_auto_maintenance(verbosity < 0);
464
0
    }
465
0
  }
466
0
  if (new_head && show_diffstat) {
467
0
    struct diff_options opts;
468
0
    repo_diff_setup(the_repository, &opts);
469
0
    init_diffstat_widths(&opts);
470
0
    opts.output_format |=
471
0
      DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
472
0
    opts.detect_rename = DIFF_DETECT_RENAME;
473
0
    diff_setup_done(&opts);
474
0
    diff_tree_oid(head, new_head, "", &opts);
475
0
    diffcore_std(&opts);
476
0
    diff_flush(&opts);
477
0
  }
478
479
  /* Run a post-merge hook */
480
0
  run_hooks_l("post-merge", squash ? "1" : "0", NULL);
481
482
0
  if (new_head)
483
0
    apply_autostash(git_path_merge_autostash(the_repository));
484
0
  strbuf_release(&reflog_message);
485
0
}
486
487
/* Get the name for the merge commit's message. */
488
static void merge_name(const char *remote, struct strbuf *msg)
489
0
{
490
0
  struct commit *remote_head;
491
0
  struct object_id branch_head;
492
0
  struct strbuf bname = STRBUF_INIT;
493
0
  struct merge_remote_desc *desc;
494
0
  const char *ptr;
495
0
  char *found_ref = NULL;
496
0
  int len, early;
497
498
0
  strbuf_branchname(&bname, remote, 0);
499
0
  remote = bname.buf;
500
501
0
  oidclr(&branch_head);
502
0
  remote_head = get_merge_parent(remote);
503
0
  if (!remote_head)
504
0
    die(_("'%s' does not point to a commit"), remote);
505
506
0
  if (repo_dwim_ref(the_repository, remote, strlen(remote), &branch_head,
507
0
        &found_ref, 0) > 0) {
508
0
    if (starts_with(found_ref, "refs/heads/")) {
509
0
      strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
510
0
            oid_to_hex(&branch_head), remote);
511
0
      goto cleanup;
512
0
    }
513
0
    if (starts_with(found_ref, "refs/tags/")) {
514
0
      strbuf_addf(msg, "%s\t\ttag '%s' of .\n",
515
0
            oid_to_hex(&branch_head), remote);
516
0
      goto cleanup;
517
0
    }
518
0
    if (starts_with(found_ref, "refs/remotes/")) {
519
0
      strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n",
520
0
            oid_to_hex(&branch_head), remote);
521
0
      goto cleanup;
522
0
    }
523
0
  }
524
525
  /* See if remote matches <name>^^^.. or <name>~<number> */
526
0
  for (len = 0, ptr = remote + strlen(remote);
527
0
       remote < ptr && ptr[-1] == '^';
528
0
       ptr--)
529
0
    len++;
530
0
  if (len)
531
0
    early = 1;
532
0
  else {
533
0
    early = 0;
534
0
    ptr = strrchr(remote, '~');
535
0
    if (ptr) {
536
0
      int seen_nonzero = 0;
537
538
0
      len++; /* count ~ */
539
0
      while (*++ptr && isdigit(*ptr)) {
540
0
        seen_nonzero |= (*ptr != '0');
541
0
        len++;
542
0
      }
543
0
      if (*ptr)
544
0
        len = 0; /* not ...~<number> */
545
0
      else if (seen_nonzero)
546
0
        early = 1;
547
0
      else if (len == 1)
548
0
        early = 1; /* "name~" is "name~1"! */
549
0
    }
550
0
  }
551
0
  if (len) {
552
0
    struct strbuf truname = STRBUF_INIT;
553
0
    strbuf_addf(&truname, "refs/heads/%s", remote);
554
0
    strbuf_setlen(&truname, truname.len - len);
555
0
    if (ref_exists(truname.buf)) {
556
0
      strbuf_addf(msg,
557
0
            "%s\t\tbranch '%s'%s of .\n",
558
0
            oid_to_hex(&remote_head->object.oid),
559
0
            truname.buf + 11,
560
0
            (early ? " (early part)" : ""));
561
0
      strbuf_release(&truname);
562
0
      goto cleanup;
563
0
    }
564
0
    strbuf_release(&truname);
565
0
  }
566
567
0
  desc = merge_remote_util(remote_head);
568
0
  if (desc && desc->obj && desc->obj->type == OBJ_TAG) {
569
0
    strbuf_addf(msg, "%s\t\t%s '%s'\n",
570
0
          oid_to_hex(&desc->obj->oid),
571
0
          type_name(desc->obj->type),
572
0
          remote);
573
0
    goto cleanup;
574
0
  }
575
576
0
  strbuf_addf(msg, "%s\t\tcommit '%s'\n",
577
0
    oid_to_hex(&remote_head->object.oid), remote);
578
0
cleanup:
579
0
  free(found_ref);
580
0
  strbuf_release(&bname);
581
0
}
582
583
static void parse_branch_merge_options(char *bmo)
584
0
{
585
0
  const char **argv;
586
0
  int argc;
587
588
0
  if (!bmo)
589
0
    return;
590
0
  argc = split_cmdline(bmo, &argv);
591
0
  if (argc < 0)
592
0
    die(_("Bad branch.%s.mergeoptions string: %s"), branch,
593
0
        _(split_cmdline_strerror(argc)));
594
0
  REALLOC_ARRAY(argv, argc + 2);
595
0
  MOVE_ARRAY(argv + 1, argv, argc + 1);
596
0
  argc++;
597
0
  argv[0] = "branch.*.mergeoptions";
598
0
  parse_options(argc, argv, NULL, builtin_merge_options,
599
0
          builtin_merge_usage, 0);
600
0
  free(argv);
601
0
}
602
603
static int git_merge_config(const char *k, const char *v,
604
          const struct config_context *ctx, void *cb)
605
0
{
606
0
  int status;
607
0
  const char *str;
608
609
0
  if (branch &&
610
0
      skip_prefix(k, "branch.", &str) &&
611
0
      skip_prefix(str, branch, &str) &&
612
0
      !strcmp(str, ".mergeoptions")) {
613
0
    free(branch_mergeoptions);
614
0
    branch_mergeoptions = xstrdup(v);
615
0
    return 0;
616
0
  }
617
618
0
  if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
619
0
    show_diffstat = git_config_bool(k, v);
620
0
  else if (!strcmp(k, "merge.verifysignatures"))
621
0
    verify_signatures = git_config_bool(k, v);
622
0
  else if (!strcmp(k, "pull.twohead"))
623
0
    return git_config_string(&pull_twohead, k, v);
624
0
  else if (!strcmp(k, "pull.octopus"))
625
0
    return git_config_string(&pull_octopus, k, v);
626
0
  else if (!strcmp(k, "commit.cleanup"))
627
0
    return git_config_string(&cleanup_arg, k, v);
628
0
  else if (!strcmp(k, "merge.ff")) {
629
0
    int boolval = git_parse_maybe_bool(v);
630
0
    if (0 <= boolval) {
631
0
      fast_forward = boolval ? FF_ALLOW : FF_NO;
632
0
    } else if (v && !strcmp(v, "only")) {
633
0
      fast_forward = FF_ONLY;
634
0
    } /* do not barf on values from future versions of git */
635
0
    return 0;
636
0
  } else if (!strcmp(k, "merge.defaulttoupstream")) {
637
0
    default_to_upstream = git_config_bool(k, v);
638
0
    return 0;
639
0
  } else if (!strcmp(k, "commit.gpgsign")) {
640
0
    sign_commit = git_config_bool(k, v) ? "" : NULL;
641
0
    return 0;
642
0
  } else if (!strcmp(k, "gpg.mintrustlevel")) {
643
0
    check_trust_level = 0;
644
0
  } else if (!strcmp(k, "merge.autostash")) {
645
0
    autostash = git_config_bool(k, v);
646
0
    return 0;
647
0
  }
648
649
0
  status = fmt_merge_msg_config(k, v, ctx, cb);
650
0
  if (status)
651
0
    return status;
652
0
  return git_diff_ui_config(k, v, ctx, cb);
653
0
}
654
655
static int read_tree_trivial(struct object_id *common, struct object_id *head,
656
           struct object_id *one)
657
0
{
658
0
  int i, nr_trees = 0;
659
0
  struct tree *trees[MAX_UNPACK_TREES];
660
0
  struct tree_desc t[MAX_UNPACK_TREES];
661
0
  struct unpack_trees_options opts;
662
663
0
  memset(&opts, 0, sizeof(opts));
664
0
  opts.head_idx = 2;
665
0
  opts.src_index = &the_index;
666
0
  opts.dst_index = &the_index;
667
0
  opts.update = 1;
668
0
  opts.verbose_update = 1;
669
0
  opts.trivial_merges_only = 1;
670
0
  opts.merge = 1;
671
0
  opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
672
0
  trees[nr_trees] = parse_tree_indirect(common);
673
0
  if (!trees[nr_trees++])
674
0
    return -1;
675
0
  trees[nr_trees] = parse_tree_indirect(head);
676
0
  if (!trees[nr_trees++])
677
0
    return -1;
678
0
  trees[nr_trees] = parse_tree_indirect(one);
679
0
  if (!trees[nr_trees++])
680
0
    return -1;
681
0
  opts.fn = threeway_merge;
682
0
  cache_tree_free(&the_index.cache_tree);
683
0
  for (i = 0; i < nr_trees; i++) {
684
0
    parse_tree(trees[i]);
685
0
    init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
686
0
  }
687
0
  if (unpack_trees(nr_trees, t, &opts))
688
0
    return -1;
689
0
  return 0;
690
0
}
691
692
static void write_tree_trivial(struct object_id *oid)
693
0
{
694
0
  if (write_index_as_tree(oid, &the_index, get_index_file(), 0, NULL))
695
0
    die(_("git write-tree failed to write a tree"));
696
0
}
697
698
static int try_merge_strategy(const char *strategy, struct commit_list *common,
699
            struct commit_list *remoteheads,
700
            struct commit *head)
701
0
{
702
0
  const char *head_arg = "HEAD";
703
704
0
  if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET,
705
0
           SKIP_IF_UNCHANGED, 0, NULL, NULL,
706
0
           NULL) < 0)
707
0
    return error(_("Unable to write index."));
708
709
0
  if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree") ||
710
0
      !strcmp(strategy, "ort")) {
711
0
    struct lock_file lock = LOCK_INIT;
712
0
    int clean, x;
713
0
    struct commit *result;
714
0
    struct commit_list *reversed = NULL;
715
0
    struct merge_options o;
716
0
    struct commit_list *j;
717
718
0
    if (remoteheads->next) {
719
0
      error(_("Not handling anything other than two heads merge."));
720
0
      return 2;
721
0
    }
722
723
0
    init_merge_options(&o, the_repository);
724
0
    if (!strcmp(strategy, "subtree"))
725
0
      o.subtree_shift = "";
726
727
0
    o.show_rename_progress =
728
0
      show_progress == -1 ? isatty(2) : show_progress;
729
730
0
    for (x = 0; x < xopts.nr; x++)
731
0
      if (parse_merge_opt(&o, xopts.v[x]))
732
0
        die(_("unknown strategy option: -X%s"), xopts.v[x]);
733
734
0
    o.branch1 = head_arg;
735
0
    o.branch2 = merge_remote_util(remoteheads->item)->name;
736
737
0
    for (j = common; j; j = j->next)
738
0
      commit_list_insert(j->item, &reversed);
739
740
0
    repo_hold_locked_index(the_repository, &lock,
741
0
               LOCK_DIE_ON_ERROR);
742
0
    if (!strcmp(strategy, "ort"))
743
0
      clean = merge_ort_recursive(&o, head, remoteheads->item,
744
0
                reversed, &result);
745
0
    else
746
0
      clean = merge_recursive(&o, head, remoteheads->item,
747
0
            reversed, &result);
748
0
    if (clean < 0) {
749
0
      rollback_lock_file(&lock);
750
0
      return 2;
751
0
    }
752
0
    if (write_locked_index(&the_index, &lock,
753
0
               COMMIT_LOCK | SKIP_IF_UNCHANGED))
754
0
      die(_("unable to write %s"), get_index_file());
755
0
    return clean ? 0 : 1;
756
0
  } else {
757
0
    return try_merge_command(the_repository,
758
0
           strategy, xopts.nr, xopts.v,
759
0
           common, head_arg, remoteheads);
760
0
  }
761
0
}
762
763
static void count_diff_files(struct diff_queue_struct *q,
764
           struct diff_options *opt UNUSED, void *data)
765
0
{
766
0
  int *count = data;
767
768
0
  (*count) += q->nr;
769
0
}
770
771
static int count_unmerged_entries(void)
772
0
{
773
0
  int i, ret = 0;
774
775
0
  for (i = 0; i < the_index.cache_nr; i++)
776
0
    if (ce_stage(the_index.cache[i]))
777
0
      ret++;
778
779
0
  return ret;
780
0
}
781
782
static void add_strategies(const char *string, unsigned attr)
783
0
{
784
0
  int i;
785
786
0
  if (string) {
787
0
    struct string_list list = STRING_LIST_INIT_DUP;
788
0
    struct string_list_item *item;
789
0
    string_list_split(&list, string, ' ', -1);
790
0
    for_each_string_list_item(item, &list)
791
0
      append_strategy(get_strategy(item->string));
792
0
    string_list_clear(&list, 0);
793
0
    return;
794
0
  }
795
0
  for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
796
0
    if (all_strategy[i].attr & attr)
797
0
      append_strategy(&all_strategy[i]);
798
799
0
}
800
801
static void read_merge_msg(struct strbuf *msg)
802
0
{
803
0
  const char *filename = git_path_merge_msg(the_repository);
804
0
  strbuf_reset(msg);
805
0
  if (strbuf_read_file(msg, filename, 0) < 0)
806
0
    die_errno(_("Could not read from '%s'"), filename);
807
0
}
808
809
static void write_merge_state(struct commit_list *);
810
static void abort_commit(struct commit_list *remoteheads, const char *err_msg)
811
0
{
812
0
  if (err_msg)
813
0
    error("%s", err_msg);
814
0
  fprintf(stderr,
815
0
    _("Not committing merge; use 'git commit' to complete the merge.\n"));
816
0
  write_merge_state(remoteheads);
817
0
  exit(1);
818
0
}
819
820
static const char merge_editor_comment[] =
821
N_("Please enter a commit message to explain why this merge is necessary,\n"
822
   "especially if it merges an updated upstream into a topic branch.\n"
823
   "\n");
824
825
static const char scissors_editor_comment[] =
826
N_("An empty message aborts the commit.\n");
827
828
static const char no_scissors_editor_comment[] =
829
N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
830
   "the commit.\n");
831
832
static void write_merge_heads(struct commit_list *);
833
static void prepare_to_commit(struct commit_list *remoteheads)
834
0
{
835
0
  struct strbuf msg = STRBUF_INIT;
836
0
  const char *index_file = get_index_file();
837
838
0
  if (!no_verify) {
839
0
    int invoked_hook;
840
841
0
    if (run_commit_hook(0 < option_edit, index_file, &invoked_hook,
842
0
            "pre-merge-commit", NULL))
843
0
      abort_commit(remoteheads, NULL);
844
    /*
845
     * Re-read the index as pre-merge-commit hook could have updated it,
846
     * and write it out as a tree.  We must do this before we invoke
847
     * the editor and after we invoke run_status above.
848
     */
849
0
    if (invoked_hook)
850
0
      discard_index(&the_index);
851
0
  }
852
0
  read_index_from(&the_index, index_file, get_git_dir());
853
0
  strbuf_addbuf(&msg, &merge_msg);
854
0
  if (squash)
855
0
    BUG("the control must not reach here under --squash");
856
0
  if (0 < option_edit) {
857
0
    strbuf_addch(&msg, '\n');
858
0
    if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
859
0
      wt_status_append_cut_line(&msg);
860
0
      strbuf_commented_addf(&msg, comment_line_char, "\n");
861
0
    }
862
0
    strbuf_commented_addf(&msg, comment_line_char,
863
0
              _(merge_editor_comment));
864
0
    if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
865
0
      strbuf_commented_addf(&msg, comment_line_char,
866
0
                _(scissors_editor_comment));
867
0
    else
868
0
      strbuf_commented_addf(&msg, comment_line_char,
869
0
        _(no_scissors_editor_comment), comment_line_char);
870
0
  }
871
0
  if (signoff)
872
0
    append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
873
0
  write_merge_heads(remoteheads);
874
0
  write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
875
0
  if (run_commit_hook(0 < option_edit, get_index_file(), NULL,
876
0
          "prepare-commit-msg",
877
0
          git_path_merge_msg(the_repository), "merge", NULL))
878
0
    abort_commit(remoteheads, NULL);
879
0
  if (0 < option_edit) {
880
0
    if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
881
0
      abort_commit(remoteheads, NULL);
882
0
  }
883
884
0
  if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
885
0
            NULL, "commit-msg",
886
0
            git_path_merge_msg(the_repository), NULL))
887
0
    abort_commit(remoteheads, NULL);
888
889
0
  read_merge_msg(&msg);
890
0
  cleanup_message(&msg, cleanup_mode, 0);
891
0
  if (!msg.len)
892
0
    abort_commit(remoteheads, _("Empty commit message."));
893
0
  strbuf_release(&merge_msg);
894
0
  strbuf_addbuf(&merge_msg, &msg);
895
0
  strbuf_release(&msg);
896
0
}
897
898
static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
899
0
{
900
0
  struct object_id result_tree, result_commit;
901
0
  struct commit_list *parents, **pptr = &parents;
902
903
0
  if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET,
904
0
           SKIP_IF_UNCHANGED, 0, NULL, NULL,
905
0
           NULL) < 0)
906
0
    return error(_("Unable to write index."));
907
908
0
  write_tree_trivial(&result_tree);
909
0
  printf(_("Wonderful.\n"));
910
0
  pptr = commit_list_append(head, pptr);
911
0
  pptr = commit_list_append(remoteheads->item, pptr);
912
0
  prepare_to_commit(remoteheads);
913
0
  if (commit_tree(merge_msg.buf, merge_msg.len, &result_tree, parents,
914
0
      &result_commit, NULL, sign_commit))
915
0
    die(_("failed to write commit object"));
916
0
  finish(head, remoteheads, &result_commit, "In-index merge");
917
0
  remove_merge_branch_state(the_repository);
918
0
  return 0;
919
0
}
920
921
static int finish_automerge(struct commit *head,
922
          int head_subsumed,
923
          struct commit_list *common,
924
          struct commit_list *remoteheads,
925
          struct object_id *result_tree,
926
          const char *wt_strategy)
927
0
{
928
0
  struct commit_list *parents = NULL;
929
0
  struct strbuf buf = STRBUF_INIT;
930
0
  struct object_id result_commit;
931
932
0
  write_tree_trivial(result_tree);
933
0
  free_commit_list(common);
934
0
  parents = remoteheads;
935
0
  if (!head_subsumed || fast_forward == FF_NO)
936
0
    commit_list_insert(head, &parents);
937
0
  prepare_to_commit(remoteheads);
938
0
  if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
939
0
      &result_commit, NULL, sign_commit))
940
0
    die(_("failed to write commit object"));
941
0
  strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
942
0
  finish(head, remoteheads, &result_commit, buf.buf);
943
0
  strbuf_release(&buf);
944
0
  remove_merge_branch_state(the_repository);
945
0
  return 0;
946
0
}
947
948
static int suggest_conflicts(void)
949
0
{
950
0
  const char *filename;
951
0
  FILE *fp;
952
0
  struct strbuf msgbuf = STRBUF_INIT;
953
954
0
  filename = git_path_merge_msg(the_repository);
955
0
  fp = xfopen(filename, "a");
956
957
  /*
958
   * We can't use cleanup_mode because if we're not using the editor,
959
   * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
960
   * though the message is meant to be processed later by git-commit.
961
   * Thus, we will get the cleanup mode which is returned when we _are_
962
   * using an editor.
963
   */
964
0
  append_conflicts_hint(&the_index, &msgbuf,
965
0
            get_cleanup_mode(cleanup_arg, 1));
966
0
  fputs(msgbuf.buf, fp);
967
0
  strbuf_release(&msgbuf);
968
0
  fclose(fp);
969
0
  repo_rerere(the_repository, allow_rerere_auto);
970
0
  printf(_("Automatic merge failed; "
971
0
      "fix conflicts and then commit the result.\n"));
972
0
  return 1;
973
0
}
974
975
static int evaluate_result(void)
976
0
{
977
0
  int cnt = 0;
978
0
  struct rev_info rev;
979
980
  /* Check how many files differ. */
981
0
  repo_init_revisions(the_repository, &rev, "");
982
0
  setup_revisions(0, NULL, &rev, NULL);
983
0
  rev.diffopt.output_format |=
984
0
    DIFF_FORMAT_CALLBACK;
985
0
  rev.diffopt.format_callback = count_diff_files;
986
0
  rev.diffopt.format_callback_data = &cnt;
987
0
  run_diff_files(&rev, 0);
988
989
  /*
990
   * Check how many unmerged entries are
991
   * there.
992
   */
993
0
  cnt += count_unmerged_entries();
994
995
0
  release_revisions(&rev);
996
0
  return cnt;
997
0
}
998
999
/*
1000
 * Pretend as if the user told us to merge with the remote-tracking
1001
 * branch we have for the upstream of the current branch
1002
 */
1003
static int setup_with_upstream(const char ***argv)
1004
0
{
1005
0
  struct branch *branch = branch_get(NULL);
1006
0
  int i;
1007
0
  const char **args;
1008
1009
0
  if (!branch)
1010
0
    die(_("No current branch."));
1011
0
  if (!branch->remote_name)
1012
0
    die(_("No remote for the current branch."));
1013
0
  if (!branch->merge_nr)
1014
0
    die(_("No default upstream defined for the current branch."));
1015
1016
0
  args = xcalloc(st_add(branch->merge_nr, 1), sizeof(char *));
1017
0
  for (i = 0; i < branch->merge_nr; i++) {
1018
0
    if (!branch->merge[i]->dst)
1019
0
      die(_("No remote-tracking branch for %s from %s"),
1020
0
          branch->merge[i]->src, branch->remote_name);
1021
0
    args[i] = branch->merge[i]->dst;
1022
0
  }
1023
0
  args[i] = NULL;
1024
0
  *argv = args;
1025
0
  return i;
1026
0
}
1027
1028
static void write_merge_heads(struct commit_list *remoteheads)
1029
0
{
1030
0
  struct commit_list *j;
1031
0
  struct strbuf buf = STRBUF_INIT;
1032
1033
0
  for (j = remoteheads; j; j = j->next) {
1034
0
    struct object_id *oid;
1035
0
    struct commit *c = j->item;
1036
0
    struct merge_remote_desc *desc;
1037
1038
0
    desc = merge_remote_util(c);
1039
0
    if (desc && desc->obj) {
1040
0
      oid = &desc->obj->oid;
1041
0
    } else {
1042
0
      oid = &c->object.oid;
1043
0
    }
1044
0
    strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
1045
0
  }
1046
0
  write_file_buf(git_path_merge_head(the_repository), buf.buf, buf.len);
1047
1048
0
  strbuf_reset(&buf);
1049
0
  if (fast_forward == FF_NO)
1050
0
    strbuf_addstr(&buf, "no-ff");
1051
0
  write_file_buf(git_path_merge_mode(the_repository), buf.buf, buf.len);
1052
0
  strbuf_release(&buf);
1053
0
}
1054
1055
static void write_merge_state(struct commit_list *remoteheads)
1056
0
{
1057
0
  write_merge_heads(remoteheads);
1058
0
  strbuf_addch(&merge_msg, '\n');
1059
0
  write_file_buf(git_path_merge_msg(the_repository), merge_msg.buf,
1060
0
           merge_msg.len);
1061
0
}
1062
1063
static int default_edit_option(void)
1064
0
{
1065
0
  static const char name[] = "GIT_MERGE_AUTOEDIT";
1066
0
  const char *e = getenv(name);
1067
0
  struct stat st_stdin, st_stdout;
1068
1069
0
  if (have_message)
1070
    /* an explicit -m msg without --[no-]edit */
1071
0
    return 0;
1072
1073
0
  if (e) {
1074
0
    int v = git_parse_maybe_bool(e);
1075
0
    if (v < 0)
1076
0
      die(_("Bad value '%s' in environment '%s'"), e, name);
1077
0
    return v;
1078
0
  }
1079
1080
  /* Use editor if stdin and stdout are the same and is a tty */
1081
0
  return (!fstat(0, &st_stdin) &&
1082
0
    !fstat(1, &st_stdout) &&
1083
0
    isatty(0) && isatty(1) &&
1084
0
    st_stdin.st_dev == st_stdout.st_dev &&
1085
0
    st_stdin.st_ino == st_stdout.st_ino &&
1086
0
    st_stdin.st_mode == st_stdout.st_mode);
1087
0
}
1088
1089
static struct commit_list *reduce_parents(struct commit *head_commit,
1090
            int *head_subsumed,
1091
            struct commit_list *remoteheads)
1092
0
{
1093
0
  struct commit_list *parents, **remotes;
1094
1095
  /*
1096
   * Is the current HEAD reachable from another commit being
1097
   * merged?  If so we do not want to record it as a parent of
1098
   * the resulting merge, unless --no-ff is given.  We will flip
1099
   * this variable to 0 when we find HEAD among the independent
1100
   * tips being merged.
1101
   */
1102
0
  *head_subsumed = 1;
1103
1104
  /* Find what parents to record by checking independent ones. */
1105
0
  parents = reduce_heads(remoteheads);
1106
0
  free_commit_list(remoteheads);
1107
1108
0
  remoteheads = NULL;
1109
0
  remotes = &remoteheads;
1110
0
  while (parents) {
1111
0
    struct commit *commit = pop_commit(&parents);
1112
0
    if (commit == head_commit)
1113
0
      *head_subsumed = 0;
1114
0
    else
1115
0
      remotes = &commit_list_insert(commit, remotes)->next;
1116
0
  }
1117
0
  return remoteheads;
1118
0
}
1119
1120
static void prepare_merge_message(struct strbuf *merge_names, struct strbuf *merge_msg)
1121
0
{
1122
0
  struct fmt_merge_msg_opts opts;
1123
1124
0
  memset(&opts, 0, sizeof(opts));
1125
0
  opts.add_title = !have_message;
1126
0
  opts.shortlog_len = shortlog_len;
1127
0
  opts.credit_people = (0 < option_edit);
1128
0
  opts.into_name = into_name;
1129
1130
0
  fmt_merge_msg(merge_names, merge_msg, &opts);
1131
0
  if (merge_msg->len)
1132
0
    strbuf_setlen(merge_msg, merge_msg->len - 1);
1133
0
}
1134
1135
static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge_names)
1136
0
{
1137
0
  const char *filename;
1138
0
  int fd, pos, npos;
1139
0
  struct strbuf fetch_head_file = STRBUF_INIT;
1140
0
  const unsigned hexsz = the_hash_algo->hexsz;
1141
1142
0
  if (!merge_names)
1143
0
    merge_names = &fetch_head_file;
1144
1145
0
  filename = git_path_fetch_head(the_repository);
1146
0
  fd = xopen(filename, O_RDONLY);
1147
1148
0
  if (strbuf_read(merge_names, fd, 0) < 0)
1149
0
    die_errno(_("could not read '%s'"), filename);
1150
0
  if (close(fd) < 0)
1151
0
    die_errno(_("could not close '%s'"), filename);
1152
1153
0
  for (pos = 0; pos < merge_names->len; pos = npos) {
1154
0
    struct object_id oid;
1155
0
    char *ptr;
1156
0
    struct commit *commit;
1157
1158
0
    ptr = strchr(merge_names->buf + pos, '\n');
1159
0
    if (ptr)
1160
0
      npos = ptr - merge_names->buf + 1;
1161
0
    else
1162
0
      npos = merge_names->len;
1163
1164
0
    if (npos - pos < hexsz + 2 ||
1165
0
        get_oid_hex(merge_names->buf + pos, &oid))
1166
0
      commit = NULL; /* bad */
1167
0
    else if (memcmp(merge_names->buf + pos + hexsz, "\t\t", 2))
1168
0
      continue; /* not-for-merge */
1169
0
    else {
1170
0
      char saved = merge_names->buf[pos + hexsz];
1171
0
      merge_names->buf[pos + hexsz] = '\0';
1172
0
      commit = get_merge_parent(merge_names->buf + pos);
1173
0
      merge_names->buf[pos + hexsz] = saved;
1174
0
    }
1175
0
    if (!commit) {
1176
0
      if (ptr)
1177
0
        *ptr = '\0';
1178
0
      die(_("not something we can merge in %s: %s"),
1179
0
          filename, merge_names->buf + pos);
1180
0
    }
1181
0
    remotes = &commit_list_insert(commit, remotes)->next;
1182
0
  }
1183
1184
0
  if (merge_names == &fetch_head_file)
1185
0
    strbuf_release(&fetch_head_file);
1186
0
}
1187
1188
static struct commit_list *collect_parents(struct commit *head_commit,
1189
             int *head_subsumed,
1190
             int argc, const char **argv,
1191
             struct strbuf *merge_msg)
1192
0
{
1193
0
  int i;
1194
0
  struct commit_list *remoteheads = NULL;
1195
0
  struct commit_list **remotes = &remoteheads;
1196
0
  struct strbuf merge_names = STRBUF_INIT, *autogen = NULL;
1197
1198
0
  if (merge_msg && (!have_message || shortlog_len))
1199
0
    autogen = &merge_names;
1200
1201
0
  if (head_commit)
1202
0
    remotes = &commit_list_insert(head_commit, remotes)->next;
1203
1204
0
  if (argc == 1 && !strcmp(argv[0], "FETCH_HEAD")) {
1205
0
    handle_fetch_head(remotes, autogen);
1206
0
    remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads);
1207
0
  } else {
1208
0
    for (i = 0; i < argc; i++) {
1209
0
      struct commit *commit = get_merge_parent(argv[i]);
1210
0
      if (!commit)
1211
0
        help_unknown_ref(argv[i], "merge",
1212
0
             _("not something we can merge"));
1213
0
      remotes = &commit_list_insert(commit, remotes)->next;
1214
0
    }
1215
0
    remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads);
1216
0
    if (autogen) {
1217
0
      struct commit_list *p;
1218
0
      for (p = remoteheads; p; p = p->next)
1219
0
        merge_name(merge_remote_util(p->item)->name, autogen);
1220
0
    }
1221
0
  }
1222
1223
0
  if (autogen) {
1224
0
    prepare_merge_message(autogen, merge_msg);
1225
0
    strbuf_release(autogen);
1226
0
  }
1227
1228
0
  return remoteheads;
1229
0
}
1230
1231
static int merging_a_throwaway_tag(struct commit *commit)
1232
0
{
1233
0
  char *tag_ref;
1234
0
  struct object_id oid;
1235
0
  int is_throwaway_tag = 0;
1236
1237
  /* Are we merging a tag? */
1238
0
  if (!merge_remote_util(commit) ||
1239
0
      !merge_remote_util(commit)->obj ||
1240
0
      merge_remote_util(commit)->obj->type != OBJ_TAG)
1241
0
    return is_throwaway_tag;
1242
1243
  /*
1244
   * Now we know we are merging a tag object.  Are we downstream
1245
   * and following the tags from upstream?  If so, we must have
1246
   * the tag object pointed at by "refs/tags/$T" where $T is the
1247
   * tagname recorded in the tag object.  We want to allow such
1248
   * a "just to catch up" merge to fast-forward.
1249
   *
1250
   * Otherwise, we are playing an integrator's role, making a
1251
   * merge with a throw-away tag from a contributor with
1252
   * something like "git pull $contributor $signed_tag".
1253
   * We want to forbid such a merge from fast-forwarding
1254
   * by default; otherwise we would not keep the signature
1255
   * anywhere.
1256
   */
1257
0
  tag_ref = xstrfmt("refs/tags/%s",
1258
0
        ((struct tag *)merge_remote_util(commit)->obj)->tag);
1259
0
  if (!read_ref(tag_ref, &oid) &&
1260
0
      oideq(&oid, &merge_remote_util(commit)->obj->oid))
1261
0
    is_throwaway_tag = 0;
1262
0
  else
1263
0
    is_throwaway_tag = 1;
1264
0
  free(tag_ref);
1265
0
  return is_throwaway_tag;
1266
0
}
1267
1268
int cmd_merge(int argc, const char **argv, const char *prefix)
1269
0
{
1270
0
  struct object_id result_tree, stash, head_oid;
1271
0
  struct commit *head_commit;
1272
0
  struct strbuf buf = STRBUF_INIT;
1273
0
  int i, ret = 0, head_subsumed;
1274
0
  int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
1275
0
  struct commit_list *common = NULL;
1276
0
  const char *best_strategy = NULL, *wt_strategy = NULL;
1277
0
  struct commit_list *remoteheads = NULL, *p;
1278
0
  void *branch_to_free;
1279
0
  int orig_argc = argc;
1280
1281
0
  if (argc == 2 && !strcmp(argv[1], "-h"))
1282
0
    usage_with_options(builtin_merge_usage, builtin_merge_options);
1283
1284
0
  prepare_repo_settings(the_repository);
1285
0
  the_repository->settings.command_requires_full_index = 0;
1286
1287
  /*
1288
   * Check if we are _not_ on a detached HEAD, i.e. if there is a
1289
   * current branch.
1290
   */
1291
0
  branch = branch_to_free = resolve_refdup("HEAD", 0, &head_oid, NULL);
1292
0
  if (branch)
1293
0
    skip_prefix(branch, "refs/heads/", &branch);
1294
1295
0
  if (!pull_twohead) {
1296
0
    char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
1297
0
    if (default_strategy && !strcmp(default_strategy, "ort"))
1298
0
      pull_twohead = "ort";
1299
0
  }
1300
1301
0
  init_diff_ui_defaults();
1302
0
  git_config(git_merge_config, NULL);
1303
1304
0
  if (!branch || is_null_oid(&head_oid))
1305
0
    head_commit = NULL;
1306
0
  else
1307
0
    head_commit = lookup_commit_or_die(&head_oid, "HEAD");
1308
1309
0
  if (branch_mergeoptions)
1310
0
    parse_branch_merge_options(branch_mergeoptions);
1311
0
  argc = parse_options(argc, argv, prefix, builtin_merge_options,
1312
0
      builtin_merge_usage, 0);
1313
0
  if (shortlog_len < 0)
1314
0
    shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
1315
1316
0
  if (verbosity < 0 && show_progress == -1)
1317
0
    show_progress = 0;
1318
1319
0
  if (abort_current_merge) {
1320
0
    int nargc = 2;
1321
0
    const char *nargv[] = {"reset", "--merge", NULL};
1322
0
    struct strbuf stash_oid = STRBUF_INIT;
1323
1324
0
    if (orig_argc != 2)
1325
0
      usage_msg_opt(_("--abort expects no arguments"),
1326
0
            builtin_merge_usage, builtin_merge_options);
1327
1328
0
    if (!file_exists(git_path_merge_head(the_repository)))
1329
0
      die(_("There is no merge to abort (MERGE_HEAD missing)."));
1330
1331
0
    if (read_oneliner(&stash_oid, git_path_merge_autostash(the_repository),
1332
0
        READ_ONELINER_SKIP_IF_EMPTY))
1333
0
      unlink(git_path_merge_autostash(the_repository));
1334
1335
    /* Invoke 'git reset --merge' */
1336
0
    ret = cmd_reset(nargc, nargv, prefix);
1337
1338
0
    if (stash_oid.len)
1339
0
      apply_autostash_oid(stash_oid.buf);
1340
1341
0
    strbuf_release(&stash_oid);
1342
0
    goto done;
1343
0
  }
1344
1345
0
  if (quit_current_merge) {
1346
0
    if (orig_argc != 2)
1347
0
      usage_msg_opt(_("--quit expects no arguments"),
1348
0
              builtin_merge_usage,
1349
0
              builtin_merge_options);
1350
1351
0
    remove_merge_branch_state(the_repository);
1352
0
    goto done;
1353
0
  }
1354
1355
0
  if (continue_current_merge) {
1356
0
    int nargc = 1;
1357
0
    const char *nargv[] = {"commit", NULL};
1358
1359
0
    if (orig_argc != 2)
1360
0
      usage_msg_opt(_("--continue expects no arguments"),
1361
0
            builtin_merge_usage, builtin_merge_options);
1362
1363
0
    if (!file_exists(git_path_merge_head(the_repository)))
1364
0
      die(_("There is no merge in progress (MERGE_HEAD missing)."));
1365
1366
    /* Invoke 'git commit' */
1367
0
    ret = cmd_commit(nargc, nargv, prefix);
1368
0
    goto done;
1369
0
  }
1370
1371
0
  if (repo_read_index_unmerged(the_repository))
1372
0
    die_resolve_conflict("merge");
1373
1374
0
  if (file_exists(git_path_merge_head(the_repository))) {
1375
    /*
1376
     * There is no unmerged entry, don't advise 'git
1377
     * add/rm <file>', just 'git commit'.
1378
     */
1379
0
    if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
1380
0
      die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1381
0
          "Please, commit your changes before you merge."));
1382
0
    else
1383
0
      die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1384
0
  }
1385
0
  if (ref_exists("CHERRY_PICK_HEAD")) {
1386
0
    if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
1387
0
      die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1388
0
          "Please, commit your changes before you merge."));
1389
0
    else
1390
0
      die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1391
0
  }
1392
0
  resolve_undo_clear_index(&the_index);
1393
1394
0
  if (option_edit < 0)
1395
0
    option_edit = default_edit_option();
1396
1397
0
  cleanup_mode = get_cleanup_mode(cleanup_arg, 0 < option_edit);
1398
1399
0
  if (verbosity < 0)
1400
0
    show_diffstat = 0;
1401
1402
0
  if (squash) {
1403
0
    if (fast_forward == FF_NO)
1404
0
      die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
1405
0
    if (option_commit > 0)
1406
0
      die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
1407
    /*
1408
     * squash can now silently disable option_commit - this is not
1409
     * a problem as it is only overriding the default, not a user
1410
     * supplied option.
1411
     */
1412
0
    option_commit = 0;
1413
0
  }
1414
1415
0
  if (option_commit < 0)
1416
0
    option_commit = 1;
1417
1418
0
  if (!argc) {
1419
0
    if (default_to_upstream)
1420
0
      argc = setup_with_upstream(&argv);
1421
0
    else
1422
0
      die(_("No commit specified and merge.defaultToUpstream not set."));
1423
0
  } else if (argc == 1 && !strcmp(argv[0], "-")) {
1424
0
    argv[0] = "@{-1}";
1425
0
  }
1426
1427
0
  if (!argc)
1428
0
    usage_with_options(builtin_merge_usage,
1429
0
      builtin_merge_options);
1430
1431
0
  if (!head_commit) {
1432
    /*
1433
     * If the merged head is a valid one there is no reason
1434
     * to forbid "git merge" into a branch yet to be born.
1435
     * We do the same for "git pull".
1436
     */
1437
0
    struct object_id *remote_head_oid;
1438
0
    if (squash)
1439
0
      die(_("Squash commit into empty head not supported yet"));
1440
0
    if (fast_forward == FF_NO)
1441
0
      die(_("Non-fast-forward commit does not make sense into "
1442
0
          "an empty head"));
1443
0
    remoteheads = collect_parents(head_commit, &head_subsumed,
1444
0
                argc, argv, NULL);
1445
0
    if (!remoteheads)
1446
0
      die(_("%s - not something we can merge"), argv[0]);
1447
0
    if (remoteheads->next)
1448
0
      die(_("Can merge only exactly one commit into empty head"));
1449
1450
0
    if (verify_signatures)
1451
0
      verify_merge_signature(remoteheads->item, verbosity,
1452
0
                 check_trust_level);
1453
1454
0
    remote_head_oid = &remoteheads->item->object.oid;
1455
0
    read_empty(remote_head_oid);
1456
0
    update_ref("initial pull", "HEAD", remote_head_oid, NULL, 0,
1457
0
         UPDATE_REFS_DIE_ON_ERR);
1458
0
    goto done;
1459
0
  }
1460
1461
  /*
1462
   * All the rest are the commits being merged; prepare
1463
   * the standard merge summary message to be appended
1464
   * to the given message.
1465
   */
1466
0
  remoteheads = collect_parents(head_commit, &head_subsumed,
1467
0
              argc, argv, &merge_msg);
1468
1469
0
  if (!head_commit || !argc)
1470
0
    usage_with_options(builtin_merge_usage,
1471
0
      builtin_merge_options);
1472
1473
0
  if (verify_signatures) {
1474
0
    for (p = remoteheads; p; p = p->next) {
1475
0
      verify_merge_signature(p->item, verbosity,
1476
0
                 check_trust_level);
1477
0
    }
1478
0
  }
1479
1480
0
  strbuf_addstr(&buf, "merge");
1481
0
  for (p = remoteheads; p; p = p->next)
1482
0
    strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name);
1483
0
  setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1484
0
  strbuf_reset(&buf);
1485
1486
0
  for (p = remoteheads; p; p = p->next) {
1487
0
    struct commit *commit = p->item;
1488
0
    strbuf_addf(&buf, "GITHEAD_%s",
1489
0
          oid_to_hex(&commit->object.oid));
1490
0
    setenv(buf.buf, merge_remote_util(commit)->name, 1);
1491
0
    strbuf_reset(&buf);
1492
0
    if (fast_forward != FF_ONLY && merging_a_throwaway_tag(commit))
1493
0
      fast_forward = FF_NO;
1494
0
  }
1495
1496
0
  if (!use_strategies && !pull_twohead &&
1497
0
      remoteheads && !remoteheads->next) {
1498
0
    char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
1499
0
    if (default_strategy)
1500
0
      append_strategy(get_strategy(default_strategy));
1501
0
  }
1502
0
  if (!use_strategies) {
1503
0
    if (!remoteheads)
1504
0
      ; /* already up-to-date */
1505
0
    else if (!remoteheads->next)
1506
0
      add_strategies(pull_twohead, DEFAULT_TWOHEAD);
1507
0
    else
1508
0
      add_strategies(pull_octopus, DEFAULT_OCTOPUS);
1509
0
  }
1510
1511
0
  for (i = 0; i < use_strategies_nr; i++) {
1512
0
    if (use_strategies[i]->attr & NO_FAST_FORWARD)
1513
0
      fast_forward = FF_NO;
1514
0
    if (use_strategies[i]->attr & NO_TRIVIAL)
1515
0
      allow_trivial = 0;
1516
0
  }
1517
1518
0
  if (!remoteheads)
1519
0
    ; /* already up-to-date */
1520
0
  else if (!remoteheads->next)
1521
0
    common = repo_get_merge_bases(the_repository, head_commit,
1522
0
                remoteheads->item);
1523
0
  else {
1524
0
    struct commit_list *list = remoteheads;
1525
0
    commit_list_insert(head_commit, &list);
1526
0
    common = get_octopus_merge_bases(list);
1527
0
    free(list);
1528
0
  }
1529
1530
0
  update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1531
0
       &head_commit->object.oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
1532
1533
0
  if (remoteheads && !common) {
1534
    /* No common ancestors found. */
1535
0
    if (!allow_unrelated_histories)
1536
0
      die(_("refusing to merge unrelated histories"));
1537
    /* otherwise, we need a real merge. */
1538
0
  } else if (!remoteheads ||
1539
0
     (!remoteheads->next && !common->next &&
1540
0
      common->item == remoteheads->item)) {
1541
    /*
1542
     * If head can reach all the merge then we are up to date.
1543
     * but first the most common case of merging one remote.
1544
     */
1545
0
    finish_up_to_date();
1546
0
    goto done;
1547
0
  } else if (fast_forward != FF_NO && !remoteheads->next &&
1548
0
      !common->next &&
1549
0
      oideq(&common->item->object.oid, &head_commit->object.oid)) {
1550
    /* Again the most common case of merging one remote. */
1551
0
    const char *msg = have_message ?
1552
0
      "Fast-forward (no commit created; -m option ignored)" :
1553
0
      "Fast-forward";
1554
0
    struct commit *commit;
1555
1556
0
    if (verbosity >= 0) {
1557
0
      printf(_("Updating %s..%s\n"),
1558
0
             repo_find_unique_abbrev(the_repository, &head_commit->object.oid,
1559
0
                   DEFAULT_ABBREV),
1560
0
             repo_find_unique_abbrev(the_repository, &remoteheads->item->object.oid,
1561
0
                   DEFAULT_ABBREV));
1562
0
    }
1563
0
    commit = remoteheads->item;
1564
0
    if (!commit) {
1565
0
      ret = 1;
1566
0
      goto done;
1567
0
    }
1568
1569
0
    if (autostash)
1570
0
      create_autostash(the_repository,
1571
0
           git_path_merge_autostash(the_repository));
1572
0
    if (checkout_fast_forward(the_repository,
1573
0
            &head_commit->object.oid,
1574
0
            &commit->object.oid,
1575
0
            overwrite_ignore)) {
1576
0
      apply_autostash(git_path_merge_autostash(the_repository));
1577
0
      ret = 1;
1578
0
      goto done;
1579
0
    }
1580
1581
0
    finish(head_commit, remoteheads, &commit->object.oid, msg);
1582
0
    remove_merge_branch_state(the_repository);
1583
0
    goto done;
1584
0
  } else if (!remoteheads->next && common->next)
1585
0
    ;
1586
    /*
1587
     * We are not doing octopus and not fast-forward.  Need
1588
     * a real merge.
1589
     */
1590
0
  else if (!remoteheads->next && !common->next && option_commit) {
1591
    /*
1592
     * We are not doing octopus, not fast-forward, and have
1593
     * only one common.
1594
     */
1595
0
    refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
1596
0
    if (allow_trivial && fast_forward != FF_ONLY) {
1597
      /*
1598
       * Must first ensure that index matches HEAD before
1599
       * attempting a trivial merge.
1600
       */
1601
0
      struct tree *head_tree = repo_get_commit_tree(the_repository,
1602
0
                      head_commit);
1603
0
      struct strbuf sb = STRBUF_INIT;
1604
1605
0
      if (repo_index_has_changes(the_repository, head_tree,
1606
0
               &sb)) {
1607
0
        error(_("Your local changes to the following files would be overwritten by merge:\n  %s"),
1608
0
              sb.buf);
1609
0
        strbuf_release(&sb);
1610
0
        ret = 2;
1611
0
        goto done;
1612
0
      }
1613
1614
      /* See if it is really trivial. */
1615
0
      git_committer_info(IDENT_STRICT);
1616
0
      printf(_("Trying really trivial in-index merge...\n"));
1617
0
      if (!read_tree_trivial(&common->item->object.oid,
1618
0
                 &head_commit->object.oid,
1619
0
                 &remoteheads->item->object.oid)) {
1620
0
        ret = merge_trivial(head_commit, remoteheads);
1621
0
        goto done;
1622
0
      }
1623
0
      printf(_("Nope.\n"));
1624
0
    }
1625
0
  } else {
1626
    /*
1627
     * An octopus.  If we can reach all the remote we are up
1628
     * to date.
1629
     */
1630
0
    int up_to_date = 1;
1631
0
    struct commit_list *j;
1632
1633
0
    for (j = remoteheads; j; j = j->next) {
1634
0
      struct commit_list *common_one;
1635
0
      struct commit *common_item;
1636
1637
      /*
1638
       * Here we *have* to calculate the individual
1639
       * merge_bases again, otherwise "git merge HEAD^
1640
       * HEAD^^" would be missed.
1641
       */
1642
0
      common_one = repo_get_merge_bases(the_repository,
1643
0
                head_commit,
1644
0
                j->item);
1645
0
      common_item = common_one->item;
1646
0
      free_commit_list(common_one);
1647
0
      if (!oideq(&common_item->object.oid, &j->item->object.oid)) {
1648
0
        up_to_date = 0;
1649
0
        break;
1650
0
      }
1651
0
    }
1652
0
    if (up_to_date) {
1653
0
      finish_up_to_date();
1654
0
      goto done;
1655
0
    }
1656
0
  }
1657
1658
0
  if (fast_forward == FF_ONLY)
1659
0
    die_ff_impossible();
1660
1661
0
  if (autostash)
1662
0
    create_autostash(the_repository,
1663
0
         git_path_merge_autostash(the_repository));
1664
1665
  /* We are going to make a new commit. */
1666
0
  git_committer_info(IDENT_STRICT);
1667
1668
  /*
1669
   * At this point, we need a real merge.  No matter what strategy
1670
   * we use, it would operate on the index, possibly affecting the
1671
   * working tree, and when resolved cleanly, have the desired
1672
   * tree in the index -- this means that the index must be in
1673
   * sync with the head commit.  The strategies are responsible
1674
   * to ensure this.
1675
   *
1676
   * Stash away the local changes so that we can try more than one
1677
   * and/or recover from merge strategies bailing while leaving the
1678
   * index and working tree polluted.
1679
   */
1680
0
  if (save_state(&stash))
1681
0
    oidclr(&stash);
1682
1683
0
  for (i = 0; i < use_strategies_nr; i++) {
1684
0
    int ret, cnt;
1685
0
    if (i) {
1686
0
      printf(_("Rewinding the tree to pristine...\n"));
1687
0
      restore_state(&head_commit->object.oid, &stash);
1688
0
    }
1689
0
    if (use_strategies_nr != 1)
1690
0
      printf(_("Trying merge strategy %s...\n"),
1691
0
        use_strategies[i]->name);
1692
    /*
1693
     * Remember which strategy left the state in the working
1694
     * tree.
1695
     */
1696
0
    wt_strategy = use_strategies[i]->name;
1697
1698
0
    ret = try_merge_strategy(wt_strategy,
1699
0
           common, remoteheads,
1700
0
           head_commit);
1701
    /*
1702
     * The backend exits with 1 when conflicts are
1703
     * left to be resolved, with 2 when it does not
1704
     * handle the given merge at all.
1705
     */
1706
0
    if (ret < 2) {
1707
0
      if (!ret) {
1708
        /*
1709
         * This strategy worked; no point in trying
1710
         * another.
1711
         */
1712
0
        merge_was_ok = 1;
1713
0
        best_strategy = wt_strategy;
1714
0
        break;
1715
0
      }
1716
0
      cnt = (use_strategies_nr > 1) ? evaluate_result() : 0;
1717
0
      if (best_cnt <= 0 || cnt <= best_cnt) {
1718
0
        best_strategy = wt_strategy;
1719
0
        best_cnt = cnt;
1720
0
      }
1721
0
    }
1722
0
  }
1723
1724
  /*
1725
   * If we have a resulting tree, that means the strategy module
1726
   * auto resolved the merge cleanly.
1727
   */
1728
0
  if (merge_was_ok && option_commit) {
1729
0
    automerge_was_ok = 1;
1730
0
    ret = finish_automerge(head_commit, head_subsumed,
1731
0
               common, remoteheads,
1732
0
               &result_tree, wt_strategy);
1733
0
    goto done;
1734
0
  }
1735
1736
  /*
1737
   * Pick the result from the best strategy and have the user fix
1738
   * it up.
1739
   */
1740
0
  if (!best_strategy) {
1741
0
    restore_state(&head_commit->object.oid, &stash);
1742
0
    if (use_strategies_nr > 1)
1743
0
      fprintf(stderr,
1744
0
        _("No merge strategy handled the merge.\n"));
1745
0
    else
1746
0
      fprintf(stderr, _("Merge with strategy %s failed.\n"),
1747
0
        use_strategies[0]->name);
1748
0
    apply_autostash(git_path_merge_autostash(the_repository));
1749
0
    ret = 2;
1750
0
    goto done;
1751
0
  } else if (best_strategy == wt_strategy)
1752
0
    ; /* We already have its result in the working tree. */
1753
0
  else {
1754
0
    printf(_("Rewinding the tree to pristine...\n"));
1755
0
    restore_state(&head_commit->object.oid, &stash);
1756
0
    printf(_("Using the %s strategy to prepare resolving by hand.\n"),
1757
0
      best_strategy);
1758
0
    try_merge_strategy(best_strategy, common, remoteheads,
1759
0
           head_commit);
1760
0
  }
1761
1762
0
  if (squash) {
1763
0
    finish(head_commit, remoteheads, NULL, NULL);
1764
1765
0
    git_test_write_commit_graph_or_die();
1766
0
  } else
1767
0
    write_merge_state(remoteheads);
1768
1769
0
  if (merge_was_ok)
1770
0
    fprintf(stderr, _("Automatic merge went well; "
1771
0
      "stopped before committing as requested\n"));
1772
0
  else
1773
0
    ret = suggest_conflicts();
1774
0
  if (autostash)
1775
0
    printf(_("When finished, apply stashed changes with `git stash pop`\n"));
1776
1777
0
done:
1778
0
  if (!automerge_was_ok) {
1779
0
    free_commit_list(common);
1780
0
    free_commit_list(remoteheads);
1781
0
  }
1782
0
  strbuf_release(&buf);
1783
0
  free(branch_to_free);
1784
0
  discard_index(&the_index);
1785
0
  return ret;
1786
0
}