Also parse "done" lines

This works well for now, but if we're going to support
multi_ack_detailed at some point, we need a better way of parsing the
particular pkt.
This commit is contained in:
Carlos Martín Nieto
2013-05-31 13:56:49 +02:00
parent 3ec3c454c8
commit bd510de1e3
2 changed files with 20 additions and 6 deletions

View File

@@ -56,12 +56,21 @@ do_unpack(Len, Rest) ->
parse_pkt(In, 0) ->
{flush, In};
parse_pkt(In, Len) ->
LenLF = Len - 5 - ?SHA_LEN, % "want " | "want " + sha, remove LF if it's there
{Type, Rest0} = pkt_type(In),
<<Sha:?SHA_LEN/binary, _:LenLF/binary, Rest/binary>> = Rest0,
{{Type, geef_oid:parse(Sha)}, Rest}.
{Type, NameLen, Rest0} = pkt_type(In),
LenLF = Len - NameLen, % lets us optionally clean the LF
{Pkt, Rest1} = case Type of
done ->
{done, Rest0};
_ ->
<<Sha:?SHA_LEN/binary, R/binary>> = Rest0,
{{Type, geef_oid:parse(Sha)}, R}
end,
<<_:LenLF/binary, Rest/binary>> = Rest1,
{Pkt, Rest}.
pkt_type(<<"have ", Rest/binary>>) ->
{have, Rest};
{have, 5 + ?SHA_LEN, Rest};
pkt_type(<<"want ", Rest/binary>>) ->
{want, Rest}.
{want, 5 + ?SHA_LEN, Rest};
pkt_type(<<"done", Rest/binary>>) ->
{done, 4, Rest}.

View File

@@ -8,6 +8,11 @@ flush_test() ->
Actual = geef_pkt:parse("0000"),
?assertEqual(Expected, Actual).
done_test() ->
Expected = {done, <<>>},
Actual = geef_pkt:parse("0009done\n"),
?assertEqual(Expected, Actual).
have_test() ->
Expected = {{want, geef_oid:parse("e17ca7f2d877acbf8b9a9a1cb4c243ca72e86463")}, <<>>},
Actual = geef_pkt:parse("0032want e17ca7f2d877acbf8b9a9a1cb4c243ca72e86463\n"),