Skip to content

Commit 6ae8086

Browse files
phillipwoodgitster
authored andcommitted
reset_head(): take struct rebase_head_opts
This function takes a confusingly large number of parameters which makes it difficult to remember which order to pass them in. The following commits will add a couple more parameters which makes the problem worse. To address this change the function to take a struct of options. Using a struct means that it is no longer necessary to remember which order to pass the parameters in and anyone reading the code can easily see which value is passed to each parameter. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ee464c4 commit 6ae8086

File tree

4 files changed

+92
-48
lines changed

4 files changed

+92
-48
lines changed

builtin/rebase.c

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ static int finish_rebase(struct rebase_options *opts)
571571
static int move_to_original_branch(struct rebase_options *opts)
572572
{
573573
struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
574+
struct reset_head_opts ropts = { 0 };
574575
int ret;
575576

576577
if (!opts->head_name)
@@ -583,9 +584,11 @@ static int move_to_original_branch(struct rebase_options *opts)
583584
opts->head_name, oid_to_hex(&opts->onto->object.oid));
584585
strbuf_addf(&head_reflog, "rebase finished: returning to %s",
585586
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, NULL);
587+
ropts.branch = opts->head_name;
588+
ropts.flags = RESET_HEAD_REFS_ONLY;
589+
ropts.orig_head_msg = orig_head_reflog.buf;
590+
ropts.head_msg = head_reflog.buf;
591+
ret = reset_head(the_repository, &ropts);
589592

590593
strbuf_release(&orig_head_reflog);
591594
strbuf_release(&head_reflog);
@@ -669,13 +672,15 @@ static int run_am(struct rebase_options *opts)
669672

