From patchwork Fri Feb 4 21:20:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12735691 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 F05D8C433F5 for ; Fri, 4 Feb 2022 21:21:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241639AbiBDVVC (ORCPT ); Fri, 4 Feb 2022 16:21:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240096AbiBDVU6 (ORCPT ); Fri, 4 Feb 2022 16:20:58 -0500 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 34E55C06173D for ; Fri, 4 Feb 2022 13:20:57 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id f17so13613816wrx.1 for ; Fri, 04 Feb 2022 13:20:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FQSmv4OyqGoFcndv1QyfnS6MQ3Tn6Evr3TWj8Tgygls=; b=DBwSPpfAlp+4GCOeZH1pZpaZeTNi5epKfM5no17JBx0q1FC/J6rWlYwak5ZnmwCvRu VnrCNj35EC/RrZWq8NvBl4CBZkQlte4HtmY33042l4XWJ1mNr+JherkgDcFxicWlP7lY ybSfWmtGJGM9/1vjUaGkKluHz4qGkBN4Ei9xoQB6ciI96OypYouVs1jciVWsDioBBMX2 0+1yHGnvqjLC5Ga49Ldpx0PUCMdaI7/pGYM3ZhT4w7H8QiXwmMcy+SwZekN1htZF6LUI pAik5Xw5C5jHzoDK/foVt6vD/DZgGpc/RMbTyLWrZwxCx+Npy37DsHXNRzHXpF9xpFo3 ii4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FQSmv4OyqGoFcndv1QyfnS6MQ3Tn6Evr3TWj8Tgygls=; b=mzhRok4XOEMz2cZS1+EU3IRK7Cb+vxyJilkpPjqrGdZTN2aKN2a1nfbBOJtVjBTaAz Xy2a2RiNB5OokbxUxN1FLei7F+XGV6u2lPzdE9FgAc6ZCNmyG1tk9Rc8y6oXmK76kmN7 qMPMBgCadUFOBTioiE7eAhJFJsh0JZB0O+JU4cy1ky/hZ5D5rzpNFEwOoVAGIDwf/UmW nElWoKi1Yj1cJ006KQd1yGhsbMUz4XK9kDYSPrlrv0weRWD+VJhwjhzMalko0t63fCwf sjYKouV4tOWWqH3nKvg14UO6WpuZkyZZuC0Rv5yLiaqtgHq8mnl8dFDx9Dr23JYY6LLj RkXg== X-Gm-Message-State: AOAM533u04CCQkQUZyeeCFEMgYVMfNlbRT1okoy5KwQ/Z+2F569okM4S ASajuf3zUVak5lKfX9f9fMN9rI3hMmhH4A== X-Google-Smtp-Source: ABdhPJxpbYPLt/AxuZNvUM+bFxN94m/lt6jZzG7HLhBIf73vIuwK53MnxN4HBQCeWR3iQACrZHl0Hw== X-Received: by 2002:a05:6000:84:: with SMTP id m4mr603738wrx.257.1644009655401; Fri, 04 Feb 2022 13:20:55 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o21sm2321598wmh.36.2022.02.04.13.20.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 13:20:54 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v10 1/9] grep.h: remove unused "regex_t regexp" from grep_opt Date: Fri, 4 Feb 2022 22:20:43 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This "regex_t" in grep_opt has not been used since f9b9faf6f8a (builtin-grep: allow more than one patterns., 2006-05-02), we still use a "regex_t" for compiling regexes, but that's in the "grep_pat" struct". Signed-off-by: Ævar Arnfjörð Bjarmason --- grep.h | 1 - 1 file changed, 1 deletion(-) diff --git a/grep.h b/grep.h index 6a1f0ab0172..400172676a1 100644 --- a/grep.h +++ b/grep.h @@ -136,7 +136,6 @@ struct grep_opt { const char *prefix; int prefix_length; - regex_t regexp; int linenum; int columnnum; int invert; From patchwork Fri Feb 4 21:20:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12735693 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 F3B41C433FE for ; Fri, 4 Feb 2022 21:21:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241882AbiBDVVG (ORCPT ); Fri, 4 Feb 2022 16:21:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241259AbiBDVU6 (ORCPT ); Fri, 4 Feb 2022 16:20:58 -0500 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DCA1C06173E for ; Fri, 4 Feb 2022 13:20:58 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id k6-20020a05600c1c8600b003524656034cso4509483wms.2 for ; Fri, 04 Feb 2022 13:20:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=H/hEoLuFM7vuTVAeVe9AYxZvcfR1hBYsPwmrcpJ8trw=; b=FjVkCD5WK5u5c+9UjTAgJbo2SZJ0/Lhe0dC1Xb14fkFXVV4isVxlScmTVN7LLV4lID EL03WRKs/g1tUH3Zl5s1AEovt3r+kIKNHRU6lrM0FuRFdaNbp/Z6dyTfRPGlUVez21sK tNRIzTcxK0AT2LTduQZL9pr23NomD51RVQXUeFRD3TNHjWQkQ26nPQCqjYzer7Q5Mpmx y3xZ3lSlc0Mj4874hZGTwfgUio+EEz84A4bpnQ4AbFapSDH9H3NNbMh6BnTVeoEVP5tQ jRVikNBT8HlmdfVWxAyM+0ccqAFYWcxj+ujvMqXu6BhdCcEMqyX6CpAa7r1yD/gv95Hq D+KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=H/hEoLuFM7vuTVAeVe9AYxZvcfR1hBYsPwmrcpJ8trw=; b=HJnMUkSZMuAJua2JCjVWPCc6VPgUH4DR6LmBcxJ5x/3jFgi5DcYpVGErnMjvlzB/uI tQpgcJWOMpr70fgRYEAfcpJ73yKe849pbkuPBYWwvP44GW1wrDsgWVcp7qTaN7Ok8TaY AiEYhM9eNS8fxeB5nNkiKvakgrp7YHnZQZIPhNuGilqeYZJ5aGcAqvf5chzQUfRus1Pc NjQnZF1Xuw0Bh3ax+/Bn7pwx3vyvVmLfG3hiNsJyouVRtFEw37JegvbxCQe+dhfvTGks +WCaCp0X+Sh9mP8wdYpc/FGR/yqBr9dTiQ6r5j1x3+Da4w+CBxUaUSRLd3/gVk1T6jum Qp+w== X-Gm-Message-State: AOAM532QJnMpfGPl1AmYshXfUHYhCh7vsQboE8SZ327SqpgHRR1/lRUt o9gHFPRTY+vo84bIVJupUNC1v5xzpuqcWw== X-Google-Smtp-Source: ABdhPJyOrmdhT0+fQe8EuwlchoVd8L4A83qMk5ScYzW2zOve9lF7W7dKmRd342Mco4zWb3rIWTRpLQ== X-Received: by 2002:a05:600c:2e02:: with SMTP id o2mr519956wmf.34.1644009656301; Fri, 04 Feb 2022 13:20:56 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o21sm2321598wmh.36.2022.02.04.13.20.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 13:20:55 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v10 2/9] log tests: check if grep_config() is called by "log"-like cmds Date: Fri, 4 Feb 2022 22:20:44 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Extend the tests added in my 9df46763ef1 (log: add exhaustive tests for pattern style options & config, 2017-05-20) to check not only whether "git log" handles "grep.patternType", but also "git show" etc. It's sufficient to check whether we match a "fixed" or a "basic" regex here to see if these codepaths correctly invoked grep_config(). We don't need to check the details of their regular expression matching as the "log" test does. Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t4202-log.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/t/t4202-log.sh b/t/t4202-log.sh index 50495598619..e775b378e4b 100755 --- a/t/t4202-log.sh +++ b/t/t4202-log.sh @@ -449,6 +449,30 @@ test_expect_success !FAIL_PREREQS 'log with various grep.patternType configurati ) ' +for cmd in show whatchanged reflog format-patch +do + case "$cmd" in + format-patch) myarg="HEAD~.." ;; + *) myarg= ;; + esac + + test_expect_success "$cmd: understands grep.patternType, like 'log'" ' + git init "pattern-type-$cmd" && + ( + cd "pattern-type-$cmd" && + test_commit 1 file A && + test_commit "(1|2)" file B 2 && + + git -c grep.patternType=fixed $cmd --grep="..." $myarg >actual && + test_must_be_empty actual && + + git -c grep.patternType=basic $cmd --grep="..." $myarg >actual && + test_file_not_empty actual + ) + ' +done +test_done + test_expect_success 'log --author' ' cat >expect <<-\EOF && Author: A U Thor From patchwork Fri Feb 4 21:20:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12735692 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 2035AC433EF for ; Fri, 4 Feb 2022 21:21:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241758AbiBDVVD (ORCPT ); Fri, 4 Feb 2022 16:21:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241540AbiBDVU7 (ORCPT ); Fri, 4 Feb 2022 16:20:59 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 067C5C061714 for ; Fri, 4 Feb 2022 13:20:59 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id m14so13506831wrg.12 for ; Fri, 04 Feb 2022 13:20:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ucLAbwHgzw6Kbb5NQJd0vhydTFqO/4PeoAl9dbORUSI=; b=J1526VrkMEme7c2zbPKM7aHNPzpLugqgUZKrIkVZ/1uctLm+vBGIgdqZhZyTjp/P20 J21eV8mxQxsBnDdYle1Z/hBQo1y0sYZh/2PDV1LGGlj5vPaLXnbJqzyNso56YWr3bSqu j4dZqTqX7PzDezjtZMcbhjRGxkIQ/dg4qAFi/3qJ+SS7lTSKt04G7VoQJ1a9D6takQ49 fTgiVg4SHHLq6Oz6GIGeU1wgUk25AW+tbaZv6l1c/Wq8qvdAwX8Zh/lQDx5UMSj+CJD2 51umL/se1sztCrsB3EXQSfmBT4TpU9bKbC0y+OttWFXRT8KnUbuOJu8GGNklZltX0qFw 5yvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ucLAbwHgzw6Kbb5NQJd0vhydTFqO/4PeoAl9dbORUSI=; b=pRzcNhWoNeK7bLuakK5iwLDz73suaJBmdX00RKUlEpb2ZM3SN8jWOlq1YArL7xEtan E8/LL0EdPzZfs0DQ3hmNxYVOHz+2ksE97cruirtgPGdxFC9k/KhLXHIW+O/0lhETxdY0 RPa85Tnsbhfe0bSSWf/FqPt0X8qefQoHav+E6YjCieB+SZu99zpBV05r0eKg4b8qmPBt oRasTBnar7GQw7CJwrc0bdbpFp2RzxKwJXS9xNjSCmB1LlfZD/nLBhwltxRycNrgEcPj iMUHU/wH8XFGkdMB+kbQpWBKC8QSamdJ5YkqCzcw2hujZbCnrR5k61chq+yRpQ2jI8P4 GD4Q== X-Gm-Message-State: AOAM5310PNUdE661Pope1ZaLWqsDRzEP6VxfPWdbWkF3bFWcNihylR5p EVyjkIGk/CznoA+jH7qpEM8eM9k9wQBCww== X-Google-Smtp-Source: ABdhPJy3oq01E5RlHBNDMcxXxiFywt+p5sFoZfTRxq9wi8+zl+qf3H5nVg1ULuvZQufhHz4kT5InzQ== X-Received: by 2002:a5d:5481:: with SMTP id h1mr635170wrv.386.1644009657322; Fri, 04 Feb 2022 13:20:57 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o21sm2321598wmh.36.2022.02.04.13.20.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 13:20:56 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v10 3/9] grep tests: add missing "grep.patternType" config tests Date: Fri, 4 Feb 2022 22:20:45 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Extend the grep tests to assert that setting "grep.patternType=extended" followed by "grep.patternType=default" will behave as if "--basic-regexp" was provided, and not as "--extended-regexp". In a subsequent commit we'll need to treat "grep.patternType=default" as a special-case, but let's make sure we ignore it if it's being set to "default" following an earlier non-"default" "grep.patternType" setting. Let's also test what happens when we have a sequence of "extended" followed by "default" and "fixed". In that case the "fixed" should prevail, as well as tests to check that a "grep.extendedRegexp=true" followed by a "grep.extendedRegexp=false" behaves as though "grep.extendedRegexp" wasn't provided. See [1] for the source of some of these tests, and their initial (pseudocode) implementation, and [2] for a later discussion about a breakage due to missing testing (which had been noted in [1] all along). 1. https://lore.kernel.org/git/xmqqv8zf6j86.fsf@gitster.g/ 2. https://lore.kernel.org/git/xmqqpmoczwtu.fsf@gitster.g/ Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Junio C Hamano --- t/t7810-grep.sh | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh index 424c31c3287..79884787da2 100755 --- a/t/t7810-grep.sh +++ b/t/t7810-grep.sh @@ -451,6 +451,93 @@ do test_cmp expected actual ' + test_expect_success "grep $L with grep.extendedRegexp is last-one-wins" ' + echo "${HC}ab:a+bc" >expected && + git \ + -c grep.extendedRegexp=true \ + -c grep.patternType=basic \ + -c grep.extendedRegexp=false \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L with grep.extendedRegexp is last-one-wins & defers to grep.patternType" ' + echo "${HC}ab:abc" >expected && + git \ + -c grep.extendedRegexp=true \ + -c grep.patternType=extended \ + -c grep.extendedRegexp=false \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L with grep.extendedRegexp and grep.patternType are both last-one-wins independently (ERE)" ' + echo "${HC}ab:abc" >expected && + git \ + -c grep.patternType=fixed \ + -c grep.extendedRegexp=true \ + -c grep.patternType=default \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L with grep.extendedRegexp and grep.patternType are both last-one-wins independently (ERE)" ' + echo "${HC}ab:abc" >expected && + git \ + -c grep.extendedRegexp=false \ + -c grep.patternType=default \ + -c grep.extendedRegexp=true \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L with grep.extendedRegexp and grep.patternType are both last-one-wins independently (BRE)" ' + echo "${HC}ab:a+bc" >expected && + git \ + -c grep.extendedRegexp=true \ + -c grep.extendedRegexp=false \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L with grep.extendedRegexp and grep.patternType are both last-one-wins independently (BRE)" ' + echo "${HC}ab:abc" >expected && + git \ + -c grep.extendedRegexp=false \ + -c grep.extendedRegexp=true \ + -c grep.patternType=default \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L with grep.extendedRegexp and grep.patternType are both last-one-wins independently (BRE)" ' + echo "${HC}ab:a+bc" >expected && + git \ + -c grep.patternType=default \ + -c grep.extendedRegexp=true \ + -c grep.patternType=basic \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + test_expect_success "grep $L with grep.patternType=extended and grep.patternType=default" ' + echo "${HC}ab:a+bc" >expected && + git \ + -c grep.patternType=extended \ + -c grep.patternType=default \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + + test_expect_success "grep $L with grep.patternType=[extended -> default -> fixed]" ' + echo "${HC}ab:a+b*c" >expected && + git \ + -c grep.patternType=extended \ + -c grep.patternType=default \ + -c grep.patternType=fixed \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" ' echo "${HC}ab:abc" >expected && git \ @@ -478,6 +565,15 @@ do test_cmp expected actual ' + test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=default" ' + echo "${HC}ab:abc" >expected && + git \ + -c grep.extendedRegexp=false \ + -c grep.patternType=extended \ + grep "a+b*c" $H ab >actual && + test_cmp expected actual + ' + test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" ' echo "${HC}ab:a+bc" >expected && git \ From patchwork Fri Feb 4 21:20:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12735695 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 E6129C433F5 for ; Fri, 4 Feb 2022 21:21:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242400AbiBDVVK (ORCPT ); Fri, 4 Feb 2022 16:21:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241566AbiBDVVB (ORCPT ); Fri, 4 Feb 2022 16:21:01 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B9DBC06173D for ; Fri, 4 Feb 2022 13:21:00 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id h125so75372wmh.1 for ; Fri, 04 Feb 2022 13:20:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VN22oQbZbH3ffCASLix+KIA464ouTp81Bu6OboYKdcE=; b=PrEXlNkQKy2ALaA8FfTMePWgcY8gTSRiLvNFQyjTNYZX9sPhuAc0LatcffA7obb3TN DXqaEFCAtES7UC9PbwTQVEOeSvGQbrzI6o8RbSQAyKsirmXN/G+mfxbG25PHO1Bs0TJV L9YqBP8RuZl2dAPKiMTDyjW4heM630931eHgOgjDjdqclOBpGVO7aaUgU3UccR2vXNJo DF9sp3yxYaSV0S1RnDCVhxNzpL3tqOyuNgfDr6Ov8VT5dzMEKH965+XXXdji27em5P5j xXdZ4MSp5ksZv3qI3SkXh568EBbsYHMBqmM2Bq1i8kBmfJ03jt4d/nE/gCJ2GJQpVAO/ ijQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VN22oQbZbH3ffCASLix+KIA464ouTp81Bu6OboYKdcE=; b=fldnnLx2QkQnAgGBWnVKiwcibrs+MVHpD9B25yRrEM0Q+CWW9Y87raYFEgzp/LWiOm rMxdU4jIf2cNZ6RwkKABkitpFD2m13qzyZfTuilbp++OYlXVaOF9y5UcFus4iPxF0AP2 9O+Ms2Y9wZ3xbSWr5GzYgoNoaTPvxGN/NjcWVk5FTsl8YETs1ovW9uaCDBp8BdOb46B9 MgRPUJYs24/5MhHqI/Q8scVecZNFiLlOtPqOnLu6zIa/6XJpIns8gLs/wg86fqRXVyrF /x0ldHS5gBGt8RRk/nbGRbBdHtgbvyMgdUASPukvv4IFk/B7Ylo1bcIu+N7prRym7Bd8 U4wQ== X-Gm-Message-State: AOAM530OxD9OS/awZlFKYXHIkU0wHDMPg1b7ifqDejZ/+yK0rMpFshwX 7ClsMfX7m3mP87VcwD5Ger3jCFzPiG1dAA== X-Google-Smtp-Source: ABdhPJxhc+YyAgZO86gCL3vNbwcauEFURjfV4zomlv2AvcDIPgT9YzQ/igTKN82SduywTgEY8CWKEQ== X-Received: by 2002:a05:600c:1ca2:: with SMTP id k34mr480152wms.171.1644009658326; Fri, 04 Feb 2022 13:20:58 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o21sm2321598wmh.36.2022.02.04.13.20.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 13:20:57 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v10 4/9] built-ins: trust the "prefix" from run_builtin() Date: Fri, 4 Feb 2022 22:20:46 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change code in "builtin/grep.c" and "builtin/ls-tree.c" to trust the "prefix" passed from "run_builtin()". The "prefix" we get from setup.c is either going to be NULL or a string of length >0, never "". So we can drop the "prefix && *prefix" checks added for "builtin/grep.c" in 0d042fecf2f (git-grep: show pathnames relative to the current directory, 2006-08-11), and for "builtin/ls-tree.c" in a69dd585fca (ls-tree: chomp leading directories when run from a subdirectory, 2005-12-23). As seen in code in revision.c that was added in cd676a51367 (diff --relative: output paths as relative to the current subdirectory, 2008-02-12) we already have existing code that does away with this assertion. This makes it easier to reason about a subsequent change to the "prefix_length" code in grep.c in a subsequent commit, and since we're going to the trouble of doing that let's leave behind an assert() to promise this to any future callers. For "builtin/grep.c" it would be painful to pass the "prefix" down the callchain of: cmd_grep -> grep_tree -> grep_submodule -> grep_cache -> grep_oid -> grep_source_name So for the code that needs it in grep_source_name() let's add a "grep_prefix" variable similar to the existing "ls_tree_prefix". While at it let's move the code in cmd_ls_tree() around so that we assign to the "ls_tree_prefix" right after declaring the variables, and stop assigning to "prefix". We only subsequently used that variable later in the function after clobbering it. Let's just use our own "grep_prefix" instead. Let's also add an assert() in git.c, so that we'll make this promise about the "prefix" to any current and future callers, as well as to any readers of the code. Code history: * The strlen() in "grep.c" hasn't been used since 493b7a08d80 (grep: accept relative paths outside current working directory, 2009-09-05). When that code was added in 0d042fecf2f (git-grep: show pathnames relative to the current directory, 2006-08-11) we used the length. But since 493b7a08d80 we haven't used it for anything except a boolean check that we could have done on the "prefix" member itself. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/grep.c | 13 ++++++++----- builtin/ls-tree.c | 2 +- git.c | 1 + grep.c | 4 +--- grep.h | 4 +--- revision.c | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index 9e34a820ad4..d85cbabea67 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -26,6 +26,8 @@ #include "object-store.h" #include "packfile.h" +static const char *grep_prefix; + static char const * const grep_usage[] = { N_("git grep [] [-e] [...] [[--] ...]"), NULL @@ -315,11 +317,11 @@ static void grep_source_name(struct grep_opt *opt, const char *filename, strbuf_reset(out); if (opt->null_following_name) { - if (opt->relative && opt->prefix_length) { + if (opt->relative && grep_prefix) { struct strbuf rel_buf = STRBUF_INIT; const char *rel_name = relative_path(filename + tree_name_len, - opt->prefix, &rel_buf); + grep_prefix, &rel_buf); if (tree_name_len) strbuf_add(out, filename, tree_name_len); @@ -332,8 +334,8 @@ static void grep_source_name(struct grep_opt *opt, const char *filename, return; } - if (opt->relative && opt->prefix_length) - quote_path(filename + tree_name_len, opt->prefix, out, 0); + if (opt->relative && grep_prefix) + quote_path(filename + tree_name_len, grep_prefix, out, 0); else quote_c_style(filename + tree_name_len, out, NULL, 0); @@ -962,9 +964,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix) PARSE_OPT_NOCOMPLETE), OPT_END() }; + grep_prefix = prefix; git_config(grep_cmd_config, NULL); - grep_init(&opt, the_repository, prefix); + grep_init(&opt, the_repository); /* * If there is no -- then the paths must exist in the working diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 3a442631c71..6cb554cbb0a 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -150,7 +150,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) git_config(git_default_config, NULL); ls_tree_prefix = prefix; - if (prefix && *prefix) + if (prefix) chomp_prefix = strlen(prefix); argc = parse_options(argc, argv, prefix, ls_tree_options, diff --git a/git.c b/git.c index edda922ce6d..9d257e092da 100644 --- a/git.c +++ b/git.c @@ -436,6 +436,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv) } else { prefix = NULL; } + assert(!prefix || *prefix); precompose_argv_prefix(argc, argv, NULL); if (use_pager == -1 && run_setup && !(p->option & DELAY_PAGER_CONFIG)) diff --git a/grep.c b/grep.c index 7bb0360869a..8421dc55486 100644 --- a/grep.c +++ b/grep.c @@ -139,13 +139,11 @@ int grep_config(const char *var, const char *value, void *cb) * default values from the template we read the configuration * information in an earlier call to git_config(grep_config). */ -void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix) +void grep_init(struct grep_opt *opt, struct repository *repo) { *opt = grep_defaults; opt->repo = repo; - opt->prefix = prefix; - opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0; opt->pattern_tail = &opt->pattern_list; opt->header_tail = &opt->header_list; } diff --git a/grep.h b/grep.h index 400172676a1..23a2a41d2c4 100644 --- a/grep.h +++ b/grep.h @@ -134,8 +134,6 @@ struct grep_opt { */ struct repository *repo; - const char *prefix; - int prefix_length; int linenum; int columnnum; int invert; @@ -182,7 +180,7 @@ struct grep_opt { }; int grep_config(const char *var, const char *value, void *); -void grep_init(struct grep_opt *, struct repository *repo, const char *prefix); +void grep_init(struct grep_opt *, struct repository *repo); void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt); void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t); diff --git a/revision.c b/revision.c index ad4286fbdde..d6e0e2b23b5 100644 --- a/revision.c +++ b/revision.c @@ -1838,7 +1838,7 @@ void repo_init_revisions(struct repository *r, revs->commit_format = CMIT_FMT_DEFAULT; revs->expand_tabs_in_log_default = 8; - grep_init(&revs->grep_filter, revs->repo, prefix); + grep_init(&revs->grep_filter, revs->repo); revs->grep_filter.status_only = 1; repo_diff_setup(revs->repo, &revs->diffopt); From patchwork Fri Feb 4 21:20:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12735694 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 83571C433F5 for ; Fri, 4 Feb 2022 21:21:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241761AbiBDVVI (ORCPT ); Fri, 4 Feb 2022 16:21:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241626AbiBDVVB (ORCPT ); Fri, 4 Feb 2022 16:21:01 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C825DC061401 for ; Fri, 4 Feb 2022 13:21:00 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id m14so13506941wrg.12 for ; Fri, 04 Feb 2022 13:21:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KGJxs0C7b7ND+ak7wsHfwV0J+rvRtG8FRFEwqqKEqKI=; b=chd7XGeq9c+fyZCyPe2MFMhF7iY4xB/a7DW9vQA3eJ+QrIsalEaeyIUaTLthxfKQV4 ikf1i1tq6Osr4Hf5QEYUBBlwaBoWOiN1rjjYgd7ZsD+N5SQHn8ux75yKhQ7GBLzeYNbb aBdjsOwtfq5gEZ6IG/irusQ7VsKqRFO+4uyY3XPuKXibKB/jpWO/aepEdq0F5bQjhMmc kGrHS6nW8JgwT8Wfb3MfBeVJfnx2kNZTBtiS6KjzT5598eEiHCu+AziLNoyY2PhHuO59 cSAH75qs8g0vDrHJiO+FSeXJinIgBv8IZBlpWaQKEfBUVBegNXlBoUf/WTehdYO+QkyF J+Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KGJxs0C7b7ND+ak7wsHfwV0J+rvRtG8FRFEwqqKEqKI=; b=1IWnrOZs7ReF37+PPaa7I3VJsvCFR/Iz5UauzdSb7eDDmd0lOSJBFLOerHj07J71ov mXOpjbScXdy4mB9u+1/KZitcEYCCJW55160XdZuB5iuon3cKEc90VSbeuN3Jrpvf2RMq ARfMT9chKHJnUx0sGphekLYs+bR6fFEz/CIXhZd+pQbM0tID6AjrFT24vTjDT7DpQ7S3 onQO1wNTrsNklzq+4X6Ve8omBfS5FffHWd0XCTRnPth/M2fPau+U13ge48yDwPaCkJei 9oC2Pzn2MdPKJvnWLb1txeT5aDQyAyYT7jeS8Np6VtOWGfpb3QZ7YDvuMcsiByOHtSVd z+BA== X-Gm-Message-State: AOAM532MRJg+jAyOzVZSNHTB2UrVqu46WvLVmDDz24qhTbQqZ2HTefRe z2v5a9e66+HXyZVoj4Vp6vdz/uKvo4FG9w== X-Google-Smtp-Source: ABdhPJwAHC3JGS102X5Qwt3teZ+E8Fk3zn9Nxq5VrXHCbj9OB/LIehi709ZDMMicRYruY0pQqhfhZg== X-Received: by 2002:a5d:484b:: with SMTP id n11mr606653wrs.528.1644009659196; Fri, 04 Feb 2022 13:20:59 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o21sm2321598wmh.36.2022.02.04.13.20.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 13:20:58 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v10 5/9] grep.c: don't pass along NULL callback value Date: Fri, 4 Feb 2022 22:20:47 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change grep_cmd_config() to stop passing around the always-NULL "cb" value. When this code was added in 7e8f59d577e (grep: color patterns in output, 2009-03-07) it was non-NULL, but when that changed in 15fabd1bbd4 (builtin/grep.c: make configuration callback more reusable, 2012-10-09) this code was left behind. In a subsequent change I'll start using the "cb" value, this will make it clear which functions we call need it, and which don't. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/grep.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index d85cbabea67..5ec4cecae45 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -285,8 +285,8 @@ static int wait_all(void) static int grep_cmd_config(const char *var, const char *value, void *cb) { - int st = grep_config(var, value, cb); - if (git_color_default_config(var, value, cb) < 0) + int st = grep_config(var, value, NULL); + if (git_color_default_config(var, value, NULL) < 0) st = -1; if (!strcmp(var, "grep.threads")) { From patchwork Fri Feb 4 21:20:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12735698 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 D56C8C433F5 for ; Fri, 4 Feb 2022 21:21:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242519AbiBDVVP (ORCPT ); Fri, 4 Feb 2022 16:21:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241716AbiBDVVD (ORCPT ); Fri, 4 Feb 2022 16:21:03 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5059C061714 for ; Fri, 4 Feb 2022 13:21:01 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id i187-20020a1c3bc4000000b0034d2ed1be2aso10256805wma.1 for ; Fri, 04 Feb 2022 13:21:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tsvs4H/Fly0kFhimtu8TpyJ9+A/14xdcqvM545cN7Ko=; b=BUDctcqQEkqWCj4L2tL4NaQlDdFiiIfJVjvEjukbshnCOXbamE/yuMtfSE96pnXENz 98PM/HFk6NNU76Y2dnWj5D2Dnh4GbEidqNszXyDvqqdd7gIOAgkVCB6OPZ3o4dspv/Yx dCyvdwr0OEYhBP+C/yMpkvNCNCpAZo74XoyO49oVjTodHV14eJ1E0TNV2n0cnofvLf9/ cifNZV069iN8wROsPlGCBynDvB8yTin66tUsrnOSGpTb7OmeVgFw4mFy2SW++pRxG6Nd 3EZa46AhaZKeH/4mKPrtv3TnH/5LBR/4tBbez7+lp/JHFhv4iem7AvAo0XHwb8jV+WGw zlzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tsvs4H/Fly0kFhimtu8TpyJ9+A/14xdcqvM545cN7Ko=; b=j0YZoH4qTEUf7mgvSDE1TvUrgpzxk3J9p2CMsJUh0QYyfP6bdnJO2baV5sJ7tP5OcN vgmqV1BxQGTDyfshI/wz+YBknzJtU2SLN8kLHo+HBZ+ziR/U28JSyTVOzuCmLTCLZStU s1QbK6Hn8XSEtVbC12zwMp7pjPudqMG7e9+8TYZ5m4YBh048qvHvcu+MIX+JwZ3GBnkF YhAMdmHYefYYwrWwPEhRJ+OqmY4QiwKAQSDfOgeNzcwIGnEg6p74UQxnJuDyrVP7BpKe BeIxppyImy1KG9RB4380q4GnMg8gNvxspVLNd6EnTwMV0XMHpnadPRV27O05rA711Cm/ Kr+A== X-Gm-Message-State: AOAM530OdsG16ngezEzhlczOamWzZGDbfTgbXdqHaFvpWiyfPY2Vqaw1 K1ZFenU1AyY00lj7Smqz/9wI/6JjOE3s5Q== X-Google-Smtp-Source: ABdhPJysclnIPa2NRhY/aoS07mdpntmuRnCDPjuFN0Z+A/TFqAzS53ee5M2Jah2YXfXdcprCShB/2A== X-Received: by 2002:a05:600c:4f0b:: with SMTP id l11mr3936725wmq.126.1644009660186; Fri, 04 Feb 2022 13:21:00 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o21sm2321598wmh.36.2022.02.04.13.20.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 13:20:59 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v10 6/9] grep API: call grep_config() after grep_init() Date: Fri, 4 Feb 2022 22:20:48 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The grep_init() function used the odd pattern of initializing the passed-in "struct grep_opt" with a statically defined "grep_defaults" struct, which would be modified in-place when we invoked grep_config(). So we effectively (b) initialized config, (a) then defaults, (c) followed by user options. Usually those are ordered as "a", "b" and "c" instead. As the comments being removed here show the previous behavior needed to be carefully explained as we'd potentially share the populated configuration among different instances of grep_init(). In practice we didn't do that, but now that it can't be a concern anymore let's remove those comments. This does not change the behavior of any of the configuration variables or options. That would have been the case if we didn't move around the grep_config() call in "builtin/log.c". But now that we call "grep_config" after "git_log_config" and "git_format_config" we'll need to pass in the already initialized "struct grep_opt *". See 6ba9bb76e02 (grep: copy struct in one fell swoop, 2020-11-29) and 7687a0541e0 (grep: move the configuration parsing logic to grep.[ch], 2012-10-09) for the commits that added the comments. The memcpy() pattern here will be optimized away and follows the convention of other *_init() functions. See 5726a6b4012 (*.c *_init(): define in terms of corresponding *_INIT macro, 2021-07-01). Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/grep.c | 4 ++-- builtin/log.c | 13 +++++++++++-- grep.c | 39 +++------------------------------------ grep.h | 21 +++++++++++++++++++++ 4 files changed, 37 insertions(+), 40 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index 5ec4cecae45..0ea124321b6 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -285,7 +285,7 @@ static int wait_all(void) static int grep_cmd_config(const char *var, const char *value, void *cb) { - int st = grep_config(var, value, NULL); + int st = grep_config(var, value, cb); if (git_color_default_config(var, value, NULL) < 0) st = -1; @@ -966,8 +966,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix) }; grep_prefix = prefix; - git_config(grep_cmd_config, NULL); grep_init(&opt, the_repository); + git_config(grep_cmd_config, &opt); /* * If there is no -- then the paths must exist in the working diff --git a/builtin/log.c b/builtin/log.c index 4b493408cc5..06283b37e7a 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -520,8 +520,6 @@ static int git_log_config(const char *var, const char *value, void *cb) return 0; } - if (grep_config(var, value, cb) < 0) - return -1; if (git_gpg_config(var, value, cb) < 0) return -1; return git_diff_ui_config(var, value, cb); @@ -536,6 +534,8 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix) git_config(git_log_config, NULL); repo_init_revisions(the_repository, &rev, prefix); + git_config(grep_config, &rev.grep_filter); + rev.diff = 1; rev.simplify_history = 0; memset(&opt, 0, sizeof(opt)); @@ -650,6 +650,8 @@ int cmd_show(int argc, const char **argv, const char *prefix) memset(&match_all, 0, sizeof(match_all)); repo_init_revisions(the_repository, &rev, prefix); + git_config(grep_config, &rev.grep_filter); + rev.diff = 1; rev.always_show_header = 1; rev.no_walk = 1; @@ -733,6 +735,8 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix) repo_init_revisions(the_repository, &rev, prefix); init_reflog_walk(&rev.reflog_info); + git_config(grep_config, &rev.grep_filter); + rev.verbose_header = 1; memset(&opt, 0, sizeof(opt)); opt.def = "HEAD"; @@ -766,6 +770,8 @@ int cmd_log(int argc, const char **argv, const char *prefix) git_config(git_log_config, NULL); repo_init_revisions(the_repository, &rev, prefix); + git_config(grep_config, &rev.grep_filter); + rev.always_show_header = 1; memset(&opt, 0, sizeof(opt)); opt.def = "HEAD"; @@ -1848,10 +1854,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix) extra_hdr.strdup_strings = 1; extra_to.strdup_strings = 1; extra_cc.strdup_strings = 1; + init_log_defaults(); init_display_notes(¬es_opt); git_config(git_format_config, NULL); repo_init_revisions(the_repository, &rev, prefix); + git_config(grep_config, &rev.grep_filter); + rev.show_notes = show_notes; memcpy(&rev.notes_opt, ¬es_opt, sizeof(notes_opt)); rev.commit_format = CMIT_FMT_EMAIL; diff --git a/grep.c b/grep.c index 8421dc55486..35e12e43c09 100644 --- a/grep.c +++ b/grep.c @@ -19,27 +19,6 @@ static void std_output(struct grep_opt *opt, const void *buf, size_t size) fwrite(buf, size, 1, stdout); } -static struct grep_opt grep_defaults = { - .relative = 1, - .pathname = 1, - .max_depth = -1, - .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED, - .colors = { - [GREP_COLOR_CONTEXT] = "", - [GREP_COLOR_FILENAME] = GIT_COLOR_MAGENTA, - [GREP_COLOR_FUNCTION] = "", - [GREP_COLOR_LINENO] = GIT_COLOR_GREEN, - [GREP_COLOR_COLUMNNO] = GIT_COLOR_GREEN, - [GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED, - [GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED, - [GREP_COLOR_SELECTED] = "", - [GREP_COLOR_SEP] = GIT_COLOR_CYAN, - }, - .only_matching = 0, - .color = -1, - .output = std_output, -}; - static const char *color_grep_slots[] = { [GREP_COLOR_CONTEXT] = "context", [GREP_COLOR_FILENAME] = "filename", @@ -75,20 +54,12 @@ define_list_config_array_extra(color_grep_slots, {"match"}); */ int grep_config(const char *var, const char *value, void *cb) { - struct grep_opt *opt = &grep_defaults; + struct grep_opt *opt = cb; const char *slot; if (userdiff_config(var, value) < 0) return -1; - /* - * The instance of grep_opt that we set up here is copied by - * grep_init() to be used by each individual invocation. - * When populating a new field of this structure here, be - * sure to think about ownership -- e.g., you might need to - * override the shallow copy in grep_init() with a deep copy. - */ - if (!strcmp(var, "grep.extendedregexp")) { opt->extended_regexp_option = git_config_bool(var, value); return 0; @@ -134,14 +105,10 @@ int grep_config(const char *var, const char *value, void *cb) return 0; } -/* - * Initialize one instance of grep_opt and copy the - * default values from the template we read the configuration - * information in an earlier call to git_config(grep_config). - */ void grep_init(struct grep_opt *opt, struct repository *repo) { - *opt = grep_defaults; + struct grep_opt blank = GREP_OPT_INIT; + memcpy(opt, &blank, sizeof(*opt)); opt->repo = repo; opt->pattern_tail = &opt->pattern_list; diff --git a/grep.h b/grep.h index 23a2a41d2c4..3112d1c2a38 100644 --- a/grep.h +++ b/grep.h @@ -179,6 +179,27 @@ struct grep_opt { void *output_priv; }; +#define GREP_OPT_INIT { \ + .relative = 1, \ + .pathname = 1, \ + .max_depth = -1, \ + .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED, \ + .colors = { \ + [GREP_COLOR_CONTEXT] = "", \ + [GREP_COLOR_FILENAME] = GIT_COLOR_MAGENTA, \ + [GREP_COLOR_FUNCTION] = "", \ + [GREP_COLOR_LINENO] = GIT_COLOR_GREEN, \ + [GREP_COLOR_COLUMNNO] = GIT_COLOR_GREEN, \ + [GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED, \ + [GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED, \ + [GREP_COLOR_SELECTED] = "", \ + [GREP_COLOR_SEP] = GIT_COLOR_CYAN, \ + }, \ + .only_matching = 0, \ + .color = -1, \ + .output = std_output, \ +} + int grep_config(const char *var, const char *value, void *); void grep_init(struct grep_opt *, struct repository *repo); void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt); From patchwork Fri Feb 4 21:20:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12735696 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 A50D1C433EF for ; Fri, 4 Feb 2022 21:21:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242424AbiBDVVM (ORCPT ); Fri, 4 Feb 2022 16:21:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241753AbiBDVVD (ORCPT ); Fri, 4 Feb 2022 16:21:03 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB2A5C06173D for ; Fri, 4 Feb 2022 13:21:02 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id bg21-20020a05600c3c9500b0035283e7a012so4538267wmb.0 for ; Fri, 04 Feb 2022 13:21:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9SyXSMkK5Z5s8yiRlt74UeTgloodTUcm3Bm0YblH99U=; b=pv6zsJLqZV4ADSuAgjvcORM3IdYz8SsFMSYsVsdXYcObC237ZjGstJOO6lTYYXJ0J4 9i7ZZ2zFo0BHWguCzcGlQhklrbA67bqBadPWHGrM3mkhoNb80n2tthjPK3aHcSxez31v 9z+xouxf4wzMRtSissdFXa3IOk0SKVOWQiT9cpgB4Shyl87jIulL8eAtmbQegjt+w/Re qOEyeaTSdpEjQ8jdNg26cx+cya4T2dfocUTy1bV/Q4YiCtVnUxdFUmsL4MjsJpTh+xKO Labpw3kJvFROo9lRFKoWSNHT/Ahrqc8LR1T+rXfn4mfvqQoAzKYuZRtX2uYD48y43EMn Y7eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9SyXSMkK5Z5s8yiRlt74UeTgloodTUcm3Bm0YblH99U=; b=0dN/Nmk+2by8qrqDdsTkmN7KCGWq5iJA5pQDe8gynYmMMQiVzUx+2g2DP/n3d0UC1j s3rR5yhd13M10iSlul0hTvID/8adcjGQQB6bfmdnOxv7Cz4o015CA1uf/rJK2F+IJGv6 7Q2VKiQ3T+M7dNGInRaUW3DKLxN2fIsKHbajPjSBEIo+1NCfYN9Shs7xAvcqLGSCFSgz s7uITk4dgzPiZD47bCq9hQakC/qtJi5cb4dGqGRphQ0v/OhJH88Wm1ix+am5fScCmllX xGJmc3Qrwqw2dz/hEXBVL6ZNcVUrRlUUv5Sq3AzUvhBij84rZ5BWixbZIQb5Kl+uv4kN QgFw== X-Gm-Message-State: AOAM533uMV4P5zJii8kVc8PJy/0jOvlg0I3DDRW1ElIkfQ5EmJ2JoTKb 2lnVaLKxVu/utZwO6KhvhUuO5Pa1GcUPuw== X-Google-Smtp-Source: ABdhPJyxRAsw3sO2oRQATp7WPyqcvbiaw5fi/ZmoISRPSr+bCQ/KnXdzRWiwdY4jghU3czQx85WdwA== X-Received: by 2002:a05:600c:4f84:: with SMTP id n4mr524916wmq.106.1644009661181; Fri, 04 Feb 2022 13:21:01 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o21sm2321598wmh.36.2022.02.04.13.21.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 13:21:00 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v10 7/9] grep.h: make "grep_opt.pattern_type_option" use its enum Date: Fri, 4 Feb 2022 22:20:49 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the "pattern_type_option" member of "struct grep_opt" to use the enum type we use for it. Signed-off-by: Ævar Arnfjörð Bjarmason --- grep.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grep.h b/grep.h index 3112d1c2a38..89a2ce51130 100644 --- a/grep.h +++ b/grep.h @@ -164,7 +164,7 @@ struct grep_opt { int funcname; int funcbody; int extended_regexp_option; - int pattern_type_option; + enum grep_pattern_type pattern_type_option; int ignore_locale; char colors[NR_GREP_COLORS][COLOR_MAXLEN]; unsigned pre_context; From patchwork Fri Feb 4 21:20:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12735697 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 D8154C433FE for ; Fri, 4 Feb 2022 21:21:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242410AbiBDVVO (ORCPT ); Fri, 4 Feb 2022 16:21:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241881AbiBDVVE (ORCPT ); Fri, 4 Feb 2022 16:21:04 -0500 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B580AC061401 for ; Fri, 4 Feb 2022 13:21:03 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id s10so10992999wra.5 for ; Fri, 04 Feb 2022 13:21:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XzdgwU8RRIe5yFiWP9ykEdLW0y6riuT+J9AQ/6zgWFo=; b=Ia7GD/B731ZP8vAmP7Ca9KU+La2bc7spYaopZqWxIZNFXql/idK37/A6CLEejRoR+f ihgh85akcVgvsRrJ4AOupWwf+576X8ujAdps51f+mJsf+oix3/i86T1fNwa2Qd4dHsmN i9ut1ZVRuO6+emGjZpdN3vV8ozcnZ/e7Rjz5u/xeGc4NwU49BDS80SUkzkaqWbcrKPwq 7AoVGRKT3euvwDxc/2pmOjlCvGN4LclfypcNYZTmE+a8S0eu9L1bDDNQz79jkqV93vwX 2oxa3aA9wyUtSjp2xdSmGXKc9lg2U2ttb0Rf7tmDanBNRrD/SE76NemKEMuNvUalPOua jq2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XzdgwU8RRIe5yFiWP9ykEdLW0y6riuT+J9AQ/6zgWFo=; b=RVwoTorYpC4bR+RurCUlV4TmeiXTI2/gEF9OfXYssqThbDZVUO339RnOnBWjmXLKpi gdI+HvmpLz086XxAcYMPWQcnlnzoWWvub28oncQ4IEMB1jE+GvFdX33dB5SUnBPHs3Es gA4mNHmsMj8Ih9VBCmjmEQkV2BXu8tGFHkQylKTEhuGTJPaJL7yOV01UkGRQ7xf+Okwy Gum/k2SBdGVr6y3nG5csVfYnmoQ3ML8VyV8yeECt0GuLErqdOUwrEyPMpSTkUmYhETN2 u/Up2ioamox63TfAGRycr9ifS2oG/2bQ7cJmx2/9MRUi5Rkxy+yqcX0SUQhvocNP66ru yIJQ== X-Gm-Message-State: AOAM530KVxDFJncXqMpxoIdyVU9ZtZaRQV/pPtZJmiRmx7bbPYUGxn7a dGoR7JLc1OihI9J+alV7HfaUXf1SJFF29A== X-Google-Smtp-Source: ABdhPJzmpZxRDmj86XCdL3E0iytTmKMysH7j2Y7RGk9yi7+bvdQkU90pja6rZBvS8FiomU3DzJcpvw== X-Received: by 2002:a05:6000:1881:: with SMTP id a1mr585006wri.561.1644009662112; Fri, 04 Feb 2022 13:21:02 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o21sm2321598wmh.36.2022.02.04.13.21.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 13:21:01 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v10 8/9] grep.c: do "if (bool && memchr())" not "if (memchr() && bool)" Date: Fri, 4 Feb 2022 22:20:50 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change code in compile_regexp() to check the cheaper boolean "!opt->pcre2" condition before the "memchr()" search. This doesn't noticeably optimize anything, but makes the code more obvious and conventional. The line wrapping being added here also makes a subsequent commit smaller. Signed-off-by: Ævar Arnfjörð Bjarmason --- grep.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/grep.c b/grep.c index 35e12e43c09..60228a95a4f 100644 --- a/grep.c +++ b/grep.c @@ -492,7 +492,8 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) p->ignore_case = opt->ignore_case; p->fixed = opt->fixed; - if (memchr(p->pattern, 0, p->patternlen) && !opt->pcre2) + if (!opt->pcre2 && + memchr(p->pattern, 0, p->patternlen)) die(_("given pattern contains NULL byte (via -f ). This is only supported with -P under PCRE v2")); p->is_fixed = is_fixed(p->pattern, p->patternlen); From patchwork Fri Feb 4 21:20:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12735699 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 ABBABC433EF for ; Fri, 4 Feb 2022 21:21:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242731AbiBDVVS (ORCPT ); Fri, 4 Feb 2022 16:21:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242091AbiBDVVG (ORCPT ); Fri, 4 Feb 2022 16:21:06 -0500 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 C4683C061749 for ; Fri, 4 Feb 2022 13:21:04 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id o1-20020a1c4d01000000b0034d95625e1fso10227214wmh.4 for ; Fri, 04 Feb 2022 13:21:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YXFqhDKZ4nARXyDLzxoaM3AwgASxo4W346Nta5A8Bos=; b=OdtW2wwLZKtwgIan0qu+ZPmjcN/o6J8AFVkBQq70Z78wnGRJtiuD1SAM67uXt7vfL7 f4epU7HDFNrv8VdCo+9kZ933szcLWfzXUEKn9yztfXhaSmqnhQdhiMStRnUWDsORhaEl ohAAswZGT5utU+WfjDXrHE4Xwww0n8dw1jXh1VOK0DNimomSv5d+UYPfeBF8O9zoO159 ioLxKATbs/zxJINcM8xZnIGULm/vadble2M5G80ZvKYjj0eaKdUi+hrfZzZYOxmoAeR0 cf0BbpIjvw0V+0zN2CDc8W84BjQbYxFxoPzBRCf3kBvwcVtNF9z8Ff6HLVZig2Ex37od wjdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YXFqhDKZ4nARXyDLzxoaM3AwgASxo4W346Nta5A8Bos=; b=I5P1o/WHFwFtaD756fXG8YOUu6yKbornn6Ryt9zhoUJuqDwHjy/5yQfv69R/iBHphW ZbCFICii8SVZfskBliqzq/O883dxuvOZ0RoR1FTrhVRKwuPhNccqPGM7aQjMMxpYrITq eORZhc9cQZ/DKo4yqjhiOJxHDO/RXcSyrp1IACLsD4ULWCT9p9rzObh/7mGfWz1wLTgs ejehuVtkVtBBzLLDZNSbhXv3u7TA5L002qZbOqcyCIGYxrdgfHxVkeHbMOPi8fyjCojF jzsONDFS8YUGpMSbokX/uABclfPqtr9OEVNikkO2F52J5XGiqVQSm6lrGDWBnWVePJSm Ul5w== X-Gm-Message-State: AOAM533uLbd4shYnjK6jYQqOC+uQrS/HnOQLVEFPICGFDITpmg1fy2xp c8VIBrYUEydcMkSLPn9StJU0jzam/6sKVQ== X-Google-Smtp-Source: ABdhPJy3eUTz+qxNHGHI/eQnoIQpLNLUC4auKPSPIiCMRIENLtSRb6FRHbgnGU5sdB+v4HAvzZCEZA== X-Received: by 2002:a05:600c:25a:: with SMTP id 26mr498294wmj.184.1644009663005; Fri, 04 Feb 2022 13:21:03 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o21sm2321598wmh.36.2022.02.04.13.21.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Feb 2022 13:21:02 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , J Smith , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v10 9/9] grep: simplify config parsing and option parsing Date: Fri, 4 Feb 2022 22:20:51 +0100 Message-Id: X-Mailer: git-send-email 2.35.1.940.ge7a5b4b05f2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Simplify the parsing of "grep.patternType" and "grep.extendedRegexp". This changes no behavior, but gets rid of complex parsing logic that isn't needed anymore. When "grep.patternType" was introduced in 84befcd0a4a (grep: add a grep.patternType configuration setting, 2012-08-03) we promised that: 1. You can set "grep.patternType", and "[setting it to] 'default' will return to the default matching behavior". In that context "the default" meant whatever the configuration system specified before that change, i.e. via grep.extendedRegexp. 2. We'd support the existing "grep.extendedRegexp" option, but ignore it when the new "grep.patternType" option is set. We said we'd only ignore the older "grep.extendedRegexp" option "when the `grep.patternType` option is set to a value other than 'default'". In a preceding commit we changed grep_config() to be called after grep_init(), which means that much of the complexity here can go away. As before both "grep.patternType" and "grep.extendedRegexp" are last-one-wins variable, with "grep.extendedRegexp" yielding to "grep.patternType", except when "grep.patternType=default". Note that as the previously added tests indicate this cannot be done on-the-fly as we see the config variables, without introducing more state keeping. I.e. if we see: -c grep.extendedRegexp=false -c grep.patternType=default -c extendedRegexp=true We need to select ERE, since grep.patternType=default unselects that variable, which normally has higher precedence, but we also need to select BRE in cases of: -c grep.extendedRegexp=true \ -c grep.extendedRegexp=false Which would not be the case for this, which select ERE: -c grep.patternType=extended \ -c grep.extendedRegexp=false Therefore we cannot do this on-the-fly in grep_config without also introducing tracking variables for not only the pattern type, but what the source of that pattern type was. So we need to decide on the pattern after our config was fully parsed. Let's do that by deferring the decision on the pattern type until it's time to compile it in compile_regexp(). By that time we've not only parsed the config, but also handled the command-line options. Those will set "opt.pattern_type_option" (*not* "opt.extended_regexp_option"!). At that point all we need to do is see if "grep.patternType" was UNSPECIFIED in the end (including an explicit "=default"), if so we'll use the "grep.extendedRegexp" configuration, if any. See my 07a3d411739 (grep: remove regflags from the public grep_opt API, 2017-06-29) for addition of the two comments being removed here, i.e. the complexity noted in that commit is now going away. 1. https://lore.kernel.org/git/patch-v8-09.10-c211bb0c69d-20220118T155211Z-avarab@gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/grep.c | 10 +++----- grep.c | 69 +++++++------------------------------------------- grep.h | 3 --- revision.c | 2 -- 4 files changed, 13 insertions(+), 71 deletions(-) diff --git a/builtin/grep.c b/builtin/grep.c index 0ea124321b6..942c4b25077 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -845,7 +845,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix) int i; int dummy; int use_index = 1; - int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED; int allow_revs; struct option options[] = { @@ -879,16 +878,16 @@ int cmd_grep(int argc, const char **argv, const char *prefix) N_("descend at most levels"), PARSE_OPT_NONEG, NULL, 1 }, OPT_GROUP(""), - OPT_SET_INT('E', "extended-regexp", &pattern_type_arg, + OPT_SET_INT('E', "extended-regexp", &opt.pattern_type_option, N_("use extended POSIX regular expressions"), GREP_PATTERN_TYPE_ERE), - OPT_SET_INT('G', "basic-regexp", &pattern_type_arg, + OPT_SET_INT('G', "basic-regexp", &opt.pattern_type_option, N_("use basic POSIX regular expressions (default)"), GREP_PATTERN_TYPE_BRE), - OPT_SET_INT('F', "fixed-strings", &pattern_type_arg, + OPT_SET_INT('F', "fixed-strings", &opt.pattern_type_option, N_("interpret patterns as fixed strings"), GREP_PATTERN_TYPE_FIXED), - OPT_SET_INT('P', "perl-regexp", &pattern_type_arg, + OPT_SET_INT('P', "perl-regexp", &opt.pattern_type_option, N_("use Perl-compatible regular expressions"), GREP_PATTERN_TYPE_PCRE), OPT_GROUP(""), @@ -982,7 +981,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix) argc = parse_options(argc, argv, prefix, options, grep_usage, PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_STOP_AT_NON_OPTION); - grep_commit_pattern_type(pattern_type_arg, &opt); if (use_index && !startup_info->have_repository) { int fallback = 0; diff --git a/grep.c b/grep.c index 60228a95a4f..a8f503f55c7 100644 --- a/grep.c +++ b/grep.c @@ -115,62 +115,6 @@ void grep_init(struct grep_opt *opt, struct repository *repo) opt->header_tail = &opt->header_list; } -static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt) -{ - /* - * When committing to the pattern type by setting the relevant - * fields in grep_opt it's generally not necessary to zero out - * the fields we're not choosing, since they won't have been - * set by anything. The extended_regexp_option field is the - * only exception to this. - * - * This is because in the process of parsing grep.patternType - * & grep.extendedRegexp we set opt->pattern_type_option and - * opt->extended_regexp_option, respectively. We then - * internally use opt->extended_regexp_option to see if we're - * compiling an ERE. It must be unset if that's not actually - * the case. - */ - if (pattern_type != GREP_PATTERN_TYPE_ERE && - opt->extended_regexp_option) - opt->extended_regexp_option = 0; - - switch (pattern_type) { - case GREP_PATTERN_TYPE_UNSPECIFIED: - /* fall through */ - - case GREP_PATTERN_TYPE_BRE: - break; - - case GREP_PATTERN_TYPE_ERE: - opt->extended_regexp_option = 1; - break; - - case GREP_PATTERN_TYPE_FIXED: - opt->fixed = 1; - break; - - case GREP_PATTERN_TYPE_PCRE: - opt->pcre2 = 1; - break; - } -} - -void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt) -{ - if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED) - grep_set_pattern_type_option(pattern_type, opt); - else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED) - grep_set_pattern_type_option(opt->pattern_type_option, opt); - else if (opt->extended_regexp_option) - /* - * This branch *must* happen after setting from the - * opt->pattern_type_option above, we don't want - * grep.extendedRegexp to override grep.patternType! - */ - grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt); -} - static struct grep_pat *create_grep_pat(const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t, @@ -488,11 +432,16 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) int err; int regflags = REG_NEWLINE; + if (opt->pattern_type_option == GREP_PATTERN_TYPE_UNSPECIFIED) + opt->pattern_type_option = (opt->extended_regexp_option + ? GREP_PATTERN_TYPE_ERE + : GREP_PATTERN_TYPE_BRE); + p->word_regexp = opt->word_regexp; p->ignore_case = opt->ignore_case; - p->fixed = opt->fixed; + p->fixed = opt->pattern_type_option == GREP_PATTERN_TYPE_FIXED; - if (!opt->pcre2 && + if (opt->pattern_type_option != GREP_PATTERN_TYPE_PCRE && memchr(p->pattern, 0, p->patternlen)) die(_("given pattern contains NULL byte (via -f ). This is only supported with -P under PCRE v2")); @@ -544,14 +493,14 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) return; } - if (opt->pcre2) { + if (opt->pattern_type_option == GREP_PATTERN_TYPE_PCRE) { compile_pcre2_pattern(p, opt); return; } if (p->ignore_case) regflags |= REG_ICASE; - if (opt->extended_regexp_option) + if (opt->pattern_type_option == GREP_PATTERN_TYPE_ERE) regflags |= REG_EXTENDED; err = regcomp(&p->regexp, p->pattern, regflags); if (err) { diff --git a/grep.h b/grep.h index 89a2ce51130..c722d25ed9d 100644 --- a/grep.h +++ b/grep.h @@ -143,7 +143,6 @@ struct grep_opt { int unmatch_name_only; int count; int word_regexp; - int fixed; int all_match; int no_body_match; int body_hit; @@ -154,7 +153,6 @@ struct grep_opt { int allow_textconv; int extended; int use_reflog_filter; - int pcre2; int relative; int pathname; int null_following_name; @@ -202,7 +200,6 @@ struct grep_opt { int grep_config(const char *var, const char *value, void *); void grep_init(struct grep_opt *, struct repository *repo); -void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt); void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t); void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t); diff --git a/revision.c b/revision.c index d6e0e2b23b5..dd301e30147 100644 --- a/revision.c +++ b/revision.c @@ -2860,8 +2860,6 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s diff_setup_done(&revs->diffopt); - grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED, - &revs->grep_filter); if (!is_encoding_utf8(get_log_output_encoding())) revs->grep_filter.ignore_locale = 1; compile_grep_patterns(&revs->grep_filter);