test-pkt-line: add unpack-sideband subcommand

Add an 'unpack-sideband' subcommand to the test-pkt-line helper to enable unpacking packet line data sent multiplexed using a sideband. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Jun 20, 2018 at 14:32 UTC 4bcd37d3bc436f202307897093d5ca162a688be2
1 file changed +33
t/helper/test-pkt-line.c
+33
@@ -1,3 +1,4 @@
1 +#include "cache.h"
2 #include "pkt-line.h"
3
4 static void pack_line(const char *line)
@@ -48,6 +49,36 @@ static void unpack(void)
49 }
50 }
51
52 +static void unpack_sideband(void)
53 +{
54 + struct packet_reader reader;
55 + packet_reader_init(&reader, 0, NULL, 0,
56 + PACKET_READ_GENTLE_ON_EOF |
57 + PACKET_READ_CHOMP_NEWLINE);
58 +
59 + while (packet_reader_read(&reader) != PACKET_READ_EOF) {
60 + int band;
61 + int fd;
62 +
63 + switch (reader.status) {
64 + case PACKET_READ_EOF:
65 + break;
66 + case PACKET_READ_NORMAL:
67 + band = reader.line[0] & 0xff;
68 + if (band < 1 || band > 2)
69 + die("unexpected side band %d", band);
70 + fd = band;
71 +
72 + write_or_die(fd, reader.line + 1, reader.pktlen - 1);
73 + break;
74 + case PACKET_READ_FLUSH:
75 + return;
76 + case PACKET_READ_DELIM:
77 + break;
78 + }
79 + }
80 +}
81 +
82 int cmd_main(int argc, const char **argv)
83 {
84 if (argc < 2)
@@ -57,6 +88,8 @@ int cmd_main(int argc, const char **argv)
88 pack(argc - 2, argv + 2);
89 else if (!strcmp(argv[1], "unpack"))
90 unpack();
91 + else if (!strcmp(argv[1], "unpack-sideband"))
92 + unpack_sideband();
93 else
94 die("invalid argument '%s'", argv[1]);
95