From patchwork Sun Nov 29 19:52:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11939297 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=-15.8 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,USER_AGENT_GIT 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 27732C3E8C5 for ; Sun, 29 Nov 2020 19:53:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D828020756 for ; Sun, 29 Nov 2020 19:53:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="O6v3WMQU" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727117AbgK2Txb (ORCPT ); Sun, 29 Nov 2020 14:53:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726304AbgK2Txb (ORCPT ); Sun, 29 Nov 2020 14:53:31 -0500 Received: from mail-lf1-x142.google.com (mail-lf1-x142.google.com [IPv6:2a00:1450:4864:20::142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3E1FC0613D2 for ; Sun, 29 Nov 2020 11:52:50 -0800 (PST) Received: by mail-lf1-x142.google.com with SMTP id d20so16446023lfe.11 for ; Sun, 29 Nov 2020 11:52:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1KDb1UzSaXXrd0cITDJRNKDkZpbmoxR1GI0d/pIcjoY=; b=O6v3WMQUG3Dimx0ft6xWQ4F4fwRXZpPAU8Iz4ZfGrTjNj/y73WO28NK8GgSBo0YU/K jyanq0igcTlqttsllIPI+JTLiZuEYXi0hZdJrgiav171uoa+SH22VREVbuiijMGP8khI o1bT1XB3gr0aLl0vffSpfA1S2XqnU49Jci1k1Z0Bof1gUmzzS0RP3FyprvPFCCFBupbE Fk9R+nsRa64N/A668WMiNOUG1TSiRWY7oilIM//KD0tMOkWxsGiLZncNZ7gNoalalWUf 3x+8rNUTCjm8vufPu8IBgVXJ7bAr7VoPASL1LUo5bDPUnd/bar1Ppvmfvrgpcx74BErd ZAow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1KDb1UzSaXXrd0cITDJRNKDkZpbmoxR1GI0d/pIcjoY=; b=XY7wsrLoDvhysTCAl9BguKVTsTjwYXSP3lwYbUT+Cel/sG5Sb4cYr1v2vESTJKipY7 iODGYsvffnzzZUQM5rjsCRQxTgAMu+r1OK6eY0fclRBIZH29N71Na89fCmOA9dfsIJq9 mSQEIhgXvybmij/hFj0VaLfLOlvHGrHX4sRERqSZXRmkI7nbT2db5wF4/8ecbxYlPVRy a/XWsVIdH0OUX/CY0gFymfPBZq24coX5Z0Pfb8P4tc9003Kh7xTzzxodmAsqbytVICMZ IBrdwqux1cWQasaPaORWO7gwQsxcCompnKKKOuHyI956ReEARhg+AP4ZpJXHsG/802bz QlrQ== X-Gm-Message-State: AOAM531Rj3uU+4ZyrxD3wdaEQ0lBYw+Do/x8UBzJetZ1hmHTvi8xU57x KFH0jmzj4Xks4te7/PtJzogersPdyYM= X-Google-Smtp-Source: ABdhPJyLun4u1H1yy8dwD4eMFCDLhh21mit9nrLX2sB+mmRLIRD41O37ELT8hTOVhTlxtM5pCHrG/w== X-Received: by 2002:a05:6512:1095:: with SMTP id j21mr7473216lfg.309.1606679569164; Sun, 29 Nov 2020 11:52:49 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id w72sm1808707lff.89.2020.11.29.11.52.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2020 11:52:48 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King Subject: [PATCH v3 1/4] grep: don't set up a "default" repo for grep Date: Sun, 29 Nov 2020 20:52:19 +0100 Message-Id: <1d3878799f8260968ea9f6a75a92c4daca1da133.1606679312.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org `init_grep_defaults()` fills a `static struct grep_opt grep_defaults`. This struct is then used by `grep_init()` as a blueprint for other such structs. Notably, `grep_init()` takes a `struct repo *` and assigns it into the target struct. As a result, it is unnecessary for us to take a `struct repo *` in `init_grep_defaults()` as well. We assign it into the default struct and never look at it again. And in light of how we return early if we have already set up the default struct, it's not just unnecessary, but is also a bit confusing: If we are called twice and with different repos, is it a bug or a feature that we ignore the second repo? Drop the repo parameter for `init_grep_defaults()`. Signed-off-by: Martin Ågren Signed-off-by: Junio C Hamano --- Documentation/MyFirstObjectWalk.txt | 2 +- grep.h | 2 +- builtin/grep.c | 2 +- builtin/log.c | 2 +- grep.c | 3 +-- revision.c | 2 +- 6 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Documentation/MyFirstObjectWalk.txt b/Documentation/MyFirstObjectWalk.txt index c3f2d1a831..85434d1938 100644 --- a/Documentation/MyFirstObjectWalk.txt +++ b/Documentation/MyFirstObjectWalk.txt @@ -394,7 +394,7 @@ First some setup. Add `init_grep_defaults()` to `init_walken_defaults()` and add ---- static void init_walken_defaults(void) { - init_grep_defaults(the_repository); + init_grep_defaults(); } ... diff --git a/grep.h b/grep.h index 9115db8515..1c5478f381 100644 --- a/grep.h +++ b/grep.h @@ -170,7 +170,7 @@ struct grep_opt { void *output_priv; }; -void init_grep_defaults(struct repository *); +void init_grep_defaults(void); int grep_config(const char *var, const char *value, void *); void grep_init(struct grep_opt *, struct repository *repo, const char *prefix); void grep_destroy(void); diff --git a/builtin/grep.c b/builtin/grep.c index e58e57504c..2b96efa8c2 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -950,7 +950,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix) OPT_END() }; - init_grep_defaults(the_repository); + init_grep_defaults(); git_config(grep_cmd_config, NULL); grep_init(&opt, the_repository, prefix); diff --git a/builtin/log.c b/builtin/log.c index 49eb8f6431..eee4beca4d 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -131,7 +131,7 @@ static int log_line_range_callback(const struct option *option, const char *arg, static void init_log_defaults(void) { - init_grep_defaults(the_repository); + init_grep_defaults(); init_diff_ui_defaults(); decoration_style = auto_decoration_style(); diff --git a/grep.c b/grep.c index 54af9f813e..b351449f7f 100644 --- a/grep.c +++ b/grep.c @@ -57,7 +57,7 @@ static void color_set(char *dst, const char *color_bytes) * We could let the compiler do this, but without C99 initializers * the code gets unwieldy and unreadable, so... */ -void init_grep_defaults(struct repository *repo) +void init_grep_defaults(void) { struct grep_opt *opt = &grep_defaults; static int run_once; @@ -67,7 +67,6 @@ void init_grep_defaults(struct repository *repo) run_once++; memset(opt, 0, sizeof(*opt)); - opt->repo = repo; opt->relative = 1; opt->pathname = 1; opt->max_depth = -1; diff --git a/revision.c b/revision.c index aa62212040..f35ea1db11 100644 --- a/revision.c +++ b/revision.c @@ -1834,7 +1834,7 @@ void repo_init_revisions(struct repository *r, revs->commit_format = CMIT_FMT_DEFAULT; revs->expand_tabs_in_log_default = 8; - init_grep_defaults(revs->repo); + init_grep_defaults(); grep_init(&revs->grep_filter, revs->repo, prefix); revs->grep_filter.status_only = 1; From patchwork Sun Nov 29 19:52:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11939299 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=-15.8 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,USER_AGENT_GIT 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 4DBE2C64E7C for ; Sun, 29 Nov 2020 19:53:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0A93620757 for ; Sun, 29 Nov 2020 19:53:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZzJVk1tJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728037AbgK2Txh (ORCPT ); Sun, 29 Nov 2020 14:53:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726304AbgK2Txe (ORCPT ); Sun, 29 Nov 2020 14:53:34 -0500 Received: from mail-lj1-x243.google.com (mail-lj1-x243.google.com [IPv6:2a00:1450:4864:20::243]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A00C7C0613D3 for ; Sun, 29 Nov 2020 11:52:53 -0800 (PST) Received: by mail-lj1-x243.google.com with SMTP id y10so13643533ljc.7 for ; Sun, 29 Nov 2020 11:52:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pFypa8gsyoCo66VLOtmqADXPSpAdCBed4UcBTQLz4nQ=; b=ZzJVk1tJM9QIVk2Q4PlE7UpiYDMjIQmqPJzz6EBFauv444rPGwGqrWfNQ4aXAJRcyo +S5/2r/9C5NMOIP90lrzsskSkoNAlInRtjPujfRBtGE8yjEh3dw0K3kPMzmkjBylxgR1 f4u3jsLdqb2nuohIYsIP80TJgkAX9q/l7O9VFnmgjDxsvgB77EuS3XldJjWwd4Tby0x9 vabE48kvTMj0lVspRkmDFnPsJDGJHhJIg/b6fChMRidp+RrDdO0VKGmV3QLzPJJDWWqa m4ZzJZyQ/0+LHDscvgOogmDFG1mqPbu/OoXcw9k0YNF9EjEkME40tzbuQo3EZOZQjOv/ vCwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pFypa8gsyoCo66VLOtmqADXPSpAdCBed4UcBTQLz4nQ=; b=nKE3KDA47my9JcKLOq1l8MtsaE/ztlmzBMw3YGf32bMQNrJnFMn0pp8WMzR4Jo9IWn jwX/EI62oMY6Yn/MJR+17JpJ9/oQjrPem4a7PzrShg5CNjwcxwpUUcVQDb3cvvylrIrU sTEh0NH6DG7TnZegXWTm3dWFupQKFeA1Xl3HCzwUjk0fgSD1uEasnKMmkM5f0Dtcaaj/ nqTBKvFRCB9N4PPmqLj6VGtCZ0mlZPqK6qocQU+O4hM6kpyOk3oWf/Xslhf2HQ+1V3lI nQJSD69JM8FeXz+03fznTY1SK4l9DdHmHDFb4h/UUfG3dX/Ii8UyoriimK7xjkUt6lha 24wg== X-Gm-Message-State: AOAM531z+VwyAFaJHb8fi31aeXBQ2nGmlAHBGTdfr5hNSoHqhK1BSKwM QnHDVuYhRC6sWi/Tt6Mb4pPi6RBjrIg= X-Google-Smtp-Source: ABdhPJwjp2ucDpXXS57kuhnzmjRIpwkbdHoM79o1h5L5Ss7C1BQeRT1OEe3go5FGKloJlWZRbcysAg== X-Received: by 2002:a2e:6e14:: with SMTP id j20mr8037593ljc.372.1606679571800; Sun, 29 Nov 2020 11:52:51 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id w72sm1808707lff.89.2020.11.29.11.52.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2020 11:52:51 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King Subject: [PATCH v3 2/4] grep: use designated initializers for `grep_defaults` Date: Sun, 29 Nov 2020 20:52:20 +0100 Message-Id: <96313423a75fa8d88b6ecd5a15c21a7fbaf9e9be.1606679312.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In 15fabd1bbd ("builtin/grep.c: make configuration callback more reusable", 2012-10-09), we learned to fill a `static struct grep_opt grep_defaults` which we can use as a blueprint for other such structs. At the time, we didn't consider designated initializers to be widely useable, but these days, we do. (See, e.g., cbc0f81d96 ("strbuf: use designated initializers in STRBUF_INIT", 2017-07-10).) Use designated initializers to let the compiler set up the struct and so that we don't need to remember to call `init_grep_defaults()`. Signed-off-by: Martin Ågren Signed-off-by: Junio C Hamano --- Documentation/MyFirstObjectWalk.txt | 10 +---- grep.h | 1 - builtin/grep.c | 1 - builtin/log.c | 1 - grep.c | 64 +++++++++++------------------ revision.c | 1 - 6 files changed, 26 insertions(+), 52 deletions(-) diff --git a/Documentation/MyFirstObjectWalk.txt b/Documentation/MyFirstObjectWalk.txt index 85434d1938..7f4bffc4dd 100644 --- a/Documentation/MyFirstObjectWalk.txt +++ b/Documentation/MyFirstObjectWalk.txt @@ -388,17 +388,9 @@ Next, let's try to filter the commits we see based on their author. This is equivalent to running `git log --author=`. We can add a filter by modifying `rev_info.grep_filter`, which is a `struct grep_opt`. -First some setup. Add `init_grep_defaults()` to `init_walken_defaults()` and add -`grep_config()` to `git_walken_config()`: +First some setup. Add `grep_config()` to `git_walken_config()`: ---- -static void init_walken_defaults(void) -{ - init_grep_defaults(); -} - -... - static int git_walken_config(const char *var, const char *value, void *cb) { grep_config(var, value, cb); diff --git a/grep.h b/grep.h index 1c5478f381..b5c4e223a8 100644 --- a/grep.h +++ b/grep.h @@ -170,7 +170,6 @@ struct grep_opt { void *output_priv; }; -void init_grep_defaults(void); int grep_config(const char *var, const char *value, void *); void grep_init(struct grep_opt *, struct repository *repo, const char *prefix); void grep_destroy(void); diff --git a/builtin/grep.c b/builtin/grep.c index 2b96efa8c2..ca259af441 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -950,7 +950,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix) OPT_END() }; - init_grep_defaults(); git_config(grep_cmd_config, NULL); grep_init(&opt, the_repository, prefix); diff --git a/builtin/log.c b/builtin/log.c index eee4beca4d..cf41714fb0 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -131,7 +131,6 @@ static int log_line_range_callback(const struct option *option, const char *arg, static void init_log_defaults(void) { - init_grep_defaults(); init_diff_ui_defaults(); decoration_style = auto_decoration_style(); diff --git a/grep.c b/grep.c index b351449f7f..8f2009ec9f 100644 --- a/grep.c +++ b/grep.c @@ -14,7 +14,31 @@ static int grep_source_load(struct grep_source *gs); static int grep_source_is_binary(struct grep_source *gs, struct index_state *istate); -static struct grep_opt grep_defaults; +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] = "", + [GREP_COLOR_FUNCTION] = "", + [GREP_COLOR_LINENO] = "", + [GREP_COLOR_COLUMNNO] = "", + [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, +}; #ifdef USE_LIBPCRE2 static pcre2_general_context *pcre2_global_context; @@ -42,49 +66,11 @@ static const char *color_grep_slots[] = { [GREP_COLOR_SEP] = "separator", }; -static void std_output(struct grep_opt *opt, const void *buf, size_t size) -{ - fwrite(buf, size, 1, stdout); -} - static void color_set(char *dst, const char *color_bytes) { xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes); } -/* - * Initialize the grep_defaults template with hardcoded defaults. - * We could let the compiler do this, but without C99 initializers - * the code gets unwieldy and unreadable, so... - */ -void init_grep_defaults(void) -{ - struct grep_opt *opt = &grep_defaults; - static int run_once; - - if (run_once) - return; - run_once++; - - memset(opt, 0, sizeof(*opt)); - opt->relative = 1; - opt->pathname = 1; - opt->max_depth = -1; - opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED; - color_set(opt->colors[GREP_COLOR_CONTEXT], ""); - color_set(opt->colors[GREP_COLOR_FILENAME], ""); - color_set(opt->colors[GREP_COLOR_FUNCTION], ""); - color_set(opt->colors[GREP_COLOR_LINENO], ""); - color_set(opt->colors[GREP_COLOR_COLUMNNO], ""); - color_set(opt->colors[GREP_COLOR_MATCH_CONTEXT], GIT_COLOR_BOLD_RED); - color_set(opt->colors[GREP_COLOR_MATCH_SELECTED], GIT_COLOR_BOLD_RED); - color_set(opt->colors[GREP_COLOR_SELECTED], ""); - color_set(opt->colors[GREP_COLOR_SEP], GIT_COLOR_CYAN); - opt->only_matching = 0; - opt->color = -1; - opt->output = std_output; -} - static int parse_pattern_type_arg(const char *opt, const char *arg) { if (!strcmp(arg, "default")) diff --git a/revision.c b/revision.c index f35ea1db11..963868f699 100644 --- a/revision.c +++ b/revision.c @@ -1834,7 +1834,6 @@ void repo_init_revisions(struct repository *r, revs->commit_format = CMIT_FMT_DEFAULT; revs->expand_tabs_in_log_default = 8; - init_grep_defaults(); grep_init(&revs->grep_filter, revs->repo, prefix); revs->grep_filter.status_only = 1; From patchwork Sun Nov 29 19:52:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11939303 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=-15.8 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,USER_AGENT_GIT 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 73E65C64E8A for ; Sun, 29 Nov 2020 19:53:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 320C620809 for ; Sun, 29 Nov 2020 19:53:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YitA2dCa" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728147AbgK2Txj (ORCPT ); Sun, 29 Nov 2020 14:53:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725950AbgK2Txj (ORCPT ); Sun, 29 Nov 2020 14:53:39 -0500 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D066EC0613D4 for ; Sun, 29 Nov 2020 11:52:58 -0800 (PST) Received: by mail-lj1-x234.google.com with SMTP id t22so13673023ljk.0 for ; Sun, 29 Nov 2020 11:52:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KXyPVqMvIDTnoE3DFp/xAUTBSZaE6gyvH0WAkC6/i2w=; b=YitA2dCaokxDdaK78st5yT6Ve79jIJpBSXJ1LBa3DxeR1S0VOdn5kD3x/eW/J5KT+k Uf/f3c33KkcH63n87KIe6p5yhDQKzJM1uF61p7NTFHeryrEEXOkTizAyrWVU91Dt0fcM 1YfvVrZqz4RvaWaTrtOKEWwjLeHCU5UqOn0MQif5semmBAGjrpsb2Pso/Mop0pEQAW4i KkmopTC1H/cwrp5BsMxtQ9NjSENoYA+IoF1RhAD0rRi2RVDx8VidR2WdC3cbFOpTi4nh 7QaWH8rhGekuoxFkXPdLsnxpdS5clWcE7pyuYo0avrjNFinl7EbfDLalg64zoZ/JvTdZ zTZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KXyPVqMvIDTnoE3DFp/xAUTBSZaE6gyvH0WAkC6/i2w=; b=FPMd3MMpbv6I76z9m4qyaDzbgcPehKt45s6pev3P+v0QfeLoWdWDqL18jMz1B57lSV mVH1kF5FOF4F93uMfiL4xmQFNfCNq+iVnkfyCwZahAbGt7KVxgRRbh5C9RSzuxZBxWsj CoAPSMYFs3H4BeP9ldUgkqmiH71pTpnev+CH0aSa/jlaqJ84FU/2WXPosuzX5PRhpenA ZN95oBJZwnPhchh6LSeDgiez7Y/buULj+yylamg9ON5ONlub3deRh5OIglbZP7uQGGXs 8PWpFfWQt+eCkw/x7zKgkIcZ/y2f7BGfkzrtWudueVKUI1L0Q42dGdZrm4XKzZOW47q+ e43A== X-Gm-Message-State: AOAM531JtnBTHA+JQ34JGHBOsLAWWlTb6QOGdfgzQeGngC3OrUvjNs36 D60z+in+W/5zuXnw3QOg1xYmwcKxpFE= X-Google-Smtp-Source: ABdhPJzOOE66mHStBjhVqvQYls2iO5zZG55RYrFx5eM8yV/5oB8BRhgamRfhQXXHJ3sHWkeTNfPXaA== X-Received: by 2002:a2e:9b83:: with SMTP id z3mr8213294lji.145.1606679577040; Sun, 29 Nov 2020 11:52:57 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id w72sm1808707lff.89.2020.11.29.11.52.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2020 11:52:56 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King Subject: [PATCH v3 3/4] grep: copy struct in one fell swoop Date: Sun, 29 Nov 2020 20:52:21 +0100 Message-Id: <77371dee2f13723cf8f766793b07241ca458adbd.1606679312.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We have a `struct grep_opt` with our defaults which we then copy into the caller's struct. Rather than zeroing the target struct and copying each element one by one, just copy everything at once. This leaves the code simpler and more maintainable. We don't have any ownership issues with what we're copying now and can just greedily copy the whole thing. If and when we do need to handle such elements (`char *`?), we must and can handle it appropriately. Make sure to leave a comment to our future selves. Signed-off-by: Martin Ågren --- grep.c | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/grep.c b/grep.c index 8f2009ec9f..efeb6dc58d 100644 --- a/grep.c +++ b/grep.c @@ -66,11 +66,6 @@ static const char *color_grep_slots[] = { [GREP_COLOR_SEP] = "separator", }; -static void color_set(char *dst, const char *color_bytes) -{ - xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes); -} - static int parse_pattern_type_arg(const char *opt, const char *arg) { if (!strcmp(arg, "default")) @@ -100,6 +95,14 @@ int grep_config(const char *var, const char *value, void *cb) 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; @@ -157,9 +160,6 @@ int grep_config(const char *var, const char *value, void *cb) */ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix) { - struct grep_opt *def = &grep_defaults; - int i; - #if defined(USE_LIBPCRE2) if (!pcre2_global_context) pcre2_global_context = pcre2_general_context_create( @@ -171,26 +171,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix pcre_free = free; #endif - memset(opt, 0, sizeof(*opt)); + *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; - - opt->only_matching = def->only_matching; - opt->color = def->color; - opt->extended_regexp_option = def->extended_regexp_option; - opt->pattern_type_option = def->pattern_type_option; - opt->linenum = def->linenum; - opt->columnnum = def->columnnum; - opt->max_depth = def->max_depth; - opt->pathname = def->pathname; - opt->relative = def->relative; - opt->output = def->output; - - for (i = 0; i < NR_GREP_COLORS; i++) - color_set(opt->colors[i], def->colors[i]); } void grep_destroy(void) From patchwork Sun Nov 29 19:52:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11939305 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=-15.8 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,USER_AGENT_GIT 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 CDD53C64E7B for ; Sun, 29 Nov 2020 19:53:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 895B320809 for ; Sun, 29 Nov 2020 19:53:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JUIfFF9Y" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728318AbgK2Txs (ORCPT ); Sun, 29 Nov 2020 14:53:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726440AbgK2Txs (ORCPT ); Sun, 29 Nov 2020 14:53:48 -0500 Received: from mail-lf1-x141.google.com (mail-lf1-x141.google.com [IPv6:2a00:1450:4864:20::141]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02943C0617A7 for ; Sun, 29 Nov 2020 11:53:02 -0800 (PST) Received: by mail-lf1-x141.google.com with SMTP id r24so16485007lfm.8 for ; Sun, 29 Nov 2020 11:53:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qGzHyjlffIaWDpyx3tiw/kpoARp3zvSiLJHYiQKp1g8=; b=JUIfFF9YEWGB+46hfQ/mJsyHSzftzaJzMfOInfSzqpSB154Mq5XsDdqfvNYtolCyVD fysnPrlKfhitfV3/Tpol4EV/VyGX9ZujzcELXEZQ45hUCBy1+PA76r89TlC3eh7qLRgb cWp+2DmAA6jJ5N+6ClygC95s1Jk30ru62cSjqS6WyMUnJhb00T3/kjDBR7akDT5aGkWP GIeTcAdOZjii5oEzI42OAvKoigq/kpweLzM5/gJfIEpilzgOiM2eNLbVpExs6AO4jFbx t0EItG6Lw3deXAei9D7Jg3Xs3esFcna40hf5IlwwK4oLYmRQuWF725LllGU7J5uI9DE9 PHKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qGzHyjlffIaWDpyx3tiw/kpoARp3zvSiLJHYiQKp1g8=; b=rkvHjaXNdNn0oKH8qF9Sp1xCxJ9Wzlp8ZvSzzHCq5unbYTHbxZi97LDoU+I/6tcrcp w5pfDX5QJsNld5o/BwfdddlsaUnokt9mJyjDXxV/Oxdz0tOc8pw5WunAi2+RBx6oAobH TPhScj78SZLeyuYDJ3pFc6vechUxqsQbMhP1mjXfHA4HFM8yXLr0pLYDe8MniYzb+Awo Ap0WsLtwmo8L7HJcpwnZJLcxIjruM6LGnLLFrl6z9KAL2WBBJsaolPraMs/YjWCJ4hS4 yzyh9rJl9P+rxSqDWR/FoNInlITWBvdggCAcMWaz5a5BrFX/nObXtCp+NlbKXVo2l/Gt eeVw== X-Gm-Message-State: AOAM530eIDra/LrZSitr5ea9uyRFTDdkBYYXpHWpp43B94BjiB1TWfsU Q7MKvZJPaZZE3VTVVJs+6LBY0OHuV6s= X-Google-Smtp-Source: ABdhPJzD/RwGW3LidEvpM2E2QE2lg8Ow2gWbStFqzAIA2HfDhqEpPCa99H84HSBTnpx6nNIChn05cg== X-Received: by 2002:a19:cd6:: with SMTP id 205mr7428325lfm.117.1606679580242; Sun, 29 Nov 2020 11:53:00 -0800 (PST) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id w72sm1808707lff.89.2020.11.29.11.52.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2020 11:52:59 -0800 (PST) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Emily Shaffer , Jeff King Subject: [PATCH v3 4/4] MyFirstObjectWalk: drop `init_walken_defaults()` Date: Sun, 29 Nov 2020 20:52:22 +0100 Message-Id: <977a7b6eca28e50d3a7377164228031b96b19ef0.1606679312.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In a recent commit, we stopped calling `init_grep_defaults()` from this function. Thus, by the end of the tutorial, we still haven't added any contents to this function. Let's remove it for simplicity. Signed-off-by: Martin Ågren Signed-off-by: Junio C Hamano --- Documentation/MyFirstObjectWalk.txt | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/Documentation/MyFirstObjectWalk.txt b/Documentation/MyFirstObjectWalk.txt index 7f4bffc4dd..2d10eea7a9 100644 --- a/Documentation/MyFirstObjectWalk.txt +++ b/Documentation/MyFirstObjectWalk.txt @@ -182,30 +182,6 @@ its `init_log_defaults()` sets its own state (`decoration_style`) and asks `grep` and `diff` to initialize themselves by calling each of their initialization functions. -For our first example within `git walken`, we don't intend to use any other -components within Git, and we don't have any configuration to do. However, we -may want to add some later, so for now, we can add an empty placeholder. Create -a new function in `builtin/walken.c`: - ----- -static void init_walken_defaults(void) -{ - /* - * We don't actually need the same components `git log` does; leave this - * empty for now. - */ -} ----- - -Make sure to add a line invoking it inside of `cmd_walken()`. - ----- -int cmd_walken(int argc, const char **argv, const char *prefix) -{ - init_walken_defaults(); -} ----- - ==== Configuring From `.gitconfig` Next, we should have a look at any relevant configuration settings (i.e.,