From patchwork Sat Feb 6 09:15: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: 12072049 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,URIBL_BLOCKED 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 74FAFC433E0 for ; Sat, 6 Feb 2021 09:17:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3771C64E24 for ; Sat, 6 Feb 2021 09:17:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229609AbhBFJQn (ORCPT ); Sat, 6 Feb 2021 04:16:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229566AbhBFJQ3 (ORCPT ); Sat, 6 Feb 2021 04:16:29 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DFFDC061756 for ; Sat, 6 Feb 2021 01:15:49 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id m13so10431378wro.12 for ; Sat, 06 Feb 2021 01:15: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=LwrB6uZOwo1xu8HwwI+9mKXDoOZa/32TzwWJ0WpgVuo=; b=r3rM264OF/XBwX591v62DHWkRYrH7BjIGo1P5zNmaaXXnSpGgGEH0I1TqpQ8NULORI H0SLMlqWi8PNUFIb7vAVoKzQo0z9DUjh92gIIayqvqJVI8LTnQd+el1QLSMel9zOZM0P Sp4YGVWTXe4F+esTcVQURAICezwFIRegZ7E+kQGVE614kNhYKeXvmPtIXXpQJhDzA2nv NuhYfsyHDxEP1xRCVsY4Ug2nHbA88HHeTjqsCHebIeI2twtVcr1ck10Zys+BHF1rE5n0 BxL2VtntrivOmU/nxkl5AwlGo7YZJYfKOZk6pImmNEebdtMTMFBU5uGQOWb3/xhWgzz2 Tbnw== 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=LwrB6uZOwo1xu8HwwI+9mKXDoOZa/32TzwWJ0WpgVuo=; b=lxMBA77/5kjlOIw6ZWQoVeirK0pDeRQKt1rp+Q0lBsEWPBGE1xGf2CLoSYwvq5gToR iF8Ivx15RpIi9Cn51MlXGE/IS0s9Ye8Z6C0dWyrl5iTk/SlP2YUrQ/kGmnnaZDyu0SgJ 68XNO3UBa/DSHrgmuC8QHnW1MOTYLIfSBzdsqH7iWHE8Zlm1iuhV/OAVQP/aPKoukw5q 857Fj1kLOt2R5FoKujEzbg0QmXVWsauWBhOrcrgM0phHjdl/C0nJ2uDW4FdFJ7HlFWzZ GhnBvg4Jp7Rvnjhx+WEONxJhk0CR0q3Vwe8M8HT3ywMaoEkraY0iXue1jyuSyItwW3wn G+0Q== X-Gm-Message-State: AOAM53343cCfmS/0Wh2O8efaanesE+oCAAjLr5JK/MIR7metNqcJjMau d4nhAVdk7e+L/oSUstAKQMHbSTlIwLA= X-Google-Smtp-Source: ABdhPJzR9+TisRPAxVNGSeuIwqpX0bIWa0p1aLkS5+6gq2OBbYLqC0pJla/M1516HQE+3nHGPxbaFg== X-Received: by 2002:a5d:4bc2:: with SMTP id l2mr9528958wrt.204.1612602947960; Sat, 06 Feb 2021 01:15:47 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id e10sm14824001wro.65.2021.02.06.01.15.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Feb 2021 01:15:47 -0800 (PST) Message-Id: <81030f00b11b39d7fa665c71d3bbe61203e84b54.1612602945.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sat, 06 Feb 2021 09:15:42 +0000 Subject: [PATCH v3 1/3] 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 3922f6f9f249..59cefdddf674 100644 --- a/pretty.c +++ b/pretty.c @@ -1148,6 +1148,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) @@ -1425,45 +1473,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 6 09:15: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: 12072051 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,URIBL_BLOCKED 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 A209FC433E6 for ; Sat, 6 Feb 2021 09:17:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6DB2A64FAE for ; Sat, 6 Feb 2021 09:17:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229613AbhBFJQq (ORCPT ); Sat, 6 Feb 2021 04:16:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229570AbhBFJQa (ORCPT ); Sat, 6 Feb 2021 04:16:30 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CEE6C061786 for ; Sat, 6 Feb 2021 01:15:50 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id c12so10461244wrc.7 for ; Sat, 06 Feb 2021 01:15:50 -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=IlKi+lddo3zJGdm0gOZSgHyoO2vFDvbFhYZtEC/xwQs=; b=VHU6XlGNLK8LLszAXMlClCqEmUTbyLKA6BhHEpACwppP6pbPj6SVwlI9AYBeV/Auj8 zg0OrHuCBKDgbEjYQUdLOQ/fSTBYiJXaDZc2+NzQ2l9LDECppELfxTfSFiFUxTvzhnR5 V3EVmCjBky6S2vwwy61DQ/ej93Wwh0m+fPhinzSJgJ0SgsMLUmg4IAKb7CDVAiRN5DTK aVcqDWStiNaKJYz6TmjZL20SYnyNM+dn5aVfzVNzE9Bdo2NGGR+eplPRYZJBwoQEn3HU Qm+qB+pt4hc2ENNxg6u7T7QKhfgtvF71nwzc5rZIAxBSb2yNexdBKGkzZi+giVUUkLZX dA0A== 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=IlKi+lddo3zJGdm0gOZSgHyoO2vFDvbFhYZtEC/xwQs=; b=nBrJYWoL3rWJJnxVQshUMaSbyGZiH0XjlC2lh/nObev/fJo7Og+9z6FiaCP+Hgskoz QYHaCcw4aNSlcF/micF4VhfkqTUzOzWQmPSesUZv7xAH8C+ppKM9JlMIkRdk7cvAImsZ ZKJCIrKED2HDUZkKsQvvFfbhNx5MBBHHt70S9U7QofGlJBvDDO6x1vPxi5dt2TWxFTpu C9jm7EwDbpEFHHTpN+LCfU95DCupH56WebfTdvrmj91dKIiS8enO8n9fQxwEjYWv1noC IoApguhdunzwez+o0RS6lwJeyjN5UsXNgJssw5LNms5evKkaZVEDk5xofQYf8B3iD7rZ MsIw== X-Gm-Message-State: AOAM532y97JVu59eI5Dd6LImL0WbdoF69ZyCv57TkGmfb/Tde/UblhsC Iik7c6GzCbLqpww3pkhE/oWmkyi5lWI= X-Google-Smtp-Source: ABdhPJw2jzfOUiYr8fAESLgQSYf79xQBehNPiNtCj/pTZtE6TYebGaCpBCTetsYsufFHelNRK8yE1Q== X-Received: by 2002:adf:ce10:: with SMTP id p16mr9569297wrn.97.1612602948988; Sat, 06 Feb 2021 01:15:48 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id r12sm9640018wmg.44.2021.02.06.01.15.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Feb 2021 01:15:48 -0800 (PST) Message-Id: In-Reply-To: References: Date: Sat, 06 Feb 2021 09:15:43 +0000 Subject: [PATCH v3 2/3] 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 59cefdddf674..ed16b32df922 100644 --- a/pretty.c +++ b/pretty.c @@ -1152,12 +1152,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; @@ -1190,8 +1194,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; } @@ -1473,7 +1482,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 6 09:15:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hariom verma X-Patchwork-Id: 12072053 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,URIBL_BLOCKED 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 7C329C433DB for ; Sat, 6 Feb 2021 09:17:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 52CEC64FAD for ; Sat, 6 Feb 2021 09:17:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229615AbhBFJQw (ORCPT ); Sat, 6 Feb 2021 04:16:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229572AbhBFJQc (ORCPT ); Sat, 6 Feb 2021 04:16:32 -0500 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCB18C061788 for ; Sat, 6 Feb 2021 01:15:51 -0800 (PST) Received: by mail-wr1-x42b.google.com with SMTP id 7so10547519wrz.0 for ; Sat, 06 Feb 2021 01:15:51 -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=qhcNMb+THj0XqC35PkuB9B/JBzdE/jH5KhZ2Xa2snrE=; b=o/EyGptYKZON/ArNmbvJ5aWtehmqUeqhM6Uuik2dxx2QZiLF52cg9uVZ8iCuNgpaMN 3v3K1+9IyF/20YYZnGckt5v/knVqu4bNo40+ae9f929Ri9hz3e1JsSzVjzfzdEi3qGnK UHU/revZSyfqlwcAkjQw6SQZhUBCcofpT5EO3DAgik4/hDJk7jC0dspeTkttrdsZ+swB zIElcyVH2NEO3s38gWHyVlKWhbTw57vuePUAjJrC6xkEIRJp2LQTOTNfKxRhOgvx6iUG lqbDkYPqY+sbrzz7y8r1Kt98+y8J9Ap9G22oyMkC+4GaQEcmwUQiGOrNuwPxPDsvj/df fXVA== 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=qhcNMb+THj0XqC35PkuB9B/JBzdE/jH5KhZ2Xa2snrE=; b=JZRJoCQi1fiidGghCtPb7rq8wG2Gmr/Bs+L66XA/vRhHq0oGPhoLkJ1QsQtR+t0bsW Jmx3WuagF3NNgLinnLEM2GjlKPQd9JrTEP/l0z+f9L/gD5PgYVqTg/FmNr3b1brGKQvV 0E0Rs1pgWkCSJSH0t2Ws+Oi+HoskRKnflS38LmpGNGKnf6oerQPc/j3GcOB4uLkdKHRo eMo8YKD8rD3Quvl1Htf8it5/SDXf3T+HQKJKs/tqBZAV1FpYfIlYqpR+2cIOrWgLyDRy VipEl/v2U45Tg8Hz1zfOwtCPNc3BgTKeVk6NgamiEocHJVXY5petHtXo6OBgupJMlh8P YbHw== X-Gm-Message-State: AOAM532CVy6MG5I8VF8UWyu08siAGEUdDvZ1Q6lPMjCr/qAoglWCcWCD /gmvY36dZkvqNQaHna27bAIwZnyZXt0= X-Google-Smtp-Source: ABdhPJy14IlCri28XeiJWh/cS//ur4iaxH8Io7gY1kXfCx0FMdUEYbaGiNEXcY48UthERk81q6RReA== X-Received: by 2002:a05:6000:104:: with SMTP id o4mr9295729wrx.419.1612602950245; Sat, 06 Feb 2021 01:15:50 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w25sm11196159wmc.42.2021.02.06.01.15.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Feb 2021 01:15:49 -0800 (PST) Message-Id: <47d89f872314cad6dc6010ff3c8ade43a70bc540.1612602945.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sat, 06 Feb 2021 09:15:44 +0000 Subject: [PATCH v3 3/3] 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 | 119 +++++++++++++++++++++++++---- 3 files changed, 129 insertions(+), 34 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 ee337df232a5..4dc4882cc768 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 ca62e764b586..4b3745839c86 100755 --- a/t/t6300-for-each-ref.sh +++ b/t/t6300-for-each-ref.sh @@ -825,14 +825,32 @@ test_expect_success '%(trailers:unfold) unfolds trailers' ' test_cmp expect actual ' -test_expect_success '%(trailers:only) shows only "key: value" trailers' ' +test_show_key_value_trailers () { + option="$1" + test_expect_success "%($option) shows only 'key: value' trailers" ' + { + grep -v patch.description expect && + 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_show_key_value_trailers 'trailers:only' +test_show_key_value_trailers 'trailers:only=no,only=true' +test_show_key_value_trailers 'trailers:only=yes' + +test_expect_success '%(trailers:only=no) shows all trailers' ' { - grep -v patch.description expect && - git for-each-ref --format="%(trailers:only)" refs/heads/main >actual && + git for-each-ref --format="%(trailers:only=no)" refs/heads/main >actual && test_cmp expect actual && - git for-each-ref --format="%(contents:trailers:only)" refs/heads/main >actual && + git for-each-ref --format="%(contents:trailers:only=no)" refs/heads/main >actual && test_cmp expect actual ' @@ -851,17 +869,92 @@ test_expect_success '%(trailers:only) and %(trailers:unfold) work together' ' test_cmp actual actual ' -test_expect_success '%(trailers) rejects unknown trailers arguments' ' - # error message cannot be checked under i18n - cat >expect <<-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_trailer_option() { + title="$1" + option="$2" + expect="$3" + test_expect_success "$title" ' + printf "$expect\n" >expect && + 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_trailer_option '%(trailers:key=foo) shows that trailer' \ + 'trailers:key=Signed-off-by' 'Signed-off-by: A U Thor \n' +test_trailer_option '%(trailers:key=foo) is case insensitive' \ + 'trailers:key=SiGned-oFf-bY' 'Signed-off-by: A U Thor \n' +test_trailer_option '%(trailers:key=foo:) trailing colon also works' \ + 'trailers:key=Signed-off-by:' 'Signed-off-by: A U Thor \n' +test_trailer_option '%(trailers:key=foo) multiple keys' \ + 'trailers:key=Reviewed-by:,key=Signed-off-by' 'Reviewed-by: A U Thor \nSigned-off-by: A U Thor \n' +test_trailer_option '%(trailers:key=nonexistent) becomes empty' \ + 'trailers:key=Shined-off-by:' '' + +test_expect_success '%(trailers:key=foo) handles multiple lines even if folded' ' + { + grep -v patch.description expect && + git for-each-ref --format="%(trailers:key=Acked-by)" refs/heads/main >actual && + test_cmp expect actual && + git for-each-ref --format="%(contents:trailers:key=Acked-by)" refs/heads/main >actual && + test_cmp expect actual +' + +test_expect_success '%(trailers:key=foo,unfold) properly unfolds' ' + { + unfold expect && + git for-each-ref --format="%(trailers:key=Signed-Off-by,unfold)" refs/heads/main >actual && + test_cmp expect actual && + git for-each-ref --format="%(contents:trailers:key=Signed-Off-by,unfold)" refs/heads/main >actual && + test_cmp expect actual ' +test_expect_success 'pretty format %(trailers:key=foo,only=no) also includes nontrailer lines' ' + { + echo "Signed-off-by: A U Thor " && + grep patch.description expect && + git for-each-ref --format="%(trailers:key=Signed-off-by,only=no)" refs/heads/main >actual && + test_cmp expect actual && + git for-each-ref --format="%(contents:trailers:key=Signed-off-by,only=no)" refs/heads/main >actual && + test_cmp expect actual +' + +test_trailer_option '%(trailers:key=foo,valueonly) shows only value' \ + 'trailers:key=Signed-off-by,valueonly' 'A U Thor \n' +test_trailer_option '%(trailers:separator) changes separator' \ + 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' 'Reviewed-by: A U Thor ,Signed-off-by: A U Thor ' +test_trailer_option '%(trailers:key_value_separator) changes key-value separator' \ + 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' 'Reviewed-by,A U Thor \nSigned-off-by,A U Thor \n' +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:' 'Reviewed-by,A U Thor ,Signed-off-by,A U Thor ' + +test_failing_trailer_option () { + title="$1" + option="$2" + error="$3" + test_expect_success "$title" ' + # error message cannot be checked under i18n + echo $error >expect && + 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:key) without value is error' \ + 'trailers:key' 'fatal: expected %(trailers:key=)' +test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \ + 'trailers:unsupported' 'fatal: unknown %(trailers) argument: unsupported' + test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' ' cat >expect <<-EOF && fatal: unrecognized %(contents) argument: trailersonly