From: Bo Yang <struggleyb.nku@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, trast@student.ethz.ch, Jens.Lehmann@web.de
Subject: Re: [PATCH v4 05/18] Parse the -L options
Date: Tue, 10 Aug 2010 23:40:39 +0800 [thread overview]
Message-ID: <AANLkTi=pzsPpC=gM3UEBAaMq7PGJYafW8SKHunVzrOyP@mail.gmail.com> (raw)
In-Reply-To: <7v39ur8r56.fsf@alter.siamese.dyndns.org>
Hi Junio,
On Sat, Aug 7, 2010 at 3:42 AM, Junio C Hamano <gitster@pobox.com> wrote:
> Bo Yang <struggleyb.nku@gmail.com> writes:
>
>> static void cmd_log_init(int argc, const char **argv, const char *prefix,
>> struct rev_info *rev, struct setup_revision_opt *opt)
>> {
>> int i;
>> int decoration_given = 0;
>> struct userformat_want w;
>> + const char *path = NULL, *pathspec = NULL;
>> + static struct diff_line_range *range = NULL, *r = NULL;
>> + static struct parse_opt_ctx_t ctx;
>> + static struct line_opt_callback_data line_cb = {&range, &ctx, NULL};
>
> Do these have to be static? cmd_log_init() may be near the top of the
> call chain and has less reason to be reentrant, but it feels somewhat
> wrong if we are placing something that should live on stack in BSS.
>
>> + static const struct option options[] = {
>> + OPT_CALLBACK('L', NULL, &line_cb, "n,m", "Process only line range n,m, counting from 1", log_line_range_callback),
>> + OPT_END()
>> + };
>> + ...
>> + 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:
>> + ... do the extra path thing ...
>> + pathspec = prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
>
> Please do not call it "pathspec", as this is a specific path in a commit.
> "pathspec" is a pattern to be matched to zero or more paths.
>
>> ...
>> + parse_options_next(&ctx, 1);
>> + continue;
>> + case PARSE_OPT_UNKNOWN:
>> + parse_options_next(&ctx, 1);
>> + continue;
>> + }
>> + parse_revision_opt(rev, &ctx, options, log_opt_usage);
>> + }
>
> Hmm, so the strategy is that you first run the command line through a pass
> of parse-options that is aware only of "-L" syntax, eat whatever it
> recognizes, and give remainder to the setup_revisions().
>
> While I agree with that strategy in general, I think this implementation
> is ugly. It may be even wrong. For example, can a user specify a path
> that begins with a dash with this parser?
>
> My gut feeling is that the capturing of the (optional) second argument
> given to -L is better done inside your callback.
>
> Now, the current callback interface does not give you access to ctx so you
> may need to invent a new type of "more powerful callback API" that gives
> you access to the ctx as well, but if you did so, you should be able to do
> something like:
>
> static int log_line_range_callback(...)
> {
> arg = parse_options_current(ctx);
> ... make sure it is a line range, e.g. "10,20"
> parse_options_next(ctx); /* consume it */
> path = parse_options_current(ctx); /* peek the second position */
> if (does it look like a path?) {
> ... associate path with the range in arg
> parse_options_next(ctx); /* consume it */
> } else if (have we already got another range earlier?) {
> ... use the previous path with the range in arg
> } else {
> die("-L range not followed by path");
> }
> }
>
> no? In the above illustration, I am assuming that the "more powerful" one
> allows the callback to control even parsing of the first argument,
> i.e. parse-options does not call get_arg() before calling you back.
>
> And "does it look like a path?" logic could say something like "If it is
> in the index, it is a path, even if it begins with a dash", or "If it is
> prefixed with ./, then it is always a path but we strip that dot-slash
> out", and somesuch, to make the heuristic of "do we have the optional
> second parameter?" better than "we do not allow a path that begins with a
> dash". After all, the callback for -L knows better than the generic
> "parse-options" infrastructure what to expect for the optional argument at
> the second position.
>
> And if you do that, I suspect that you also can lose the "clear up the
> last range" hack after the loop is done, no?
Yes, I think so. And if I change the logic to what you suggest, it
will also make the later 'move/copy detect' related argument parsing
easy. Because in move/copy detect, I should remove the 'remain path'
before feed it to setup_revisions. So, I hope I can make this change
along with the 'move/copy detect' change together, I hope this is
acceptable. :)
--
Regards!
Bo
----------------------------
My blog: http://blog.morebits.org
Why Git: http://www.whygitisbetterthanx.com/
next prev parent reply other threads:[~2010-08-10 15:40 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-08-05 16:11 [PATCH v4 00/18] Reroll the line log series Bo Yang
2010-08-05 16:11 ` [PATCH v4 01/18] parse-options: enhance STOP_AT_NON_OPTION Bo Yang
2010-08-05 16:11 ` [PATCH v4 02/18] parse-options: add two helper functions Bo Yang
2010-08-05 20:43 ` Thomas Rast
2010-08-05 16:11 ` [PATCH v4 03/18] Add the basic data structure for line level history Bo Yang
2010-08-05 21:09 ` Thomas Rast
2010-08-06 19:42 ` Junio C Hamano
2010-08-05 16:11 ` [PATCH v4 04/18] Refactor parse_loc Bo Yang
2010-08-05 16:11 ` [PATCH v4 05/18] Parse the -L options Bo Yang
2010-08-06 19:42 ` Junio C Hamano
2010-08-10 15:40 ` Bo Yang [this message]
2010-08-05 16:11 ` [PATCH v4 06/18] Export three functions from diff.c Bo Yang
2010-08-05 16:11 ` [PATCH v4 07/18] Add range clone functions Bo Yang
2010-08-05 16:11 ` [PATCH v4 08/18] map/take range to the parent of commits Bo Yang
2010-08-05 21:32 ` Thomas Rast
2010-08-05 16:11 ` [PATCH v4 09/18] Print the line log Bo Yang
2010-08-05 16:11 ` [PATCH v4 10/18] Hook line history into cmd_log, ensuring a topo-ordered walk Bo Yang
2010-08-05 16:11 ` [PATCH v4 11/18] Add tests for line history browser Bo Yang
2010-08-05 20:38 ` Thomas Rast
2010-08-06 5:28 ` Bo Yang
2010-08-06 9:04 ` Thomas Rast
2010-08-05 16:11 ` [PATCH v4 12/18] Make rewrite_parents public to other part of git Bo Yang
2010-08-05 16:11 ` [PATCH v4 13/18] Make graph_next_line external " Bo Yang
2010-08-05 16:11 ` [PATCH v4 14/18] Add parent rewriting to line history browser Bo Yang
2010-08-05 16:11 ` [PATCH v4 15/18] Add --graph prefix before line history output Bo Yang
2010-08-05 16:11 ` [PATCH v4 16/18] Add --full-line-diff option Bo Yang
2010-08-05 16:11 ` [PATCH v4 17/18] Add test cases for '--graph' of line level log Bo Yang
2010-08-05 16:11 ` [PATCH v4 18/18] Document line history browser Bo Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='AANLkTi=pzsPpC=gM3UEBAaMq7PGJYafW8SKHunVzrOyP@mail.gmail.com' \
--to=struggleyb.nku@gmail.com \
--cc=Jens.Lehmann@web.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=trast@student.ethz.ch \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).