Skip to content

Commit bab6f80

Browse files
phillipwoodgitster
authored andcommitted
reset_head(): take struct rebase_head_opts
This function already takes a confusingly large number of parameters some of which are optional or not always required. The following commits will add a couple more parameters so change it to take a struct of options first. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb91588 commit bab6f80

File tree

4 files changed

+80
-47
lines changed

4 files changed

+80
-47
lines changed

builtin/rebase.c

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
779779
static int move_to_original_branch(struct rebase_options *opts)
780780
{
781781
struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
782+
struct reset_head_opts ropts = { 0 };
782783
int ret;
783784

784785
if (!opts->head_name)
@@ -791,9 +792,11 @@ static int move_to_original_branch(struct rebase_options *opts)
791792
opts->head_name, oid_to_hex(&opts->onto->object.oid));
792793
strbuf_addf(&head_reflog, "rebase finished: returning to %s",
793794
opts->head_name);
794-
ret = reset_head(the_repository, NULL, opts->head_name,
795-
RESET_HEAD_REFS_ONLY,
796-
orig_head_reflog.buf, head_reflog.buf, NULL);
795+
ropts.branch = opts->head_name;
796+
ropts.flags = RESET_HEAD_REFS_ONLY;
797+
ropts.orig_head_msg = orig_head_reflog.buf;
798+
ropts.head_msg = head_reflog.buf;
799+
ret = reset_head(the_repository, &ropts);
797800

798801
strbuf_release(&orig_head_reflog);
799802
strbuf_release(&head_reflog);
@@ -877,13 +880,15 @@ static int run_am(struct rebase_options *opts)
877880

