From patchwork Wed Aug 25 09:08:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12456753 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.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 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 9624BC432BE for ; Wed, 25 Aug 2021 09:08:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7B2BC610CD for ; Wed, 25 Aug 2021 09:08:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239447AbhHYJJl (ORCPT ); Wed, 25 Aug 2021 05:09:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235904AbhHYJJl (ORCPT ); Wed, 25 Aug 2021 05:09:41 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EB74C0613C1 for ; Wed, 25 Aug 2021 02:08:55 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id h4so5107099wro.7 for ; Wed, 25 Aug 2021 02:08:55 -0700 (PDT) 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=fsaoL+6Pqer72FU5kYg8C0inGt5ue3iTSQcHJXS750k=; b=B2Df1t/ezfvYFigb5lz3CmwFAuI5XOJWM+JODXP8rvwCe70VTAXIR4vX2ki3niUyui ch7yiIjhFnbGhRYjxK2L1SXaVOxJgQqYOiZyPZCbcYNNbashwVFlCTc1ZlIt5uHMA8SL VV8sAZNo+WwFdh342M9Ge3hXlNCrT5DY46ao5c2UIDPGkaR0y9oPfpnPWHs6GB3NAxjY 5MYaJV4bM6QuYLxSGH92e93iXvqDPOz2ujG8TefaBOqrb/73qKLUdkaQDrUTHuu6j03x +XlykNJWMhMXAWIqW7lobLsSVM8hbEExgCJaSYWWfZOLzGs9LgoGRqXGtLlvhoQ07DID AFUw== 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=fsaoL+6Pqer72FU5kYg8C0inGt5ue3iTSQcHJXS750k=; b=HM/dnBDF7JAJvL5zozCTLe6WZPSabF1RRyZzC738PfNTnMJb+8aB9Wu9uu8EOl2/A2 xeav3DJ1j3OyjV1ZSnmwcxP00NlOyQn7H5WlghmKb8774xQ48peep6aPr7SJPvlTDqn8 94zXfYr7qDR1F7zG/gSafKs9mr40CDOsj1JpupjG21hv7sk0Nz3VBZi7V3YH67Lzjx2e JBkiLIPitoBuu0K8+R/Bqi92p79zPAluOACAEcHkLLvtLQyl7lNS0D0GM8Cgi7hNyGdB HljczvDsUUTzHBVblWNvaAynGIndwcePe7maxT9wpCu2g1d/KoAeNiZgisDSnlmnVrUc 7jqQ== X-Gm-Message-State: AOAM53354J0nf9Jk5TQHvQBExGLBBNVw9/V3ZdmKjmb6MeoLozHP1wfh GC2AR0IIbvddA+BvnAHS4UhXKvpBQpU= X-Google-Smtp-Source: ABdhPJxAHMqywHqyHP1hdy5J4z/IBgwPUsDMXyy0p1JRFhdm4eZjl5IfqO+UyAYR0w+ym48/rnMLHA== X-Received: by 2002:a5d:464c:: with SMTP id j12mr6737446wrs.27.1629882533975; Wed, 25 Aug 2021 02:08:53 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j7sm4715435wmi.37.2021.08.25.02.08.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 02:08:53 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Wed, 25 Aug 2021 09:08:45 +0000 Subject: [PATCH 1/8] [GSOC] ref-filter: use list api to replace ref_sorting linked list Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu struct ref_sorting is connected by the internal singly linked list, it is very difficult to use, and each node is added by the head insertion method but we can only traverse it forward. Replace it with list api, which is easy to use and introduce a free_ref_sorting_list() which can free all ref_sorting item and solve the problem of memory leak. This will help the logic of ref-filter become clearer. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- builtin/branch.c | 20 +++++++++++--------- builtin/for-each-ref.c | 15 ++++++++------- builtin/ls-remote.c | 12 ++++++------ builtin/tag.c | 19 +++++++++++-------- ref-filter.c | 42 ++++++++++++++++++++++++++++-------------- ref-filter.h | 8 +++++--- 6 files changed, 69 insertions(+), 47 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index b23b1d1752a..72fafd301c5 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -77,7 +77,7 @@ define_list_config_array(color_branch_slots); static int git_branch_config(const char *var, const char *value, void *cb) { const char *slot_name; - struct ref_sorting **sorting_tail = (struct ref_sorting **)cb; + struct ref_sorting *sorting_tail = (struct ref_sorting *)cb; if (!strcmp(var, "branch.sort")) { if (!value) @@ -624,7 +624,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) enum branch_track track; struct ref_filter filter; int icase = 0; - static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; + struct ref_sorting sorting; struct ref_format format = REF_FORMAT_INIT; struct option options[] = { @@ -665,7 +665,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) OPT_MERGED(&filter, N_("print only branches that are merged")), OPT_NO_MERGED(&filter, N_("print only branches that are not merged")), OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")), - OPT_REF_SORT(sorting_tail), + OPT_REF_SORT(&sorting), OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"), N_("print only branches of the object"), parse_opt_object_name), OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), @@ -678,11 +678,12 @@ int cmd_branch(int argc, const char **argv, const char *prefix) memset(&filter, 0, sizeof(filter)); filter.kind = FILTER_REFS_BRANCHES; filter.abbrev = -1; + INIT_LIST_HEAD(&sorting.list); if (argc == 2 && !strcmp(argv[1], "-h")) usage_with_options(builtin_branch_usage, options); - git_config(git_branch_config, sorting_tail); + git_config(git_branch_config, &sorting); track = git_branch_track; @@ -748,12 +749,12 @@ int cmd_branch(int argc, const char **argv, const char *prefix) * local branches 'refs/heads/...' and finally remote-tracking * branches 'refs/remotes/...'. */ - if (!sorting) - sorting = ref_default_sorting(); - ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); + if (list_empty(&sorting.list)) + ref_default_sorting(&sorting); + ref_sorting_set_sort_flags_all(&sorting, REF_SORTING_ICASE, icase); ref_sorting_set_sort_flags_all( - sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1); - print_ref_list(&filter, sorting, &format); + &sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1); + print_ref_list(&filter, &sorting, &format); print_columns(&output, colopts, NULL); string_list_clear(&output, 0); return 0; @@ -864,5 +865,6 @@ int cmd_branch(int argc, const char **argv, const char *prefix) } else usage_with_options(builtin_branch_usage, options); + free_ref_sorting_list(&sorting); return 0; } diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 89cb6307d46..3cd38eacd7c 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -17,7 +17,7 @@ static char const * const for_each_ref_usage[] = { int cmd_for_each_ref(int argc, const char **argv, const char *prefix) { int i; - struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; + struct ref_sorting sorting; int maxcount = 0, icase = 0; struct ref_array array; struct ref_filter filter; @@ -39,7 +39,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) OPT_INTEGER( 0 , "count", &maxcount, N_("show only matched refs")), OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), OPT__COLOR(&format.use_color, N_("respect format colors")), - OPT_REF_SORT(sorting_tail), + OPT_REF_SORT(&sorting), OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"), N_("print only refs which points at the given object"), parse_opt_object_name), @@ -51,6 +51,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) OPT_END(), }; + INIT_LIST_HEAD(&sorting.list); memset(&array, 0, sizeof(array)); memset(&filter, 0, sizeof(filter)); @@ -70,15 +71,15 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) if (verify_ref_format(&format)) usage_with_options(for_each_ref_usage, opts); - if (!sorting) - sorting = ref_default_sorting(); - ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); + if (list_empty(&sorting.list)) + ref_default_sorting(&sorting); + ref_sorting_set_sort_flags_all(&sorting, REF_SORTING_ICASE, icase); filter.ignore_case = icase; filter.name_patterns = argv; filter.match_as_path = 1; filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN); - ref_array_sort(sorting, &array); + ref_array_sort(&sorting, &array); if (!maxcount || array.nr < maxcount) maxcount = array.nr; @@ -96,6 +97,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) ref_array_clear(&array); free_commit_list(filter.with_commit); free_commit_list(filter.no_commit); - UNLEAK(sorting); + free_ref_sorting_list(&sorting); return 0; } diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c index 1794548c711..717ecde03d1 100644 --- a/builtin/ls-remote.c +++ b/builtin/ls-remote.c @@ -54,7 +54,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) struct transport *transport; const struct ref *ref; struct ref_array ref_array; - static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; + static struct ref_sorting sorting; struct option options[] = { OPT__QUIET(&quiet, N_("do not print remote URL")), @@ -68,7 +68,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL), OPT_BOOL(0, "get-url", &get_url, N_("take url..insteadOf into account")), - OPT_REF_SORT(sorting_tail), + OPT_REF_SORT(&sorting), OPT_SET_INT_F(0, "exit-code", &status, N_("exit with exit code 2 if no matching refs are found"), 2, PARSE_OPT_NOCOMPLETE), @@ -79,13 +79,12 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) }; memset(&ref_array, 0, sizeof(ref_array)); + INIT_LIST_HEAD(&sorting.list); argc = parse_options(argc, argv, prefix, options, ls_remote_usage, PARSE_OPT_STOP_AT_NON_OPTION); dest = argv[0]; - UNLEAK(sorting); - if (argc > 1) { int i; CALLOC_ARRAY(pattern, argc); @@ -137,8 +136,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) item->symref = xstrdup_or_null(ref->symref); } - if (sorting) - ref_array_sort(sorting, &ref_array); + if (!list_empty(&sorting.list)) + ref_array_sort(&sorting, &ref_array); for (i = 0; i < ref_array.nr; i++) { const struct ref_array_item *ref = ref_array.items[i]; @@ -149,6 +148,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) } ref_array_clear(&ref_array); + free_ref_sorting_list(&sorting); if (transport_disconnect(transport)) return 1; return status; diff --git a/builtin/tag.c b/builtin/tag.c index 452558ec957..9d057b214e2 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -178,7 +178,7 @@ static const char tag_template_nocleanup[] = static int git_tag_config(const char *var, const char *value, void *cb) { int status; - struct ref_sorting **sorting_tail = (struct ref_sorting **)cb; + struct ref_sorting *sorting_tail = (struct ref_sorting *)cb; if (!strcmp(var, "tag.gpgsign")) { config_sign_tag = git_config_bool(var, value); @@ -438,7 +438,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix) struct ref_transaction *transaction; struct strbuf err = STRBUF_INIT; struct ref_filter filter; - static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; + struct ref_sorting sorting; struct ref_format format = REF_FORMAT_INIT; int icase = 0; int edit_flag = 0; @@ -472,7 +472,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix) OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")), OPT_MERGED(&filter, N_("print only tags that are merged")), OPT_NO_MERGED(&filter, N_("print only tags that are not merged")), - OPT_REF_SORT(sorting_tail), + OPT_REF_SORT(&sorting), { OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"), N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT, @@ -487,7 +487,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix) setup_ref_filter_porcelain_msg(); - git_config(git_tag_config, sorting_tail); + INIT_LIST_HEAD(&sorting.list); + git_config(git_tag_config, &sorting); memset(&opt, 0, sizeof(opt)); memset(&filter, 0, sizeof(filter)); @@ -526,9 +527,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix) die(_("--column and -n are incompatible")); colopts = 0; } - if (!sorting) - sorting = ref_default_sorting(); - ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); + if (list_empty(&sorting.list)) + ref_default_sorting(&sorting); + ref_sorting_set_sort_flags_all(&sorting, REF_SORTING_ICASE, icase); filter.ignore_case = icase; if (cmdmode == 'l') { int ret; @@ -539,11 +540,13 @@ int cmd_tag(int argc, const char **argv, const char *prefix) run_column_filter(colopts, &copts); } filter.name_patterns = argv; - ret = list_tags(&filter, sorting, &format); + ret = list_tags(&filter, &sorting, &format); if (column_active(colopts)) stop_column_filter(); return ret; } + free_ref_sorting_list(&sorting); + if (filter.lines != -1) die(_("-n option is only allowed in list mode")); if (filter.with_commit) diff --git a/ref-filter.c b/ref-filter.c index 93ce2a6ef2e..e9e3841c326 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2534,14 +2534,16 @@ static int compare_refs(const void *a_, const void *b_, void *ref_sorting) { struct ref_array_item *a = *((struct ref_array_item **)a_); struct ref_array_item *b = *((struct ref_array_item **)b_); - struct ref_sorting *s; + struct ref_sorting *s = ref_sorting; + struct list_head *pos; - for (s = ref_sorting; s; s = s->next) { - int cmp = cmp_ref_sorting(s, a, b); + list_for_each_prev(pos, &s->list) { + int cmp = cmp_ref_sorting(list_entry(pos, struct ref_sorting, list), + a, b); if (cmp) return cmp; } - s = ref_sorting; + return s && s->sort_flags & REF_SORTING_ICASE ? strcasecmp(a->refname, b->refname) : strcmp(a->refname, b->refname); @@ -2550,11 +2552,15 @@ static int compare_refs(const void *a_, const void *b_, void *ref_sorting) void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on) { - for (; sorting; sorting = sorting->next) { + struct list_head *pos; + struct ref_sorting *entry; + + list_for_each(pos, &sorting->list) { + entry = list_entry(pos, struct ref_sorting, list); if (on) - sorting->sort_flags |= mask; + entry->sort_flags |= mask; else - sorting->sort_flags &= ~mask; + entry->sort_flags &= ~mask; } } @@ -2667,24 +2673,32 @@ static int parse_sorting_atom(const char *atom) } /* If no sorting option is given, use refname to sort as default */ -struct ref_sorting *ref_default_sorting(void) +void ref_default_sorting(struct ref_sorting *sorting_list) { static const char cstr_name[] = "refname"; struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting)); - - sorting->next = NULL; + list_add_tail(&sorting->list, &sorting_list->list); sorting->atom = parse_sorting_atom(cstr_name); - return sorting; } -void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg) +void free_ref_sorting_list(struct ref_sorting *sorting_list) { + struct list_head *pos, *tmp; + struct ref_sorting *item; + + list_for_each_safe(pos, tmp, &sorting_list->list) { + item = list_entry(pos, struct ref_sorting, list); + list_del(pos); + free(item); + } +} + +void parse_ref_sorting(struct ref_sorting *sorting_list, const char *arg) { struct ref_sorting *s; CALLOC_ARRAY(s, 1); - s->next = *sorting_tail; - *sorting_tail = s; + list_add_tail(&s->list, &sorting_list->list); if (*arg == '-') { s->sort_flags |= REF_SORTING_REVERSE; diff --git a/ref-filter.h b/ref-filter.h index c15dee8d6b9..a502c2758e9 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -26,7 +26,7 @@ struct atom_value; struct ref_sorting { - struct ref_sorting *next; + struct list_head list; int atom; /* index into used_atom array (internal) */ enum { REF_SORTING_REVERSE = 1<<0, @@ -123,11 +123,13 @@ int format_ref_array_item(struct ref_array_item *info, struct strbuf *final_buf, struct strbuf *error_buf); /* Parse a single sort specifier and add it to the list */ -void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom); +void parse_ref_sorting(struct ref_sorting *sorting_list, const char *arg); /* Callback function for parsing the sort option */ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset); /* Default sort option based on refname */ -struct ref_sorting *ref_default_sorting(void); +void ref_default_sorting(struct ref_sorting *sorting_list); +/* Free all ref_sorting items in sorting list */ +void free_ref_sorting_list(struct ref_sorting *sorting_list); /* Function to parse --merged and --no-merged options */ int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset); /* Get the current HEAD's description */ From patchwork Wed Aug 25 09:08:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12456755 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.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 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 685D1C4320A for ; Wed, 25 Aug 2021 09:08:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 432EE61184 for ; Wed, 25 Aug 2021 09:08:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239535AbhHYJJm (ORCPT ); Wed, 25 Aug 2021 05:09:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233248AbhHYJJl (ORCPT ); Wed, 25 Aug 2021 05:09:41 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7508C061757 for ; Wed, 25 Aug 2021 02:08:55 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id m2so2876859wmm.0 for ; Wed, 25 Aug 2021 02:08:55 -0700 (PDT) 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=LjDYR1w4qdTIxeiUaAfZyvattLGrRqdvJL2R4qwJoiU=; b=q23b35qGhjxB//++6OjOcd7wyencxiYuJlIQ0ICZknT5c10IIHnVVlqXIfAak7Qir2 gORg4E1H5qc+iNvue/k9No/nn4YGmojpCsi1TKXrNeM5nfAPtHDs14MrUjVNuzT5SUm3 If93/w0cOUBaGZM7prfK3OloeQaBaSKC2VxygKfzelnJCte1P5tCP7ofB7LSlsjEdmbL wSn6cacGedwzdc2tnofNxt4Xfa4UjRUqgMCpT+5pgn6g4EZyASVw2I4YNyVmsh5oHB9Y 2ThOI/LGw2P+lAwBUjXfMjTr6TtnqzLBDxwJG76NmqHv0pHA71JgmyXxbPwi2KTAbwpf 2HJQ== 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=LjDYR1w4qdTIxeiUaAfZyvattLGrRqdvJL2R4qwJoiU=; b=SZNcfc0BXM7gTrtg2wBtv/rGT9ZdBTxxaRi27zM/9GtPoNY//V3aJIrSAtOJzL/bFg dLNNxZlyAMHghazRrmiC1hPwSyngku3LLKbTO37DEPgtQCg5D4QRNRanGaJtnPxvqwRI RcsGEAs3ag2hnCe9pkuL+sMGGIMOnZCdecCWY3YAg3ZxOOdXsUeMkTVXKQrqdHJ+EY+W UQc7aEbxz+wVcA0KNXpES3XQ+8AFuYVWNtT318KElMjSV1w6YDXijYQrd9Aq8LoqCN2r IlSIvgL6oGcpsoywm7NhoM5tcvIj/ZUVDyRriHaPfR4l5OIo0kUgzVgUDOrOPLf2Cq5e NBbQ== X-Gm-Message-State: AOAM5321CBC7A5HFNKYS9aiC0HVj2yKPnEFL98S8vmvzrBScKWbaJWa7 IMRRDAtvn35vr2rMRzXzlmBFF2Qz7B8= X-Google-Smtp-Source: ABdhPJz4YvhGKE2quZ8dHw0Wi6eQefXWmHMKVItnqU7S2lWXKeWtAkg5Bp8zpnGXaHX1xOtLs/EdxA== X-Received: by 2002:a1c:2209:: with SMTP id i9mr1389115wmi.92.1629882534580; Wed, 25 Aug 2021 02:08:54 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id v5sm20921101wru.37.2021.08.25.02.08.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 02:08:54 -0700 (PDT) Message-Id: <97d4a0b3793240b1bd5e3bed5318611064d9ccb7.1629882532.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 25 Aug 2021 09:08:46 +0000 Subject: [PATCH 2/8] [GSOC] ref-filter: let parse_sorting_atom() use real ref_format Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu parse_sorting_atom() currently uses a "dummy" ref_format, because it doesn't care about the details of real ref_format. But in fact, we expect to save some state in ref_format, and we need to check these statuses when executing some functions, e.g. in verify_ref_format() we check format->quote_style and format->need_color_reset_at_eol. And we intentionally want some global options (e.g. need_tagged) to be added to struct ref_format, so we need to care about ref_format details now, but the "dummy" ref_format prevents the truly ref_format to record them correctly. So let parse_sorting_atom() use the real ref_format, this requires us to separate the work of parsing the sort atom from parse_ref_sorting(). Add an "arg" member to struct ref_sorting to record the name of the sort atom, and parse_ref_sorting() only records the atom name, and introduce a parse_ref_sorting_list() to traverse the ref_sorting linked list and parsing each sort atom. This will help us add some global options to struct ref_format later. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- builtin/branch.c | 1 + builtin/for-each-ref.c | 1 + builtin/ls-remote.c | 6 ++++-- builtin/tag.c | 1 + ref-filter.c | 25 ++++++++++++++----------- ref-filter.h | 3 +++ 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index 72fafd301c5..3b21c2b54b9 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -751,6 +751,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) */ if (list_empty(&sorting.list)) ref_default_sorting(&sorting); + parse_ref_sorting_list(&sorting, &format); ref_sorting_set_sort_flags_all(&sorting, REF_SORTING_ICASE, icase); ref_sorting_set_sort_flags_all( &sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1); diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 3cd38eacd7c..8948b7bcd32 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -73,6 +73,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) if (list_empty(&sorting.list)) ref_default_sorting(&sorting); + parse_ref_sorting_list(&sorting, &format); ref_sorting_set_sort_flags_all(&sorting, REF_SORTING_ICASE, icase); filter.ignore_case = icase; diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c index 717ecde03d1..7fac069a29d 100644 --- a/builtin/ls-remote.c +++ b/builtin/ls-remote.c @@ -49,6 +49,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) TRANSPORT_LS_REFS_OPTIONS_INIT; int i; struct string_list server_options = STRING_LIST_INIT_DUP; + struct ref_format dummy = REF_FORMAT_INIT; struct remote *remote; struct transport *transport; @@ -136,9 +137,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) item->symref = xstrdup_or_null(ref->symref); } - if (!list_empty(&sorting.list)) + if (!list_empty(&sorting.list)) { + parse_ref_sorting_list(&sorting, &dummy); ref_array_sort(&sorting, &ref_array); - + } for (i = 0; i < ref_array.nr; i++) { const struct ref_array_item *ref = ref_array.items[i]; if (show_symref_target && ref->symref) diff --git a/builtin/tag.c b/builtin/tag.c index 9d057b214e2..a0d4aa775e3 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -529,6 +529,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix) } if (list_empty(&sorting.list)) ref_default_sorting(&sorting); + parse_ref_sorting_list(&sorting, &format); ref_sorting_set_sort_flags_all(&sorting, REF_SORTING_ICASE, icase); filter.ignore_case = icase; if (cmdmode == 'l') { diff --git a/ref-filter.c b/ref-filter.c index e9e3841c326..88a3e06eea9 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2656,30 +2656,33 @@ void pretty_print_ref(const char *name, const struct object_id *oid, free_array_item(ref_item); } -static int parse_sorting_atom(const char *atom) +static int parse_sorting_atom(struct ref_format *format, const char *atom) { - /* - * This parses an atom using a dummy ref_format, since we don't - * actually care about the formatting details. - */ - struct ref_format dummy = REF_FORMAT_INIT; const char *end = atom + strlen(atom); struct strbuf err = STRBUF_INIT; - int res = parse_ref_filter_atom(&dummy, atom, end, &err); + int res = parse_ref_filter_atom(format, atom, end, &err); if (res < 0) die("%s", err.buf); strbuf_release(&err); return res; } +void parse_ref_sorting_list(struct ref_sorting *sorting_list, struct ref_format *format) { + struct list_head *pos; + struct ref_sorting *entry; + + list_for_each(pos, &sorting_list->list) { + entry = list_entry(pos, struct ref_sorting, list); + entry->atom = parse_sorting_atom(format, entry->arg); + } +} + /* If no sorting option is given, use refname to sort as default */ void ref_default_sorting(struct ref_sorting *sorting_list) { - static const char cstr_name[] = "refname"; - struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting)); list_add_tail(&sorting->list, &sorting_list->list); - sorting->atom = parse_sorting_atom(cstr_name); + sorting->arg = "refname"; } void free_ref_sorting_list(struct ref_sorting *sorting_list) { @@ -2707,7 +2710,7 @@ void parse_ref_sorting(struct ref_sorting *sorting_list, const char *arg) if (skip_prefix(arg, "version:", &arg) || skip_prefix(arg, "v:", &arg)) s->sort_flags |= REF_SORTING_VERSION; - s->atom = parse_sorting_atom(arg); + s->arg = arg; } int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset) diff --git a/ref-filter.h b/ref-filter.h index a502c2758e9..8f602583788 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -28,6 +28,7 @@ struct atom_value; struct ref_sorting { struct list_head list; int atom; /* index into used_atom array (internal) */ + const char *arg; enum { REF_SORTING_REVERSE = 1<<0, REF_SORTING_ICASE = 1<<1, @@ -128,6 +129,8 @@ void parse_ref_sorting(struct ref_sorting *sorting_list, const char *arg); int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset); /* Default sort option based on refname */ void ref_default_sorting(struct ref_sorting *sorting_list); +/* Parse every sort atom of ref_sorting list */ +void parse_ref_sorting_list(struct ref_sorting *sorting_list, struct ref_format *format); /* Free all ref_sorting items in sorting list */ void free_ref_sorting_list(struct ref_sorting *sorting_list); /* Function to parse --merged and --no-merged options */ From patchwork Wed Aug 25 09:08:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12456757 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.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 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 2AD9BC4338F for ; Wed, 25 Aug 2021 09:09:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 103D361183 for ; Wed, 25 Aug 2021 09:09:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239548AbhHYJJn (ORCPT ); Wed, 25 Aug 2021 05:09:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235904AbhHYJJm (ORCPT ); Wed, 25 Aug 2021 05:09:42 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B0EDC061757 for ; Wed, 25 Aug 2021 02:08:56 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id q11so8336774wrr.9 for ; Wed, 25 Aug 2021 02:08:56 -0700 (PDT) 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=XSBf/aAHXqJhg9StH69xfoXXIZ1sjrcbPQnVZ7UmDzA=; b=sfQq9b1Rx3ftLWOBKerEa6Fo6eGSPzHLKu2meyJAoMuBoFO5OIlyfZMxCDf2PhuAhZ Qm3e1g0zhSRgkQUSMir89HtCFMD5P4L3bBE6WSjvivvGCr7j0P2eA3KJNjcufIO4oeu9 PwXBXrVY7drG4XpueWCatm1RZnsJTUnQ/NBFU1Ap1ZHQW51tKyNHx5GlIjs4EmJ4/+74 uEpHG/a87b6ZaQ3uGwIblMBGXjXQ7mIaq4Avealuz6CiZe2Rdbq2fq0gcnIGqjmuxMpt lDvaT/DNrvq0xABbtDVtzt8CcR4zxhABjDvnxcMl/LFAosvxQn74Au7AGcZkrRalVcVN Bg1w== 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=XSBf/aAHXqJhg9StH69xfoXXIZ1sjrcbPQnVZ7UmDzA=; b=tAWLaqKf8reuvkcOosCqaqkuUEkxNKdaJVIIHp8WYjSFAcHpqY/d4rdtpdbxgwXeL/ cBnc5HpoWolHs+coyueRpf4JB14BnN130ZTRrQyVG2XeuYgw632S2u3zu6CKtSBmB81W DPM5tDdYmsrCt1Zreo39r7haNOPe83r984LjHCXEuJFFiLvyE6eAP8u2MdhxI1xIyP7X FOUuoYTvOR0Bo3R78ULOKShXZihtW3kDCmIT5vIZYhYj7eINIKVIZ4tQw41NGXakE6vF jlWEC8/G80NzGk1jhxbMiOD/RuGXD/Kxy022m0PshsRCQg/Pag3vKmZqUxGyE4psFMWo l9nA== X-Gm-Message-State: AOAM531e3L0O7DS8/v1jmgSutCHr+zfO6gLbbKgDQxbghTeaUsuqsJuS 6YqJ/+MJEt3kX0M+1fm3y0xZdUru4Q0= X-Google-Smtp-Source: ABdhPJx3c/2JtMicPRjcGemOqLw7Di4RRl8V3uOKqiuss+CdAENsJgvH4RP7DRVt9GyzR2Vp2ZKCEw== X-Received: by 2002:a5d:508e:: with SMTP id a14mr24036494wrt.306.1629882535117; Wed, 25 Aug 2021 02:08:55 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f20sm523872wmb.32.2021.08.25.02.08.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 02:08:54 -0700 (PDT) Message-Id: <48458437c4a6e0633643f383fb0bf14e710bc9a9.1629882532.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 25 Aug 2021 09:08:47 +0000 Subject: [PATCH 3/8] [GSOC] ref-filter: add ref_format to ref_array_item Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu Since we expect to move some global options to ref_format, we may want check these global options in some functions like populate_value(), but the problem is that these functions cannot use ref_format, which means we may need a deep call chain to pass ref_format to them, this will be a bit cumbersome. We can use another method: Add the ref_format member to ref_array_item. Since we have successfully eliminated dummy ref_format, this can ensure that the same ref_array can use the same ref_format, so we pass ref_format through the call chain when ref_array_item is initialized, we also add ref_format member to ref_cbdata. This will help us move the global options to ref_format later. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- builtin/branch.c | 5 +++-- builtin/for-each-ref.c | 4 ++-- builtin/ls-remote.c | 2 +- builtin/tag.c | 4 ++-- ref-filter.c | 24 ++++++++++++++---------- ref-filter.h | 7 ++++--- 6 files changed, 26 insertions(+), 20 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index 3b21c2b54b9..4c6e7ef5f10 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -427,7 +427,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin memset(&array, 0, sizeof(array)); - filter_refs(&array, filter, filter->kind | FILTER_REFS_INCLUDE_BROKEN); + filter_refs(&array, filter, format, filter->kind | FILTER_REFS_INCLUDE_BROKEN); if (filter->verbose) maxwidth = calc_maxwidth(&array, strlen(remote_prefix)); @@ -444,7 +444,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin for (i = 0; i < array.nr; i++) { strbuf_reset(&err); strbuf_reset(&out); - if (format_ref_array_item(array.items[i], format, &out, &err)) + array.items[i]->format = format; + if (format_ref_array_item(array.items[i], &out, &err)) die("%s", err.buf); if (column_active(colopts)) { assert(!filter->verbose && "--column and --verbose are incompatible"); diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 8948b7bcd32..4a71caca929 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -79,7 +79,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) filter.name_patterns = argv; filter.match_as_path = 1; - filter_refs(&array, &filter, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN); + filter_refs(&array, &filter, &format, FILTER_REFS_ALL | FILTER_REFS_INCLUDE_BROKEN); ref_array_sort(&sorting, &array); if (!maxcount || array.nr < maxcount) @@ -87,7 +87,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) for (i = 0; i < maxcount; i++) { strbuf_reset(&err); strbuf_reset(&output); - if (format_ref_array_item(array.items[i], &format, &output, &err)) + if (format_ref_array_item(array.items[i], &output, &err)) die("%s", err.buf); fwrite(output.buf, 1, output.len, stdout); putchar('\n'); diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c index 7fac069a29d..2dd19a8583d 100644 --- a/builtin/ls-remote.c +++ b/builtin/ls-remote.c @@ -133,7 +133,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) continue; if (!tail_match(pattern, ref->name)) continue; - item = ref_array_push(&ref_array, ref->name, &ref->old_oid); + item = ref_array_push(&ref_array, ref->name, &ref->old_oid, &dummy); item->symref = xstrdup_or_null(ref->symref); } diff --git a/builtin/tag.c b/builtin/tag.c index a0d4aa775e3..9a66c4d37a5 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -62,13 +62,13 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, if (verify_ref_format(format)) die(_("unable to parse format string")); filter->with_commit_tag_algo = 1; - filter_refs(&array, filter, FILTER_REFS_TAGS); + filter_refs(&array, filter, format, FILTER_REFS_TAGS); ref_array_sort(sorting, &array); for (i = 0; i < array.nr; i++) { strbuf_reset(&output); strbuf_reset(&err); - if (format_ref_array_item(array.items[i], format, &output, &err)) + if (format_ref_array_item(array.items[i], &output, &err)) die("%s", err.buf); fwrite(output.buf, 1, output.len, stdout); putchar('\n'); diff --git a/ref-filter.c b/ref-filter.c index 88a3e06eea9..f4fa3102da6 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2167,22 +2167,24 @@ static const struct object_id *match_points_at(struct oid_array *points_at, * Callers can then fill in other struct members at their leisure. */ static struct ref_array_item *new_ref_array_item(const char *refname, - const struct object_id *oid) + const struct object_id *oid, + struct ref_format *format) { struct ref_array_item *ref; FLEX_ALLOC_STR(ref, refname, refname); oidcpy(&ref->objectname, oid); ref->rest = NULL; - + ref->format = format; return ref; } struct ref_array_item *ref_array_push(struct ref_array *array, const char *refname, - const struct object_id *oid) + const struct object_id *oid, + struct ref_format *format) { - struct ref_array_item *ref = new_ref_array_item(refname, oid); + struct ref_array_item *ref = new_ref_array_item(refname, oid, format); ALLOC_GROW(array->items, array->nr + 1, array->alloc); array->items[array->nr++] = ref; @@ -2226,6 +2228,7 @@ static int filter_ref_kind(struct ref_filter *filter, const char *refname) struct ref_filter_cbdata { struct ref_array *array; struct ref_filter *filter; + struct ref_format *format; struct contains_cache contains_cache; struct contains_cache no_contains_cache; }; @@ -2238,6 +2241,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid, { struct ref_filter_cbdata *ref_cbdata = cb_data; struct ref_filter *filter = ref_cbdata->filter; + struct ref_format *format = ref_cbdata->format; struct ref_array_item *ref; struct commit *commit = NULL; unsigned int kind; @@ -2288,7 +2292,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid, * to do its job and the resulting list may yet to be pruned * by maxcount logic. */ - ref = ref_array_push(ref_cbdata->array, refname, oid); + ref = ref_array_push(ref_cbdata->array, refname, oid, format); ref->commit = commit; ref->flag = flag; ref->kind = kind; @@ -2401,7 +2405,7 @@ static void reach_filter(struct ref_array *array, * as per the given ref_filter structure and finally store the * filtered refs in the ref_array structure. */ -int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type) +int filter_refs(struct ref_array *array, struct ref_filter *filter, struct ref_format *format, unsigned int type) { struct ref_filter_cbdata ref_cbdata; int ret = 0; @@ -2409,7 +2413,7 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int ref_cbdata.array = array; ref_cbdata.filter = filter; - + ref_cbdata.format = format; if (type & FILTER_REFS_INCLUDE_BROKEN) broken = 1; filter->kind = type & FILTER_REFS_KIND_MASK; @@ -2592,12 +2596,12 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting } int format_ref_array_item(struct ref_array_item *info, - struct ref_format *format, struct strbuf *final_buf, struct strbuf *error_buf) { const char *cp, *sp, *ep; struct ref_formatting_state state = REF_FORMATTING_STATE_INIT; + struct ref_format *format = info->format; state.quote_style = format->quote_style; push_stack_element(&state.stack); @@ -2644,9 +2648,9 @@ void pretty_print_ref(const char *name, const struct object_id *oid, struct strbuf output = STRBUF_INIT; struct strbuf err = STRBUF_INIT; - ref_item = new_ref_array_item(name, oid); + ref_item = new_ref_array_item(name, oid, format); ref_item->kind = ref_kind_from_refname(name); - if (format_ref_array_item(ref_item, format, &output, &err)) + if (format_ref_array_item(ref_item, &output, &err)) die("%s", err.buf); fwrite(output.buf, 1, output.len, stdout); putchar('\n'); diff --git a/ref-filter.h b/ref-filter.h index 8f602583788..e95c055fb86 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -45,6 +45,7 @@ struct ref_array_item { const char *symref; struct commit *commit; struct atom_value *value; + struct ref_format *format; char refname[FLEX_ARRAY]; }; @@ -109,7 +110,7 @@ struct ref_format { * as per the given ref_filter structure and finally store the * filtered refs in the ref_array structure. */ -int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type); +int filter_refs(struct ref_array *array, struct ref_filter *filter, struct ref_format *format, unsigned int type); /* Clear all memory allocated to ref_array */ void ref_array_clear(struct ref_array *array); /* Used to verify if the given format is correct and to parse out the used atoms */ @@ -120,7 +121,6 @@ void ref_array_sort(struct ref_sorting *sort, struct ref_array *array); void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on); /* Based on the given format and quote_style, fill the strbuf */ int format_ref_array_item(struct ref_array_item *info, - struct ref_format *format, struct strbuf *final_buf, struct strbuf *error_buf); /* Parse a single sort specifier and add it to the list */ @@ -153,6 +153,7 @@ void pretty_print_ref(const char *name, const struct object_id *oid, */ struct ref_array_item *ref_array_push(struct ref_array *array, const char *refname, - const struct object_id *oid); + const struct object_id *oid, + struct ref_format *format); #endif /* REF_FILTER_H */ From patchwork Wed Aug 25 09:08:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12456759 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.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 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 56819C432BE for ; Wed, 25 Aug 2021 09:09:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 383A2610CD for ; Wed, 25 Aug 2021 09:09:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239574AbhHYJJq (ORCPT ); Wed, 25 Aug 2021 05:09:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239515AbhHYJJm (ORCPT ); Wed, 25 Aug 2021 05:09:42 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11508C0613C1 for ; Wed, 25 Aug 2021 02:08:57 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id h4so5107221wro.7 for ; Wed, 25 Aug 2021 02:08:56 -0700 (PDT) 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=TxgZ6MG6jeycLd8+efH9Z8+HyeeiY47xyNkuHo2M6K8=; b=b2GykZJWgL2Y3O7GkPClNMpKM5xGq97GtG37+vdq+lE7a4FIbeXJjTPopRQ3L8Qt2z ercOqHUBbnjYp8VNUSJWHmsfZ7HBptZqxJ6Q/8wgYYR9TytzOoSLCD/nqm1WR+1zEkGn eQoTZfW13ddrLwHM60cY2dotNt9W6FmpmoIL6aA53ac0LrqMbfdXndr2f/Naf/t1K9vD OoJYiTktRxGuE70VLSBVx5GY2NJ5nDYJ90ke4uiNEt7I6ZUZbneCQLbTiLH1/cHKQZo/ Nzlgj9tVFIHJcD9EqSMa9ob1jq9bk2gRChGd+JaqYsElvKBkyv/Rwe7X1F+9IZOr39MR 9qMg== 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=TxgZ6MG6jeycLd8+efH9Z8+HyeeiY47xyNkuHo2M6K8=; b=bvT5aN1N8bYUR+Bc7exPnc3fTxYE2RYXoK6WeRY5sNNvgSlUB0/osiXIuGGZz5nNwm dBjM48vyAmhbBykyrvMoOUkk9/cMlR3l2eYD9c8Q0/jvKU1dQvKgxQmy2s9ovNJkaeWR pgkBqPbhZgLj6M/YKYKxXuDgz9+FLRrv1EXntjTleGpkA3GFTKUJezY7Ki5ZOcAEOIT3 ubhxap7mRvLxOKXq56jHUwwm5vIH+2aNJNmFQKjQY07dzRffzbypw0smTG14LskETCQA Q0u1WCUwVkDDwM5uWltpbi7Dc0PrdsA3pfiJoxWJopLKP/qkblylitvD/PrD4r90Wzw4 QcJw== X-Gm-Message-State: AOAM533Pnmd88mZ/0plulFVgNj8Fcx5F5SLE9SbeKHvmCIha0x0yfFCm VMYbrJDcT1Mfy110tklOkvjQeo1QsqQ= X-Google-Smtp-Source: ABdhPJyM+3rb/J1+IxH4lH5wyMNGQmVo5VkwQAWPkYbA/wNcMZ6RTRwGy5UXMFpZ7ns0GmsMwQnBCQ== X-Received: by 2002:a5d:55cd:: with SMTP id i13mr7221520wrw.366.1629882535742; Wed, 25 Aug 2021 02:08:55 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id v13sm13957231wrf.55.2021.08.25.02.08.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 02:08:55 -0700 (PDT) Message-Id: <54c95b0386ea7238b09931cecabc71dc1b9c0d21.1629882532.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 25 Aug 2021 09:08:48 +0000 Subject: [PATCH 4/8] [GSOC] ref-filter: clean up verify_ref_format() Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu Because we may not use the real ref_format when using parse_ref_filter_atom() before, we have to perform some atom checks in verify_ref_format() (instead of the parsing function of each atom), which is very inelegant. Since we have eliminated dummy ref_format, we can freely put some of the check logic from verify_ref_format() to their corresponding atom parsing functions. Introduce raw_atom_reject_quote_style() to check if there is any incompatibility quote style with the %(raw) atom. This can make the logic of the program more concise. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index f4fa3102da6..e42cd738c32 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -242,6 +242,7 @@ static int color_atom_parser(struct ref_format *format, struct used_atom *atom, */ if (!want_color(format->use_color)) color_parse("", atom->u.color); + format->need_color_reset_at_eol = !!strcmp(color_value, "reset"); return 0; } @@ -432,12 +433,29 @@ static int contents_atom_parser(struct ref_format *format, struct used_atom *ato return 0; } +static int raw_atom_reject_quote_style(int quote_style) { + switch (quote_style) { + case QUOTE_PYTHON: + case QUOTE_SHELL: + case QUOTE_TCL: + return 1; + case QUOTE_PERL: + case 0: + return 0; + default: + BUG("unknown quote style %d", quote_style); + } +} + static int raw_atom_parser(struct ref_format *format, struct used_atom *atom, const char *arg, struct strbuf *err) { - if (!arg) + if (!arg) { atom->u.raw_data.option = RAW_BARE; - else if (!strcmp(arg, "size")) + if (raw_atom_reject_quote_style(format->quote_style)) + return strbuf_addf_ret(err, -1, _("--format=%%(raw) cannot be used with" + "--python, --shell, --tcl")); + } else if (!strcmp(arg, "size")) atom->u.raw_data.option = RAW_LENGTH; else return strbuf_addf_ret(err, -1, _("unrecognized %%(raw) argument: %s"), arg); @@ -567,7 +585,7 @@ static int rest_atom_parser(struct ref_format *format, struct used_atom *atom, if (arg) return strbuf_addf_ret(err, -1, _("%%(rest) does not take arguments")); format->use_rest = 1; - return 0; + return strbuf_addf_ret(err, -1, _("this command reject atom %%(rest)")); } static int head_atom_parser(struct ref_format *format, struct used_atom *atom, @@ -1004,11 +1022,6 @@ static const char *find_next(const char *cp) return NULL; } -static int reject_atom(enum atom_type atom_type) -{ - return atom_type == ATOM_REST; -} - /* * Make sure the format string is well formed, and parse out * the used atoms. @@ -1020,7 +1033,7 @@ int verify_ref_format(struct ref_format *format) format->need_color_reset_at_eol = 0; for (cp = format->format; *cp && (sp = find_next(cp)); ) { struct strbuf err = STRBUF_INIT; - const char *color, *ep = strchr(sp, ')'); + const char *ep = strchr(sp, ')'); int at; if (!ep) @@ -1029,20 +1042,8 @@ int verify_ref_format(struct ref_format *format) at = parse_ref_filter_atom(format, sp + 2, ep, &err); if (at < 0) die("%s", err.buf); - if (reject_atom(used_atom[at].atom_type)) - die(_("this command reject atom %%(%.*s)"), (int)(ep - sp - 2), sp + 2); - - if ((format->quote_style == QUOTE_PYTHON || - format->quote_style == QUOTE_SHELL || - format->quote_style == QUOTE_TCL) && - used_atom[at].atom_type == ATOM_RAW && - used_atom[at].u.raw_data.option == RAW_BARE) - die(_("--format=%.*s cannot be used with" - "--python, --shell, --tcl"), (int)(ep - sp - 2), sp + 2); cp = ep + 1; - if (skip_prefix(used_atom[at].name, "color:", &color)) - format->need_color_reset_at_eol = !!strcmp(color, "reset"); strbuf_release(&err); } if (format->need_color_reset_at_eol && !want_color(format->use_color)) From patchwork Wed Aug 25 09:08:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12456761 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.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 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 4E9F4C4338F for ; Wed, 25 Aug 2021 09:09:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3705361183 for ; Wed, 25 Aug 2021 09:09:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239582AbhHYJJr (ORCPT ); Wed, 25 Aug 2021 05:09:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239540AbhHYJJn (ORCPT ); Wed, 25 Aug 2021 05:09:43 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9CBBC061757 for ; Wed, 25 Aug 2021 02:08:57 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id h13so35343024wrp.1 for ; Wed, 25 Aug 2021 02:08:57 -0700 (PDT) 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=0Gwt0qGbQp6JBOc3bncmKJ9Qs1Afo8N71MKuGNPRT8w=; b=YUf2yzcb0Z70bgQdVehn24iDORWOjR6glVuyGMrH29Pgb0k+4ZpXV6pjWTf/y0NuK3 5bCJ/ti54S+JTGWoafWtEf9IqaMCc8kWar8UI7BUVkVFGPaR4j1metCN3tt4NJjDL2CG 4sKIkBwVkWnp/55vBfBnv0a5TwoMSxC0OC7/QAoWmICdUXI0B7z0R4S11uqsUMPUcR5b 2tTgbn3QzxFxWtfZXwQnA1KXkRJvCtb2TGeKFJzoKJrmy8ucl23Kzs4vqhxih5YIIPvP 6FbF3omecd/iUe3W0wlXurCUxN2jUy+MINERj21u8jT61x4V/Lzmq/hEnkRGAueZ5qgM vMVw== 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=0Gwt0qGbQp6JBOc3bncmKJ9Qs1Afo8N71MKuGNPRT8w=; b=poiMxYH05lOK8kOMcDoouX1/g/Rg3KgSObYyUpqoGPyIzGEEyX17A80PF5waXu0l5/ ZWUPchKsO68WZ9ZldaCNiUq7H6kC8Ii5p3ChxNkNzhUPAnLQyk1+vBbG2zYLmG2ukPBx oo3BqAizExU3oFpDWp8xkunRMT5XWOFMz/d/5pA6OLErePhbWe2gft1DVYMsATnN4f6r DIajxUSzXMa0XaTC3stfAoNll+xeKEi3d4YRy6M1zm3TiS9s21fJWrFKO5uMaDZPLj+k Jhx9eObVEG6P5K0lJeXbgfWeEB2FBGO0SYutfNPfpawFrqvB++y202LV6p1z4v5aovpz RBUA== X-Gm-Message-State: AOAM532t9JDrDpphJuYH7ev7iC6C2+LfbzOdsO0NIUuMD9IgRK47JYYI ccJkAcH1INxzy6yqTZa1PhzG1XDVVgQ= X-Google-Smtp-Source: ABdhPJzmgQacazYe5mcwLTV/7DaKMvVq0eSwN/IUxZWIf2MxFlkhTSb222J8heYsV4RR4RvqyoBL7w== X-Received: by 2002:a5d:4605:: with SMTP id t5mr23363197wrq.276.1629882536396; Wed, 25 Aug 2021 02:08:56 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f20sm4606122wml.38.2021.08.25.02.08.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 02:08:56 -0700 (PDT) Message-Id: <40ab1faad39285ecd05ea57d6d2107e644d01bb7.1629882532.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 25 Aug 2021 09:08:49 +0000 Subject: [PATCH 5/8] [GSOC] ref-filter: introduce symref_atom_parser() Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu parse_ref_filter_atom() sets need_symref by checking whether the atom type is ATOM_SYMREF. When we are operating other atoms here, this step of checking is not necessary. So add the symref_atom_parser() specifically to parse the %(symref) atom, and set need_symref in it. This can make the program logic more concise. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index e42cd738c32..8dca273f720 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -495,6 +495,13 @@ static int person_email_atom_parser(struct ref_format *format, struct used_atom return 0; } +static int symref_atom_parser(struct ref_format *format, struct used_atom *atom, + const char *arg, struct strbuf *err) +{ + need_symref = 1; + return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err); +} + static int refname_atom_parser(struct ref_format *format, struct used_atom *atom, const char *arg, struct strbuf *err) { @@ -634,7 +641,7 @@ static struct { [ATOM_RAW] = { "raw", SOURCE_OBJ, FIELD_STR, raw_atom_parser }, [ATOM_UPSTREAM] = { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, [ATOM_PUSH] = { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, - [ATOM_SYMREF] = { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser }, + [ATOM_SYMREF] = { "symref", SOURCE_NONE, FIELD_STR, symref_atom_parser }, [ATOM_FLAG] = { "flag", SOURCE_NONE }, [ATOM_HEAD] = { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser }, [ATOM_COLOR] = { "color", SOURCE_NONE, FIELD_STR, color_atom_parser }, @@ -758,8 +765,6 @@ static int parse_ref_filter_atom(struct ref_format *format, return -1; if (*atom == '*') need_tagged = 1; - if (i == ATOM_SYMREF) - need_symref = 1; return at; } From patchwork Wed Aug 25 09:08:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12456763 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.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 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 BAE2DC4338F for ; Wed, 25 Aug 2021 09:09:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9F39561178 for ; Wed, 25 Aug 2021 09:09:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239604AbhHYJJv (ORCPT ); Wed, 25 Aug 2021 05:09:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239551AbhHYJJo (ORCPT ); Wed, 25 Aug 2021 05:09:44 -0400 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 70CE4C0613CF for ; Wed, 25 Aug 2021 02:08:58 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id m25-20020a7bcb99000000b002e751bcb5dbso4053518wmi.5 for ; Wed, 25 Aug 2021 02:08:58 -0700 (PDT) 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=lpQhvxlL0QcXxpdG+yUKOp+FwwcDEMJUHfGIZGJpidQ=; b=f2mdwxi3RsVFns5oGRe4H3X3pCK5b0s0dwGzUDBqNU+A7OAnaxlinVntJiOxX48upE /vJp7YX5AcuX7vk65lo6QBz6wc/qWWQx+zH2PChRfRmbCXLgnWR1TeoJI+uwmh/KG/xu EDoZGukU9myAhtdWW3oIGYWK7A9kHj9u2G1dZfIoIPw1xuJ8fheA5rVDGxl3dga3C02N NCR+T/pmBBjXg2yQ2TBRE99kB+ethD5L3gtNqk1gvelUSBuKOajsIJVjGi7rqayYh8f7 KvO3mDSSkUU9ITXtyelTRYi8k/7ja81VtlnFApRMpxS5/kVIuj3SpnqS8KtNEtUzDBQ6 URqA== 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=lpQhvxlL0QcXxpdG+yUKOp+FwwcDEMJUHfGIZGJpidQ=; b=QNHeKGVDioBqX08ISIZDmw5LZOIiTpBhZXwUAdP6AALPH7VZdKlV58CaY8gwcikuag nZCyhikD8d7+Z8+JoLiEdqc1LSo2CRfykbNmrQYX3gwKfB6PtYBtFMyNddxCQAXbJNqC 6ijjhRQaSahAMiQQKw1rO6ke56KmrPaeM6BXbdOq86QFVMTbhdmP4e8soK5+RbepAwCH SsW51NN5wf8yWBerdH9xUSy/xm5kt8xYBDIpcKVxtsdmeLhz6VxLUHFay0TmD57g57QV v8nh2gGgqZ1AItfqUwpWLBebYjctYsYkp/55vB1/rsM+dyG2KoTvnLeWWewT+c+3WOBD kG4Q== X-Gm-Message-State: AOAM533HGpoJUQAj7KJhVG+aH12/PzgdSudANZ4Df/e1I5HAZsmmRgtS BJ9BiLPpioZ3I3aYuzya2tucG6rrPSo= X-Google-Smtp-Source: ABdhPJzPhndRFOUO61RphkBwV3cB1WIuRMKU4b0U0z59lVufJgLwnnxpEStuWMB+K/+H0n742saKhA== X-Received: by 2002:a05:600c:4ba3:: with SMTP id e35mr8233681wmp.130.1629882537048; Wed, 25 Aug 2021 02:08:57 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o17sm4502822wmp.13.2021.08.25.02.08.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 02:08:56 -0700 (PDT) Message-Id: <63758a71e4001aa0a09d64a2c61f8c8b256c480a.1629882532.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 25 Aug 2021 09:08:50 +0000 Subject: [PATCH 6/8] [GSOC] ref-filter: remove grab_oid() function Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu Because "atom_type == ATOM_OBJECTNAME" implies the condition of `starts_with(name, "objectname")`, "atom_type == ATOM_TREE" implies the condition of `starts_with(name, "tree")`, so the check for `starts_with(name, field)` in grab_oid() is redundant. Remove the grab_oid() from ref-filter, to reduce repeated check. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 8dca273f720..67f62a3b591 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1071,16 +1071,6 @@ static const char *do_grab_oid(const char *field, const struct object_id *oid, } } -static int grab_oid(const char *name, const char *field, const struct object_id *oid, - struct atom_value *v, struct used_atom *atom) -{ - if (starts_with(name, field)) { - v->s = xstrdup(do_grab_oid(field, oid, atom)); - return 1; - } - return 0; -} - /* See grab_values */ static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi) { @@ -1106,8 +1096,9 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_ } } else if (atom_type == ATOM_DELTABASE) v->s = xstrdup(oid_to_hex(&oi->delta_base_oid)); - else if (atom_type == ATOM_OBJECTNAME && deref) - grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]); + else if (atom_type == ATOM_OBJECTNAME && deref) { + v->s = xstrdup(do_grab_oid("objectname", &oi->oid, &used_atom[i])); + } } } @@ -1148,9 +1139,10 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object continue; if (deref) name++; - if (atom_type == ATOM_TREE && - grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i])) + if (atom_type == ATOM_TREE) { + v->s = xstrdup(do_grab_oid("tree", get_commit_tree_oid(commit), &used_atom[i])); continue; + } if (atom_type == ATOM_NUMPARENT) { v->value = commit_list_count(commit->parents); v->s = xstrfmt("%lu", (unsigned long)v->value); @@ -1923,9 +1915,9 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) v->s = xstrdup(buf + 1); } continue; - } else if (!deref && atom_type == ATOM_OBJECTNAME && - grab_oid(name, "objectname", &ref->objectname, v, atom)) { - continue; + } else if (!deref && atom_type == ATOM_OBJECTNAME) { + v->s = xstrdup(do_grab_oid("objectname", &ref->objectname, atom)); + continue; } else if (atom_type == ATOM_HEAD) { if (atom->u.head && !strcmp(ref->refname, atom->u.head)) v->s = xstrdup("*"); From patchwork Wed Aug 25 09:08:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12456765 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.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 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 D6FD9C4320E for ; Wed, 25 Aug 2021 09:09:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B64ED61178 for ; Wed, 25 Aug 2021 09:09:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239588AbhHYJJy (ORCPT ); Wed, 25 Aug 2021 05:09:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239568AbhHYJJo (ORCPT ); Wed, 25 Aug 2021 05:09:44 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C223C061796 for ; Wed, 25 Aug 2021 02:08:59 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id x2-20020a1c7c02000000b002e6f1f69a1eso3733629wmc.5 for ; Wed, 25 Aug 2021 02:08:58 -0700 (PDT) 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=1xg5sUV/7LIe+qkxBKd+G0gs9ESX3DSJaNFTErbPFgI=; b=dXz4LaP9bjTIk5EbpKallmDFDzokCFE6Amz7nhYpNVeG8vAZXEWQU3pS4vD5vIXeYW ULVm/5UVjaBPCiC+wVELwjLtWKKB5EW3uohRM/wOc+vAqpC5SImTHPxMe62tWRuW6rID Un1VnM+fidHjJ2f4+IZdU/yECX2S5W8tYSet+ZTCQnnmrXggXbBCoAedJ/LGpNCYkWFy UoPfm4pHMVOK/lC/rQxIoYL3N11R0Ask/J1/iEMBqK0/g1vkgPpLTLuCO6NVQnLgGdLI goNi7h5Ux07LBwf5GePic363rVJbqB9W/5rhK9mznefpiL9JRygl/Jk/RmEwx6+qd1pE kN9A== 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=1xg5sUV/7LIe+qkxBKd+G0gs9ESX3DSJaNFTErbPFgI=; b=NYC6Ee7P5WoTDDfpcymwMmic/y/Uktsrhmr/1sf3yCQxMf5nJTEtbj2ttOGxkI7ANc T6HNUuk8OCwBP1FGy1/y1We5UwDadhKN/JJMpSXeBfAVtPhA+XtSxMEddX4Y7eObZ/Re NnucEDLFFSmYGBLq24XPUzh49JPbVvLMryKVUiUQf83TqxRSM0X+m6ViYNtVU6RGvLxn nczUq5LPFkt6+wXoi//Qvc0WQXG6n4G2Pq/ZNITo3+8f7FZ+JJ0gHd2Bi3Ytz3wPSxMC c5psTYdvKxFHTpH9zjUNK3f6gVBkftSe4s6kghKXzVfRZwC2gCm9Ak64Uzp+xc/wjujv DS1Q== X-Gm-Message-State: AOAM531A2gDnW/Nsf/l38AKRLhMLp7VNDmYrctbm9gT7fD/BRhBc16zt 9YulD86LgOROuu0zIzD2MQbouoU+FEM= X-Google-Smtp-Source: ABdhPJxAVapNKqaXAVtZFCsrIJWJ7zZXHkL1TyyTvoE9htfYBrUInE6LmtnUEF8TpikwW95hCVgDVg== X-Received: by 2002:a7b:c0cc:: with SMTP id s12mr8301676wmh.180.1629882537690; Wed, 25 Aug 2021 02:08:57 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j7sm4715594wmi.37.2021.08.25.02.08.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 02:08:57 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Wed, 25 Aug 2021 09:08:51 +0000 Subject: [PATCH 7/8] [GSOC] ref-filter: add deref member to struct used_atom Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu In the ref-filter logic, the dereference flag '*' is stored at the beginning of used_atom[i].name (e.g. '*objectname:short'), this leads us to make some check on used_atom[i].name[0] to know if the atom need to be dereferenced. This is not only cumbersome to use, but also poor in readability. So add `deref` flag to struct used_atom used to indicate whether the atom needs to be dereferenced and let used_atom record only the rest part (i.e. 'objectname:short'). This can make the logic of the program clearer and easy to use. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 55 ++++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 67f62a3b591..e4f96dad504 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -171,6 +171,7 @@ enum atom_type { * array. */ static struct used_atom { + unsigned int deref : 1; enum atom_type atom_type; const char *name; cmp_type type; @@ -319,7 +320,7 @@ static int objecttype_atom_parser(struct ref_format *format, struct used_atom *a { if (arg) return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments")); - if (*atom->name == '*') + if (atom->deref) oi_deref.info.typep = &oi_deref.type; else oi.info.typep = &oi.type; @@ -331,13 +332,13 @@ static int objectsize_atom_parser(struct ref_format *format, struct used_atom *a { if (!arg) { atom->u.objectsize.option = O_SIZE; - if (*atom->name == '*') + if (atom->deref) oi_deref.info.sizep = &oi_deref.size; else oi.info.sizep = &oi.size; } else if (!strcmp(arg, "disk")) { atom->u.objectsize.option = O_SIZE_DISK; - if (*atom->name == '*') + if (atom->deref) oi_deref.info.disk_sizep = &oi_deref.disk_size; else oi.info.disk_sizep = &oi.disk_size; @@ -351,7 +352,7 @@ static int deltabase_atom_parser(struct ref_format *format, struct used_atom *at { if (arg) return strbuf_addf_ret(err, -1, _("%%(deltabase) does not take arguments")); - if (*atom->name == '*') + if (atom->deref) oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid; else oi.info.delta_base_oid = &oi.delta_base_oid; @@ -697,10 +698,13 @@ static int parse_ref_filter_atom(struct ref_format *format, const char *sp; const char *arg; int i, at, atom_len; + int deref = 0; sp = atom; - if (*sp == '*' && sp < ep) + if (*sp == '*' && sp < ep) { sp++; /* deref */ + deref = 1; + } if (ep <= sp) return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"), (int)(ep-atom), atom); @@ -717,7 +721,7 @@ static int parse_ref_filter_atom(struct ref_format *format, /* Do we have the atom already used elsewhere? */ for (i = 0; i < used_atom_cnt; i++) { int len = strlen(used_atom[i].name); - if (len == ep - atom && !memcmp(used_atom[i].name, atom, len)) + if (len == ep - sp && !memcmp(used_atom[i].name, sp, len)) return i; } @@ -741,17 +745,18 @@ static int parse_ref_filter_atom(struct ref_format *format, used_atom_cnt++; REALLOC_ARRAY(used_atom, used_atom_cnt); used_atom[at].atom_type = i; - used_atom[at].name = xmemdupz(atom, ep - atom); + used_atom[at].deref = deref; + used_atom[at].name = xmemdupz(sp, ep - sp); used_atom[at].type = valid_atom[i].cmp_type; used_atom[at].source = valid_atom[i].source; if (used_atom[at].source == SOURCE_OBJ) { - if (*atom == '*') + if (deref) oi_deref.info.contentp = &oi_deref.content; else oi.info.contentp = &oi.content; } if (arg) { - arg = used_atom[at].name + (arg - atom) + 1; + arg = used_atom[at].name + (arg - sp) + 1; if (!*arg) { /* * Treat empty sub-arguments list as NULL (i.e., @@ -763,7 +768,7 @@ static int parse_ref_filter_atom(struct ref_format *format, memset(&used_atom[at].u, 0, sizeof(used_atom[at].u)); if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err)) return -1; - if (*atom == '*') + if (deref) need_tagged = 1; return at; } @@ -1077,13 +1082,10 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_ int i; for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i].name; enum atom_type atom_type = used_atom[i].atom_type; struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if (atom_type == ATOM_OBJECTTYPE) v->s = xstrdup(type_name(oi->type)); else if (atom_type == ATOM_OBJECTSIZE) { @@ -1109,13 +1111,10 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob struct tag *tag = (struct tag *) obj; for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i].name; enum atom_type atom_type = used_atom[i].atom_type; struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if (atom_type == ATOM_TAG) v->s = xstrdup(tag->tag); else if (atom_type == ATOM_TYPE && tag->tagged) @@ -1132,13 +1131,10 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object struct commit *commit = (struct commit *) obj; for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i].name; enum atom_type atom_type = used_atom[i].atom_type; struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if (atom_type == ATOM_TREE) { v->s = xstrdup(do_grab_oid("tree", get_commit_tree_oid(commit), &used_atom[i])); continue; @@ -1290,10 +1286,8 @@ static void grab_person(const char *who, struct atom_value *val, int deref, void for (i = 0; i < used_atom_cnt; i++) { const char *name = used_atom[i].name; struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if (strncmp(who, name, wholen)) continue; if (name[wholen] != 0 && @@ -1432,10 +1426,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp struct atom_value *v = &val[i]; enum atom_type atom_type = atom->atom_type; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if (atom_type == ATOM_RAW) { unsigned long buf_size = data->size; @@ -1840,7 +1832,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) enum atom_type atom_type = atom->atom_type; const char *name = used_atom[i].name; struct atom_value *v = &ref->value[i]; - int deref = 0; + int deref = atom->deref; const char *refname; struct branch *branch = NULL; @@ -1848,11 +1840,6 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) v->handler = append_atom; v->atom = atom; - if (*name == '*') { - deref = 1; - name++; - } - if (atom_type == ATOM_REFNAME) refname = get_refname(atom, ref); else if (atom_type == ATOM_WORKTREEPATH) { From patchwork Wed Aug 25 09:08:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12456767 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.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 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 3AFDBC4338F for ; Wed, 25 Aug 2021 09:09:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0FB5A610CD for ; Wed, 25 Aug 2021 09:09:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233248AbhHYJJz (ORCPT ); Wed, 25 Aug 2021 05:09:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239587AbhHYJJt (ORCPT ); Wed, 25 Aug 2021 05:09:49 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A572BC06179A for ; Wed, 25 Aug 2021 02:08:59 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id m2so2876980wmm.0 for ; Wed, 25 Aug 2021 02:08:59 -0700 (PDT) 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=LxBSAtjRmZzNVPjGW3EYc1+alzELuDnAH/h0kwGP7ZU=; b=HU3JYqCSUmnAdDWwcYU8eC6hXN+pFlL0hGh4wNvsIxYt11pG/5ioorEVtDIitENxdk cF9dH43lfxrJ8+B6kZmcGk74XfKLsY6hhGuW9PEHZ67Nj9j30gOoLrU8XOltdO9WGshE nKrOxF/vbMBIbjffWODGUh2xcmcqDsuw3ubJ/n198OW9Cm5t14jBY+foep3/JdKgYQQr EDiHRn/26dnLcKvgqBSp5UpeQ4SO3CXiaqi2qbWMOtvgpzHm6X1hRTYdeI7znrbfBBSq 4eJ1J7SwM+MWX4bZLiBKoCMMHq3LEXoPDsNcG7ueSpyi5SM+KXBbpdOV4msp8f0hrrLm j2IQ== 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=LxBSAtjRmZzNVPjGW3EYc1+alzELuDnAH/h0kwGP7ZU=; b=Y53ICKd/F/jFJwyJ51i43AxjsR6F8BEkCU3VoxLy9MUIz2M6gr7VK81K1Z9Af8gIr2 owkDBVIKHl/G+8hGPCYa4xL/Eafh0eT29kg6YpvuCyrOTCsvZqrCaa/MVItAkq2HGCR2 2xdex+l8DR4oQJF7FYm3sKKXhnTB7GleP7tW6BWbLYZnrXcfgKEmPSgrPsftaWPD+wDp JPDv3rgQKiOjJ1Ac5yHdvM3xfYK4ITm4J5j53DtKXcneEjGa4FSRlzq6WBhbV0cGraET rJqVlCHIn0iruhhDr6yssmPJd1VC1w/p/uGdUYXVjMd124DIb8DAiU6x35VUxZoqZA/O nuLg== X-Gm-Message-State: AOAM532apna0YbGeRZOhMyGl3UK7hNIyQ8DDmMVBh2YZ0VaW7hWBbWJ/ amf8oNbPcIbAUJ+SyZ+FDyrkAynNNsw= X-Google-Smtp-Source: ABdhPJwT+LZv7/ywT+AY6ZKd0KTmtgHdOxWTonQ7kFpyWP8wDb8McQU7+lGfVDHFbk+RAvtHx37+ZQ== X-Received: by 2002:a7b:cbc2:: with SMTP id n2mr8179242wmi.155.1629882538269; Wed, 25 Aug 2021 02:08:58 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id b24sm725242wmj.43.2021.08.25.02.08.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Aug 2021 02:08:57 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Wed, 25 Aug 2021 09:08:52 +0000 Subject: [PATCH 8/8] [GSOC] ref-filter: move need_symref, need_tagged into ref_format Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu There are many global options variables in ref-filter, e.g. need_tagged, need_symref, which may affect the reentrancy of the functions. Moving these variables into ref_format which can make the code cleaner. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 14 +++++++------- ref-filter.h | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index e4f96dad504..29be1e865d2 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -213,7 +213,7 @@ static struct used_atom { char *head; } u; } *used_atom; -static int used_atom_cnt, need_tagged, need_symref; +static int used_atom_cnt; /* * Expand string, append it to strbuf *sb, then return error code ret. @@ -499,7 +499,7 @@ static int person_email_atom_parser(struct ref_format *format, struct used_atom static int symref_atom_parser(struct ref_format *format, struct used_atom *atom, const char *arg, struct strbuf *err) { - need_symref = 1; + format->need_symref = 1; return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err); } @@ -769,7 +769,7 @@ static int parse_ref_filter_atom(struct ref_format *format, if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err)) return -1; if (deref) - need_tagged = 1; + format->need_tagged = 1; return at; } @@ -1816,10 +1816,10 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) struct object *obj; int i; struct object_info empty = OBJECT_INFO_INIT; - + struct ref_format *format = ref->format; CALLOC_ARRAY(ref->value, used_atom_cnt); - if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { + if (format->need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, NULL, NULL); if (!ref->symref) @@ -1958,7 +1958,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) oid_to_hex(&ref->objectname), ref->refname); } - if (need_tagged) + if (format->need_tagged) oi.info.contentp = &oi.content; if (!memcmp(&oi.info, &empty, sizeof(empty)) && !memcmp(&oi_deref.info, &empty, sizeof(empty))) @@ -1973,7 +1973,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) * If there is no atom that wants to know about tagged * object, we are done. */ - if (!need_tagged || (obj->type != OBJ_TAG)) + if (!format->need_tagged || (obj->type != OBJ_TAG)) return 0; /* diff --git a/ref-filter.h b/ref-filter.h index e95c055fb86..ec4cc1e5f9c 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -86,6 +86,8 @@ struct ref_format { /* Internal state to ref-filter */ int need_color_reset_at_eol; + int need_tagged; + int need_symref; }; #define REF_FORMAT_INIT { .use_color = -1 }