From patchwork Tue Aug 23 18:04:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Schindelin X-Patchwork-Id: 12952528 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 3E4C3C32796 for ; Tue, 23 Aug 2022 19:23:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230050AbiHWTX0 (ORCPT ); Tue, 23 Aug 2022 15:23:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232434AbiHWTXF (ORCPT ); Tue, 23 Aug 2022 15:23:05 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A573134CAF for ; Tue, 23 Aug 2022 11:04:35 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id e20so17440105wri.13 for ; Tue, 23 Aug 2022 11:04:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc; bh=I3Ojpviz0Kfb3I0CUQKssWArPb+HJXPRsrZsL/QUGG0=; b=D/oHAg8CZE3L/jeL/SokAMnCAGkKJ1eRidzlqX1rK77SWn16AhZm2ffzwm82qcsAaC UONXklTT+yxuOewGROhGG6dJjqENbZPmaq0rJo4SmHdtsGkqdPZDWfHQO9g0VMwQUfQq sb/nID4xS1f29uazcXRekrj9hPGSBPs6FBEfkI5Oibgbwlk26Bp0hYnlnNYYy18wWOoX P+NTbmD5ipK+BPGSo8FeT7fe9em0FxaI8CJ+w+0f94wBgamQ8dWZCHBWBkcMssMfFDFI Q6pwPeuLKEalC8P1eotMFKamF28NjpDl5ht5NUPqdvRkDsURL3vrR80kdfVdXCHfNIO0 Tn5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc; bh=I3Ojpviz0Kfb3I0CUQKssWArPb+HJXPRsrZsL/QUGG0=; b=n803axMuGgfh2R0DUQqcNvAdtfkS4kcLHcju7RovFV5CX5ATmcZpkgiX+e+EnXzPxb CZ7j4AciTFd00H7EkeNQVUj3Ju15IGmh4Sh0x8osQ15l7/WmjgHNlJ08Q+4SXwsqy35+ JbO+fSpNytF3a8B3mcWeUFF0jad7gG7KPCAyz/F77RsencD1aEtWS5EtufojpJyjN4uY 4ylMA+McmfB2iIvps2PVROF2qiIEPk4ltavAkZ/ZdSsqJsECCymA+fChjduqdXZa1EvY 8EhkVpYr2XDnoVoAE6AC7tMdOWSZIFsjsvbmKMSzGhDzXJ7EuNvGroUEkYcOszsFvor+ EPGw== X-Gm-Message-State: ACgBeo3PRsOpPsvHhffWU25uBxZis2/qXGO2P+aqGUpV9cCJJw5/aMBU 8LKHUwCXhiE/74Vc6v/1AL5pvgQZF50= X-Google-Smtp-Source: AA6agR5JTwoVGvKH/KYx5b422zez/Kjgu40M8k6Nz1cXqoewmfxSSoO585UGu1UyXWWJPBfTeAjkAQ== X-Received: by 2002:a05:6000:11d1:b0:225:3354:674d with SMTP id i17-20020a05600011d100b002253354674dmr12545309wrx.234.1661277873076; Tue, 23 Aug 2022 11:04:33 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t9-20020a05600c198900b003a4efb794d7sm20011142wmq.36.2022.08.23.11.04.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Aug 2022 11:04:32 -0700 (PDT) Message-Id: <74ab50eeb1c6debd2f371e548cf6c782479f69af.1661277870.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 23 Aug 2022 18:04:28 +0000 Subject: [PATCH 1/3] t3701: redefine what is "bogus" output of a diff filter Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Johannes Schindelin Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Johannes Schindelin From: Johannes Schindelin When parsing the colored version of a diff, the interactive `add` command really relies on the colored version having the same number of lines as the non-colored version. That is an invariant. However, in the 'detect bogus diffFilter output' test case in t3701, we essentially required a hunk header that contains parseable `@@ ... @@` hunk headers, and called all colored diffs without such hunks bogus. The reason for this is that we would like to show the users the adjusted hunk headers _including_ the extra part after the `@@ ... @@` information, which usually contains things like the function name or soms such. Now, there is a _very_ popular diff colorizer called `diff-so-fancy` that does not produce such colored diffs as the built-in `add` command expects. Nevertheless, the Perl variant of the `add` command handles those nicely, essentially by ignoring the hunk header and saying "there is nothing else we can show except the original hunk header, even if we had to adjust the line range and the original hunk header might get that wrong". In preparation for teaching the built-in interactive `add` to be a bit more lenient, let's change the 'detect bogus diffFilter output' test case so that it verifies that a mismatched number of lines causes the command to error out, but not an unparseable hunk header. Signed-off-by: Johannes Schindelin --- t/t3701-add-interactive.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index b354fb39de8..b40d1c94d99 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -761,7 +761,7 @@ test_expect_success 'detect bogus diffFilter output' ' git reset --hard && echo content >test && - test_config interactive.diffFilter "sed 1d" && + test_config interactive.diffFilter "sed q" && printf y >y && force_color test_must_fail git add -p X-Patchwork-Id: 12952529 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 8233BC32789 for ; Tue, 23 Aug 2022 19:23:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232487AbiHWTX3 (ORCPT ); Tue, 23 Aug 2022 15:23:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231363AbiHWTXG (ORCPT ); Tue, 23 Aug 2022 15:23:06 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D86E134CBF for ; Tue, 23 Aug 2022 11:04:36 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id k17so7604840wmr.2 for ; Tue, 23 Aug 2022 11:04:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc; bh=M/2g7wHp52k6Rtik7/3DhHa3bU30ZQdpTtM1UecamjY=; b=BclyCqVpvuH7H3rUS3XOoYpAicNJzCd8Hajq10oOulIeJex2ThnczZj2KUYmUM0rjG R19WY/NSR4Sh4Jr7UsgT/pJ2PSAZvRTpJVqzPVhhbnj/YKZWMQ2ist2vCek0sJ4AeLPi wtbXFCoKqEF3oLSscX1t+v/28rbLfooc5S6394at1CoAPA8E3heIboV97L9NdSdMEj7S KFe1YFxATbb7E8hXBctfzpFTo99dmfkwj7Ey79yQ7aXe2qKLYLIjjl/2cr+/Po2RXSTG mV4nIPhAdJEWoQyYRSkbV65VtzzFvOPWStHa+9uf9eZbz1GxZS1WkIFE590nAQ7uOtGi XpOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc; bh=M/2g7wHp52k6Rtik7/3DhHa3bU30ZQdpTtM1UecamjY=; b=my0ZUideC7PqCk7G65VNnTed3KKcSneWyzKR4XRi5TavS8EP0djqwH2xzoiWKzlG94 b3LsJ3vWEIIztHIqEXN8dI1Sjh9rp6Aq2/3OR+7loVvzW5IHUCc+dJEF16IGzSm5Pk3T yNMlwXp+Riyiu/d49iYrw2aNWPQzc3Fpslgb1RIi2W5tdNGVfKIPX2nHh1JtVb22mtv0 y0Hb7kBQvGNTCpD+PflQRJYEcVRxkfw6yRkjDwAwNJSlse3WoQ5tvfWu0wt8M4RqMnQ4 gUwSGheSuyFq/1ibKmnRiqqIpigPxWBHIu98Jb9eh37UTeU9iMfwsOlPeWrkcVJgoKiW ZFsA== X-Gm-Message-State: ACgBeo2JUIvUj28+gxt4ExebeX4BDSniNbgbvuHWCBO+E1bqgDSkwub8 sqCfdegSRnYp2TKv7mkAkTW/g9z9x38= X-Google-Smtp-Source: AA6agR6UFhnABT8DI5d8SFyR1qKLVJKFmx/S+7aHYvkVDGkfzIxWSnrNwBXPWxWzTzHwg+muyCFA3w== X-Received: by 2002:a05:600c:3846:b0:3a6:5292:f8af with SMTP id s6-20020a05600c384600b003a65292f8afmr2944949wmr.50.1661277874062; Tue, 23 Aug 2022 11:04:34 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l23-20020a05600c1d1700b003a61306d79dsm22786647wms.41.2022.08.23.11.04.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Aug 2022 11:04:33 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Tue, 23 Aug 2022 18:04:29 +0000 Subject: [PATCH 2/3] add -p: gracefully ignore unparseable hunk headers in colored diffs Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Johannes Schindelin Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Johannes Schindelin From: Johannes Schindelin In https://lore.kernel.org/git/ecf6f5be-22ca-299f-a8f1-bda38e5ca246@gmail.com, Phillipe Blain reported that the built-in `git add -p` command fails when asked to use [`diff-so-fancy`][diff-so-fancy] to colorize the diff. The reason is that this tool produces colored diffs with a hunk header that does not contain any parseable `@@ ... @@` line range information, and therefore we cannot detect any part in that header that comes after the line range. Let's punt for now and simply show nothing apart from the line range in that case. [diff-so-fancy]: https://github.com/so-fancy/diff-so-fancy Signed-off-by: Johannes Schindelin --- add-patch.c | 15 ++++++--------- t/t3701-add-interactive.sh | 9 +++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/add-patch.c b/add-patch.c index 509ca04456b..f2fffe1af02 100644 --- a/add-patch.c +++ b/add-patch.c @@ -357,16 +357,13 @@ static int parse_hunk_header(struct add_p_state *s, struct hunk *hunk) eol = memchr(line, '\n', s->colored.len - hunk->colored_start); if (!eol) eol = s->colored.buf + s->colored.len; - p = memmem(line, eol - line, "@@ -", 4); - if (!p) - return error(_("could not parse colored hunk header '%.*s'"), - (int)(eol - line), line); - p = memmem(p + 4, eol - p - 4, " @@", 3); - if (!p) - return error(_("could not parse colored hunk header '%.*s'"), - (int)(eol - line), line); hunk->colored_start = eol - s->colored.buf + (*eol == '\n'); - header->colored_extra_start = p + 3 - s->colored.buf; + p = memmem(line, eol - line, "@@ -", 4); + if (p && (p = memmem(p + 4, eol - p - 4, " @@", 3))) + header->colored_extra_start = p + 3 - s->colored.buf; + else + /* could not parse colored hunk header, showing nothing */ + header->colored_extra_start = hunk->colored_start; header->colored_extra_end = hunk->colored_start; return 0; diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index b40d1c94d99..7e3c1de71f5 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -766,6 +766,15 @@ test_expect_success 'detect bogus diffFilter output' ' force_color test_must_fail git add -p test && + test_config interactive.diffFilter "sed s/@@/XX/g" && + printf y >y && + force_color git add -p X-Patchwork-Id: 12952526 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 C7F33C32789 for ; Tue, 23 Aug 2022 19:23:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231638AbiHWTXX (ORCPT ); Tue, 23 Aug 2022 15:23:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232378AbiHWTXE (ORCPT ); Tue, 23 Aug 2022 15:23:04 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 607DA60502 for ; Tue, 23 Aug 2022 11:04:37 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id bq11so11366715wrb.12 for ; Tue, 23 Aug 2022 11:04:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc; bh=ikFQWEfRdIynsdVGW6fUd+BYEfs3PqfKzNFN9wLsLzY=; b=QxJMtMKmpSHuJsvzV0m5B9MGOenxjHLIhlYnsV/0oThGJJnsA/1hmBq3oHXrsrklcE /dkCEfayo3ZwZy0WnX0XugLmft39UCV9Ypr+PMQKW92fHjMbro8WYAtrBtJiBGTbkM/z Xqeu29LW7loHTvrfyWScXiA10+XISl705VGP6GWXThLuK1gI/d5jysU3X0lLMhghWIX8 j5663I5mSn/VU/4iVdDQIZ7VL0ff2gnQMxybsmcf9tHUUGbBjNG/GHkm81XHdkJsyqVe hrJoef4tHX74u1fTdKUygk+g7UeNT43e6ViBXhORZS7aS5nB2NV4ohMW72vAtVZXoHAw fkBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc; bh=ikFQWEfRdIynsdVGW6fUd+BYEfs3PqfKzNFN9wLsLzY=; b=1R2GWOYXqxe89s/mtq3jmlj/Q28SvSgdq2MaN5VLmyJ9B5PPyH/baEi3CLc450Tg4H xVF1vQrI10xKVakseN/K0ANbkNl4w0TTuXWHAQFCXI3Wd2OLc17ymp73IuHGohsHm9vV 14Sg4qLxwi5E23vHvN0zvQamNKS0CcsmL/0XzInteStbK2zOCFWMcg0FMHlPadVTmuBG DMALxoOOC16njEULqiTIBJzniKJcnbcR29ROqTLN2RXSXHwSm7Ohhw382A3CuaZPD3or YPNjQksR4VS4akb2J+NOprOqz6WtmvrDjTn+NU6wplmWzY6wSfD5hfe1Y8EGRVDu/gg7 t6zw== X-Gm-Message-State: ACgBeo1jo39+W/USxqLQAo1yFgNQH9G2AjKmAEAyQB1C1cuFrdJo4O6f UbHJpQS5lzmYawOdBDjal9ClVa++YBk= X-Google-Smtp-Source: AA6agR61XRP55jmW+f7Xmqwj+F7R51Fv8ZvQXjpXuiq2s7lejERVi7BnjoS3Sv79ZBAQW7Kke+spAg== X-Received: by 2002:a5d:6f0f:0:b0:225:5589:576f with SMTP id ay15-20020a5d6f0f000000b002255589576fmr6727597wrb.607.1661277875240; Tue, 23 Aug 2022 11:04:35 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j14-20020a05600c410e00b003a50924f1c0sm18361828wmi.18.2022.08.23.11.04.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Aug 2022 11:04:34 -0700 (PDT) Message-Id: <9dac9f74d2e19899b3e6c1d28e83878ded4469d6.1661277870.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 23 Aug 2022 18:04:30 +0000 Subject: [PATCH 3/3] add -p: handle `diff-so-fancy`'s hunk headers better Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philippe Blain , Johannes Schindelin , Johannes Schindelin Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Johannes Schindelin From: Johannes Schindelin The `diff-so-fancy` diff colorizer produces hunk headers that look nothing like the built-in `add -p` expects: there is no `@@ ... @@` line range, and therefore the parser cannot determine where any extra information starts, such as the function name that is often added to those hunk header lines. However, we can do better than simply swallowing the unparseable hunk header. In the `diff-so-fancy` case, it shows something like `@ file:1 @`. Let's just show the complete hunk header because it probably offers useful information. Signed-off-by: Johannes Schindelin --- add-patch.c | 8 +++++++- t/t3701-add-interactive.sh | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/add-patch.c b/add-patch.c index f2fffe1af02..1f3f3611ee9 100644 --- a/add-patch.c +++ b/add-patch.c @@ -8,6 +8,7 @@ #include "diff.h" #include "compat/terminal.h" #include "prompt.h" +#include "utf8.h" enum prompt_mode_type { PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_ADDITION, PROMPT_HUNK, @@ -363,7 +364,7 @@ static int parse_hunk_header(struct add_p_state *s, struct hunk *hunk) header->colored_extra_start = p + 3 - s->colored.buf; else /* could not parse colored hunk header, showing nothing */ - header->colored_extra_start = hunk->colored_start; + header->colored_extra_start = line - s->colored.buf; header->colored_extra_end = hunk->colored_start; return 0; @@ -649,6 +650,7 @@ static void render_hunk(struct add_p_state *s, struct hunk *hunk, size_t len; unsigned long old_offset = header->old_offset; unsigned long new_offset = header->new_offset; + int needs_extra_space = 0; if (!colored) { p = s->plain.buf + header->extra_start; @@ -658,6 +660,8 @@ static void render_hunk(struct add_p_state *s, struct hunk *hunk, p = s->colored.buf + header->colored_extra_start; len = header->colored_extra_end - header->colored_extra_start; + if (utf8_strnwidth(p, len, 1 /* skip ANSI */) > 0) + needs_extra_space = 1; } if (s->mode->is_reverse) @@ -673,6 +677,8 @@ static void render_hunk(struct add_p_state *s, struct hunk *hunk, strbuf_addf(out, ",%lu", header->new_count); strbuf_addstr(out, " @@"); + if (needs_extra_space) + strbuf_addch(out, ' '); if (len) strbuf_add(out, p, len); else if (colored) diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 7e3c1de71f5..9deb7a87f1e 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -772,7 +772,8 @@ test_expect_success 'gracefully fail to parse colored hunk header' ' echo content >test && test_config interactive.diffFilter "sed s/@@/XX/g" && printf y >y && - force_color git add -p output 2>&1