blob: 6e736881d900bf76f33c1684e411b498e37582bd [file] [log] [blame]
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +07001#include "cache.h"
Elijah Newrenb1017932017-12-21 11:19:06 -08002#include "diff.h"
3#include "diffcore.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02004#include "lockfile.h"
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +07005#include "commit.h"
6#include "run-command.h"
7#include "resolve-undo.h"
8#include "tree-walk.h"
9#include "unpack-trees.h"
10#include "dir.h"
11
12static const char *merge_argument(struct commit *commit)
13{
brian m. carlsone9fe6f22018-05-02 00:25:57 +000014 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 +070015}
16
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020017int try_merge_command(struct repository *r,
18 const char *strategy, size_t xopts_nr,
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070019 const char **xopts, struct commit_list *common,
20 const char *head_arg, struct commit_list *remotes)
21{
Jeff Kingc972bf42020-07-28 16:25:12 -040022 struct strvec args = STRVEC_INIT;
Jeff King5c1753b2014-06-19 17:29:31 -040023 int i, ret;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070024 struct commit_list *j;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070025
Jeff Kingc972bf42020-07-28 16:25:12 -040026 strvec_pushf(&args, "merge-%s", strategy);
Jeff King5c1753b2014-06-19 17:29:31 -040027 for (i = 0; i < xopts_nr; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -040028 strvec_pushf(&args, "--%s", xopts[i]);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070029 for (j = common; j; j = j->next)
Jeff Kingc972bf42020-07-28 16:25:12 -040030 strvec_push(&args, merge_argument(j->item));
31 strvec_push(&args, "--");
32 strvec_push(&args, head_arg);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070033 for (j = remotes; j; j = j->next)
Jeff Kingc972bf42020-07-28 16:25:12 -040034 strvec_push(&args, merge_argument(j->item));
Jeff King5c1753b2014-06-19 17:29:31 -040035
Jeff Kingd70a9eb2020-07-28 20:37:20 -040036 ret = run_command_v_opt(args.v, RUN_GIT_CMD);
Jeff Kingc972bf42020-07-28 16:25:12 -040037 strvec_clear(&args);
Jeff King5c1753b2014-06-19 17:29:31 -040038
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020039 discard_index(r->index);
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +070040 if (repo_read_index(r) < 0)
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070041 die(_("failed to read the cache"));
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020042 resolve_undo_clear_index(r->index);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070043
44 return ret;
45}
46
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020047int checkout_fast_forward(struct repository *r,
48 const struct object_id *head,
brian m. carlsonf06e90d2017-05-06 22:10:33 +000049 const struct object_id *remote,
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070050 int overwrite_ignore)
51{
52 struct tree *trees[MAX_UNPACK_TREES];
53 struct unpack_trees_options opts;
54 struct tree_desc t[MAX_UNPACK_TREES];
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +070055 int i, nr_trees = 0;
Ævar Arnfjörð Bjarmasonce93a4c2021-07-01 12:51:27 +020056 struct dir_struct dir = DIR_INIT;
Martin Ågren837e34e2017-10-05 22:32:04 +020057 struct lock_file lock_file = LOCK_INIT;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070058
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020059 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070060
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +070061 if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
Johannes Schindelin55f57042016-09-09 16:38:00 +020062 return -1;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070063
64 memset(&trees, 0, sizeof(trees));
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070065 memset(&t, 0, sizeof(t));
Martin Ågren89e653d2018-05-20 12:17:34 +020066
67 trees[nr_trees] = parse_tree_indirect(head);
68 if (!trees[nr_trees++]) {
69 rollback_lock_file(&lock_file);
70 return -1;
71 }
72 trees[nr_trees] = parse_tree_indirect(remote);
73 if (!trees[nr_trees++]) {
74 rollback_lock_file(&lock_file);
75 return -1;
76 }
77 for (i = 0; i < nr_trees; i++) {
78 parse_tree(trees[i]);
79 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
80 }
81
82 memset(&opts, 0, sizeof(opts));
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070083 if (overwrite_ignore) {
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070084 dir.flags |= DIR_SHOW_IGNORED;
85 setup_standard_excludes(&dir);
86 opts.dir = &dir;
87 }
88
89 opts.head_idx = 1;
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +020090 opts.src_index = r->index;
91 opts.dst_index = r->index;
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070092 opts.update = 1;
93 opts.verbose_update = 1;
94 opts.merge = 1;
95 opts.fn = twoway_merge;
brian m. carlson13e7ed62020-03-16 18:05:04 +000096 init_checkout_metadata(&opts.meta, NULL, remote, NULL);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +070097 setup_unpack_trees_porcelain(&opts, "merge");
98
Martin Ågren5790d252018-02-28 20:07:57 +010099 if (unpack_trees(nr_trees, t, &opts)) {
100 rollback_lock_file(&lock_file);
Martin Ågren1c41d282018-05-21 16:54:28 +0200101 clear_unpack_trees_porcelain(&opts);
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +0700102 return -1;
Martin Ågren5790d252018-02-28 20:07:57 +0100103 }
Elijah Newreneceba532020-08-18 22:58:26 +0000104 dir_clear(&dir);
Martin Ågren1c41d282018-05-21 16:54:28 +0200105 clear_unpack_trees_porcelain(&opts);
106
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +0200107 if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
Johannes Schindelin55f57042016-09-09 16:38:00 +0200108 return error(_("unable to write new index file"));
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +0700109 return 0;
110}