git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] builtin/revert.c: refactor using an enum for cmd-action
@ 2024-01-11  8:04 Michael Lohmann
  2024-01-11 16:57 ` Phillip Wood
  0 siblings, 1 reply; 11+ messages in thread
From: Michael Lohmann @ 2024-01-11  8:04 UTC (permalink / raw)
  To: git; +Cc: Michael Lohmann, Wanja Henze

This is done to avoid having to keep the char values in sync in
different places and also to get compiler warnings on non-exhaustive
switches.

The newly introduced `revert_action`-enum aligns with the
builtin/rebase.c `action`-enum though the name `revert_action` is chosen
to better differentiate it from `replay_opts->action` with a different
function. For rebase the equivalent of the latter is
`rebase_options->type` and `rebase_options->action` corresponds to the
`cmd` variable in revert.c.

In the rebase `action` enum there is the enumeration constant
`ACTION_NONE` which is not particularly descriptive, since it seems to
imply that no action should be taken. Instead it signals a start of a
revert/cherry-pick process, so here `REVERT_ACTION_START` was chosen.

Co-authored-by: Wanja Henze <wanja.hentze@bevuta.com>
Signed-off-by: Michael Lohmann <mi.al.lohmann@gmail.com>
---
Hello!

When I was working on `revert/cherry-pick --show-current-patch` (still
needs a little bit more discussion, if actually wanted, see thread [1])
I found the construct with the `cmd` as an int a bit irritating. I hope
this patch makes it more obvious what is actually going on.

Is there a reason why `ACTION_NONE` was chosen as a name in
builtin/rebase.c? My best guess is that it came along because it is the
implied action when no other specific action is passed in, but I don't
find that particularly descriptive on what its actual function is...
(Yes, naming things is hard... :D)

An alternative to prefixing the enum name with "revert_" would be to
rename `replay_opts->action` to `replay_opts->type` so it aligns with
rebase. Would you prefer that instead?

Cheers
Michael

[1] https://lore.kernel.org/git/20231218121048.68290-1-mi.al.lohmann@gmail.com/

 builtin/revert.c | 80 +++++++++++++++++++++++++++++-------------------
 1 file changed, 49 insertions(+), 31 deletions(-)

diff --git a/builtin/revert.c b/builtin/revert.c
index 89821bab95..b5278b7a3b 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -20,6 +20,14 @@
  * Copyright (c) 2005 Junio C Hamano
  */
 
+enum revert_action {
+	REVERT_ACTION_START = 0,
+	REVERT_ACTION_CONTINUE,
+	REVERT_ACTION_SKIP,
+	REVERT_ACTION_ABORT,
+	REVERT_ACTION_QUIT,
+};
+
 static const char * const revert_usage[] = {
 	N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
 	N_("git revert (--continue | --skip | --abort | --quit)"),
@@ -85,12 +93,12 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 	const char * const * usage_str = revert_or_cherry_pick_usage(opts);
 	const char *me = action_name(opts);
 	const char *cleanup_arg = NULL;
-	int cmd = 0;
+	enum revert_action cmd = REVERT_ACTION_START;
 	struct option base_options[] = {
-		OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
-		OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
-		OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
-		OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
+		OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), REVERT_ACTION_QUIT),
+		OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), REVERT_ACTION_CONTINUE),
+		OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), REVERT_ACTION_ABORT),
+		OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), REVERT_ACTION_SKIP),
 		OPT_CLEANUP(&cleanup_arg),
 		OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
 		OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
@@ -144,30 +152,37 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 	}
 
 	/* Check for incompatible command line arguments */
-	if (cmd) {
-		char *this_operation;
-		if (cmd == 'q')
+	{
+		char *this_operation = 0;
+		switch (cmd) {
+		case REVERT_ACTION_START:
+			break;
+		case REVERT_ACTION_QUIT:
 			this_operation = "--quit";
-		else if (cmd == 'c')
+			break;
+		case REVERT_ACTION_CONTINUE:
 			this_operation = "--continue";
-		else if (cmd == 's')
+			break;
+		case REVERT_ACTION_SKIP:
 			this_operation = "--skip";
-		else {
-			assert(cmd == 'a');
+			break;
+		case REVERT_ACTION_ABORT:
 			this_operation = "--abort";
+			break;
 		}
 
-		verify_opt_compatible(me, this_operation,
-				"--no-commit", opts->no_commit,
-				"--signoff", opts->signoff,
-				"--mainline", opts->mainline,
-				"--strategy", opts->strategy ? 1 : 0,
-				"--strategy-option", opts->xopts.nr ? 1 : 0,
-				"-x", opts->record_origin,
-				"--ff", opts->allow_ff,
-				"--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
-				"--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
-				NULL);
+		if (this_operation)
+			verify_opt_compatible(me, this_operation,
+					"--no-commit", opts->no_commit,
+					"--signoff", opts->signoff,
+					"--mainline", opts->mainline,
+					"--strategy", opts->strategy ? 1 : 0,
+					"--strategy-option", opts->xopts.nr ? 1 : 0,
+					"-x", opts->record_origin,
+					"--ff", opts->allow_ff,
+					"--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
+					"--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
+					NULL);
 	}
 
 	if (!opts->strategy && opts->default_strategy) {
@@ -183,9 +198,7 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 				"--edit", opts->edit > 0,
 				NULL);
 
-	if (cmd) {
-		opts->revs = NULL;
-	} else {
+	if (cmd == REVERT_ACTION_START) {
 		struct setup_revision_opt s_r_opt;
 		opts->revs = xmalloc(sizeof(*opts->revs));
 		repo_init_revisions(the_repository, opts->revs, NULL);
@@ -198,6 +211,8 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 		memset(&s_r_opt, 0, sizeof(s_r_opt));
 		s_r_opt.assume_dashdash = 1;
 		argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
+	} else {
+		opts->revs = NULL;
 	}
 
 	if (argc > 1)
@@ -210,19 +225,22 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 		opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
 	free(options);
 
-	if (cmd == 'q') {
+	switch (cmd) {
+	case REVERT_ACTION_QUIT: {
 		int ret = sequencer_remove_state(opts);
 		if (!ret)
 			remove_branch_state(the_repository, 0);
 		return ret;
 	}
-	if (cmd == 'c')
+	case REVERT_ACTION_CONTINUE:
 		return sequencer_continue(the_repository, opts);
-	if (cmd == 'a')
+	case REVERT_ACTION_ABORT:
 		return sequencer_rollback(the_repository, opts);
-	if (cmd == 's')
+	case REVERT_ACTION_SKIP:
 		return sequencer_skip(the_repository, opts);
-	return sequencer_pick_revisions(the_repository, opts);
+	case REVERT_ACTION_START:
+		return sequencer_pick_revisions(the_repository, opts);
+	}
 }
 
 int cmd_revert(int argc, const char **argv, const char *prefix)
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-11  8:04 [PATCH] builtin/revert.c: refactor using an enum for cmd-action Michael Lohmann
@ 2024-01-11 16:57 ` Phillip Wood
  2024-01-11 17:47   ` [PATCH v2] " Michael Lohmann
  2024-01-11 19:37   ` [PATCH] " Junio C Hamano
  0 siblings, 2 replies; 11+ messages in thread
