]> git.ipfire.org Git - thirdparty/git.git/blame - merge.c
The twentieth batch
[thirdparty/git.git] / merge.c
CommitLineData
bc5c5ec0 1#include "git-compat-util.h"
f394e093 2#include "gettext.h"
df6e8744 3#include "hash.h"
41771fa4 4#include "hex.h"
697cc8ef 5#include "lockfile.h"
750324dd 6#include "merge.h"
db699a8a 7#include "commit.h"
df6e8744 8#include "repository.h"
db699a8a
NTND
9#include "run-command.h"
10#include "resolve-undo.h"
d4a4f929 11#include "tree.h"
db699a8a
NTND
12#include "tree-walk.h"
13#include "unpack-trees.h"
db699a8a
NTND
14
15static const char *merge_argument(struct commit *commit)
16{
e9fe6f26 17 return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
db699a8a
NTND
18}
19
7e196c3a
NTND
20int try_merge_command(struct repository *r,
21 const char *strategy, size_t xopts_nr,
db699a8a
NTND
22 const char **xopts, struct commit_list *common,
23 const char *head_arg, struct commit_list *remotes)
24{
0e906739 25 struct child_process cmd = CHILD_PROCESS_INIT;
5c1753b1 26 int i, ret;
db699a8a 27 struct commit_list *j;
db699a8a 28
0e906739 29 strvec_pushf(&cmd.args, "merge-%s", strategy);
5c1753b1 30 for (i = 0; i < xopts_nr; i++)
0e906739 31 strvec_pushf(&cmd.args, "--%s", xopts[i]);
db699a8a 32 for (j = common; j; j = j->next)
0e906739
RS
33 strvec_push(&cmd.args, merge_argument(j->item));
34 strvec_push(&cmd.args, "--");
35 strvec_push(&cmd.args, head_arg);
db699a8a 36 for (j = remotes; j; j = j->next)
0e906739 37 strvec_push(&cmd.args, merge_argument(j->item));
5c1753b1 38
0e906739
RS
39 cmd.git_cmd = 1;
40 ret = run_command(&cmd);
5c1753b1 41
7e196c3a 42 discard_index(r->index);
e1ff0a32 43 if (repo_read_index(r) < 0)
db699a8a 44 die(_("failed to read the cache"));
7e196c3a 45 resolve_undo_clear_index(r->index);
db699a8a
NTND
46
47 return ret;
48}
49
7e196c3a
NTND
50int checkout_fast_forward(struct repository *r,
51 const struct object_id *head,
f06e90da 52 const struct object_id *remote,
db699a8a
NTND
53 int overwrite_ignore)
54{
55 struct tree *trees[MAX_UNPACK_TREES];
56 struct unpack_trees_options opts;
57 struct tree_desc t[MAX_UNPACK_TREES];
03b86647 58 int i, nr_trees = 0;
837e34eb 59 struct lock_file lock_file = LOCK_INIT;
db699a8a 60
7e196c3a 61 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
db699a8a 62
3a95f31d 63 if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
55f5704d 64 return -1;
db699a8a
NTND
65
66 memset(&trees, 0, sizeof(trees));
db699a8a 67 memset(&t, 0, sizeof(t));
89e653da
68
69 trees[nr_trees] = parse_tree_indirect(head);
70 if (!trees[nr_trees++]) {
71 rollback_lock_file(&lock_file);
72 return -1;
73 }
74 trees[nr_trees] = parse_tree_indirect(remote);
75 if (!trees[nr_trees++]) {
76 rollback_lock_file(&lock_file);
77 return -1;
78 }
79 for (i = 0; i < nr_trees; i++) {
aa9f6189
JS
80 if (parse_tree(trees[i]) < 0) {
81 rollback_lock_file(&lock_file);
82 return -1;
83 }
efed687e
EB
84 init_tree_desc(t+i, &trees[i]->object.oid,
85 trees[i]->buffer, trees[i]->size);
89e653da
86 }
87
88 memset(&opts, 0, sizeof(opts));
04988c8d 89 opts.preserve_ignored = !overwrite_ignore;
db699a8a
NTND
90
91 opts.head_idx = 1;
7e196c3a
NTND
92 opts.src_index = r->index;
93 opts.dst_index = r->index;
db699a8a
NTND
94 opts.update = 1;
95 opts.verbose_update = 1;
96 opts.merge = 1;
97 opts.fn = twoway_merge;
13e7ed6a 98 init_checkout_metadata(&opts.meta, NULL, remote, NULL);
db699a8a
NTND
99 setup_unpack_trees_porcelain(&opts, "merge");
100
5790d258
101 if (unpack_trees(nr_trees, t, &opts)) {
102 rollback_lock_file(&lock_file);
1c41d280 103 clear_unpack_trees_porcelain(&opts);
db699a8a 104 return -1;
5790d258 105 }
1c41d280
106 clear_unpack_trees_porcelain(&opts);
107
7e196c3a 108 if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
55f5704d 109 return error(_("unable to write new index file"));
db699a8a
NTND
110 return 0;
111}