Merge branch 'jk/reduce-gc-aggressive-depth' into maint
[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         hold_locked_index(lock_file, 1);
61
62         memset(&trees, 0, sizeof(trees));
63         memset(&opts, 0, sizeof(opts));
64         memset(&t, 0, sizeof(t));
65         if (overwrite_ignore) {
66                 memset(&dir, 0, sizeof(dir));
67                 dir.flags |= DIR_SHOW_IGNORED;
68                 setup_standard_excludes(&dir);
69                 opts.dir = &dir;
70         }
71
72         opts.head_idx = 1;
73         opts.src_index = &the_index;
74         opts.dst_index = &the_index;
75         opts.update = 1;
76         opts.verbose_update = 1;
77         opts.merge = 1;
78         opts.fn = twoway_merge;
79         setup_unpack_trees_porcelain(&opts, "merge");
80
81         trees[nr_trees] = parse_tree_indirect(head);
82         if (!trees[nr_trees++])
83                 return -1;
84         trees[nr_trees] = parse_tree_indirect(remote);
85         if (!trees[nr_trees++])
86                 return -1;
87         for (i = 0; i < nr_trees; i++) {
88                 parse_tree(trees[i]);
89                 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
90         }
91         if (unpack_trees(nr_trees, t, &opts))
92                 return -1;
93         if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
94                 die(_("unable to write new index file"));
95         return 0;
96 }