git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 10/16] fast-import: use skip_prefix for parsing input

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jun 20, 2014, 03:19 UTC
Message-ID
<CAPig+cTgz1s_68MVT5XgTsM9j=NZnCt3tNX3wBGddEq3nWZgyw@mail.gmail.com>
In-Reply-To
<20140618194912.GJ22622@sigill.intra.peff.net>
On Wed, Jun 18, 2014 at 3:49 PM, Jeff King <peff@peff.net> wrote:
Show 47 quoted lines
> Fast-import does a lot of parsing of commands and
> dispatching to sub-functions. For example, given "option
> foo", we might recognize "option " using starts_with, and
> then hand it off to parse_option() to do the rest.
>
> However, we do not let parse_option know that we have parsed
> the first part already. It gets the full buffer, and has to
> skip past the uninteresting bits. Some functions simply add
> a magic constant:
>
>   char *option = command_buf.buf + 7;
>
> Others use strlen:
>
>   char *option = command_buf.buf + strlen("option ");
>
> And others use strchr:
>
>   char *option = strchr(command_buf.buf, ' ') + 1;
>
> All of these are brittle and easy to get wrong (especially
> given that the starts_with call and the code that assumes
> the presence of the prefix are far apart). Instead, we can
> use skip_prefix, and just pass each handler a pointer to its
> arguments.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
> diff --git a/fast-import.c b/fast-import.c
> index a3ffe30..5f17adb 100644
> --- a/fast-import.c
> +++ b/fast-import.c
> @@ -2713,20 +2706,22 @@ static void parse_new_commit(void)
>
>         /* file_change* */
>         while (command_buf.len > 0) {
> -               if (starts_with(command_buf.buf, "M "))
> -                       file_change_m(b);
> -               else if (starts_with(command_buf.buf, "D "))
> -                       file_change_d(b);
> -               else if (starts_with(command_buf.buf, "R "))
> -                       file_change_cr(b, 1);
> -               else if (starts_with(command_buf.buf, "C "))
> -                       file_change_cr(b, 0);
> -               else if (starts_with(command_buf.buf, "N "))
> -                       note_change_n(b, &prev_fanout);
> +               const char *v;

This declaration of 'v' shadows the 'v' added by patch 8/16 earlier in the function.

Show 19 quoted lines
> +               if (skip_prefix(command_buf.buf, "M ", &v))
> +                       file_change_m(v, b);
> +               else if (skip_prefix(command_buf.buf, "D ", &v))
> +                       file_change_d(v, b);
> +               else if (skip_prefix(command_buf.buf, "R ", &v))
> +                       file_change_cr(v, b, 1);
> +               else if (skip_prefix(command_buf.buf, "C ", &v))
> +                       file_change_cr(v, b, 0);
> +               else if (skip_prefix(command_buf.buf, "N ", &v))
> +                       note_change_n(v, b, &prev_fanout);
>                 else if (!strcmp("deleteall", command_buf.buf))
>                         file_change_deleteall(b);
> -               else if (starts_with(command_buf.buf, "ls "))
> -                       parse_ls(b);
> +               else if (skip_prefix(command_buf.buf, "ls ", &v))
> +                       parse_ls(v, b);
>                 else {
>                         unread_command_buf = 1;
>                         break;
Previous: Jeff KingNext: Jeff King
Message 21 of 33 in “skip_prefix refactoring and cleanups”
  1. 0/16 skip_prefix refactoring and cleanupsJeff King, Jun 18, 2014
  2. 01/16 parse_diff_color_slot: drop ofs parameterJeff King, Jun 18, 2014
  3. 02/16 daemon: mark some strings as constJeff King, Jun 18, 2014
  4. 03/16 avoid using skip_prefix as a booleanJeff King, Jun 18, 2014
  5. 04/16 refactor skip_prefix to return a booleanJeff King, Jun 18, 2014
  6. Eric SunshineJun 20, 2014
  7. Jeff KingJun 20, 2014
  8. Eric SunshineJun 20, 2014
  9. Jeff KingJun 20, 2014
  10. Junio C HamanoJun 23, 2014
  11. Jeff KingJun 23, 2014
  12. builtin/clone.c: detect a clone starting at a tag correctlyJunio C Hamano, Jun 23, 2014
  13. 05/16 apply: use skip_prefix instead of raw additionJeff King, Jun 18, 2014
  14. 06/16 fast-import: fix read of uninitialized argv memoryJeff King, Jun 18, 2014
  15. 07/16 transport-helper: avoid reading past end-of-stringJeff King, Jun 18, 2014
  16. 08/16 use skip_prefix to avoid magic numbersJeff King, Jun 18, 2014
  17. Junio C HamanoJun 23, 2014
  18. Jeff KingJul 1, 2014
  19. 09/16 use skip_prefix to avoid repeating stringsJeff King, Jun 18, 2014
  20. 10/16 fast-import: use skip_prefix for parsing inputJeff King, Jun 18, 2014
  21. Eric SunshineJun 20, 2014
  22. Jeff KingJun 20, 2014
  23. Eric SunshineJun 20, 2014
  24. 11/16 daemon: use skip_prefix to avoid magic numbersJeff King, Jun 18, 2014
  25. 12/16 stat_opt: check extra strlen callJeff King, Jun 18, 2014
  26. 13/16 fast-import: refactor parsing of spacesJeff King, Jun 18, 2014
  27. 14/16 fetch-pack: refactor parsing in get_ackJeff King, Jun 18, 2014
  28. Eric SunshineJun 20, 2014
  29. 15/16 git: avoid magic number with skip_prefixJeff King, Jun 18, 2014
  30. 16/16 use skip_prefix to avoid repeated calculationsJeff King, Jun 18, 2014
  31. Tanay AbhraJun 19, 2014
  32. 17/16 http-push: refactor parsing of remote object namesJeff King, Jun 19, 2014
  33. Jeff KingJun 19, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.