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