# [PATCH] Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname

2 messages from 2014-02-27 to 2014-02-27. Participants: Sun He, Junio C Hamano.
Thread: https://gitlist.dev/t/35975

## Sun He, 2014-02-27 14:00

Subject: [PATCH] Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname
Message-ID: <1393509601-31312-1-git-send-email-sunheehnus@gmail.com>
URL: https://gitlist.dev/e/1393509601-31312-1-git-send-email-sunheehnus%40gmail.com

```

Signed-off-by: Sun He <sunheehnus@gmail.com>
---
 bulk-checkin.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/bulk-checkin.c b/bulk-checkin.c
index 118c625..8c47d71 100644
--- a/bulk-checkin.c
+++ b/bulk-checkin.c
@@ -23,7 +23,8 @@ static struct bulk_checkin_state {
 static void finish_bulk_checkin(struct bulk_checkin_state *state)
 {
 	unsigned char sha1[20];
-	char packname[PATH_MAX];
+	char *packname;
+    struct strbuf sb;
 	int i;
 
 	if (!state->f)
@@ -43,6 +44,10 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
 		close(fd);
 	}
 
+    /* 64-1 is more than the sum of len(sha1_to_hex(sha1)) and len(".pack") */
+    strbuf_init(&sb,strlen(get_object_directory())+64);
+    packname = sb.buf;
+
 	sprintf(packname, "%s/pack/pack-", get_object_directory());
 	finish_tmp_packfile(packname, state->pack_tmp_name,
 			    state->written, state->nr_written,
@@ -54,6 +59,9 @@ clear_exit:
 	free(state->written);
 	memset(state, 0, sizeof(*state));
 
+    /* release sb space */
+    strbuf_release(&sb);
+
 	/* Make objects we just wrote available to ourselves */
 	reprepare_packed_git();
 }
-- 
1.7.1

```

## Junio C Hamano, 2014-02-27 19:39

Subject: Re: [PATCH] Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname
Message-ID: <xmqq8uswz6np.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqq8uswz6np.fsf%40gitster.dls.corp.google.com
In-Reply-To: <1393509601-31312-1-git-send-email-sunheehnus@gmail.com>

```
Sun He <sunheehnus@gmail.com> writes:

> Signed-off-by: Sun He <sunheehnus@gmail.com>
> ---
>  bulk-checkin.c |   10 +++++++++-
>  1 files changed, 9 insertions(+), 1 deletions(-)
>
> diff --git a/bulk-checkin.c b/bulk-checkin.c
> index 118c625..8c47d71 100644
> --- a/bulk-checkin.c
> +++ b/bulk-checkin.c
> @@ -23,7 +23,8 @@ static struct bulk_checkin_state {
>  static void finish_bulk_checkin(struct bulk_checkin_state *state)
>  {
>  	unsigned char sha1[20];
> -	char packname[PATH_MAX];
> +	char *packname;
> +    struct strbuf sb;

Funny indentation.

>  	int i;
>  
>  	if (!state->f)
> @@ -43,6 +44,10 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
>  		close(fd);
>  	}
>  
> +    /* 64-1 is more than the sum of len(sha1_to_hex(sha1)) and len(".pack") */
> +    strbuf_init(&sb,strlen(get_object_directory())+64);
> +    packname = sb.buf;
> +
>  	sprintf(packname, "%s/pack/pack-", get_object_directory());

If you are using strbuf why not use strbuf_addf() instead?  Then you
do not have to worry about "Is 64-1 enough?" and things like that.

>  	finish_tmp_packfile(packname, state->pack_tmp_name,
>  			    state->written, state->nr_written,
> @@ -54,6 +59,9 @@ clear_exit:
>  	free(state->written);
>  	memset(state, 0, sizeof(*state));
>  
> +    /* release sb space */
> +    strbuf_release(&sb);

The function name is more than enough to explain what it does.  Drop
that comment.

>  	/* Make objects we just wrote available to ourselves */
>  	reprepare_packed_git();
>  }

```
