refs: support symrefs in 'reference-transaction' hook
[git.git] / merge.c
blob752a937fa93dd3bf8703400c6311afe7a5265e02
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "hash.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "merge.h"
7 #include "commit.h"
8 #include "repository.h"
9 #include "run-command.h"
10 #include "resolve-undo.h"
11 #include "tree.h"
12 #include "tree-walk.h"
13 #include "unpack-trees.h"
15 static const char *merge_argument(struct commit *commit)
17 return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
20 int try_merge_command(struct repository *r,
21 const char *strategy, size_t xopts_nr,
22 const char **xopts, struct commit_list *common,
23 const char *head_arg, struct commit_list *remotes)
25 struct child_process cmd = CHILD_PROCESS_INIT;
26 int i, ret;
27 struct commit_list *j;
29 strvec_pushf(&cmd.args, "merge-%s", strategy);
30 for (i = 0; i < xopts_nr; i++)
31 strvec_pushf(&cmd.args, "--%s", xopts[i]);
32 for (j = common; j; j = j->next)
33 strvec_push(&cmd.args, merge_argument(j->item));
34 strvec_push(&cmd.args, "--");
35 strvec_push(&cmd.args, head_arg);
36 for (j = remotes; j; j = j->next)
37 strvec_push(&cmd.args, merge_argument(j->item));
39 cmd.git_cmd = 1;
40 ret = run_command(&cmd);
42 discard_index(r->index);
43 if (repo_read_index(r) < 0)
44 die(_("failed to read the cache"));
45 resolve_undo_clear_index(r->index);
47 return ret;
50 int checkout_fast_forward(struct repository *r,
51 const struct object_id *head,
52 const struct object_id *remote,
53 int overwrite_ignore)
55 struct tree *trees[MAX_UNPACK_TREES];
56 struct unpack_trees_options opts;
57 struct tree_desc t[MAX_UNPACK_TREES];
58 int i, nr_trees = 0;
59 struct lock_file lock_file = LOCK_INIT;
61 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
63 if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
64 return -1;
66 memset(&trees, 0, sizeof(trees));
67 memset(&t, 0, sizeof(t));
69 trees[nr_trees] = parse_tree_indirect(head);
70 if (!trees[nr_trees++]) {
71 rollback_lock_file(&lock_file);
72 return -1;
74 trees[nr_trees] = parse_tree_indirect(remote);
75 if (!trees[nr_trees++]) {
76 rollback_lock_file(&lock_file);
77 return -1;
79 for (i = 0; i < nr_trees; i++) {
80 if (parse_tree(trees[i]) < 0) {
81 rollback_lock_file(&lock_file);
82 return -1;
84 init_tree_desc(t+i, &trees[i]->object.oid,
85 trees[i]->buffer, trees[i]->size);
88 memset(&opts, 0, sizeof(opts));
89 opts.preserve_ignored = !overwrite_ignore;
91 opts.head_idx = 1;
92 opts.src_index = r->index;
93 opts.dst_index = r->index;
94 opts.update = 1;
95 opts.verbose_update = 1;
96 opts.merge = 1;
97 opts.fn = twoway_merge;
98 init_checkout_metadata(&opts.meta, NULL, remote, NULL);
99 setup_unpack_trees_porcelain(&opts, "merge");
101 if (unpack_trees(nr_trees, t, &opts)) {
102 rollback_lock_file(&lock_file);
103 clear_unpack_trees_porcelain(&opts);
104 return -1;
106 clear_unpack_trees_porcelain(&opts);
108 if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
109 return error(_("unable to write new index file"));
110 return 0;