{"thread":{"id":"35975","subject":"[PATCH] Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname","startedAt":"2014-02-27T14:00:01Z","lastAt":"2014-02-27T19:39:54Z","messageCount":2,"participants":["Sun He","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"235477","messageId":"1393509601-31312-1-git-send-email-sunheehnus@gmail.com","threadId":"35975","inReplyTo":null,"subject":"[PATCH] Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname","fromName":"Sun He","fromEmail":"sunheehnus@gmail.com","sentAt":"2014-02-27T14:00:01Z","receivedAt":"2014-02-27T14:00:01Z","isPatch":true,"sender":{"key":"sunheehnus@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2889804?v=4"},"body":"\nSigned-off-by: Sun He <sunheehnus@gmail.com>\n---\n bulk-checkin.c |   10 +++++++++-\n 1 files changed, 9 insertions(+), 1 deletions(-)\n\ndiff --git a/bulk-checkin.c b/bulk-checkin.c\nindex 118c625..8c47d71 100644\n--- a/bulk-checkin.c\n+++ b/bulk-checkin.c\n@@ -23,7 +23,8 @@ static struct bulk_checkin_state {\n static void finish_bulk_checkin(struct bulk_checkin_state *state)\n {\n \tunsigned char sha1[20];\n-\tchar packname[PATH_MAX];\n+\tchar *packname;\n+    struct strbuf sb;\n \tint i;\n \n \tif (!state->f)\n@@ -43,6 +44,10 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)\n \t\tclose(fd);\n \t}\n \n+    /* 64-1 is more than the sum of len(sha1_to_hex(sha1)) and len(\".pack\") */\n+    strbuf_init(&sb,strlen(get_object_directory())+64);\n+    packname = sb.buf;\n+\n \tsprintf(packname, \"%s/pack/pack-\", get_object_directory());\n \tfinish_tmp_packfile(packname, state->pack_tmp_name,\n \t\t\t    state->written, state->nr_written,\n@@ -54,6 +59,9 @@ clear_exit:\n \tfree(state->written);\n \tmemset(state, 0, sizeof(*state));\n \n+    /* release sb space */\n+    strbuf_release(&sb);\n+\n \t/* Make objects we just wrote available to ourselves */\n \treprepare_packed_git();\n }\n-- \n1.7.1\n"},{"id":"235495","messageId":"xmqq8uswz6np.fsf@gitster.dls.corp.google.com","threadId":"35975","inReplyTo":"1393509601-31312-1-git-send-email-sunheehnus@gmail.com","subject":"Re: [PATCH] Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-02-27T19:39:54Z","receivedAt":"2014-02-27T19:39:54Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Sun He <sunheehnus@gmail.com> writes:\n\n> Signed-off-by: Sun He <sunheehnus@gmail.com>\n> ---\n>  bulk-checkin.c |   10 +++++++++-\n>  1 files changed, 9 insertions(+), 1 deletions(-)\n>\n> diff --git a/bulk-checkin.c b/bulk-checkin.c\n> index 118c625..8c47d71 100644\n> --- a/bulk-checkin.c\n> +++ b/bulk-checkin.c\n> @@ -23,7 +23,8 @@ static struct bulk_checkin_state {\n>  static void finish_bulk_checkin(struct bulk_checkin_state *state)\n>  {\n>  \tunsigned char sha1[20];\n> -\tchar packname[PATH_MAX];\n> +\tchar *packname;\n> +    struct strbuf sb;\n\nFunny indentation.\n\n>  \tint i;\n>  \n>  \tif (!state->f)\n> @@ -43,6 +44,10 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)\n>  \t\tclose(fd);\n>  \t}\n>  \n> +    /* 64-1 is more than the sum of len(sha1_to_hex(sha1)) and len(\".pack\") */\n> +    strbuf_init(&sb,strlen(get_object_directory())+64);\n> +    packname = sb.buf;\n> +\n>  \tsprintf(packname, \"%s/pack/pack-\", get_object_directory());\n\nIf you are using strbuf why not use strbuf_addf() instead?  Then you\ndo not have to worry about \"Is 64-1 enough?\" and things like that.\n\n>  \tfinish_tmp_packfile(packname, state->pack_tmp_name,\n>  \t\t\t    state->written, state->nr_written,\n> @@ -54,6 +59,9 @@ clear_exit:\n>  \tfree(state->written);\n>  \tmemset(state, 0, sizeof(*state));\n>  \n> +    /* release sb space */\n> +    strbuf_release(&sb);\n\nThe function name is more than enough to explain what it does.  Drop\nthat comment.\n\n>  \t/* Make objects we just wrote available to ourselves */\n>  \treprepare_packed_git();\n>  }\n"}]}