reset_head(): make default_reflog_action optional

This parameter is only needed when a ref is going to be updated and the caller does not pass an explicit reflog message. Callers that are only discarding uncommitted changes in the working tree such as such as "rebase --skip" or create_autostash() do not update any refs so should not have to worry about passing this parameter. This change is not intended to have any user visible changes. The pointer comparison between `oid` and `&head_oid` checks that the caller did not pass an oid to be checked out. As no callers pass RESET_HEAD_RUN_POST_CHECKOUT_HOOK without passing an oid there are no changes to when the post-checkout hook is run. As update_ref() only updates the ref if the oid passed to it differs from the current ref there are no changes to when HEAD is updated. 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 1526d0fcfd20efca24bc96a4bc14c8d5459ec470
2 files changed +16 -10
builtin/rebase.c
+4 -6
@@ -585,8 +585,7 @@ static int move_to_original_branch(struct rebase_options *opts)
585 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);
588 + orig_head_reflog.buf, head_reflog.buf, NULL);
589
590 strbuf_release(&orig_head_reflog);
591 strbuf_release(&head_reflog);
@@ -822,7 +821,7 @@ static int checkout_up_to_date(struct rebase_options *options)
821 options->switch_to);
822 if (reset_head(the_repository, &options->orig_head,
823 options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
825 - NULL, buf.buf, DEFAULT_REFLOG_ACTION) < 0)
824 + NULL, buf.buf, NULL) < 0)
825 ret = error(_("could not switch to %s"), options->switch_to);
826 strbuf_release(&buf);
827
@@ -1273,7 +1272,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1272 string_list_clear(&merge_rr, 1);
1273
1274 if (reset_head(the_repository, NULL, NULL, RESET_HEAD_HARD,
1276 - NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1275 + NULL, NULL, NULL) < 0)
1276 die(_("could not discard worktree changes"));
1277 remove_branch_state(the_repository, 0);
1278 if (read_basic_state(&options))
@@ -1778,8 +1777,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1777 options.head_name ? options.head_name : "detached HEAD",
1778 oid_to_hex(&options.onto->object.oid));
1779 reset_head(the_repository, NULL, options.head_name,
1781 - RESET_HEAD_REFS_ONLY, "HEAD", msg.buf,
1782 - DEFAULT_REFLOG_ACTION);
1780 + RESET_HEAD_REFS_ONLY, "HEAD", msg.buf, NULL);
1781 strbuf_release(&msg);
1782 ret = finish_rebase(&options);
1783 goto cleanup;
reset.c
+12 -4
@@ -22,8 +22,13 @@ static int update_refs(const struct object_id *oid, const char *switch_to_branch
22 size_t prefix_len;
23 int ret;
24
25 - reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
26 - strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action);
25 + if ((update_orig_head && !reflog_orig_head) || !reflog_head) {
26 + if (!default_reflog_action)
27 + BUG("default_reflog_action must be given when reflog messages are omitted");
28 + reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
29 + strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action :
30 + default_reflog_action);
31 + }
32 prefix_len = msg.len;
33
34 if (update_orig_head) {
@@ -71,6 +76,7 @@ int reset_head(struct repository *r, struct object_id *oid,
76 {
77 unsigned reset_hard = flags & RESET_HEAD_HARD;
78 unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
79 + unsigned update_orig_head = flags & RESET_ORIG_HEAD;
80 struct object_id *head = NULL, head_oid;
81 struct tree_desc desc[2] = { { NULL }, { NULL } };
82 struct lock_file lock = LOCK_INIT;
@@ -144,8 +150,10 @@ int reset_head(struct repository *r, struct object_id *oid,
150 goto leave_reset_head;
151 }
152
147 - ret = update_refs(oid, switch_to_branch, head, reflog_head,
148 - reflog_orig_head, default_reflog_action, flags);
153 + if (oid != &head_oid || update_orig_head || switch_to_branch)
154 + ret = update_refs(oid, switch_to_branch, head, reflog_head,
155 + reflog_orig_head, default_reflog_action,
156 + flags);
157
158 leave_reset_head:
159 rollback_lock_file(&lock);