17 Commits

Author SHA1 Message Date
Billy Keyes 981bc4b60c Fix parsing of mode lines with trailing space (#38)
If a patch is passed through a system that converts line endings to
'\r\n', mode lines end up with trailing whitespace that confuses
strconv.ParseInt. In Git, this is avoided by using strtoul() for parsing,
which stops at the first non-digit character.

Changing line endings in patch content itself will cause other problems
so it is best to avoid this transform, but if it does happen, it
shouldn't cause a parse error.
2022-12-26 17:56:30 -05:00
Billy Keyes b6a90110a3 Fix out-of-bounds panic parsing timestamps (#28)
Found by go-fuzz.
2021-09-09 14:24:16 -04:00
Billy Keyes 3772c9eb65 Fix parsing for file names with spaces (#24)
Git does not quote file names containing spaces when generating header
lines. However, I misread the Git implementation and thought it used
spaces as a name terminator by default, implying that names with spaces
would be quoted.

Rewrite the the name parsing code to better match Git. Specifically,
when both names in a header are not quoted, test all splits to see if
any of them produce two equal names. Also account for spaces in file
names when parsing file metadata.

Finally, allow trailing spaces on file names, which seem to be allowed
by Git (although this sounds like a terrible idea to me.)
2021-04-24 15:23:31 -07:00
Billy Keyes d42fb0e93f Fix spelling mistakes, add Go Report Card badge 2019-04-14 19:53:39 -07:00
Billy Keyes 7478889ab4 Do not accept io.EOF when expecting an error
This should almost always be handled specially, so don't let tests that
expect errors hide the fact that they returned an io.EOF. This could be
revisited if the tests are ever updated to check for specific errors.
2019-04-01 22:58:20 -07:00
Billy Keyes 8753644cc0 Add tests for single fragment parsing
To make output better, also add some (temporary?) String() functions and
fix an assumption about the smallest fragment header I discovered was
wrong while looking at sample patches.
2019-03-31 21:32:39 -07:00
Billy Keyes 9b0fc30459 Document and enforce parser invariants
After considering fragment parsing, it made sense to change the
invariant estabilished in the previous commit. Specifically, parser
functions now assume they are call on the first line of their object and
return with the parser on the first line after their object. This means
code can call parse function immediately after each other without
advancing the parser in between.

It's possible this will change back later on... there seem to be
annoying edge cases with either choice, but I think making the functions
consistent is important.
2019-03-27 23:10:47 -07:00
Billy Keyes e04aad2256 Fix parser advancement in header functions
After parsing a valid header, the current line of the parser should be
the last line of the header. If the header is invalid (the parse
function returns an error), the state of the parser is undefined.

Also add a test since this is easy to mess up.
2019-03-26 21:56:02 -07:00
Billy Keyes 4b25230457 Check for expected input in parsing functions
Instead of checking that a line has a certain prefix before calling a
header parsing function, the functions now check this and return nil
objects when called on the wrong line type. If the line passes this
basic check but is still invalid, and error is returned as before.
2019-03-25 22:27:31 -07:00
Billy Keyes dc8fb6fdb5 Support adjustable read-ahead in parser
At the moment, we need only to read three lines, but now the value is
easy to adjust as needed. I've only seen the Git implementation read two
lines ahead so far, but it has the whole input in memory and may read
more in other places.
2019-03-25 21:30:08 -07:00
Billy Keyes ccf0c58db8 Change parser interface to be more iterator-like
Call Next() to advance the parser state until it returns a non-nil
error, then check if the error is io.EOF. This makes EOF handling easier
and also means that Line() and PeekLine() can be called multiple times
without changing state.

The next step is to update parse functions to return an internal marker
error if they are called on an invalid line. This should improve
correctness and remove the duplication of testing a condition and then
calling a parse function, which checks the same condition.
2019-03-24 22:03:53 -07:00
Billy Keyes 6b7e4cd811 Move all header functions to file_header.go
Even though these are defined on *parser, it makes more sense to have
them in this file.
2019-03-21 22:51:19 -07:00
Billy Keyes cf2f946a66 Implement traditional file header parsing
While I think this works, this is mostly for completeness. I (and I
expect most other users, if any) intend to use this with git patches.
2019-03-21 22:47:52 -07:00
Billy Keyes e7719741fc Allow percent sign on similarity index lines 2019-03-20 22:01:52 -07:00
Billy Keyes c818281d3a Always accept newline as a name terminator
This avoids including the newline character as part of the name when it
is the last name on a line. Also split out functions for quoted and
unquoted names to clarify the logic.
2019-03-20 21:52:13 -07:00
Billy Keyes aa3e0d357e Clarify behavior when parsing "index" lines
The parsed OIDs are usually prefixes, not full hashes, so rename the
fields appropriately. Also note that values that are too long to be
valid OIDs are still accepted by the library.
2019-03-19 22:35:52 -07:00
Billy Keyes b8b0c25c1e Move file header functions to a new file 2019-03-19 22:00:11 -07:00