pkt-line: memorize sideband fragment in reader

When we turn on the "use_sideband" field of the packet_reader, "packet_reader_read()" will call the function "demultiplex_sideband()" to parse and consume sideband messages. Sideband fragment which does not end with "\r" or "\n" will be saved in the sixth parameter "scratch" and it can be reused and be concatenated when parsing another sideband message. In "packet_reader_read()" function, the local variable "scratch" can only be reused by subsequent sideband messages. But if there is a payload message between two sideband fragments, the first fragment which is saved in the local variable "scratch" will be lost. To solve this problem, we can add a new field "scratch" in packet_reader to memorize the sideband fragment across different calls of "packet_reader_read()". Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jiang Xin committed Dec 17, 2023 at 22:41 UTC 64220dc5f7450d8eca471fa484965b1dcddc2827
3 files changed +6 -4
pkt-line.c
+2 -3
@@ -592,12 +592,11 @@ void packet_reader_init(struct packet_reader *reader, int fd,
592 reader->options = options;
593 reader->me = "git";
594 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
595 + strbuf_init(&reader->scratch, 0);
596 }
597
598 enum packet_read_status packet_reader_read(struct packet_reader *reader)
599 {
599 - struct strbuf scratch = STRBUF_INIT;
600 -
600 if (reader->line_peeked) {
601 reader->line_peeked = 0;
602 return reader->status;
@@ -620,7 +619,7 @@ enum packet_read_status packet_reader_read(struct packet_reader *reader)
619 break;
620 if (demultiplex_sideband(reader->me, reader->status,
621 reader->buffer, reader->pktlen, 1,
623 - &scratch, &sideband_type))
622 + &reader->scratch, &sideband_type))
623 break;
624 }
625
pkt-line.h
+3
@@ -194,6 +194,9 @@ struct packet_reader {
194
195 /* hash algorithm in use */
196 const struct git_hash_algo *hash_algo;
197 +
198 + /* hold temporary sideband message */
199 + struct strbuf scratch;
200 };
201
202 /*
t/t0070-fundamental.sh
+1 -1
@@ -81,7 +81,7 @@ test_expect_success 'unpack-sideband: --chomp-newline (default)' '
81 test_cmp expect-err err
82 '
83
84 -test_expect_failure 'unpack-sideband: packet_reader_read() consumes sideband, no chomp payload' '
84 +test_expect_success 'unpack-sideband: packet_reader_read() consumes sideband, no chomp payload' '
85 test_when_finished "rm -f expect-out expect-err" &&
86 test-tool pkt-line send-split-sideband >split-sideband &&
87 test-tool pkt-line unpack-sideband \