reset: modernize flags passed to `reset_working_tree()`
The flags passed to `reset_working_tree()` are declared as defines. This has fallen a bit out of practice nowadays, where we instead prefer to use enums. Furthermore, the prefix of those flags does not match the function name anymore after the rename in the preceding commit. Adapt the code to follow modern best practices and adapt the flag names. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jul 1, 2026 at 13:35 UTC
2535b951e07ac0da0d1c6c06bd76effe879c18a4
4 files changed
+39
-28
builtin/rebase.c
+8
-7
@@ -607,7 +607,7 @@ static int move_to_original_branch(struct rebase_options *opts)
607
strbuf_addf(&head_reflog, "%s (finish): returning to %s",
608
opts->reflog_action, opts->head_name);
609
ropts.branch = opts->head_name;
610
- ropts.flags = RESET_HEAD_REFS_ONLY;
610
+ ropts.flags = RESET_WORKING_TREE_REFS_ONLY;
611
ropts.branch_msg = branch_reflog.buf;
612
ropts.head_msg = head_reflog.buf;
613
ret = reset_working_tree(the_repository, &ropts);
@@ -862,9 +862,9 @@ static int checkout_up_to_date(struct rebase_options *options)
862
options->reflog_action, options->switch_to);
863
ropts.oid = &options->orig_head->object.oid;
864
ropts.branch = options->head_name;
865
- ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
865
+ ropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;
866
if (!ropts.branch)
867
- ropts.flags |= RESET_HEAD_DETACH;
867
+ ropts.flags |= RESET_WORKING_TREE_DETACH;
868
ropts.head_msg = buf.buf;
869
if (reset_working_tree(the_repository, &ropts) < 0)
870
ret = error(_("could not switch to %s"), options->switch_to);
@@ -1384,7 +1384,7 @@ int cmd_rebase(int argc,
1384
1385
rerere_clear(the_repository, &merge_rr);
1386
string_list_clear(&merge_rr, 1);
1387
- ropts.flags = RESET_HEAD_HARD;
1387
+ ropts.flags = RESET_WORKING_TREE_HARD;
1388
if (reset_working_tree(the_repository, &ropts) < 0)
1389
die(_("could not discard worktree changes"));
1390
remove_branch_state(the_repository, 0);
@@ -1409,7 +1409,7 @@ int cmd_rebase(int argc,
1409
ropts.oid = &options.orig_head->object.oid;
1410
ropts.head_msg = head_msg.buf;
1411
ropts.branch = options.head_name;
1412
- ropts.flags = RESET_HEAD_HARD;
1412
+ ropts.flags = RESET_WORKING_TREE_HARD;
1413
if (reset_working_tree(the_repository, &ropts) < 0)
1414
die(_("could not move back to %s"),
1415
oid_to_hex(&options.orig_head->object.oid));
@@ -1876,8 +1876,9 @@ int cmd_rebase(int argc,
1876
options.reflog_action, options.onto_name);
1877
ropts.oid = &options.onto->object.oid;
1878
ropts.orig_head = &options.orig_head->object.oid;
1879
- ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |
1880
- RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
1879
+ ropts.flags = RESET_WORKING_TREE_DETACH |
1880
+ RESET_WORKING_TREE_UPDATE_ORIG_HEAD |
1881
+ RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;
1882
ropts.head_msg = msg.buf;
1883
ropts.default_reflog_action = options.reflog_action;
1884
if (reset_working_tree(the_repository, &ropts)) {
reset.c
+6
-6
@@ -16,9 +16,9 @@ static int update_refs(struct repository *repo,
16
const struct object_id *oid,
17
const struct object_id *head)
18
{
19
- unsigned detach_head = opts->flags & RESET_HEAD_DETACH;
20
- unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
21
- unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
19
+ unsigned detach_head = opts->flags & RESET_WORKING_TREE_DETACH;
20
+ unsigned run_hook = opts->flags & RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;
21
+ unsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;
22
const struct object_id *orig_head = opts->orig_head;
23
const char *switch_to_branch = opts->branch;
24
const char *reflog_branch = opts->branch_msg;
@@ -90,9 +90,9 @@ int reset_working_tree(struct repository *r,
90
{
91
const struct object_id *oid = opts->oid;
92
const char *switch_to_branch = opts->branch;
93
- unsigned reset_hard = opts->flags & RESET_HEAD_HARD;
94
- unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;
95
- unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
93
+ unsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;
94
+ unsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;
95
+ unsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;
96
struct object_id *head = NULL, head_oid;
97
struct tree_desc desc[2] = { { NULL }, { NULL } };
98
struct lock_file lock = LOCK_INIT;
reset.h
+19
-12
@@ -6,16 +6,22 @@
6
7
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
8
9
-/* Request a detached checkout */
10
-#define RESET_HEAD_DETACH (1<<0)
11
-/* Request a reset rather than a checkout */
12
-#define RESET_HEAD_HARD (1<<1)
13
-/* Run the post-checkout hook */
14
-#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
15
-/* Only update refs, do not touch the worktree */
16
-#define RESET_HEAD_REFS_ONLY (1<<3)
17
-/* Update ORIG_HEAD as well as HEAD */
18
-#define RESET_ORIG_HEAD (1<<4)
9
+enum reset_working_tree_flags {
10
+ /* Request a detached checkout */
11
+ RESET_WORKING_TREE_DETACH = (1 << 0),
12
+
13
+ /* Request a reset rather than a checkout */
14
+ RESET_WORKING_TREE_HARD = (1 << 1),
15
+
16
+ /* Run the post-checkout hook */
17
+ RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK = (1 << 2),
18
+
19
+ /* Only update refs, do not touch the worktree */
20
+ RESET_WORKING_TREE_REFS_ONLY = (1 << 3),
21
+
22
+ /* Update ORIG_HEAD as well as HEAD */
23
+ RESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),
24
+};
25
26
struct reset_working_tree_options {
27
/*
@@ -33,7 +39,7 @@ struct reset_working_tree_options {
39
/*
40
* Flags defined above.
41
*/
36
- unsigned flags;
42
+ enum reset_working_tree_flags flags;
43
/*
44
* Optional reflog message for branch, defaults to head_msg.
45
*/
@@ -45,7 +51,8 @@ struct reset_working_tree_options {
51
const char *head_msg;
52
/*
53
* Optional reflog message for ORIG_HEAD, if this omitted and flags
48
- * contains RESET_ORIG_HEAD then default_reflog_action must be given.
54
+ * contains RESET_WORKING_TREE_UPDATE_ORIG_HEAD then
55
+ * default_reflog_action must be given.
56
*/
57
const char *orig_head_msg;
58
/*
sequencer.c
+6
-3
@@ -4677,7 +4677,9 @@ static void create_autostash_internal(struct repository *r,
4677
if (has_unstaged_changes(r, 1) ||
4678
has_uncommitted_changes(r, 1)) {
4679
struct child_process stash = CHILD_PROCESS_INIT;
4680
- struct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };
4680
+ struct reset_working_tree_options ropts = {
4681
+ .flags = RESET_WORKING_TREE_HARD,
4682
+ };
4683
struct object_id oid;
4684
4685
strvec_pushl(&stash.args,
@@ -4870,8 +4872,9 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,
4872
struct reset_working_tree_options ropts = {
4873
.oid = onto,
4874
.orig_head = orig_head,
4873
- .flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |
4874
- RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
4875
+ .flags = RESET_WORKING_TREE_DETACH |
4876
+ RESET_WORKING_TREE_UPDATE_ORIG_HEAD |
4877
+ RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK,
4878
.head_msg = reflog_message(opts, "start", "checkout %s",
4879
onto_name),
4880
.default_reflog_action = sequencer_reflog_action(opts)