670673
status = run_command(&format_patch);
671674
if (status) {
675+
struct reset_head_opts ropts = { 0 };
672676
unlink(rebased_patches);
673677
free(rebased_patches);
674678
strvec_clear(&am.args);
675679

676-
reset_head(the_repository, &opts->orig_head,
677-
opts->head_name, 0,
678-
NULL, NULL, DEFAULT_REFLOG_ACTION);
680+
ropts.oid = &opts->orig_head;
681+
ropts.branch = opts->head_name;
682+
ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
683+
reset_head(the_repository, &ropts);
679684
error(_("\ngit encountered an error while preparing the "
680685
"patches to replay\n"
681686
"these revisions:\n"
@@ -814,14 +819,17 @@ static int rebase_config(const char *var, const char *value, void *data)
814819
static int checkout_up_to_date(struct rebase_options *options)
815820
{
816821
struct strbuf buf = STRBUF_INIT;
822+
struct reset_head_opts ropts = { 0 };
817823
int ret = 0;
818824

819825
strbuf_addf(&buf, "%s: checkout %s",
820826
getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
821827
options->switch_to);
822-
if (reset_head(the_repository, &options->orig_head,
823-
options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
824-
NULL, buf.buf, NULL) < 0)
828+
ropts.oid = &options->orig_head;
829+
ropts.branch = options->head_name;
830+
ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
831+
ropts.head_msg = buf.buf;
832+
if (reset_head(the_repository, &ropts) < 0)
825833
ret = error(_("could not switch to %s"), options->switch_to);
826834
strbuf_release(&buf);
827835

@@ -1033,6 +1041,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10331041
int reschedule_failed_exec = -1;
10341042
int allow_preemptive_ff = 1;
10351043
int preserve_merges_selected = 0;
1044+
struct reset_head_opts ropts = { 0 };
10361045
struct option builtin_rebase_options[] = {
10371046
OPT_STRING(0, "onto", &options.onto_name,
10381047
N_("revision"),
@@ -1270,9 +1279,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
12701279

12711280
rerere_clear(the_repository, &merge_rr);
12721281
string_list_clear(&merge_rr, 1);
1273-
1274-
if (reset_head(the_repository, NULL, NULL, RESET_HEAD_HARD,
1275-
NULL, NULL, NULL) < 0)
1282+
ropts.flags = RESET_HEAD_HARD;
1283+
if (reset_head(the_repository, &ropts) < 0)
12761284
die(_("could not discard worktree changes"));
12771285
remove_branch_state(the_repository, 0);
12781286
if (read_basic_state(&options))
@@ -1289,9 +1297,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
12891297

12901298
if (read_basic_state(&options))
12911299
exit(1);
1292-
if (reset_head(the_repository, &options.orig_head,
1293-
options.head_name, RESET_HEAD_HARD,
1294-
NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1300+
ropts.oid = &options.orig_head;
1301+
ropts.branch = options.head_name;
1302+
ropts.flags = RESET_HEAD_HARD;
1303+
ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
1304+
if (reset_head(the_repository, &ropts) < 0)
12951305
die(_("could not move back to %s"),
12961306
oid_to_hex(&options.orig_head));
12971307
remove_branch_state(the_repository, 0);
@@ -1758,10 +1768,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
17581768

17591769
strbuf_addf(&msg, "%s: checkout %s",
17601770
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
1761-
if (reset_head(the_repository, &options.onto->object.oid, NULL,
1762-
RESET_HEAD_DETACH | RESET_ORIG_HEAD |
1763-
RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1764-
NULL, msg.buf, DEFAULT_REFLOG_ACTION))
1771+
ropts.oid = &options.onto->object.oid;
1772+
ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |
1773+
RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
1774+
ropts.head_msg = msg.buf;
1775+
ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
1776+
if (reset_head(the_repository, &ropts))
17651777
die(_("Could not detach HEAD"));
17661778
strbuf_release(&msg);
17671779

@@ -1776,8 +1788,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
17761788
strbuf_addf(&msg, "rebase finished: %s onto %s",
17771789
options.head_name ? options.head_name : "detached HEAD",
17781790
oid_to_hex(&options.onto->object.oid));
1779-
reset_head(the_repository, NULL, options.head_name,
1780-
RESET_HEAD_REFS_ONLY, NULL, msg.buf, NULL);
1791+
memset(&ropts, 0, sizeof(ropts));
1792+
ropts.branch = options.head_name;
1793+
ropts.flags = RESET_HEAD_REFS_ONLY;
1794+
ropts.head_msg = msg.buf;
1795+
reset_head(the_repository, &ropts);
17811796
strbuf_release(&msg);
17821797
ret = finish_rebase(&options);
17831798
goto cleanup;

reset.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
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 struct object_id *head, const char *reflog_head,
13-
const char *reflog_orig_head,
14-
const char *default_reflog_action, unsigned flags)
11+
static int update_refs(const struct reset_head_opts *opts,
12+
const struct object_id *oid,
13+
const struct object_id *head)
1514
{
16-
unsigned detach_head = flags & RESET_HEAD_DETACH;
17-
unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
18-
unsigned update_orig_head = flags & RESET_ORIG_HEAD;
15+
unsigned detach_head = opts->flags & RESET_HEAD_DETACH;
16+
unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
17+
unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
18+
const char *switch_to_branch = opts->branch;
19+
const char *reflog_head = opts->head_msg;
20+
const char *reflog_orig_head = opts->orig_head_msg;
21+
const char *default_reflog_action = opts->default_reflog_action;
1922
struct object_id *old_orig = NULL, oid_old_orig;
2023
struct strbuf msg = STRBUF_INIT;
2124
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 = NULL, head_oid;
8183
struct tree_desc desc[2] = { { NULL }, { NULL } };
8284
struct lock_file lock = LOCK_INIT;
@@ -104,9 +106,7 @@ int reset_head(struct repository *r, struct object_id *oid,
104106
oid = &head_oid;
105107

106108
if (refs_only)
107-
return update_refs(oid, switch_to_branch, head, reflog_head,
108-
reflog_orig_head, default_reflog_action,
109-
flags);
109+
return update_refs(opts, oid, head);
110110

111111
action = reset_hard ? "reset" : "checkout";
112112
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
@@ -151,9 +151,7 @@ int reset_head(struct repository *r, struct object_id *oid,
151151
}
152152

153153
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);
154+
ret = update_refs(opts, oid, head);
157155

158156
leave_reset_head:
159157
rollback_lock_file(&lock);

reset.h

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,47 @@
66

77
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
88

9+
/* Request a detached checkout */
910
#define RESET_HEAD_DETACH (1<<0)
11+
/* Request a reset rather than a checkout */
1012
#define RESET_HEAD_HARD (1<<1)
13+
/* Run the post-checkout hook */
1114
#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
15+
/* Only update refs, do not touch the worktree */
1216
#define RESET_HEAD_REFS_ONLY (1<<3)
17+
/* Update ORIG_HEAD as well as HEAD */
1318
#define RESET_ORIG_HEAD (1<<4)
1419

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);
20+
struct reset_head_opts {
21+
/*
22+
* The commit to checkout/reset to. Defaults to HEAD.
23+
*/
24+
const struct object_id *oid;
25+
/*
26+
* Optional branch to switch to.
27+
*/
28+
const char *branch;
29+
/*
30+
* Flags defined above.
31+
*/
32+
unsigned flags;
33+
/*
34+
* Optional reflog message for HEAD, if this omitted but oid or branch
35+
* are given then default_reflog_action must be given.
36+
*/
37+
const char *head_msg;
38+
/*
39+
* Optional reflog message for ORIG_HEAD, if this omitted and flags
40+
* contains RESET_ORIG_HEAD then default_reflog_action must be given.
41+
*/
42+
const char *orig_head_msg;
43+
/*
44+
* Action to use in default reflog messages, only required if a ref is
45+
* being updated and the reflog messages above are omitted.
46+
*/
47+
const char *default_reflog_action;
48+
};
49+
50+
int reset_head(struct repository *r, const struct reset_head_opts *opts);
1951

2052
#endif

sequencer.c

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

41204121
strvec_pushl(&stash.args,
@@ -4136,10 +4137,8 @@ void create_autostash(struct repository *r, const char *path)
41364137
path);
41374138
write_file(path, "%s", oid_to_hex(&oid));
41384139
printf(_("Created autostash: %s\n"), buf.buf);
4139-
if (reset_head(r, NULL, NULL, RESET_HEAD_HARD, NULL, NULL,
4140-
NULL) < 0)
4140+
if (reset_head(r, &ropts) < 0)
41414141
die(_("could not reset --hard"));
4142-
41434142
if (discard_index(r->index) < 0 ||
41444143
repo_read_index(r) < 0)
41454144
die(_("could not read index"));

0 commit comments

Comments
 (0)