From: Phillip Wood @ 2024-01-11 16:57 UTC (permalink / raw)
  To: Michael Lohmann, git; +Cc: Wanja Henze

Hi Michael

On 11/01/2024 08:04, Michael Lohmann wrote:
> This is done to avoid having to keep the char values in sync in
> different places and also to get compiler warnings on non-exhaustive
> switches.

I think this is a reasonable change, thanks for working on it.

> The newly introduced `revert_action`-enum aligns with the
> builtin/rebase.c `action`-enum though the name `revert_action` is chosen
> to better differentiate it from `replay_opts->action` with a different
> function. For rebase the equivalent of the latter is
> `rebase_options->type` and `rebase_options->action` corresponds to the
> `cmd` variable in revert.c.
> 
> In the rebase `action` enum there is the enumeration constant
> `ACTION_NONE` which is not particularly descriptive, since it seems to
> imply that no action should be taken. Instead it signals a start of a
> revert/cherry-pick process, so here `REVERT_ACTION_START` was chosen.

I think ACTION_NONE was intended to covey that the user did not pass one 
of the OPT_CMDMODE() options like "--continue" as there isn't a 
"--start" option. I don't have a strong opinion between "_NONE" and 
"_START".

> +enum revert_action {
> +	REVERT_ACTION_START = 0,
> +	REVERT_ACTION_CONTINUE,
> +	REVERT_ACTION_SKIP,
> +	REVERT_ACTION_ABORT,
> +	REVERT_ACTION_QUIT,
> +};

The "REVERT_" prefix is a bit unfortunate as this is used by cherry-pick 
as well but it does match the filename. As this enum is only used in 
this file I'd be quite happy to drop the "REVERT_" prefix. (I don't 
think we need to go messing with the "action" member of struct 
replay_opts to do that)

