From patchwork Fri Jan 29 21:09:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hariom verma X-Patchwork-Id: 12056569 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 6AC07C433E6 for ; Fri, 29 Jan 2021 21:09:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0CEF764E04 for ; Fri, 29 Jan 2021 21:09:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233360AbhA2VJy (ORCPT ); Fri, 29 Jan 2021 16:09:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233339AbhA2VJs (ORCPT ); Fri, 29 Jan 2021 16:09:48 -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 8783AC061756 for ; Fri, 29 Jan 2021 13:09:08 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id i9so8222072wmq.1 for ; Fri, 29 Jan 2021 13:09:08 -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=9Ss/PDy0tKp4OviPmOB4XTZJuQJi7ftN+z5e1y21gQE=; b=dsAQl4Og0o0gA6+T2WhoQ/HqnC2w/tXa117RlTdnsdSB+9s6UlPYdQ8WIHS3MlyAGr L/qr0ZDPibM5PFRoPVXPFRJKb28moKQ5KVlOQTAz02DRaEw3P9LYNBY3Q931O8EPnN6A U9hZ0k1N1ekb3h7jcZfJEltdAjGIyb5UQJyjdfaOBAMGnN6kqw1afcLtvaeHX0vXWJtf ojqZcRTm1Dz2cfZDXUqAxo9KIgool2QKq1E8oOZNd2eMJlRsriFHkT7oWB88rImBxn+z CkCOoddJs2+e9VUP1nqa328yaweGRGrB+H93xLha1NgZdtn0vqmnZu9lmZuRIFCZi/M9 jLiQ== 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=9Ss/PDy0tKp4OviPmOB4XTZJuQJi7ftN+z5e1y21gQE=; b=XIYjAEbzwUqjfjMftKWPs7GRdfSOqthtrapL7B0SDialjyyBJZiKvQLhT14fujnfx9 rsMR/wUKEXsLknk5dFcLSL69o3YpqMHwOUz9qsIrISR246io70hB+LdA4YDDw1QmR9j7 LgPFjzBzFDTAP3J2DUqWU/6sVAVG2qQQMmgNdfgUEAzrY1bJlNSEF4S3baYcpKIlJ+cD WwJTpl6yyMwhlKArsEft960SYEc2p6ipZmkYcKVHUuYCwJ/uvahnE9xNVSZZzUCGKywQ 3y+JyQCMlJC5eyaTDCu3q2yxN0t+AmPHL+0wNTpGUb0bfLaB4Otch/QIcAFMuot3icJC rZKg== X-Gm-Message-State: AOAM530whe1wiKnWlNlAcbPHIauMpUlbHtKm8642bIxV1MRrAoAUqbik CXHKXlYnlFmSyHx8/cP+5OUakEeuUzo= X-Google-Smtp-Source: ABdhPJw0XUnYOavsUp6vLfzfmQiUdZk1TFnz0mYe+VsZreXvpDtMXpvuouIXJhFYmVKa9I9gongQFw== X-Received: by 2002:a05:600c:2905:: with SMTP id i5mr5397851wmd.28.1611954546527; Fri, 29 Jan 2021 13:09:06 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p18sm11150429wmc.31.2021.01.29.13.09.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jan 2021 13:09:05 -0800 (PST) Message-Id: In-Reply-To: References: Date: Fri, 29 Jan 2021 21:09:01 +0000 Subject: [PATCH v2 1/3] pretty.c: refactor trailer logic to `format_set_trailers_options()` Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: 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 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 | 85 ++++++++++++++++++++++++++++++-------------------------- pretty.h | 11 ++++++++ 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/pretty.c b/pretty.c index 3922f6f9f24..bb6a3c634ac 100644 --- a/pretty.c +++ b/pretty.c @@ -1148,6 +1148,50 @@ 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; + 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; + 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 +1469,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 7ce6c0b437b..7369cf7e148 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 Fri Jan 29 21:09:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hariom verma X-Patchwork-Id: 12056571 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 05E43C433DB for ; Fri, 29 Jan 2021 21:10:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9677264E04 for ; Fri, 29 Jan 2021 21:10:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233361AbhA2VKF (ORCPT ); Fri, 29 Jan 2021 16:10:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233340AbhA2VJt (ORCPT ); Fri, 29 Jan 2021 16:09:49 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7A99C0613D6 for ; Fri, 29 Jan 2021 13:09:08 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id g10so10187023wrx.1 for ; Fri, 29 Jan 2021 13:09:08 -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=J6nWjVN5iKrMOdK8lXNN4DOueqgALdm0kjEvmZ4i4OA=; b=T567sSXoqpaXOBVfWIEsCpK1GODFylFYfAv5mx73eJnUzYFtPTNHo/fTGj5eMR8NSr nsUl/IKlYPk0WmKKpW356O3wlM7un3MmEL8a15AyRSZFr3AYzCyvfQzpciSXCA1o0mpD 9vMnmOGpiFFEJYJOzGqxkrnZAMMrv7GVKtDzonKIw7FWumyVw0hW1ABBnhNcePWhF1LU GkjGCw4GQZYDc8nzZTU4LFgVaUkU60oF33rXSbyUVlxetFTcf/pQjfwilpZuPWnCH4t6 dQ3Ewx3y5DRal0jn0Bxc2GJWsQ5VakC+Fy0q30bjpeoaakzYayGbs0uRyugOE+U86mlM 46nw== 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=J6nWjVN5iKrMOdK8lXNN4DOueqgALdm0kjEvmZ4i4OA=; b=XGepYZwK86ZKov63pR4LnAVR/jhRcc1ncoFNUv8kqKXoOdDdtU2u02UnC2jMB0A/Ia mP4n7SH5li6SDul+k8rWdpYwQvB7cM34TkPtZFAlmSsIVG2HZXhd4meNe9WogcMx/ZHx WsrhJpBAidJ/cq6q7NwJvMwluVJx0+Vig79PpOnzfRVROdIGanHSN8Xhia9l/8gIVSsq lOjuDaoEh9LqidqOg3fihoVCLdPoxlGupl4wbVN16ShpVZpSlqr7HjBZV+UupOMPp0lm fq53XAQx5sfJQD+xAEjDEfg2Zt7VjrDdQGMwcNWvpstip8Mv4wYWyq7kle0yOfk9Q7fa kf3g== X-Gm-Message-State: AOAM531gpUo1qhiJy3r1HISxFTiqFDa8alOMswtqCqG9X4+Q4sOP4791 ZbMAA8BQormHEXzVEgsdinualQWk0ME= X-Google-Smtp-Source: ABdhPJx0RJnArf0rBrOj7mxtFEnLBwJaziWpkklsxZStD2hajlLnjfV2KEbEbFvpRLkv02ZNw7lR5A== X-Received: by 2002:a5d:6986:: with SMTP id g6mr6487180wru.263.1611954547491; Fri, 29 Jan 2021 13:09:07 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m12sm11794091wmc.10.2021.01.29.13.09.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jan 2021 13:09:06 -0800 (PST) Message-Id: <245e48eb6835cae4e61f65af780b766d990d4b5f.1611954543.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 29 Jan 2021 21:09:02 +0000 Subject: [PATCH v2 2/3] pretty.c: capture invalid trailer argument Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: 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 same logic in ref-filter, it's nice to get invalid trailer argument. This will allow us to print precise error message, while using `format_set_trailers_options()` in ref-filter. Mentored-by: Christian Couder Mentored-by: Heba Waly Signed-off-by: Hariom Verma --- pretty.c | 18 ++++++++++++++---- pretty.h | 3 ++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pretty.c b/pretty.c index bb6a3c634ac..b5fa7944389 100644 --- a/pretty.c +++ b/pretty.c @@ -1152,12 +1152,17 @@ 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; @@ -1186,8 +1191,11 @@ 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)) { + size_t invalid_arg_len = strcspn(*arg, ",)"); + *invalid_arg = xstrndup(*arg, invalid_arg_len); + return 1; + } } return 0; } @@ -1464,12 +1472,13 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */ struct strbuf sepbuf = STRBUF_INIT; struct strbuf kvsepbuf = STRBUF_INIT; size_t ret = 0; + char *unused = NULL; opts.no_divider = 1; 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, &unused)) goto trailer_out; } if (*arg == ')') { @@ -1479,6 +1488,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */ trailer_out: string_list_clear(&filter_list, 0); strbuf_release(&sepbuf); + free((char *)unused); return ret; } diff --git a/pretty.h b/pretty.h index 7369cf7e148..d902cdd70a9 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 Fri Jan 29 21:09:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hariom verma X-Patchwork-Id: 12056573 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 4FFCDC433E0 for ; Fri, 29 Jan 2021 21:10:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E450C64E06 for ; Fri, 29 Jan 2021 21:10:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233370AbhA2VKI (ORCPT ); Fri, 29 Jan 2021 16:10:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233343AbhA2VJ5 (ORCPT ); Fri, 29 Jan 2021 16:09:57 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7537C0613ED for ; Fri, 29 Jan 2021 13:09:09 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id g10so10187057wrx.1 for ; Fri, 29 Jan 2021 13:09:09 -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=9L5tYd1zDSsXacY/l8HMxPiegMGQWdMmroNXlEoEons=; b=WM+qNKsjvKwwILRPi5vp33RaVTe4LJG1SPjxDlIAPMJR1+wI77GEaivicgnnggAJ/m zMvaMvPUuEN43c9wiTlPieAi/ih4rfyOT0BYMsJbErab2f+55/+FFlpxA2CKATfC5KgO fldqvfqups5ZoXff6RZCWy3HYPPtrPMn05rmQak5VO1/mtOnVirfDGwCoNzk4ySUUMvE U8Sa1hPfyoHMT6HAgxrl4V/2EYB4bzq+xT250a4ojED84Qq9OTgLKSc64eldR3OTxh7b wPcgxHjEwChOiN9NGuRcGYXq81UeJaVbcaMRVovZqkVPTK54KbGASy80eRUHK3Z3ddQe zs4Q== 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=9L5tYd1zDSsXacY/l8HMxPiegMGQWdMmroNXlEoEons=; b=JRr85ofFk/92D7J6ub8bzABnzQlo3vqyuo6A5GFANMHALuB0P8XiM0YnLzwXAB2QFt INnOxvDwb5Rfhie5PibxuHloat1Ih4Pay12mfCMYWAzkIvczCnIB3c6cafGRKRXQZ8Rc /XB4e69uxjV7bwOUje6+PeYkn8dOGeTa3nwjOlDUOpijLYmc/bSDXm7CtJOkszQk/ERG H9lVi3apezozoVfCs+lGwKpjPPd/5nbRHEuukm2rG6NRTyi2emkiOQ5tg02bCLRcmwrj JEYrYd0swqFjGx0stkJyW9EYjofGKyBNyGubjPsONw30mvUJ0q2AgZeclcvTkhUGfBos yNAg== X-Gm-Message-State: AOAM531e2i9zIaPtGYV753PRIT2tQpppVLRAGfzXKPXd2AoIM4tZ5b7t q0eukK2JNvuz3+q4MeAnLvDU4ZXsvyM= X-Google-Smtp-Source: ABdhPJwRmPsMq1VJ7snz07RCEKG2s59DVrCiP0HCe9cYTki0c5bK5688zKI6IDeI+F1dRP5eoQqI9g== X-Received: by 2002:adf:ffce:: with SMTP id x14mr6759065wrs.390.1611954548352; Fri, 29 Jan 2021 13:09:08 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y20sm11121398wmj.44.2021.01.29.13.09.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jan 2021 13:09:07 -0800 (PST) Message-Id: <7b8cfb2721c349f2bcebec98f84291b1cffd3b49.1611954543.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 29 Jan 2021 21:09:03 +0000 Subject: [PATCH v2 3/3] ref-filter: use pretty.c logic for trailers Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: 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 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 | 39 ++++++++-- ref-filter.c | 36 +++++---- t/t6300-for-each-ref.sh | 119 +++++++++++++++++++++++++---- 3 files changed, 160 insertions(+), 34 deletions(-) diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt index 2962f85a502..ea1f8417176 100644 --- a/Documentation/git-for-each-ref.txt +++ b/Documentation/git-for-each-ref.txt @@ -260,11 +260,40 @@ 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]`). Valid [:option] are: +** 'key=': only show trailers with specified key. Matching is done + case-insensitively and trailing colon is optional. If option is + given multiple times trailer lines matching any of the keys are + shown. This option automatically enables the `only` option so that + non-trailer lines in the trailer block are hidden. If that is not + desired it can be disabled with `only=false`. E.g., + `%(trailers:key=Reviewed-by)` shows trailer lines with key + `Reviewed-by`. +** 'only[=val]': select whether non-trailer lines from the trailer + block should be included. The `only` keyword may optionally be + followed by an equal sign and one of `true`, `on`, `yes` to omit or + `false`, `off`, `no` to show the non-trailer lines. If option is + given without value it is enabled. If given multiple times the last + value is used. +** 'separator=': specify a separator inserted between trailer + lines. When this option is not given each trailer line is + terminated with a line feed character. The string SEP may contain + the literal formatting codes. To use comma as separator one must use + `%x2C` as it would otherwise be parsed as next option. If separator + option is given multiple times only the last one is used. + E.g., `%(trailers:key=Ticket,separator=%x2C)` shows all trailer lines + whose key is "Ticket" separated by a comma. +** 'key_value_separator=': specify a separator inserted between + key and value. The string SEP may contain the literal formatting codes. + E.g., `%(trailers:key=Ticket,key_value_separator=%x2C)` shows all trailer + lines whose key is "Ticket" with key and value separated by a comma. +** 'unfold[=val]': make it behave as if interpret-trailer's `--unfold` + option was given. In same way as to for `only` it can be followed + by an equal sign and explicit value. E.g., + `%(trailers:only,unfold=true)` unfolds and shows all trailer lines. +** 'valueonly[=val]': skip over the key part of the trailer line and only + show the value part. Also this optionally allows explicit value. 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 ee337df232a..2b1c61eadf4 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -67,6 +67,12 @@ struct refname_atom { int lstrip, rstrip; }; +struct ref_trailer_buf { + struct string_list filter_list; + struct strbuf sepbuf; + struct strbuf kvsepbuf; +} ref_trailer_buf; + 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 ca62e764b58..a8835b13915 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" ' + echo $expect >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