878881
status = run_command(&format_patch);
879882
if (status) {
883+
struct reset_head_opts ropts = { 0 };
880884
unlink(rebased_patches);
881885
free(rebased_patches);
882886
strvec_clear(&am.args);
883887

884-
reset_head(the_repository, &opts->orig_head,
885-
opts->head_name, 0,
886-
NULL, NULL, DEFAULT_REFLOG_ACTION);
888+
ropts.oid = &opts->orig_head;
889+
ropts.branch = opts->head_name;
890+
ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
891+
reset_head(the_repository, &ropts);
887892
error(_("\ngit encountered an error while preparing the "
888893
"patches to replay\n"
889894
"these revisions:\n"
@@ -1103,14 +1108,17 @@ static int rebase_config(const char *var, const char *value, void *data)
11031108
static int checkout_up_to_date(struct rebase_options *options)
11041109
{
11051110
struct strbuf buf = STRBUF_INIT;
1111+
struct reset_head_opts ropts = { 0 };
11061112
int ret = 0;
11071113

11081114
strbuf_addf(&buf, "%s: checkout %s",
11091115
getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
11101116
options->switch_to);
1111-
if (reset_head(the_repository, &options->orig_head,
1112-
options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1113-
NULL, buf.buf, NULL) < 0)
1117+
ropts.oid = &options->orig_head;
1118+
ropts.branch = options->head_name;
1119+
ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
1120+
ropts.head_msg = buf.buf;
1121+
if (reset_head(the_repository, &ropts) < 0)
11141122
ret = error(_("could not switch to %s"), options->switch_to);
11151123
strbuf_release(&buf);
11161124

@@ -1321,6 +1329,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13211329
char *squash_onto_name = NULL;
13221330
int reschedule_failed_exec = -1;
13231331
int allow_preemptive_ff = 1;
1332+
struct reset_head_opts ropts = { 0 };
13241333
struct option builtin_rebase_options[] = {
13251334
OPT_STRING(0, "onto", &options.onto_name,
13261335
N_("revision"),
@@ -1560,9 +1569,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15601569

15611570
rerere_clear(the_repository, &merge_rr);
15621571
string_list_clear(&merge_rr, 1);
1563-
1564-
if (reset_head(the_repository, NULL, NULL, RESET_HEAD_HARD,
1565-
NULL, NULL, NULL) < 0)
1572+
ropts.flags = RESET_HEAD_HARD;
1573+
if (reset_head(the_repository, &ropts) < 0)
15661574
die(_("could not discard worktree changes"));
15671575
remove_branch_state(the_repository, 0);
15681576
if (read_basic_state(&options))
@@ -1579,9 +1587,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15791587

15801588
if (read_basic_state(&options))
15811589
exit(1);
1582-
if (reset_head(the_repository, &options.orig_head,
1583-
options.head_name, RESET_HEAD_HARD,
1584-
NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1590+
ropts.oid = &options.orig_head;
1591+
ropts.branch = options.head_name;
1592+
ropts.flags = RESET_HEAD_HARD;
1593+
ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
1594+
if (reset_head(the_repository, &ropts) < 0)
15851595
die(_("could not move back to %s"),
15861596
oid_to_hex(&options.orig_head));
15871597
remove_branch_state(the_repository, 0);
@@ -2068,10 +2078,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
20682078

20692079
strbuf_addf(&msg, "%s: checkout %s",
20702080
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
2071-
if (reset_head(the_repository, &options.onto->object.oid, NULL,
2072-
RESET_HEAD_DETACH | RESET_ORIG_HEAD |
2073-
RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
2074-
NULL, msg.buf, DEFAULT_REFLOG_ACTION))
2081+
ropts.oid = &options.onto->object.oid;
2082+
ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |
2083+
RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
2084+
ropts.head_msg = msg.buf;
2085+
ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
2086+
if (reset_head(the_repository, &ropts))
20752087
die(_("Could not detach HEAD"));
20762088
strbuf_release(&msg);
20772089

@@ -2086,8 +2098,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
20862098
strbuf_addf(&msg, "rebase finished: %s onto %s",
20872099
options.head_name ? options.head_name : "detached HEAD",
20882100
oid_to_hex(&options.onto->object.oid));
2089-
reset_head(the_repository, NULL, options.head_name,
2090-
RESET_HEAD_REFS_ONLY, NULL, msg.buf, NULL);
2101+
memset(&ropts, 0, sizeof(ropts));
2102+
ropts.branch = options.head_name;
2103+
ropts.flags = RESET_HEAD_REFS_ONLY;
2104+
ropts.head_msg = msg.buf;
2105+
reset_head(the_repository, &ropts);
20912106
strbuf_release(&msg);
20922107
ret = finish_rebase(&options);
20932108
goto cleanup;

reset.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
#include "tree.h"
99
#include "unpack-trees.h"
1010

11-
static int update_refs(const struct object_id *oid, const char *switch_to_branch,
12-
const char *reflog_head, const char *reflog_orig_head,
13-
const char *default_reflog_action, unsigned flags)
11+
static int update_refs(const struct reset_head_opts *opts,
12+
const struct object_id *oid)
1413
{
15-
unsigned detach_head = flags & RESET_HEAD_DETACH;
16-
unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
17-
unsigned update_orig_head = flags & RESET_ORIG_HEAD;
14+
unsigned detach_head = opts->flags & RESET_HEAD_DETACH;
15+
unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
16+
unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
17+
const char *switch_to_branch = opts->branch;
18+
const char *reflog_head = opts->head_msg;
19+
const char *reflog_orig_head = opts->orig_head_msg;
20+
const char *default_reflog_action = opts->default_reflog_action;
1821
struct object_id *orig = NULL, oid_orig, *old_orig = NULL, oid_old_orig;
1922
struct strbuf msg = STRBUF_INIT;
2023
const char *reflog_action;
@@ -69,14 +72,13 @@ static int update_refs(const struct object_id *oid, const char *switch_to_branch
6972
return ret;
7073
}
7174

72-
int reset_head(struct repository *r, struct object_id *oid,
73-
const char *switch_to_branch, unsigned flags,
74-
const char *reflog_orig_head, const char *reflog_head,
75-
const char *default_reflog_action)
75+
int reset_head(struct repository *r, const struct reset_head_opts *opts)
7676
{
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;
77+
const struct object_id *oid = opts->oid;
78+
const char *switch_to_branch = opts->branch;
79+
unsigned reset_hard = opts->flags & RESET_HEAD_HARD;
80+
unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;
81+
unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
8082
struct object_id head_oid;
8183
struct tree_desc desc[2] = { { NULL }, { NULL } };
8284
struct lock_file lock = LOCK_INIT;
@@ -102,9 +104,7 @@ int reset_head(struct repository *r, struct object_id *oid,
102104
oid = &head_oid;
103105

104106
if (refs_only)
105-
return update_refs(oid, switch_to_branch, reflog_head,
106-
reflog_orig_head, default_reflog_action,
107-
flags);
107+
return update_refs(opts, oid);
108108

109109
action = reset_hard ? "reset" : "checkout";
110110
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
@@ -148,9 +148,7 @@ int reset_head(struct repository *r, struct object_id *oid,
148148
}
149149

150150
if (oid != &head_oid || update_orig_head || switch_to_branch)
151-
ret = update_refs(oid, switch_to_branch, reflog_head,
152-
reflog_orig_head, default_reflog_action,
153-
flags);
151+
ret = update_refs(opts, oid);
154152

155153
leave_reset_head:
156154
rollback_lock_file(&lock);

reset.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,30 @@
1212
#define RESET_HEAD_REFS_ONLY (1<<3)
1313
#define RESET_ORIG_HEAD (1<<4)
1414

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);
15+
struct reset_head_opts {
16+
/* The oid of the commit to checkout/reset to. Defaults to HEAD */
17+
const struct object_id *oid;
18+
/* Optional branch to switch to */
19+
const char *branch;
20+
/* Flags defined above */
21+
unsigned flags;
22+
/*
23+
* Optional reflog message for HEAD, if this is not set then
24+
* default_reflog_action must be.
25+
*/
26+
const char *head_msg;
27+
/*
28+
* Optional reflog message for ORIG_HEAD, if this is not set and flags
29+
* contains RESET_ORIG_HEAD then default_reflog_action must be set.
30+
*/
31+
const char *orig_head_msg;
32+
/*
33+
* Action to use in default reflog messages, only required if a ref is
34+
* being updated and the reflog messages above are omitted.
35+
*/
36+
const char *default_reflog_action;
37+
};
38+
39+
int reset_head(struct repository *r, const struct reset_head_opts *opts);
1940

2041
#endif

sequencer.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4118,6 +4118,7 @@ void create_autostash(struct repository *r, const char *path)
41184118
if (has_unstaged_changes(r, 1) ||
41194119
has_uncommitted_changes(r, 1)) {
41204120
struct child_process stash = CHILD_PROCESS_INIT;
4121+
struct reset_head_opts ropts = { .flags = RESET_HEAD_HARD };
41214122
struct object_id oid;
41224123

41234124
strvec_pushl(&stash.args,
@@ -4139,10 +4140,8 @@ void create_autostash(struct repository *r, const char *path)
41394140
path);
41404141
write_file(path, "%s", oid_to_hex(&oid));
41414142
printf(_("Created autostash: %s\n"), buf.buf);
4142-
if (reset_head(r, NULL, NULL, RESET_HEAD_HARD, NULL, NULL,
4143-
NULL) < 0)
4143+
if (reset_head(r, &ropts) < 0)
41444144
die(_("could not reset --hard"));
4145-
41464145
if (discard_index(r->index) < 0 ||
41474146
repo_read_index(r) < 0)
41484147
die(_("could not read index"));

0 commit comments

Comments
 (0)