Subject: Re: [PATCH v3 05/13] parse the -L options

From: Junio C Hamano <[email-masked]>
Date: Tue, 13 Jul 2010 16:03:14 -0700

Bo Yang <[email-masked]> writes:

> +static int log_line_range_callback(const struct option *option, const char *arg, int unset)
> +{
> +	struct line_opt_callback_data *data = option->value;
> +	struct diff_line_range *r = *data->range;
> +	struct parse_opt_ctx_t *ctx = data->ctx;

Need a blank line here for readability.

> +	if (!arg)
> +		return -1;
> ...
> @@ -75,6 +118,58 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
>  	 */
>  	if (argc == 2 && !strcmp(argv[1], "-h"))
>  		usage(builtin_log_usage);
> +
> +	parse_options_start(&ctx, argc, argv, prefix, PARSE_OPT_KEEP_DASHDASH |
> +			PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_STOP_AT_NON_OPTION);
> +	for (;;) {
> +		switch (parse_options_step(&ctx, options, log_opt_usage)) {
> +		case PARSE_OPT_HELP:
> +			exit(129);
> +		case PARSE_OPT_DONE:
> +			goto parse_done;
> +		case PARSE_OPT_NON_OPTION:
> +			path = parse_options_current(&ctx);
> +			pathspec = prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
> +			range->spec = alloc_filespec(pathspec);
> +			free((void *)pathspec);
> +			if (range->nr == 0) {
> +				if(range->next) {
> +					die("Path %s need a -L <range> option\n"
> +					"If you want follow the history of the whole file "
> +					"whether to using 'git log' without -L or using "
> +					"'git log -L 1,$ <path>'", range->spec->path);
> +				} else {
> +					parse_options_next(&ctx, 1);
> +					continue;

This loop smells bad.

When "-L n,m" appears on the command line, log_line_range_callback() would
be called and would eat n,m (which is correct), but at that point you
would not just want to be prepared to accept a non-option ("path" in "-L
n,m path"), but actually would want to force the user to give a path, no?
IOW, isn't "git log -L n,m -U20" an error, unless "-U20" is a filename
that the user wants to track?

I somehow suspect that futzing with STOP_AT_NON_OPTION (done in the first
two patches in the series) to parse "-L n,m path" is a misguided design
attempt.  Shouldn't you be instead giving a support for option callback to
take more than one argument to do this?

> +				}
> +			}
> +			struct diff_line_range *r = xmalloc(sizeof(*r));

decl-after-statement, but at this point it may be moot as I am doubting
the higher-level design of this option parser now.