From patchwork Sun Dec 17 14:41:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Xin X-Patchwork-Id: 13495890 Received: from mail-il1-f170.google.com (mail-il1-f170.google.com [209.85.166.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9928E446C6 for ; Sun, 17 Dec 2023 14:41:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jwEv4e7Q" Received: by mail-il1-f170.google.com with SMTP id e9e14a558f8ab-35d82fb7e86so7156725ab.2 for ; Sun, 17 Dec 2023 06:41:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702824101; x=1703428901; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1mEXw1xVmZxwcgAP/Spwp2DIwcfIyU1eJ67eSxjKcis=; b=jwEv4e7QPGjxL3TGDx92fnLMdM0HhA4LkDXd1Mh5eR872bFwCWVYe5zkxSBX/yciy6 R6yr/k49+8rCPTtt2jReX5TAFktV0aedrUOJ1dbeA0s8Ck09WfdNyMLz03i4358WS4pb yuBNcfyetz4+BzbhpP8xVakIvxGv4O7ZCkcn4ejijf2vp/53Osmuo1/84yfPoYbh7dTp oUPtPfxV8ykz4gwG+GAD/8FuD6MUooLhDZH63Ja4OmYVPy9x1xuxNdBvLCvjAMOIEHxq 7cwdLKBpUEtYeIxXAB5FWkjzTgHl6d7qdrOf9joPy1OSScRw7c8/cBGXaM3VDn3jFelu GVEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702824101; x=1703428901; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1mEXw1xVmZxwcgAP/Spwp2DIwcfIyU1eJ67eSxjKcis=; b=GYwkqNLvExXUG981cP5VJaV/sVZIsVFNNxnFtpiGPZ7MRSrLnPrdvhzWTK013fzR09 sqhZu97SUKK6Usxenj0WYJKsxzdVl7GnFiQyH5UhtbC3uluhUfFSfs2KMAFzwPATm2e2 g7veR8TY643GoX+OzaSCBnfHtuSX5cFwrxi0Oo6yDJNvtNzbma9NIfXUKOY2HFyFnrFC YuUg7A4gh1ZOGNlRIeuvU9oDeLYdSnJpRm/Jk6UO0hNKLVhqRg23U4c1iSTEEzRz5wx9 teWQpplWMI/h+8RRGrOj5xBW7KrK8reJxrpmCIGlHOAJv0sMbl723yP65WhERHAs0/Es /GSg== X-Gm-Message-State: AOJu0YwFm42aoOhnC8FACw8URstk1T80fslYnwMfwfLrTGrSBVbrwUQ0 VRhVOUf5jmYqss+IoQJ+/Bjh2rbkjrQ= X-Google-Smtp-Source: AGHT+IFpKRvVqR3J4zvOO65hvOER1n/wPp86dmxXMQsQfjEz1OLqr5U4YP7b6fpJZCKigWByQmNjUQ== X-Received: by 2002:a92:cdaa:0:b0:35f:84b8:c1a1 with SMTP id g10-20020a92cdaa000000b0035f84b8c1a1mr6813414ild.4.1702824101526; Sun, 17 Dec 2023 06:41:41 -0800 (PST) Received: from tigtog-proxy.localdomain.localdomain (144.34.163.219.16clouds.com. [144.34.163.219]) by smtp.gmail.com with ESMTPSA id i4-20020a63cd04000000b005c1ce3c960bsm16276657pgg.50.2023.12.17.06.41.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Dec 2023 06:41:41 -0800 (PST) From: Jiang Xin To: Git List , Junio C Hamano , Jonathan Tan , Oswald Buddenhagen Cc: Jiang Xin Subject: [PATCH v4 1/3] test-pkt-line: add option parser for unpack-sideband Date: Sun, 17 Dec 2023 22:41:36 +0800 Message-Id: X-Mailer: git-send-email 2.32.0.rc3 In-Reply-To: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Jiang Xin We can use the test helper program "test-tool pkt-line" to test pkt-line related functions. E.g.: * Use "test-tool pkt-line send-split-sideband" to generate sideband messages. * Pipe these generated sideband messages to command "test-tool pkt-line unpack-sideband" to test packet_reader_read() function. In order to make a complete test of the packet_reader_read() function, add option parser for command "test-tool pkt-line unpack-sideband". * To remove newlines in sideband messages, we can use: $ test-tool pkt-line unpack-sideband --chomp-newline * To preserve newlines in sideband messages, we can use: $ test-tool pkt-line unpack-sideband --no-chomp-newline * To parse sideband messages using "demultiplex_sideband()" inside the function "packet_reader_read()", we can use: $ test-tool pkt-line unpack-sideband --reader-use-sideband We also add new example sideband packets in send_split_sideband() and add several new test cases in t0070. Among these test cases, we pipe output of the "send-split-sideband" subcommand to the "unpack-sideband" subcommand. We found two issues: 1. The two splitted sideband messages "Hello," and " world!\n" should be concatenated together. But when we turn on use_sideband field of reader to parse sideband messages, the first part of the splitted message ("Hello,") is lost. 2. The newline characters in sideband 2 (progress info) and sideband 3 (error message) should be preserved, but they are both trimmed. Will fix the above two issues in subsequent commits. Signed-off-by: Jiang Xin --- t/helper/test-pkt-line.c | 59 ++++++++++++++++++++++++++++++++++++---- t/t0070-fundamental.sh | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 5 deletions(-) diff --git a/t/helper/test-pkt-line.c b/t/helper/test-pkt-line.c index f4d134a145..6b306cf5ca 100644 --- a/t/helper/test-pkt-line.c +++ b/t/helper/test-pkt-line.c @@ -2,6 +2,7 @@ #include "test-tool.h" #include "pkt-line.h" #include "write-or-die.h" +#include "parse-options.h" static void pack_line(const char *line) { @@ -64,12 +65,33 @@ static void unpack(void) } } -static void unpack_sideband(void) +static void unpack_sideband(int argc, const char **argv) { struct packet_reader reader; - packet_reader_init(&reader, 0, NULL, 0, - PACKET_READ_GENTLE_ON_EOF | - PACKET_READ_CHOMP_NEWLINE); + int options = PACKET_READ_GENTLE_ON_EOF; + int chomp_newline = 1; + int reader_use_sideband = 0; + const char *const unpack_sideband_usage[] = { + "test_tool unpack_sideband [options...]", NULL + }; + struct option cmd_options[] = { + OPT_BOOL(0, "reader-use-sideband", &reader_use_sideband, + "set use_sideband bit for packet reader (Default: off)"), + OPT_BOOL(0, "chomp-newline", &chomp_newline, + "chomp newline in packet (Default: on)"), + OPT_END() + }; + + argc = parse_options(argc, argv, "", cmd_options, unpack_sideband_usage, + 0); + if (argc > 0) + usage_msg_opt(_("too many arguments"), unpack_sideband_usage, + cmd_options); + + if (chomp_newline) + options |= PACKET_READ_CHOMP_NEWLINE; + packet_reader_init(&reader, 0, NULL, 0, options); + reader.use_sideband = reader_use_sideband; while (packet_reader_read(&reader) != PACKET_READ_EOF) { int band; @@ -79,6 +101,17 @@ static void unpack_sideband(void) case PACKET_READ_EOF: break; case PACKET_READ_NORMAL: + /* + * When the "use_sideband" field of the reader is turned + * on, sideband packets other than the payload have been + * parsed and consumed in packet_reader_read(), and only + * the payload arrives here. + */ + if (reader.use_sideband) { + write_or_die(1, reader.line, reader.pktlen - 1); + break; + } + band = reader.line[0] & 0xff; if (band < 1 || band > 2) continue; /* skip non-sideband packets */ @@ -97,15 +130,31 @@ static void unpack_sideband(void) static int send_split_sideband(void) { + const char *foo = "Foo.\n"; + const char *bar = "Bar.\n"; const char *part1 = "Hello,"; const char *primary = "\001primary: regular output\n"; const char *part2 = " world!\n"; + /* Each sideband message has a trailing newline character. */ + send_sideband(1, 2, foo, strlen(foo), LARGE_PACKET_MAX); + send_sideband(1, 2, bar, strlen(bar), LARGE_PACKET_MAX); + + /* + * One sideband message is divided into part1 and part2 + * by the primary message. + */ send_sideband(1, 2, part1, strlen(part1), LARGE_PACKET_MAX); packet_write(1, primary, strlen(primary)); send_sideband(1, 2, part2, strlen(part2), LARGE_PACKET_MAX); packet_response_end(1); + /* + * We use unpack_sideband() to consume packets. A flush packet + * is required to end parsing. + */ + packet_flush(1); + return 0; } @@ -126,7 +175,7 @@ int cmd__pkt_line(int argc, const char **argv) else if (!strcmp(argv[1], "unpack")) unpack(); else if (!strcmp(argv[1], "unpack-sideband")) - unpack_sideband(); + unpack_sideband(argc - 1, argv + 1); else if (!strcmp(argv[1], "send-split-sideband")) send_split_sideband(); else if (!strcmp(argv[1], "receive-sideband")) diff --git a/t/t0070-fundamental.sh b/t/t0070-fundamental.sh index 574de34198..297a7f772e 100755 --- a/t/t0070-fundamental.sh +++ b/t/t0070-fundamental.sh @@ -53,4 +53,62 @@ test_expect_success 'missing sideband designator is reported' ' test_i18ngrep "missing sideband" err ' +test_expect_success 'unpack-sideband: --no-chomp-newline' ' + test_when_finished "rm -f expect-out expect-err" && + test-tool pkt-line send-split-sideband >split-sideband && + test-tool pkt-line unpack-sideband \ + --no-chomp-newline out 2>err && + cat >expect-out <<-EOF && + primary: regular output + EOF + cat >expect-err <<-EOF && + Foo. + Bar. + Hello, world! + EOF + test_cmp expect-out out && + test_cmp expect-err err +' + +test_expect_success 'unpack-sideband: --chomp-newline (default)' ' + test_when_finished "rm -f expect-out expect-err" && + test-tool pkt-line send-split-sideband >split-sideband && + test-tool pkt-line unpack-sideband \ + --chomp-newline out 2>err && + printf "primary: regular output" >expect-out && + printf "Foo.Bar.Hello, world!" >expect-err && + test_cmp expect-out out && + test_cmp expect-err err +' + +test_expect_failure '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 \ + --reader-use-sideband \ + --no-chomp-newline out 2>err && + cat >expect-out <<-EOF && + primary: regular output + EOF + printf "remote: Foo. \n" >expect-err && + printf "remote: Bar. \n" >>expect-err && + printf "remote: Hello, world! \n" >>expect-err && + test_cmp expect-out out && + test_cmp expect-err err +' + +test_expect_failure 'unpack-sideband: packet_reader_read() consumes sideband, 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 \ + --reader-use-sideband \ + --chomp-newline out 2>err && + printf "primary: regular output" >expect-out && + printf "remote: Foo. \n" >expect-err && + printf "remote: Bar. \n" >>expect-err && + printf "remote: Hello, world! \n" >>expect-err && + test_cmp expect-out out && + test_cmp expect-err err +' + test_done From patchwork Sun Dec 17 14:41:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Xin X-Patchwork-Id: 13495891 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6213A44C65 for ; Sun, 17 Dec 2023 14:41:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DTAhYjQ+" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6ceb2501f1bso1969542b3a.0 for ; Sun, 17 Dec 2023 06:41:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702824102; x=1703428902; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WNEk2OqAFB0M16I9BuVYseFIfPuU3rmPfSd/KC8MluU=; b=DTAhYjQ+8rARrlhmEWh0HoGhnpbpd1d5GvF+xcosuajWgdiGTeB9g4sRqsmQojI9HL vaCXj0DktRGFjMgfHlJzGDWph81CmF1sKFsOOHHdhkYkVX8S0S9CDQZifoKWm9XpqADh 9UD4spVXbCieR4N7SwvtFtcgI6zv57Ml9nqV9u80lcGlJqS+lKEUIaQX1Q9L+kgfqw4E rhkKr/v2CK58B17zCxURj1Hm8rDFQu5NAbtn9i8NaAGDWSoi8hSBK6PbQNVNS/xpngpD E0oROti96ybB4kIzPiK+tiO9SDKQTj3noU7MM9o0VZDBxRVjEpQsXQBT0W6QeQbBr1pW Rapw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702824102; x=1703428902; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WNEk2OqAFB0M16I9BuVYseFIfPuU3rmPfSd/KC8MluU=; b=OMfx8otRNFF3wTwQP+wA8ZoNsh2CRH5s1RLgAhkPuJkZpoy2fYEv2G+Jn9eiKKfe3i 4h1FBuUaoGFKRhzISpsHyEl8YDFkJs7zaEfF54gZnUJPbiAlrDrdKZCRVZ+/1kFvefXo j/UH7cU5QcrtE7/jveQrT3e6nxmuVzChOnn4brXIItfTXZDWaC+SrQpAKcBhg6Rm9l60 /TG3VkGHPJ2RSy869DrB9dYrCXJMGbM3c/8Hmy4YoGFy2et3WlQYeM2W7FBZ1mVaD37q XlqNkOKIv2JEKwB5cpyjOI48sTqAukjvwCFGikN4nStMa/8gzlHVDF7Y84PP936yVVh9 4gaw== X-Gm-Message-State: AOJu0YxjRQmkhzZY0WK0fKgzlnSu4zs8y+tqSumYsvSIdgbXxsvXDY4A AZx6uzvp2T8MOnfXLvgVduLc1U8judU= X-Google-Smtp-Source: AGHT+IHdUrNYUckSKPvWvVCIB21CMLroZtU5ymQ4/yCdqfmXfq9/+f8mpPC+rrtUEEI9srqtqXMSvA== X-Received: by 2002:a05:6a00:aca:b0:6ce:55d1:8b70 with SMTP id c10-20020a056a000aca00b006ce55d18b70mr17432695pfl.44.1702824102382; Sun, 17 Dec 2023 06:41:42 -0800 (PST) Received: from tigtog-proxy.localdomain.localdomain (144.34.163.219.16clouds.com. [144.34.163.219]) by smtp.gmail.com with ESMTPSA id i4-20020a63cd04000000b005c1ce3c960bsm16276657pgg.50.2023.12.17.06.41.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Dec 2023 06:41:42 -0800 (PST) From: Jiang Xin To: Git List , Junio C Hamano , Jonathan Tan , Oswald Buddenhagen Cc: Jiang Xin Subject: [PATCH v4 2/3] pkt-line: memorize sideband fragment in reader Date: Sun, 17 Dec 2023 22:41:37 +0800 Message-Id: <5942b74cab4ffe8509169a0a4b8442019fd05e01.1702823801.git.zhiyou.jx@alibaba-inc.com> X-Mailer: git-send-email 2.32.0.rc3 In-Reply-To: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Jiang Xin 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 --- pkt-line.c | 5 ++--- pkt-line.h | 3 +++ t/t0070-fundamental.sh | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pkt-line.c b/pkt-line.c index af83a19f4d..5943777a17 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -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; } diff --git a/pkt-line.h b/pkt-line.h index 954eec8719..be1010d34e 100644 --- a/pkt-line.h +++ b/pkt-line.h @@ -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; }; /* diff --git a/t/t0070-fundamental.sh b/t/t0070-fundamental.sh index 297a7f772e..275edbf6e7 100755 --- a/t/t0070-fundamental.sh +++ b/t/t0070-fundamental.sh @@ -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 \ From patchwork Sun Dec 17 14:41:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Xin X-Patchwork-Id: 13495892 Received: from mail-oa1-f46.google.com (mail-oa1-f46.google.com [209.85.160.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D89144C81 for ; Sun, 17 Dec 2023 14:41:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="T2hYPccN" Received: by mail-oa1-f46.google.com with SMTP id 586e51a60fabf-1f055438492so1749330fac.3 for ; Sun, 17 Dec 2023 06:41:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702824103; x=1703428903; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4opvOjBcm3b9ZlpvxPjhXXCr3HxAO3fjdsZCeIwaGV0=; b=T2hYPccNiE4ViBoE/d4Jk00edpN64VwyC3GSBjZNq6SwUQBFW1mDdO5KkjiGhSMVOc QKdTdvSM50yMJwo+Ssd/9xy5lRrYEluLqNCNbaSlxY0WxaC5n318mBfE3+smun2sSQn8 8VKBOyRCBmmH6QEbZ5euK99WMYNNY2NjcfMr8XcPXujnAaWUgr5vgJqY8HyQbirv3AcG 79XHmn+5s9IlPU5zqFGyQlo9Vuf2MO2RK1/sKo5Y+pLhfaGedCttPT0MQlVLzH/F7Rbq 2FIDCQBiB73k4p5PMkTG/zZUkncYJckREOIw2F9hThZ5qi2O7PO+ps+KhFDnF0hswAqh vGjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702824103; x=1703428903; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4opvOjBcm3b9ZlpvxPjhXXCr3HxAO3fjdsZCeIwaGV0=; b=KjvWtCRlnbkQt/ZoIzKrwzunHvZQyKKABUA/xVf52pIMsXo4GyxI8koCcFAsQSDK5l Grklfjj2XcKgqJqkmWIMLxBCKf+ehXqfLSLx92/e0efxZV/sNLy3N+QRCxi+hodijjJH fbi3oLQ99HUIHkPbIhDNAfleIGnfz3jgYxQifvqNADKRajHQWquhxu92DmsOK0LNCXRb UvtqXRLKFIkehyWBZA0cRxU4wdq4iThzXZmdDC+aNZS87S0LARcxkWjoQqhdzz3YLOUU M2kIj8WknPbfyznpnEPmc8IpjJMmZR10Ai8JVS9zQtt0dg0/YN238dDeHSOhjC3XJ1ZL k9pg== X-Gm-Message-State: AOJu0YxHz1QENcGiTtiwHjzwWIOjl+9hM3D705cBdwZ3AOIIPW78stlq BWLfK/ByZ4MHoc39EWTMRtkFaP48ZR0= X-Google-Smtp-Source: AGHT+IHAR5Zdq+V1tebbtFiJB/CWioJPXyDIYVcyduqRf7P6+sLTvzu4qN/wLjzD1jx1+y1pXHWCXw== X-Received: by 2002:a05:6870:c785:b0:203:9f4b:9529 with SMTP id dy5-20020a056870c78500b002039f4b9529mr1805051oab.36.1702824103409; Sun, 17 Dec 2023 06:41:43 -0800 (PST) Received: from tigtog-proxy.localdomain.localdomain (144.34.163.219.16clouds.com. [144.34.163.219]) by smtp.gmail.com with ESMTPSA id i4-20020a63cd04000000b005c1ce3c960bsm16276657pgg.50.2023.12.17.06.41.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Dec 2023 06:41:42 -0800 (PST) From: Jiang Xin To: Git List , Junio C Hamano , Jonathan Tan , Oswald Buddenhagen Cc: Jiang Xin Subject: [PATCH v4 3/3] pkt-line: do not chomp newlines for sideband messages Date: Sun, 17 Dec 2023 22:41:38 +0800 Message-Id: X-Mailer: git-send-email 2.32.0.rc3 In-Reply-To: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Jiang Xin When calling "packet_read_with_status()" to parse pkt-line encoded packets, we can turn on the flag "PACKET_READ_CHOMP_NEWLINE" to chomp newline character for each packet for better line matching. But when receiving data and progress information using sideband, we should turn off the flag "PACKET_READ_CHOMP_NEWLINE" to prevent mangling newline characters from data and progress information. When both the server and the client support "sideband-all" capability, we have a dilemma that newline characters in negotiation packets should be removed, but the newline characters in the progress information should be left intact. Add new flag "PACKET_READ_USE_SIDEBAND" for "packet_read_with_status()" to prevent mangling newline characters in sideband messages. Helped-by: Jonathan Tan Helped-by: Oswald Buddenhagen Signed-off-by: Jiang Xin --- pkt-line.c | 31 +++++++++++++++++++++++++++++-- pkt-line.h | 1 + t/t0070-fundamental.sh | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pkt-line.c b/pkt-line.c index 5943777a17..e9061e61a4 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -462,8 +462,32 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer, } if ((options & PACKET_READ_CHOMP_NEWLINE) && - len && buffer[len-1] == '\n') - len--; + len && buffer[len-1] == '\n') { + if (options & PACKET_READ_USE_SIDEBAND) { + int band = *buffer & 0xff; + switch (band) { + case 1: + /* Chomp newline for payload */ + len--; + break; + case 2: + case 3: + /* + * Do not chomp newline for progress and error + * message. + */ + break; + default: + /* + * Bad sideband, let's leave it to + * demultiplex_sideband() to catch this error. + */ + break; + } + } else { + len--; + } + } buffer[len] = 0; if (options & PACKET_READ_REDACT_URI_PATH && @@ -602,6 +626,9 @@ enum packet_read_status packet_reader_read(struct packet_reader *reader) return reader->status; } + if (reader->use_sideband) + reader->options |= PACKET_READ_USE_SIDEBAND; + /* * Consume all progress packets until a primary payload packet is * received diff --git a/pkt-line.h b/pkt-line.h index be1010d34e..a7ff2e2f18 100644 --- a/pkt-line.h +++ b/pkt-line.h @@ -85,6 +85,7 @@ void packet_fflush(FILE *f); #define PACKET_READ_DIE_ON_ERR_PACKET (1u<<2) #define PACKET_READ_GENTLE_ON_READ_ERROR (1u<<3) #define PACKET_READ_REDACT_URI_PATH (1u<<4) +#define PACKET_READ_USE_SIDEBAND (1u<<5) int packet_read(int fd, char *buffer, unsigned size, int options); /* diff --git a/t/t0070-fundamental.sh b/t/t0070-fundamental.sh index 275edbf6e7..0d2b7d8d93 100755 --- a/t/t0070-fundamental.sh +++ b/t/t0070-fundamental.sh @@ -97,7 +97,7 @@ test_expect_success 'unpack-sideband: packet_reader_read() consumes sideband, no test_cmp expect-err err ' -test_expect_failure 'unpack-sideband: packet_reader_read() consumes sideband, chomp payload' ' +test_expect_success 'unpack-sideband: packet_reader_read() consumes sideband, 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 \