repository: create read_replace_refs setting
[alt-git.git] / merge.c
blobda7fa652c271b7990f5f6d1d82abb63dc8341261
1 #include "cache.h"
2 #include "diff.h"
3 #include "diffcore.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "lockfile.h"
7 #include "commit.h"
8 #include "run-command.h"
9 #include "resolve-undo.h"
10 #include "tree-walk.h"
11 #include "unpack-trees.h"
12 #include "dir.h"
14 static const char *merge_argument(struct commit *commit)
16 return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
19 int try_merge_command(struct repository *r,
20 const char *strategy, size_t xopts_nr,
21 const char **xopts, struct commit_list *common,
22 const char *head_arg, struct commit_list *remotes)
24 struct child_process cmd = CHILD_PROCESS_INIT;
25 int i, ret;
26 struct commit_list *j;
28 strvec_pushf(&cmd.args, "merge-%s", strategy);
29 for (i = 0; i < xopts_nr; i++)
30 strvec_pushf(&cmd.args, "--%s", xopts[i]);
31 for (j = common; j; j = j->next)
32 strvec_push(&cmd.args, merge_argument(j->item));
33 strvec_push(&cmd.args, "--");
34 strvec_push(&cmd.args, head_arg);
35 for (j = remotes; j; j = j->next)
36 strvec_push(&cmd.args, merge_argument(j->item));
38 cmd.git_cmd = 1;
39 ret = run_command(&cmd);
41 discard_index(r->index);
42 if (repo_read_index(r) < 0)
43 die(_("failed to read the cache"));
44 resolve_undo_clear_index(r->index);
46 return ret;
49 int checkout_fast_forward(struct repository *r,
50 const struct object_id *head,
51 const struct object_id *remote,
52 int overwrite_ignore)
54 struct tree *trees[MAX_UNPACK_TREES];
55 struct unpack_trees_options opts;
56 struct tree_desc t[MAX_UNPACK_TREES];
57 int i, nr_trees = 0;
58 struct lock_file lock_file = LOCK_INIT;
60 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
62 if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
63 return -1;
65 memset(&trees, 0, sizeof(trees));
66 memset(&t, 0, sizeof(t));
68 trees[nr_trees] = parse_tree_indirect(head);
69 if (!trees[nr_trees++]) {
70 rollback_lock_file(&lock_file);
71 return -1;
73 trees[nr_trees] = parse_tree_indirect(remote);
74 if (!trees[nr_trees++]) {
75 rollback_lock_file(&lock_file);
76 return -1;
78 for (i = 0; i < nr_trees; i++) {
79 parse_tree(trees[i]);
80 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
83 memset(&opts, 0, sizeof(opts));
84 opts.preserve_ignored = !overwrite_ignore;
86 opts.head_idx = 1;
87 opts.src_index = r->index;
88 opts.dst_index = r->index;
89 opts.update = 1;
90 opts.verbose_update = 1;
91 opts.merge = 1;
92 opts.fn = twoway_merge;
93 init_checkout_metadata(&opts.meta, NULL, remote, NULL);
94 setup_unpack_trees_porcelain(&opts, "merge");
96 if (unpack_trees(nr_trees, t, &opts)) {
97 rollback_lock_file(&lock_file);
98 clear_unpack_trees_porcelain(&opts);
99 return -1;
101 clear_unpack_trees_porcelain(&opts);
103 if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
104 return error(_("unable to write new index file"));
105 return 0;