From patchwork Thu Nov 17 11:30:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13046622 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 1DD53C4332F for ; Thu, 17 Nov 2022 11:30:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239667AbiKQLam (ORCPT ); Thu, 17 Nov 2022 06:30:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234237AbiKQLag (ORCPT ); Thu, 17 Nov 2022 06:30:36 -0500 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 379C942998 for ; Thu, 17 Nov 2022 03:30:35 -0800 (PST) Received: by mail-pj1-x1033.google.com with SMTP id t17so715960pjo.3 for ; Thu, 17 Nov 2022 03:30:35 -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=dh4cvVseARQgAC7cBURaZyijkuBDTKBviI67fsY+1Z8=; b=ElY5jKXKDg/zhIrAQVLXPXZjktT2GkRd5ZahsPXivSdrirs7MtKBUohmJupl5vEu+h Mr/sPLimSNONXn1wox7GEeHoncoU1+awqCGrb6M27wc1/NF7ESOqzXFCbFtUE1l77Kgh VpNaZy4zPYQbFFrBfHgP6524B2MaunsYBgyBWUSV/HwSpLjp5rcmK8w1GfG1GoU7IqnZ Qs0KpkHEX14qBW00HU/J2VzqfGJ/rWwsbd/D1gBPQFWuT1HSfJzsTg25PX1TFdhAu8uR y7sSmhrU4vz8j/P6h+8/8ZJCalHL+nk0EcMtgCwm75dEVQzC9LcYLPgLjd2kYeTtb1Gg lOfw== 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=dh4cvVseARQgAC7cBURaZyijkuBDTKBviI67fsY+1Z8=; b=f0EjFnvRR50szUyv/tkVcnxL6hJkEUwclTIRglke2eCQYLbmtJ5AMyKER+daaxx2Pk /STWGC/rvWswmG8M1xeVa1psHYu9aSM8qO+LRo/KX25u6y7SzgLWmc9POrDmaTmgN3Ja Bl3nBaywoxLiyybq2Qz92zoKAFBmv5K6qHbDwHq+Sy2jQmkTBUzUp8IVIPl9pVasDdus ZPj8kveiaWYDCO2+ziu/q7QcCDxHJwqqzeBtuxjAHNyu9FIlVROr1aZRnWykUqvJkNOR yBuelF2GxaJ6FAew2olisG3tsDU4CeuDPTykaICN74CCnVPAnGkxol8WyhVg574j7GMz el0Q== X-Gm-Message-State: ANoB5pnTheCJM8lKFMiMAwLCArwPuaDk6JSfpegncOxnjrLuETfn94ly IQBvS6lLKGc3d5WKZyDGOpnvt8LSMExKYw== X-Google-Smtp-Source: AA0mqf4hbLnU7TGVggFLd+T4s3Ce6vnZmCaYJXnq4is0eE4qfOt0psLkHk9Cn7PDUBGkgDKkx6z/AA== X-Received: by 2002:a17:90a:ac07:b0:20a:f75d:b9af with SMTP id o7-20020a17090aac0700b0020af75db9afmr8097543pjq.222.1668684634584; Thu, 17 Nov 2022 03:30:34 -0800 (PST) Received: from localhost.localdomain ([47.246.101.60]) by smtp.gmail.com with ESMTPSA id f7-20020a170902684700b00186c5e8b1d0sm1056117pln.149.2022.11.17.03.30.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Nov 2022 03:30:34 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, tenglong.tl@alibaba-inc.com, me@ttaylorr.com, Teng Long Subject: [RFC PATCH 1/6] ls-tree: cleanup the redundant SPACE Date: Thu, 17 Nov 2022 19:30:18 +0800 Message-Id: <20221117113023.65865-2-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.38.1.426.g770fc8806cb.dirty In-Reply-To: <20221117113023.65865-1-tenglong.tl@alibaba-inc.com> References: <20221117113023.65865-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 c3ea09281af..8256fc0bc97 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -32,7 +32,7 @@ struct show_tree_data { struct strbuf *base; }; -static const char * const ls_tree_usage[] = { +static const char * const ls_tree_usage[] = { N_("git ls-tree [] [...]"), NULL }; From patchwork Thu Nov 17 11:30:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13046623 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 770E8C433FE for ; Thu, 17 Nov 2022 11:30:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239702AbiKQLao (ORCPT ); Thu, 17 Nov 2022 06:30:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239408AbiKQLai (ORCPT ); Thu, 17 Nov 2022 06:30:38 -0500 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29FCE429A2 for ; Thu, 17 Nov 2022 03:30:37 -0800 (PST) Received: by mail-pj1-x1033.google.com with SMTP id t17so716030pjo.3 for ; Thu, 17 Nov 2022 03:30:37 -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=JYs1XQAtgBF+mAFz2tGoG6J4xF6KDUhA8aM+Qa5N/74=; b=Wrsmqc3va9Ty+dnV4O2SVFJG7qwT1uyUsnhqoO8FEyLsvqjO5rTzLcTpZ9qIhCfX7c M+ayzqHvhnzNQKHUlBxBCYQbMVdfN6QJXTWeVtSy6Mt3xs1cYx5fQqx7W5sAOIXbi3l7 R5wsHRNCOfRTvkoTgvVbnmbfuvyWd6031i8vrb8P1dPg7xAXxl58rjjlefDDMDoSrS20 WRhEYP8QEqNhhaSh4mToILuv3Jago8mWYE2Q78rA9msBjM/TrvSsiAU+bW/X7LrXt0Mx qa8j18DnG1p6Ir0DvE5UDawuKpP4XEl6nVUX/IZl2IqZgygnY+eu2+3rcIxJj0nzMd22 HSng== 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=JYs1XQAtgBF+mAFz2tGoG6J4xF6KDUhA8aM+Qa5N/74=; b=3kVmt551xrjqg1WxlvUbjVqi0QdM5lmRTfaDOOSVBwoqWRp3qI3IeArtqm33cHounA mr2aWabx4FVTlh+OffDxDnbrt+J+HWQVDcUvis3ja39yd6EARE4iY8bigZKxC+z6Vaym SRlnTfpy+vszmj24ddR3lg+1nA2ct61sS8i2+cSPh4QP+JspMzFUW54+owzTyv3KYX0Y b4rwbvQ7gDVQ6p3B7qF95LiffGko2Lq+LSKzxyzZf3+DzWrfvb1mBpXZZbCuFjUmImVh vDcpfOz+McmKF7hExbq4mFj8JQex0uxHgalsDtHxbIz3CiOZU+qLshjbOVy5PHjskYPb Ceag== X-Gm-Message-State: ANoB5pk37Ep1CMErA6jJDkw3n0hJmWMEfRLxWt+R+3eP6HdK4rOe6yFu yB609zly4Dww7pT7LIvPnnRQJ5EynYKoAQ== X-Google-Smtp-Source: AA0mqf78upJgGeAcozKeWzDbyPev5P6rI8kndpj6C8dDORF2L8itUaGosQ1FrOqLnAi/DRc3yOo9zQ== X-Received: by 2002:a17:90a:4f4e:b0:212:84c:1e59 with SMTP id w14-20020a17090a4f4e00b00212084c1e59mr2327993pjl.88.1668684636711; Thu, 17 Nov 2022 03:30:36 -0800 (PST) Received: from localhost.localdomain ([47.246.101.60]) by smtp.gmail.com with ESMTPSA id f7-20020a170902684700b00186c5e8b1d0sm1056117pln.149.2022.11.17.03.30.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Nov 2022 03:30:36 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, tenglong.tl@alibaba-inc.com, me@ttaylorr.com, Teng Long Subject: [RFC PATCH 2/6] t3104: remove shift code in 'test_ls_tree_format' Date: Thu, 17 Nov 2022 19:30:19 +0800 Message-Id: <20221117113023.65865-3-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.38.1.426.g770fc8806cb.dirty In-Reply-To: <20221117113023.65865-1-tenglong.tl@alibaba-inc.com> References: <20221117113023.65865-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 && From patchwork Thu Nov 17 11:30:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13046624 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 30A11C4332F for ; Thu, 17 Nov 2022 11:30:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239713AbiKQLar (ORCPT ); Thu, 17 Nov 2022 06:30:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239322AbiKQLak (ORCPT ); Thu, 17 Nov 2022 06:30:40 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E405D42998 for ; Thu, 17 Nov 2022 03:30:39 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id v4-20020a17090a088400b00212cb0ed97eso1644514pjc.5 for ; Thu, 17 Nov 2022 03:30:39 -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=UnrIaY15ocEMQVBl2WYat1qxKAuWAkWFdRSCE7rxH1M=; b=pzaQ/0cMz2seF/ktc8J3CRBvkc3v7Q8pH5pph/gojgL8/zA7BPhdp+fDzX1YRjP8Mm IFpV0ObYn2TCsmn+m7RJ4Fcsqyvr9eKDw9UTbvK0n6RvahVXOXIjSpu/gRiFowXtYpJw pPljX7Z1Y3hjO9ditwpPH6sZGugd7Nqx17RS6YjKmanXB4efTs+Larcev4DNcIYrKvAh boPjohdJBI5XltnofeZDPzzVshsU3/rYdiv2RnRVrrlerICPVuvrGLmjhH2yuR53PuKv rlZNIZOk4uBgpXBfIihjyYYhxFnGSGug4ykXQW1LmBOPJYg7EhHWoPqDza10Zoc0ehKA Seww== 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=UnrIaY15ocEMQVBl2WYat1qxKAuWAkWFdRSCE7rxH1M=; b=yv3HLmM2ixndKMdti6O/DZIhyXfSucnGGPQ3uA+kwoikTpPYtQud+NwF754cSy1M0x 1qGV9AQNOMDcd9CsmOmv+mu12KiR28G1wdodYU/Nq8GszUyRwXjbMMBb4GaUTOq3Btqy yFRYRZ6WmP0oFgV9iQNIFnju16QFldLCVT8MOUVVIeiRfTxWR0gmz6OMb7h0YJXsGV+W OgLDvJ7inpnt5k4CnDlyGvaayTR3UEQmpPVBl4+Qv5YqpvAwo1vIvqISveIZ+9A3f0DF Tzpv/tLbnY3SV5oyzaAyE+tyRR8UzZ74IPzM7sGICTntq0YfV0QGuv+xsN4ffMWVSrA6 wiew== X-Gm-Message-State: ANoB5pmwJLscjlgsJwLpGZ4T6XSBddndgKt59vpgV/xCz8n2zjVVXMNu wCdspXizgt0AhBqFPoD87JKR4/CvDzy2xw== X-Google-Smtp-Source: AA0mqf4PYRtPl/vGT2v8okNvugiVVODhFeREMwfsHrrg6+UxdGhZPJ9CLL0h/4R0YZo935zHfQXnPw== X-Received: by 2002:a17:90a:c217:b0:212:ca89:41c9 with SMTP id e23-20020a17090ac21700b00212ca8941c9mr2266177pjt.244.1668684638920; Thu, 17 Nov 2022 03:30:38 -0800 (PST) Received: from localhost.localdomain ([47.246.101.60]) by smtp.gmail.com with ESMTPSA id f7-20020a170902684700b00186c5e8b1d0sm1056117pln.149.2022.11.17.03.30.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Nov 2022 03:30:38 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, tenglong.tl@alibaba-inc.com, me@ttaylorr.com, Teng Long Subject: [RFC PATCH 3/6] ls-tree: optimize params of 'show_tree_common_default_long()' Date: Thu, 17 Nov 2022 19:30:20 +0800 Message-Id: <20221117113023.65865-4-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.38.1.426.g770fc8806cb.dirty In-Reply-To: <20221117113023.65865-1-tenglong.tl@alibaba-inc.com> References: <20221117113023.65865-1-tenglong.tl@alibaba-inc.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Teng Long Function 'show_tree_common_default_long' has 3 parameters: * struct strbuf *base * const char *pathname * const size_t baselen And the struct 'show_tree_data' which was introduced in 'e81517155e0370ae5433d256046ab287bb10d04d' is: struct show_tree_data { unsigned mode; enum object_type type; const struct object_id *oid; const char *pathname; struct strbuf *base; }; Actually, the struct includes all the data that we need to pass to the function, so we could make a small refactoring by using 'show_tree_data' struct directly. Signed-off-by: Teng Long --- builtin/ls-tree.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 8256fc0bc97..afb65af4280 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -200,15 +200,15 @@ static int show_tree_common(struct show_tree_data *data, int *recurse, return ret; } -static void show_tree_common_default_long(struct strbuf *base, - const char *pathname, - const size_t baselen) +static void show_tree_common_default_long(struct show_tree_data *data) { - strbuf_addstr(base, pathname); - write_name_quoted_relative(base->buf, + int base_len = data->base->len; + + strbuf_addstr(data->base, data->pathname); + write_name_quoted_relative(data->base->buf, chomp_prefix ? ls_tree_prefix : NULL, stdout, line_termination); - strbuf_setlen(base, baselen); + strbuf_setlen(data->base, base_len); } static int show_tree_default(const struct object_id *oid, struct strbuf *base, @@ -225,7 +225,7 @@ static int show_tree_default(const struct object_id *oid, struct strbuf *base, 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); + show_tree_common_default_long(&data); return recurse; } @@ -255,7 +255,7 @@ static int show_tree_long(const struct object_id *oid, struct strbuf *base, 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); + show_tree_common_default_long(&data); return recurse; } From patchwork Thu Nov 17 11:30:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13046625 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 6D6BDC43217 for ; Thu, 17 Nov 2022 11:30:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239718AbiKQLax (ORCPT ); Thu, 17 Nov 2022 06:30:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239668AbiKQLan (ORCPT ); Thu, 17 Nov 2022 06:30:43 -0500 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2910A4509F for ; Thu, 17 Nov 2022 03:30:42 -0800 (PST) Received: by mail-pf1-x435.google.com with SMTP id k22so1497060pfd.3 for ; Thu, 17 Nov 2022 03:30:41 -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=53njfhXcFSFE/dk0W1eUPSZJzqE9q5J/Ul2o7oNGCzM=; b=Ni0n3dptsu8LNH5IGRzYCXLOijhoJ9/97xC9oPdl+COzmk4X9/dkoWuOUtTTDRzrWI mbGT9NzFa9gp2l6Pb0FGWbjrZfRgY7z4WcdmPZNHtospNXeMMb5yWc8wx6VYD+WqmuG+ BuChB7Mkrs1Trj4ERkEzlQobFGAXNFsNhHVGhN2mRo7cl4Fc6ul2zQ1vgs4uWkxJGHa+ Hcvb1M/UtBqsejcX3IvD9z4yEEtNpWS3MqOK3YlR0rcXusKPHEQFCrCHNRCy8fDEGYdS h3NqmZpjU5QnALqDg5MtTze6kbSHfjpdPJOoCgb5bFAoIrqDNCdVNoyJYlh+Sh5CxtsI DfAw== 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=53njfhXcFSFE/dk0W1eUPSZJzqE9q5J/Ul2o7oNGCzM=; b=pC3d7e2ubiYBLnaZS6I5iN02QsAua0SiTkNVFXy/huQhj/XAN4Zhqn5mhCwK3ZkFEp frYeVT7oRsRKPkF8yRHXGB5nQkgzYaeAuOdvE19vtPRb71boGF94wE02lqFCz17Kh3pW /yLT46SV0vVH9tlImfP5zA95GpoXrEogrLBCT7a+1ux+K+uAZr8xAM/JmrW+olyZKEWs aCyNFYEfEsP2qNlIEpIFv3vGBJmTvm49iWD3PKEY32w1HVR2EmV06e4u9Iis+i7obw4V lLawInedZ/IrdGpP4YUtj79xzi/YOUxijs8iByIPgX8QQLLmg2k6y7myGOpNnwhdewJ/ N4kg== X-Gm-Message-State: ANoB5plfkgq0sf/Ruem86sn2Dx/nFCX/q5zqkVuPp6uJSybTqFZytDMJ wek119stKWTvXMLgZIPs6de2gqhOpALePA== X-Google-Smtp-Source: AA0mqf6ZLfsMVk8eg2IXWP/qyVu0SW27GqWL5nCDgQrkVScYIey2DksaZ7dLwrjsM9u0l66EbEWC0g== X-Received: by 2002:a62:3084:0:b0:56d:dd2a:c494 with SMTP id w126-20020a623084000000b0056ddd2ac494mr2436276pfw.76.1668684641468; Thu, 17 Nov 2022 03:30:41 -0800 (PST) Received: from localhost.localdomain ([47.246.101.60]) by smtp.gmail.com with ESMTPSA id f7-20020a170902684700b00186c5e8b1d0sm1056117pln.149.2022.11.17.03.30.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Nov 2022 03:30:41 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, tenglong.tl@alibaba-inc.com, me@ttaylorr.com, Teng Long Subject: [RFC PATCH 4/6] ls-tree: improving cohension in the print code Date: Thu, 17 Nov 2022 19:30:21 +0800 Message-Id: <20221117113023.65865-5-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.38.1.426.g770fc8806cb.dirty In-Reply-To: <20221117113023.65865-1-tenglong.tl@alibaba-inc.com> References: <20221117113023.65865-1-tenglong.tl@alibaba-inc.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Teng Long We use 'show_tree_long()' and 'show_tree_default()' to print entries when we want the output in 'default' or 'default long' formats. Function 'show_tree_common_default_long()' prints the "path" field as the last part of output, the previous part which separately implements in 'show_tree_long()' and 'show_tree_default()'. We can package the separate implementation together by the extension of "size_text" in struct "show_tree_data". By improving the cohesion in these two locations, some benefits such as uniform processing of the output can be achieved in future. Signed-off-by: Teng Long --- builtin/ls-tree.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index afb65af4280..7661170f7ca 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -30,6 +30,7 @@ struct show_tree_data { const struct object_id *oid; const char *pathname; struct strbuf *base; + char *size_text; }; static const char * const ls_tree_usage[] = { @@ -186,6 +187,7 @@ static int show_tree_common(struct show_tree_data *data, int *recurse, data->oid = oid; data->pathname = pathname; data->base = base; + data->size_text = NULL; if (type == OBJ_BLOB) { if (ls_options & LS_TREE_ONLY) @@ -204,6 +206,13 @@ static void show_tree_common_default_long(struct show_tree_data *data) { int base_len = data->base->len; + if (data->size_text) + printf("%06o %s %s %7s\t", data->mode, type_name(data->type), + find_unique_abbrev(data->oid, abbrev), data->size_text); + else + printf("%06o %s %s\t", data->mode, type_name(data->type), + find_unique_abbrev(data->oid, abbrev)); + strbuf_addstr(data->base, data->pathname); write_name_quoted_relative(data->base->buf, chomp_prefix ? ls_tree_prefix : NULL, stdout, @@ -223,8 +232,6 @@ static int show_tree_default(const struct object_id *oid, struct strbuf *base, 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(&data); return recurse; } @@ -253,8 +260,7 @@ 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); + data.size_text = size_text; show_tree_common_default_long(&data); return recurse; } From patchwork Thu Nov 17 11:30:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13046626 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 5019DC4332F for ; Thu, 17 Nov 2022 11:31:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239735AbiKQLbI (ORCPT ); Thu, 17 Nov 2022 06:31:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239722AbiKQLax (ORCPT ); Thu, 17 Nov 2022 06:30:53 -0500 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 864514875A for ; Thu, 17 Nov 2022 03:30:44 -0800 (PST) Received: by mail-pg1-x536.google.com with SMTP id s196so1724565pgs.3 for ; Thu, 17 Nov 2022 03:30:44 -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=aXx3uJztlxTc5guOX6lQ/1R4HDmOgcHIbupMGnumBhE=; b=a8KKjw+vWDLrMyVeDGgjWazI3dBlM+pPQtnKHttafLRx97f1rH4X9cAhGlqgKeuJJ7 SY5qrEf1S5+zQYJ43/aijeHurtj5Gn0VwMKlo+rZhmF7bGJjxcCWgdLEwt6cgm7+8Knu Coa6td7GrQF9Tj7QZ7WtbS2ddGap7FsmvmSbd7PHSFw/Lggvjkzmimp7YqHO7f89sS6e Y9du185+JS/lTqDN/3a2w161lD0FphR/nvow1NutsoSWKNeAWw+0a2NK3tN3yiRxnRS/ 19i0ktu7druAsoD85psbbvLaJicxh6wwP8r8nkYLUamjrGFIADaKP3Kbx1RpVe77WrSH CvZA== 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=aXx3uJztlxTc5guOX6lQ/1R4HDmOgcHIbupMGnumBhE=; b=hRrXjPzxct2dF5pr0/1Vd1UJGFyPPjBf0jyjOFBMeEGplxrMzBNUNKyLx/ixyS1U8L c8KdITrdE2q6rojcGd/EjIyNpdPyXolLd7tnDu+s59/Wp0CEUnlYZd5epEefb1sygDOk /RJwDPgz64vDfDkgfNQ6RpsqW1gFqwHVte5eYnD7huIENynU72MtIrTdzX9vmqOP2JuH 43L991G6qbqQS/2VodZ58AcrWs3aiiGEnIQYR5KjALVbXbNUc8cZfH/UWubLODy075LB ipy80R44oWLLfgzfX6Fmejvd58ltAqY4cGJDiFkh4Juk3DqN0GEyp/YmfUwaULC+bU/M a7dA== X-Gm-Message-State: ANoB5pn30skCiTSnin58IsCBrBJ4GM2U2CeskNuV/25ltxPMBUmt5F1Y U6wSQZBD84YMQmh1ix3JUC//qqCc/sfMtA== X-Google-Smtp-Source: AA0mqf6rfe8praUsN7azbp/e51/0yXpX1RBKwHsuVdsTRdDk/fZUbbujWwvdg3psYCyPJCEnLXoirw== X-Received: by 2002:a05:6a00:301b:b0:56e:1ce2:c919 with SMTP id ay27-20020a056a00301b00b0056e1ce2c919mr2486697pfb.78.1668684643820; Thu, 17 Nov 2022 03:30:43 -0800 (PST) Received: from localhost.localdomain ([47.246.101.60]) by smtp.gmail.com with ESMTPSA id f7-20020a170902684700b00186c5e8b1d0sm1056117pln.149.2022.11.17.03.30.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Nov 2022 03:30:43 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, tenglong.tl@alibaba-inc.com, me@ttaylorr.com, Teng Long Subject: [RFC PATCH 5/6] ls-tree: introduce 'match_pattern()' function Date: Thu, 17 Nov 2022 19:30:22 +0800 Message-Id: <20221117113023.65865-6-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.38.1.426.g770fc8806cb.dirty In-Reply-To: <20221117113023.65865-1-tenglong.tl@alibaba-inc.com> References: <20221117113023.65865-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 preparation for actually implementing the '--pattern' option, we add a new method called 'match_pattern' that uses regular expressions to match 'ls-tree' entities. Signed-off-by: Teng Long --- builtin/ls-tree.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 7661170f7ca..03dd3fbcb26 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -24,6 +24,7 @@ static struct pathspec pathspec; static int chomp_prefix; static const char *ls_tree_prefix; static const char *format; +static const char *pattern; struct show_tree_data { unsigned mode; enum object_type type; @@ -46,6 +47,32 @@ static enum ls_tree_cmdmode { MODE_OBJECT_ONLY, } cmdmode; +__attribute__((unused)) +static int match_pattern(const char *line) +{ + int ret = 0; + regex_t r; + regmatch_t m[1]; + char errbuf[64]; + + ret = regcomp(&r, pattern, 0); + if (ret) { + regerror(ret, &r, errbuf, sizeof(errbuf)); + die("failed regcomp() for pattern '%s' (%s)", pattern, errbuf); + } + ret = regexec(&r, line, 1, m, 0); + if (ret) { + if (ret == REG_NOMATCH) + goto cleanup; + regerror(ret, &r, errbuf, sizeof(errbuf)); + die("failed regexec() for subject '%s' (%s)", line, errbuf); + } + +cleanup: + regfree(&r); + return ret; +} + static void expand_objectsize(struct strbuf *line, const struct object_id *oid, const enum object_type type, unsigned int padded) { From patchwork Thu Nov 17 11:30:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 13046627 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 52199C4332F for ; Thu, 17 Nov 2022 11:31:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239748AbiKQLbL (ORCPT ); Thu, 17 Nov 2022 06:31:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239734AbiKQLa6 (ORCPT ); Thu, 17 Nov 2022 06:30:58 -0500 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC0504B98E for ; Thu, 17 Nov 2022 03:30:46 -0800 (PST) Received: by mail-pg1-x535.google.com with SMTP id r18so1672827pgr.12 for ; Thu, 17 Nov 2022 03:30:46 -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=0CqloFlxJgXjbKhG0by4EOHXWGlnl7aJpTlCORFLAF8=; b=QtUu9ZUkfrorgxJUqww+GV/O2OO7403L0Afp1ECDVeAgMMoFcvr9Es355whG0+T5tF kjkY5Htw1mqeGqFAf0Hs8rQFHpImpWPYOiLuzvL6wPKpBbVPmJ+jK4KKrclWkXkgBJvd 7mJ2LWhg4d/4dopoyy9BB7J3J81dwUl6eeCdMQKv8oTfT0P85y6Fj/qcWJTRKfNrr3Qx KOZ3aA1YI7KaPxCetR61wrdy4HDg61WvyHw5qrnSZeFjmnRN2biPw+EwnEnpmelDo69/ KWxdfcRP68+JTN8dl4Q8gRoldL+RRfSLCL4KMtDEMmJg6Ir50RaSwP0LP9AFpvdAmnmN wwUg== 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=0CqloFlxJgXjbKhG0by4EOHXWGlnl7aJpTlCORFLAF8=; b=ZvmXq9v5K4/sIlpLaoeqpjFrB1XPX8hddi6CkR6DZE/4rWNojRtpqZMM3Rm2yk9k5Z vuPqDoKoC6l5zCJx5UCHJLE4KjYofWwzU2ruyYTRtYnYV5YDxJ+9FnanTs/ettQ3NAiS D26SHedHV2yMDT92PNE1m+8fo78bPjWII1rVnQOsL5IaZ1prVjDy9HqqIDc+J8prssX2 FajCat4FpPOsMFzm84DNWf2INwTss6HTvwpzcOF25Q/6Iv1gUAWuhRDWxiw3pTeYpZjc uV9/LdIYAR/p3/zTGnChIZdT5yS3aA5tAuJtIeNUD3Q/V+zYvmtrn5YPkbFnCw9jc5iR Dzmw== X-Gm-Message-State: ANoB5pni6zOKHtz35BSi1o/yBs3uCtuYxQI53MS6KypRqmzR7kRKiS5H iJ6rKJx7cTcHKTrhyfnA1nTUQBFZWOUT6A== X-Google-Smtp-Source: AA0mqf52s/1mHHEJ6+3Q/p9yzryi3kfMgj/YGq/uclZSeQUp8cvkD7V8OTNKZKA98FADzac8/lkcWw== X-Received: by 2002:a63:5850:0:b0:46e:f67c:c0a7 with SMTP id i16-20020a635850000000b0046ef67cc0a7mr1599879pgm.464.1668684646017; Thu, 17 Nov 2022 03:30:46 -0800 (PST) Received: from localhost.localdomain ([47.246.101.60]) by smtp.gmail.com with ESMTPSA id f7-20020a170902684700b00186c5e8b1d0sm1056117pln.149.2022.11.17.03.30.44 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Nov 2022 03:30:45 -0800 (PST) From: Teng Long X-Google-Original-From: Teng Long To: git@vger.kernel.org Cc: avarab@gmail.com, tenglong.tl@alibaba-inc.com, me@ttaylorr.com, Teng Long Subject: [RFC PATCH 6/6] ls-tree: introduce '--pattern' option Date: Thu, 17 Nov 2022 19:30:23 +0800 Message-Id: <20221117113023.65865-7-tenglong.tl@alibaba-inc.com> X-Mailer: git-send-email 2.38.1.426.g770fc8806cb.dirty In-Reply-To: <20221117113023.65865-1-tenglong.tl@alibaba-inc.com> References: <20221117113023.65865-1-tenglong.tl@alibaba-inc.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Teng Long The "--pattern" option uses regular expressions to match each entry, then filter the output of "ls-tree" . Signed-off-by: Teng Long Signed-off-by: Johannes Schindelin --- Documentation/git-ls-tree.txt | 7 ++- builtin/ls-tree.c | 82 +++++++++++++++++++++++------------ t/t3106-ls-tree-pattern.sh | 70 ++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 28 deletions(-) create mode 100755 t/t3106-ls-tree-pattern.sh diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt index 0240adb8eec..39346409f2f 100644 --- a/Documentation/git-ls-tree.txt +++ b/Documentation/git-ls-tree.txt @@ -10,7 +10,7 @@ SYNOPSIS -------- [verse] 'git ls-tree' [-d] [-r] [-t] [-l] [-z] - [--name-only] [--name-status] [--object-only] [--full-name] [--full-tree] [--abbrev[=]] [--format=] + [--name-only] [--name-status] [--object-only] [--full-name] [--full-tree] [--abbrev[=]] [--format=] [--pattern=] [...] DESCRIPTION @@ -93,6 +93,11 @@ OPTIONS format-altering options, including `--long`, `--name-only` and `--object-only`. +--pattern=:: + The is a string of regular expression format used to + match each entry. Unmatched entries will be filtered and not + dump to the output. + [...]:: When paths are given, show them (note that this isn't really raw pathnames, but rather a list of patterns to match). Otherwise diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 03dd3fbcb26..576fc9ad16f 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -13,6 +13,7 @@ #include "builtin.h" #include "parse-options.h" #include "pathspec.h" +#include static int line_termination = '\n'; #define LS_RECURSIVE 1 @@ -25,6 +26,7 @@ static int chomp_prefix; static const char *ls_tree_prefix; static const char *format; static const char *pattern; +static regex_t *regex; struct show_tree_data { unsigned mode; enum object_type type; @@ -47,29 +49,29 @@ static enum ls_tree_cmdmode { MODE_OBJECT_ONLY, } cmdmode; -__attribute__((unused)) static int match_pattern(const char *line) { int ret = 0; - regex_t r; regmatch_t m[1]; char errbuf[64]; - ret = regcomp(&r, pattern, 0); - if (ret) { - regerror(ret, &r, errbuf, sizeof(errbuf)); - die("failed regcomp() for pattern '%s' (%s)", pattern, errbuf); + if (!regex) { + regex = xmalloc(sizeof(*regex)); + ret = regcomp(regex, pattern, 0); + if (ret) { + regerror(ret, regex, errbuf, sizeof(errbuf)); + die("failed regcomp() for pattern '%s' (%s)", pattern, errbuf); + } } - ret = regexec(&r, line, 1, m, 0); + + ret = regexec(regex, line, 1, m, 0); if (ret) { if (ret == REG_NOMATCH) - goto cleanup; - regerror(ret, &r, errbuf, sizeof(errbuf)); + return ret; + regerror(ret, regex, errbuf, sizeof(errbuf)); die("failed regexec() for subject '%s' (%s)", line, errbuf); } -cleanup: - regfree(&r); return ret; } @@ -194,8 +196,12 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, baselen = base->len; strbuf_expand(&sb, format, expand_show_tree, &data); - strbuf_addch(&sb, line_termination); - fwrite(sb.buf, sb.len, 1, stdout); + + if (!pattern || !match_pattern(sb.buf)) { + strbuf_addch(&sb, line_termination); + fwrite(sb.buf, sb.len, 1, stdout); + } + strbuf_release(&sb); strbuf_setlen(base, baselen); return recurse; @@ -232,19 +238,33 @@ static int show_tree_common(struct show_tree_data *data, int *recurse, static void show_tree_common_default_long(struct show_tree_data *data) { int base_len = data->base->len; + struct strbuf sb = STRBUF_INIT; + int sb_len = 0; if (data->size_text) - printf("%06o %s %s %7s\t", data->mode, type_name(data->type), - find_unique_abbrev(data->oid, abbrev), data->size_text); + strbuf_addf(&sb, "%06o %s %s %7s\t", data->mode, + type_name(data->type), + find_unique_abbrev(data->oid, abbrev), + data->size_text); else - printf("%06o %s %s\t", data->mode, type_name(data->type), - find_unique_abbrev(data->oid, abbrev)); + strbuf_addf(&sb, "%06o %s %s\t", data->mode, + type_name(data->type), + find_unique_abbrev(data->oid, abbrev)); strbuf_addstr(data->base, data->pathname); - write_name_quoted_relative(data->base->buf, - chomp_prefix ? ls_tree_prefix : NULL, stdout, - line_termination); + sb_len = sb.len; + strbuf_addbuf(&sb, data->base); + + if (!pattern || !match_pattern(sb.buf)) { + strbuf_setlen(&sb, sb_len); + printf("%s", sb.buf); + write_name_quoted_relative(data->base->buf, + chomp_prefix ? ls_tree_prefix : NULL, + stdout, line_termination); + } strbuf_setlen(data->base, base_len); + + strbuf_release(&sb); } static int show_tree_default(const struct object_id *oid, struct strbuf *base, @@ -306,9 +326,11 @@ static int show_tree_name_only(const struct object_id *oid, struct strbuf *base, return early; strbuf_addstr(base, pathname); - write_name_quoted_relative(base->buf, - chomp_prefix ? ls_tree_prefix : NULL, - stdout, line_termination); + if (!pattern || !match_pattern(base->buf)) { + write_name_quoted_relative(base->buf, + chomp_prefix ? ls_tree_prefix : NULL, + stdout, line_termination); + } strbuf_setlen(base, baselen); return recurse; } @@ -320,12 +342,14 @@ static int show_tree_object(const struct object_id *oid, struct strbuf *base, int early; int recurse; struct show_tree_data data = { 0 }; + const char *oid_text = find_unique_abbrev(oid, abbrev); early = show_tree_common(&data, &recurse, oid, base, pathname, mode); if (early >= 0) return early; - printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination); + if (!pattern || !match_pattern(oid_text)) + printf("%s%c", oid_text, line_termination); return recurse; } @@ -391,8 +415,10 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) N_("list entire tree; not just current directory " "(implies --full-name)")), OPT_STRING_F(0, "format", &format, N_("format"), - N_("format to use for the output"), - PARSE_OPT_NONEG), + N_("format to use for the output"), + PARSE_OPT_NONEG), + OPT_STRING(0, "pattern", &pattern, "pattern", + "pattern to use to match the output"), OPT__ABBREV(&abbrev), OPT_END() }; @@ -430,10 +456,12 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) usage_with_options(ls_tree_usage, ls_tree_options); if (get_oid(argv[0], &oid)) die("Not a valid object name %s", argv[0]); + if (pattern && !strlen(pattern)) + die("Not a valid pattern, the value is empty"); /* * show_recursive() rolls its own matching code and is - * generally ignorant of 'struct pathspec'. The magic mask + * generally ignorant f 'struct pathspec'. The magic mask * cannot be lifted until it is converted to use * match_pathspec() or tree_entry_interesting() */ diff --git a/t/t3106-ls-tree-pattern.sh b/t/t3106-ls-tree-pattern.sh new file mode 100755 index 00000000000..e4a81c8c47e --- /dev/null +++ b/t/t3106-ls-tree-pattern.sh @@ -0,0 +1,70 @@ +#!/bin/sh + +test_description='ls-tree pattern' + +TEST_PASSES_SANITIZE_LEAK=true +. ./test-lib.sh +. "$TEST_DIRECTORY"/lib-t3100.sh + +test_expect_success 'setup' ' + setup_basic_ls_tree_data +' + +test_expect_success 'ls-tree pattern usage' ' + test_expect_code 129 git ls-tree --pattern HEAD && + test_expect_code 128 git ls-tree --pattern "" HEAD >err 2>&1 && + grep "Not a valid pattern, the value is empty" err +' + +test_expect_success 'combine with "--object-only"' ' + cat > expect <<-EOF && + 6da7993 + EOF + + git ls-tree --object-only --abbrev=7 --pattern "6da7993" HEAD > actual && + test_cmp expect actual +' + +test_expect_success 'combine with "--name-only"' ' + cat > expect <<-EOF && + .gitmodules + top-file.t + EOF + + git ls-tree --name-only --pattern "\." HEAD > actual && + test_cmp expect actual +' + +test_expect_success 'combine with "--long"' ' + cat > expect <<-EOF && + 100644 blob 6da7993 61 .gitmodules + 100644 blob 02dad95 9 top-file.t + EOF + git ls-tree --long --abbrev=7 --pattern "blob" HEAD > actual && + test_cmp expect actual +' + +test_expect_success 'combine with "--format"' ' + # Change the output format by replacing space separators with asterisks. + format="%(objectmode)*%(objecttype)*%(objectname)%x09%(path)" && + pattern="100644\*blob" && + + cat > expect <<-EOF && + 100644*blob*6da7993 .gitmodules + 100644*blob*02dad95 top-file.t + EOF + + git ls-tree --abbrev=7 --format "$format" --pattern "$pattern" HEAD >actual && + test_cmp expect actual +' + +test_expect_success 'default output format (only with "--pattern" option)' ' + cat > expect <<-EOF && + 100644 blob 6da7993ca1a3435f63c598464f77bdc6dae15aa5 .gitmodules + 100644 blob 02dad956d9274a70f7fafe45a5ffa2e123acd9a8 top-file.t + EOF + git ls-tree --pattern "blob" HEAD > actual && + test_cmp expect actual +' + +test_done