blob: 752a937fa93dd3bf8703400c6311afe7a5265e02 [file] [log] [blame]
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00001#include "git-compat-util.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00002#include "gettext.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +00003#include "hash.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00004#include "hex.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02005#include "lockfile.h"
Elijah Newren750324d2023-05-16 06:33:54 +00006#include "merge.h"
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +07007#include "commit.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +00008#include "repository.h"
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +07009#include "run-command.h"
10#include "resolve-undo.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +000011#include "tree.h"
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070012#include "tree-walk.h"
13#include "unpack-trees.h"
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070014
15static const char *merge_argument(struct commit *commit)
16{
brian m. carlsone9fe6f22018-05-02 00:25:57 +000017 return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070018}
19
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020020int try_merge_command(struct repository *r,
21 const char *strategy, size_t xopts_nr,
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070022 const char **xopts, struct commit_list *common,
23 const char *head_arg, struct commit_list *remotes)
24{
René Scharfe0e906732022-10-30 12:51:14 +010025 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff King5c1753b2014-06-19 17:29:31 -040026 int i, ret;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070027 struct commit_list *j;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070028
René Scharfe0e906732022-10-30 12:51:14 +010029 strvec_pushf(&cmd.args, "merge-%s", strategy);
Jeff King5c1753b2014-06-19 17:29:31 -040030 for (i = 0; i < xopts_nr; i++)
René Scharfe0e906732022-10-30 12:51:14 +010031 strvec_pushf(&cmd.args, "--%s", xopts[i]);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070032 for (j = common; j; j = j->next)
René Scharfe0e906732022-10-30 12:51:14 +010033 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 Duydb699a82012-10-26 22:53:49 +070036 for (j = remotes; j; j = j->next)
René Scharfe0e906732022-10-30 12:51:14 +010037 strvec_push(&cmd.args, merge_argument(j->item));
Jeff King5c1753b2014-06-19 17:29:31 -040038
René Scharfe0e906732022-10-30 12:51:14 +010039 cmd.git_cmd = 1;
40 ret = run_command(&cmd);
Jeff King5c1753b2014-06-19 17:29:31 -040041
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020042 discard_index(r->index);
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +070043 if (repo_read_index(r) < 0)
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070044 die(_("failed to read the cache"));
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020045 resolve_undo_clear_index(r->index);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070046
47 return ret;
48}
49
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020050int checkout_fast_forward(struct repository *r,
51 const struct object_id *head,
brian m. carlsonf06e90d2017-05-06 22:10:33 +000052 const struct object_id *remote,
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070053 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 Duy03b86642014-06-13 19:19:23 +070058 int i, nr_trees = 0;
Martin Ågren837e34e2017-10-05 22:32:04 +020059 struct lock_file lock_file = LOCK_INIT;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070060
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020061 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070062
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +070063 if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
Johannes Schindelin55f57042016-09-09 16:38:00 +020064 return -1;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070065
66 memset(&trees, 0, sizeof(trees));
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070067 memset(&t, 0, sizeof(t));
Martin Ågren89e653d2018-05-20 12:17:34 +020068
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 Schindelinaa9f6182024-02-23 08:34:23 +000080 if (parse_tree(trees[i]) < 0) {
81 rollback_lock_file(&lock_file);
82 return -1;
83 }
Eric W. Biedermanefed6872023-10-01 21:40:28 -050084 init_tree_desc(t+i, &trees[i]->object.oid,
85 trees[i]->buffer, trees[i]->size);
Martin Ågren89e653d2018-05-20 12:17:34 +020086 }
87
88 memset(&opts, 0, sizeof(opts));
Elijah Newren04988c82021-09-27 16:33:41 +000089 opts.preserve_ignored = !overwrite_ignore;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070090
91 opts.head_idx = 1;
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020092 opts.src_index = r->index;
93 opts.dst_index = r->index;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070094 opts.update = 1;
95 opts.verbose_update = 1;
96 opts.merge = 1;
97 opts.fn = twoway_merge;
brian m. carlson13e7ed62020-03-16 18:05:04 +000098 init_checkout_metadata(&opts.meta, NULL, remote, NULL);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070099 setup_unpack_trees_porcelain(&opts, "merge");
100
Martin Ågren5790d252018-02-28 20:07:57 +0100101 if (unpack_trees(nr_trees, t, &opts)) {
102 rollback_lock_file(&lock_file);
Martin Ågren1c41d282018-05-21 16:54:28 +0200103 clear_unpack_trees_porcelain(&opts);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +0700104 return -1;
Martin Ågren5790d252018-02-28 20:07:57 +0100105 }
Martin Ågren1c41d282018-05-21 16:54:28 +0200106 clear_unpack_trees_porcelain(&opts);
107
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +0200108 if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
Johannes Schindelin55f57042016-09-09 16:38:00 +0200109 return error(_("unable to write new index file"));
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +0700110 return 0;
111}