From patchwork Fri Aug 13 08:23:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12435127 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=-9.9 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,UNWANTED_LANGUAGE_BODY 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 E8C68C4338F for ; Fri, 13 Aug 2021 08:23:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CB9476056B for ; Fri, 13 Aug 2021 08:23:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238821AbhHMIYN (ORCPT ); Fri, 13 Aug 2021 04:24:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238823AbhHMIXy (ORCPT ); Fri, 13 Aug 2021 04:23:54 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7192DC0612AC for ; Fri, 13 Aug 2021 01:23:27 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id f5so12088582wrm.13 for ; Fri, 13 Aug 2021 01:23:27 -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=nB54XRvV0PUoeKaFKTkUHVNqT6ILEprINfQkt5qsgLY=; b=o2BffcwZC1NWJ9kDBE4NFNsjXHHpNmULwhv1xM4SapYtPApqog5CNU/WJOQCAqUH/j UrEgyyCgeQIzAcHvZiWEqNj//xS3GXY+Gq0nsc81aI93ArAIFnGoIvTyl8axlcUuMFxV hh209hrZ2/FdKs2UAlceggJmYtsjCZKI4mUbQLUvfxx3aQTc7IPfaInl41KP3UpIsykT FP4mZGC2bdalhZviq/g9O0OX/b6XuvA5bRanr/YR6qnz8JbafFUiDE9LBFLYqNfpBxxm 6fl2VUb17Z/U0QIoD1bY3pT0owqQkrWU+FI+MB0TWFLn7Y6eo7k66uX7VkMuOKkPcZzA SZgg== 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=nB54XRvV0PUoeKaFKTkUHVNqT6ILEprINfQkt5qsgLY=; b=X47LB9UVhR3aexpjFu3b1URs3//3jENLANLvSVfkOSYoYoMMIHjepOodH5usqdFPE7 P2a4wdoBdrukqMPyw2Trv7R46Eys0Sjmkm0yyfNXzoiX/+Eo82GX7c5knfhy9dgbpvLu 0o4LwkYYspH4VEwLJaZvPIGpSrttPQAZOTSw9ALsNKOzdGyms9Cf191g0gr3xuoSyFW4 VHZdMo6p8UmprMzHS/lrg5g3Scbfu6E5B6KZU1oZ9yNk47zXuvkKGWoJtgBddmJLKuWA RoXxENTmGg8Qq5RzPvoJe1p+ZVESjZPwshoh+thFYFWcc67SD8Zpjl2ZZfVYZd9LY5sm abyw== X-Gm-Message-State: AOAM5317Mwh8JZsbThCkzQqh3GtuLn4S6hi3tbmZcgnxnQdnh0qbfQlN 4apU1b3KFiGnnnXakb1k6D8duqt+gYU= X-Google-Smtp-Source: ABdhPJxSlGrD/fy3iKY/+T68VfGwCaBOaBPkES6SONunBvdMI99fsAZOqDIKWtR7BpQ1+dp6w7caHQ== X-Received: by 2002:adf:8102:: with SMTP id 2mr1598956wrm.89.1628843006055; Fri, 13 Aug 2021 01:23:26 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id s1sm676847wmh.46.2021.08.13.01.23.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Aug 2021 01:23:25 -0700 (PDT) Message-Id: <5b21b04683429fdf85b9df9723777fb6fad8ea7a.1628842990.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 13 Aug 2021 08:23:06 +0000 Subject: [PATCH 23/27] [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 original 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 we the atom needs to be dereferenced. 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 remaining part (i.e. 'objectname:short') This can simplify the logic of the program. 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 5e00dbd993a..bde1f3de9eb 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -174,6 +174,7 @@ enum atom_type { * array. */ static struct used_atom { + unsigned int deref : 1; enum atom_type atom_type; const char *name; cmp_type type; @@ -321,7 +322,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; @@ -333,13 +334,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; @@ -353,7 +354,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; @@ -675,10 +676,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); @@ -695,7 +699,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; } @@ -719,17 +723,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., @@ -741,7 +746,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; if (i == ATOM_SYMREF) need_symref = 1; @@ -1112,13 +1117,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) { @@ -1144,13 +1146,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) @@ -1167,13 +1166,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; @@ -1327,10 +1323,8 @@ static void grab_person(enum atom_type type, struct atom_value *val, int deref, 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 < type || atom_type > type + 3) && /* * For a tag or a commit object, if "creator" or "creatordate" is @@ -1446,10 +1440,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; @@ -1895,7 +1887,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; @@ -1903,11 +1895,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) {