>   	/* Check for incompatible command line arguments */
> -	if (cmd) {
> -		char *this_operation;
> -		if (cmd == 'q')
> +	{
> +		char *this_operation = 0;

style note: we use "NULL" rather than "0" when initializing pointers. 
Ideally this would be a "const char *" as we assign string literals but 
that is not a new problem with this patch.

> +		switch (cmd) {
> +		case REVERT_ACTION_START:
> +			break;

I can see the attraction of using an exhaustive switch() here but as we 
don't want to do anything in the _START case it gets a bit messy with 
the extra conditional below. I wonder if we'd be better to replace "if 
(cmd) {" with "if (cmd != REVERT_ACTION_START) {". Alternatively if you 
want to stick with the switch then declaring "this_operation" at the 
beginning of the function would mean you can get rid of the new "{}" block.

> +		case REVERT_ACTION_QUIT:
>   			this_operation = "--quit";
> -		else if (cmd == 'c')
> +			break;
> +		case REVERT_ACTION_CONTINUE:
>   			this_operation = "--continue";
> -		else if (cmd == 's')
> +			break;
> +		case REVERT_ACTION_SKIP:
>   			this_operation = "--skip";
> -		else {
> -			assert(cmd == 'a'); > +			break;
> +		case REVERT_ACTION_ABORT:
>   			this_operation = "--abort";
> +			break;
>   		}
>   
> -		verify_opt_compatible(me, this_operation,
> -				"--no-commit", opts->no_commit,
> -				"--signoff", opts->signoff,
> -				"--mainline", opts->mainline,
> -				"--strategy", opts->strategy ? 1 : 0,
> -				"--strategy-option", opts->xopts.nr ? 1 : 0,
> -				"-x", opts->record_origin,
> -				"--ff", opts->allow_ff,
> -				"--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
> -				"--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
> -				NULL);
> +		if (this_operation)

The extra indentation here is unfortunate as some of the lines are 
rather long already. In the current code it is clear that we only call 
verify_opt_compatible() when cmd is non-nul, I think it would be clearer 
to use "if (cmd != REVERT_ACTION_START)" here.

> +			verify_opt_compatible(me, this_operation,
> +					"--no-commit", opts->no_commit,
> +					"--signoff", opts->signoff,
> +					"--mainline", opts->mainline,
> +					"--strategy", opts->strategy ? 1 : 0,
> +					"--strategy-option", opts->xopts.nr ? 1 : 0,
> +					"-x", opts->record_origin,
> +					"--ff", opts->allow_ff,
> +					"--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
> +					"--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
> +					NULL);
>   	}
 > [...]
> -	if (cmd) {
> -		opts->revs = NULL;
> -	} else {
> +	if (cmd == REVERT_ACTION_START) {

I was momentarily confused by this change but you're reversing the 
conditional. I agree that the result is an improvement.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-11 16:57 ` Phillip Wood
@ 2024-01-11 17:47   ` Michael Lohmann
  2024-01-11 19:50     ` Junio C Hamano
  2024-01-11 19:37   ` [PATCH] " Junio C Hamano
  1 sibling, 1 reply; 11+ messages in thread
From: Michael Lohmann @ 2024-01-11 17:47 UTC (permalink / raw)
  To: phillip.wood123; +Cc: git, mi.al.lohmann, phillip.wood, wanja.hentze

This is done to avoid having to keep the char values in sync in
different places and also to get compiler warnings on non-exhaustive
switches.

In the rebase `action` enum there is the enumeration constant
`ACTION_NONE` which is not particularly descriptive, since it seems to
imply that no action should be taken. Instead it signals a start of a
revert/cherry-pick process, so here `ACTION_START` was chosen.

Co-authored-by: Wanja Henze <wanja.hentze@bevuta.com>
Signed-off-by: Michael Lohmann <mi.al.lohmann@gmail.com>
---
Hi Phillip!

Thanks for the feedback!

On 11/01/2024 16:57, Phillip Wood wrote:
> I can see the attraction of using an exhaustive switch() here but as
> we don't want to do anything in the _START case it gets a bit messy
> with the extra conditional below. I wonder if we'd be better to
> replace "if (cmd) {" with "if (cmd != REVERT_ACTION_START) {".
> Alternatively if you want to stick with the switch then declaring
> "this_operation" at the beginning of the function would mean you can
> get rid of the new "{}" block.

> The extra indentation here is unfortunate as some of the lines are
> rather long already. In the current code it is clear that we only call
> verify_opt_compatible() when cmd is non-nul, I think it would be
> clearer to use "if (cmd != REVERT_ACTION_START)" here.

Totally agreed - an alternative to the `if` would be a `goto` (see this
version of the patch). This would keep the benefit of the exhaustive
switch, but I don't know if that would fit the style used in this
project... (at least it is a jump forward...)

Changes compared to the previous patch:
- initialize `this_operation` pointer with NULL instead of 0
- drop "REVERT_" prefix of enum and its members
- declare `this_operation` at the toplevel to get rid of codeblock
- skip the verify_opt_compatible in case of ACTION_START with a `goto`

Best wishes
Michael

 builtin/revert.c | 90 ++++++++++++++++++++++++++++--------------------
 1 file changed, 53 insertions(+), 37 deletions(-)

diff --git a/builtin/revert.c b/builtin/revert.c
index 89821bab95..19e6653f99 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -20,6 +20,14 @@
  * Copyright (c) 2005 Junio C Hamano
  */
 
+enum action {
+	ACTION_START = 0,
+	ACTION_CONTINUE,
+	ACTION_SKIP,
+	ACTION_ABORT,
+	ACTION_QUIT,
+};
+
 static const char * const revert_usage[] = {
 	N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
 	N_("git revert (--continue | --skip | --abort | --quit)"),
@@ -85,12 +93,13 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 	const char * const * usage_str = revert_or_cherry_pick_usage(opts);
 	const char *me = action_name(opts);
 	const char *cleanup_arg = NULL;
-	int cmd = 0;
+	char *this_operation = NULL;
+	enum action cmd = ACTION_START;
 	struct option base_options[] = {
-		OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
-		OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
-		OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
-		OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
+		OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), ACTION_QUIT),
+		OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), ACTION_CONTINUE),
+		OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), ACTION_ABORT),
+		OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), ACTION_SKIP),
 		OPT_CLEANUP(&cleanup_arg),
 		OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
 		OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
@@ -144,32 +153,36 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 	}
 
 	/* Check for incompatible command line arguments */
