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