reset_head(): remove action parameter

The only use of the action parameter is to setup the error messages for unpack_trees(). All but two cases pass either "checkout" or "reset". The case that passes "reset --hard" would be better passing "reset" so that the error messages match the builtin reset command like all the other callers that are doing a reset. The case that passes "Fast-forwarded" is only updating HEAD and so the parameter is unused in that case as it does not call unpack_trees(). The value to pass to setup_unpack_trees_porcelain() can be determined by checking whether flags contains RESET_HEAD_HARD without the caller having to specify it. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Jan 26, 2022 at 13:05 UTC 1946d45844c65ede4e3a514a5decf16612ad79f0
4 files changed +12 -12
builtin/rebase.c
+7 -7
@@ -583,7 +583,7 @@ static int move_to_original_branch(struct rebase_options *opts)
583 opts->head_name, oid_to_hex(&opts->onto->object.oid));
584 strbuf_addf(&head_reflog, "rebase finished: returning to %s",
585 opts->head_name);
586 - ret = reset_head(the_repository, NULL, "", opts->head_name,
586 + ret = reset_head(the_repository, NULL, opts->head_name,
587 RESET_HEAD_REFS_ONLY,
588 orig_head_reflog.buf, head_reflog.buf,
589 DEFAULT_REFLOG_ACTION);
@@ -674,7 +674,7 @@ static int run_am(struct rebase_options *opts)
674 free(rebased_patches);
675 strvec_clear(&am.args);
676
677 - reset_head(the_repository, &opts->orig_head, "checkout",
677 + reset_head(the_repository, &opts->orig_head,
678 opts->head_name, 0,
679 "HEAD", NULL, DEFAULT_REFLOG_ACTION);
680 error(_("\ngit encountered an error while preparing the "
@@ -820,7 +820,7 @@ static int checkout_up_to_date(struct rebase_options *options)
820 strbuf_addf(&buf, "%s: checkout %s",
821 getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
822 options->switch_to);
823 - if (reset_head(the_repository, &options->orig_head, "checkout",
823 + if (reset_head(the_repository, &options->orig_head,
824 options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
825 NULL, buf.buf, DEFAULT_REFLOG_ACTION) < 0)
826 ret = error(_("could not switch to %s"), options->switch_to);
@@ -1272,7 +1272,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1272 rerere_clear(the_repository, &merge_rr);
1273 string_list_clear(&merge_rr, 1);
1274
1275 - if (reset_head(the_repository, NULL, "reset", NULL, RESET_HEAD_HARD,
1275 + if (reset_head(the_repository, NULL, NULL, RESET_HEAD_HARD,
1276 NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1277 die(_("could not discard worktree changes"));
1278 remove_branch_state(the_repository, 0);
@@ -1290,7 +1290,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1290
1291 if (read_basic_state(&options))
1292 exit(1);
1293 - if (reset_head(the_repository, &options.orig_head, "reset",
1293 + if (reset_head(the_repository, &options.orig_head,
1294 options.head_name, RESET_HEAD_HARD,
1295 NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1296 die(_("could not move back to %s"),
@@ -1759,7 +1759,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1759
1760 strbuf_addf(&msg, "%s: checkout %s",
1761 getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
1762 - if (reset_head(the_repository, &options.onto->object.oid, "checkout", NULL,
1762 + if (reset_head(the_repository, &options.onto->object.oid, NULL,
1763 RESET_HEAD_DETACH | RESET_ORIG_HEAD |
1764 RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1765 NULL, msg.buf, DEFAULT_REFLOG_ACTION))
@@ -1777,7 +1777,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1777 strbuf_addf(&msg, "rebase finished: %s onto %s",
1778 options.head_name ? options.head_name : "detached HEAD",
1779 oid_to_hex(&options.onto->object.oid));
1780 - reset_head(the_repository, NULL, "Fast-forwarded", options.head_name,
1780 + reset_head(the_repository, NULL, options.head_name,
1781 RESET_HEAD_REFS_ONLY, "HEAD", msg.buf,
1782 DEFAULT_REFLOG_ACTION);
1783 strbuf_release(&msg);
reset.c
+3 -2
@@ -8,7 +8,7 @@
8 #include "tree.h"
9 #include "unpack-trees.h"
10
11 -int reset_head(struct repository *r, struct object_id *oid, const char *action,
11 +int reset_head(struct repository *r, struct object_id *oid,
12 const char *switch_to_branch, unsigned flags,
13 const char *reflog_orig_head, const char *reflog_head,
14 const char *default_reflog_action)
@@ -23,7 +23,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
23 struct lock_file lock = LOCK_INIT;
24 struct unpack_trees_options unpack_tree_opts = { 0 };
25 struct tree *tree;
26 - const char *reflog_action;
26 + const char *action, *reflog_action;
27 struct strbuf msg = STRBUF_INIT;
28 size_t prefix_len;
29 struct object_id *old_orig = NULL, oid_old_orig;
@@ -50,6 +50,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
50 if (refs_only)
51 goto reset_head_refs;
52
53 + action = reset_hard ? "reset" : "checkout";
54 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
55 unpack_tree_opts.head_idx = 1;
56 unpack_tree_opts.src_index = r->index;
reset.h
+1 -1
@@ -12,7 +12,7 @@
12 #define RESET_HEAD_REFS_ONLY (1<<3)
13 #define RESET_ORIG_HEAD (1<<4)
14
15 -int reset_head(struct repository *r, struct object_id *oid, const char *action,
15 +int reset_head(struct repository *r, struct object_id *oid,
16 const char *switch_to_branch, unsigned flags,
17 const char *reflog_orig_head, const char *reflog_head,
18 const char *default_reflog_action);
sequencer.c
+1 -2
@@ -4137,8 +4137,7 @@ void create_autostash(struct repository *r, const char *path,
4137 path);
4138 write_file(path, "%s", oid_to_hex(&oid));
4139 printf(_("Created autostash: %s\n"), buf.buf);
4140 - if (reset_head(r, NULL, "reset --hard",
4141 - NULL, RESET_HEAD_HARD, NULL, NULL,
4140 + if (reset_head(r, NULL, NULL, RESET_HEAD_HARD, NULL, NULL,
4141 default_reflog_action) < 0)
4142 die(_("could not reset --hard"));
4143