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