Skip to content

Commit 4a241d7

Browse files
MadCodergitster
authored andcommitted
fast-import: Use strbuf API, and simplify cmd_data()
This patch features the use of strbuf_detach, and prevent the programmer to mess with allocation directly. The code is as efficent as before, just more concise and more straightforward. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a604f1 commit 4a241d7

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

fast-import.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ static struct tag *last_tag;
340340

341341
/* Input stream parsing */
342342
static whenspec_type whenspec = WHENSPEC_RAW;
343-
static struct strbuf command_buf;
343+
static struct strbuf command_buf = STRBUF_INIT;
344344
static int unread_command_buf;
345345
static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
346346
static struct recent_command *cmd_tail = &cmd_hist;
@@ -1638,39 +1638,35 @@ static void cmd_mark(void)
16381638

16391639
static void *cmd_data (size_t *size)
16401640
{
1641-
size_t length;
1642-
char *buffer;
1641+
struct strbuf buffer;
16431642

1643+
strbuf_init(&buffer);
16441644
if (prefixcmp(command_buf.buf, "data "))
16451645
die("Expected 'data n' command, found: %s", command_buf.buf);
16461646

16471647
if (!prefixcmp(command_buf.buf + 5, "<<")) {
16481648
char *term = xstrdup(command_buf.buf + 5 + 2);
1649-
size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1650-
length = 0;
1651-
buffer = xmalloc(sz);
1649+
size_t term_len = command_buf.len - 5 - 2;
1650+
16521651
for (;;) {
16531652
read_line(&command_buf, stdin, '\n');
16541653
if (command_buf.eof)
16551654
die("EOF in data (terminator '%s' not found)", term);
16561655
if (term_len == command_buf.len
16571656
&& !strcmp(term, command_buf.buf))
16581657
break;
1659-
ALLOC_GROW(buffer, length + command_buf.len + 1, sz);
1660-
memcpy(buffer + length,
1661-
command_buf.buf,
1662-
command_buf.len);
1663-
length += command_buf.len;
1664-
buffer[length++] = '\n';
1658+
strbuf_addbuf(&buffer, &command_buf);
1659+
strbuf_addch(&buffer, '\n');
16651660
}
16661661
free(term);
16671662
}
16681663
else {
1669-
size_t n = 0;
1664+
size_t n = 0, length;
1665+
16701666
length = strtoul(command_buf.buf + 5, NULL, 10);
1671-
buffer = xmalloc(length);
1667+
16721668
while (n < length) {
1673-
size_t s = fread(buffer + n, 1, length - n, stdin);
1669+
size_t s = strbuf_fread(&buffer, length - n, stdin);
16741670
if (!s && feof(stdin))
16751671
die("EOF in data (%lu bytes remaining)",
16761672
(unsigned long)(length - n));
@@ -1679,8 +1675,8 @@ static void *cmd_data (size_t *size)
16791675
}
16801676

16811677
skip_optional_lf();
1682-
*size = length;
1683-
return buffer;
1678+
*size = buffer.len;
1679+
return strbuf_detach(&buffer);
16841680
}
16851681

16861682
static int validate_raw_date(const char *src, char *result, int maxlen)

0 commit comments

Comments
 (0)