From patchwork Mon Aug 29 15:11:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Schindelin X-Patchwork-Id: 12958015 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 F345BECAAD2 for ; Mon, 29 Aug 2022 15:12:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229958AbiH2PMG (ORCPT ); Mon, 29 Aug 2022 11:12:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230098AbiH2PME (ORCPT ); Mon, 29 Aug 2022 11:12:04 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61E017FFB4 for ; Mon, 29 Aug 2022 08:12:02 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id b16so2961222wru.7 for ; Mon, 29 Aug 2022 08:12:02 -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=INSp9ODgFBimHntLRKzgvnj5KBQj+US8b8e2OelsXQI=; b=VObxgBLFcZyFwOIuHxNDB4GRTFqR14rqcc2QyqdaUCVc3L+/BefWYyGm2pln4w0DQQ 8qfKZGkq/uMpSxi4lDQcYipGN3xglQoW5uG62fJhpphXzXUu18ZtBQ5ymlFXYe8qW6NW +dr6M+eSi6cyeh8klo9kG+UXgPVXMbJsHHwsUJI7ZcURfAxnQb72uTBfjMrLpDZmclJW nf2KopDmm32Y/PnHj9WV63Tzri5Hk3DSKg3RnWf8yDEej27sNc5zjatMVFWdTrkbvCLo qaT3BQbegY9kPur7ZXXUSXEaYgEyk7bnul4XagbkSFXPwII2WeAe4HtJAKmjrQswseGp pk+g== 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=INSp9ODgFBimHntLRKzgvnj5KBQj+US8b8e2OelsXQI=; b=fZMrK66UrZzrIZnnOfUwvRKcJPnuzf88LfF1tYbT//kXi9FrBvXFvlWo6UnNOvqAoH 94CrAG0++dzGbvqVt/Ri4ssIBU7ve9eokQ7Nsf4rR6Im2vbFffj7ZXCdWIACLc2214yH Ji5q8RCyWOAO+qh6bFFhURU/NhedpUCyW38ekY7jHkedlBy8gFC7YwtXHbh54B4i3+BY B2ZUn2TdWrkZ3qhpd0kQeniXHXs6jjylLrvPhiHkka1MDcIfFYlDbz/oliJJ/4rYXS/Y HpOcyMPHyuUpc+0nPh/05NciQs61KzMhiSYwvpsshGDKvuLTAkPSCdic3PZN5zDCU2EC Fwcw== X-Gm-Message-State: ACgBeo0YaAUXtJRk3eCEocekXRVPr5LWjM5Z8FRh5cda5tdUvYnlC1jq EJN/VvIdPJU28ydp1F1kXfEI2s2tZwg= X-Google-Smtp-Source: AA6agR7UPHy8zCtPqQBL+OM8Nkiq57zmPcRcJHWjxkOCA4FeUbADY7eDeZFp2D7qkvWSdoKZF1QM9Q== X-Received: by 2002:a05:6000:1c1e:b0:225:84e6:658f with SMTP id ba30-20020a0560001c1e00b0022584e6658fmr6814621wrb.6.1661785920457; Mon, 29 Aug 2022 08:12:00 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w17-20020a5d6091000000b002237fd66585sm7254301wrt.92.2022.08.29.08.11.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 08:11:59 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Mon, 29 Aug 2022 15:11:52 +0000 Subject: [PATCH v3 1/5] 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 3b7df9bed5a..88d8133f38f 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: 12958016 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 363F6ECAAD2 for ; Mon, 29 Aug 2022 15:12:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229781AbiH2PMI (ORCPT ); Mon, 29 Aug 2022 11:12:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230134AbiH2PMF (ORCPT ); Mon, 29 Aug 2022 11:12:05 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B88180F49 for ; Mon, 29 Aug 2022 08:12:03 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id bv25so3557666wrb.10 for ; Mon, 29 Aug 2022 08:12:03 -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=i2Fz6HL6thdGuveotn37qJfG3xTHh/nY5WVtZxeZ1CI=; b=eDhWQFSYj3/2RClXboU9CrMoQdW2kwkKMXmOkLbvbkuPTVdmO7PBuwLrxsnXzJuKf7 osLuhZoBJXzGOo5fIKORo+1VNarvwDzSRZm7Ye74thNZSAL6AffiYlrqDS1i7iLqyJzW JS3IY4cOMxpTwAlXSx1mz4kpaTur+zJJmA66/QXUFuctxVhjGhxm3kfTT0QHrxgfar7/ OWY+hNFt1IUkFbQfbP8NQrEXHUNXaNHZ9hdlcis6iDWwtat7uW1LSF5lc6KKp5VzuP2d 6T70F7jHuPdlSoJrPOPbv/i/jEhHi+Rm+hj49NbVpasImLHLehehdZ11eYpQ3m0151Ls xHAQ== 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=i2Fz6HL6thdGuveotn37qJfG3xTHh/nY5WVtZxeZ1CI=; b=fjyjXuhlAxzHnr5v2tmia8DSKsY6n9X5iX2Q1JtnSMcEDn4jK/dvLEBuuz4+Eu5Xtp bLqshKb719kt+KdvRpZCsm6OJLx+zonCZUo4/2KF74Lqr+J6v4AkC+1FEf4rjJ1EhGgB sbkMZt6aFJI8zSCGadkcGJjGUxUj+ktHeOlwaG5wc2F0fp2dMm0vrhT2vtrnzVPQVwmX ku1VlwoTAzz2rcjLzBVNca+FybvR/Vp7crrc7coOzp0ZNNgXzP6w/BWy1Gnr03DFq4ag IbK9SDg917ujW4+GFOpPx6RAWnnU+orpwXL1CMMA/ttHaBfb0Csdir8UhY/GFjNpzt/U J/jQ== X-Gm-Message-State: ACgBeo15z+LN3cx2eBNo9DRGtjv083xAR13+WEge3/yaMNla5z2ky0KW ZLB/Hk26l6McwpRbgiVRc7eUCBOQX6g= X-Google-Smtp-Source: AA6agR5upIdqWse5qP6XQIzMi6GYvlUny+I2sZZt5o156sibV/DKkfpvNUlnMzC1yd/Ybg75i8J3aw== X-Received: by 2002:a5d:6f0d:0:b0:226:d1c8:a1 with SMTP id ay13-20020a5d6f0d000000b00226d1c800a1mr5956353wrb.476.1661785921798; Mon, 29 Aug 2022 08:12:01 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id i19-20020a05600c355300b003a53731f273sm9572277wmq.31.2022.08.29.08.12.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 08:12:01 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Mon, 29 Aug 2022 15:11:53 +0000 Subject: [PATCH v3 2/5] 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 Reported-by: Philippe Blain 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 88d8133f38f..c2187f9cec8 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 && + printf n >n && + force_color git -c interactive.diffFilter="sed s/@@/XX/g" \ + add -p X-Patchwork-Id: 12958018 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 2FF56C0502C for ; Mon, 29 Aug 2022 15:12:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229531AbiH2PMO (ORCPT ); Mon, 29 Aug 2022 11:12:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229841AbiH2PMG (ORCPT ); Mon, 29 Aug 2022 11:12: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 5C3B28048C for ; Mon, 29 Aug 2022 08:12:04 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id v7-20020a1cac07000000b003a6062a4f81so8358176wme.1 for ; Mon, 29 Aug 2022 08:12:04 -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=s2wDran9qrKhEZfd9bp5Vw0MKm0nDv2S770U3TfCHeA=; b=Z0/+Zfh48XzuI8hjFqfvuLNey1KuPr2JR14+dQV2mPklXQB3GC4AQfsUH7vvcf8xsm 7sD0Xbxg/D6tWNzOahOi5v3hH7+3rDy42bUOQTSkD6w3E3fmu88LBn245M//14QG10X9 dAjG3kxnKkINKrOOy7U7pubAR1+L/urejdcKf+n6ixJ5NRjwoYkkPoTCXfMzXW+pSkOF nXDRfFXi/ZXgf8AvAxSw+JUKL8Ti8FGYEuhZz8SWax4qGz78hwm25ZSF/x7KNBDYEdjN KCalvegKztJCEtlb/l9Id6vqzQHQ6ur6lfLRiL16i4DLXtwHP9nC58GUSb5Mwm5fA5xW dfTA== 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=s2wDran9qrKhEZfd9bp5Vw0MKm0nDv2S770U3TfCHeA=; b=0YcyPL0IToW+tnBU9RieZK6mZT2tTKpBUB+V481RP8FWQ/Fz6x3B2+gFVr/G2hl7Ta l4mjX2zN60meiD7x8XJQO0tCjePXQsFTU46IsPxujf0jt8E3BmN+8AMwis963A3rnceF 3vI5blnAMXODaMnG5VNyGFUQzjmMKYTQSA8zp4w94IwRPF+m7oXQN7LJ9+/2/n0SSs+t xu2184G43oSPdOo5G285jr9mNMrS5Vyy9HqETf6SyApUbS/826Cx9a0mBIjg6JsdA+Qa ihobdrmfIwIkrx4mzpO6XqjOK69BJPyQoGpv/94j9oQWLk+2W+Dtx/eXRI4Y2jOByvQA NvYA== X-Gm-Message-State: ACgBeo3YqSzooUKFb+B8u0Dm8aO0vBzlHVKFq8l5DcyaP7cU+4aJr6DT mnOFM+HQ4V3LBqOfPbS8hStNRhmEchc= X-Google-Smtp-Source: AA6agR4f5MXO1/pSs22zn0wvPdUzS8xDP/GA2VD1we8pIcHLwIGuAIfQifkJZBUZnDy0PDvS1+eShA== X-Received: by 2002:a05:600c:4f48:b0:3a5:e707:bb8c with SMTP id m8-20020a05600c4f4800b003a5e707bb8cmr7312642wmq.198.1661785922678; Mon, 29 Aug 2022 08:12:02 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l6-20020adff486000000b0021e13efa17esm7509899wro.70.2022.08.29.08.12.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 08:12:02 -0700 (PDT) Message-Id: <7a9f0b107e636ed57e511d2c145fc63596c1c69b.1661785916.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 29 Aug 2022 15:11:54 +0000 Subject: [PATCH v3 3/5] add -p: insert space in colored hunk header as needed 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 We are about to teach `git add -p` to show the entire hunk header if the `@@ ... @@` line range cannot be parsed. Previously, we showed only the remainder of that hunk header as an "colored_extra" part. To prepare for that, detect if that "colored_extra" part starts with any non-whitespace character (ignoring ANSI escape sequences) and insert a space, to make the output much more pleasant. Note that this has an effect already before we make `git add -p` more lenient when parsing the hunk headers: diff filters could already remove the space after the line range, which is precisely what we do in the regression test introduced by this commit. Signed-off-by: Johannes Schindelin --- add-patch.c | 22 ++++++++++++++++++++++ t/t3701-add-interactive.sh | 10 +++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/add-patch.c b/add-patch.c index f2fffe1af02..9d575d30ed0 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, @@ -635,6 +636,23 @@ static size_t find_next_line(struct strbuf *sb, size_t offset) return eol - sb->buf + 1; } +static int starts_with_non_ws(const char *p, size_t len) +{ + for (;;) { + size_t skip; + + if (!len || isspace(*p)) + return 0; + skip = display_mode_esc_sequence_len(p); + if (!skip) + return 1; + if (skip > len) + return 0; + p += skip; + len -= skip; + } +} + static void render_hunk(struct add_p_state *s, struct hunk *hunk, ssize_t delta, int colored, struct strbuf *out) { @@ -649,6 +667,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 +677,7 @@ 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; + needs_extra_space = starts_with_non_ws(p, len); } if (s->mode->is_reverse) @@ -673,6 +693,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 c2187f9cec8..49200b7df68 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -772,7 +772,15 @@ test_expect_success 'handle iffy colored hunk headers' ' echo content >test && printf n >n && force_color git -c interactive.diffFilter="sed s/@@/XX/g" \ - add -p output 2>&1 X-Patchwork-Id: 12958017 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 46E77ECAAD2 for ; Mon, 29 Aug 2022 15:12:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230098AbiH2PML (ORCPT ); Mon, 29 Aug 2022 11:12:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230056AbiH2PMG (ORCPT ); Mon, 29 Aug 2022 11:12:06 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD2617FE63 for ; Mon, 29 Aug 2022 08:12:04 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id b16so2961448wru.7 for ; Mon, 29 Aug 2022 08:12:04 -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=MQ1YxpBjGbCs7mGlupYs9gX/EOGPy8W/4fdNR9zno1Y=; b=OXpT4aYZWagH5PCuiGjAFgpV2zWwAZqUZON2KJaC36iwufe2rd7vOUrvC1KTvP9mb5 jLOjNbIeEwP2PDkjnz8KG0j9L4+XKXuKqrFbnrkFHXrLrMZWyZ+pDqyh+gJTKpqSzFtg KPxoWQZQJfKPTH+2xuBjOUn+ql14tfAXQNNcpWz8Yia+Cbf7jJzbxmtyOVpnUSqiuQLn id+inzUtrOoNecUV2OYJBZhRcHTLfKPAJxDOkBXiA0ijxAyX7oRJcD70WNQQFJk3rUk1 OeIUVNWTzM2Z8x5+Ou+w+B3JtvjmfTamDaB2O6ILQ03O+X3spzpSs9HqKgtfeL0ES940 IVWQ== 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=MQ1YxpBjGbCs7mGlupYs9gX/EOGPy8W/4fdNR9zno1Y=; b=4MAGP1/eiYy8INaouK/Wcm2mNv+1D0tkRdN+gAhXW5NI3xlWDOiGVQW/zsjAt67kL3 6P/dNsx92JkIL2esC/UfgybCjvdvD7eZJXNiSlNwlX6XOw0cHKpxBFVSv1JUlWgIk1Jr pMttThiJ03o6rDBD/qFvzQpIqswfRC9TmbYQTWyDJ5yMHdKbU2UzleJnG0oXqOgU4IzZ B8SJYazAPCuZaqiumP/VRReEgRt70MuEDZwv6qhRhNd75v0rG1kGqBHmSgUNyHUrZKMq GA6p49aaYFOoGOCkQAtrNei1f5E5a25MNhydaYSzXsZnoVm4H1K+85IUfRy2uLdQliHw FcEw== X-Gm-Message-State: ACgBeo39sSfGBOTes0z5sCprmwg4VUPnGnADULhZkiJOB9Fcbplq89XI gzimbbDzPNGGQhPxC/uIMXf9QasfbyM= X-Google-Smtp-Source: AA6agR4tgE4b+O72YI34Gv8Y+MGVkG0QDBZDP9mXSFTPm07eZYFy5r7K1GZuphzb++nVbeO41ZcxGg== X-Received: by 2002:a05:6000:795:b0:226:d45a:ffe5 with SMTP id bu21-20020a056000079500b00226d45affe5mr5378794wrb.33.1661785923902; Mon, 29 Aug 2022 08:12:03 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u1-20020a05600c440100b003a31ca9dfb6sm9700415wmn.32.2022.08.29.08.12.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 08:12:03 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Mon, 29 Aug 2022 15:11:55 +0000 Subject: [PATCH v3 4/5] 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 what 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. There is probably information the user wants to see, after all. In the `diff-so-fancy` case, it shows something like `@ file:1 @`. If the line range could not be found in the colored hunk header, let's just show the complete hunk header. Signed-off-by: Johannes Schindelin --- add-patch.c | 13 +++++++++++-- t/t3701-add-interactive.sh | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/add-patch.c b/add-patch.c index 9d575d30ed0..0217cdd7c4a 100644 --- a/add-patch.c +++ b/add-patch.c @@ -363,8 +363,17 @@ static int parse_hunk_header(struct add_p_state *s, struct hunk *hunk) 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; + /* + * We tried to parse the line range out of the colored hunk + * header, so that we could show just the extra information + * after the line range. + * + * At this point, we did not find that line range, but the hunk + * header likely has information that the user might find + * interesting. Let's just show the entire hunk header instead + * in that case. + */ + header->colored_extra_start = line - s->colored.buf; header->colored_extra_end = hunk->colored_start; return 0; diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 49200b7df68..39e68b6d066 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -775,10 +775,14 @@ test_expect_success 'handle iffy colored hunk headers' ' add -p output 2>&1 output-so-fancy 2>&1 X-Patchwork-Id: 12958019 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 D1272ECAAD2 for ; Mon, 29 Aug 2022 15:12:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230147AbiH2PM2 (ORCPT ); Mon, 29 Aug 2022 11:12:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230144AbiH2PMS (ORCPT ); Mon, 29 Aug 2022 11:12:18 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED74A84EC8 for ; Mon, 29 Aug 2022 08:12:06 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id j26so4421347wms.0 for ; Mon, 29 Aug 2022 08:12:06 -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=AmBpF+luDAG/4AOSYnKvZhSPCdIyN9n6q5ReqcPRgYk=; b=Pgk2NgXHoUYwui4Jixda5J2Zvw8bV//uXkQg60Sd1QqWcg8IIRU4qjl3QfgT/87N2L Rm/T7dE+pIeKMnaF4PT9VtMmhTRGT4ebVFooo538QY71N27XZ8w5Rbb0xKc5DnlsYMru C9N+HhdaoF8HxCYtF+x2bwMIYVqs844+HtCjBx7KjUWLHW77Wa9oipg+wYYDbtj3EFz9 5pT+AayificFVpjRKnwuqVoVNiPTWLbXuQnO9ODX8NcV2CdeCv0yRNXWMDc4+XpqHH4E DlQpjr0HNLbEDwUCwrWUaQilZaQbJvI0/t54ptLTIDbkTxT13NKRFFhrCNLffSvQFF0j WRIA== 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=AmBpF+luDAG/4AOSYnKvZhSPCdIyN9n6q5ReqcPRgYk=; b=cJmO4JO13hOsdIW0kuVy+gq5eSFHaloWQUuxV25vbupjSBDc2uhmlzcCby76yD5v7p r0pwe6rA3iQGyIsGr+Fv3FTNUjwDMWVCqajXfTEU6kPgbDY/kKHClo3jRefPr4sLJMCf sawVaV68NvAyy/zIWpS1SbaB5lnPSc7DQhrNNQJ0DXf7g4SPzVweA2NNUMUSUO/9FR12 VrM+fPFZ3qK6q18ZuMogzheHc8BPxO+ly9awzRiMRjxkNH9yrkNo0ZeBl+FeXK0CKOdJ dcKfLmWVYRRYxLp7hwD61WFI8UMa/j++3YeX2C2Zo/jyn9Kt2EermINKhTRF/dWtNaVR +YwA== X-Gm-Message-State: ACgBeo0UrbVQCiAQ0AchRQ2mMXuBss8FSGKoI185stnQBq/BJiCsdmNC iB4T1ADQJN6P1exhjGdCOVP3oouVzME= X-Google-Smtp-Source: AA6agR5YWocVsc1bncLIRicaoQr2uowieGR3ck1uLyMV90Bf6fpd+N7PRuSrzoxb25++xeNQSNgSwQ== X-Received: by 2002:a05:600c:4148:b0:3a5:4f94:577b with SMTP id h8-20020a05600c414800b003a54f94577bmr7284075wmm.167.1661785924976; Mon, 29 Aug 2022 08:12:04 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id i15-20020a05600c400f00b003a54f1d007csm9444263wmm.10.2022.08.29.08.12.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Aug 2022 08:12:04 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Mon, 29 Aug 2022 15:11:56 +0000 Subject: [PATCH v3 5/5] add -p: ignore dirty submodules 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 Thanks to alwyas running `diff-index` and `diff-files` with the `--numstat` option (the latter with `--ignore-submodules=dirty`) before even generating any real diff to parse, the Perl version of `git add -p` simply ignored dirty submodules and does not even offer them up for staging. However, the built-in variant did not use that flag because it tries to run only one `diff` command, skipping the unneeded `diff-index`/`diff-files` invocation of the Perl variant and therefore only faithfully recapitulates what the Perl code does once it _does_ generate and parse the real diff. This causes a problem when running the built-in `add -p` with `diff-so-fancy` because that diff colorizer always inserts an empty line before the diff header to ensure that it produces 4 lines as expected by `git add -p` (the equivalent of the non-colorized `diff`, `index`, `---` and `+++` lines). But `git diff-files` does not produce any `index` line for dirty submodules. The underlying problem is not even the discrepancy in lines, but that `git add -p` presents diffs for dirty submodules: there is nothing that _can_ be staged for those. Let's fix that bug, and teach the built-in `add -p` to ignore dirty submodules, too. This _incidentally_ also fixes the `diff-so-fancy` problem ;-) Signed-off-by: Johannes Schindelin --- add-patch.c | 3 ++- t/t3701-add-interactive.sh | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/add-patch.c b/add-patch.c index 0217cdd7c4a..ee6a3d3b712 100644 --- a/add-patch.c +++ b/add-patch.c @@ -426,7 +426,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps) } color_arg_index = args.nr; /* Use `--no-color` explicitly, just in case `diff.color = always`. */ - strvec_pushl(&args, "--no-color", "-p", "--", NULL); + strvec_pushl(&args, "--no-color", "--ignore-submodules=dirty", "-p", + "--", NULL); for (i = 0; i < ps->nr; i++) strvec_push(&args, ps->items[i].original); diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 39e68b6d066..a4f45fc48a0 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -965,6 +965,18 @@ test_expect_success 'status ignores dirty submodules (except HEAD)' ' ! grep dirty-otherwise output ' +test_expect_success 'handle submodules' ' + echo 123 >>for-submodules/dirty-otherwise/initial.t && + + force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 && + grep "No changes" output && + + force_color git -C for-submodules add -p dirty-head >output 2>&1 actual && + rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" && + grep "$rev" actual +' + test_expect_success 'set up pathological context' ' git reset --hard && test_write_lines a a a a a a a a a a a >a &&