]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pkt-line: memorize sideband fragment in reader
authorJiang Xin <zhiyou.jx@alibaba-inc.com>
Sun, 17 Dec 2023 14:41:37 +0000 (22:41 +0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 18 Dec 2023 21:24:37 +0000 (13:24 -0800)
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>
pkt-line.c
pkt-line.h
t/t0070-fundamental.sh

index af83a19f4df5537da9f1c7a87ea29cb2f5903ea2..5943777a172b93e56bff4cea254395d4149317a0 100644 (file)
@@ -592,12 +592,11 @@ void packet_reader_init(struct packet_reader *reader, int fd,
        reader->options = options;
        reader->me = "git";
        reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
+       strbuf_init(&reader->scratch, 0);
 }
 
 enum packet_read_status packet_reader_read(struct packet_reader *reader)
 {
-       struct strbuf scratch = STRBUF_INIT;
-
        if (reader->line_peeked) {
                reader->line_peeked = 0;
                return reader->status;
@@ -620,7 +619,7 @@ enum packet_read_status packet_reader_read(struct packet_reader *reader)
                        break;
                if (demultiplex_sideband(reader->me, reader->status,
                                         reader->buffer, reader->pktlen, 1,
-                                        &scratch, &sideband_type))
+                                        &reader->scratch, &sideband_type))
                        break;
        }
 
index 954eec87197d3e0c50812879d47e0afbb008baa3..be1010d34e65de67a6ee0e958f0a867438d8c6a7 100644 (file)
@@ -194,6 +194,9 @@ struct packet_reader {
 
        /* hash algorithm in use */
        const struct git_hash_algo *hash_algo;
+
+       /* hold temporary sideband message */
+       struct strbuf scratch;
 };
 
 /*
index 297a7f772e3e21071d68ee79648ded42439b5b8c..275edbf6e75442d5da5cb891711c88414d375bd9 100755 (executable)
@@ -81,7 +81,7 @@ test_expect_success 'unpack-sideband: --chomp-newline (default)' '
        test_cmp expect-err err
 '
 
-test_expect_failure 'unpack-sideband: packet_reader_read() consumes sideband, no chomp payload' '
+test_expect_success 'unpack-sideband: packet_reader_read() consumes sideband, no chomp payload' '
        test_when_finished "rm -f expect-out expect-err" &&
        test-tool pkt-line send-split-sideband >split-sideband &&
        test-tool pkt-line unpack-sideband \