merge.c: remove implicit dependency on the_index

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy
2018-09-21 17:57:29 +02:00
committed by Junio C Hamano
parent f4a55b2797
commit 7e196c3a28
5 changed files with 28 additions and 19 deletions

View File

@ -728,8 +728,9 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
die(_("unable to write %s"), get_index_file()); die(_("unable to write %s"), get_index_file());
return clean ? 0 : 1; return clean ? 0 : 1;
} else { } else {
return try_merge_command(strategy, xopts_nr, xopts, return try_merge_command(the_repository,
common, head_arg, remoteheads); strategy, xopts_nr, xopts,
common, head_arg, remoteheads);
} }
} }
@ -1470,7 +1471,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
goto done; goto done;
} }
if (checkout_fast_forward(&head_commit->object.oid, if (checkout_fast_forward(the_repository,
&head_commit->object.oid,
&commit->object.oid, &commit->object.oid,
overwrite_ignore)) { overwrite_ignore)) {
ret = 1; ret = 1;

View File

@ -562,7 +562,9 @@ static int pull_into_void(const struct object_id *merge_head,
* index/worktree changes that the user already made on the unborn * index/worktree changes that the user already made on the unborn
* branch. * branch.
*/ */
if (checkout_fast_forward(the_hash_algo->empty_tree, merge_head, 0)) if (checkout_fast_forward(the_repository,
the_hash_algo->empty_tree,
merge_head, 0))
return 1; return 1;
if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR)) if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
@ -915,7 +917,8 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
"fast-forwarding your working tree from\n" "fast-forwarding your working tree from\n"
"commit %s."), oid_to_hex(&orig_head)); "commit %s."), oid_to_hex(&orig_head));
if (checkout_fast_forward(&orig_head, &curr_head, 0)) if (checkout_fast_forward(the_repository, &orig_head,
&curr_head, 0))
die(_("Cannot fast-forward your working tree.\n" die(_("Cannot fast-forward your working tree.\n"
"After making sure that you saved anything precious from\n" "After making sure that you saved anything precious from\n"
"$ git diff %s\n" "$ git diff %s\n"

View File

@ -1716,10 +1716,12 @@ extern struct startup_info *startup_info;
/* merge.c */ /* merge.c */
struct commit_list; struct commit_list;
int try_merge_command(const char *strategy, size_t xopts_nr, int try_merge_command(struct repository *r,
const char *strategy, size_t xopts_nr,
const char **xopts, struct commit_list *common, const char **xopts, struct commit_list *common,
const char *head_arg, struct commit_list *remotes); const char *head_arg, struct commit_list *remotes);
int checkout_fast_forward(const struct object_id *from, int checkout_fast_forward(struct repository *r,
const struct object_id *from,
const struct object_id *to, const struct object_id *to,
int overwrite_ignore); int overwrite_ignore);

20
merge.c
View File

@ -14,7 +14,8 @@ static const char *merge_argument(struct commit *commit)
return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree); return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
} }
int try_merge_command(const char *strategy, size_t xopts_nr, int try_merge_command(struct repository *r,
const char *strategy, size_t xopts_nr,
const char **xopts, struct commit_list *common, const char **xopts, struct commit_list *common,
const char *head_arg, struct commit_list *remotes) const char *head_arg, struct commit_list *remotes)
{ {
@ -35,15 +36,16 @@ int try_merge_command(const char *strategy, size_t xopts_nr,
ret = run_command_v_opt(args.argv, RUN_GIT_CMD); ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
argv_array_clear(&args); argv_array_clear(&args);
discard_cache(); discard_index(r->index);
if (read_cache() < 0) if (read_index(r->index) < 0)
die(_("failed to read the cache")); die(_("failed to read the cache"));
resolve_undo_clear(); resolve_undo_clear_index(r->index);
return ret; return ret;
} }
int checkout_fast_forward(const struct object_id *head, int checkout_fast_forward(struct repository *r,
const struct object_id *head,
const struct object_id *remote, const struct object_id *remote,
int overwrite_ignore) int overwrite_ignore)
{ {
@ -54,7 +56,7 @@ int checkout_fast_forward(const struct object_id *head,
struct dir_struct dir; struct dir_struct dir;
struct lock_file lock_file = LOCK_INIT; struct lock_file lock_file = LOCK_INIT;
refresh_cache(REFRESH_QUIET); refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0) if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0)
return -1; return -1;
@ -86,8 +88,8 @@ int checkout_fast_forward(const struct object_id *head,
} }
opts.head_idx = 1; opts.head_idx = 1;
opts.src_index = &the_index; opts.src_index = r->index;
opts.dst_index = &the_index; opts.dst_index = r->index;
opts.update = 1; opts.update = 1;
opts.verbose_update = 1; opts.verbose_update = 1;
opts.merge = 1; opts.merge = 1;
@ -101,7 +103,7 @@ int checkout_fast_forward(const struct object_id *head,
} }
clear_unpack_trees_porcelain(&opts); clear_unpack_trees_porcelain(&opts);
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
return error(_("unable to write new index file")); return error(_("unable to write new index file"));
return 0; return 0;
} }

View File

@ -470,8 +470,8 @@ static int fast_forward_to(const struct object_id *to, const struct object_id *f
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
struct strbuf err = STRBUF_INIT; struct strbuf err = STRBUF_INIT;
read_cache(); read_index(&the_index);
if (checkout_fast_forward(from, to, 1)) if (checkout_fast_forward(the_repository, from, to, 1))
return -1; /* the callee should have complained already */ return -1; /* the callee should have complained already */
strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts))); strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts)));
@ -1827,7 +1827,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
commit_list_insert(base, &common); commit_list_insert(base, &common);
commit_list_insert(next, &remotes); commit_list_insert(next, &remotes);
res |= try_merge_command(opts->strategy, res |= try_merge_command(the_repository, opts->strategy,
opts->xopts_nr, (const char **)opts->xopts, opts->xopts_nr, (const char **)opts->xopts,
common, oid_to_hex(&head), remotes); common, oid_to_hex(&head), remotes);
free_commit_list(common); free_commit_list(common);