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