merge-blobs.c: remove implicit dependency on the_index
[git/git.git] / merge.c
CommitLineData
db699a8a 1#include "cache.h"
b101793c
EN
2#include "diff.h"
3#include "diffcore.h"
697cc8ef 4#include "lockfile.h"
db699a8a
NTND
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
12static const char *merge_argument(struct commit *commit)
13{
e9fe6f26 14 return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
db699a8a
NTND
15}
16
17int 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{
5c1753b1
JK
21 struct argv_array args = ARGV_ARRAY_INIT;
22 int i, ret;
db699a8a 23 struct commit_list *j;
db699a8a 24
5c1753b1
JK
25 argv_array_pushf(&args, "merge-%s", strategy);
26 for (i = 0; i < xopts_nr; i++)
27 argv_array_pushf(&args, "--%s", xopts[i]);
db699a8a 28 for (j = common; j; j = j->next)
5c1753b1
JK
29 argv_array_push(&args, merge_argument(j->item));
30 argv_array_push(&args, "--");
31 argv_array_push(&args, head_arg);
db699a8a 32 for (j = remotes; j; j = j->next)
5c1753b1
JK
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
db699a8a
NTND
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
f06e90da 46int checkout_fast_forward(const struct object_id *head,
47 const struct object_id *remote,
db699a8a
NTND
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];
03b86647 53 int i, nr_trees = 0;
db699a8a 54 struct dir_struct dir;
837e34eb 55 struct lock_file lock_file = LOCK_INIT;
db699a8a
NTND
56
57 refresh_cache(REFRESH_QUIET);
58
837e34eb 59 if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0)
55f5704d 60 return -1;
db699a8a
NTND
61
62 memset(&trees, 0, sizeof(trees));
db699a8a 63 memset(&t, 0, sizeof(t));
89e653da
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));
db699a8a
NTND
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
5790d258
97 if (unpack_trees(nr_trees, t, &opts)) {
98 rollback_lock_file(&lock_file);
1c41d280 99 clear_unpack_trees_porcelain(&opts);
db699a8a 100 return -1;
5790d258 101 }
1c41d280
102 clear_unpack_trees_porcelain(&opts);
103
df60cf57 104 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
55f5704d 105 return error(_("unable to write new index file"));
db699a8a
NTND
106 return 0;
107}