From patchwork Wed Oct 4 13:18:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Xin X-Patchwork-Id: 13408787 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50F2CE7B611 for ; Wed, 4 Oct 2023 13:18:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242630AbjJDNSb (ORCPT ); Wed, 4 Oct 2023 09:18:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233370AbjJDNSX (ORCPT ); Wed, 4 Oct 2023 09:18:23 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A005A6 for ; Wed, 4 Oct 2023 06:18:19 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-2774874c3daso1578174a91.1 for ; Wed, 04 Oct 2023 06:18:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696425498; x=1697030298; 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=Z8eU6KlYNOUbo4tew0LaMGYacf7pAFMZMHgVRtMA2Uc=; b=DIOTth3OX3SYO5sPTlqKhUg9qemDme5SfgFrJIvBDcbW51FXo8sIYiAkNwkjVsX1D1 mC/Iz0vZylFTHnlQoZ+KjcRUyoF5EhHLS7G5CaC1TFNyJkZfAjcxkHVZMWQoPxcODzu4 5VxWgLgvgtSqUbwkJAHlH50yP80Qg6QGuvtgVHv2qi+ElqzBM/aynvvuPWNEm21r+MWG kqp7xm0/QJ7tJQWr+GAQ0n2HMIMd2DOIeqQAnMJhPqzTGRo/8TmVW/enxsvuzrE8xW47 yPdPpItebPh0PnS2MqvCqeMCWCv0vvB9y63Et75r+s/EC6MnFyAYcporjmsreCxH6Ypc Cwkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696425498; x=1697030298; 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=Z8eU6KlYNOUbo4tew0LaMGYacf7pAFMZMHgVRtMA2Uc=; b=wYJ8xcDkCj1t8To1EJztCpSEt4su2eMIEjEI2C48kUXdkCcTHdQumVP5L1TiOD2oyW Zjoqq3Xl/1cAO5yC9DhMVpZV8QS4TuUwSNIdnDLsLRprkNnpZr3YMEdFmaY7F4OhY3Jb FkDPXvn3zYcPfvZ+KyGG8Vj6FB9I1Z0Ad9vjmZRe8fvPi0R/veRXpIoJJBll9LOBcT6k +69BTrasSEYPY0IrvgvmAIWETiMiL5kbtsU2Km7wL5nvRONevwfc7mhsl+EXiuZfoBZX Vk0vvC2RqvcoJ9gOb+M/zKGiXraFJk8eAUCYAGNQlW5DSiaLPC6SnuThDRaI0p2e9hhK rnQw== X-Gm-Message-State: AOJu0YyOE8VE8a9ezHFbn9fBiGkJT3GhAKot/ltc9TqNxpw714/mRV3+ kmZ4UfdAwCkdaM5UL+LhEGtIQAp7g+s= X-Google-Smtp-Source: AGHT+IHfUIejH6WSuymMJFncHwXPwyhDeeG9ELG9qhRs0CobuoTo2bk9Xh2kXsCPpHrmNxebMDvbCA== X-Received: by 2002:a17:90b:11cc:b0:277:6d6a:33ba with SMTP id gv12-20020a17090b11cc00b002776d6a33bamr2267035pjb.28.1696425498339; Wed, 04 Oct 2023 06:18:18 -0700 (PDT) Received: from tigtog-proxy.localdomain.localdomain (144.34.163.219.16clouds.com. [144.34.163.219]) by smtp.gmail.com with ESMTPSA id y14-20020a17090a134e00b00260a5ecd273sm1465496pjf.1.2023.10.04.06.18.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Oct 2023 06:18:17 -0700 (PDT) From: Jiang Xin To: Git List , Junio C Hamano , Jonathan Tan , Oswald Buddenhagen Cc: Jiang Xin Subject: [PATCH v3 1/3] test-pkt-line: add option parser for unpack-sideband Date: Wed, 4 Oct 2023 21:18:12 +0800 Message-Id: <17d88ecab6228fbf56f934bc09c921268d202874.1696425168.git.zhiyou.jx@alibaba-inc.com> X-Mailer: git-send-email 2.32.0.rc3 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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. * We can 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 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 enabled the function "demultiplex_sideband()" 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 also trimmed. Will fix the above two issues in subsequent commits. Signed-off-by: Jiang Xin --- t/helper/test-pkt-line.c | 58 ++++++++++++++++++++++++++++++++++++---- t/t0070-fundamental.sh | 58 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/t/helper/test-pkt-line.c b/t/helper/test-pkt-line.c index f4d134a145..9aa35f7861 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,16 @@ 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. + */ + 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 +129,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); + /* + * The unpack_sideband() function above requires a flush + * packet to end parsing. + */ + packet_flush(1); + return 0; } @@ -126,7 +174,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..1053913d2d 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 with demultiplex_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 \ + --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 with demultiplex_sideband(), 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 \ + --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 Wed Oct 4 13:18:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Xin X-Patchwork-Id: 13408786 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE405E7B615 for ; Wed, 4 Oct 2023 13:18:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242609AbjJDNS3 (ORCPT ); Wed, 4 Oct 2023 09:18:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233134AbjJDNSX (ORCPT ); Wed, 4 Oct 2023 09:18:23 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44E569E for ; Wed, 4 Oct 2023 06:18:20 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1c61acd1285so15262345ad.2 for ; Wed, 04 Oct 2023 06:18:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696425499; x=1697030299; 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=IFTKN8E2VbQJgmeQ1ifbFtW24IbNvHZHQMBXGEY0E/I=; b=KwWqK/ejqxcYgDj7HM+atYw1KXqGVH1oZJZXSMq+VNF7bWkf+1ye8PNMVSYWBj5M/l BHfvLdVlMStmRCCOA9R0ouDh3MJZDYQkcnGIemVx250/XXnnTNZKYuDwEsC36juvWhHD kD73XiTHU/qTFz+BIee4UJJCjIcGOPfPYfaK9TRGVJ+bg+yIJYZd3G5CdosEakrrCLSG 9bA6ezfHHcHxJy0vPCKMP8XP4GZCI7qJd4oaixFm3NayoUpgjj8uteg0z4vgYndtniT1 eJFOIJIPGf6Wwv51y8+SQCMiYi0lCi34s174nEy2GvfTMWngCe/mc8If5fLFJa425/X+ 4ruA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696425499; x=1697030299; 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=IFTKN8E2VbQJgmeQ1ifbFtW24IbNvHZHQMBXGEY0E/I=; b=HWVCtH9jSXDZUjmN51NNzIknbriSAUJFpULqR40+xPA/QS4z9dqcLJqnqRA7Pw2ohy +93QnXFQ+CF9u2jQj4HdF1cgE42BGLjpyB1EysFKoa/kBu3BCOx8EL+wK2+MEkcMZXiI 2qgsZYdHneurcYEGbHwWg8ITMCI2RPXiftiPS0pN3PYu99xZIaMfJYpzueJjabKBTvSl adZUNe1lkDoAcWFjb/n/ZMgP98UZ/UiQeXX8tw8ltjmIdxKqGsNLGfsbdjOGt3R/BDT2 0NDXwVgRBHAQGIJMgIRtj5XFFUAonLDhmDHBqEuK/4Oes49ab6D3M7D+Dzla82RG2SGe nOTg== X-Gm-Message-State: AOJu0YynGIAInrRStovMICg0tcB6tiNc5MP7flOB06V/xF2w5QzHNeZw WxyErFWfOgTZQf8c5aBexaBsZhb/nto= X-Google-Smtp-Source: AGHT+IGNELiN6kDL6ysXKlrg193nPDHb1NumHwijKJSaui6mrYxdzUU4ZKjdQIE3yGSwcC6P7Z76/A== X-Received: by 2002:a17:90a:3de6:b0:26d:17da:5e9f with SMTP id i93-20020a17090a3de600b0026d17da5e9fmr2073189pjc.1.1696425499460; Wed, 04 Oct 2023 06:18:19 -0700 (PDT) Received: from tigtog-proxy.localdomain.localdomain (144.34.163.219.16clouds.com. [144.34.163.219]) by smtp.gmail.com with ESMTPSA id y14-20020a17090a134e00b00260a5ecd273sm1465496pjf.1.2023.10.04.06.18.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Oct 2023 06:18:18 -0700 (PDT) From: Jiang Xin To: Git List , Junio C Hamano , Jonathan Tan , Oswald Buddenhagen Cc: Jiang Xin Subject: [PATCH v3 2/3] pkt-line: memorize sideband fragment in reader Date: Wed, 4 Oct 2023 21:18:13 +0800 Message-Id: X-Mailer: git-send-email 2.32.0.rc3 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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 1053913d2d..a927c665d6 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 with demultiplex_sideband(), no chomp newline' ' +test_expect_success 'unpack-sideband with demultiplex_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 \ From patchwork Wed Oct 4 13:18:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Xin X-Patchwork-Id: 13408788 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8F13E7B60F for ; Wed, 4 Oct 2023 13:18:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242620AbjJDNSc (ORCPT ); Wed, 4 Oct 2023 09:18:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242551AbjJDNSY (ORCPT ); Wed, 4 Oct 2023 09:18:24 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E307A1 for ; Wed, 4 Oct 2023 06:18:21 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-278fde50024so730534a91.1 for ; Wed, 04 Oct 2023 06:18:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696425500; x=1697030300; 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=OJdymBj+423fFLsFiCBX4i6fvmXBJ4gIUzQTjizvMag=; b=QNJlzKKGoc4mlcReviNHWkfQL0hsrdhCwI+sGS8hqjNoICTNYJb9eaftq45ZsSNWwu joNqR+SRo31JFdEl8HTxzo2ZktsS+UM+xMylqGNrwhWuaombWgpbbNKbbrNsHmW+06jN NuW9RIvIHYfGOo/lEZE5es/S128ld1X5XOK48ApF+uzsgY586Ayry/BVs29DLo66s546 zdI2FCUM7yYdb++Hyk+RjJDKaCKIcW7KteW4Q9jkNGKYtrWCCQDvB6OiGDsdVsQmOjMq I3qHNNycoRyvKeRZEC+Y7O3ioRwCp5pLwo46HGTg/id7eoReph7ZlsogVlOnAMYbGSWe FkhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696425500; x=1697030300; 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=OJdymBj+423fFLsFiCBX4i6fvmXBJ4gIUzQTjizvMag=; b=EbU/2xIOkL1JgGnGTo0xZXG0FtMgk+pAyetYJTgyb6b8/wqJoH1kLteiH3Uv+OKrXa 4pKlFclMs1fZDgZ1B2qsl1Q7FyiYRY43Q2VrBAMzbWY9qdoLHfDcu3e2wl3oBnrlThfT KtAi72dhaIO80Z0TccX69wQ6zNatYR0/y06YpELCiDAqaclHBnj9Qd6kQdFr1lekrluh L0mjZLLJ2RdL++9iVwjguSBryqG2aZY52H/p5oyqPFMaYNsyWsyN2pOcGLpuMuMuHdrU Thwo5LmCPnA+kEqw12U45FYq2xNLJ+BTq/lJESX5B2pHfRHLjRVALiwvJj3U7CCXk8tI aFxw== X-Gm-Message-State: AOJu0YyDapTnK/J7QhEOJz9yCjvz8tHzh0eGxcjz/i09Yu0rKv/uCUXO hXVCkdgCAGGx2v6lhDTf+6qQH7yAbNg= X-Google-Smtp-Source: AGHT+IHb2ZhmxEyyvL4pMz4bmZwW/KipTB2ZsQdEob2vh3ilHZDDEk7gpK8CPEDsehKHYj03NINjeA== X-Received: by 2002:a17:90b:18b:b0:276:6be8:8bfe with SMTP id t11-20020a17090b018b00b002766be88bfemr3373871pjs.23.1696425500413; Wed, 04 Oct 2023 06:18:20 -0700 (PDT) Received: from tigtog-proxy.localdomain.localdomain (144.34.163.219.16clouds.com. [144.34.163.219]) by smtp.gmail.com with ESMTPSA id y14-20020a17090a134e00b00260a5ecd273sm1465496pjf.1.2023.10.04.06.18.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Oct 2023 06:18:19 -0700 (PDT) From: Jiang Xin To: Git List , Junio C Hamano , Jonathan Tan , Oswald Buddenhagen Cc: Jiang Xin Subject: [PATCH v3 3/3] pkt-line: do not chomp newlines for sideband messages Date: Wed, 4 Oct 2023 21:18:14 +0800 Message-Id: <68ac3ea711ce2705a328e6b59c95c0ca884a7e6c.1696425168.git.zhiyou.jx@alibaba-inc.com> X-Mailer: git-send-email 2.32.0.rc3 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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 a927c665d6..138c2becc1 100755 --- a/t/t0070-fundamental.sh +++ b/t/t0070-fundamental.sh @@ -97,7 +97,7 @@ test_expect_success 'unpack-sideband with demultiplex_sideband(), no chomp newli test_cmp expect-err err ' -test_expect_failure 'unpack-sideband with demultiplex_sideband(), chomp newline' ' +test_expect_success 'unpack-sideband with demultiplex_sideband(), 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 \