-	if (cmd) {
-		char *this_operation;
-		if (cmd == 'q')
-			this_operation = "--quit";
-		else if (cmd == 'c')
-			this_operation = "--continue";
-		else if (cmd == 's')
-			this_operation = "--skip";
-		else {
-			assert(cmd == 'a');
-			this_operation = "--abort";
-		}
-
-		verify_opt_compatible(me, this_operation,
-				"--no-commit", opts->no_commit,
-				"--signoff", opts->signoff,
-				"--mainline", opts->mainline,
-				"--strategy", opts->strategy ? 1 : 0,
-				"--strategy-option", opts->xopts.nr ? 1 : 0,
-				"-x", opts->record_origin,
-				"--ff", opts->allow_ff,
-				"--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
-				"--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
-				NULL);
+	switch (cmd) {
+	case ACTION_START:
+		goto skip_opt_compatibility_verification;
+	case ACTION_QUIT:
+		this_operation = "--quit";
+		break;
+	case ACTION_CONTINUE:
+		this_operation = "--continue";
+		break;
+	case ACTION_SKIP:
+		this_operation = "--skip";
+		break;
+	case ACTION_ABORT:
+		this_operation = "--abort";
+		break;
 	}
 
+	verify_opt_compatible(me, this_operation,
+			"--no-commit", opts->no_commit,
+			"--signoff", opts->signoff,
+			"--mainline", opts->mainline,
+			"--strategy", opts->strategy ? 1 : 0,
+			"--strategy-option", opts->xopts.nr ? 1 : 0,
+			"-x", opts->record_origin,
+			"--ff", opts->allow_ff,
+			"--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
+			"--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
+			NULL);
+
+skip_opt_compatibility_verification:
 	if (!opts->strategy && opts->default_strategy) {
 		opts->strategy = opts->default_strategy;
 		opts->default_strategy = NULL;
@@ -183,9 +196,7 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 				"--edit", opts->edit > 0,
 				NULL);
 
-	if (cmd) {
-		opts->revs = NULL;
-	} else {
+	if (cmd == ACTION_START) {
 		struct setup_revision_opt s_r_opt;
 		opts->revs = xmalloc(sizeof(*opts->revs));
 		repo_init_revisions(the_repository, opts->revs, NULL);
@@ -198,6 +209,8 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 		memset(&s_r_opt, 0, sizeof(s_r_opt));
 		s_r_opt.assume_dashdash = 1;
 		argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
+	} else {
+		opts->revs = NULL;
 	}
 
 	if (argc > 1)
@@ -210,19 +223,22 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 		opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
 	free(options);
 
-	if (cmd == 'q') {
+	switch (cmd) {
+	case ACTION_QUIT: {
 		int ret = sequencer_remove_state(opts);
 		if (!ret)
 			remove_branch_state(the_repository, 0);
 		return ret;
 	}
-	if (cmd == 'c')
+	case ACTION_CONTINUE:
 		return sequencer_continue(the_repository, opts);
-	if (cmd == 'a')
+	case ACTION_ABORT:
 		return sequencer_rollback(the_repository, opts);
-	if (cmd == 's')
+	case ACTION_SKIP:
 		return sequencer_skip(the_repository, opts);
-	return sequencer_pick_revisions(the_repository, opts);
+	case ACTION_START:
+		return sequencer_pick_revisions(the_repository, opts);
+	}
 }
 
 int cmd_revert(int argc, const char **argv, const char *prefix)
-- 
2.39.3 (Apple Git-145)


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-11 16:57 ` Phillip Wood
  2024-01-11 17:47   ` [PATCH v2] " Michael Lohmann
@ 2024-01-11 19:37   ` Junio C Hamano
  1 sibling, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2024-01-11 19:37 UTC (permalink / raw)
  To: Phillip Wood; +Cc: Michael Lohmann, git, Wanja Henze

Phillip Wood <phillip.wood123@gmail.com> writes:

> I think ACTION_NONE was intended to covey that the user did not pass
> one of the OPT_CMDMODE() options like "--continue" as there isn't a
> "--start" option. I don't have a strong opinion between "_NONE" and
> "_START".

I agree with you why NONE is called as such.  If "revert" does not
take "--start" (I do not remember offhand), I would think it would
be better to follow suit.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-11 17:47   ` [PATCH v2] " Michael Lohmann
@ 2024-01-11 19:50     ` Junio C Hamano
  2024-01-11 20:06       ` [PATCH v3] " Michael Lohmann
  0 siblings, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2024-01-11 19:50 UTC (permalink / raw)
  To: Michael Lohmann; +Cc: phillip.wood123, git, phillip.wood, wanja.hentze

Michael Lohmann <mi.al.lohmann@gmail.com> writes:

>  	struct option base_options[] = {
> -		OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
> -		OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
> -		OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
> -		OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
> +		OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), ACTION_QUIT),
> +		OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), ACTION_CONTINUE),
> +		OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), ACTION_ABORT),
> +		OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), ACTION_SKIP),
>  		OPT_CLEANUP(&cleanup_arg),
>  		OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
>  		OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),

I actually do not terribly mind reusing the single letter option
(e.g. 'x' in "-x") and the command mode, as it is descriptive
enough.  The argument that an enum allows compilers warn about
non-exhausitve switches is valid if we have such a switch.

