Merge branch 'nd/init-core-worktree-in-multi-worktree-world'
[git] / merge.c
1 #include "cache.h"
2 #include "lockfile.h"
3 #include "commit.h"
4 #include "run-command.h"
5 #include "resolve-undo.h"
6 #include "tree-walk.h"
7 #include "unpack-trees.h"
8 #include "dir.h"
9
10 static const char *merge_argument(struct commit *commit)
11 {
12         if (commit)
13                 return oid_to_hex(&commit->object.oid);
14         else
15                 return EMPTY_TREE_SHA1_HEX;
16 }
17
18 int try_merge_command(const char *strategy, size_t xopts_nr,
19                       const char **xopts, struct commit_list *common,
20                       const char *head_arg, struct commit_list *remotes)
21 {
22         struct argv_array args = ARGV_ARRAY_INIT;
23         int i, ret;
24         struct commit_list *j;
25
26         argv_array_pushf(&args, "merge-%s", strategy);
27         for (i = 0; i < xopts_nr; i++)
28                 argv_array_pushf(&args, "--%s", xopts[i]);
29         for (j = common; j; j = j->next)
30                 argv_array_push(&args, merge_argument(j->item));
31         argv_array_push(&args, "--");
32         argv_array_push(&args, head_arg);
33         for (j = remotes; j; j = j->next)
34                 argv_array_push(&args, merge_argument(j->item));
35
36         ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
37         argv_array_clear(&args);
38
39         discard_cache();
40         if (read_cache() < 0)
41                 die(_("failed to read the cache"));
42         resolve_undo_clear();
43
44         return ret;
45 }
46
47 int checkout_fast_forward(const unsigned char *head,
48                           const unsigned char *remote,
49                           int overwrite_ignore)
50 {
51         struct tree *trees[MAX_UNPACK_TREES];
52         struct unpack_trees_options opts;
53         struct tree_desc t[MAX_UNPACK_TREES];
54         int i, nr_trees = 0;
55         struct dir_struct dir;
56         struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
57
58         refresh_cache(REFRESH_QUIET);
59
60         if (hold_locked_index(lock_file, 0) < 0)
61                 return -1;
62
63         memset(&trees, 0, sizeof(trees));
64         memset(&opts, 0, sizeof(opts));
65         memset(&t, 0, sizeof(t));
66         if (overwrite_ignore) {
67                 memset(&dir, 0, sizeof(dir));
68                 dir.flags |= DIR_SHOW_IGNORED;
69                 setup_standard_excludes(&dir);
70                 opts.dir = &dir;
71         }
72
73         opts.head_idx = 1;
74         opts.src_index = &the_index;
75         opts.dst_index = &the_index;
76         opts.update = 1;
77         opts.verbose_update = 1;
78         opts.merge = 1;
79         opts.fn = twoway_merge;
80         setup_unpack_trees_porcelain(&opts, "merge");
81
82         trees[nr_trees] = parse_tree_indirect(head);
83         if (!trees[nr_trees++])
84                 return -1;
85         trees[nr_trees] = parse_tree_indirect(remote);
86         if (!trees[nr_trees++])
87                 return -1;
88         for (i = 0; i < nr_trees; i++) {
89                 parse_tree(trees[i]);
90                 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
91         }
92         if (unpack_trees(nr_trees, t, &opts))
93                 return -1;
94         if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) {
95                 rollback_lock_file(lock_file);
96                 return error(_("unable to write new index file"));
97         }
98         return 0;
99 }