@matheusbsbs / matheus-bagliano / commits / eec813cfc6
Fork 0

fast-import was using dbuf's, replace them with strbuf's.

Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 17, 2007 at 13:48 UTC eec813cfc6409a1b72399acf7ac08283faad7b8f
1 file changed +68 -109
fast-import.c
+68 -109
@@ -250,12 +250,6 @@ struct tag
250 unsigned char sha1[20];
251 };
252
253 -struct dbuf
254 -{
255 - void *buffer;
256 - size_t capacity;
257 -};
258 -
253 struct hash_list
254 {
255 struct hash_list *next;
@@ -323,8 +317,8 @@ static unsigned int tree_entry_alloc = 1000;
317 static void *avail_tree_entry;
318 static unsigned int avail_tree_table_sz = 100;
319 static struct avail_tree_content **avail_tree_table;
326 -static struct dbuf old_tree;
327 -static struct dbuf new_tree;
320 +static struct strbuf old_tree = STRBUF_INIT;
321 +static struct strbuf new_tree = STRBUF_INIT;
322
323 /* Branch data */
324 static unsigned long max_active_branches = 5;
@@ -346,7 +340,7 @@ static struct recent_command *cmd_tail = &cmd_hist;
340 static struct recent_command *rc_free;
341 static unsigned int cmd_save = 100;
342 static uintmax_t next_mark;
349 -static struct dbuf new_data;
343 +static struct strbuf new_data = STRBUF_INIT;
344
345 static void write_branch_report(FILE *rpt, struct branch *b)
346 {
@@ -566,17 +560,6 @@ static char *pool_strdup(const char *s)
560 return r;
561 }
562
569 -static void size_dbuf(struct dbuf *b, size_t maxlen)
570 -{
571 - if (b->buffer) {
572 - if (b->capacity >= maxlen)
573 - return;
574 - free(b->buffer);
575 - }
576 - b->capacity = ((maxlen / 1024) + 1) * 1024;
577 - b->buffer = xmalloc(b->capacity);
578 -}
579 -
563 static void insert_mark(uintmax_t idnum, struct object_entry *oe)
564 {
565 struct mark_set *s = marks;
@@ -1005,8 +988,7 @@ static size_t encode_header(
988
989 static int store_object(
990 enum object_type type,
1008 - void *dat,
1009 - size_t datlen,
991 + struct strbuf *dat,
992 struct last_object *last,
993 unsigned char *sha1out,
994 uintmax_t mark)
@@ -1020,10 +1002,10 @@ static int store_object(
1002 z_stream s;
1003
1004 hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
1023 - (unsigned long)datlen) + 1;
1005 + (unsigned long)dat->len) + 1;
1006 SHA1_Init(&c);
1007 SHA1_Update(&c, hdr, hdrlen);
1026 - SHA1_Update(&c, dat, datlen);
1008 + SHA1_Update(&c, dat->buf, dat->len);
1009 SHA1_Final(sha1, &c);
1010 if (sha1out)
1011 hashcpy(sha1out, sha1);
@@ -1044,9 +1026,9 @@ static int store_object(
1026
1027 if (last && last->data && last->depth < max_depth) {
1028 delta = diff_delta(last->data, last->len,
1047 - dat, datlen,
1029 + dat->buf, dat->len,
1030 &deltalen, 0);
1049 - if (delta && deltalen >= datlen) {
1031 + if (delta && deltalen >= dat->len) {
1032 free(delta);
1033 delta = NULL;
1034 }
@@ -1059,8 +1041,8 @@ static int store_object(
1041 s.next_in = delta;
1042 s.avail_in = deltalen;
1043 } else {
1062 - s.next_in = dat;
1063 - s.avail_in = datlen;
1044 + s.next_in = (void *)dat->buf;
1045 + s.avail_in = dat->len;
1046 }
1047 s.avail_out = deflateBound(&s, s.avail_in);
1048 s.next_out = out = xmalloc(s.avail_out);
@@ -1083,8 +1065,8 @@ static int store_object(
1065
1066 memset(&s, 0, sizeof(s));
1067 deflateInit(&s, zlib_compression_level);
1086 - s.next_in = dat;
1087 - s.avail_in = datlen;
1068 + s.next_in = (void *)dat->buf;
1069 + s.avail_in = dat->len;
1070 s.avail_out = deflateBound(&s, s.avail_in);
1071 s.next_out = out = xrealloc(out, s.avail_out);
1072 while (deflate(&s, Z_FINISH) == Z_OK)
@@ -1118,7 +1100,7 @@ static int store_object(
1100 } else {
1101 if (last)
1102 last->depth = 0;
1121 - hdrlen = encode_header(type, datlen, hdr);
1103 + hdrlen = encode_header(type, dat->len, hdr);
1104 write_or_die(pack_data->pack_fd, hdr, hdrlen);
1105 pack_size += hdrlen;
1106 }
@@ -1131,9 +1113,9 @@ static int store_object(
1113 if (last) {
1114 if (!last->no_free)
1115 free(last->data);
1134 - last->data = dat;
1116 last->offset = e->offset;
1136 - last->len = datlen;
1117 + last->data = dat->buf;
1118 + last->len = dat->len;
1119 }
1120 return 0;
1121 }
@@ -1229,14 +1211,10 @@ static int tecmp1 (const void *_a, const void *_b)
1211 b->name->str_dat, b->name->str_len, b->versions[1].mode);
1212 }
1213
1232 -static void mktree(struct tree_content *t,
1233 - int v,
1234 - unsigned long *szp,
1235 - struct dbuf *b)
1214 +static void mktree(struct tree_content *t, int v, struct strbuf *b)
1215 {
1216 size_t maxlen = 0;
1217 unsigned int i;
1239 - char *c;
1218
1219 if (!v)
1220 qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
@@ -1248,27 +1226,22 @@ static void mktree(struct tree_content *t,
1226 maxlen += t->entries[i]->name->str_len + 34;
1227 }
1228
1251 - size_dbuf(b, maxlen);
1252 - c = b->buffer;
1229 + strbuf_reset(b);
1230 + strbuf_grow(b, maxlen);
1231 for (i = 0; i < t->entry_count; i++) {
1232 struct tree_entry *e = t->entries[i];
1233 if (!e->versions[v].mode)
1234 continue;
1257 - c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1258 - *c++ = ' ';
1259 - strcpy(c, e->name->str_dat);
1260 - c += e->name->str_len + 1;
1261 - hashcpy((unsigned char*)c, e->versions[v].sha1);
1262 - c += 20;
1235 + strbuf_addf(b, "%o %s%c", (unsigned int)e->versions[v].mode,
1236 + e->name->str_dat, '\0');
1237 + strbuf_add(b, e->versions[v].sha1, 20);
1238 }
1264 - *szp = c - (char*)b->buffer;
1239 }
1240
1241 static void store_tree(struct tree_entry *root)
1242 {
1243 struct tree_content *t = root->tree;
1244 unsigned int i, j, del;
1271 - unsigned long new_len;
1245 struct last_object lo;
1246 struct object_entry *le;
1247
@@ -1288,16 +1261,16 @@ static void store_tree(struct tree_entry *root)
1261 lo.depth = 0;
1262 lo.no_free = 0;
1263 } else {
1291 - mktree(t, 0, &lo.len, &old_tree);
1292 - lo.data = old_tree.buffer;
1264 + mktree(t, 0, &old_tree);
1265 + lo.len = old_tree.len;
1266 + lo.data = old_tree.buf;
1267 lo.offset = le->offset;
1268 lo.depth = t->delta_depth;
1269 lo.no_free = 1;
1270 }
1271
1298 - mktree(t, 1, &new_len, &new_tree);
1299 - store_object(OBJ_TREE, new_tree.buffer, new_len,
1300 - &lo, root->versions[1].sha1, 0);
1272 + mktree(t, 1, &new_tree);
1273 + store_object(OBJ_TREE, &new_tree, &lo, root->versions[1].sha1, 0);
1274
1275 t->delta_depth = lo.depth;
1276 for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
@@ -1642,11 +1615,10 @@ static void cmd_mark(void)
1615 next_mark = 0;
1616 }
1617
1645 -static void *cmd_data (size_t *size)
1618 +static void cmd_data(struct strbuf *sb)
1619 {
1647 - struct strbuf buffer;
1620 + strbuf_reset(sb);
1621
1649 - strbuf_init(&buffer, 0);
1622 if (prefixcmp(command_buf.buf, "data "))
1623 die("Expected 'data n' command, found: %s", command_buf.buf);
1624
@@ -1660,8 +1632,8 @@ static void *cmd_data (size_t *size)
1632 if (term_len == command_buf.len
1633 && !strcmp(term, command_buf.buf))
1634 break;
1663 - strbuf_addbuf(&buffer, &command_buf);
1664 - strbuf_addch(&buffer, '\n');
1635 + strbuf_addbuf(sb, &command_buf);
1636 + strbuf_addch(sb, '\n');
1637 }
1638 free(term);
1639 }
@@ -1671,7 +1643,7 @@ static void *cmd_data (size_t *size)
1643 length = strtoul(command_buf.buf + 5, NULL, 10);
1644
1645 while (n < length) {
1674 - size_t s = strbuf_fread(&buffer, length - n, stdin);
1646 + size_t s = strbuf_fread(sb, length - n, stdin);
1647 if (!s && feof(stdin))
1648 die("EOF in data (%lu bytes remaining)",
1649 (unsigned long)(length - n));
@@ -1680,8 +1652,6 @@ static void *cmd_data (size_t *size)
1652 }
1653
1654 skip_optional_lf();
1683 - *size = buffer.len;
1684 - return strbuf_detach(&buffer);
1655 }
1656
1657 static int validate_raw_date(const char *src, char *result, int maxlen)
@@ -1744,15 +1714,14 @@ static char *parse_ident(const char *buf)
1714
1715 static void cmd_new_blob(void)
1716 {
1747 - size_t l;
1748 - void *d;
1717 + struct strbuf buf;
1718
1719 read_next_command();
1720 cmd_mark();
1752 - d = cmd_data(&l);
1753 -
1754 - if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1755 - free(d);
1721 + strbuf_init(&buf, 0);
1722 + cmd_data(&buf);
1723 + if (store_object(OBJ_BLOB, &buf, &last_blob, NULL, next_mark))
1724 + strbuf_release(&buf);
1725 }
1726
1727 static void unload_one_branch(void)
@@ -1848,14 +1817,15 @@ static void file_change_m(struct branch *b)
1817 }
1818
1819 if (inline_data) {
1851 - size_t l;
1852 - void *d;
1820 + struct strbuf buf;
1821 +
1822 if (!p_uq)
1823 p = p_uq = xstrdup(p);
1824 read_next_command();
1856 - d = cmd_data(&l);
1857 - if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1858 - free(d);
1825 + strbuf_init(&buf, 0);
1826 + cmd_data(&buf);
1827 + if (store_object(OBJ_BLOB, &buf, &last_blob, sha1, 0))
1828 + strbuf_release(&buf);
1829 } else if (oe) {
1830 if (oe->type != OBJ_BLOB)
1831 die("Not a blob (actually a %s): %s",
@@ -2062,9 +2032,8 @@ static struct hash_list *cmd_merge(unsigned int *count)
2032
2033 static void cmd_new_commit(void)
2034 {
2035 + static struct strbuf msg = STRBUF_INIT;
2036 struct branch *b;
2066 - void *msg;
2067 - size_t msglen;
2037 char *sp;
2038 char *author = NULL;
2039 char *committer = NULL;
@@ -2089,7 +2058,7 @@ static void cmd_new_commit(void)
2058 }
2059 if (!committer)
2060 die("Expected committer but didn't get one");
2092 - msg = cmd_data(&msglen);
2061 + cmd_data(&msg);
2062 read_next_command();
2063 cmd_from(b);
2064 merge_list = cmd_merge(&merge_count);
@@ -2124,46 +2093,39 @@ static void cmd_new_commit(void)
2093 store_tree(&b->branch_tree);
2094 hashcpy(b->branch_tree.versions[0].sha1,
2095 b->branch_tree.versions[1].sha1);
2127 - size_dbuf(&new_data, 114 + msglen
2128 - + merge_count * 49
2129 - + (author
2130 - ? strlen(author) + strlen(committer)
2131 - : 2 * strlen(committer)));
2132 - sp = new_data.buffer;
2133 - sp += sprintf(sp, "tree %s\n",
2096 +
2097 + strbuf_reset(&new_data);
2098 + strbuf_addf(&new_data, "tree %s\n",
2099 sha1_to_hex(b->branch_tree.versions[1].sha1));
2100 if (!is_null_sha1(b->sha1))
2136 - sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
2101 + strbuf_addf(&new_data, "parent %s\n", sha1_to_hex(b->sha1));
2102 while (merge_list) {
2103 struct hash_list *next = merge_list->next;
2139 - sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
2104 + strbuf_addf(&new_data, "parent %s\n", sha1_to_hex(merge_list->sha1));
2105 free(merge_list);
2106 merge_list = next;
2107 }
2143 - sp += sprintf(sp, "author %s\n", author ? author : committer);
2144 - sp += sprintf(sp, "committer %s\n", committer);
2145 - *sp++ = '\n';
2146 - memcpy(sp, msg, msglen);
2147 - sp += msglen;
2108 + strbuf_addf(&new_data,
2109 + "author %s\n"
2110 + "committer %s\n"
2111 + "\n",
2112 + author ? author : committer, committer);
2113 + strbuf_addbuf(&new_data, &msg);
2114 free(author);
2115 free(committer);
2150 - free(msg);
2116
2152 - if (!store_object(OBJ_COMMIT,
2153 - new_data.buffer, sp - (char*)new_data.buffer,
2154 - NULL, b->sha1, next_mark))
2117 + if (!store_object(OBJ_COMMIT, &new_data, NULL, b->sha1, next_mark))
2118 b->pack_id = pack_id;
2119 b->last_commit = object_count_by_type[OBJ_COMMIT];
2120 }
2121
2122 static void cmd_new_tag(void)
2123 {
2124 + static struct strbuf msg = STRBUF_INIT;
2125 char *sp;
2126 const char *from;
2127 char *tagger;
2128 struct branch *s;
2165 - void *msg;
2166 - size_t msglen;
2129 struct tag *t;
2130 uintmax_t from_mark = 0;
2131 unsigned char sha1[20];
@@ -2214,24 +2176,21 @@ static void cmd_new_tag(void)
2176
2177 /* tag payload/message */
2178 read_next_command();
2217 - msg = cmd_data(&msglen);
2179 + cmd_data(&msg);
2180
2181 /* build the tag object */
2220 - size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
2221 - sp = new_data.buffer;
2222 - sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
2223 - sp += sprintf(sp, "type %s\n", commit_type);
2224 - sp += sprintf(sp, "tag %s\n", t->name);
2225 - sp += sprintf(sp, "tagger %s\n", tagger);
2226 - *sp++ = '\n';
2227 - memcpy(sp, msg, msglen);
2228 - sp += msglen;
2182 + strbuf_reset(&new_data);
2183 + strbuf_addf(&new_data,
2184 + "object %s\n"
2185 + "type %s\n"
2186 + "tag %s\n"
2187 + "tagger %s\n"
2188 + "\n",
2189 + sha1_to_hex(sha1), commit_type, t->name, tagger);
2190 + strbuf_addbuf(&new_data, &msg);
2191 free(tagger);
2230 - free(msg);
2192
2232 - if (store_object(OBJ_TAG, new_data.buffer,
2233 - sp - (char*)new_data.buffer,
2234 - NULL, t->sha1, 0))
2193 + if (store_object(OBJ_TAG, &new_data, NULL, t->sha1, 0))
2194 t->pack_id = MAX_PACK_ID;
2195 else
2196 t->pack_id = pack_id;