From patchwork Sat Feb 13 01:52:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hariom verma X-Patchwork-Id: 12086507 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E126AC433E6 for ; Sat, 13 Feb 2021 01:53:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A90E064EA1 for ; Sat, 13 Feb 2021 01:53:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232246AbhBMBx3 (ORCPT ); Fri, 12 Feb 2021 20:53:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229918AbhBMBx1 (ORCPT ); Fri, 12 Feb 2021 20:53:27 -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 1ABECC061756 for ; Fri, 12 Feb 2021 17:52:47 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id i9so1531685wmq.1 for ; Fri, 12 Feb 2021 17:52:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=79t9jJ+yZK63eTMvmuldDuFRk21XscqyhvqiHozR0Bk=; b=dyC5NNSDszRODFzV7rMyHUb5t+umXA+tgX3LhB4GscfcaTKz1rIS6uDmC3446SDswz TlMwKMTDYWHRI5VK0+eN/aQcKta0Gdj0RgwHWo012gydpKyNY8z4jWDTMkGnwRVS+bxI DJqHdyfP2pQgkbFvrM6ZE+Qmc9vxfxDHk3BeFkxiPhqbuj8lhnJh2zHSGmvSndxFl/Xk 1ItCPFNabK886oXHkrmZBeyfOdzzuEnvGtxq6yEEsdJgi+m1dSNQ2Ti4aOX5vNE3IAGf VlJ3dyji3HgkhsfeOfhNjTyWvkL16rAdynoWvL+2thmsApwHz8TVCIS2k5ArtTEIsxpq pWFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=79t9jJ+yZK63eTMvmuldDuFRk21XscqyhvqiHozR0Bk=; b=N7/HQsjBMLHT4GMK2H8rr4FZ/fnluGj3Ll98L9r5fU8BYgWe3LFdVRhsqYJurjL+Me LC0x9R8oZV8hXmwNL9dDhQE8uGOSHC3pzhi/V7nDaNpA5eytnigFA24GpFpuISIoSZle e/UWoYzLNcCxYpLpm4WaRUxZeGXdjYmcwEAfCbESB2cFHjrHsK/08Wp9Zf86/JUiYPQL wsPJOPOA8vA5OOuWmR8gTeKF4i0COE5NIo4V8O3SMtu4776czUG1TDZsDfKJZ3Az7zg6 WgQurmF9Y+sqqa8lgZ0viNZw7JYSewqIxy+4If7/YOLnbcWSBr3pGeuEA9iM82Efe3ia HaHA== X-Gm-Message-State: AOAM532OzTOW83xkHej6hEAKTMjJoWZVN//9/Q79lXJXqAGdnJJwQd4G Sg+wTX5lEpfn/yBe5Kn7eTXL6oyZ/OE= X-Google-Smtp-Source: ABdhPJzdjAS/cs+F8oEl8YA70Yy3KSE+sADYY7hnXKlD/NXB1Ifz3ki27VTzaeSWHPSyTAPI8uWl/w== X-Received: by 2002:a05:600c:2351:: with SMTP id 17mr4929624wmq.2.1613181165667; Fri, 12 Feb 2021 17:52:45 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id e12sm4944347wrv.59.2021.02.12.17.52.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Feb 2021 17:52:45 -0800 (PST) Message-Id: <410b02dbad20c77662dd4581f9985783ca08fc70.1613181163.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sat, 13 Feb 2021 01:52:40 +0000 Subject: [PATCH v4 1/4] t6300: use function to test trailer options Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , =?utf-8?b?w4Z2YXIgQXJu?= =?utf-8?b?ZmrDtnLDsA==?= Bjarmason , Junio C Hamano , Hariom Verma , Hariom Verma Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Hariom Verma From: Hariom Verma Add a function to test trailer options. This will make tests look cleaner, as well as will make it easier to add new tests for trailers in the future. Mentored-by: Christian Couder Mentored-by: Heba Waly Signed-off-by: Hariom Verma --- t/t6300-for-each-ref.sh | 90 +++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh index ca62e764b586..a8faddd18a9b 100755 --- a/t/t6300-for-each-ref.sh +++ b/t/t6300-for-each-ref.sh @@ -814,53 +814,57 @@ test_expect_success 'set up trailers for next test' ' EOF ' -test_expect_success '%(trailers:unfold) unfolds trailers' ' - { - unfold expect && - git for-each-ref --format="%(trailers:unfold)" refs/heads/main >actual && - test_cmp expect actual && - git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/main >actual && - test_cmp expect actual -' +test_trailer_option () { + title=$1 option=$2 + cat >expect + test_expect_success "$title" ' + git for-each-ref --format="%($option)" refs/heads/main >actual && + test_cmp expect actual && + git for-each-ref --format="%(contents:$option)" refs/heads/main >actual && + test_cmp expect actual + ' +} -test_expect_success '%(trailers:only) shows only "key: value" trailers' ' - { - grep -v patch.description expect && - git for-each-ref --format="%(trailers:only)" refs/heads/main >actual && - test_cmp expect actual && - git for-each-ref --format="%(contents:trailers:only)" refs/heads/main >actual && - test_cmp expect actual -' +test_trailer_option '%(trailers:unfold) unfolds trailers' \ + 'trailers:unfold' <<-EOF + $(unfold expect && - git for-each-ref --format="%(trailers:only,unfold)" refs/heads/main >actual && - test_cmp expect actual && - git for-each-ref --format="%(trailers:unfold,only)" refs/heads/main >actual && - test_cmp actual actual && - git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/main >actual && - test_cmp expect actual && - git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/main >actual && - test_cmp actual actual -' - -test_expect_success '%(trailers) rejects unknown trailers arguments' ' - # error message cannot be checked under i18n - cat >expect <<-EOF && + EOF + +test_trailer_option '%(trailers:only) shows only "key: value" trailers' \ + 'trailers:only' <<-EOF + $(grep -v patch.description expect + test_expect_success "$title" ' + # error message cannot be checked under i18n + test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual && + test_i18ncmp expect actual && + test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual && + test_i18ncmp expect actual + ' +} + +test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \ + 'trailers:unsupported' <<-\EOF fatal: unknown %(trailers) argument: unsupported EOF - test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual && - test_i18ncmp expect actual && - test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual && - test_i18ncmp expect actual -' test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' ' cat >expect <<-EOF && From patchwork Sat Feb 13 01:52:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hariom verma X-Patchwork-Id: 12086511 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0F05DC433E9 for ; Sat, 13 Feb 2021 01:53:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C3CA064EAC for ; Sat, 13 Feb 2021 01:53:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232273AbhBMBxj (ORCPT ); Fri, 12 Feb 2021 20:53:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231894AbhBMBx1 (ORCPT ); Fri, 12 Feb 2021 20:53:27 -0500 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 833AAC0613D6 for ; Fri, 12 Feb 2021 17:52:47 -0800 (PST) Received: by mail-wr1-x42a.google.com with SMTP id 7so1615873wrz.0 for ; Fri, 12 Feb 2021 17:52:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=fsQBiIZcQH8JGGQguYV0c/NEq5ZAJTXvRc+DDzxgLh8=; b=KhPFCtjXWqhakLGOVObGzKPUaPIngLzhMjMma6+RhgjqBu65mNAp+cjRxBdgVYkzX+ 1SHChaNBCLmABnlfpFSNRqKt+cdLD0wNgtDOmCB2kYYeXLVGYezOmLqZ/4RkZxKEgDAQ D8Lwc0Bo0yS8fsy/qGV2NiZT69Bol1taysGraXvtWeJr1/BP4Gqxp91C4TycN6inr5mT R/EOxIRYxBh6PZqXqwSeJgbeTpxVK3KIloX6Ibeyo9vF2awTV19VwarKZh55IdZYkg3c xXbW8Veqskuf2qxMRtlKYAfzTvBc3E5sjziP4Qmga7jBecYri0XW1HSafxPYvEn8QUWt C3zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=fsQBiIZcQH8JGGQguYV0c/NEq5ZAJTXvRc+DDzxgLh8=; b=JLa/7KUSR4eV/CTEOE5+E+7t8+5GFsAmszuurV3bJlqsFxs1KAGQcH5c2RJdcgXSlI 8Yb0CpyZVYXf6J54kAUp0Swlzgt9KYBXSzGwO7QAZvgT/5bhOnsIHt6cJ+nS1VJ+oSY5 E7XGBu7nscyMlWJQ2DaSZD+oEpfURkFgjw9OvetomflKouh5/cLtKwE3GXvj0V0+oGE2 arfm2NCCZFi0FMXZH0SPu9zzS/3/3TpgJjSJtBml18jzJWs4fxmIqFFPSORmn/1dZry7 sgyhvpmsLhl2oD24y6wdQK0v9KIUNxxMii+L47edwhlqhc2uc0GjDhPQIgCm6rRo4tF7 3Q/w== X-Gm-Message-State: AOAM531bXc1AeZGl1OEtUEoxNDT/TTc52yvTPHq0D4QSHcnEP4Q/TCGz jUC/yM51tpGDLYbecRKMwJWOYZhNLgo= X-Google-Smtp-Source: ABdhPJz4+X5o40WhKXqwJALG9c3vGWce86jScZjDeD7r03vZAGAtwgKI56XfuRMdFfuanbF0vCzjmQ== X-Received: by 2002:adf:cd0b:: with SMTP id w11mr6217438wrm.327.1613181166356; Fri, 12 Feb 2021 17:52:46 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id k1sm6891460wrx.77.2021.02.12.17.52.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Feb 2021 17:52:46 -0800 (PST) Message-Id: In-Reply-To: References: Date: Sat, 13 Feb 2021 01:52:41 +0000 Subject: [PATCH v4 2/4] pretty.c: refactor trailer logic to `format_set_trailers_options()` Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , =?utf-8?b?w4Z2YXIgQXJu?= =?utf-8?b?ZmrDtnLDsA==?= Bjarmason , Junio C Hamano , Hariom Verma , Hariom Verma Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Hariom Verma From: Hariom Verma Refactored trailers formatting logic inside pretty.c to a new function `format_set_trailers_options()`. This new function returns the non-zero in case of unusual. The caller handles the non-zero by "goto trailers_out". This change will allow us to reuse the same logic in other places. Mentored-by: Christian Couder Mentored-by: Heba Waly Signed-off-by: Hariom Verma --- pretty.c | 89 +++++++++++++++++++++++++++++++------------------------- pretty.h | 11 +++++++ 2 files changed, 61 insertions(+), 39 deletions(-) diff --git a/pretty.c b/pretty.c index b4ff3f602f9b..304b73068bc4 100644 --- a/pretty.c +++ b/pretty.c @@ -1149,6 +1149,54 @@ static int format_trailer_match_cb(const struct strbuf *key, void *ud) return 0; } +int format_set_trailers_options(struct process_trailer_options *opts, + struct string_list *filter_list, + struct strbuf *sepbuf, + struct strbuf *kvsepbuf, + const char **arg) +{ + for (;;) { + const char *argval; + size_t arglen; + + if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) { + uintptr_t len = arglen; + + if (!argval) + return -1; + + if (len && argval[len - 1] == ':') + len--; + string_list_append(filter_list, argval)->util = (char *)len; + + opts->filter = format_trailer_match_cb; + opts->filter_data = filter_list; + opts->only_trailers = 1; + } else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) { + char *fmt; + + strbuf_reset(sepbuf); + fmt = xstrndup(argval, arglen); + strbuf_expand(sepbuf, fmt, strbuf_expand_literal_cb, NULL); + free(fmt); + opts->separator = sepbuf; + } else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) { + char *fmt; + + strbuf_reset(kvsepbuf); + fmt = xstrndup(argval, arglen); + strbuf_expand(kvsepbuf, fmt, strbuf_expand_literal_cb, NULL); + free(fmt); + opts->key_value_separator = kvsepbuf; + } else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) && + !match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) && + !match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) && + !match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) + break; + } + return 0; +} + static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */ const char *placeholder, void *context) @@ -1429,45 +1477,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */ if (*arg == ':') { arg++; - for (;;) { - const char *argval; - size_t arglen; - - if (match_placeholder_arg_value(arg, "key", &arg, &argval, &arglen)) { - uintptr_t len = arglen; - - if (!argval) - goto trailer_out; - - if (len && argval[len - 1] == ':') - len--; - string_list_append(&filter_list, argval)->util = (char *)len; - - opts.filter = format_trailer_match_cb; - opts.filter_data = &filter_list; - opts.only_trailers = 1; - } else if (match_placeholder_arg_value(arg, "separator", &arg, &argval, &arglen)) { - char *fmt; - - strbuf_reset(&sepbuf); - fmt = xstrndup(argval, arglen); - strbuf_expand(&sepbuf, fmt, strbuf_expand_literal_cb, NULL); - free(fmt); - opts.separator = &sepbuf; - } else if (match_placeholder_arg_value(arg, "key_value_separator", &arg, &argval, &arglen)) { - char *fmt; - - strbuf_reset(&kvsepbuf); - fmt = xstrndup(argval, arglen); - strbuf_expand(&kvsepbuf, fmt, strbuf_expand_literal_cb, NULL); - free(fmt); - opts.key_value_separator = &kvsepbuf; - } else if (!match_placeholder_bool_arg(arg, "only", &arg, &opts.only_trailers) && - !match_placeholder_bool_arg(arg, "unfold", &arg, &opts.unfold) && - !match_placeholder_bool_arg(arg, "keyonly", &arg, &opts.key_only) && - !match_placeholder_bool_arg(arg, "valueonly", &arg, &opts.value_only)) - break; - } + if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg)) + goto trailer_out; } if (*arg == ')') { format_trailers_from_commit(sb, msg + c->subject_off, &opts); diff --git a/pretty.h b/pretty.h index 7ce6c0b437b4..7369cf7e1484 100644 --- a/pretty.h +++ b/pretty.h @@ -6,6 +6,7 @@ struct commit; struct strbuf; +struct process_trailer_options; /* Commit formats */ enum cmit_fmt { @@ -142,4 +143,14 @@ int commit_format_is_empty(enum cmit_fmt); /* Make subject of commit message suitable for filename */ void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len); +/* + * Set values of fields in "struct process_trailer_options" + * according to trailers arguments. + */ +int format_set_trailers_options(struct process_trailer_options *opts, + struct string_list *filter_list, + struct strbuf *sepbuf, + struct strbuf *kvsepbuf, + const char **arg); + #endif /* PRETTY_H */ From patchwork Sat Feb 13 01:52:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hariom verma X-Patchwork-Id: 12086513 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A8CDC433DB for ; Sat, 13 Feb 2021 01:53:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E28FD64DE9 for ; Sat, 13 Feb 2021 01:53:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232280AbhBMBxk (ORCPT ); Fri, 12 Feb 2021 20:53:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232235AbhBMBx2 (ORCPT ); Fri, 12 Feb 2021 20:53:28 -0500 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66FD0C061786 for ; Fri, 12 Feb 2021 17:52:48 -0800 (PST) Received: by mail-wm1-x331.google.com with SMTP id m1so1528385wml.2 for ; Fri, 12 Feb 2021 17:52:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=Ff2AqqL3y6GxGc/eNdx+JLDjZ8NyNQopnEjHfkTd+7w=; b=Q9CJS32KO/wgiZ7MhNXrr3nX/DFQFz42XcI1sp0Q8tzH1nbKcocDebzLI11OgECSxn A12UCnrcLXzXQZcQs3BCd/cHrT7WoeEa/j4jIX7ruujpurTwdP7mj6YbLu/8tXuvrZjq sfw/Giz42bh0qKYWU9Tby/EI/OWfp45K3hgFK7E067Df8xuSFbcqnZ8BbguBjIjeD+n6 6c9D8THa7ahbN7l85D/DFHg2uhFZV42gUcaTXXwG8NijJZVhAELfBh3TzpoW7Sqvo/JE F1HVzH7QmYRxLrDc8OE+ES/iRH74Qv+oyK8X5yejmw/Wrv6Sl2JBHstXU+4AsiRH7ovU Jguw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=Ff2AqqL3y6GxGc/eNdx+JLDjZ8NyNQopnEjHfkTd+7w=; b=kCtq/WNy/gm967OPoi9PWu+IbY11zSK4p9SBeeutallrt1OviCQnCpq3hXqN+BHQkF +ztS/gKeBQPuObaMSdHvDKMjAmwYbsJUzn4KulO1c0awD3QiRKKe6pwl3TRZKS+D2LKi OZa8MPbEkUvwt7dzq5uqDULfqshG8Xvxf6vc1eOjqkNDvhlvXz1YGVfQLV3857HhX3IK tY+S7ZYCbW669ugBkm/rM0QVBjLLn8lF4+WBl/8mD/Al7LakKTozZ6QgHtk707p5k+jY b3GDF7seGxAISinxupBySufbf+/2CVjmGsgm4spHXBz2unIgTXSAnB8eNqecWjbB00Cm oKWw== X-Gm-Message-State: AOAM5334w9A9J05flLEilaJOMoNgUrpb1ntsV4LiVRrQSX1kMk6PLGGb Bna8Cfc3djqslsjIPZFSfX5VOZ1SzCg= X-Google-Smtp-Source: ABdhPJwEadeKcV8ounGMcUUqJGHo+sAPGFEj+6Q7eArb2NxN6rN3b0g4MbdejWA/3kjbeAeffZ3GCw== X-Received: by 2002:a1c:b7d7:: with SMTP id h206mr4991526wmf.64.1613181167195; Fri, 12 Feb 2021 17:52:47 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m5sm13604092wmc.25.2021.02.12.17.52.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Feb 2021 17:52:46 -0800 (PST) Message-Id: <073c75dc4494db3074e426a751595ea83467fece.1613181163.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sat, 13 Feb 2021 01:52:42 +0000 Subject: [PATCH v4 3/4] pretty.c: capture invalid trailer argument Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , =?utf-8?b?w4Z2YXIgQXJu?= =?utf-8?b?ZmrDtnLDsA==?= Bjarmason , Junio C Hamano , Hariom Verma , Hariom Verma Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Hariom Verma From: Hariom Verma As we would like to use this trailers logic in the ref-filter, it's nice to get an invalid trailer argument. This will allow us to print precise error message while using `format_set_trailers_options()` in ref-filter. For capturing the invalid argument, we changed the working of `format_set_trailers_options()` a little bit. Original logic does "break" and fell through in mainly 2 cases - 1. unknown/invalid argument 2. end of the arg string But now instead of "break", we capture invalid argument and return non-zero. And non-zero is handled by the caller. (We prepared the caller to handle non-zero in the previous commit). Capturing invalid arguments this way will also affects the working of current logic. As at the end of the arg string it will return non-zero. So in order to make things correct, introduced an additional conditional statement i.e if encounter ")", do 'break'. Mentored-by: Christian Couder Mentored-by: Heba Waly Signed-off-by: Hariom Verma --- pretty.c | 17 +++++++++++++---- pretty.h | 3 ++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pretty.c b/pretty.c index 304b73068bc4..c5f5ecc40d3f 100644 --- a/pretty.c +++ b/pretty.c @@ -1153,12 +1153,16 @@ int format_set_trailers_options(struct process_trailer_options *opts, struct string_list *filter_list, struct strbuf *sepbuf, struct strbuf *kvsepbuf, - const char **arg) + const char **arg, + char **invalid_arg) { for (;;) { const char *argval; size_t arglen; + if (**arg == ')') + break; + if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) { uintptr_t len = arglen; @@ -1191,8 +1195,13 @@ int format_set_trailers_options(struct process_trailer_options *opts, } else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) && !match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) && !match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) && - !match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) - break; + !match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) { + if (invalid_arg) { + size_t len = strcspn(*arg, ",)"); + *invalid_arg = xstrndup(*arg, len); + } + return -1; + } } return 0; } @@ -1477,7 +1486,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */ if (*arg == ':') { arg++; - if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg)) + if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg, NULL)) goto trailer_out; } if (*arg == ')') { diff --git a/pretty.h b/pretty.h index 7369cf7e1484..d902cdd70a95 100644 --- a/pretty.h +++ b/pretty.h @@ -151,6 +151,7 @@ int format_set_trailers_options(struct process_trailer_options *opts, struct string_list *filter_list, struct strbuf *sepbuf, struct strbuf *kvsepbuf, - const char **arg); + const char **arg, + char **invalid_arg); #endif /* PRETTY_H */ From patchwork Sat Feb 13 01:52:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hariom verma X-Patchwork-Id: 12086515 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3EF22C43381 for ; Sat, 13 Feb 2021 01:53:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0F58764E8E for ; Sat, 13 Feb 2021 01:53:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232287AbhBMBxn (ORCPT ); Fri, 12 Feb 2021 20:53:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232256AbhBMBx3 (ORCPT ); Fri, 12 Feb 2021 20:53:29 -0500 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 2989DC061788 for ; Fri, 12 Feb 2021 17:52:49 -0800 (PST) Received: by mail-wm1-x335.google.com with SMTP id o24so1500082wmh.5 for ; Fri, 12 Feb 2021 17:52:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=Hai9aTv6kH+slevzrTwkB5WpAOfiscriXAwCQcaluGU=; b=cQnjAd4AW25YVZE7jhjyh588PrlL9p4GvMdB5hr5uJX2FQ+mncjsg25KryTSd5nm+0 +AsuzWyo+yrLlFhwmJmCGbUMC6aVgOxYxER7BNpSDtflulfQbjvGJkM3yWFixG3e0A/C YIUFJGpsNI+Vax0jHf+oSslXE7Wxw3ctJwgrRQhwtDcmCu5gmImaBPvdB+r2OFZSPkbM RKzrP5B6ldku5MIvIwd5nisvLqLNKhRpL3z2zz5MbDjUVCGk6jXOeOXm55wcT/maC2qd vZcRT/rMyYiv3xJM6LWSbyCCt853bQpi/bqa15qKcb4KCCha3tVFyEYiIEKjoCFJW6R9 MWVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=Hai9aTv6kH+slevzrTwkB5WpAOfiscriXAwCQcaluGU=; b=JpZynyWI7h3M1qvNQx+bmNCWnMwDtTZ5ui4/C7GatKwf2okA5+GCTSz6BV+ABb/1Nz lZ/jqZj9ACJUghnf39VwYwqDYK0mEus5unAsWAJkxNrkOj7WrgZNOdM8AqFNDK0iDLPR qIuNgXNAGfr0fOpdc/JbP6y6kYsZlPrkPxgrSjc30y6BvW2HBDrfmqgcF1ZcSOfJzzav kBV1BPxI3a8zgDhZ1bsuLJ0wOWTmPzX9Pr0nH3I07pKB7jrvrMJ5bXL1XmK1c4D5Vcos xshq/2YgVHPeh2EYt6DF93S8Uo0pd5859/U1cnc8Ld1BxgmxuSIJ14ZUQOWTQgqVG9wI 5qfw== X-Gm-Message-State: AOAM5301droNVIV6lp1dXjpYjFFHQ5tTFFPu/TzkppVc8eL21/oiIOFe JkMBmrU4Dq9b614jVet2VdfeR+CnBLM= X-Google-Smtp-Source: ABdhPJxLnmkce5li9u7Y9G/h5wZYFqyxWNKxaLGf9zjAO6ZIFSTcQlzr7Q5RWiNicVoErpO8ztMn5A== X-Received: by 2002:a1c:7e85:: with SMTP id z127mr4939269wmc.131.1613181167922; Fri, 12 Feb 2021 17:52:47 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c22sm14839479wrb.91.2021.02.12.17.52.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Feb 2021 17:52:47 -0800 (PST) Message-Id: <9ec98917699382c2a43b29e1c552d3685f63f29a.1613181163.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sat, 13 Feb 2021 01:52:43 +0000 Subject: [PATCH v4 4/4] ref-filter: use pretty.c logic for trailers Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , =?utf-8?b?w4Z2YXIgQXJu?= =?utf-8?b?ZmrDtnLDsA==?= Bjarmason , Junio C Hamano , Hariom Verma , Hariom Verma Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Hariom Verma From: Hariom Verma Now, ref-filter is using pretty.c logic for setting trailer options. New to ref-filter: :key= - only show trailers with specified key. :valueonly[=val] - only show the value part. :separator= - inserted between trailer lines. :key_value_separator= - inserted between key and value in trailer lines Enhancement to existing options(now can take value and its optional): :only[=val] :unfold[=val] 'val' can be: true, on, yes or false, off, no. Mentored-by: Christian Couder Mentored-by: Heba Waly Signed-off-by: Hariom Verma --- Documentation/git-for-each-ref.txt | 8 +-- ref-filter.c | 36 ++++++----- t/t6300-for-each-ref.sh | 95 ++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 21 deletions(-) diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt index 2962f85a502a..2ae2478de706 100644 --- a/Documentation/git-for-each-ref.txt +++ b/Documentation/git-for-each-ref.txt @@ -260,11 +260,9 @@ contents:lines=N:: The first `N` lines of the message. Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1] -are obtained as `trailers` (or by using the historical alias -`contents:trailers`). Non-trailer lines from the trailer block can be omitted -with `trailers:only`. Whitespace-continuations can be removed from trailers so -that each trailer appears on a line by itself with its full content with -`trailers:unfold`. Both can be used together as `trailers:unfold,only`. +are obtained as `trailers[:options]` (or by using the historical alias +`contents:trailers[:options]`). For valid [:option] values see `trailers` +section of linkgit:git-log[1]. For sorting purposes, fields with numeric values sort in numeric order (`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`). diff --git a/ref-filter.c b/ref-filter.c index fd994e18744c..5224037d3da4 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -67,6 +67,12 @@ struct refname_atom { int lstrip, rstrip; }; +static struct ref_trailer_buf { + struct string_list filter_list; + struct strbuf sepbuf; + struct strbuf kvsepbuf; +} ref_trailer_buf = {STRING_LIST_INIT_NODUP, STRBUF_INIT, STRBUF_INIT}; + static struct expand_data { struct object_id oid; enum object_type type; @@ -313,28 +319,26 @@ static int subject_atom_parser(const struct ref_format *format, struct used_atom static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg, struct strbuf *err) { - struct string_list params = STRING_LIST_INIT_DUP; - int i; - atom->u.contents.trailer_opts.no_divider = 1; if (arg) { - string_list_split(¶ms, arg, ',', -1); - for (i = 0; i < params.nr; i++) { - const char *s = params.items[i].string; - if (!strcmp(s, "unfold")) - atom->u.contents.trailer_opts.unfold = 1; - else if (!strcmp(s, "only")) - atom->u.contents.trailer_opts.only_trailers = 1; - else { - strbuf_addf(err, _("unknown %%(trailers) argument: %s"), s); - string_list_clear(¶ms, 0); - return -1; - } + const char *argbuf = xstrfmt("%s)", arg); + char *invalid_arg = NULL; + + if (format_set_trailers_options(&atom->u.contents.trailer_opts, + &ref_trailer_buf.filter_list, + &ref_trailer_buf.sepbuf, + &ref_trailer_buf.kvsepbuf, + &argbuf, &invalid_arg)) { + if (!invalid_arg) + strbuf_addf(err, _("expected %%(trailers:key=)")); + else + strbuf_addf(err, _("unknown %%(trailers) argument: %s"), invalid_arg); + free((char *)invalid_arg); + return -1; } } atom->u.contents.option = C_TRAILERS; - string_list_clear(¶ms, 0); return 0; } diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh index a8faddd18a9b..cac7f443d004 100755 --- a/t/t6300-for-each-ref.sh +++ b/t/t6300-for-each-ref.sh @@ -837,6 +837,24 @@ test_trailer_option '%(trailers:only) shows only "key: value" trailers' \ EOF +test_trailer_option '%(trailers:only=no,only=true) shows only "key: value" trailers' \ + 'trailers:only=no,only=true' <<-EOF + $(grep -v patch.description + + EOF + +test_trailer_option '%(trailers:key=foo) is case insensitive' \ + 'trailers:key=SiGned-oFf-bY' <<-EOF + Signed-off-by: A U Thor + + EOF + +test_trailer_option '%(trailers:key=foo:) trailing colon also works' \ + 'trailers:key=Signed-off-by:' <<-EOF + Signed-off-by: A U Thor + + EOF + +test_trailer_option '%(trailers:key=foo) multiple keys' \ + 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF + Reviewed-by: A U Thor + Signed-off-by: A U Thor + + EOF + +test_trailer_option '%(trailers:key=nonexistent) becomes empty' \ + 'trailers:key=Shined-off-by:' <<-EOF + + EOF + +test_trailer_option '%(trailers:key=foo) handles multiple lines even if folded' \ + 'trailers:key=Acked-by' <<-EOF + $(grep -v patch.description + $(grep patch.description + + EOF + +test_trailer_option '%(trailers:separator) changes separator' \ + 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF + Reviewed-by: A U Thor ,Signed-off-by: A U Thor + EOF + +test_trailer_option '%(trailers:key_value_separator) changes key-value separator' \ + 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF + Reviewed-by,A U Thor + Signed-off-by,A U Thor + + EOF + +test_trailer_option '%(trailers:separator,key_value_separator) changes both separators' \ + 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF + Reviewed-by,A U Thor ,Signed-off-by,A U Thor + EOF + test_failing_trailer_option () { title=$1 option=$2 cat >expect @@ -866,6 +956,11 @@ test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \ fatal: unknown %(trailers) argument: unsupported EOF +test_failing_trailer_option '%(trailers:key) without value is error' \ + 'trailers:key' <<-\EOF + fatal: expected %(trailers:key=) + EOF + test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' ' cat >expect <<-EOF && fatal: unrecognized %(contents) argument: trailersonly