Make trailing LF following fast-import `data` commands optional
A few fast-import frontend developers have found it odd that we require the LF following a `data` command, especially in the exact byte count format. Technically we don't need this LF to parse the stream properly, but having it here does make the stream more readable to humans. We can easily make the LF optional by peeking at the next byte available from the stream and pushing it back into the buffer if its not LF. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Aug 1, 2007 at 00:24 UTC
2c570cde98cfde704ee67ea29031493c633f971e
3 files changed
+39
-5
Documentation/git-fast-import.txt
+9
-1
@@ -708,13 +708,18 @@ Exact byte count format::
708
+
709
....
710
'data' SP <count> LF
711
- <raw> LF
711
+ <raw> LF?
712
....
713
+
714
where `<count>` is the exact number of bytes appearing within
715
`<raw>`. The value of `<count>` is expressed as an ASCII decimal
716
integer. The `LF` on either side of `<raw>` is not
717
included in `<count>` and will not be included in the imported data.
718
++
719
+The `LF` after `<raw>` is optional (it used to be required) but
720
+recommended. Always including it makes debugging a fast-import
721
+stream easier as the next command always starts in column 0
722
+of the next line, even if `<raw>` did not end with an `LF`.
723
724
Delimited format::
725
A delimiter string is used to mark the end of the data.
@@ -726,6 +731,7 @@ Delimited format::
731
'data' SP '<<' <delim> LF
732
<raw> LF
733
<delim> LF
734
+ LF?
735
....
736
+
737
where `<delim>` is the chosen delimiter string. The string `<delim>`
@@ -734,6 +740,8 @@ fast-import will think the data ends earlier than it really does. The `LF`
740
immediately trailing `<raw>` is part of `<raw>`. This is one of
741
the limitations of the delimited format, it is impossible to supply
742
a data chunk which does not have an LF as its last byte.
743
++
744
+The `LF` after `<delim> LF` is optional (it used to be required).
745
746
`checkpoint`
747
~~~~~~~~~~~~
fast-import.c
+9
-4
@@ -61,7 +61,7 @@ Format of STDIN stream:
61
#
62
mark ::= 'mark' sp idnum lf;
63
data ::= (delimited_data | exact_data)
64
- lf;
64
+ lf?;
65
66
# note: delim may be any string but must not contain lf.
67
# data_line may contain any data but must not be exactly
@@ -1470,6 +1470,13 @@ static void read_next_command(void)
1470
} while (!command_buf.eof && command_buf.buf[0] == '#');
1471
}
1472
1473
+static void skip_optional_lf()
1474
+{
1475
+ int term_char = fgetc(stdin);
1476
+ if (term_char != '\n' && term_char != EOF)
1477
+ ungetc(term_char, stdin);
1478
+}
1479
+
1480
static void cmd_mark(void)
1481
{
1482
if (!prefixcmp(command_buf.buf, "mark :")) {
@@ -1522,9 +1529,7 @@ static void *cmd_data (size_t *size)
1529
}
1530
}
1531
1525
- if (fgetc(stdin) != '\n')
1526
- die("An lf did not trail the binary data as expected.");
1527
-
1532
+ skip_optional_lf();
1533
*size = length;
1534
return buffer;
1535
}
t/t9300-fast-import.sh
+21
@@ -818,4 +818,25 @@ test_expect_success \
818
'git-fast-import <input &&
819
test `git-rev-parse N3` = `git-rev-parse O1`'
820
821
+cat >input <<INPUT_END
822
+commit refs/heads/O2
823
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
824
+data <<COMMIT
825
+dirty directory copy
826
+COMMIT
827
+from refs/heads/branch^0
828
+M 644 inline file2/file5
829
+data <<EOF
830
+$file5_data
831
+EOF
832
+C file2 file3
833
+D file2/file5
834
+
835
+INPUT_END
836
+
837
+test_expect_success \
838
+ 'O: blank lines not necessary after data commands' \
839
+ 'git-fast-import <input &&
840
+ test `git-rev-parse N3` = `git-rev-parse O2`'
841
+
842
test_done