Merge branch 'ds/ahead-behind'
[git.git] / merge.c
blob2c8b84568421b193f5032f3a9b095287666ddc46
1 #include "cache.h"
2 #include "diff.h"
3 #include "diffcore.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "commit.h"
7 #include "run-command.h"
8 #include "resolve-undo.h"
9 #include "tree-walk.h"
10 #include "unpack-trees.h"
11 #include "dir.h"
13 static const char *merge_argument(struct commit *commit)
15 return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
18 int try_merge_command(struct repository *r,
19 const char *strategy, size_t xopts_nr,
20 const char **xopts, struct commit_list *common,
21 const char *head_arg, struct commit_list *remotes)
23 struct child_process cmd = CHILD_PROCESS_INIT;
24 int i, ret;
25 struct commit_list *j;
27 strvec_pushf(&cmd.args, "merge-%s", strategy);
28 for (i = 0; i < xopts_nr; i++)
29 strvec_pushf(&cmd.args, "--%s", xopts[i]);
30 for (j = common; j; j = j->next)
31 strvec_push(&cmd.args, merge_argument(j->item));
32 strvec_push(&cmd.args, "--");
33 strvec_push(&cmd.args, head_arg);
34 for (j = remotes; j; j = j->next)
35 strvec_push(&cmd.args, merge_argument(j->item));
37 cmd.git_cmd = 1;
38 ret = run_command(&cmd);
40 discard_index(r->index);
41 if (repo_read_index(r) < 0)
42 die(_("failed to read the cache"));
43 resolve_undo_clear_index(r->index);
45 return ret;
48 int checkout_fast_forward(struct repository *r,
49 const struct object_id *head,
50 const struct object_id *remote,
51 int overwrite_ignore)
53 struct tree *trees[MAX_UNPACK_TREES];
54 struct unpack_trees_options opts;
55 struct tree_desc t[MAX_UNPACK_TREES];
56 int i, nr_trees = 0;
57 struct lock_file lock_file = LOCK_INIT;
59 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
61 if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
62 return -1;
64 memset(&trees, 0, sizeof(trees));
65 memset(&t, 0, sizeof(t));
67 trees[nr_trees] = parse_tree_indirect(head);
68 if (!trees[nr_trees++]) {
69 rollback_lock_file(&lock_file);
70 return -1;
72 trees[nr_trees] = parse_tree_indirect(remote);
73 if (!trees[nr_trees++]) {
74 rollback_lock_file(&lock_file);
75 return -1;
77 for (i = 0; i < nr_trees; i++) {
78 parse_tree(trees[i]);
79 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
82 memset(&opts, 0, sizeof(opts));
83 opts.preserve_ignored = !overwrite_ignore;
85 opts.head_idx = 1;
86 opts.src_index = r->index;
87 opts.dst_index = r->index;
88 opts.update = 1;
89 opts.verbose_update = 1;
90 opts.merge = 1;
91 opts.fn = twoway_merge;
92 init_checkout_metadata(&opts.meta, NULL, remote, NULL);
93 setup_unpack_trees_porcelain(&opts, "merge");
95 if (unpack_trees(nr_trees, t, &opts)) {
96 rollback_lock_file(&lock_file);
97 clear_unpack_trees_porcelain(&opts);
98 return -1;
100 clear_unpack_trees_porcelain(&opts);
102 if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
103 return error(_("unable to write new index file"));
104 return 0;