From patchwork Thu Jan 12 09:11:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13097703 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8D4AC54EBC for ; Thu, 12 Jan 2023 09:20:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239822AbjALJUN (ORCPT ); Thu, 12 Jan 2023 04:20:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240256AbjALJTc (ORCPT ); Thu, 12 Jan 2023 04:19:32 -0500 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 234F452C59 for ; Thu, 12 Jan 2023 01:11:49 -0800 (PST) Received: by mail-pf1-x431.google.com with SMTP id a30so13335537pfr.6 for ; Thu, 12 Jan 2023 01:11:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0arj68Lj9hpU0ub5Z6NeHHKErrCvPeDV4kiysNNwpEA=; b=GeOU7NMMKh8eN6FtdvaacqTnF3nJSKwVWiwVUPMRy+jaRq9QJ146faExmnIAJ6BJYj KnNg+98/MLzikDdy/+TGkoRb07dPDAYiSvcrcQEEij4H2qNs30LE0v3uWOV5+rcs2Tc1 Y6hArJpQL37cALH/Svh+8WWMUgWFUj5YKMt1RDbjXpqMZkQWbdzDCiGXtzLcaG0LbuLY efoNQuYYFEa5Nqv+QYaN2kwCI95d8D+OvYQiROC1tpVOGLNU7b4qv8wZdmi5tyNh8fCL /S5+mm1kHxebYZ8z7kL4Bit/32KsqYLGoUhFvyPFpp1FnJeDPSnakCAl1fV/TpBn7Lo6 Leyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0arj68Lj9hpU0ub5Z6NeHHKErrCvPeDV4kiysNNwpEA=; b=ikM18HoxqNlLLHBMWTKsHqTYT97Avqu6OFw5sqNG/SsXhpeOft8GoR9g8eYtp2340R POS14l9YmqVwejCNw97l050apacBnN6onyPT1HWA2PAuXCbkhJuxDYceA0Dx4VrwwgKn MrNC6hRhTGEFQT6qcLBB7xd1BDu6ChBWfxOl2EU5/giyCg7/N7tCQT62JdBRMqxTwHiU 9sHfztoJhAl0xXKTIH42eC6A2Yf+tCYEIK6KFQga1yPa5LUcIvWtC1OyLa+CkCyLGYT3 bIVoUfDJBcHhbnAIwSk5dUH97fMvOrUxQqRsHTeo+fUd/eptTQfBbL/iWoT6+P+5ZiPL f/6g== X-Gm-Message-State: AFqh2kpRldi/3yJyhUxZMRqzK+UDx5cX4o9x2j/hHNK6r0P75hYeX2+s umobIRusa/sOg0fy2PicbzPWyXuxLZQuEcyz X-Google-Smtp-Source: AMrXdXsuSJWiTI/Daje0kcd3mTpUBpfuNCctu6XdcA6iKWTflca9CPXP3Zrhfet7JpYvdPb5il+NJQ== X-Received: by 2002:aa7:850e:0:b0:583:39ca:82f3 with SMTP id v14-20020aa7850e000000b0058339ca82f3mr23865900pfn.18.1673514708255; Thu, 12 Jan 2023 01:11:48 -0800 (PST) Received: from localhost.localdomain ([47.246.101.48]) by smtp.gmail.com with ESMTPSA id d126-20020a623684000000b0058426d9db66sm11257069pfa.218.2023.01.12.01.11.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Jan 2023 01:11:47 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, dyroneteng@gmail.com, tenglong.tl@alibaba-inc.com, Teng Long Subject: [PATCH 1/6] ls-tree: don't use "show_tree_data" for "fast" callbacks Date: Thu, 12 Jan 2023 17:11:30 +0800 Message-Id: <20230112091135.20050-2-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.39.0.98.g174eaa53973.dirty In-Reply-To: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> References: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason As noted in [1] the code that made it in as part of 9c4d58ff2c3 (ls-tree: split up "fast path" callbacks, 2022-03-23) was a "maybe a good idea, maybe not" RFC-quality patch. I hadn't looked very carefully at the resulting patterns. The implementation shared the "struct show_tree_data data", which was introduced in e81517155e0 (ls-tree: introduce struct "show_tree_data", 2022-03-23) both for use in 455923e0a15 (ls-tree: introduce "--format" option, 2022-03-23), and because the "fat" callback hadn't been split up as 9c4d58ff2c3 did. Now that that's been done we can see that most of what show_tree_common() was doing could be done lazily by the callbacks themselves, who in the pre-image were often using an odd mis-match of their own arguments and those same arguments stuck into the "data" structure. Let's also have the callers initialize the "type", rather than grabbing it from the "data" structure afterwards. 1. https://lore.kernel.org/git/cover-0.7-00000000000-20220310T134811Z-avarab@gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index c3ea09281af..cbb6782f9a5 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -173,19 +173,11 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, return recurse; } -static int show_tree_common(struct show_tree_data *data, int *recurse, - const struct object_id *oid, struct strbuf *base, - const char *pathname, unsigned mode) +static int show_tree_common(int *recurse, struct strbuf *base, + const char *pathname, enum object_type type) { - enum object_type type = object_type(mode); int ret = -1; - *recurse = 0; - data->mode = mode; - data->type = type; - data->oid = oid; - data->pathname = pathname; - data->base = base; if (type == OBJ_BLOB) { if (ls_options & LS_TREE_ONLY) @@ -217,15 +209,15 @@ static int show_tree_default(const struct object_id *oid, struct strbuf *base, { int early; int recurse; - struct show_tree_data data = { 0 }; + enum object_type type = object_type(mode); - early = show_tree_common(&data, &recurse, oid, base, pathname, mode); + early = show_tree_common(&recurse, base, pathname, type); if (early >= 0) return early; - printf("%06o %s %s\t", data.mode, type_name(data.type), - find_unique_abbrev(data.oid, abbrev)); - show_tree_common_default_long(base, pathname, data.base->len); + printf("%06o %s %s\t", mode, type_name(object_type(mode)), + find_unique_abbrev(oid, abbrev)); + show_tree_common_default_long(base, pathname, base->len); return recurse; } @@ -235,16 +227,16 @@ static int show_tree_long(const struct object_id *oid, struct strbuf *base, { int early; int recurse; - struct show_tree_data data = { 0 }; char size_text[24]; + enum object_type type = object_type(mode); - early = show_tree_common(&data, &recurse, oid, base, pathname, mode); + early = show_tree_common(&recurse, base, pathname, type); if (early >= 0) return early; - if (data.type == OBJ_BLOB) { + if (type == OBJ_BLOB) { unsigned long size; - if (oid_object_info(the_repository, data.oid, &size) == OBJ_BAD) + if (oid_object_info(the_repository, oid, &size) == OBJ_BAD) xsnprintf(size_text, sizeof(size_text), "BAD"); else xsnprintf(size_text, sizeof(size_text), @@ -253,9 +245,9 @@ static int show_tree_long(const struct object_id *oid, struct strbuf *base, xsnprintf(size_text, sizeof(size_text), "-"); } - printf("%06o %s %s %7s\t", data.mode, type_name(data.type), - find_unique_abbrev(data.oid, abbrev), size_text); - show_tree_common_default_long(base, pathname, data.base->len); + printf("%06o %s %s %7s\t", mode, type_name(type), + find_unique_abbrev(oid, abbrev), size_text); + show_tree_common_default_long(base, pathname, base->len); return recurse; } @@ -266,9 +258,9 @@ static int show_tree_name_only(const struct object_id *oid, struct strbuf *base, int early; int recurse; const size_t baselen = base->len; - struct show_tree_data data = { 0 }; + enum object_type type = object_type(mode); - early = show_tree_common(&data, &recurse, oid, base, pathname, mode); + early = show_tree_common(&recurse, base, pathname, type); if (early >= 0) return early; @@ -286,9 +278,9 @@ static int show_tree_object(const struct object_id *oid, struct strbuf *base, { int early; int recurse; - struct show_tree_data data = { 0 }; + enum object_type type = object_type(mode); - early = show_tree_common(&data, &recurse, oid, base, pathname, mode); + early = show_tree_common(&recurse, base, pathname, type); if (early >= 0) return early; From patchwork Thu Jan 12 09:11:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13097704 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B139CC54EBC for ; Thu, 12 Jan 2023 09:20:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239889AbjALJUb (ORCPT ); Thu, 12 Jan 2023 04:20:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240283AbjALJTi (ORCPT ); Thu, 12 Jan 2023 04:19:38 -0500 Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2576652772 for ; Thu, 12 Jan 2023 01:11:52 -0800 (PST) Received: by mail-pg1-x529.google.com with SMTP id v3so12352158pgh.4 for ; Thu, 12 Jan 2023 01:11:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=i1qlnOeqEVaiMKSbmI5sjDPgEr5m39gVYOb9vyTkwAY=; b=aBW40ZsqzgdI+YNdH1NL3J6P/tHa9/yJd1o8Hvzuu2rmWavxKrxc5XSiAcMIpP807o GpPhe5yWfBVUqW98fLHtdxArDqHjnt0fbIt80f2WHkozugG/2JQRkKx/7XLLL/clZ2LR qHhCmI3l5qV50jbsiwK943pbXidxkaf1TQ0TXJFLVNlw8bawqEv/4JuT92h6WeagibK7 vFWPue9IG4MQQtjHRnS3kvtrz08GES5bib6F6q8DA7fh9a/ZRiCT/usNppgoP2o5VIYL M1XXkwsmcWYCwrDLFP8lKvwxr8/3L1/u0K5I+iO5lbsfuTZahARKy9nfuP/q51GluW0C RQbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i1qlnOeqEVaiMKSbmI5sjDPgEr5m39gVYOb9vyTkwAY=; b=JI5F38st5NzLufAulTxZaQdyqfVUZAQtQYNyCRtD+P0DySatRXVqgu6VKjljg7RqZK /pIoBx9OW9FEqJN2mGRvQ4aPD2FGRIIjUAj141ekC14sXw9B+JETAD4LtLNVJ8SNq98c GFneWbTDRBMpOqzsUWIBpQyLWP05koAwag0mBARi4Nck/OEilBpbfHTxhu6SQlGR9Jfr jmsIY8BDiPCY/WZslDp+n8I96WOvr+gIg3NG6ph3x3ewKAGJY6TxCn79LSAI8MKeMjiW iPl62ywjfFtrEl2xbf5KGP6lYN5kkRht5tKjqsGRQnmjb6Ho0LF5/UomRpnwbxZr5VTR PwWg== X-Gm-Message-State: AFqh2kqqvmqaKzzeqLxxtQ8vXEEuFrkFEABR0H0Z8OR9LA/mkvVHtRV7 3uzwg97e41COMh7mg19W6fPoedPkh6NsHwlb X-Google-Smtp-Source: AMrXdXs2ZxiJTLrbiT/h+iL0hq1lxbEqJAfeF/UU5AVfv1Q0PRWXpVfNLC77qQdgUrS2BOxM+sqeKQ== X-Received: by 2002:a62:1d0f:0:b0:581:b3b3:7717 with SMTP id d15-20020a621d0f000000b00581b3b37717mr46866737pfd.26.1673514711226; Thu, 12 Jan 2023 01:11:51 -0800 (PST) Received: from localhost.localdomain ([47.246.101.48]) by smtp.gmail.com with ESMTPSA id d126-20020a623684000000b0058426d9db66sm11257069pfa.218.2023.01.12.01.11.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Jan 2023 01:11:50 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, dyroneteng@gmail.com, tenglong.tl@alibaba-inc.com Subject: [PATCH 2/6] ls-tree: use a "struct options" Date: Thu, 12 Jan 2023 17:11:31 +0800 Message-Id: <20230112091135.20050-3-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.39.0.98.g174eaa53973.dirty In-Reply-To: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> References: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason As a first step towards being able to turn this code into an API some day let's change the "static" options in builtin/ls-tree.c into a "struct ls_tree_options" that can be constructed dynamically without the help of parse_options(). Because we're now using non-static variables for this we'll need to clear_pathspec() at the end of cmd_ls_tree(), least various tests start failing under SANITIZE=leak. The memory leak was already there before, now it's just being brought to the surface. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 203 ++++++++++++++++++++++++++-------------------- 1 file changed, 116 insertions(+), 87 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index cbb6782f9a5..54f7b604cb7 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -14,37 +14,11 @@ #include "parse-options.h" #include "pathspec.h" -static int line_termination = '\n'; -#define LS_RECURSIVE 1 -#define LS_TREE_ONLY (1 << 1) -#define LS_SHOW_TREES (1 << 2) -static int abbrev; -static int ls_options; -static struct pathspec pathspec; -static int chomp_prefix; -static const char *ls_tree_prefix; -static const char *format; -struct show_tree_data { - unsigned mode; - enum object_type type; - const struct object_id *oid; - const char *pathname; - struct strbuf *base; -}; - static const char * const ls_tree_usage[] = { N_("git ls-tree [] [...]"), NULL }; -static enum ls_tree_cmdmode { - MODE_DEFAULT = 0, - MODE_LONG, - MODE_NAME_ONLY, - MODE_NAME_STATUS, - MODE_OBJECT_ONLY, -} cmdmode; - static void expand_objectsize(struct strbuf *line, const struct object_id *oid, const enum object_type type, unsigned int padded) { @@ -64,10 +38,39 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid, } } +struct ls_tree_options { + int line_termination; + int abbrev; + enum ls_tree_path_options { + LS_RECURSIVE = 1 << 0, + LS_TREE_ONLY = 1 << 1, + LS_SHOW_TREES = 1 << 2, + } ls_options; + struct pathspec pathspec; + int chomp_prefix; + const char *ls_tree_prefix; + const char *format; +}; + +struct show_tree_data { + unsigned mode; + enum object_type type; + const struct object_id *oid; + const char *pathname; + struct strbuf *base; +}; + +struct show_tree_data_cb { + struct ls_tree_options *options; + struct show_tree_data *data; +}; + static size_t expand_show_tree(struct strbuf *sb, const char *start, void *context) { - struct show_tree_data *data = context; + struct show_tree_data_cb *wrapper = context; + struct ls_tree_options *options = wrapper->options; + struct show_tree_data *data = wrapper->data; const char *end; const char *p; unsigned int errlen; @@ -92,10 +95,10 @@ static size_t expand_show_tree(struct strbuf *sb, const char *start, } else if (skip_prefix(start, "(objectsize)", &p)) { expand_objectsize(sb, data->oid, data->type, 0); } else if (skip_prefix(start, "(objectname)", &p)) { - strbuf_add_unique_abbrev(sb, data->oid, abbrev); + strbuf_add_unique_abbrev(sb, data->oid, options->abbrev); } else if (skip_prefix(start, "(path)", &p)) { const char *name = data->base->buf; - const char *prefix = chomp_prefix ? ls_tree_prefix : NULL; + const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL; struct strbuf quoted = STRBUF_INIT; struct strbuf sbuf = STRBUF_INIT; strbuf_addstr(data->base, data->pathname); @@ -111,18 +114,19 @@ static size_t expand_show_tree(struct strbuf *sb, const char *start, return len; } -static int show_recursive(const char *base, size_t baselen, const char *pathname) +static int show_recursive(struct ls_tree_options *options, const char *base, + size_t baselen, const char *pathname) { int i; - if (ls_options & LS_RECURSIVE) + if (options->ls_options & LS_RECURSIVE) return 1; - if (!pathspec.nr) + if (!options->pathspec.nr) return 0; - for (i = 0; i < pathspec.nr; i++) { - const char *spec = pathspec.items[i].match; + for (i = 0; i < options->pathspec.nr; i++) { + const char *spec = options->pathspec.items[i].match; size_t len, speclen; if (strncmp(base, spec, baselen)) @@ -142,13 +146,13 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname } static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, - const char *pathname, unsigned mode, void *context UNUSED) + const char *pathname, unsigned mode, void *context) { + struct ls_tree_options *options = context; size_t baselen; int recurse = 0; struct strbuf sb = STRBUF_INIT; enum object_type type = object_type(mode); - struct show_tree_data data = { .mode = mode, .type = type, @@ -156,81 +160,89 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, .pathname = pathname, .base = base, }; + struct show_tree_data_cb cb_data = { + .data = &data, + .options = options, + }; - if (type == OBJ_TREE && show_recursive(base->buf, base->len, pathname)) + if (type == OBJ_TREE && show_recursive(options, base->buf, base->len, pathname)) recurse = READ_TREE_RECURSIVE; - if (type == OBJ_TREE && recurse && !(ls_options & LS_SHOW_TREES)) + if (type == OBJ_TREE && recurse && !(options->ls_options & LS_SHOW_TREES)) return recurse; - if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) + if (type == OBJ_BLOB && (options->ls_options & LS_TREE_ONLY)) return 0; baselen = base->len; - strbuf_expand(&sb, format, expand_show_tree, &data); - strbuf_addch(&sb, line_termination); + strbuf_expand(&sb, options->format, expand_show_tree, &cb_data); + strbuf_addch(&sb, options->line_termination); fwrite(sb.buf, sb.len, 1, stdout); strbuf_release(&sb); strbuf_setlen(base, baselen); return recurse; } -static int show_tree_common(int *recurse, struct strbuf *base, - const char *pathname, enum object_type type) +static int show_tree_common(struct ls_tree_options *options, int *recurse, + struct strbuf *base, const char *pathname, + enum object_type type) { int ret = -1; *recurse = 0; if (type == OBJ_BLOB) { - if (ls_options & LS_TREE_ONLY) + if (options->ls_options & LS_TREE_ONLY) ret = 0; } else if (type == OBJ_TREE && - show_recursive(base->buf, base->len, pathname)) { + show_recursive(options, base->buf, base->len, pathname)) { *recurse = READ_TREE_RECURSIVE; - if (!(ls_options & LS_SHOW_TREES)) + if (!(options->ls_options & LS_SHOW_TREES)) ret = *recurse; } return ret; } -static void show_tree_common_default_long(struct strbuf *base, +static void show_tree_common_default_long(struct ls_tree_options *options, + struct strbuf *base, const char *pathname, const size_t baselen) { strbuf_addstr(base, pathname); write_name_quoted_relative(base->buf, - chomp_prefix ? ls_tree_prefix : NULL, stdout, - line_termination); + options->chomp_prefix ? options->ls_tree_prefix : NULL, stdout, + options->line_termination); strbuf_setlen(base, baselen); } static int show_tree_default(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, - void *context UNUSED) + void *context) { + struct ls_tree_options *options = context; int early; int recurse; enum object_type type = object_type(mode); - early = show_tree_common(&recurse, base, pathname, type); + early = show_tree_common(options, &recurse, base, pathname, type); if (early >= 0) return early; printf("%06o %s %s\t", mode, type_name(object_type(mode)), - find_unique_abbrev(oid, abbrev)); - show_tree_common_default_long(base, pathname, base->len); + find_unique_abbrev(oid, options->abbrev)); + show_tree_common_default_long(options, base, pathname, base->len); return recurse; } static int show_tree_long(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, - void *context UNUSED) + void *context) { + struct ls_tree_options *options = context; int early; int recurse; char size_text[24]; enum object_type type = object_type(mode); - early = show_tree_common(&recurse, base, pathname, type); + early = show_tree_common(options, &recurse, base, pathname, type); if (early >= 0) return early; @@ -246,48 +258,58 @@ static int show_tree_long(const struct object_id *oid, struct strbuf *base, } printf("%06o %s %s %7s\t", mode, type_name(type), - find_unique_abbrev(oid, abbrev), size_text); - show_tree_common_default_long(base, pathname, base->len); + find_unique_abbrev(oid, options->abbrev), size_text); + show_tree_common_default_long(options, base, pathname, base->len); return recurse; } static int show_tree_name_only(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, - void *context UNUSED) + void *context) { + struct ls_tree_options *options = context; int early; int recurse; const size_t baselen = base->len; enum object_type type = object_type(mode); - early = show_tree_common(&recurse, base, pathname, type); + early = show_tree_common(options, &recurse, base, pathname, type); if (early >= 0) return early; strbuf_addstr(base, pathname); write_name_quoted_relative(base->buf, - chomp_prefix ? ls_tree_prefix : NULL, - stdout, line_termination); + options->chomp_prefix ? options->ls_tree_prefix : NULL, + stdout, options->line_termination); strbuf_setlen(base, baselen); return recurse; } static int show_tree_object(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, - void *context UNUSED) + void *context) { + struct ls_tree_options *options = context; int early; int recurse; enum object_type type = object_type(mode); - early = show_tree_common(&recurse, base, pathname, type); + early = show_tree_common(options, &recurse, base, pathname, type); if (early >= 0) return early; - printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination); + printf("%s%c", find_unique_abbrev(oid, options->abbrev), options->line_termination); return recurse; } +enum ls_tree_cmdmode { + MODE_DEFAULT = 0, + MODE_LONG, + MODE_NAME_ONLY, + MODE_NAME_STATUS, + MODE_OBJECT_ONLY, +}; + struct ls_tree_cmdmode_to_fmt { enum ls_tree_cmdmode mode; const char *const fmt; @@ -327,14 +349,18 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) struct tree *tree; int i, full_tree = 0; read_tree_fn_t fn = NULL; + enum ls_tree_cmdmode cmdmode = MODE_DEFAULT; + struct ls_tree_options options = { + .line_termination = '\n', + }; const struct option ls_tree_options[] = { - OPT_BIT('d', NULL, &ls_options, N_("only show trees"), + OPT_BIT('d', NULL, &options.ls_options, N_("only show trees"), LS_TREE_ONLY), - OPT_BIT('r', NULL, &ls_options, N_("recurse into subtrees"), + OPT_BIT('r', NULL, &options.ls_options, N_("recurse into subtrees"), LS_RECURSIVE), - OPT_BIT('t', NULL, &ls_options, N_("show trees when recursing"), + OPT_BIT('t', NULL, &options.ls_options, N_("show trees when recursing"), LS_SHOW_TREES), - OPT_SET_INT('z', NULL, &line_termination, + OPT_SET_INT('z', NULL, &options.line_termination, N_("terminate entries with NUL byte"), 0), OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"), MODE_LONG), @@ -344,29 +370,30 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) MODE_NAME_STATUS), OPT_CMDMODE(0, "object-only", &cmdmode, N_("list only objects"), MODE_OBJECT_ONLY), - OPT_SET_INT(0, "full-name", &chomp_prefix, + OPT_SET_INT(0, "full-name", &options.chomp_prefix, N_("use full path names"), 0), OPT_BOOL(0, "full-tree", &full_tree, N_("list entire tree; not just current directory " "(implies --full-name)")), - OPT_STRING_F(0, "format", &format, N_("format"), + OPT_STRING_F(0, "format", &options.format, N_("format"), N_("format to use for the output"), PARSE_OPT_NONEG), - OPT__ABBREV(&abbrev), + OPT__ABBREV(&options.abbrev), OPT_END() }; struct ls_tree_cmdmode_to_fmt *m2f = ls_tree_cmdmode_format; + int ret; git_config(git_default_config, NULL); - ls_tree_prefix = prefix; + options.ls_tree_prefix = prefix; if (prefix) - chomp_prefix = strlen(prefix); + options.chomp_prefix = strlen(prefix); argc = parse_options(argc, argv, prefix, ls_tree_options, ls_tree_usage, 0); if (full_tree) { - ls_tree_prefix = prefix = NULL; - chomp_prefix = 0; + options.ls_tree_prefix = prefix = NULL; + options.chomp_prefix = 0; } /* * We wanted to detect conflicts between --name-only and @@ -378,10 +405,10 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) /* -d -r should imply -t, but -d by itself should not have to. */ if ( (LS_TREE_ONLY|LS_RECURSIVE) == - ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options)) - ls_options |= LS_SHOW_TREES; + ((LS_TREE_ONLY|LS_RECURSIVE) & options.ls_options)) + options.ls_options |= LS_SHOW_TREES; - if (format && cmdmode) + if (options.format && cmdmode) usage_msg_opt( _("--format can't be combined with other format-altering options"), ls_tree_usage, ls_tree_options); @@ -396,13 +423,13 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) * cannot be lifted until it is converted to use * match_pathspec() or tree_entry_interesting() */ - parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC & - ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL), + parse_pathspec(&options.pathspec, PATHSPEC_ALL_MAGIC & + ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL), PATHSPEC_PREFER_CWD, prefix, argv + 1); - for (i = 0; i < pathspec.nr; i++) - pathspec.items[i].nowildcard_len = pathspec.items[i].len; - pathspec.has_wildcard = 0; + for (i = 0; i < options.pathspec.nr; i++) + options.pathspec.items[i].nowildcard_len = options.pathspec.items[i].len; + options.pathspec.has_wildcard = 0; tree = parse_tree_indirect(&oid); if (!tree) die("not a tree object"); @@ -412,11 +439,11 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) */ while (m2f) { if (!m2f->fmt) { - fn = format ? show_tree_fmt : show_tree_default; - } else if (format && !strcmp(format, m2f->fmt)) { + fn = options.format ? show_tree_fmt : show_tree_default; + } else if (options.format && !strcmp(options.format, m2f->fmt)) { cmdmode = m2f->mode; fn = m2f->fn; - } else if (!format && cmdmode == m2f->mode) { + } else if (!options.format && cmdmode == m2f->mode) { fn = m2f->fn; } else { m2f++; @@ -425,5 +452,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) break; } - return !!read_tree(the_repository, tree, &pathspec, fn, NULL); + ret = !!read_tree(the_repository, tree, &options.pathspec, fn, &options); + clear_pathspec(&options.pathspec); + return ret; } From patchwork Thu Jan 12 09:11:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13097705 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71ACFC54EBC for ; Thu, 12 Jan 2023 09:20:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238322AbjALJUl (ORCPT ); Thu, 12 Jan 2023 04:20:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240292AbjALJTm (ORCPT ); Thu, 12 Jan 2023 04:19:42 -0500 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA0EA55647 for ; Thu, 12 Jan 2023 01:11:53 -0800 (PST) Received: by mail-pg1-x52d.google.com with SMTP id s67so12351336pgs.3 for ; Thu, 12 Jan 2023 01:11:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KryojS0G7P9W6tYC6yBRT5E6nDu36WxkHjsJQBFLPk4=; b=TrfJlMUYjs6JWTbGWjBGW6bS36DoSUthgmf+16YLXIxCGdr5hlAIrYtftGIChHLFz1 iZtq46mpOiTwGh6hrCVThepLU14oAFRtIaxJ0iE6GBn0m2BJ/DLfLSSyUsh27AV7OTGe EU/dIL+f3lqQ0z+EE1/joygELOZTz12sup34gHHyLcva27KKPbYtl2dvs00ucaFTSVRy U659KOpb16K/E8wI7mjhjToRkadqD2OQJ57QswC2OIOS1Wo6V0DAQUE6615zb5iXMnzU eC2cDozvKTSUes1uQFB0c9/13D6CQF1+CXDv9hbSNWJG7HJ5aOH2RZD1PNkEi1v6duOw KRHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KryojS0G7P9W6tYC6yBRT5E6nDu36WxkHjsJQBFLPk4=; b=10TjLXlygUrDNKWJPQhDakQG8WWm9zZzam9HRUCCyUI/7rwGjm+XEsWOpGg2L4SeWH YhlnetQhtszlEhqC0ep1a9sd14y8BHktKHhcoZBTNTj/BNEhJ8ZosBYPH3NN7q2WeTft A5nf/9gMa0y/DppRTgNriDNRjWzvd1F4Ikn+lJuRX3OViTS3MNSbR8qN4nbPuVKJ2azv K3/Jx2T3llgsXr8TjlfHcLkJOMLd1CBY+BK+QCZySHHWs55UDS3QOi45578ECtNFyrLr 5MYrycUTNO+GJthNpj6vciDGCb9womnBJUVVHjo6iIGKQi52M7/F/Px8Z2Gs+nrqulbC xoDA== X-Gm-Message-State: AFqh2kpPghdN2MTU078v4lcLiozMutc0KFVK/AlXTOhilKZs6tCc+ECU yjViRflIcf83hMp3A3a6AeCOAqp3VZnBtTLs X-Google-Smtp-Source: AMrXdXtRUFa3HhCAsjBhnEh7yM4JJ0+cW8vbHll9B1tzlVo3YRISBLblfQCL4zjjM5As9gwwwE6S/w== X-Received: by 2002:aa7:93de:0:b0:587:25ea:4fcb with SMTP id y30-20020aa793de000000b0058725ea4fcbmr5968017pff.19.1673514713080; Thu, 12 Jan 2023 01:11:53 -0800 (PST) Received: from localhost.localdomain ([47.246.101.48]) by smtp.gmail.com with ESMTPSA id d126-20020a623684000000b0058426d9db66sm11257069pfa.218.2023.01.12.01.11.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Jan 2023 01:11:52 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, dyroneteng@gmail.com, tenglong.tl@alibaba-inc.com Subject: [PATCH 3/6] ls-tree: fold "show_tree_data" into "cb" struct Date: Thu, 12 Jan 2023 17:11:32 +0800 Message-Id: <20230112091135.20050-4-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.39.0.98.g174eaa53973.dirty In-Reply-To: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> References: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason After the the preceding two commits the only user of the "show_tree_data" struct needed it along with the "options" member, let's instead fold all of that into a "show_tree_data" struct that we'll use only for that callback. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 54f7b604cb7..da664eecfb9 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -53,6 +53,7 @@ struct ls_tree_options { }; struct show_tree_data { + struct ls_tree_options *options; unsigned mode; enum object_type type; const struct object_id *oid; @@ -60,17 +61,11 @@ struct show_tree_data { struct strbuf *base; }; -struct show_tree_data_cb { - struct ls_tree_options *options; - struct show_tree_data *data; -}; - static size_t expand_show_tree(struct strbuf *sb, const char *start, void *context) { - struct show_tree_data_cb *wrapper = context; - struct ls_tree_options *options = wrapper->options; - struct show_tree_data *data = wrapper->data; + struct show_tree_data *data = context; + struct ls_tree_options *options = data->options; const char *end; const char *p; unsigned int errlen; @@ -153,17 +148,14 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, int recurse = 0; struct strbuf sb = STRBUF_INIT; enum object_type type = object_type(mode); - struct show_tree_data data = { + struct show_tree_data cb_data = { + .options = options, .mode = mode, .type = type, .oid = oid, .pathname = pathname, .base = base, }; - struct show_tree_data_cb cb_data = { - .data = &data, - .options = options, - }; if (type == OBJ_TREE && show_recursive(options, base->buf, base->len, pathname)) recurse = READ_TREE_RECURSIVE; From patchwork Thu Jan 12 09:11:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13097706 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C439C54EBD for ; Thu, 12 Jan 2023 09:20:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239965AbjALJUy (ORCPT ); Thu, 12 Jan 2023 04:20:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240314AbjALJTy (ORCPT ); Thu, 12 Jan 2023 04:19:54 -0500 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D608E55665 for ; Thu, 12 Jan 2023 01:11:55 -0800 (PST) Received: by mail-pf1-x42e.google.com with SMTP id i65so9875237pfc.0 for ; Thu, 12 Jan 2023 01:11:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SJp6tC+Xif0aSR9Zczbm75OWJSTLCCvZNGx+/VKW72I=; b=fTPVOQujzIpcFdUx1CFh49e72E85WBbAdIY57JGz3oC1jii7UAoTjSnAY2gNZ6kBFW Iz2bonaJY7xbHZhPZQqyYKM/UmUTZug+uOqHE575pAwMjaju/1Wy0n6B3gex29fwJj9j nbGF917pmJB3vAMZud5zPekv/TzYqlwvIzhLzJDnWcGPHkTWTLJFOeQHrvfMzyTwIUuD Qi5koJRGLsxzStr67bAwrXtF1gMr0cBXm8GSpdFyEjcXL0oQAdEqF5dezBIdl8+oOCbk NBPfDPXRpU2dHBnKFZMNcwtPhLyvUDvS/zrjV+DjH2nI3IpZoWO6w8XFO/N6LwLepoZw +gAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SJp6tC+Xif0aSR9Zczbm75OWJSTLCCvZNGx+/VKW72I=; b=bPoup54IYwFUv4giTKpnZGM0x2ibaHm7m5D/jP1HUUGwiMDYdWqs1kxTkdYe8wuZ/Z xYb8ubhNJWHZmyewqW5KEZBMYf1i0RlU3qu0OmhNhzuvx/wnFEE+NWYKk91Q1xxiSyH+ /2cw8DCWYx/CzsqTE4eaq5JhV+55v2DrJ1eNV1V0D/aQmBU9y8bHIOztfLS6jEDYwwE0 nM3NR7G5+2LHsZvQYRIZzT952ObxTnWN0fZsrpLVVo8/uH8MWKNN6QM6XkMefzC8Gs8j U0674FE84rv8kIzcit+NLhbowEMJOZhAHZNSYfR8csNRpyOVl/y1nKkkGLRovk9yJqEF zICA== X-Gm-Message-State: AFqh2koZxNmcXiAwVO8/aVKvSOfXEmz9ec4jdhE4wVCE0nDpxhP9smCb Mf6bP3RVG/gbAWNL6y10N7/xzkaQnIvgf9Vf X-Google-Smtp-Source: AMrXdXscSVK+Hc//1K7RMvKHaFb5A6g1yHkyLx1tp9j6+mTYsYSrKGUHHA66zk6VU94maINu4ID/RQ== X-Received: by 2002:a62:1b58:0:b0:581:4ab0:abcd with SMTP id b85-20020a621b58000000b005814ab0abcdmr5491574pfb.14.1673514715018; Thu, 12 Jan 2023 01:11:55 -0800 (PST) Received: from localhost.localdomain ([47.246.101.48]) by smtp.gmail.com with ESMTPSA id d126-20020a623684000000b0058426d9db66sm11257069pfa.218.2023.01.12.01.11.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Jan 2023 01:11:54 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, dyroneteng@gmail.com, tenglong.tl@alibaba-inc.com Subject: [PATCH 4/6] ls-tree: make "line_termination" less generic Date: Thu, 12 Jan 2023 17:11:33 +0800 Message-Id: <20230112091135.20050-5-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.39.0.98.g174eaa53973.dirty In-Reply-To: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> References: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason The "ls-tree" command isn't capable of ending "lines" with anything except '\n' or '\0', and in the latter case we can avoid calling write_name_quoted_relative() entirely. Let's do that, less for optimization and more for clarity, the write_name_quoted_relative() API itself does much the same thing. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 58 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index da664eecfb9..a743959f2b3 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -39,7 +39,7 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid, } struct ls_tree_options { - int line_termination; + unsigned null_termination:1; int abbrev; enum ls_tree_path_options { LS_RECURSIVE = 1 << 0, @@ -166,7 +166,7 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, baselen = base->len; strbuf_expand(&sb, options->format, expand_show_tree, &cb_data); - strbuf_addch(&sb, options->line_termination); + strbuf_addch(&sb, options->null_termination ? '\0' : '\n'); fwrite(sb.buf, sb.len, 1, stdout); strbuf_release(&sb); strbuf_setlen(base, baselen); @@ -198,10 +198,22 @@ static void show_tree_common_default_long(struct ls_tree_options *options, const char *pathname, const size_t baselen) { + const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL; + strbuf_addstr(base, pathname); - write_name_quoted_relative(base->buf, - options->chomp_prefix ? options->ls_tree_prefix : NULL, stdout, - options->line_termination); + + if (options->null_termination) { + struct strbuf sb = STRBUF_INIT; + const char *name = relative_path(base->buf, prefix, &sb); + + fputs(name, stdout); + fputc('\0', stdout); + + strbuf_release(&sb); + } else { + write_name_quoted_relative(base->buf, prefix, stdout, '\n'); + } + strbuf_setlen(base, baselen); } @@ -264,15 +276,25 @@ static int show_tree_name_only(const struct object_id *oid, struct strbuf *base, int recurse; const size_t baselen = base->len; enum object_type type = object_type(mode); + const char *prefix; early = show_tree_common(options, &recurse, base, pathname, type); if (early >= 0) return early; + prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL; strbuf_addstr(base, pathname); - write_name_quoted_relative(base->buf, - options->chomp_prefix ? options->ls_tree_prefix : NULL, - stdout, options->line_termination); + if (options->null_termination) { + struct strbuf sb = STRBUF_INIT; + const char *name = relative_path(base->buf, prefix, &sb); + + fputs(name, stdout); + fputc('\0', stdout); + + strbuf_release(&sb); + } else { + write_name_quoted_relative(base->buf, prefix, stdout, '\n'); + } strbuf_setlen(base, baselen); return recurse; } @@ -285,12 +307,19 @@ static int show_tree_object(const struct object_id *oid, struct strbuf *base, int early; int recurse; enum object_type type = object_type(mode); + const char *str; early = show_tree_common(options, &recurse, base, pathname, type); if (early >= 0) return early; - printf("%s%c", find_unique_abbrev(oid, options->abbrev), options->line_termination); + str = find_unique_abbrev(oid, options->abbrev); + if (options->null_termination) { + fputs(str, stdout); + fputc('\0', stdout); + } else { + puts(str); + } return recurse; } @@ -342,9 +371,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) int i, full_tree = 0; read_tree_fn_t fn = NULL; enum ls_tree_cmdmode cmdmode = MODE_DEFAULT; - struct ls_tree_options options = { - .line_termination = '\n', - }; + int null_termination = 0; + struct ls_tree_options options = { 0 }; const struct option ls_tree_options[] = { OPT_BIT('d', NULL, &options.ls_options, N_("only show trees"), LS_TREE_ONLY), @@ -352,8 +380,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) LS_RECURSIVE), OPT_BIT('t', NULL, &options.ls_options, N_("show trees when recursing"), LS_SHOW_TREES), - OPT_SET_INT('z', NULL, &options.line_termination, - N_("terminate entries with NUL byte"), 0), + OPT_BOOL('z', NULL, &null_termination, + N_("terminate entries with NUL byte")), OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"), MODE_LONG), OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"), @@ -383,6 +411,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) argc = parse_options(argc, argv, prefix, ls_tree_options, ls_tree_usage, 0); + options.null_termination = null_termination; + if (full_tree) { options.ls_tree_prefix = prefix = NULL; options.chomp_prefix = 0; From patchwork Thu Jan 12 09:11:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13097707 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C45AC61DB3 for ; Thu, 12 Jan 2023 09:20:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239989AbjALJU4 (ORCPT ); Thu, 12 Jan 2023 04:20:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240320AbjALJTz (ORCPT ); Thu, 12 Jan 2023 04:19:55 -0500 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B864954736 for ; Thu, 12 Jan 2023 01:11:57 -0800 (PST) Received: by mail-pf1-x432.google.com with SMTP id k19so13313958pfg.11 for ; Thu, 12 Jan 2023 01:11:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=I4cnAn6aFg6X/ukxRgPg5Mqy5tU0lOzmc4SyhpP9DMY=; b=OQIS8CorJC54w+yGRM1dG7DUkAHvN8zLuynoBxVn8pQYUqvCsBUb7pcXnsWrY0UsaS BQwiDflLMddDSSuoTBSWAJ0cxWXxdMt1M37q8gRkxLz0zYM45H6V14h2evl+BY7zRwSK FUF1yKJiaazDV4P35MZvn+FoG9vM5rL+2FaLVetjx0XUzT+D1hDjmnAcwS1T2AIBXW2N 9el0mH1ECQ+qZx41fNo+ARINb3DG0TsOgPqAzVv1RZ8x9FcUCnxzThxbOmmEWC7j483E PSW2gF63ZwEuaAcb8U4ToM+SShitdEeJC9pwvBa6fE7JVG2rmT9LTi68smX3x8o+gDYq A+/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I4cnAn6aFg6X/ukxRgPg5Mqy5tU0lOzmc4SyhpP9DMY=; b=YXsVRYv+3xavdf0mRyEqF6aBKRXbqcRxdX1iS7dT9p2c4BAPxKR/SqtbewHK65h9Ht +c0AkU0bts+/rjabLW4VKMBm58SXg3tWrP/q9OytZpVFFpU46WufU4iK3i/8zXrr6VC6 aKZ25tHTTsF1ltRHZzXJjdiL/hNuSFxGLowBa/tlNyTYdnLXVu2PGR6+TxMuckvTFOSq qfZQCMjS1T4s0CpiKB8K8Z0qVK3Rwcx6PLc7BP+hIy+XqK91k8qd301mNQ7KzSc5NnGN EMK4erwHwoUxQLzadvtjdwPG0qs5uaQGJ1sm9Ru2LzuZZFDL1Fx7cPGBLgEoRwvutBde ZcbA== X-Gm-Message-State: AFqh2krekRWN9I9Wu/G3vWjIJxmKIhztzWbH67SgCI07wKJSR+Y3DMQL 49wqx3ZJwc6VXKnX/IwEzsjQoS+hXbkyMjds X-Google-Smtp-Source: AMrXdXt/1W2B10Dvg1MYyPaxDxaXpnvATpdLbv/NoRjbAXcUN2CdEAVw48TfZ7i3bhqPRl6B3Bq8Xw== X-Received: by 2002:a62:1d97:0:b0:578:ac9f:79a9 with SMTP id d145-20020a621d97000000b00578ac9f79a9mr70761656pfd.15.1673514716953; Thu, 12 Jan 2023 01:11:56 -0800 (PST) Received: from localhost.localdomain ([47.246.101.48]) by smtp.gmail.com with ESMTPSA id d126-20020a623684000000b0058426d9db66sm11257069pfa.218.2023.01.12.01.11.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Jan 2023 01:11:56 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, dyroneteng@gmail.com, tenglong.tl@alibaba-inc.com Subject: [PATCH 5/6] ls-tree: cleanup the redundant SPACE Date: Thu, 12 Jan 2023 17:11:34 +0800 Message-Id: <20230112091135.20050-6-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.39.0.98.g174eaa53973.dirty In-Reply-To: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> References: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Teng Long An redundant space was found in ls-tree.c, which is no doubt a small change, but it might be OK to make a commit on its own. Signed-off-by: Teng Long --- builtin/ls-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index a743959f2b3..72eb70823d5 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -14,7 +14,7 @@ #include "parse-options.h" #include "pathspec.h" -static const char * const ls_tree_usage[] = { +static const char * const ls_tree_usage[] = { N_("git ls-tree [] [...]"), NULL }; From patchwork Thu Jan 12 09:11:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13097708 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32CACC54EBD for ; Thu, 12 Jan 2023 09:21:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240012AbjALJVI (ORCPT ); Thu, 12 Jan 2023 04:21:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240337AbjALJT6 (ORCPT ); Thu, 12 Jan 2023 04:19:58 -0500 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7DB65564D for ; Thu, 12 Jan 2023 01:11:59 -0800 (PST) Received: by mail-pg1-x532.google.com with SMTP id 7so12363640pga.1 for ; Thu, 12 Jan 2023 01:11:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=g9VFD8Mq5DWbcibSMZCTlrnddSh63BJmMEBiiD3nANM=; b=XSbGIjTjAZdAbh65VcaubIc+DWnOmaaEYYTZggh42AzDw2X77r4LAMV54wTBREhuqT xjt07iG3SST77Hkd8dtb8RCET+VxdgxfM/sthFgh7wJXfjenvODjecnUUvey7HYxr+et qs++Yd0dWnfrE5HVlHtolza1nNwV/WvI9r+Tpnz3xl5UFHAxx5xlcRqAl36+ykO9Hq37 UNzOE8ExKkP64Wkq5yZ352BFzFpqER8+bLEi6P72tuwHff6MO6ZTTezTmSR4cfL64sk6 ilMgo2olHKAcEVAhCedOFHa0+V+ozybNIrlexDl6Ac9niSfYvT7xqxqmTt+Blg76T+kD 9HhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=g9VFD8Mq5DWbcibSMZCTlrnddSh63BJmMEBiiD3nANM=; b=yhoUjPaPNxTKS46MCHat8opUvinQfAiV5lvfyD8IIjfpg7Qq8xWrDvdnpJTIrMIW9v HdauC6nRNbmDpJlXEYentyC7IvJT8Ky23cBDV+hGEtVLNZS1qrmCnpxyrkSehIRqzL5k iWlCLwyvjbCl0TaJltlEiSxq+rOdSmS/FQ8jPOsQ0lWmDQ0il35Hx7d2hVMYkI8XdgRU 6mXcsyT5ouofMEnlSxdtwKR8XDfN8DFH+lTbXfQ37w59IrxM0SUSFoy5hslMZn6mphhA vregGkUPRP27Hsu/RPoNdG7QfcXuqECzsOfTBeX5tZwaDcb1wn0KsQ0aEGxY3mee1VQD YCcA== X-Gm-Message-State: AFqh2krtaFIkjmEkoQyVcBAsb96blVA7c3X/jpbN1GM9eXyIlZnrv/eT iibwm9iEHUDvbumZTe0Ostud12Y+8Vki6HKg X-Google-Smtp-Source: AMrXdXuCqfkEdR8M2De4KW28irdXpMNKwLqa2kga0TG62ienZQmvHXWt03k4JC9iRPvcnwpZjncu/w== X-Received: by 2002:a62:84c9:0:b0:581:1f4b:d1e5 with SMTP id k192-20020a6284c9000000b005811f4bd1e5mr60663724pfd.12.1673514719003; Thu, 12 Jan 2023 01:11:59 -0800 (PST) Received: from localhost.localdomain ([47.246.101.48]) by smtp.gmail.com with ESMTPSA id d126-20020a623684000000b0058426d9db66sm11257069pfa.218.2023.01.12.01.11.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Jan 2023 01:11:58 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, dyroneteng@gmail.com, tenglong.tl@alibaba-inc.com Subject: [PATCH 6/6] t3104: remove shift code in 'test_ls_tree_format' Date: Thu, 12 Jan 2023 17:11:35 +0800 Message-Id: <20230112091135.20050-7-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.39.0.98.g174eaa53973.dirty In-Reply-To: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> References: <20230112091135.20050-1-tenglong.tl@alibaba-inc.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Teng Long In t3104-ls-tree-format.sh, There is a legacy 'shift 2' code and the relevant code block no longer depends on it anymore, so let's remove it for a small cleanup. Signed-off-by: Teng Long --- t/t3104-ls-tree-format.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/t/t3104-ls-tree-format.sh b/t/t3104-ls-tree-format.sh index 383896667b6..74053978f49 100755 --- a/t/t3104-ls-tree-format.sh +++ b/t/t3104-ls-tree-format.sh @@ -20,7 +20,6 @@ test_ls_tree_format () { format=$1 && opts=$2 && fmtopts=$3 && - shift 2 && test_expect_success "ls-tree '--format=<$format>' is like options '$opts $fmtopts'" ' git ls-tree $opts -r HEAD >expect &&