diff mbox series

sequencer: rectify empty hint in call of require_clean_work_tree()

Message ID 20230323162234.995514-1-oswald.buddenhagen@gmx.de (mailing list archive)
State New, archived
Headers show
Series sequencer: rectify empty hint in call of require_clean_work_tree() | expand

Commit Message

Oswald Buddenhagen March 23, 2023, 4:22 p.m. UTC
The canonical way to represent "no error hint" is making it null, which
shortcuts the error() call altogether.

Signed-off-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
---
 sequencer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Oswald Buddenhagen April 27, 2023, 7:58 a.m. UTC | #1
ping!

On Thu, Mar 23, 2023 at 05:22:34PM +0100, Oswald Buddenhagen wrote:
>The canonical way to represent "no error hint" is making it null, which
>shortcuts the error() call altogether.
>
Junio C Hamano April 27, 2023, 9:13 p.m. UTC | #2
Oswald Buddenhagen <oswald.buddenhagen@gmx.de> writes:

> ping!
>
> On Thu, Mar 23, 2023 at 05:22:34PM +0100, Oswald Buddenhagen wrote:
>>The canonical way to represent "no error hint" is making it null, which
>>shortcuts the error() call altogether.

I won't repeat what Peff already said on another ping! we saw
recently on the list.

The call to require_clean_work_tree() with "" hint existed ever
since this part of the "rebase" machinery was rewritten in C by
b97e1873 (rebase -i: rewrite complete_action() in C, 2018-08-28).
I added the author of that change to the Cc: list.

The original implementation of require_clean_work_tree looked like
this:

require_clean_work_tree () {
	git rev-parse --verify HEAD >/dev/null || exit 1
	git update-index -q --ignore-submodules --refresh
	err=0

	if ! git diff-files --quiet --ignore-submodules
	then
		...
		err=1
	fi

	if ! git diff-index --cached --quiet --ignore-submodules HEAD --
	then
		...
		err=1
	fi

	if test $err = 1
	then
		test -n "$2" && echo "$2" >&2
		exit 1
	fi
}

I.e. the second argument, "hint", is shown only when it was a
non-empty string.  It did not add "error:" prefix before the
message.

In contrast, this is what wt-status.c has:

int require_clean_work_tree(struct repository *r,
			    const char *action,
			    const char *hint,
			    int ignore_submodules,
			    int gently)
{
	struct lock_file lock_file = LOCK_INIT;
	int err = 0, fd;

	...
	if (err) {
		if (hint)
			error("%s", hint);
		if (!gently)
			exit(128);
	}

	return err;
}

Arguably, using error() as a replacement for 'echo "$2" >&2' was a
sloppy conversion made back in ea63b393 (pull: make code more
similar to the shell script again, 2016-10-07), but I suspect that
in-tree callers that do have something to say, and the end-users who
are used to see the messages these callers produce, expect to see
the "error:" prefix these days, so it needs further study if we
wanted to "fix" the misuse of error() there.  In any case, the
observation that motivated your patch is not error() vs fputs().

For squelching a useless "hint" that is empty (other than that
mistaken "error:" prefix), however, I think you can and should do
better than replacing "" with NULL on the callers' side.  As we can
see from the comparison between the original, scripted version and
the verison in C that is in wt-status.c of require_clean_work_tree,
checking for NULL-ness of hint is another misconversion made when
it was rewritten in C.

I think the right fix would be more like the attached patch, which
will fix any other callsites that pass "" at the same time.  Of
course, you can fix the callers on top, but that is secondary.

 wt-status.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git i/wt-status.c w/wt-status.c
index 97b9c1c035..2b6dc4d6ac 100644
--- i/wt-status.c
+++ w/wt-status.c
@@ -2650,7 +2650,7 @@ int require_clean_work_tree(struct repository *r,
 	}
 
 	if (err) {
-		if (hint)
+		if (hint && *hint)
 			error("%s", hint);
 		if (!gently)
 			exit(128);
Oswald Buddenhagen April 27, 2023, 10:33 p.m. UTC | #3
On Thu, Apr 27, 2023 at 02:13:29PM -0700, Junio C Hamano wrote:
>I think the right fix would be more like the attached patch, which
>will fix any other callsites that pass "" at the same time.  Of
>course, you can fix the callers on top, but that is secondary.
>
there is only that one incorrect (in-tree) call.
i don't think that making the behavior more compliant with the shell 
implementation is particularly elegant or even useful.
if i wanted to be super-pedantic about it, i'd assert that non-null 
strings are non-empty. but that would only help if all error paths 
actually have test coverage.

>--- i/wt-status.c
>+++ w/wt-status.c
>@@ -2650,7 +2650,7 @@ int require_clean_work_tree(struct repository *r,
> 
>-		if (hint)
>+		if (hint && *hint)
> 			error("%s", hint);

-- ossi
Felipe Contreras May 2, 2023, 6:57 p.m. UTC | #4
Oswald Buddenhagen wrote:
> if i wanted to be super-pedantic about it, i'd assert that non-null 
> strings are non-empty.

I would disagree. "" is empty but not null, not just in C but in many
other languages, including shell.
Oswald Buddenhagen May 3, 2023, 7:15 a.m. UTC | #5
On Tue, May 02, 2023 at 12:57:03PM -0600, Felipe Contreras wrote:
>Oswald Buddenhagen wrote:
>> if i wanted to be super-pedantic about it, i'd assert that non-null 
>> strings are non-empty.
>
>I would disagree. "" is empty but not null, not just in C but in many
>other languages, including shell.
>
yes. that's kinda the point the assert would make.

-- ossi
diff mbox series

Patch

diff --git a/sequencer.c b/sequencer.c
index 3be23d7ca2..7c275c9a65 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -6168,7 +6168,7 @@  int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
 	if (checkout_onto(r, opts, onto_name, &oid, orig_head))
 		goto cleanup;
 
-	if (require_clean_work_tree(r, "rebase", "", 1, 1))
+	if (require_clean_work_tree(r, "rebase", NULL, 1, 1))
 		goto cleanup;
 
 	todo_list_write_total_nr(&new_todo);