@matheusbsbs / matheus-bagliano / commits / 3b4dce0275
Fork 0

Support delimited data regions in fast-import.

During testing its nice to not have to feed the length of a data chunk to the 'data' command of fast-import. Instead we would prefer to be able to establish a data chunk much like shell's << operator and use a line delimiter to denote the end of the input. So now if a data command is started as 'data <<EOF' we will look for a terminator line containing only the string EOF on that line. Once found, we stop the data command. Everything between the two lines is used as the data value. The 'data <<' syntax is slower than 'data n', as we don't know how many bytes to expect and instead must grow our buffer on the fly. It also has the problem that the frontend must use a string which will not appear on a line by itself in the input, and the data region will always end in an LF. For these reasons real import frontends are encouraged to continue to use _only_ 'data n'. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jan 18, 2007 at 13:14 UTC 3b4dce02752d37c3cef9308eefb01ed758efe323
1 file changed +49 -15
fast-import.c
+49 -15
@@ -50,14 +50,21 @@ Format of STDIN stream:
50 # a new mark directive with the old idnum.
51 #
52 mark ::= 'mark' sp idnum lf;
53 + data ::= (delimited_data | exact_data)
54 + lf;
55 +
56 + # note: delim may be any string but must not contain lf.
57 + # data_line may contain any data but must not be exactly
58 + # delim.
59 + delimited_data ::= 'data' sp '<<' delim lf
60 + (data_line lf)*
61 + delim lf;
62
63 # note: declen indicates the length of binary_data in bytes.
55 - # declen does not include the lf preceeding or trailing the
56 - # binary data.
64 + # declen does not include the lf preceeding the binary data.
65 #
58 - data ::= 'data' sp declen lf
59 - binary_data
60 - lf;
66 + exact_data ::= 'data' sp declen lf
67 + binary_data;
68
69 # note: quoted strings are C-style quoting supporting \c for
70 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
@@ -1334,21 +1341,48 @@ static void cmd_mark(void)
1341
1342 static void* cmd_data (size_t *size)
1343 {
1337 - size_t n = 0;
1338 - void *buffer;
1344 size_t length;
1345 + char *buffer;
1346
1347 if (strncmp("data ", command_buf.buf, 5))
1348 die("Expected 'data n' command, found: %s", command_buf.buf);
1349
1344 - length = strtoul(command_buf.buf + 5, NULL, 10);
1345 - buffer = xmalloc(length);
1346 -
1347 - while (n < length) {
1348 - size_t s = fread((char*)buffer + n, 1, length - n, stdin);
1349 - if (!s && feof(stdin))
1350 - die("EOF in data (%lu bytes remaining)", length - n);
1351 - n += s;
1350 + if (!strncmp("<<", command_buf.buf + 5, 2)) {
1351 + char *term = xstrdup(command_buf.buf + 5 + 2);
1352 + size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1353 + length = 0;
1354 + buffer = xmalloc(sz);
1355 + for (;;) {
1356 + read_next_command();
1357 + if (command_buf.eof)
1358 + die("EOF in data (terminator '%s' not found)", term);
1359 + if (term_len == command_buf.len
1360 + && !strcmp(term, command_buf.buf))
1361 + break;
1362 + if (sz < (length + command_buf.len)) {
1363 + sz = sz * 3 / 2 + 16;
1364 + if (sz < (length + command_buf.len))
1365 + sz = length + command_buf.len;
1366 + buffer = xrealloc(buffer, sz);
1367 + }
1368 + memcpy(buffer + length,
1369 + command_buf.buf,
1370 + command_buf.len - 1);
1371 + length += command_buf.len - 1;
1372 + buffer[length++] = '\n';
1373 + }
1374 + free(term);
1375 + }
1376 + else {
1377 + size_t n = 0;
1378 + length = strtoul(command_buf.buf + 5, NULL, 10);
1379 + buffer = xmalloc(length);
1380 + while (n < length) {
1381 + size_t s = fread(buffer + n, 1, length - n, stdin);
1382 + if (!s && feof(stdin))
1383 + die("EOF in data (%lu bytes remaining)", length - n);
1384 + n += s;
1385 + }
1386 }
1387
1388 if (fgetc(stdin) != '\n')