Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 2 | #include "gettext.h" |
Elijah Newren | df6e874 | 2023-05-16 06:34:00 +0000 | [diff] [blame] | 3 | #include "hash.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 4 | #include "hex.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 5 | #include "lockfile.h" |
Elijah Newren | 750324d | 2023-05-16 06:33:54 +0000 | [diff] [blame] | 6 | #include "merge.h" |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 7 | #include "commit.h" |
Elijah Newren | df6e874 | 2023-05-16 06:34:00 +0000 | [diff] [blame] | 8 | #include "repository.h" |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 9 | #include "run-command.h" |
| 10 | #include "resolve-undo.h" |
Elijah Newren | d4a4f92 | 2023-04-22 20:17:26 +0000 | [diff] [blame] | 11 | #include "tree.h" |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 12 | #include "tree-walk.h" |
| 13 | #include "unpack-trees.h" |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 14 | |
| 15 | static const char *merge_argument(struct commit *commit) |
| 16 | { |
brian m. carlson | e9fe6f2 | 2018-05-02 00:25:57 +0000 | [diff] [blame] | 17 | return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 18 | } |
| 19 | |
Nguyễn Thái Ngọc Duy | 7e196c3 | 2018-09-21 17:57:29 +0200 | [diff] [blame] | 20 | int try_merge_command(struct repository *r, |
| 21 | const char *strategy, size_t xopts_nr, |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 22 | const char **xopts, struct commit_list *common, |
| 23 | const char *head_arg, struct commit_list *remotes) |
| 24 | { |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 25 | struct child_process cmd = CHILD_PROCESS_INIT; |
Jeff King | 5c1753b | 2014-06-19 17:29:31 -0400 | [diff] [blame] | 26 | int i, ret; |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 27 | struct commit_list *j; |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 28 | |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 29 | strvec_pushf(&cmd.args, "merge-%s", strategy); |
Jeff King | 5c1753b | 2014-06-19 17:29:31 -0400 | [diff] [blame] | 30 | for (i = 0; i < xopts_nr; i++) |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 31 | strvec_pushf(&cmd.args, "--%s", xopts[i]); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 32 | for (j = common; j; j = j->next) |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 33 | strvec_push(&cmd.args, merge_argument(j->item)); |
| 34 | strvec_push(&cmd.args, "--"); |
| 35 | strvec_push(&cmd.args, head_arg); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 36 | for (j = remotes; j; j = j->next) |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 37 | strvec_push(&cmd.args, merge_argument(j->item)); |
Jeff King | 5c1753b | 2014-06-19 17:29:31 -0400 | [diff] [blame] | 38 | |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 39 | cmd.git_cmd = 1; |
| 40 | ret = run_command(&cmd); |
Jeff King | 5c1753b | 2014-06-19 17:29:31 -0400 | [diff] [blame] | 41 | |
Nguyễn Thái Ngọc Duy | 7e196c3 | 2018-09-21 17:57:29 +0200 | [diff] [blame] | 42 | discard_index(r->index); |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 43 | if (repo_read_index(r) < 0) |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 44 | die(_("failed to read the cache")); |
Nguyễn Thái Ngọc Duy | 7e196c3 | 2018-09-21 17:57:29 +0200 | [diff] [blame] | 45 | resolve_undo_clear_index(r->index); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 46 | |
| 47 | return ret; |
| 48 | } |
| 49 | |
Nguyễn Thái Ngọc Duy | 7e196c3 | 2018-09-21 17:57:29 +0200 | [diff] [blame] | 50 | int checkout_fast_forward(struct repository *r, |
| 51 | const struct object_id *head, |
brian m. carlson | f06e90d | 2017-05-06 22:10:33 +0000 | [diff] [blame] | 52 | const struct object_id *remote, |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 53 | int overwrite_ignore) |
| 54 | { |
| 55 | struct tree *trees[MAX_UNPACK_TREES]; |
| 56 | struct unpack_trees_options opts; |
| 57 | struct tree_desc t[MAX_UNPACK_TREES]; |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 58 | int i, nr_trees = 0; |
Martin Ågren | 837e34e | 2017-10-05 22:32:04 +0200 | [diff] [blame] | 59 | struct lock_file lock_file = LOCK_INIT; |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 60 | |
Nguyễn Thái Ngọc Duy | 7e196c3 | 2018-09-21 17:57:29 +0200 | [diff] [blame] | 61 | refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 62 | |
Nguyễn Thái Ngọc Duy | 3a95f31 | 2019-01-12 09:13:24 +0700 | [diff] [blame] | 63 | if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0) |
Johannes Schindelin | 55f5704 | 2016-09-09 16:38:00 +0200 | [diff] [blame] | 64 | return -1; |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 65 | |
| 66 | memset(&trees, 0, sizeof(trees)); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 67 | memset(&t, 0, sizeof(t)); |
Martin Ågren | 89e653d | 2018-05-20 12:17:34 +0200 | [diff] [blame] | 68 | |
| 69 | trees[nr_trees] = parse_tree_indirect(head); |
| 70 | if (!trees[nr_trees++]) { |
| 71 | rollback_lock_file(&lock_file); |
| 72 | return -1; |
| 73 | } |
| 74 | trees[nr_trees] = parse_tree_indirect(remote); |
| 75 | if (!trees[nr_trees++]) { |
| 76 | rollback_lock_file(&lock_file); |
| 77 | return -1; |
| 78 | } |
| 79 | for (i = 0; i < nr_trees; i++) { |
Johannes Schindelin | aa9f618 | 2024-02-23 08:34:23 +0000 | [diff] [blame] | 80 | if (parse_tree(trees[i]) < 0) { |
| 81 | rollback_lock_file(&lock_file); |
| 82 | return -1; |
| 83 | } |
Eric W. Biederman | efed687 | 2023-10-01 21:40:28 -0500 | [diff] [blame] | 84 | init_tree_desc(t+i, &trees[i]->object.oid, |
| 85 | trees[i]->buffer, trees[i]->size); |
Martin Ågren | 89e653d | 2018-05-20 12:17:34 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | memset(&opts, 0, sizeof(opts)); |
Elijah Newren | 04988c8 | 2021-09-27 16:33:41 +0000 | [diff] [blame] | 89 | opts.preserve_ignored = !overwrite_ignore; |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 90 | |
| 91 | opts.head_idx = 1; |
Nguyễn Thái Ngọc Duy | 7e196c3 | 2018-09-21 17:57:29 +0200 | [diff] [blame] | 92 | opts.src_index = r->index; |
| 93 | opts.dst_index = r->index; |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 94 | opts.update = 1; |
| 95 | opts.verbose_update = 1; |
| 96 | opts.merge = 1; |
| 97 | opts.fn = twoway_merge; |
brian m. carlson | 13e7ed6 | 2020-03-16 18:05:04 +0000 | [diff] [blame] | 98 | init_checkout_metadata(&opts.meta, NULL, remote, NULL); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 99 | setup_unpack_trees_porcelain(&opts, "merge"); |
| 100 | |
Martin Ågren | 5790d25 | 2018-02-28 20:07:57 +0100 | [diff] [blame] | 101 | if (unpack_trees(nr_trees, t, &opts)) { |
| 102 | rollback_lock_file(&lock_file); |
Martin Ågren | 1c41d28 | 2018-05-21 16:54:28 +0200 | [diff] [blame] | 103 | clear_unpack_trees_porcelain(&opts); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 104 | return -1; |
Martin Ågren | 5790d25 | 2018-02-28 20:07:57 +0100 | [diff] [blame] | 105 | } |
Martin Ågren | 1c41d28 | 2018-05-21 16:54:28 +0200 | [diff] [blame] | 106 | clear_unpack_trees_porcelain(&opts); |
| 107 | |
Nguyễn Thái Ngọc Duy | 7e196c3 | 2018-09-21 17:57:29 +0200 | [diff] [blame] | 108 | if (write_locked_index(r->index, &lock_file, COMMIT_LOCK)) |
Johannes Schindelin | 55f5704 | 2016-09-09 16:38:00 +0200 | [diff] [blame] | 109 | return error(_("unable to write new index file")); |
Nguyễn Thái Ngọc Duy | db699a8 | 2012-10-26 22:53:49 +0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |