2023-05-16 06:33:57 +00:00
|
|
|
#include "git-compat-util.h"
|
2023-03-21 06:25:54 +00:00
|
|
|
#include "gettext.h"
|
2023-05-16 06:34:00 +00:00
|
|
|
#include "hash.h"
|
2023-02-24 00:09:27 +00:00
|
|
|
#include "hex.h"
|
2014-10-01 10:28:42 +00:00
|
|
|
#include "lockfile.h"
|
2023-05-16 06:33:54 +00:00
|
|
|
#include "merge.h"
|
2012-10-26 15:53:49 +00:00
|
|
|
#include "commit.h"
|
2023-05-16 06:34:00 +00:00
|
|
|
#include "repository.h"
|
2012-10-26 15:53:49 +00:00
|
|
|
#include "run-command.h"
|
|
|
|
#include "resolve-undo.h"
|
2023-04-22 20:17:26 +00:00
|
|
|
#include "tree.h"
|
2012-10-26 15:53:49 +00:00
|
|
|
#include "tree-walk.h"
|
|
|
|
#include "unpack-trees.h"
|
|
|
|
|
|
|
|
static const char *merge_argument(struct commit *commit)
|
|
|
|
{
|
2018-05-02 00:25:57 +00:00
|
|
|
return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
|
2012-10-26 15:53:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 15:57:29 +00:00
|
|
|
int try_merge_command(struct repository *r,
|
|
|
|
const char *strategy, size_t xopts_nr,
|
2012-10-26 15:53:49 +00:00
|
|
|
const char **xopts, struct commit_list *common,
|
|
|
|
const char *head_arg, struct commit_list *remotes)
|
|
|
|
{
|
2022-10-30 11:51:14 +00:00
|
|
|
struct child_process cmd = CHILD_PROCESS_INIT;
|
2014-06-19 21:29:31 +00:00
|
|
|
int i, ret;
|
2012-10-26 15:53:49 +00:00
|
|
|
struct commit_list *j;
|
|
|
|
|
2022-10-30 11:51:14 +00:00
|
|
|
strvec_pushf(&cmd.args, "merge-%s", strategy);
|
2014-06-19 21:29:31 +00:00
|
|
|
for (i = 0; i < xopts_nr; i++)
|
2022-10-30 11:51:14 +00:00
|
|
|
strvec_pushf(&cmd.args, "--%s", xopts[i]);
|
2012-10-26 15:53:49 +00:00
|
|
|
for (j = common; j; j = j->next)
|
2022-10-30 11:51:14 +00:00
|
|
|
strvec_push(&cmd.args, merge_argument(j->item));
|
|
|
|
strvec_push(&cmd.args, "--");
|
|
|
|
strvec_push(&cmd.args, head_arg);
|
2012-10-26 15:53:49 +00:00
|
|
|
for (j = remotes; j; j = j->next)
|
2022-10-30 11:51:14 +00:00
|
|
|
strvec_push(&cmd.args, merge_argument(j->item));
|
2014-06-19 21:29:31 +00:00
|
|
|
|
2022-10-30 11:51:14 +00:00
|
|
|
cmd.git_cmd = 1;
|
|
|
|
ret = run_command(&cmd);
|
2014-06-19 21:29:31 +00:00
|
|
|
|
2018-09-21 15:57:29 +00:00
|
|
|
discard_index(r->index);
|
2019-01-12 02:13:26 +00:00
|
|
|
if (repo_read_index(r) < 0)
|
2012-10-26 15:53:49 +00:00
|
|
|
die(_("failed to read the cache"));
|
2018-09-21 15:57:29 +00:00
|
|
|
resolve_undo_clear_index(r->index);
|
2012-10-26 15:53:49 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-09-21 15:57:29 +00:00
|
|
|
int checkout_fast_forward(struct repository *r,
|
|
|
|
const struct object_id *head,
|
2017-05-06 22:10:33 +00:00
|
|
|
const struct object_id *remote,
|
2012-10-26 15:53:49 +00:00
|
|
|
int overwrite_ignore)
|
|
|
|
{
|
|
|
|
struct tree *trees[MAX_UNPACK_TREES];
|
|
|
|
struct unpack_trees_options opts;
|
|
|
|
struct tree_desc t[MAX_UNPACK_TREES];
|
2014-06-13 12:19:23 +00:00
|
|
|
int i, nr_trees = 0;
|
2017-10-05 20:32:04 +00:00
|
|
|
struct lock_file lock_file = LOCK_INIT;
|
2012-10-26 15:53:49 +00:00
|
|
|
|
2018-09-21 15:57:29 +00:00
|
|
|
refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
|
2012-10-26 15:53:49 +00:00
|
|
|
|
2019-01-12 02:13:24 +00:00
|
|
|
if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
|
2016-09-09 14:38:00 +00:00
|
|
|
return -1;
|
2012-10-26 15:53:49 +00:00
|
|
|
|
|
|
|
memset(&trees, 0, sizeof(trees));
|
|
|
|
memset(&t, 0, sizeof(t));
|
2018-05-20 10:17:34 +00:00
|
|
|
|
|
|
|
trees[nr_trees] = parse_tree_indirect(head);
|
|
|
|
if (!trees[nr_trees++]) {
|
|
|
|
rollback_lock_file(&lock_file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
trees[nr_trees] = parse_tree_indirect(remote);
|
|
|
|
if (!trees[nr_trees++]) {
|
|
|
|
rollback_lock_file(&lock_file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < nr_trees; i++) {
|
2024-02-23 08:34:23 +00:00
|
|
|
if (parse_tree(trees[i]) < 0) {
|
|
|
|
rollback_lock_file(&lock_file);
|
|
|
|
return -1;
|
|
|
|
}
|
2023-10-02 02:40:28 +00:00
|
|
|
init_tree_desc(t+i, &trees[i]->object.oid,
|
|
|
|
trees[i]->buffer, trees[i]->size);
|
2018-05-20 10:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
2021-09-27 16:33:41 +00:00
|
|
|
opts.preserve_ignored = !overwrite_ignore;
|
2012-10-26 15:53:49 +00:00
|
|
|
|
|
|
|
opts.head_idx = 1;
|
2018-09-21 15:57:29 +00:00
|
|
|
opts.src_index = r->index;
|
|
|
|
opts.dst_index = r->index;
|
2012-10-26 15:53:49 +00:00
|
|
|
opts.update = 1;
|
|
|
|
opts.verbose_update = 1;
|
|
|
|
opts.merge = 1;
|
|
|
|
opts.fn = twoway_merge;
|
2020-03-16 18:05:04 +00:00
|
|
|
init_checkout_metadata(&opts.meta, NULL, remote, NULL);
|
2012-10-26 15:53:49 +00:00
|
|
|
setup_unpack_trees_porcelain(&opts, "merge");
|
|
|
|
|
2018-02-28 19:07:57 +00:00
|
|
|
if (unpack_trees(nr_trees, t, &opts)) {
|
|
|
|
rollback_lock_file(&lock_file);
|
2018-05-21 14:54:28 +00:00
|
|
|
clear_unpack_trees_porcelain(&opts);
|
2012-10-26 15:53:49 +00:00
|
|
|
return -1;
|
2018-02-28 19:07:57 +00:00
|
|
|
}
|
2018-05-21 14:54:28 +00:00
|
|
|
clear_unpack_trees_porcelain(&opts);
|
|
|
|
|
2018-09-21 15:57:29 +00:00
|
|
|
if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
|
2016-09-09 14:38:00 +00:00
|
|
|
return error(_("unable to write new index file"));
|
2012-10-26 15:53:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|