Skip to contentSkip to conversation

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

PATCH In review Sun He 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore

Sun He

author
#0 lore raw

PATCH 1/1 Rewrite bulk-checkin.c:finish_bulk_checkin() to use a strbuf for handling packname

---
 bulk-checkin.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
bulk-checkin.c +9 -1
index 118c625..8c47d71 100644
@@ -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();
 }
Signature · 1 line
1.7.1
#1 lore raw
Quoted 10 lines · Sun He
---
 bulk-checkin.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
bulk-checkin.c +0 -0
index 118c625..8c47d71 100644

bulk-checkin.c static struct bulk_checkin_state { · line 27

Quoted 6 lines
bulk-checkin.c +2 -1
@@ -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.

bulk-checkin.c static void finish_bulk_checkin(struct bulk_checkin_state *state) · line 51

Quoted 12 lines
bulk-checkin.c +4 -0
 	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.

bulk-checkin.c clear_exit: · line 63

Quoted 8 lines
bulk-checkin.c +2 -0
 	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.

Quoted 3 lines
bulk-checkin.c +0 -0
 	/* Make objects we just wrote available to ourselves */
 	reprepare_packed_git();
 }