{"thread":{"id":"36030","subject":"[PATCH] commit.c: Replace starts_with() with skip_prefix()","startedAt":"2014-03-03T07:47:55Z","lastAt":"2014-03-03T23:54:13Z","messageCount":4,"participants":["Karthik Nayak","Eric Sunshine","karthik nayak"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"235844","messageId":"1393832875-29096-1-git-send-email-karthik.188@gmail.com","threadId":"36030","inReplyTo":null,"subject":"[PATCH] commit.c: Replace starts_with() with skip_prefix()","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2014-03-03T07:47:55Z","receivedAt":"2014-03-03T07:47:55Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Replace with skip_prefix(), which uses the inbuilt function\nstrcmp() to compare.\n\nOther Places were this can be implemented:\ncommit.c : line 1117\ncommit.c : line 1197\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n commit.c | 3 ++-\n 1 file changed, 2 insertions(+), 1 deletion(-)\n\ndiff --git a/commit.c b/commit.c\nindex 6bf4fe0..1e181cf 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -552,6 +552,7 @@ static void record_author_date(struct author_date_slab *author_date,\n \tchar *buffer = NULL;\n \tstruct ident_split ident;\n \tchar *date_end;\n+\tchar *flag_sp; /* stores return value of skip_prefix() */\n \tunsigned long date;\n \n \tif (!commit->buffer) {\n@@ -566,7 +567,7 @@ static void record_author_date(struct author_date_slab *author_date,\n \t     buf;\n \t     buf = line_end + 1) {\n \t\tline_end = strchrnul(buf, '\\n');\n-\t\tif (!starts_with(buf, \"author \")) {\n+\t\tif ((flag_sp = skip_prefix(buf, \"author \")) == NULL) {\n \t\t\tif (!line_end[0] || line_end[1] == '\\n')\n \t\t\t\treturn; /* end of header */\n \t\t\tcontinue;\n-- \n1.9.0.138.g2de3478\n"},{"id":"235849","messageId":"CAPig+cQLoZy=MV_AqPQseMxsrU6T_PqmCifWn1SrOGwF2FcYzg@mail.gmail.com","threadId":"36030","inReplyTo":"1393832875-29096-1-git-send-email-karthik.188@gmail.com","subject":"Re: [PATCH] commit.c: Replace starts_with() with skip_prefix()","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-03-03T08:52:47Z","receivedAt":"2014-03-03T08:52:47Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"Thanks for the submission. Some commentary below to expose you to the\nreview process on this project...\n\nOn Mon, Mar 3, 2014 at 2:47 AM, Karthik Nayak <karthik.188@gmail.com> wrote:\n> Replace with skip_prefix(), which uses the inbuilt function\n> strcmp() to compare.\n\nExplaining the purpose of the change is indeed important, however,\nthis description misses the mark. See below.\n\n> Other Places were this can be implemented:\n> commit.c : line 1117\n> commit.c : line 1197\n\nBonus points if you actually follow through with this.\n\n> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n> ---\n>  commit.c | 3 ++-\n>  1 file changed, 2 insertions(+), 1 deletion(-)\n>\n> diff --git a/commit.c b/commit.c\n> index 6bf4fe0..1e181cf 100644\n> --- a/commit.c\n> +++ b/commit.c\n> @@ -552,6 +552,7 @@ static void record_author_date(struct author_date_slab *author_date,\n>         char *buffer = NULL;\n>         struct ident_split ident;\n>         char *date_end;\n> +       char *flag_sp; /* stores return value of skip_prefix() */\n\nIt's not clear why this variable is needed. It's only assigned (below)\nbut never referenced. Also, the name conveys little or no meaning. If\nyou need a comment to explain the purpose of the variable, that's\nprobably a good indication that it's poorly named.\n\n>         unsigned long date;\n>\n>         if (!commit->buffer) {\n> @@ -566,7 +567,7 @@ static void record_author_date(struct author_date_slab *author_date,\n>              buf;\n>              buf = line_end + 1) {\n>                 line_end = strchrnul(buf, '\\n');\n> -               if (!starts_with(buf, \"author \")) {\n> +               if ((flag_sp = skip_prefix(buf, \"author \")) == NULL) {\n\nUnfortunately, this change makes the code more difficult to read.\nLet's review the GSoC microproject:\n\n    Rewrite commit.c:record_author_date() to use skip_prefix().\n    Are there other places in this file where skip_prefix() would\n    be **more readable** than starts_with()?\n\nNote the part I **highlighted**. This is a good clue that use of\nskip_prefix() can be used to simplify the code. Examine\nrecord_author_date() more carefully and see if you can identify how to\ndo so.\n\n>                         if (!line_end[0] || line_end[1] == '\\n')\n>                                 return; /* end of header */\n>                         continue;\n> --\n> 1.9.0.138.g2de3478\n"},{"id":"235879","messageId":"CAOLa=ZRxj-iBiQRXEYvq6MrQyQcyzk32kPij8zzxh4ECbBrnMw@mail.gmail.com","threadId":"36030","inReplyTo":"CAPig+cQLoZy=MV_AqPQseMxsrU6T_PqmCifWn1SrOGwF2FcYzg@mail.gmail.com","subject":"Re: [PATCH] commit.c: Replace starts_with() with skip_prefix()","fromName":"karthik nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2014-03-03T15:23:47Z","receivedAt":"2014-03-03T15:23:47Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Hello Eric,\nThanks for Pointing out everything, i had a thorough look and fixed a\ncouple of things.\nHere is an Updated Patch.\n- Removed unnecessary code and variables.\n- Replaced all instances of starts_with() with skip_prefix()\n\nReplace starts_with() with skip_prefix() for better reading purposes.\nAlso to replace \"buf + strlen(author )\" by skip_prefix(), which is\nsaved in a new \"const char\" variable \"buf_skipprefix\".\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n commit.c | 13 ++++++++-----\n 1 file changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/commit.c b/commit.c\nindex 6bf4fe0..e5dc2e2 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -552,6 +552,7 @@ static void record_author_date(struct\nauthor_date_slab *author_date,\n  char *buffer = NULL;\n  struct ident_split ident;\n  char *date_end;\n+ const char *buf_skipprefix;\n  unsigned long date;\n\n  if (!commit->buffer) {\n@@ -562,18 +563,20 @@ static void record_author_date(struct\nauthor_date_slab *author_date,\n  return;\n  }\n\n+ buf_skipprefix = skip_prefix(buf, \"author \");\n+\n  for (buf = commit->buffer ? commit->buffer : buffer;\n      buf;\n      buf = line_end + 1) {\n  line_end = strchrnul(buf, '\\n');\n- if (!starts_with(buf, \"author \")) {\n+ if (!buf_skipprefix) {\n  if (!line_end[0] || line_end[1] == '\\n')\n  return; /* end of header */\n  continue;\n  }\n  if (split_ident_line(&ident,\n-     buf + strlen(\"author \"),\n-     line_end - (buf + strlen(\"author \"))) ||\n+ buf_skipprefix,\n+ line_end - buf_skipprefix) ||\n     !ident.date_begin || !ident.date_end)\n  goto fail_exit; /* malformed \"author\" line */\n  break;\n@@ -1113,7 +1116,7 @@ int parse_signed_commit(const unsigned char *sha1,\n  next = next ? next + 1 : tail;\n  if (in_signature && line[0] == ' ')\n  sig = line + 1;\n- else if (starts_with(line, gpg_sig_header) &&\n+ else if (skip_prefix(line, gpg_sig_header) &&\n  line[gpg_sig_header_len] == ' ')\n  sig = line + gpg_sig_header_len + 1;\n  if (sig) {\n@@ -1193,7 +1196,7 @@ static void parse_gpg_output(struct signature_check *sigc)\n  for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {\n  const char *found, *next;\n\n- if (starts_with(buf, sigcheck_gpg_status[i].check + 1)) {\n+ if (skip_prefix(buf, sigcheck_gpg_status[i].check + 1)) {\n  /* At the very beginning of the buffer */\n  found = buf + strlen(sigcheck_gpg_status[i].check + 1);\n  } else {\n-- \n1.9.0.138.g2de3478\n"},{"id":"235966","messageId":"CAPig+cQuzWv1+yg6F4zBqYfGdH6iz5xteCFHW6_o8Wyx68JT7w@mail.gmail.com","threadId":"36030","inReplyTo":"CAOLa=ZRxj-iBiQRXEYvq6MrQyQcyzk32kPij8zzxh4ECbBrnMw@mail.gmail.com","subject":"Re: [PATCH] commit.c: Replace starts_with() with skip_prefix()","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-03-03T23:54:13Z","receivedAt":"2014-03-03T23:54:13Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Mon, Mar 3, 2014 at 10:23 AM, karthik nayak <karthik.188@gmail.com> wrote:\n> Hello Eric,\n> Thanks for Pointing out everything, i had a thorough look and fixed a\n> couple of things.\n> Here is an Updated Patch.\n> - Removed unnecessary code and variables.\n> - Replaced all instances of starts_with() with skip_prefix()\n\nThis commentary is important for the on-going email discussion but\ndoes not belong in the commit message for the permanent project\nhistory, so place it below the \"---\" line just under your sign-off.\n\nExplaining what changed since the last attempt, as you do here, is a\ngood thing. To further ease the review process, supply a link to the\nprevious attempt, like this [1].\n\n[1]: http://thread.gmane.org/gmane.comp.version-control.git/243194\n\n> Replace starts_with() with skip_prefix() for better reading purposes.\n> Also to replace \"buf + strlen(author )\" by skip_prefix(), which is\n> saved in a new \"const char\" variable \"buf_skipprefix\".\n\nThe second sentence is really the meat of this change, and not at all\nincidental as \"Also\" implies. You should use it (or a refinement of\nit) as your commit message. The first sentence doesn't particularly\nadd much and can easily be dropped.\n\n> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n> ---\n>  commit.c | 13 ++++++++-----\n>  1 file changed, 8 insertions(+), 5 deletions(-)\n>\n> diff --git a/commit.c b/commit.c\n> index 6bf4fe0..e5dc2e2 100644\n> --- a/commit.c\n> +++ b/commit.c\n> @@ -552,6 +552,7 @@ static void record_author_date(struct\n> author_date_slab *author_date,\n>   char *buffer = NULL;\n>   struct ident_split ident;\n>   char *date_end;\n> + const char *buf_skipprefix;\n\nRead this response by Junio [2] to another GSoC candidate which\nexplains why this is a poor variable name and how to craft a better\none.\n\n[2]: http://thread.gmane.org/gmane.comp.version-control.git/243231/focus=243259\n\n>   unsigned long date;\n>\n>   if (!commit->buffer) {\n> @@ -562,18 +563,20 @@ static void record_author_date(struct\n> author_date_slab *author_date,\n>   return;\n>   }\n>\n> + buf_skipprefix = skip_prefix(buf, \"author \");\n\nUnfortunately, your patch is badly whitespace-damaged as if it was\npasted into the mailer and mangled. (Your first submission did not\nhave this problem.) If you can, use \"git send-email\" to avoid such\ncorruption.\n\n> +\n>   for (buf = commit->buffer ? commit->buffer : buffer;\n>       buf;\n>       buf = line_end + 1) {\n>   line_end = strchrnul(buf, '\\n');\n> - if (!starts_with(buf, \"author \")) {\n> + if (!buf_skipprefix) {\n>   if (!line_end[0] || line_end[1] == '\\n')\n>   return; /* end of header */\n>   continue;\n>   }\n>   if (split_ident_line(&ident,\n> -     buf + strlen(\"author \"),\n> -     line_end - (buf + strlen(\"author \"))) ||\n> + buf_skipprefix,\n> + line_end - buf_skipprefix) ||\n\nLooks reasonable (sans whitespace damage).\n\n>      !ident.date_begin || !ident.date_end)\n>   goto fail_exit; /* malformed \"author\" line */\n>   break;\n> @@ -1113,7 +1116,7 @@ int parse_signed_commit(const unsigned char *sha1,\n>   next = next ? next + 1 : tail;\n>   if (in_signature && line[0] == ' ')\n>   sig = line + 1;\n> - else if (starts_with(line, gpg_sig_header) &&\n> + else if (skip_prefix(line, gpg_sig_header) &&\n>   line[gpg_sig_header_len] == ' ')\n>   sig = line + gpg_sig_header_len + 1;\n>   if (sig) {\n> @@ -1193,7 +1196,7 @@ static void parse_gpg_output(struct signature_check *sigc)\n>   for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {\n>   const char *found, *next;\n>\n> - if (starts_with(buf, sigcheck_gpg_status[i].check + 1)) {\n> + if (skip_prefix(buf, sigcheck_gpg_status[i].check + 1)) {\n>   /* At the very beginning of the buffer */\n>   found = buf + strlen(sigcheck_gpg_status[i].check + 1);\n>   } else {\n\nFor these two sets of changes, unless you are actually taking\nadvantage of the return value of skip_prefix(), the mere mechanical\nreplacement of starts_with() with skip_prefix() actually hurts\nreadability. Examine each of these cases more carefully to determine\nwhether skip_prefix() is in fact useful. If so, take advantage of it.\nIf not, explain in the patch commentary why skip_prefix() doesn't\nhelp.\n\n> --\n> 1.9.0.138.g2de3478\n"}]}