> +	switch (cmd) {
> +	case ACTION_START:
> +		goto skip_opt_compatibility_verification;
> +	case ACTION_QUIT:
> +		this_operation = "--quit";
> +		break;
> +	case ACTION_CONTINUE:
> +		this_operation = "--continue";
> +		break;
> +	case ACTION_SKIP:
> +		this_operation = "--skip";
> +		break;
> +	case ACTION_ABORT:
> +		this_operation = "--abort";
> +		break;
>  	}
>  
> +	verify_opt_compatible(me, this_operation,

And indeed the if/elseif cascade in the original is easier to ensure
its exhaustiveness by turning it into a switch.

HOWEVER.

If I were writing this patch, I would rather do it like so:

	this_operation = NULL;
	switch (cmd) {
	case 'q':
		this_operation = '--quit";
                break;
	...
	case 'a':
		this_operation = '--abort";
                break;
	default: /* everything else is compatible with others */
		break;
	}
	if (this_operation)
		verify_opt_compatible(me, this_operation, ...);

Use of enum is optional; I just didn't like too much churning to
illustrate a single idea (here, the single idea being "switch is
more appropriate then if/else cascade in this case"), and I think
this is easier to read than with enums [*].

	Side note: After all the single letter option names are
	meant to be mnemonic.  "case 'q'" is just as descriptive as
	"case ACTION_QUIT" in the context of this switch statement.

HTH.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-11 19:50     ` Junio C Hamano
@ 2024-01-11 20:06       ` Michael Lohmann
  2024-01-11 21:47         ` Junio C Hamano
                           ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Michael Lohmann @ 2024-01-11 20:06 UTC (permalink / raw)
  To: gitster; +Cc: git, mi.al.lohmann, phillip.wood123, phillip.wood, wanja.hentze

This is done to avoid having to keep the char values in sync in
different places and also to get compiler warnings on non-exhaustive
switches.

In the rebase `action` enum there is the enumeration constant
`ACTION_NONE` which is not particularly descriptive, since it seems to
imply that no action should be taken. Instead it signals a start of a
revert/cherry-pick process, so here `ACTION_START` was chosen.

Co-authored-by: Wanja Henze <wanja.hentze@bevuta.com>
Signed-off-by: Michael Lohmann <mi.al.lohmann@gmail.com>
---

On 11. Jan 2024, at 20:37, Junio C Hamano <gitster@pobox.com> wrote:
> Phillip Wood <phillip.wood123@gmail.com> writes:
> > I think ACTION_NONE was intended to covey that the user did not pass
> > one of the OPT_CMDMODE() options like "--continue" as there isn't a
> > "--start" option. I don't have a strong opinion between "_NONE" and
> > "_START".
>
> I agree with you why NONE is called as such.  If "revert" does not
> take "--start" (I do not remember offhand), I would think it would
> be better to follow suit.
My point was that yes, it might be in sync with what the user passes in
as arguments, but when I followed the code and saw lots of references to
ACTION_NONE I was puzzled, since my intuition of that name was that
_no action_ should be taken (which did not make sense to me).

So the (provocative) question is: Do we want to keep the variable name
in sync with some input parameters, or rather with the real action that
should be taken?

(Depending on the outcome of this discussion I would also prepare a
patch renaming it in builtin/rebase.c)

What do you think about this version which keeps the
`if (cmd != ACTION_START)` in favour of the `goto` and instead of the
constant if/else checks for the `verify_opt_compatible` (with the
`assert` at the last one) here is one version with a
`get_cmd_flag`-function (I am not that happy with the name...) that has
a `switch` and it has a runtime error handling with `BUG`.

I think it is the most concise of the options so far.

Ciao
Michael

 builtin/revert.c | 65 +++++++++++++++++++++++++++---------------------
 1 file changed, 37 insertions(+), 28 deletions(-)

diff --git a/builtin/revert.c b/builtin/revert.c
index 89821bab95..891aa1d720 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -20,6 +20,14 @@
  * Copyright (c) 2005 Junio C Hamano
  */
 
+enum action {
+	ACTION_START = 0,
+	ACTION_CONTINUE,
+	ACTION_SKIP,
+	ACTION_ABORT,
+	ACTION_QUIT,
+};
+
 static const char * const revert_usage[] = {
 	N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
 	N_("git revert (--continue | --skip | --abort | --quit)"),
@@ -33,6 +41,17 @@ static const char * const cherry_pick_usage[] = {
 	NULL
 };
 
+static char* get_cmd_optionname(enum action cmd)
+{
+	switch (cmd) {
+	case ACTION_CONTINUE: return "--continue";
+	case ACTION_SKIP: return "--skip";
+	case ACTION_ABORT: return "--abort";
+	case ACTION_QUIT: return "--quit";
+	case ACTION_START: BUG("no commandline flag for ACTION_START");
+	}
+}
+
 static const char *action_name(const struct replay_opts *opts)
 {
 	return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
@@ -85,12 +104,12 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 	const char * const * usage_str = revert_or_cherry_pick_usage(opts);
 	const char *me = action_name(opts);
 	const char *cleanup_arg = NULL;
-	int cmd = 0;
+	enum action cmd = ACTION_START;
 	struct option base_options[] = {
-		OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
-		OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
-		OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
-		OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
+		OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), ACTION_QUIT),
+		OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), ACTION_CONTINUE),
+		OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), ACTION_ABORT),
+		OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), ACTION_SKIP),
 		OPT_CLEANUP(&cleanup_arg),
 		OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
 		OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
@@ -144,20 +163,8 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 	}
 
 	/* Check for incompatible command line arguments */
-	if (cmd) {
-		char *this_operation;
-		if (cmd == 'q')
-			this_operation = "--quit";
-		else if (cmd == 'c')
-			this_operation = "--continue";
-		else if (cmd == 's')
-			this_operation = "--skip";
-		else {
-			assert(cmd == 'a');
-			this_operation = "--abort";
-		}
-
-		verify_opt_compatible(me, this_operation,
+	if (cmd != ACTION_START)
+		verify_opt_compatible(me, get_cmd_optionname(cmd),
 				"--no-commit", opts->no_commit,
 				"--signoff", opts->signoff,
 				"--mainline", opts->mainline,
@@ -168,7 +175,6 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 				"--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
 				"--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
 				NULL);
-	}
 
 	if (!opts->strategy && opts->default_strategy) {
 		opts->strategy = opts->default_strategy;
@@ -183,9 +189,7 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 				"--edit", opts->edit > 0,
 				NULL);
 
-	if (cmd) {
-		opts->revs = NULL;
-	} else {
+	if (cmd == ACTION_START) {
 		struct setup_revision_opt s_r_opt;
 		opts->revs = xmalloc(sizeof(*opts->revs));
 		repo_init_revisions(the_repository, opts->revs, NULL);
@@ -198,6 +202,8 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 		memset(&s_r_opt, 0, sizeof(s_r_opt));
 		s_r_opt.assume_dashdash = 1;
 		argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
+	} else {
+		opts->revs = NULL;
 	}
 
 	if (argc > 1)
@@ -210,19 +216,22 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
 		opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
 	free(options);
 
-	if (cmd == 'q') {
+	switch (cmd) {
+	case ACTION_QUIT: {
 		int ret = sequencer_remove_state(opts);
 		if (!ret)
 			remove_branch_state(the_repository, 0);
 		return ret;
 	}
-	if (cmd == 'c')
+	case ACTION_CONTINUE:
 		return sequencer_continue(the_repository, opts);
-	if (cmd == 'a')
+	case ACTION_ABORT:
 		return sequencer_rollback(the_repository, opts);
-	if (cmd == 's')
+	case ACTION_SKIP:
 		return sequencer_skip(the_repository, opts);
-	return sequencer_pick_revisions(the_repository, opts);
+	case ACTION_START:
+		return sequencer_pick_revisions(the_repository, opts);
+	}
 }
 
 int cmd_revert(int argc, const char **argv, const char *prefix)
-- 
2.39.3 (Apple Git-145)


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-11 20:06       ` [PATCH v3] " Michael Lohmann
@ 2024-01-11 21:47         ` Junio C Hamano
  2024-01-12  0:40         ` Junio C Hamano
  2024-01-12  7:24         ` Jeff King
  2 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2024-01-11 21:47 UTC (permalink / raw)
  To: Michael Lohmann; +Cc: git, phillip.wood123, phillip.wood, wanja.hentze

Michael Lohmann <mi.al.lohmann@gmail.com> writes:

> This is done to avoid having to keep the char values in sync in
> different places and also to get compiler warnings on non-exhaustive
> switches.
>
> In the rebase `action` enum there is the enumeration constant
> `ACTION_NONE` which is not particularly descriptive, since it seems to
> imply that no action should be taken. Instead it signals a start of a
> revert/cherry-pick process, so here `ACTION_START` was chosen.
>
> Co-authored-by: Wanja Henze <wanja.hentze@bevuta.com>
> Signed-off-by: Michael Lohmann <mi.al.lohmann@gmail.com>
> ---
>
> On 11. Jan 2024, at 20:37, Junio C Hamano <gitster@pobox.com> wrote:
>> Phillip Wood <phillip.wood123@gmail.com> writes:
>> > I think ACTION_NONE was intended to covey that the user did not pass
>> > one of the OPT_CMDMODE() options like "--continue" as there isn't a
>> > "--start" option. I don't have a strong opinion between "_NONE" and
>> > "_START".
>>
>> I agree with you why NONE is called as such.  If "revert" does not
>> take "--start" (I do not remember offhand), I would think it would
>> be better to follow suit.
> My point was that yes, it might be in sync with what the user passes in
> as arguments, but when I followed the code and saw lots of references to
> ACTION_NONE I was puzzled, since my intuition of that name was that
> _no action_ should be taken (which did not make sense to me).

I know you wrote that ;-).  But _NONE is "no action was specified",
and has been so for a long time in the context of "rebase". I do not
see any confusion expressed there.  I do not expect to see any
confusion here, either, if we were to introduce these new enum.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-11 20:06       ` [PATCH v3] " Michael Lohmann
  2024-01-11 21:47         ` Junio C Hamano
@ 2024-01-12  0:40         ` Junio C Hamano
  2024-01-12  7:24         ` Jeff King
  2 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2024-01-12  0:40 UTC (permalink / raw)
  To: Michael Lohmann; +Cc: git, phillip.wood123, phillip.wood, wanja.hentze

Michael Lohmann <mi.al.lohmann@gmail.com> writes:

> -	if (cmd == 'c')
> +	case ACTION_CONTINUE:
>  		return sequencer_continue(the_repository, opts);
> -	if (cmd == 'a')
> +	case ACTION_ABORT:
>  		return sequencer_rollback(the_repository, opts);
> -	if (cmd == 's')
> +	case ACTION_SKIP:
>  		return sequencer_skip(the_repository, opts);
> -	return sequencer_pick_revisions(the_repository, opts);
> +	case ACTION_START:
> +		return sequencer_pick_revisions(the_repository, opts);
> +	}
>  }

This change broke the build when merged to 'seen' like so ...

builtin/revert.c: In function 'run_sequencer':
builtin/revert.c:242:1: error: control reaches end of non-void function [-Werror=return-typ ]
  242 | }
      | ^
 
... so I'm discarding
it out of my tree and redoing today's integration cycle.

Different compilers are smart in different ways, and we shouldn't
overly rely on the fact that some compilers may be happy by seeing a
switch that has all the enum values and notice that one of the return
will be triggered in its case arms.




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-11 20:06       ` [PATCH v3] " Michael Lohmann
  2024-01-11 21:47         ` Junio C Hamano
  2024-01-12  0:40         ` Junio C Hamano
@ 2024-01-12  7:24         ` Jeff King
  2024-01-12 18:13           ` Junio C Hamano
  2 siblings, 1 reply; 11+ messages in thread
From: Jeff King @ 2024-01-12  7:24 UTC (permalink / raw)
  To: Michael Lohmann; +Cc: gitster, git, phillip.wood123, phillip.wood, wanja.hentze

On Thu, Jan 11, 2024 at 09:06:27PM +0100, Michael Lohmann wrote:

> > I agree with you why NONE is called as such.  If "revert" does not
> > take "--start" (I do not remember offhand), I would think it would
> > be better to follow suit.
> My point was that yes, it might be in sync with what the user passes in
> as arguments, but when I followed the code and saw lots of references to
> ACTION_NONE I was puzzled, since my intuition of that name was that
> _no action_ should be taken (which did not make sense to me).

Just my two cents as an observer who is very familiar with the idioms of
Git's codebase: it's common for us to use NONE here to mean "an action
has not been selected", which the code then translates to a default
action. So that's what I would have chosen.

But your way of seeing it also makes sense to me. I think I just find
the "START" name jarring because we do not use that word elsewhere to
describe the action. What if you called it ACTION_DEFAULT? Then it is
both the "default" value we give it, and also the default action (which
is not otherwise named in the code).


As far as the enum vs char thing, I do not have a strong opinion (though
I do tend to like enums myself). Here are a few minor style comments
(again that are idiomatic for our code base):

> +enum action {
> +	ACTION_START = 0,
> +	ACTION_CONTINUE,
> +	ACTION_SKIP,
> +	ACTION_ABORT,
> +	ACTION_QUIT,
> +};

Explicitly setting ACTION_START to 0 is good (even though it is not
strictly necessary) because it makes it clear that we expect to use its
truthiness later. But later...

> @@ -183,9 +189,7 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
>  				"--edit", opts->edit > 0,
>  				NULL);
>  
> -	if (cmd) {
> -		opts->revs = NULL;
> -	} else {
> +	if (cmd == ACTION_START) {
>  		struct setup_revision_opt s_r_opt;
>  		opts->revs = xmalloc(sizeof(*opts->revs));
>  		repo_init_revisions(the_repository, opts->revs, NULL);
> @@ -198,6 +202,8 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
>  		memset(&s_r_opt, 0, sizeof(s_r_opt));
>  		s_r_opt.assume_dashdash = 1;
>  		argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
> +	} else {
> +		opts->revs = NULL;

We do not take advantage of that. It is still OK to do "if (cmd)" with
the enum, and that's what I'd usually expect in our code base. There is
no need for this hunk at all (which also switches the order of the
conditional, which just seems like churn to me).

> @@ -33,6 +41,17 @@ static const char * const cherry_pick_usage[] = {
>  	NULL
>  };
>  
> +static char* get_cmd_optionname(enum action cmd)

From CodingGuidelines:

  When declaring pointers, the star sides with the variable name, i.e.
  "char *string", not "char* string" or "char * string".  This makes it
  easier to understand code like "char *string, c;".

(Yes, I know there are arguments for the other way, too; but consistency
is the most important thing, I think).

> +{
> +	switch (cmd) {
> +	case ACTION_CONTINUE: return "--continue";
> +	case ACTION_SKIP: return "--skip";
> +	case ACTION_ABORT: return "--abort";
> +	case ACTION_QUIT: return "--quit";
> +	case ACTION_START: BUG("no commandline flag for ACTION_START");
> +	}
> +}

I find this perfectly readable, and is likely the way I'd write it in a
personal project. But in this project I find we tend to stick to more
conventional formatting, like:

  switch (cmd) {
  case ACTION_CONTINUE:
	return "--continue";
  case ACTION_SKIP:
	return "--skip";
  ...and so on...

> @@ -144,20 +163,8 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
>  	}
>  
>  	/* Check for incompatible command line arguments */
> -	if (cmd) {
> -		char *this_operation;
> -		if (cmd == 'q')
> -			this_operation = "--quit";
> -		else if (cmd == 'c')
> -			this_operation = "--continue";
> -		else if (cmd == 's')
> -			this_operation = "--skip";
> -		else {
> -			assert(cmd == 'a');
> -			this_operation = "--abort";
> -		}
> -
> -		verify_opt_compatible(me, this_operation,
> +	if (cmd != ACTION_START)

Likewise here I'd probably leave this as "if (cmd)".

> [...]

Everything else looked pretty good to me.

-Peff

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-12  7:24         ` Jeff King
@ 2024-01-12 18:13           ` Junio C Hamano
  2024-01-12 19:33             ` Michael Lohmann
  0 siblings, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2024-01-12 18:13 UTC (permalink / raw)
  To: Jeff King
  Cc: Michael Lohmann, git, phillip.wood123, phillip.wood, wanja.hentze

Jeff King <peff@peff.net> writes:

> But your way of seeing it also makes sense to me. I think I just find
> the "START" name jarring because we do not use that word elsewhere to
> describe the action.

Thanks.  I forgot to say that I share the same feeling, both about
"NONE could mean no-op" (but then seriously why would anybody sane
want that?) and "START is not how we spell these things".  I can see
how DEFAULT could make sense, but if somebody picked DEFAULT between
two sensible choices NONE and DEFAULT here, especially if they claim
that they started this enum to mimick what is done in another place,
and after they were told that the other place they are imitating
follows the convention of using NONE for "nothing specified, so use
default", I would have to say that they are trying to be different
for the sake of being different, which is not a good sign.  I'd want
our contributors to be original where being original matters more.

>> +{
>> +	switch (cmd) {
>> +	case ACTION_CONTINUE: return "--continue";
>> +	case ACTION_SKIP: return "--skip";
>> +	case ACTION_ABORT: return "--abort";
>> +	case ACTION_QUIT: return "--quit";
>> +	case ACTION_START: BUG("no commandline flag for ACTION_START");
>> +	}
>> +}
>
> I find this perfectly readable, and is likely the way I'd write it in a
> personal project. But in this project I find we tend to stick to more
> conventional formatting, like:
>
>   switch (cmd) {
>   case ACTION_CONTINUE:
> 	return "--continue";
>   case ACTION_SKIP:
> 	return "--skip";
>   ...and so on...

Same.  I try to do the latter while working on this project, but I
do admit I use the former in small one-page tools I write outside
the context of this project.

>> +	if (cmd != ACTION_START)
>
> Likewise here I'd probably leave this as "if (cmd)".

I 100% agree with the suggestion to explicitly define something to
be 0 when we are going to use it for its Boolean value.  So an
alternative would be to treat all ACTION_* enum values the same, and
not define the first one explicitly to 0.  

Especially in the context of a patch that wants to turn if/elseif
cascades to switch, I would suspect that the latter, as switch/case
does not special case the falsehood among other possible values of
integer type, might be easier to maintain in the longer term.

Thanks.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] builtin/revert.c: refactor using an enum for cmd-action
  2024-01-12 18:13           ` Junio C Hamano
@ 2024-01-12 19:33             ` Michael Lohmann
  0 siblings, 0 replies; 11+ messages in thread
From: Michael Lohmann @ 2024-01-12 19:33 UTC (permalink / raw)
  To: gitster
  Cc: git, mi.al.lohmann, peff, phillip.wood123, phillip.wood, wanja.hentze

On 12. Jan 2024, at 19:13, Junio C Hamano <gitster@pobox.com> wrote:
> > But your way of seeing it also makes sense to me. I think I just find
> > the "START" name jarring because we do not use that word elsewhere to
> > describe the action.
> 
> Thanks.  I forgot to say that I share the same feeling, both about
> "NONE could mean no-op" (but then seriously why would anybody sane
> want that?) and "START is not how we spell these things".  I can see
> how DEFAULT could make sense, but if somebody picked DEFAULT between
> two sensible choices NONE and DEFAULT here, especially if they claim
> that they started this enum to mimick what is done in another place,
> and after they were told that the other place they are imitating
> follows the convention of using NONE for "nothing specified, so use
> default", I would have to say that they are trying to be different
> for the sake of being different, which is not a good sign.  I'd want
> our contributors to be original where being original matters more.

I am sorry to have left this feeling in you. It was not my intention to
be original, but I just did not understand the reason for the other
name. If I wanted to be "sneaky" and wasn't truly open for a discussion
I would not have mentioned that it is different in the other file. I
don't try to be original for the sake of it, but yes indeed if I have a
hard time understanding some reasoning, in my day job it is my role to
ask these. But I think I am indeed questioning a bit too much here.
Sorry for that! You as the project lead constantly have to do the same
and I am in awe as how you handle it.

I am sorry that this discussion did get out of hand. Especially since
this patch does not even introduce a feature, but is only a refactoring
of an already perfectly fine codebase. My only intention was to align
builtin/refactor.c a bit more to builtin/rebase.c but the current state
is 100% good as is, so I think we should just drop this discussion.

> > + if (cmd != ACTION_START)
> > 
> > Likewise here I'd probably leave this as "if (cmd)".
> 
> I 100% agree with the suggestion to explicitly define something to
> be 0 when we are going to use it for its Boolean value.  So an
> alternative would be to treat all ACTION_* enum values the same, and
> not define the first one explicitly to 0.  
> 
> Especially in the context of a patch that wants to turn if/elseif
> cascades to switch, I would suspect that the latter, as switch/case
> does not special case the falsehood among other possible values of
> integer type, might be easier to maintain in the longer term.

That is indeed what v4 of the patch did that I prepared half a day ago
and just did not have the time to properly check again before I submit
it. It also tackles the other issues you mentioned, but my feeling is
that the current state is good as it is with the characters and so we
should just drop this discussion.

Sorry to have caused such a stir and that I took so much of all of your
valuable time! I for myself have learned a great deal from all of you
and your interactions, so thank you!

Michael Lohmann

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-01-12 19:34 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-11  8:04 [PATCH] builtin/revert.c: refactor using an enum for cmd-action Michael Lohmann
2024-01-11 16:57 ` Phillip Wood
2024-01-11 17:47   ` [PATCH v2] " Michael Lohmann
2024-01-11 19:50     ` Junio C Hamano
2024-01-11 20:06       ` [PATCH v3] " Michael Lohmann
2024-01-11 21:47         ` Junio C Hamano
2024-01-12  0:40         ` Junio C Hamano
2024-01-12  7:24         ` Jeff King
2024-01-12 18:13           ` Junio C Hamano
2024-01-12 19:33             ` Michael Lohmann
2024-01-11 19:37   ` [PATCH] " Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).