Subject: Re: [PATCH v3 6/8] parseopt: autocorrect mistyped subcommands

From: Junio C Hamano <[email-masked]>
Date: Tue, 10 Mar 2026 13:16:39 -0700

Jiamu Sun <[email-masked]> writes:

> +static int similar_enough(const char *cmd, unsigned int dist)
> +{
> +	size_t len = strlen(cmd);
> +	unsigned int threshold = len < 3 ? 1 : len < 6 ? 3 : 6;
> +
> +	return dist < threshold;
> +}

There should be some explanation on the reason why this is very
different from SIMILAR_ENOUGH used in help.c for main commands,
especially given that the levenshtein() call here uses identical
weight parameters (0,2,1,3) as used by the call there.

> +static const char *autocorrect_subcommand(const char *cmd,
> + ...
> +	for_each_string_list_item(cand, cmds) {
> +		unsigned int dist = levenshtein(cmd, cand->string, 0, 2, 1, 3);
> +
> +		if (dist < min) {
> +			min = dist;
> +			best = cand;
> +			ties = 0;
> +		} else if (dist == min) {
> +			ties++;
> +		}
> +	}
> +
> +	if (!ties && similar_enough(cmd, min)) {