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