@matheusbsbs / matheus-bagliano / commits / 1fdb649c6a
Fork 0

Make trailing LF optional for all fast-import commands

For the same reasons as the prior change we want to allow frontends to omit the trailing LF that usually delimits commands. In some cases these just make the input stream more verbose looking than it needs to be, and its just simpler for the frontend developer to get started if our parser is slightly more lenient about where an LF is required and where it isn't. To make this optional LF feature work we now have to buffer up to one line of input in command_buf. This buffering can happen if we look at the current input command but don't recognize it at this point in the code. In such a case we need to "unget" the entire line, but we cannot depend upon the stdio library to let us do ungetc() for that many characters at once. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Aug 1, 2007 at 02:22 UTC 1fdb649c6ac4cfc536983077b4851a1959cbc1c4
3 files changed +74 -17
Documentation/git-fast-import.txt
+8 -3
@@ -312,7 +312,7 @@ change to the project.
312 ('from' SP <committish> LF)?
313 ('merge' SP <committish> LF)?
314 (filemodify | filedelete | filecopy | filerename | filedeleteall)*
315 - LF
315 + LF?
316 ....
317
318 where `<ref>` is the name of the branch to make the commit on.
@@ -343,6 +343,8 @@ all `filemodify`, `filecopy` and `filerename` commands in the same
343 commit, as `filedeleteall`
344 wipes the branch clean (see below).
345
346 +The `LF` after the command is optional (it used to be required).
347 +
348 `author`
349 ^^^^^^^^
350 An `author` command may optionally appear, if the author information
@@ -654,12 +656,14 @@ branch from an existing commit without creating a new commit.
656 ....
657 'reset' SP <ref> LF
658 ('from' SP <committish> LF)?
657 - LF
659 + LF?
660 ....
661
662 For a detailed description of `<ref>` and `<committish>` see above
663 under `commit` and `from`.
664
665 +The `LF` after the command is optional (it used to be required).
666 +
667 The `reset` command can also be used to create lightweight
668 (non-annotated) tags. For example:
669
@@ -750,7 +754,7 @@ save out all current branch refs, tags and marks.
754
755 ....
756 'checkpoint' LF
753 - LF
757 + LF?
758 ....
759
760 Note that fast-import automatically switches packfiles when the current
@@ -769,6 +773,7 @@ process access to a branch. However given that a 30 GiB Subversion
773 repository can be loaded into Git through fast-import in about 3 hours,
774 explicit checkpointing may not be necessary.
775
776 +The `LF` after the command is optional (it used to be required).
777
778 Tips and Tricks
779 ---------------
fast-import.c
+20 -14
@@ -23,7 +23,7 @@ Format of STDIN stream:
23 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
24 ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
25 file_change*
26 - lf;
26 + lf?;
27 commit_msg ::= data;
28
29 file_change ::= file_clr
@@ -48,10 +48,10 @@ Format of STDIN stream:
48
49 reset_branch ::= 'reset' sp ref_str lf
50 ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
51 - lf;
51 + lf?;
52
53 checkpoint ::= 'checkpoint' lf
54 - lf;
54 + lf?;
55
56 # note: the first idnum in a stream should be 1 and subsequent
57 # idnums should not have gaps between values as this will cause
@@ -330,6 +330,7 @@ static struct tag *last_tag;
330 /* Input stream parsing */
331 static whenspec_type whenspec = WHENSPEC_RAW;
332 static struct strbuf command_buf;
333 +static int unread_command_buf;
334 static uintmax_t next_mark;
335 static struct dbuf new_data;
336
@@ -1466,7 +1467,10 @@ static void dump_marks(void)
1467 static void read_next_command(void)
1468 {
1469 do {
1469 - read_line(&command_buf, stdin, '\n');
1470 + if (unread_command_buf)
1471 + unread_command_buf = 0;
1472 + else
1473 + read_line(&command_buf, stdin, '\n');
1474 } while (!command_buf.eof && command_buf.buf[0] == '#');
1475 }
1476
@@ -1825,13 +1829,13 @@ static void cmd_from_existing(struct branch *b)
1829 }
1830 }
1831
1828 -static void cmd_from(struct branch *b)
1832 +static int cmd_from(struct branch *b)
1833 {
1834 const char *from;
1835 struct branch *s;
1836
1837 if (prefixcmp(command_buf.buf, "from "))
1834 - return;
1838 + return 0;
1839
1840 if (b->branch_tree.tree) {
1841 release_tree_content_recursive(b->branch_tree.tree);
@@ -1866,6 +1870,7 @@ static void cmd_from(struct branch *b)
1870 die("Invalid ref name or SHA1 expression: %s", from);
1871
1872 read_next_command();
1873 + return 1;
1874 }
1875
1876 static struct hash_list *cmd_merge(unsigned int *count)
@@ -1950,10 +1955,8 @@ static void cmd_new_commit(void)
1955 }
1956
1957 /* file_change* */
1953 - for (;;) {
1954 - if (1 == command_buf.len)
1955 - break;
1956 - else if (!prefixcmp(command_buf.buf, "M "))
1958 + while (!command_buf.eof && command_buf.len > 1) {
1959 + if (!prefixcmp(command_buf.buf, "M "))
1960 file_change_m(b);
1961 else if (!prefixcmp(command_buf.buf, "D "))
1962 file_change_d(b);
@@ -1963,8 +1966,10 @@ static void cmd_new_commit(void)
1966 file_change_cr(b, 0);
1967 else if (!strcmp("deleteall", command_buf.buf))
1968 file_change_deleteall(b);
1966 - else
1967 - die("Unsupported file_change: %s", command_buf.buf);
1969 + else {
1970 + unread_command_buf = 1;
1971 + break;
1972 + }
1973 read_next_command();
1974 }
1975
@@ -2105,7 +2110,8 @@ static void cmd_reset_branch(void)
2110 else
2111 b = new_branch(sp);
2112 read_next_command();
2108 - cmd_from(b);
2113 + if (!cmd_from(b) && command_buf.len > 1)
2114 + unread_command_buf = 1;
2115 }
2116
2117 static void cmd_checkpoint(void)
@@ -2116,7 +2122,7 @@ static void cmd_checkpoint(void)
2122 dump_tags();
2123 dump_marks();
2124 }
2119 - read_next_command();
2125 + skip_optional_lf();
2126 }
2127
2128 static void import_marks(const char *input_file)
t/t9300-fast-import.sh
+46
@@ -839,4 +839,50 @@ test_expect_success \
839 'git-fast-import <input &&
840 test `git-rev-parse N3` = `git-rev-parse O2`'
841
842 +test_expect_success \
843 + 'O: repack before next test' \
844 + 'git repack -a -d'
845 +
846 +cat >input <<INPUT_END
847 +commit refs/heads/O3
848 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
849 +data <<COMMIT
850 +zstring
851 +COMMIT
852 +commit refs/heads/O3
853 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
854 +data <<COMMIT
855 +zof
856 +COMMIT
857 +checkpoint
858 +commit refs/heads/O3
859 +mark :5
860 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
861 +data <<COMMIT
862 +zempty
863 +COMMIT
864 +checkpoint
865 +commit refs/heads/O3
866 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
867 +data <<COMMIT
868 +zcommits
869 +COMMIT
870 +reset refs/tags/O3-2nd
871 +from :5
872 +INPUT_END
873 +
874 +cat >expect <<INPUT_END
875 +string
876 +of
877 +empty
878 +commits
879 +INPUT_END
880 +test_expect_success \
881 + 'O: blank lines not necessary after other commands' \
882 + 'git-fast-import <input &&
883 + test 8 = `find .git/objects/pack -type f | wc -l` &&
884 + test `git rev-parse refs/tags/O3-2nd` = `git rev-parse O3^` &&
885 + git log --reverse --pretty=oneline O3 | sed s/^.*z// >actual &&
886 + git diff expect actual'
887 +
888 test_done