git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / merge.c
| 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 "path.h" |
| 12 | #include "run-command.h" |
| 13 | #include "resolve-undo.h" |
| 14 | #include "tree.h" |
| 15 | #include "tree-walk.h" |
| 16 | #include "unpack-trees.h" |
| 17 | |
| 18 | static const char *merge_argument(struct commit *commit) |
| 19 | { |
| 20 | return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree); |
| 21 | } |
| 22 | |
| 23 | int try_merge_command(struct repository *r, |
| 24 | const char *strategy, size_t xopts_nr, |
| 25 | const char **xopts, struct commit_list *common, |
| 26 | const char *head_arg, struct commit_list *remotes) |
| 27 | { |
| 28 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 29 | int ret; |
| 30 | struct commit_list *j; |
| 31 | |
| 32 | strvec_pushf(&cmd.args, "merge-%s", strategy); |
| 33 | for (size_t i = 0; i < xopts_nr; i++) |
| 34 | strvec_pushf(&cmd.args, "--%s", xopts[i]); |
| 35 | for (j = common; j; j = j->next) |
| 36 | strvec_push(&cmd.args, merge_argument(j->item)); |
| 37 | strvec_push(&cmd.args, "--"); |
| 38 | strvec_push(&cmd.args, head_arg); |
| 39 | for (j = remotes; j; j = j->next) |
| 40 | strvec_push(&cmd.args, merge_argument(j->item)); |
| 41 | |
| 42 | cmd.git_cmd = 1; |
| 43 | ret = run_command(&cmd); |
| 44 | |
| 45 | discard_index(r->index); |
| 46 | if (repo_read_index(r) < 0) |
| 47 | die(_("failed to read the cache")); |
| 48 | resolve_undo_clear_index(r->index); |
| 49 | |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | int checkout_fast_forward(struct repository *r, |
| 54 | const struct object_id *head, |
| 55 | const struct object_id *remote, |
| 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]; |
| 61 | int i, nr_trees = 0; |
| 62 | struct lock_file lock_file = LOCK_INIT; |
| 63 | |
| 64 | refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); |
| 65 | |
| 66 | if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0) |
| 67 | return -1; |
| 68 | |
| 69 | memset(&trees, 0, sizeof(trees)); |
| 70 | memset(&t, 0, sizeof(t)); |
| 71 | |
| 72 | trees[nr_trees] = repo_parse_tree_indirect(the_repository, head); |
| 73 | if (!trees[nr_trees++]) { |
| 74 | rollback_lock_file(&lock_file); |
| 75 | return -1; |
| 76 | } |
| 77 | trees[nr_trees] = repo_parse_tree_indirect(the_repository, 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 | if (repo_parse_tree(the_repository, trees[i]) < 0) { |
| 84 | rollback_lock_file(&lock_file); |
| 85 | return -1; |
| 86 | } |
| 87 | init_tree_desc(t+i, &trees[i]->object.oid, |
| 88 | trees[i]->buffer, trees[i]->size); |
| 89 | } |
| 90 | |
| 91 | memset(&opts, 0, sizeof(opts)); |
| 92 | opts.preserve_ignored = !overwrite_ignore; |
| 93 | |
| 94 | opts.head_idx = 1; |
| 95 | opts.src_index = r->index; |
| 96 | opts.dst_index = r->index; |
| 97 | opts.update = 1; |
| 98 | opts.verbose_update = 1; |
| 99 | opts.merge = 1; |
| 100 | opts.fn = twoway_merge; |
| 101 | init_checkout_metadata(&opts.meta, NULL, remote, NULL); |
| 102 | setup_unpack_trees_porcelain(&opts, "merge"); |
| 103 | |
| 104 | if (unpack_trees(nr_trees, t, &opts)) { |
| 105 | rollback_lock_file(&lock_file); |
| 106 | clear_unpack_trees_porcelain(&opts); |
| 107 | return -1; |
| 108 | } |
| 109 | clear_unpack_trees_porcelain(&opts); |
| 110 | |
| 111 | if (write_locked_index(r->index, &lock_file, COMMIT_LOCK)) |
| 112 | return error(_("unable to write new index file")); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int write_merge_labels(struct repository *r, const char *labels[3]) |
| 117 | { |
| 118 | FILE *f = fopen_or_warn(git_path_merge_labels(r), "w"); |
| 119 | |
| 120 | if (!f) |
| 121 | return -1; |
| 122 | |
| 123 | fprintf(f, "%s\n%s\n%s\n", labels[0], labels[1], labels[2]); |
| 124 | if (fclose(f)) |
| 125 | return error_errno("could not write '%s'", |
| 126 | git_path_merge_labels(r)); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static char *parse_merge_label_line(FILE *fp) |
| 132 | { |
| 133 | struct strbuf buf = STRBUF_INIT; |
| 134 | |
| 135 | if (strbuf_getline(&buf, fp) == EOF) { |
| 136 | strbuf_release(&buf); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | return strbuf_detach(&buf, NULL); |
| 141 | } |
| 142 | |
| 143 | int read_merge_labels(struct repository *r, |
| 144 | char **pbase, char **pours, char **ptheirs) |
| 145 | { |
| 146 | char *base = NULL, *ours = NULL, *theirs = NULL; |
| 147 | int ret = -1; |
| 148 | FILE *fp = fopen(git_path_merge_labels(r), "r"); |
| 149 | |
| 150 | if (!fp) |
| 151 | return -1; |
| 152 | |
| 153 | base = parse_merge_label_line(fp); |
| 154 | if (!base) |
| 155 | goto out; |
| 156 | |
| 157 | ours = parse_merge_label_line(fp); |
| 158 | if (!ours) |
| 159 | goto out; |
| 160 | |
| 161 | theirs = parse_merge_label_line(fp); |
| 162 | if (!theirs) |
| 163 | goto out; |
| 164 | /* We ignore any trailing lines */ |
| 165 | |
| 166 | ret = 0; |
| 167 | *pbase = base; |
| 168 | *pours = ours; |
| 169 | *ptheirs = theirs; |
| 170 | out: |
| 171 | if (ret) { |
| 172 | free(base); |
| 173 | free(ours); |
| 174 | free(theirs); |
| 175 | } |
| 176 | fclose(fp); |
| 177 | |
| 178 | return ret; |
| 179 | } |