refs.h: HEAD is not that special
[alt-git.git] / merge.c
blobb60925459c292bbb4a9daae46534edfbb3eca756
1 #include "git-compat-util.h"
2 #include "diff.h"
3 #include "diffcore.h"
4 #include "gettext.h"
5 #include "hash.h"
6 #include "hex.h"
7 #include "lockfile.h"
8 #include "merge.h"
9 #include "commit.h"
10 #include "repository.h"
11 #include "run-command.h"
12 #include "resolve-undo.h"
13 #include "tree.h"
14 #include "tree-walk.h"
15 #include "unpack-trees.h"
16 #include "dir.h"
18 static const char *merge_argument(struct commit *commit)
20 return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
23 int try_merge_command(struct repository *r,
24 const char *strategy, size_t xopts_nr,
25 const char **xopts, struct commit_list *common,
26 const char *head_arg, struct commit_list *remotes)
28 struct child_process cmd = CHILD_PROCESS_INIT;
29 int i, ret;
30 struct commit_list *j;
32 strvec_pushf(&cmd.args, "merge-%s", strategy);
33 for (i = 0; i < xopts_nr; i++)
34 strvec_pushf(&cmd.args, "--%s", xopts[i]);
35 for (j = common; j; j = j->next)
36 strvec_push(&cmd.args, merge_argument(j->item));
37 strvec_push(&cmd.args, "--");
38 strvec_push(&cmd.args, head_arg);
39 for (j = remotes; j; j = j->next)
40 strvec_push(&cmd.args, merge_argument(j->item));
42 cmd.git_cmd = 1;
43 ret = run_command(&cmd);
45 discard_index(r->index);
46 if (repo_read_index(r) < 0)
47 die(_("failed to read the cache"));
48 resolve_undo_clear_index(r->index);
50 return ret;
53 int checkout_fast_forward(struct repository *r,
54 const struct object_id *head,
55 const struct object_id *remote,
56 int overwrite_ignore)
58 struct tree *trees[MAX_UNPACK_TREES];
59 struct unpack_trees_options opts;
60 struct tree_desc t[MAX_UNPACK_TREES];
61 int i, nr_trees = 0;
62 struct lock_file lock_file = LOCK_INIT;
64 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
66 if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
67 return -1;
69 memset(&trees, 0, sizeof(trees));
70 memset(&t, 0, sizeof(t));
72 trees[nr_trees] = parse_tree_indirect(head);
73 if (!trees[nr_trees++]) {
74 rollback_lock_file(&lock_file);
75 return -1;
77 trees[nr_trees] = parse_tree_indirect(remote);
78 if (!trees[nr_trees++]) {
79 rollback_lock_file(&lock_file);
80 return -1;
82 for (i = 0; i < nr_trees; i++) {
83 parse_tree(trees[i]);
84 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
87 memset(&opts, 0, sizeof(opts));
88 opts.preserve_ignored = !overwrite_ignore;
90 opts.head_idx = 1;
91 opts.src_index = r->index;
92 opts.dst_index = r->index;
93 opts.update = 1;
94 opts.verbose_update = 1;
95 opts.merge = 1;
96 opts.fn = twoway_merge;
97 init_checkout_metadata(&opts.meta, NULL, remote, NULL);
98 setup_unpack_trees_porcelain(&opts, "merge");
100 if (unpack_trees(nr_trees, t, &opts)) {
101 rollback_lock_file(&lock_file);
102 clear_unpack_trees_porcelain(&opts);
103 return -1;
105 clear_unpack_trees_porcelain(&opts);
107 if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
108 return error(_("unable to write new index file"));
109 return 0;