From patchwork Mon Mar 21 07:33:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786933 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 49089C433F5 for ; Mon, 21 Mar 2022 07:33:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344924AbiCUHfL (ORCPT ); Mon, 21 Mar 2022 03:35:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344925AbiCUHfG (ORCPT ); Mon, 21 Mar 2022 03:35:06 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDFE1186FD for ; Mon, 21 Mar 2022 00:33:40 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id o68-20020a17090a0a4a00b001c686a48263so7017044pjo.1 for ; Mon, 21 Mar 2022 00:33:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B8KvgDMkGTVIZ7rFKOZDIQPb1H6uqVbE3nFPXjxkaEU=; b=m77zGxGb6OCd8xveQtQzfkQ6RdbyHhRdFQykTtDN/yDtYXAHFg9Ca+PvPKoaKBl0kS xrcQYwliE8r4skJ7+m2NXek0bjGP6Ue/JeHan+fLT/08IJUYZyP0Vl/R7nZDC+KmLPsV egqLeXrxqDeFci22oRUZoJx89qUgBWYzwPwn7/MoKcA/oDintydFA7MLgJM2ygeBYwai wLhvqNZB3g369THtOyhol05q5CuFQjtmg0to3x4iH/7MLdOirb28C+PQm4PS4rWpCmzX 57f7QUBg/yoVJdOJYj9p+u4emoeuTh31LSiA/Awu4eL9H2J8RccEg/Y5Yvd1czHXHTP4 9Eag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=B8KvgDMkGTVIZ7rFKOZDIQPb1H6uqVbE3nFPXjxkaEU=; b=pEFRLZKgBjUq9cv+xSyI9oW/zP0l92lVLwDZBho5xwB4xnq6I2uLJh/Z1VBv5Xazwv 9kOUXIgUusvDK4BLkHatvSkUMYcuNL2pw1KdlTMhp2U6arXlHfFB4LG4TuQxmYpm29V9 aGWOXsVHww9621eJGFE/dxN0Daf40AURpJTSdaEbc75ulnoWgwdlGGpM+k+cc38LoYr0 LnTVZGnKfTmzyqSNOkFngNwhn3EpU++V1yMabnhY8j9wKlp9y1NE2yHpDWolBJYYnmdr j77TMM1QQ2UbaKgEOVguzgMDbJkz6jwV3cYQ8q+oeLjKBVdqhuU6sP35bXwLqI2t/Jbk 6iEQ== X-Gm-Message-State: AOAM5301sMUZvUEcwTQaJsw+SJuT3g12ktBVsaiw4ZqMAFrIYd8Bh9II 9erOnxOtql0RttLBzvT2Weo= X-Google-Smtp-Source: ABdhPJxEqlgdN6AB0+FtT7HSOxfWk0lv2/VsBGrkL5lD0qAQOtb+7hua3j2PKnUdPErghg/LvG6xWQ== X-Received: by 2002:a17:903:1ce:b0:150:2117:16b3 with SMTP id e14-20020a17090301ce00b00150211716b3mr11696511plh.26.1647848020212; Mon, 21 Mar 2022 00:33:40 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.33.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:33:39 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 01/16] ls-tree: remove commented-out code Date: Mon, 21 Mar 2022 15:33:15 +0800 Message-Id: <2fcff7e0d40255e0be9496647787352dd0aeb784.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason Remove code added in f35a6d3bce7 (Teach core object handling functions about gitlinks, 2007-04-09), later patched in 7d0b18a4da1 (Add output flushing before fork(), 2008-08-04), and then finally ending up in its current form in d3bee161fef (tree.c: allow read_tree_recursive() to traverse gitlink entries, 2009-01-25). All while being commented-out! Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/ls-tree.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 3a442631c7..5f7c84950c 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -69,15 +69,6 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, const char *type = blob_type; if (S_ISGITLINK(mode)) { - /* - * Maybe we want to have some recursive version here? - * - * Something similar to this incomplete example: - * - if (show_subprojects(base, baselen, pathname)) - retval = READ_TREE_RECURSIVE; - * - */ type = commit_type; } else if (S_ISDIR(mode)) { if (show_recursive(base->buf, base->len, pathname)) { From patchwork Mon Mar 21 07:33:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786934 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 35730C433EF for ; Mon, 21 Mar 2022 07:33:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344931AbiCUHfM (ORCPT ); Mon, 21 Mar 2022 03:35:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344928AbiCUHfI (ORCPT ); Mon, 21 Mar 2022 03:35:08 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA29854BD8 for ; Mon, 21 Mar 2022 00:33:43 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id m11-20020a17090a7f8b00b001beef6143a8so13639530pjl.4 for ; Mon, 21 Mar 2022 00:33:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+OdBXWAZOjwpWgkNLycV7jv8YJ07vdbO6u3OAvzfWCQ=; b=WVMeo1QeOOYlwRN8CdSt7CQYSvl9SHtHTrprc7Fc51azBBL2D/2lk+8a+PRzRZpxrS o0AoUgX0ewI/cKJZKIGpdNvM74+Ll/NmOpDKNHfzJExYfgg/4AhsWRSryQAUmgAB/i+G Gl6Wu840suEzhYd9c0t9o3tH9YhPpY5ZXIOwq8LYkQimaCONrBQ/6HfGJoVa0WQ3Nmm8 XAGnpH+zY94a1yOzgt+7yEXT4GJyy+roQLP41F68I0lAOSexgE1jurdRT84DKGAhKJj8 QR9+O899a4Z36NmRcwp2aOdd2heSYNGbMHy5r0m8WGr+Eu+ag/CE0EsV3KYcDBcR1bxu 7n+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+OdBXWAZOjwpWgkNLycV7jv8YJ07vdbO6u3OAvzfWCQ=; b=ysq0Z7i6yUrtk4rFemb3TJEvrkJ2RfKxCf43Y/lMmshWywLOktV+0K4TpXO2L/uOBZ G0byudORF8L2mfiJ2AIpuu/Uj0SxpA5q+5iCffYIACISyjESbTIFvLHWDkQyBW9YpT+o ulCWf0T5UnKkrd5YlVl+DGLinF35wH01cvLrfh0QXbFixrWJxDybGjN65XBV2leJAcjB wk2tx2LY4hdhcoNRrd9dEfLo6CL4cUczPi7iCJaVdtnLfrmllf8YPHyTKR+/sMr3sb8Y 5qOkAX6+6JfIcBuNicS2FyJhHWmuKeab3gC8HwYa1yrPJFFEG8S6DWpxdeJsUHytklM+ gNfA== X-Gm-Message-State: AOAM530wBTmX1baw5FaiLwo0GmX7etAzLbgCLJIZ2kVOtyNs0NogP+hk vn8TooOihplrnzCTRiaZsyk= X-Google-Smtp-Source: ABdhPJxpIMKNBQUc+S7PazR8u1L5paSIkqELc4TlVvU9nxWZNG0+rRyAqcvMoW1sFfSl0S/aJldNug== X-Received: by 2002:a17:902:cec8:b0:154:6dd6:255d with SMTP id d8-20020a170902cec800b001546dd6255dmr743291plg.62.1647848023166; Mon, 21 Mar 2022 00:33:43 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.33.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:33:42 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 02/16] ls-tree: add missing braces to "else" arms Date: Mon, 21 Mar 2022 15:33:16 +0800 Message-Id: <6fd1dd938303192d5b0a4cdc2914b2ff72cf5a3d.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason Add missing {} to the "else" arms in show_tree() per the CodingGuidelines. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/ls-tree.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 5f7c84950c..0a28f32ccb 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -92,14 +92,16 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, else xsnprintf(size_text, sizeof(size_text), "%"PRIuMAX, (uintmax_t)size); - } else + } else { xsnprintf(size_text, sizeof(size_text), "-"); + } printf("%06o %s %s %7s\t", mode, type, find_unique_abbrev(oid, abbrev), size_text); - } else + } else { printf("%06o %s %s\t", mode, type, find_unique_abbrev(oid, abbrev)); + } } baselen = base->len; strbuf_addstr(base, pathname); From patchwork Mon Mar 21 07:33:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786935 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 C28B3C433FE for ; Mon, 21 Mar 2022 07:33:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344928AbiCUHfN (ORCPT ); Mon, 21 Mar 2022 03:35:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344933AbiCUHfL (ORCPT ); Mon, 21 Mar 2022 03:35:11 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5AA9140BB for ; Mon, 21 Mar 2022 00:33:46 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id i11so11281413plr.1 for ; Mon, 21 Mar 2022 00:33:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YqhkHQi2ES/1xSYog9B6VjOokqU4c3bhxV8BlGZFTis=; b=aAzOQ0N0BiIQzuDhMT5SWabmCsWeUx2hM8UeNmhXJzEbTK05ZjWCUkyCun2sw2Cvex dh6EoUzc2BqCyTL8CeOtcKFojRBcBUFRnkFzX746WRuWf49olW3gBAAABZ/+5v9T3v7j 5zN4Gltw9zf8VKQ/vipJ62KQ4E619Uk5Ik/pmi2P4RatDUs+OMbOF4NoCOWXNk8m0gtR jyr4zDshC/moJTC9LVv99Jk43DR6TtG4OB+YdTLtLTpaXaekTAm5aQqVLaOb80RS+k1p HuS7VXQuNJZKEyfRAEXEYv5ykz7PSeEh8VwNuVDQe5KLzlqz9iU06PtaOwZ6Stw9lsVs A8Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YqhkHQi2ES/1xSYog9B6VjOokqU4c3bhxV8BlGZFTis=; b=0ooRCe5HBfa6/eNUZUyzWV5xD1CVeJWkkaZNlGgT731m8LwNannnPJxgwp1CZZ3vrs IYlZIrBxCfwDn2k/edxRPiv4MBG7QhbN3BT1OYV1Opuv91noxJlwZaLP2vyOj8H0HDt/ xoh+v0FmoKKmGyXVm5b0LoN00spYAwrgGP4FCg03zfvgaZ+QVEefV3Rq4/Rbw5/gy19v NT3tVXODUg1tytNb3ALlyO7VlTB9PHuee7gQNPE3o/NnV0ZrAnOBMyNEEhneqgH9zKSP dzhd04Cc8lpD56h2hkYs0Pp5u0dPYkII2gx2XJ4oeBhD0mVZIyFv7Mxcr4kbYuzyMf2K ns1g== X-Gm-Message-State: AOAM533ncmJGqSkM39IMhLOgnqBZum6uLgNEAuoEhG7r48yPSjb36OHo 9ScFjWDLC+miv3R8s5jegdQ= X-Google-Smtp-Source: ABdhPJy/Jb1TjmMQ3jba3F7wyfJrCa7jtxnK6woGwVaPAT24cosjSq2ySBZdKmNpaj60IWXb4WMoKg== X-Received: by 2002:a17:902:7613:b0:151:6e1c:c49c with SMTP id k19-20020a170902761300b001516e1cc49cmr11588073pll.109.1647848026126; Mon, 21 Mar 2022 00:33:46 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.33.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:33:45 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 03/16] ls-tree: use "enum object_type", not {blob,tree,commit}_type Date: Mon, 21 Mar 2022 15:33:17 +0800 Message-Id: <208654b5e2aebbe988b5a8f478869253a0166e9b.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason Change the ls-tree.c code to use type_name() on the enum instead of using the string constants. This doesn't matter either way for performance, but makes this a bit easier to read as we'll no longer need a strcmp() here. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/ls-tree.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 0a28f32ccb..3f0225b097 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -66,17 +66,17 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, { int retval = 0; int baselen; - const char *type = blob_type; + enum object_type type = OBJ_BLOB; if (S_ISGITLINK(mode)) { - type = commit_type; + type = OBJ_COMMIT; } else if (S_ISDIR(mode)) { if (show_recursive(base->buf, base->len, pathname)) { retval = READ_TREE_RECURSIVE; if (!(ls_options & LS_SHOW_TREES)) return retval; } - type = tree_type; + type = OBJ_TREE; } else if (ls_options & LS_TREE_ONLY) return 0; @@ -84,7 +84,7 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, if (!(ls_options & LS_NAME_ONLY)) { if (ls_options & LS_SHOW_SIZE) { char size_text[24]; - if (!strcmp(type, blob_type)) { + if (type == OBJ_BLOB) { unsigned long size; if (oid_object_info(the_repository, oid, &size) == OBJ_BAD) xsnprintf(size_text, sizeof(size_text), @@ -95,11 +95,11 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, } else { xsnprintf(size_text, sizeof(size_text), "-"); } - printf("%06o %s %s %7s\t", mode, type, + printf("%06o %s %s %7s\t", mode, type_name(type), find_unique_abbrev(oid, abbrev), size_text); } else { - printf("%06o %s %s\t", mode, type, + printf("%06o %s %s\t", mode, type_name(type), find_unique_abbrev(oid, abbrev)); } } From patchwork Mon Mar 21 07:33:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786936 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 DA69DC433F5 for ; Mon, 21 Mar 2022 07:33:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344937AbiCUHfT (ORCPT ); Mon, 21 Mar 2022 03:35:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344935AbiCUHfO (ORCPT ); Mon, 21 Mar 2022 03:35:14 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B94355BCC for ; Mon, 21 Mar 2022 00:33:49 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id j13so2067635plj.8 for ; Mon, 21 Mar 2022 00:33:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YDvZsShFMnXjG5lbO/nJWg+6l37lngugqc3el6D+dZk=; b=i16RvNSmrJGG3RnvkoQ0Jw4Uru38KKeoy5X/LePbAQECVID1W6FJYyhh3nuRar/sZo nm7IlGwWDKWzq7zMC+1U4aHCQvnr+sYltLIAsp8D3qrpvdaKXmBcBxHP8sWsmH1GuE44 yDdM3zNu5DXRVr/LInO6BL2EreMNRugWu4GIg4OPIObmwi9L3tDT2sO2Z9b37ZA2JMRb Vf/jiVhVtbVN2IFeOnp99oNOipt0U/JapTF1BjtChmsSxzaCb1gNsiyRJh/XFTKsvmjj dO5ABNvT2/mzn3xn5TmbXdOgoYW+cWqKxCpZFmt/l8ofRO/iPzVoOJSADInNYKCx+xD0 xeHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YDvZsShFMnXjG5lbO/nJWg+6l37lngugqc3el6D+dZk=; b=I+A4PO8kyBcBrIZHWFj2g44bpK3pH395Z9fDx6Jg1EJjik96QyKoMrOeBpWyQeU/fA 42t79hI2gJr3I1p85w8OUvvXCe2FyDwmeBmfJXljpfKqKQLIkZ0UCzuPMEQJNw9YkCnF xhqQ5TlRFYPViuTgFAITbc/iPY7TdHTgu3M+Z3zyk4izL3/cUEt3ZIKRGj0bxmDx436A kTe8Sb+xC7E0Xs2BGNivJw+Sx/uvEm9tXcLzfp40FH24xI8tfcZ3ZHYzUVyWukW1I6d/ PoLg+zqOEEzwfuDB8xTr1S96EeSfxDvgZ3TKRtg0NIsoz3wBz4iC8G+TpMIzricF8PUe rfXg== X-Gm-Message-State: AOAM530AxmqVyeTM2jR2eitXlYGt5NGVyAjuE1cgBblRafuwfB2RXtge N5K4F0/zH05leGqbSlQ/01w= X-Google-Smtp-Source: ABdhPJw0bKMHk1PXCpYxGi6Ba8bKXotNnOt/UU9Di2p2K0Q3CAaX6z2kj28fj0agkL9sy1CbGkAWew== X-Received: by 2002:a17:90b:3a81:b0:1c6:834e:cd61 with SMTP id om1-20020a17090b3a8100b001c6834ecd61mr22651259pjb.149.1647848029095; Mon, 21 Mar 2022 00:33:49 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.33.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:33:48 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 04/16] ls-tree: use "size_t", not "int" for "struct strbuf"'s "len" Date: Mon, 21 Mar 2022 15:33:18 +0800 Message-Id: <2637464fd8f6b8bae6c681f20c10c9fc76dc6d6c.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason The "struct strbuf"'s "len" member is a "size_t", not an "int", so let's change our corresponding types accordingly. This also changes the "len" and "speclen" variables, which are likewise used to store the return value of strlen(), which returns "size_t", not "int". Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/ls-tree.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 3f0225b097..eecc7482d5 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -31,7 +31,7 @@ static const char * const ls_tree_usage[] = { NULL }; -static int show_recursive(const char *base, int baselen, const char *pathname) +static int show_recursive(const char *base, size_t baselen, const char *pathname) { int i; @@ -43,7 +43,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname) for (i = 0; i < pathspec.nr; i++) { const char *spec = pathspec.items[i].match; - int len, speclen; + size_t len, speclen; if (strncmp(base, spec, baselen)) continue; @@ -65,7 +65,7 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, void *context) { int retval = 0; - int baselen; + size_t baselen; enum object_type type = OBJ_BLOB; if (S_ISGITLINK(mode)) { From patchwork Mon Mar 21 07:33: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: 12786937 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 AE5B1C433F5 for ; Mon, 21 Mar 2022 07:33:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344933AbiCUHfU (ORCPT ); Mon, 21 Mar 2022 03:35:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344941AbiCUHfS (ORCPT ); Mon, 21 Mar 2022 03:35:18 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A08FA94E5 for ; Mon, 21 Mar 2022 00:33:52 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id gb19so12347023pjb.1 for ; Mon, 21 Mar 2022 00:33:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=waTYiZ0ViPTkz6ea+O0CLBHGhMB38eJFC+nBA85V2oM=; b=XgRsiVm2dBWmXPi3x41Kl0qlCOahygS3oMUm46TOUnd3R+cJMBIc++iQvPcJ27Y0KD P71oes7JD/tHzhT8WgC5rDdLZ9Er1bc3ThwlRiAErMbtD0r2wPXIj9BH3+EHRjgUxWSz GvNVFTnF8M3fE6234CESrahFnLRckgrgRl84k9f2X1tZaSjHxDRp0bKw1gjlnB1+imb6 DqmKqne5xMm+nwGcyQtmYkhasUtcRAHNH1PKe/EZMw1kxK22j8MyO7Hyman2AWz9CHXo 3ntgsQFeZ1jYETxneiwtO/OTHgaOjmF1HhOpsXW0skS1T0WknJydcd2jXhfi7CW+IlH5 OcQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=waTYiZ0ViPTkz6ea+O0CLBHGhMB38eJFC+nBA85V2oM=; b=UrvgOWFKOrTkey2oDarj8jLdKJ0plwIZvMDCngE0oecEXU65lBuxFtqlL7GiLg56Rc Gmjf+4ogkDPVJrZK/nEnDXEiSIkg/1GhLqMAyWhwrZE5HC7RB45+yeJ95bKdAGTJgDdR BA48g5Fx5cOTmdg8UrcUf4+1aj/rz+7R0sqG9D/p/H3usyo/8i/VyxY/togA4UIsbrhP toGyc6RFI3P/cJNj5NX8Vbhwo+YdM6VPzxy1gIymoGLdrXVZlZX6mJLv6JjRYY48GLqd RWhOQb7QeFX5auYCOIrvq4iLyVGStaPnJ0FCZfyCwT6hOsNSio2jabkCAcXDmJ1ujlWE 3PUQ== X-Gm-Message-State: AOAM532d0BUtuyBgjyNZNHDCYWpnsY6n9SP4Ft/PmjFIdur31wvoatiV cw/OdqzYz6tuU9Bthc9SkGU= X-Google-Smtp-Source: ABdhPJx6hpzDFp0Gxiupt68CgPBvIKrvtqL0g4+1kKxyC8UXVD/4HU6bW+AZ5tKvIHyDJC7va0qt+g== X-Received: by 2002:a17:90a:ba15:b0:1c6:7873:b192 with SMTP id s21-20020a17090aba1500b001c67873b192mr24384962pjr.76.1647848032002; Mon, 21 Mar 2022 00:33:52 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.33.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:33:51 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 05/16] ls-tree: rename "retval" to "recurse" in "show_tree()" Date: Mon, 21 Mar 2022 15:33:19 +0800 Message-Id: <99e6d47108308b0bbaf406b439170de04e375c56.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The variable which "show_tree()" return is named "retval", a name that's a little hard to understand. The commit rename "retval" to "recurse" which is a more meaningful name than before in the context. We do not need to take a look at "read_tree_at()" in "tree.c" to make sure what does "retval" mean. Signed-off-by: Teng Long --- builtin/ls-tree.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index eecc7482d5..ef8c414f61 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -64,7 +64,7 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname static int show_tree(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, void *context) { - int retval = 0; + int recurse = 0; size_t baselen; enum object_type type = OBJ_BLOB; @@ -72,9 +72,9 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, type = OBJ_COMMIT; } else if (S_ISDIR(mode)) { if (show_recursive(base->buf, base->len, pathname)) { - retval = READ_TREE_RECURSIVE; + recurse = READ_TREE_RECURSIVE; if (!(ls_options & LS_SHOW_TREES)) - return retval; + return recurse; } type = OBJ_TREE; } @@ -109,7 +109,7 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, chomp_prefix ? ls_tree_prefix : NULL, stdout, line_termination); strbuf_setlen(base, baselen); - return retval; + return recurse; } int cmd_ls_tree(int argc, const char **argv, const char *prefix) From patchwork Mon Mar 21 07:33: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: 12786938 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 F1C67C433F5 for ; Mon, 21 Mar 2022 07:34:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344940AbiCUHfY (ORCPT ); Mon, 21 Mar 2022 03:35:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344935AbiCUHfU (ORCPT ); Mon, 21 Mar 2022 03:35:20 -0400 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 9D6E224BFE for ; Mon, 21 Mar 2022 00:33:55 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id e3so12312202pjm.5 for ; Mon, 21 Mar 2022 00:33:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9yBIuaZPOU3IJb7rGPaPtwN3Y6mEsmIG8ce5QSypmj8=; b=U/IVf1BZfPaSW0QEaWswkhQRLunMpATYl9dGD4CtlHA2F89MNju5j6dYHg65CftotP TlIkJivFZithqpu90AL5Ilw/E47SrV8KJwL9aiSsQgTl/zeMlCH2YacErBPggHSXaYZJ RcycVdbCYeuq9J5GINPploUYi6fsucRyjbdMfrx8S3AroOzP4G1rTOXYVzL1J4HPjipy zxvu0BsqEg81QxBmmLOX/FHp2vh1D8od0HekA5vO2rHKWcbu3Cy02a4OsgNYYXYof/+h it7R+Ay6JgoO+Qjwovzt7LoRJAUj7ZFO6M2zSGz4fUWejX7nkfSG5TWkFE2kwRunikVO x4Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9yBIuaZPOU3IJb7rGPaPtwN3Y6mEsmIG8ce5QSypmj8=; b=exHpXxVfkrrOUFrzazG6Czl09pJICw4DlMqkiE0sULuof56cWTZPxOo/TKIMUpvQ0W qJqSoNFE4CbDn0rP7sCTco4DtRS60dw3NgMhmGZcgK5CXYKtogvFlDtJKUoU7CAPEGiQ h6oo1jD8Il1TjOjUdiy3tGMf+y7qE/EU5Ae2DyIps1NjBLdPnT3AE3yuTzSXmalbvw9r 4xRZRRwEsFutZ8vMDkgvjEARWLjtVXtx2/Tvlql9XqOfsiFG1Z0ehGAH+8Nm781Pl6aS rs+wDReOQZCgFTpr3wST1aiRYLSJuFY2nrQywvEMtxeFPiiTc+00XSzheTlFNDP0OL3M n0Yw== X-Gm-Message-State: AOAM532vEVYh2MmYj7AOshmcGV+g4KXvdZrEktb6YELlrQLA2XGDhXXw vBpfdVzcVO9bC0BJggWq1LE= X-Google-Smtp-Source: ABdhPJxK59cvErLs0a1o4CJdtJTtSQhLClnOpUVilzHZ/EWy6g92dvEKI/DDf8DJGi9Yx6je7mArvw== X-Received: by 2002:a17:90b:1803:b0:1c7:24c4:ab52 with SMTP id lw3-20020a17090b180300b001c724c4ab52mr2551205pjb.240.1647848035123; Mon, 21 Mar 2022 00:33:55 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.33.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:33:54 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Teng Long Subject: [PATCH v13 06/16] ls-tree: simplify nesting if/else logic in "show_tree()" Date: Mon, 21 Mar 2022 15:33:20 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Use the object_type() function to determine the object type from the "mode" passed to us by read_tree(), instead of doing so with the S_*() macros. Signed-off-by: Teng Long --- builtin/ls-tree.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index ef8c414f61..d4be71bad2 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -66,20 +66,17 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, { int recurse = 0; size_t baselen; - enum object_type type = OBJ_BLOB; + enum object_type type = object_type(mode); - if (S_ISGITLINK(mode)) { - type = OBJ_COMMIT; - } else if (S_ISDIR(mode)) { - if (show_recursive(base->buf, base->len, pathname)) { - recurse = READ_TREE_RECURSIVE; - if (!(ls_options & LS_SHOW_TREES)) - return recurse; - } - type = OBJ_TREE; + if (type == OBJ_BLOB) { + if (ls_options & LS_TREE_ONLY) + return 0; + } else if (type == OBJ_TREE && + show_recursive(base->buf, base->len, pathname)) { + recurse = READ_TREE_RECURSIVE; + if (!(ls_options & LS_SHOW_TREES)) + return recurse; } - else if (ls_options & LS_TREE_ONLY) - return 0; if (!(ls_options & LS_NAME_ONLY)) { if (ls_options & LS_SHOW_SIZE) { From patchwork Mon Mar 21 07:33: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: 12786939 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 B0AA5C433EF for ; Mon, 21 Mar 2022 07:34:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344950AbiCUHf1 (ORCPT ); Mon, 21 Mar 2022 03:35:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242942AbiCUHfZ (ORCPT ); Mon, 21 Mar 2022 03:35:25 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32544AA03C for ; Mon, 21 Mar 2022 00:33:59 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id mm17-20020a17090b359100b001c6da62a559so3907285pjb.3 for ; Mon, 21 Mar 2022 00:33:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=esbW2+0bS/aEXI6nWZlj3jKCsIYW/DTe+MH1VU3cVqw=; b=Q1lChfjbEODZ3+MoGlwrJWEbyH75eQSztbzCfK3zNcnaJr/k82gHn3qYU0Hvwad6qa TqDZj8jd2liNl5QgrrHiZDrQroQTPuk9Bo5N7nlQU/ldT1AhMvo54vPlRJ8tniAZhO/w ISndE7MxJOwoiVuMyRNdbUZ0ty5ue3XBMY2S+HUfQ6K7OFcOWeqGmRbhqx7T0zEAHosa KOBv1dQihYPioU0Ir/UnJLSoqLmirbYTNzFGxA9m5X7vipcM1uXW5VdlZAHFajPNwwfw aWN8U/SJkcPxWYS7erGsQz52bt/83TNk5tTgN8oWasagtucqsYDWsWM3lcpzeQZqJisJ TITg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=esbW2+0bS/aEXI6nWZlj3jKCsIYW/DTe+MH1VU3cVqw=; b=pXhxq40JMeUWQJlBS/O1AzXhU+HOyrTcos7cWnoNy31AJsZIMFMdeIAlAzkAEhwHVt 04nK7YCbCzj9J5237X04yoUZAN/a54wPOIH/MU/U0xgtwtunFcPdDwdZ+fHyFiuN9Y8c uOZHSPWGquv7bds7ATN+Dktut4DtBsIlL1pFQtAiDUV/Zm/36RyRLF7eWD0v3cUC9/fO 25GfNqlx9yYYiiW3vgVZcQW3zWC0T2NUu+y9LlcpikFbEbL4QUW+pPZfg5BQwZ+HGyX8 EaoYbdx6E7OYKFjeTLKdOGBuHNSQUMBIfDzpr/Q18gP8289K/CC/Eavj/6QIipbQcNsK R1/g== X-Gm-Message-State: AOAM53229PdDL91pfaIySpPEtdyW270uy8kkWT7ECW4x8qxRRQyWlWEp iCEo0AYYunxUPdY3aeLD+SJ8mmkVaRohmg== X-Google-Smtp-Source: ABdhPJxdv2nb+4zzeFtLcutFgqSE8KL2H29y03oLgFVbMxUgTRFEbE/JQ1Y4ovtIZjY9ca3Ucm+qaw== X-Received: by 2002:a17:90a:af88:b0:1bd:6b5d:4251 with SMTP id w8-20020a17090aaf8800b001bd6b5d4251mr24437105pjq.134.1647848038642; Mon, 21 Mar 2022 00:33:58 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.33.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:33:58 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 07/16] ls-tree: fix "--name-only" and "--long" combined use bug Date: Mon, 21 Mar 2022 15:33:21 +0800 Message-Id: <25a07e048f25e0efdd2f8690f2e6967352a20e9b.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org If we execute "git ls-tree" with combined "--name-only" and "--long" , only the pathname will be printed, the size is omitted (the original discoverer was Peff in [1]). This commit fix this issue by using `OPT_CMDMODE()` instead to make both of them mutually exclusive. [1] https://public-inbox.org/git/YZK0MKCYAJmG+pSU@coredump.intra.peff.net/ Signed-off-by: Teng Long --- builtin/ls-tree.c | 12 ++++++------ t/t3103-ls-tree-misc.sh | 9 +++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index d4be71bad2..7be4c13228 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -123,12 +123,12 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) LS_SHOW_TREES), OPT_SET_INT('z', NULL, &line_termination, N_("terminate entries with NUL byte"), 0), - OPT_BIT('l', "long", &ls_options, N_("include object size"), - LS_SHOW_SIZE), - OPT_BIT(0, "name-only", &ls_options, N_("list only filenames"), - LS_NAME_ONLY), - OPT_BIT(0, "name-status", &ls_options, N_("list only filenames"), - LS_NAME_ONLY), + OPT_CMDMODE('l', "long", &ls_options, N_("include object size"), + LS_SHOW_SIZE), + OPT_CMDMODE(0, "name-only", &ls_options, N_("list only filenames"), + LS_NAME_ONLY), + OPT_CMDMODE(0, "name-status", &ls_options, N_("list only filenames"), + LS_NAME_ONLY), OPT_SET_INT(0, "full-name", &chomp_prefix, N_("use full path names"), 0), OPT_BOOL(0, "full-tree", &full_tree, diff --git a/t/t3103-ls-tree-misc.sh b/t/t3103-ls-tree-misc.sh index d18ba1bd84..d9d7fa932f 100755 --- a/t/t3103-ls-tree-misc.sh +++ b/t/t3103-ls-tree-misc.sh @@ -23,4 +23,13 @@ test_expect_success 'ls-tree fails with non-zero exit code on broken tree' ' test_must_fail git ls-tree -r HEAD ' +for opts in \ + "--name-only --long" \ + "--name-status --long" +do + test_expect_success "usage: incompatible options: $opts" ' + test_expect_code 129 git ls-tree $opts $tree + ' +done + test_done From patchwork Mon Mar 21 07:33: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: 12786940 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 BCC61C433F5 for ; Mon, 21 Mar 2022 07:34:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344953AbiCUHfi (ORCPT ); Mon, 21 Mar 2022 03:35:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344935AbiCUHf3 (ORCPT ); Mon, 21 Mar 2022 03:35:29 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C9C55A08B for ; Mon, 21 Mar 2022 00:34:02 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id mp6-20020a17090b190600b001c6841b8a52so9673781pjb.5 for ; Mon, 21 Mar 2022 00:34:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=swDp9j7imjeO4rLpZc4qlEHF1d+pYwuCo05VsVEGN+g=; b=PzMzqOEJnIujwxJcPbKTJRxSULJ39zOkIOeOc67UY7Q7Np3qODuWq847BQPvcVE3TO azGibveLDcedvN324x5gyU7SnyqrBcEV0bsEQTsqK9fkmyQyeXzBuPXNaxvrLuBFVQzd 5HDYho/XMn15PwKp7av/iQhyniLFXT3/jmxCIC4XAy2yfWBTyolqQeTSus33waitvOlc 7Gn02+Hh8/7MfQMRbTDaY7xh0xnr7sVWa0jpSiG+bxp0Ic4X8vCUCDYV2QBu8hhT8DcK QZuH9n303CQqzfPlbsI3uDw8ORQiaw7LBl26BQNabpIkpXfhrqayRYFfFj6b2f5P1SPu jbNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=swDp9j7imjeO4rLpZc4qlEHF1d+pYwuCo05VsVEGN+g=; b=5gT1qhPRFg2/fP4RMUIXk5guu9dPdwAB8QVQv0bGLthT4GB7FRRwJiTE301uQKZDuc uS/XHaDHub9wrFxcC4Oyr8Dp1W2XJDRO0gfKcF8PLA1HRMcEcLJx6BJyA9fPUkvs11ME 4tIjsY+6ks7M7C+/0KZCkwAJca8ZsMG0X+OvQVOqhIRmb5PXwS48BbAiuBbjqpxk2bM0 5l6NQGgnax99ew5shtXuoKOYHtnia0OOrdnX3MJWN7BbQLjuG+0EcaSqWVTPPkPqGSLE Xj5QTMa7fT64ZPDiGZcPu+Yd0rodZgRTDvelorn9KG0vjMpfKlr2OxQmQcWgsT024M0o L4cg== X-Gm-Message-State: AOAM5327lLl7Q4luZPyqf/sJ428BOGomTRE83U+eyA9/zAvf1QvdNXWT sdD3FnuCIhCS53NL5c7ZpeI= X-Google-Smtp-Source: ABdhPJxaqR6/PWqxocK06hHbhbzWuUrD4P3gxdRWhrb4xhoMH+PrekL8kUwWKdKxz3ZeHgkC11yRDA== X-Received: by 2002:a17:90a:7ac2:b0:1bf:3573:179b with SMTP id b2-20020a17090a7ac200b001bf3573179bmr24158294pjl.216.1647848041699; Mon, 21 Mar 2022 00:34:01 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.33.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:34:01 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 08/16] ls-tree: slightly refactor `show_tree()` Date: Mon, 21 Mar 2022 15:33:22 +0800 Message-Id: <55f1e10d7e38d85f15565845c8d29ed733da9f45.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This is a non-functional change, we use a new int "shown_fields" to mark which columns to output, and `parse_shown_fields()` to calculate the value of "shown_fields". This has the advantage of making the show_tree logic simpler and more readable, as well as making it easier to extend new options (for example, if we want to add a "--object-only" option, we just need to add a similar "if (shown_fields == FIELD_OBJECT_NAME)" short-circuit logic in "show_tree()"). Signed-off-by: Teng Long --- builtin/ls-tree.c | 121 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 37 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 7be4c13228..4364416e1c 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -16,21 +16,48 @@ static int line_termination = '\n'; #define LS_RECURSIVE 1 -#define LS_TREE_ONLY 2 -#define LS_SHOW_TREES 4 -#define LS_NAME_ONLY 8 -#define LS_SHOW_SIZE 16 +#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 unsigned int shown_fields; +#define FIELD_PATH_NAME 1 +#define FIELD_SIZE (1 << 1) +#define FIELD_OBJECT_NAME (1 << 2) +#define FIELD_TYPE (1 << 3) +#define FIELD_MODE (1 << 4) +#define FIELD_DEFAULT 29 /* 11101 size is not shown to output by default */ +#define FIELD_LONG_DEFAULT (FIELD_DEFAULT | FIELD_SIZE) static const char * const ls_tree_usage[] = { N_("git ls-tree [] [...]"), NULL }; +static enum ls_tree_cmdmode { + MODE_LONG = 1, + MODE_NAME_ONLY, +} cmdmode; + +static int parse_shown_fields(void) +{ + if (cmdmode == MODE_NAME_ONLY) { + shown_fields = FIELD_PATH_NAME; + return 0; + } + + if (!ls_options || (ls_options & LS_RECURSIVE) + || (ls_options & LS_SHOW_TREES) + || (ls_options & LS_TREE_ONLY)) + shown_fields = FIELD_DEFAULT; + if (cmdmode == MODE_LONG) + shown_fields = FIELD_LONG_DEFAULT; + return 1; +} + static int show_recursive(const char *base, size_t baselen, const char *pathname) { int i; @@ -61,6 +88,39 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname return 0; } +static int show_default(const struct object_id *oid, enum object_type type, + const char *pathname, unsigned mode, + struct strbuf *base) +{ + size_t baselen = base->len; + + if (shown_fields & FIELD_SIZE) { + char size_text[24]; + if (type == OBJ_BLOB) { + unsigned long size; + if (oid_object_info(the_repository, oid, &size) == OBJ_BAD) + xsnprintf(size_text, sizeof(size_text), "BAD"); + else + xsnprintf(size_text, sizeof(size_text), + "%" PRIuMAX, (uintmax_t)size); + } else { + xsnprintf(size_text, sizeof(size_text), "-"); + } + printf("%06o %s %s %7s\t", mode, type_name(type), + find_unique_abbrev(oid, abbrev), size_text); + } else { + printf("%06o %s %s\t", mode, type_name(type), + find_unique_abbrev(oid, abbrev)); + } + baselen = base->len; + strbuf_addstr(base, pathname); + write_name_quoted_relative(base->buf, + chomp_prefix ? ls_tree_prefix : NULL, stdout, + line_termination); + strbuf_setlen(base, baselen); + return 1; +} + static int show_tree(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, void *context) { @@ -78,34 +138,19 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, return recurse; } - if (!(ls_options & LS_NAME_ONLY)) { - if (ls_options & LS_SHOW_SIZE) { - char size_text[24]; - if (type == OBJ_BLOB) { - unsigned long size; - if (oid_object_info(the_repository, oid, &size) == OBJ_BAD) - xsnprintf(size_text, sizeof(size_text), - "BAD"); - else - xsnprintf(size_text, sizeof(size_text), - "%"PRIuMAX, (uintmax_t)size); - } else { - xsnprintf(size_text, sizeof(size_text), "-"); - } - printf("%06o %s %s %7s\t", mode, type_name(type), - find_unique_abbrev(oid, abbrev), - size_text); - } else { - printf("%06o %s %s\t", mode, type_name(type), - find_unique_abbrev(oid, abbrev)); - } + if (shown_fields == FIELD_PATH_NAME) { + baselen = base->len; + strbuf_addstr(base, pathname); + write_name_quoted_relative(base->buf, + chomp_prefix ? ls_tree_prefix : NULL, + stdout, line_termination); + strbuf_setlen(base, baselen); + return recurse; } - baselen = base->len; - strbuf_addstr(base, pathname); - write_name_quoted_relative(base->buf, - chomp_prefix ? ls_tree_prefix : NULL, - stdout, line_termination); - strbuf_setlen(base, baselen); + + if (shown_fields >= FIELD_DEFAULT) + show_default(oid, type, pathname, mode, base); + return recurse; } @@ -123,12 +168,12 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) LS_SHOW_TREES), OPT_SET_INT('z', NULL, &line_termination, N_("terminate entries with NUL byte"), 0), - OPT_CMDMODE('l', "long", &ls_options, N_("include object size"), - LS_SHOW_SIZE), - OPT_CMDMODE(0, "name-only", &ls_options, N_("list only filenames"), - LS_NAME_ONLY), - OPT_CMDMODE(0, "name-status", &ls_options, N_("list only filenames"), - LS_NAME_ONLY), + OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"), + MODE_LONG), + OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"), + MODE_NAME_ONLY), + OPT_CMDMODE(0, "name-status", &cmdmode, N_("list only filenames"), + MODE_NAME_ONLY), OPT_SET_INT(0, "full-name", &chomp_prefix, N_("use full path names"), 0), OPT_BOOL(0, "full-tree", &full_tree, @@ -159,6 +204,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) if (get_oid(argv[0], &oid)) die("Not a valid object name %s", argv[0]); + parse_shown_fields(); + /* * show_recursive() rolls its own matching code and is * generally ignorant of 'struct pathspec'. The magic mask From patchwork Mon Mar 21 07:33:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786941 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 CC9D5C433EF for ; Mon, 21 Mar 2022 07:34:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344935AbiCUHfj (ORCPT ); Mon, 21 Mar 2022 03:35:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344955AbiCUHfd (ORCPT ); Mon, 21 Mar 2022 03:35:33 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C800AA030 for ; Mon, 21 Mar 2022 00:34:05 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id l4-20020a17090a49c400b001c6840df4a3so9858508pjm.0 for ; Mon, 21 Mar 2022 00:34:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZWXgBHpEp0Y9fnYXGP77yCUTSo7XHAg8IOANSnuwhHI=; b=nl6bBlBn8sOYQXf8T2botIYJVC2uBqmZAL5z6lX8Z3iTU4sP48epZFcJefbi9udBvX U43xg+f/3YEF9PFLTFfCqWoEOa5zsacbfslYdWYlhuwbOuuD9IL2qYIJnCJXTgkv9si8 3/KVZNKP4BsVtOZf0tauILxU+TtdC473CEYxxbx09ydnnTYJcGFZx7Ys287bvcBkp+iu dWfSQkCSQP6Oop5OJ6qS8S/pTIotH5fGYn1NHtC9XWHhycCP24JFSDuTnEVuA5kYbZgx h2XMls0vKUrYLks9tPtXCSf1aEjZqBPWa3Rpy5SP8sd4en2r/mu3sBtAmpBKxY9jjEb2 Cxng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZWXgBHpEp0Y9fnYXGP77yCUTSo7XHAg8IOANSnuwhHI=; b=3yaastUUXMZhAMMkrEOt3UWoR3a+1G6/P+ndhPPx5LMvIwMkO9cqp0k0DMINzJ3roS 3xYy7lGSXwGouvoaFyn40yIu/Jri+zA/1cohy/DUrnnjUM5jr6o05Qxb17vShzze9RxK hWF0ycKAsrSlGBhwam4/nx1mOPP3/Id3kXJlKdGXI0kDhGfff09E1wJaxyTOAp86Am9q n/ydjaxPsrYXSCE93+VTkxmZWeu3ZWiiPoKlW2ggyu2uX/H5fiIfwEzd7yKv1mZioZfT x2S4PpAZ1eeYpY7AlVL6XnP7JgSn0M+WH1D34zc9GWjRN9/21RrmC5uXfmq+yglURSdu lDzQ== X-Gm-Message-State: AOAM530gg2G7DlfFpcY1b21NcnxTwQVbqS/8qHbV/TImy3EhznxK36XU USUwv8sGIGh/00BhoOycjRI= X-Google-Smtp-Source: ABdhPJwOqaVu8P6ryDrlsXhkZQavaFo5xH9xuDQPg87No53HNm+OQO963FzGjB9b5bojDZzUmFrFEw== X-Received: by 2002:a17:903:22d1:b0:154:4cfa:2b12 with SMTP id y17-20020a17090322d100b001544cfa2b12mr5317414plg.47.1647848044614; Mon, 21 Mar 2022 00:34:04 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.34.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:34:04 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 09/16] ls-tree: introduce struct "show_tree_data" Date: Mon, 21 Mar 2022 15:33:23 +0800 Message-Id: <85d0b0da1d3b6e8e099a7b78acca57e6a0019a1f.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason "show_tree_data" is a struct that packages the necessary fields for "show_tree()". This commit is a pre-prepared commit for supporting "--format" option and it does not affect any existing functionality. At the same time, we also move global "shown_fields" into "show_tree_data" , the purpose of this is to avoid use both global variable and pass-around struct. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 69 +++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 4364416e1c..0491847b43 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -23,7 +23,6 @@ static int ls_options; static struct pathspec pathspec; static int chomp_prefix; static const char *ls_tree_prefix; -static unsigned int shown_fields; #define FIELD_PATH_NAME 1 #define FIELD_SIZE (1 << 1) #define FIELD_OBJECT_NAME (1 << 2) @@ -32,6 +31,15 @@ static unsigned int shown_fields; #define FIELD_DEFAULT 29 /* 11101 size is not shown to output by default */ #define FIELD_LONG_DEFAULT (FIELD_DEFAULT | FIELD_SIZE) +struct show_tree_data { + unsigned mode; + enum object_type type; + const struct object_id *oid; + const char *pathname; + struct strbuf *base; + unsigned int shown_fields; +}; + static const char * const ls_tree_usage[] = { N_("git ls-tree [] [...]"), NULL @@ -42,19 +50,19 @@ static enum ls_tree_cmdmode { MODE_NAME_ONLY, } cmdmode; -static int parse_shown_fields(void) +static int parse_shown_fields(unsigned int *shown_fields) { if (cmdmode == MODE_NAME_ONLY) { - shown_fields = FIELD_PATH_NAME; + *shown_fields = FIELD_PATH_NAME; return 0; } if (!ls_options || (ls_options & LS_RECURSIVE) || (ls_options & LS_SHOW_TREES) || (ls_options & LS_TREE_ONLY)) - shown_fields = FIELD_DEFAULT; + *shown_fields = FIELD_DEFAULT; if (cmdmode == MODE_LONG) - shown_fields = FIELD_LONG_DEFAULT; + *shown_fields = FIELD_LONG_DEFAULT; return 1; } @@ -88,17 +96,15 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname return 0; } -static int show_default(const struct object_id *oid, enum object_type type, - const char *pathname, unsigned mode, - struct strbuf *base) +static int show_default(struct show_tree_data *data) { - size_t baselen = base->len; + size_t baselen = data->base->len; - if (shown_fields & FIELD_SIZE) { + if (data->shown_fields & FIELD_SIZE) { char size_text[24]; - if (type == OBJ_BLOB) { + if (data->type == OBJ_BLOB) { unsigned long size; - if (oid_object_info(the_repository, oid, &size) == OBJ_BAD) + if (oid_object_info(the_repository, data->oid, &size) == OBJ_BAD) xsnprintf(size_text, sizeof(size_text), "BAD"); else xsnprintf(size_text, sizeof(size_text), @@ -106,18 +112,18 @@ static int show_default(const struct object_id *oid, enum object_type type, } else { xsnprintf(size_text, sizeof(size_text), "-"); } - printf("%06o %s %s %7s\t", mode, type_name(type), - find_unique_abbrev(oid, abbrev), size_text); + printf("%06o %s %s %7s\t", data->mode, type_name(data->type), + find_unique_abbrev(data->oid, abbrev), size_text); } else { - printf("%06o %s %s\t", mode, type_name(type), - find_unique_abbrev(oid, abbrev)); + printf("%06o %s %s\t", data->mode, type_name(data->type), + find_unique_abbrev(data->oid, abbrev)); } - baselen = base->len; - strbuf_addstr(base, pathname); - write_name_quoted_relative(base->buf, + baselen = 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, baselen); return 1; } @@ -127,6 +133,15 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, int recurse = 0; size_t baselen; enum object_type type = object_type(mode); + unsigned int shown_fields = *(unsigned int *)context; + struct show_tree_data data = { + .mode = mode, + .type = type, + .oid = oid, + .pathname = pathname, + .base = base, + .shown_fields = shown_fields, + }; if (type == OBJ_BLOB) { if (ls_options & LS_TREE_ONLY) @@ -148,8 +163,8 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, return recurse; } - if (shown_fields >= FIELD_DEFAULT) - show_default(oid, type, pathname, mode, base); + if (shown_fields>= FIELD_DEFAULT) + show_default(&data); return recurse; } @@ -159,6 +174,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) struct object_id oid; struct tree *tree; int i, full_tree = 0; + unsigned int shown_fields = 0; const struct option ls_tree_options[] = { OPT_BIT('d', NULL, &ls_options, N_("only show trees"), LS_TREE_ONLY), @@ -204,8 +220,6 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) if (get_oid(argv[0], &oid)) die("Not a valid object name %s", argv[0]); - parse_shown_fields(); - /* * show_recursive() rolls its own matching code and is * generally ignorant of 'struct pathspec'. The magic mask @@ -222,6 +236,9 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) tree = parse_tree_indirect(&oid); if (!tree) die("not a tree object"); - return !!read_tree(the_repository, tree, - &pathspec, show_tree, NULL); + + parse_shown_fields(&shown_fields); + + return !!read_tree(the_repository, tree, &pathspec, show_tree, + &shown_fields); } From patchwork Mon Mar 21 07:33:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786942 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 0ECB6C433EF for ; Mon, 21 Mar 2022 07:34:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344957AbiCUHfl (ORCPT ); Mon, 21 Mar 2022 03:35:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344962AbiCUHfh (ORCPT ); Mon, 21 Mar 2022 03:35:37 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E7D5AC918 for ; Mon, 21 Mar 2022 00:34:08 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id c11so9718982pgu.11 for ; Mon, 21 Mar 2022 00:34:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Xjtpr71rmRe863Ta57w9ChYNkXlSn55eFsnOfSKRC7E=; b=TNbvLBZlpSp3leGCL/hsLmBM3vevLDa/gNA4dHFdD7nE02SRUuVLqQSy4jck6VHgaV 7txnpwRwc1taWmsNiyGcRIiswP3mKSpg87V3vb/pKsQSZmoQAGmgInCjiQhVjQCpvZvm m3hgy+5E/egQ/axEhwNMmdr7grZl9rwvJqv5b1NblcCZPrBUo2mhL3z5KiiZZmJugRzS iKkOz+NO4N/t015KBMlhY0hijnCxtGcbaBt8RBm+rdLBLUUGcJ4rv2iOx19fGyxv2RyH ajKQTph2YapecjI/BVKPQnMzm3opl03fSOIvTGzxFSNUa6uwHKkhAuQcw/oj/oznVN1H e5qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Xjtpr71rmRe863Ta57w9ChYNkXlSn55eFsnOfSKRC7E=; b=KDy17nQkqZBd4JKxT1wmwe2yohAE5QBeLZ6S6gBtCrQ0aO0yn3EMWUdDjQRuQHSY0R X+SWzO+BuVruFTuasu6opq9D4ZwZUtFRvXdSxHkN1wLnNCKJhY6Sak3C7pZOoGck6uEL Yt2EESi9KKaeS+9I30vWUgqhkc0m9+OvTIJFECB2Ieg0x6ljQBPwJD1CJ2AEAYnKqsqQ M7XIDSotPkvZsKDbjGvY9xxDQTLA2ULAfmVWJTFtP9cwR1BONjVfKi7N4grQMaR0sAD6 7qzHYEbksTQGdhvHMQRdh4FQFC3ZpBnQtSY2JBxHf+mLqlZN3TgeRZgl1B4S9zsC2aLr 2V8g== X-Gm-Message-State: AOAM530VS0tSxgISp11B/BRUCEoluYwDguI/RwXmfn/27knVkGpbij1F dwD+h07dV6a5Yduw2K315Zk= X-Google-Smtp-Source: ABdhPJxCjW1pqGnFwzChkliitBKJDqJ1wN+9d6cY1TWUb5o7zdLAhMAJV8A8UtCKR40+XyctWrz0mg== X-Received: by 2002:a65:61ad:0:b0:378:8f01:7674 with SMTP id i13-20020a6561ad000000b003788f017674mr16782049pgv.314.1647848047769; Mon, 21 Mar 2022 00:34:07 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.34.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:34:07 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes Schindelin Subject: [PATCH v13 10/16] cocci: allow padding with `strbuf_addf()` Date: Mon, 21 Mar 2022 15:33:24 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Johannes Schindelin A convenient way to pad strings is to use something like `strbuf_addf(&buf, "%20s", "Hello, world!")`. However, the Coccinelle rule that forbids a format `"%s"` with a constant string argument cast too wide a net, and also forbade such padding. The original rule was introduced by commit: 28c23cd4c39 (strbuf.cocci: suggest strbuf_addbuf() to add one strbuf to an other, 2019-01-25) Signed-off-by: Johannes Schindelin Signed-off-by: Teng Long --- contrib/coccinelle/strbuf.cocci | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci index d9ada69b43..0970d98ad7 100644 --- a/contrib/coccinelle/strbuf.cocci +++ b/contrib/coccinelle/strbuf.cocci @@ -15,7 +15,7 @@ constant fmt !~ "%"; @@ expression E; struct strbuf SB; -format F =~ "s"; +format F =~ "^s$"; @@ - strbuf_addf(E, "%@F@", SB.buf); + strbuf_addbuf(E, &SB); @@ -23,7 +23,7 @@ format F =~ "s"; @@ expression E; struct strbuf *SBP; -format F =~ "s"; +format F =~ "^s$"; @@ - strbuf_addf(E, "%@F@", SBP->buf); + strbuf_addbuf(E, SBP); @@ -44,7 +44,7 @@ struct strbuf *SBP; @@ expression E1, E2; -format F =~ "s"; +format F =~ "^s$"; @@ - strbuf_addf(E1, "%@F@", E2); + strbuf_addstr(E1, E2); From patchwork Mon Mar 21 07:33:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786944 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 B5552C433F5 for ; Mon, 21 Mar 2022 07:34:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344949AbiCUHfo (ORCPT ); Mon, 21 Mar 2022 03:35:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344974AbiCUHfi (ORCPT ); Mon, 21 Mar 2022 03:35:38 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 647D4AD10D for ; Mon, 21 Mar 2022 00:34:11 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id l4-20020a17090a49c400b001c6840df4a3so9858722pjm.0 for ; Mon, 21 Mar 2022 00:34:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TlaEpPbYj7ha75k31ab5pIqOEcpDvjqrjTUb42FwGUU=; b=SjxY0u+ybNoaZOew7fBPS+oXDB7yUjDugFAidqGR/xSZQzBkWOC13+3YlxMHM0/pro 6EiLQkJZwCakbakYLJpFnLw3T/9DzOBIVB/OubnvY3DBRh/s8Tf6RFz34eaQRXsDZszd t+txWjDdUcrD0pPLnMOUbgoO9/n83MAyjtIvifv/fIhrOHB//6dqvy+om5dvVKXjAOzt X6BnpGrt39ifqM72xW16pKiHGr9kEfpA/czEqosMeixFiAuAiq+xpxVNGJ4/g8TBDpUF c+6VsW76LWW9KcD34uxg0svEs51rCeKhL+I/ierUALx5feaIVHbTt/vaZnVpfa9CBjWe s65A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TlaEpPbYj7ha75k31ab5pIqOEcpDvjqrjTUb42FwGUU=; b=ng1C2VBQ1IQL1+IImqmhtFl0gaOG0gQMHsy9UnVtVvK4L6yEWjDHDIm81uQ/U4G0m7 hilbi4DYYr4ML8ZuE5baFuHuuxEs3WN7XbXTePJeuuO2Uh3E/Tar/rCkDfMjsHwUT8FT DN1CUVTPdcCtqMYXAPzvfdoaJtrKsgKYVeS2DRXNouyQQGTRNfNKvzvyBF8XHy3SSluc L3IrLpf9HGjjxzoFoqv4ku4fl9Gpz2Abmx9M4+auhQ1yC+p5OaF5QolRf9XKGVJb2D8N sjvIXX+QL4NPyuc+WWmpPCkK/g6OKbVzuei7mih/Xx2VBFN4PxkTs6l+zkjp1tuXYs6v OZFg== X-Gm-Message-State: AOAM5306aubwt+esokX1qV/ATQTyzoqeilh5gI2/dmDyYR9pR7xOT1Gm iPQeFavhKpPlzmsV7aExBgE= X-Google-Smtp-Source: ABdhPJwxqM+HNsNF1A+4yLRhgssoZ/HUB+kopvw1c9FkXhcfi8e0U7Ja0M8wYVC8xOPFjUgTY89QgA== X-Received: by 2002:a17:902:cec8:b0:154:6dd6:255d with SMTP id d8-20020a170902cec800b001546dd6255dmr744564plg.62.1647848050808; Mon, 21 Mar 2022 00:34:10 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.34.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:34:10 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 11/16] ls-tree: introduce "--format" option Date: Mon, 21 Mar 2022 15:33:25 +0800 Message-Id: <7451242daae018c629a62cf2fdb5631b556ad3b8.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason Add a --format option to ls-tree. It has an existing default output, and then --long and --name-only options to emit the default output along with the objectsize and, or to only emit object paths. Rather than add --type-only, --object-only etc. we can just support a --format using a strbuf_expand() similar to "for-each-ref --format". We might still add such options in the future for convenience. The --format implementation is slower than the existing code, but this change does not cause any performance regressions. We'll leave the existing show_tree() unchanged, and only run show_tree_fmt() in if a --format different than the hardcoded built-in ones corresponding to the existing modes is provided. I.e. something like the "--long" output would be much slower with this, mainly due to how we need to allocate various things to do with quote.c instead of spewing the output directly to stdout. The new option of '--format' comes from Ævar Arnfjörð Bjarmasonn's idea and suggestion, this commit makes modifications in terms of the original discussion on community [1]. In [1] there was a "GIT_TEST_LS_TREE_FORMAT_BACKEND" variable to ensure that we had test coverage for passing tests that would otherwise use show_tree() through show_tree_fmt(), and thus that the formatting mechanism could handle all the same cases as the non-formatting options. Somewhere in subsequent re-rolls of that we seem to have drifted away from what the goal of these tests should be. We're trying to ensure correctness of show_tree_fmt(). We can't tell if we "hit [the] fast-path" here, and instead of having an explicit test for that, we can just add it to something our "test_ls_tree_format" tests for. Here is the statistics about performance tests: 1. Default format (hitten the builtin formats): "git ls-tree " vs "--format='%(mode) %(type) %(object)%x09%(file)'" $hyperfine --warmup=10 "/opt/git/master/bin/git ls-tree -r HEAD" Benchmark 1: /opt/git/master/bin/git ls-tree -r HEAD Time (mean ± σ): 105.2 ms ± 3.3 ms [User: 84.3 ms, System: 20.8 ms] Range (min … max): 99.2 ms … 113.2 ms 28 runs $hyperfine --warmup=10 "/opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='%(mode) %(type) %(object)%x09%(file)' HEAD" Benchmark 1: /opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='%(mode) %(type) %(object)%x09%(file)' HEAD Time (mean ± σ): 106.4 ms ± 2.7 ms [User: 86.1 ms, System: 20.2 ms] Range (min … max): 100.2 ms … 110.5 ms 29 runs 2. Default format includes object size (hitten the builtin formats): "git ls-tree -l " vs "--format='%(mode) %(type) %(object) %(size:padded)%x09%(file)'" $hyperfine --warmup=10 "/opt/git/master/bin/git ls-tree -r -l HEAD" Benchmark 1: /opt/git/master/bin/git ls-tree -r -l HEAD Time (mean ± σ): 335.1 ms ± 6.5 ms [User: 304.6 ms, System: 30.4 ms] Range (min … max): 327.5 ms … 348.4 ms 10 runs $hyperfine --warmup=10 "/opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='%(mode) %(type) %(object) %(size:padded)%x09%(file)' HEAD" Benchmark 1: /opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='%(mode) %(type) %(object) %(size:padded)%x09%(file)' HEAD Time (mean ± σ): 337.2 ms ± 8.2 ms [User: 309.2 ms, System: 27.9 ms] Range (min … max): 328.8 ms … 349.4 ms 10 runs Links: [1] https://public-inbox.org/git/RFC-patch-6.7-eac299f06ff-20211217T131635Z-avarab@gmail.com/ [2] https://lore.kernel.org/git/cb717d08be87e3239117c6c667cb32caabaad33d.1646390152.git.dyroneteng@gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- Documentation/git-ls-tree.txt | 59 ++++++++++++++-- builtin/ls-tree.c | 129 +++++++++++++++++++++++++++++++++- t/t3104-ls-tree-format.sh | 67 ++++++++++++++++++ 3 files changed, 248 insertions(+), 7 deletions(-) create mode 100755 t/t3104-ls-tree-format.sh diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt index db02d6d79a..db29a9efb5 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] [--full-name] [--full-tree] [--abbrev[=]] + [--name-only] [--name-status] [--full-name] [--full-tree] [--abbrev[=]] [--format=] [...] DESCRIPTION @@ -74,6 +74,16 @@ OPTIONS Do not limit the listing to the current working directory. Implies --full-name. +--format=:: + A string that interpolates `%(fieldname)` from the result + being shown. It also interpolates `%%` to `%`, and + `%xx` where `xx` are hex digits interpolates to character + with hex code `xx`; for example `%00` interpolates to + `\0` (NUL), `%09` to `\t` (TAB) and `%0a` to `\n` (LF). + When specified, `--format` cannot be combined with other + format-altering options, including `--long`, `--name-only` + and `--object-only`. + [...]:: When paths are given, show them (note that this isn't really raw pathnames, but rather a list of patterns to match). Otherwise @@ -82,16 +92,29 @@ OPTIONS Output Format ------------- - SP SP TAB + +The output format of `ls-tree` is determined by either the `--format` +option, or other format-altering options such as `--name-only` etc. +(see `--format` above). + +The use of certain `--format` directives is equivalent to using those +options, but invoking the full formatting machinery can be slower than +using an appropriate formatting option. + +In cases where the `--format` would exactly map to an existing option +`ls-tree` will use the appropriate faster path. Thus the default format +is equivalent to: + + %(objectmode) %(objecttype) %(objectname)%x09%(path) This output format is compatible with what `--index-info --stdin` of 'git update-index' expects. When the `-l` option is used, format changes to - SP SP SP TAB + %(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path) -Object size identified by is given in bytes, and right-justified +Object size identified by is given in bytes, and right-justified with minimum width of 7 characters. Object size is given only for blobs (file) entries; for other entries `-` character is used in place of size. @@ -100,6 +123,34 @@ quoted as explained for the configuration variable `core.quotePath` (see linkgit:git-config[1]). Using `-z` the filename is output verbatim and the line is terminated by a NUL byte. +Customized format: + +It is possible to print in a custom format by using the `--format` option, +which is able to interpolate different fields using a `%(fieldname)` notation. +For example, if you only care about the "objectname" and "path" fields, you +can execute with a specific "--format" like + + git ls-tree --format='%(objectname) %(path)' + +FIELD NAMES +----------- + +Various values from structured fields can be used to interpolate +into the resulting output. For each outputing line, the following +names can be used: + +objectmode:: + The mode of the object. +objecttype:: + The type of the object (`blob` or `tree`). +objectname:: + The name of the object. +objectsize[:padded]:: + The size of the object ("-" if it's a tree). + It also supports a padded format of size with "%(size:padded)". +path:: + The pathname of the object. + GIT --- Part of the linkgit:git[1] suite diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 0491847b43..182705e894 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -30,7 +30,10 @@ static const char *ls_tree_prefix; #define FIELD_MODE (1 << 4) #define FIELD_DEFAULT 29 /* 11101 size is not shown to output by default */ #define FIELD_LONG_DEFAULT (FIELD_DEFAULT | FIELD_SIZE) - +static const char *format; +static const char *default_format = "%(objectmode) %(objecttype) %(objectname)%x09%(path)"; +static const char *long_format = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)"; +static const char *name_only_format = "%(path)"; struct show_tree_data { unsigned mode; enum object_type type; @@ -50,6 +53,72 @@ static enum ls_tree_cmdmode { MODE_NAME_ONLY, } cmdmode; +static void expand_objectsize(struct strbuf *line, const struct object_id *oid, + const enum object_type type, unsigned int padded) +{ + if (type == OBJ_BLOB) { + unsigned long size; + if (oid_object_info(the_repository, oid, &size) < 0) + die(_("could not get object info about '%s'"), + oid_to_hex(oid)); + if (padded) + strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size); + else + strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size); + } else if (padded) { + strbuf_addf(line, "%7s", "-"); + } else { + strbuf_addstr(line, "-"); + } +} + +static size_t expand_show_tree(struct strbuf *sb, const char *start, + void *context) +{ + struct show_tree_data *data = context; + const char *end; + const char *p; + unsigned int errlen; + size_t len = strbuf_expand_literal_cb(sb, start, NULL); + + if (len) + return len; + if (*start != '(') + die(_("bad ls-tree format: element '%s' does not start with '('"), start); + + end = strchr(start + 1, ')'); + if (!end) + die(_("bad ls-tree format: element '%s' does not end in ')'"), start); + + len = end - start + 1; + if (skip_prefix(start, "(objectmode)", &p)) { + strbuf_addf(sb, "%06o", data->mode); + } else if (skip_prefix(start, "(objecttype)", &p)) { + strbuf_addstr(sb, type_name(data->type)); + } else if (skip_prefix(start, "(objectsize:padded)", &p)) { + expand_objectsize(sb, data->oid, data->type, 1); + } 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); + } else if (skip_prefix(start, "(path)", &p)) { + const char *name = data->base->buf; + const char *prefix = chomp_prefix ? ls_tree_prefix : NULL; + struct strbuf quoted = STRBUF_INIT; + struct strbuf sbuf = STRBUF_INIT; + strbuf_addstr(data->base, data->pathname); + name = relative_path(data->base->buf, prefix, &sbuf); + quote_c_style(name, "ed, NULL, 0); + strbuf_addbuf(sb, "ed); + strbuf_release(&sbuf); + strbuf_release("ed); + } else { + errlen = (unsigned long)len; + die(_("bad ls-tree format: %%%.*s"), errlen, start); + } + return len; +} + static int parse_shown_fields(unsigned int *shown_fields) { if (cmdmode == MODE_NAME_ONLY) { @@ -96,6 +165,38 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname return 0; } +static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, + const char *pathname, unsigned mode, void *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, + .oid = oid, + .pathname = pathname, + .base = base, + }; + + if (type == OBJ_TREE && show_recursive(base->buf, base->len, pathname)) + recurse = READ_TREE_RECURSIVE; + if (type == OBJ_TREE && recurse && !(ls_options & LS_SHOW_TREES)) + return recurse; + if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) + return 0; + + baselen = base->len; + strbuf_expand(&sb, format, expand_show_tree, &data); + strbuf_addch(&sb, line_termination); + fwrite(sb.buf, sb.len, 1, stdout); + strbuf_release(&sb); + strbuf_setlen(base, baselen); + return recurse; +} + static int show_default(struct show_tree_data *data) { size_t baselen = data->base->len; @@ -175,6 +276,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) struct tree *tree; int i, full_tree = 0; unsigned int shown_fields = 0; + read_tree_fn_t fn = show_tree; const struct option ls_tree_options[] = { OPT_BIT('d', NULL, &ls_options, N_("only show trees"), LS_TREE_ONLY), @@ -195,6 +297,9 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) 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"), + N_("format to use for the output"), + PARSE_OPT_NONEG), OPT__ABBREV(&abbrev), OPT_END() }; @@ -215,6 +320,10 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options)) ls_options |= LS_SHOW_TREES; + if (format && cmdmode) + usage_msg_opt( + _("--format can't be combined with other format-altering options"), + ls_tree_usage, ls_tree_options); if (argc < 1) usage_with_options(ls_tree_usage, ls_tree_options); if (get_oid(argv[0], &oid)) @@ -239,6 +348,20 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) parse_shown_fields(&shown_fields); - return !!read_tree(the_repository, tree, &pathspec, show_tree, - &shown_fields); + /* + * The generic show_tree_fmt() is slower than show_tree(), so + * take the fast path if possible. + */ + if (format && (!strcmp(format, default_format))) { + fn = show_tree; + } else if (format && (!strcmp(format, long_format))) { + shown_fields = shown_fields | FIELD_SIZE; + fn = show_tree; + } else if (format && (!strcmp(format, name_only_format))) { + shown_fields = FIELD_PATH_NAME; + fn = show_tree; + } else if (format) + fn = show_tree_fmt; + + return !!read_tree(the_repository, tree, &pathspec, fn, &shown_fields); } diff --git a/t/t3104-ls-tree-format.sh b/t/t3104-ls-tree-format.sh new file mode 100755 index 0000000000..7f1eb699d3 --- /dev/null +++ b/t/t3104-ls-tree-format.sh @@ -0,0 +1,67 @@ +#!/bin/sh + +test_description='ls-tree --format' + +TEST_PASSES_SANITIZE_LEAK=true +. ./test-lib.sh + +test_expect_success 'ls-tree --format usage' ' + test_expect_code 129 git ls-tree --format=fmt -l HEAD && + test_expect_code 129 git ls-tree --format=fmt --name-only HEAD && + test_expect_code 129 git ls-tree --format=fmt --name-status HEAD +' + +test_expect_success 'setup' ' + mkdir dir && + test_commit dir/sub-file && + test_commit top-file +' + +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 && + git ls-tree --format="$format" -r $fmtopts HEAD >actual && + test_cmp expect actual + ' + + test_expect_success "ls-tree '--format=<$format>' on optimized v.s. non-optimized path" ' + git ls-tree --format="$format" -r $fmtopts HEAD >expect && + git ls-tree --format="> $format" -r $fmtopts HEAD >actual.raw && + sed "s/^> //" >actual X-Patchwork-Id: 12786945 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 AF4C1C433EF for ; Mon, 21 Mar 2022 07:34:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344960AbiCUHfq (ORCPT ); Mon, 21 Mar 2022 03:35:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242322AbiCUHfj (ORCPT ); Mon, 21 Mar 2022 03:35:39 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F2B715FD7 for ; Mon, 21 Mar 2022 00:34:14 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id n15so11986982plh.2 for ; Mon, 21 Mar 2022 00:34:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ozDbs5sUwie0NM1zyriGBx2cZxocADZ+d/+QmMBj+NM=; b=FxsAgUuxLbmOdsRNbAn5qZjO72Hi7C78xZ7OvXywaauSFgQqvnVZy7fzoOryLEOFqn lbhOcs2f38IvbWURx+nvozUbsyqmLtVU9eh1Kl6/6syRajGnXz18au5G0rm8thKafKe6 jOyTOqjrVguoWsx+qOdM/aNMPwMIjiTcRpWB2DnJlU8Y/jvj/2tY5reemJ3e/1DeqCiV 1HtNjAYP15e13naKKtsR6CZaNJwMtHokgI8/Xws6bzovdYGPvCjafuEsUeq1GZO2lJzl /hN403xiGFnY1Gw/jL6GmT4OBs1nJzYdhkv6VCRnQ/uOq2j2xkP6CNVZlN/5Pap+ZXQD 809g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ozDbs5sUwie0NM1zyriGBx2cZxocADZ+d/+QmMBj+NM=; b=LbND5mJTjCTHYlzRVUfD7+0xLYoc4Z9+9TF1ZMBg/3ApJAVCHBVJUUsJShzHYOwCCR KZqr295BIaDokBzM/hK7gxGAk/yY4ud4z0eUylSVDWJHU1zXoSOW7bfkEUzVLWSUxiR9 bw7Re4cEZTF+GDRFSCqbhYuJTAHSvJaww8+CYJOa34pVSFx3jRtVJ/8E3UMQwndTIzVB qhZazQhkcv5xktU44kLhhVzbZuKp2xJYdlsJ+N8gpJ83H6dJPh0NA2x+yOAySDy0A+FQ NDu9Vr2faYmZQveuXgWt+iy0H6Dx/SKHfM48cIq9b5dXJb8zrvXYkkcOw7oIE9QGwmjX iBAw== X-Gm-Message-State: AOAM532bfGXi6eUheC6/C/uOZKoL6MimuRWl6zwcHXnfU2swLtsIBFnN VQjALS7P6PzPGmyA8+YLXXg= X-Google-Smtp-Source: ABdhPJzCkNlPyU6UpyEPD8lg02Z6FfVl5JaCg2IbnOhDGpAv31BLqsTbMBFkpCmy9x9h2ONb2j/grw== X-Received: by 2002:a17:902:e846:b0:154:3d19:c5ae with SMTP id t6-20020a170902e84600b001543d19c5aemr8097338plg.87.1647848053758; Mon, 21 Mar 2022 00:34:13 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.34.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:34:13 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 12/16] ls-tree: support --object-only option for "git-ls-tree" Date: Mon, 21 Mar 2022 15:33:26 +0800 Message-Id: <2bcd08ebd0a0126f50be677884a02603b9a96a2d.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org '--object-only' is an alias for '--format=%(objectname)'. It cannot be used together other format-altering options like '--name-only', '--long' or '--format', they are mutually exclusive. The "--name-only" option outputs only. Likewise, is another high frequency used field, so implement '--object-only' option will bring intuitive and clear semantics for this scenario. Using '--format=%(objectname)' we can achieve a similar effect, but the former is with a lower learning cost(without knowing the format requirement of '--format' option). Even so, if a user is prefer to use "--format=%(objectname)", this is entirely welcome because they are not only equivalent in function, but also have almost identical performance. The reason is this commit also add the specific of "--format=%(objectname)" to the current fast-pathes (builtin formats) to avoid running unnecessary parsing mechanisms. The following performance benchmarks are based on torvalds/linux.git: When hit the fast-path: Benchmark 1: /opt/git/ls-tree-oid-only/bin/git ls-tree -r --object-only HEAD Time (mean ± σ): 83.6 ms ± 2.0 ms [User: 59.4 ms, System: 24.1 ms] Range (min … max): 80.4 ms … 87.2 ms 35 runs Benchmark 1: /opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='%(objectname)' HEAD Time (mean ± σ): 84.1 ms ± 1.8 ms [User: 61.7 ms, System: 22.3 ms] Range (min … max): 80.9 ms … 87.5 ms 35 runs But for a customized format, it will be slower: Benchmark 1: /opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='oid: %(objectname)' HEAD Time (mean ± σ): 96.5 ms ± 2.5 ms [User: 72.9 ms, System: 23.5 ms] Range (min … max): 93.1 ms … 104.1 ms 31 runs Signed-off-by: Teng Long --- Documentation/git-ls-tree.txt | 11 ++++++++++- builtin/ls-tree.c | 17 ++++++++++++++++- t/t3103-ls-tree-misc.sh | 7 +++++-- t/t3104-ls-tree-format.sh | 9 +++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt index db29a9efb5..1aea998e72 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] [--full-name] [--full-tree] [--abbrev[=]] [--format=] + [--name-only] [--name-status] [--object-only] [--full-name] [--full-tree] [--abbrev[=]] [--format=] [...] DESCRIPTION @@ -59,6 +59,15 @@ OPTIONS --name-only:: --name-status:: List only filenames (instead of the "long" output), one per line. + Cannot be combined with `--object-only`. + +--object-only:: + List only names of the objects, one per line. Cannot be combined + with `--name-only` or `--name-status`. + This is equivalent to specifying `--format='%(objectname)'`, but + for both this option and that exact format the command takes a + hand-optimized codepath instead of going through the generic + formatting mechanism. --abbrev[=]:: Instead of showing the full 40-byte hexadecimal object diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 182705e894..1aa65cc87f 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -34,6 +34,7 @@ static const char *format; static const char *default_format = "%(objectmode) %(objecttype) %(objectname)%x09%(path)"; static const char *long_format = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)"; static const char *name_only_format = "%(path)"; +static const char *object_only_format = "%(objectname)"; struct show_tree_data { unsigned mode; enum object_type type; @@ -51,6 +52,7 @@ static const char * const ls_tree_usage[] = { static enum ls_tree_cmdmode { MODE_LONG = 1, MODE_NAME_ONLY, + MODE_OBJECT_ONLY, } cmdmode; static void expand_objectsize(struct strbuf *line, const struct object_id *oid, @@ -125,7 +127,10 @@ static int parse_shown_fields(unsigned int *shown_fields) *shown_fields = FIELD_PATH_NAME; return 0; } - + if (cmdmode == MODE_OBJECT_ONLY) { + *shown_fields = FIELD_OBJECT_NAME; + return 0; + } if (!ls_options || (ls_options & LS_RECURSIVE) || (ls_options & LS_SHOW_TREES) || (ls_options & LS_TREE_ONLY)) @@ -254,6 +259,11 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, return recurse; } + if (shown_fields == FIELD_OBJECT_NAME) { + printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination); + return recurse; + } + if (shown_fields == FIELD_PATH_NAME) { baselen = base->len; strbuf_addstr(base, pathname); @@ -292,6 +302,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) MODE_NAME_ONLY), OPT_CMDMODE(0, "name-status", &cmdmode, N_("list only filenames"), MODE_NAME_ONLY), + OPT_CMDMODE(0, "object-only", &cmdmode, N_("list only objects"), + MODE_OBJECT_ONLY), OPT_SET_INT(0, "full-name", &chomp_prefix, N_("use full path names"), 0), OPT_BOOL(0, "full-tree", &full_tree, @@ -360,6 +372,9 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) } else if (format && (!strcmp(format, name_only_format))) { shown_fields = FIELD_PATH_NAME; fn = show_tree; + } else if (format && (!strcmp(format, object_only_format))) { + shown_fields = FIELD_OBJECT_NAME; + fn = show_tree; } else if (format) fn = show_tree_fmt; diff --git a/t/t3103-ls-tree-misc.sh b/t/t3103-ls-tree-misc.sh index d9d7fa932f..d979c0df5d 100755 --- a/t/t3103-ls-tree-misc.sh +++ b/t/t3103-ls-tree-misc.sh @@ -25,11 +25,14 @@ test_expect_success 'ls-tree fails with non-zero exit code on broken tree' ' for opts in \ "--name-only --long" \ - "--name-status --long" + "--name-status --long" \ + "--name-only --object-only" \ + "--name-status --object-only" \ + "--object-only --long" \ + "--object-only --format" do test_expect_success "usage: incompatible options: $opts" ' test_expect_code 129 git ls-tree $opts $tree ' done - test_done diff --git a/t/t3104-ls-tree-format.sh b/t/t3104-ls-tree-format.sh index 7f1eb699d3..0769a933d6 100755 --- a/t/t3104-ls-tree-format.sh +++ b/t/t3104-ls-tree-format.sh @@ -49,6 +49,15 @@ test_ls_tree_format \ "%(path)" \ "--name-only" +test_ls_tree_format \ + "%(objectname)" \ + "--object-only" + +test_ls_tree_format \ + "%(objectname)" \ + "--object-only --abbrev" \ + "--abbrev" + test_ls_tree_format \ "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ "-t" \ From patchwork Mon Mar 21 07:33:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786946 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 D0EF4C433F5 for ; Mon, 21 Mar 2022 07:34:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344962AbiCUHfr (ORCPT ); Mon, 21 Mar 2022 03:35:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344958AbiCUHfl (ORCPT ); Mon, 21 Mar 2022 03:35:41 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55319AA02F for ; Mon, 21 Mar 2022 00:34:17 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id mp6-20020a17090b190600b001c6841b8a52so9674259pjb.5 for ; Mon, 21 Mar 2022 00:34:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xdahYEsNby27wbywjbp5s109PyZ4kfrEEAWM0hAma2k=; b=fcd7FoWJyzQ+/XG4GS8HVFnYyGiIa4VXNGP20T7I0RGq2MmoZmh/c4WrpE/PaCLd7A A/JnWW3wSsKxGQX0lJhvSfk7SC+H8IDa9wTvOKv71qTwMo0wBXtQo3wt1lWZdHa+gCct jqn8M59jQ4CQ5uUL3G/8242xHLmh8JsILIlWNbXVwMNY+s5VX2vaIJ2AfN3mhciBM1aR XOEQF14p+OHl75RAdbS24GtiVSl7WsaJsFzXWXVWXR+qUEE3sPsEzcM4T5Oom4oWocY/ LiBbUNbkiD2TUOXDOYmoWL4l1kQ+jb3Z6fMIctk/4Me3N6iLcD9NV/QhddS3Pz2KPHvP dJJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xdahYEsNby27wbywjbp5s109PyZ4kfrEEAWM0hAma2k=; b=AF3/lFNowWoyh+c/ptHJ8NM6eRXBrVK9MR573uA8/p5WoxFfVBGwFaXcXkG5mrrruz t58AR5wyQkJCo7gHL9zmJaoQPY8bBZnP67tE2B/oocXreZEpf1XGvpf6fVk4m6mTU4DV ZoYWcVFVAG8cQnuDk67vcF+kqJ7reDL2L0mn4zrf5rQBpZ+izMMAqxvjrWXro3puLgpW 6JnlxaNcQWRr83PRal5+OcoU4GIoJfp/lWc9SNoFALaDTvGJfKMb7onSBM8vB3m6xOCD qezjx6xD8Dg1zE+vislCiA++8i0VSVYVDg1r7wIgbEWQYKfkWEUjJnhXOP4DSBMBnZbw b4Fw== X-Gm-Message-State: AOAM531MULgjsXQPysGAWYaSnfP8msNsbI2Rw5QBoaonEnRJyBMUAN/X r0UBpFfbZS/bhNc4TatOujM= X-Google-Smtp-Source: ABdhPJzO/sLhx7xIRXcfKxvwr+ATCsQ1Swk4Duw5CSFyqIhB3EGznq3FIAvnxN3cThL4llrn7uugvQ== X-Received: by 2002:a17:903:1c2:b0:154:5edf:56d3 with SMTP id e2-20020a17090301c200b001545edf56d3mr3133439plh.10.1647848056786; Mon, 21 Mar 2022 00:34:16 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.34.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:34:16 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 13/16] ls-tree tests: add tests for --name-status Date: Mon, 21 Mar 2022 15:33:27 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason The --name-status synonym for --name-only added in c639a5548a5 (ls-tree: --name-only, 2005-12-01) had no tests, let's make sure it works the same way as its sibling. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- t/t3101-ls-tree-dirname.sh | 55 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/t/t3101-ls-tree-dirname.sh b/t/t3101-ls-tree-dirname.sh index 05fde64225..217006d1bf 100755 --- a/t/t3101-ls-tree-dirname.sh +++ b/t/t3101-ls-tree-dirname.sh @@ -201,31 +201,34 @@ EOF test_cmp expected check ' -test_expect_success 'ls-tree --name-only' ' - git ls-tree --name-only $tree >current && - cat >expected <<\EOF && -1.txt -2.txt -path0 -path1 -path2 -path3 -EOF - test_output -' - -test_expect_success 'ls-tree --name-only -r' ' - git ls-tree --name-only -r $tree >current && - cat >expected <<\EOF && -1.txt -2.txt -path0/a/b/c/1.txt -path1/b/c/1.txt -path2/1.txt -path3/1.txt -path3/2.txt -EOF - test_output -' +for opt in --name-only --name-status +do + test_expect_success "ls-tree $opt" ' + git ls-tree $opt $tree >current && + cat >expected <<-\EOF && + 1.txt + 2.txt + path0 + path1 + path2 + path3 + EOF + test_output + ' + + test_expect_success "ls-tree $opt -r" ' + git ls-tree $opt -r $tree >current && + cat >expected <<-\EOF && + 1.txt + 2.txt + path0/a/b/c/1.txt + path1/b/c/1.txt + path2/1.txt + path3/1.txt + path3/2.txt + EOF + test_output + ' +done test_done From patchwork Mon Mar 21 07:33:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786947 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 B5A50C433F5 for ; Mon, 21 Mar 2022 07:34:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344964AbiCUHfu (ORCPT ); Mon, 21 Mar 2022 03:35:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344951AbiCUHfo (ORCPT ); Mon, 21 Mar 2022 03:35:44 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C319192B4 for ; Mon, 21 Mar 2022 00:34:20 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id s11so14669596pfu.13 for ; Mon, 21 Mar 2022 00:34:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MErRpfnVkg7CEB9VOLwBllqQC8cdkdTVDeLq8PZFpns=; b=B5E3RaayK18lRoLnS3BW40+olOj8y3+CxOjK+A4KDU3zkFuAyNhYJqmwF/YVKvK560 OhPCe0ZXrBItiyxqk2WQxLWd/vOY1Uje0BdP4mujUOD0DiRHV31ipH8vjWarzwYIhUMa uvfVvGRI1RGvU11ZiDTOXWvQpf/hTUAzxKSVbwv4MpHpX4XTEvJuKFUKjO2LUhwFLJG6 13E33qsI9ZBJ6K1lNx59yZjjoJZOesuoaAbA+6BF6aiwmIIhYorcmdQI0/LCweUXNFJD +hi7WPqsXAPxdY59iI7DJwbYfGRd1oNXtqSNJ5R9SnOYhnV8KyxnbnYVH2lw4ryjyDJD d2XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MErRpfnVkg7CEB9VOLwBllqQC8cdkdTVDeLq8PZFpns=; b=zu+6wS35GZX2QGHPI8+zP24QAJL5oIyNnhPTTKQsxUktqN8LF0hTR3TruMWZgAFlIG VERFqrsj1hnUig90PDSmcN9Xubg1LT1EnqZeazShcXR5cOgSrVt+VBvwhhn5QnjsFlug e3A3hR+8y3JFZRfPVhL+xlDbPkxsIDk43JYQDCVeSZ+lGeuX5lW6oajgCAnmQVd1VbOS rF8DEatv28pmvcd8HbTWUJ1v7ctkFQURlf+jrzeYsnn4jD+XIZKNahJFkaoFzhtDpCOp Os5XXTm6reS8Z3Rhqvu48fOgi3LehFpphzRiPNegBdNLkcL104XCnHb/hVlr2usKuj2C ib+g== X-Gm-Message-State: AOAM5328KFoW1ckQrQ1tF5R6wtuXxaUTA+Vk+hq9TwCzJ+ehQno8fuYB BvugHmX42ASYksMXqll27Xo= X-Google-Smtp-Source: ABdhPJyuNrqpGYt/uFkkBQSeHCITeHOOHkPbnshvi6/moCimQiunYOmiu764KRd0Q/0Ayk8uqLonwg== X-Received: by 2002:a63:f10c:0:b0:382:623b:3bb9 with SMTP id f12-20020a63f10c000000b00382623b3bb9mr5977964pgi.97.1647848059706; Mon, 21 Mar 2022 00:34:19 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.34.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:34:19 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 14/16] ls-tree: detect and error on --name-only --name-status Date: Mon, 21 Mar 2022 15:33:28 +0800 Message-Id: <89402a851806aabfcde6ebcbcb2bc16826d7ed4e.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason The --name-only and --name-status options are synonyms, but let's detect and error if both are provided. In addition let's add explicit --format tests for the combination of these various options. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 11 ++++++++++- t/t3103-ls-tree-misc.sh | 15 +++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 1aa65cc87f..a271941540 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -52,6 +52,7 @@ static const char * const ls_tree_usage[] = { static enum ls_tree_cmdmode { MODE_LONG = 1, MODE_NAME_ONLY, + MODE_NAME_STATUS, MODE_OBJECT_ONLY, } cmdmode; @@ -301,7 +302,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"), MODE_NAME_ONLY), OPT_CMDMODE(0, "name-status", &cmdmode, N_("list only filenames"), - MODE_NAME_ONLY), + MODE_NAME_STATUS), OPT_CMDMODE(0, "object-only", &cmdmode, N_("list only objects"), MODE_OBJECT_ONLY), OPT_SET_INT(0, "full-name", &chomp_prefix, @@ -327,6 +328,14 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) ls_tree_prefix = prefix = NULL; chomp_prefix = 0; } + /* + * We wanted to detect conflicts between --name-only and + * --name-status, but once we're done with that subsequent + * code should only need to check the primary name. + */ + if (cmdmode == MODE_NAME_STATUS) + cmdmode = MODE_NAME_ONLY; + /* -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)) diff --git a/t/t3103-ls-tree-misc.sh b/t/t3103-ls-tree-misc.sh index d979c0df5d..7454ebbb00 100755 --- a/t/t3103-ls-tree-misc.sh +++ b/t/t3103-ls-tree-misc.sh @@ -24,15 +24,18 @@ test_expect_success 'ls-tree fails with non-zero exit code on broken tree' ' ' for opts in \ - "--name-only --long" \ - "--name-status --long" \ - "--name-only --object-only" \ + "--long --name-only" \ + "--name-only --name-status" \ "--name-status --object-only" \ - "--object-only --long" \ - "--object-only --format" + "--object-only --long" do test_expect_success "usage: incompatible options: $opts" ' test_expect_code 129 git ls-tree $opts $tree - ' + ' + + one_opt=$(echo "$opts" | cut -d' ' -f1) + test_expect_success "usage: incompatible options: $one_opt and --format" ' + test_expect_code 129 git ls-tree $one_opt --format=fmt $tree + ' done test_done From patchwork Mon Mar 21 07:33:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786948 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 EA559C433EF for ; Mon, 21 Mar 2022 07:34:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344975AbiCUHfx (ORCPT ); Mon, 21 Mar 2022 03:35:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344968AbiCUHft (ORCPT ); Mon, 21 Mar 2022 03:35:49 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E3DFAD10F for ; Mon, 21 Mar 2022 00:34:23 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id p5so8598638pfo.5 for ; Mon, 21 Mar 2022 00:34:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Xt4DXtThObflsr/SrL/svQNyy+x2Tjz5S/vXMdKxLsk=; b=XJ9iUbO6iwXcNaTYccJYcJz30s1Ew7CotQNU0xbMcW+LmwzraQjjbgy2cDKj6WwCvA DhAKFrW4udU40pNIjEbiEn4fMTPywXxkncnIMpySVvCoXqHF255tGjE01djNKKUVAnfS ZTLw9zzwBF0NUFY7OBPTcutgIu/oWOiH7iViy2ldRjK+fviS0WhCfQe2RICYPx6xh2gF 0hRzdTVTR/6lX4u4cLKa44t5i0ZuToIKuU2/9jk5DZnhkGi/zDw5feOruFidaSjdvr4t rB6oz1/LLz5ZzhloYWLUq045DOq6vVsFV2HYpOoESpaNMN1kiRCpxZ7Ano6bZgHPmWNX l4vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Xt4DXtThObflsr/SrL/svQNyy+x2Tjz5S/vXMdKxLsk=; b=np4C/IOSL9DXBRz+4rlwz+Tu34jKvioAduy89U3h4e2go3or2uS3Dah2O0HfELK1Lb DlTlhOfdDEeSyMoNXRzHn2v+nr0KLn/FEHEHhYedokzGzoOWurbbS0WwMtDDQARyWzGH fS4UkdOH2Q3mPpsadADoUee3eNNltlt6aY5S+VuFmWDFS0cPjHphMd+s4m64aBN8M+pJ xKhATgAaRzD8pHalDn0qO44/r2DR2jjIzsyynIryxOkVHt85aYKVvtFGi6lp/P30oBYg jgSiUfhPHkAOHoF/Mg2rPUZ013lpNnF/rHyiru0VAsyECJkcXxW7qrWGe3uTqEEOwkFk aANQ== X-Gm-Message-State: AOAM5304nvATgHWdqTCNgHFsvwzwp0wdbvj+MnoWF1Jqj84nXb/PREQb rnP8iIhj/Y3u/UW0pgJalUA= X-Google-Smtp-Source: ABdhPJx3P3Be1s1GYeso3FAVwkgsu6ERmakiPkh9bsWrw9jlDLKldl41/bsOX/2g10/b0vwgUCXEKg== X-Received: by 2002:a65:45cb:0:b0:380:cf1d:6b7a with SMTP id m11-20020a6545cb000000b00380cf1d6b7amr17312108pgr.495.1647848062646; Mon, 21 Mar 2022 00:34:22 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.34.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:34:22 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 15/16] ls-tree: remove FIELD_*, just use MODE_* Date: Mon, 21 Mar 2022 15:33:29 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason When we're picking where we should go in the optimized "show_tree" path there's no reason for why we need to convert our "cmdmode" of e.g. MODE_LONG into a FIELD_LONG_DEFAULT. Instead we can simply do those checks in the show_tree() function itself. Let's also make this code more future-proof by unrolling the hardcoded strmp() if/else if chain into something that checks a new "static struct" providing a bidirectional mapping between optimized formats and the ls_tree_cmdmode. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 98 ++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 52 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index a271941540..3e756b5eee 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -23,25 +23,13 @@ static int ls_options; static struct pathspec pathspec; static int chomp_prefix; static const char *ls_tree_prefix; -#define FIELD_PATH_NAME 1 -#define FIELD_SIZE (1 << 1) -#define FIELD_OBJECT_NAME (1 << 2) -#define FIELD_TYPE (1 << 3) -#define FIELD_MODE (1 << 4) -#define FIELD_DEFAULT 29 /* 11101 size is not shown to output by default */ -#define FIELD_LONG_DEFAULT (FIELD_DEFAULT | FIELD_SIZE) static const char *format; -static const char *default_format = "%(objectmode) %(objecttype) %(objectname)%x09%(path)"; -static const char *long_format = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)"; -static const char *name_only_format = "%(path)"; -static const char *object_only_format = "%(objectname)"; struct show_tree_data { unsigned mode; enum object_type type; const struct object_id *oid; const char *pathname; struct strbuf *base; - unsigned int shown_fields; }; static const char * const ls_tree_usage[] = { @@ -50,7 +38,8 @@ static const char * const ls_tree_usage[] = { }; static enum ls_tree_cmdmode { - MODE_LONG = 1, + MODE_DEFAULT = 0, + MODE_LONG, MODE_NAME_ONLY, MODE_NAME_STATUS, MODE_OBJECT_ONLY, @@ -122,25 +111,6 @@ static size_t expand_show_tree(struct strbuf *sb, const char *start, return len; } -static int parse_shown_fields(unsigned int *shown_fields) -{ - if (cmdmode == MODE_NAME_ONLY) { - *shown_fields = FIELD_PATH_NAME; - return 0; - } - if (cmdmode == MODE_OBJECT_ONLY) { - *shown_fields = FIELD_OBJECT_NAME; - return 0; - } - if (!ls_options || (ls_options & LS_RECURSIVE) - || (ls_options & LS_SHOW_TREES) - || (ls_options & LS_TREE_ONLY)) - *shown_fields = FIELD_DEFAULT; - if (cmdmode == MODE_LONG) - *shown_fields = FIELD_LONG_DEFAULT; - return 1; -} - static int show_recursive(const char *base, size_t baselen, const char *pathname) { int i; @@ -207,7 +177,7 @@ static int show_default(struct show_tree_data *data) { size_t baselen = data->base->len; - if (data->shown_fields & FIELD_SIZE) { + if (cmdmode == MODE_LONG) { char size_text[24]; if (data->type == OBJ_BLOB) { unsigned long size; @@ -240,14 +210,12 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, int recurse = 0; size_t baselen; enum object_type type = object_type(mode); - unsigned int shown_fields = *(unsigned int *)context; struct show_tree_data data = { .mode = mode, .type = type, .oid = oid, .pathname = pathname, .base = base, - .shown_fields = shown_fields, }; if (type == OBJ_BLOB) { @@ -260,12 +228,12 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, return recurse; } - if (shown_fields == FIELD_OBJECT_NAME) { + if (cmdmode == MODE_OBJECT_ONLY) { printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination); return recurse; } - if (shown_fields == FIELD_PATH_NAME) { + if (cmdmode == MODE_NAME_ONLY) { baselen = base->len; strbuf_addstr(base, pathname); write_name_quoted_relative(base->buf, @@ -275,12 +243,40 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, return recurse; } - if (shown_fields>= FIELD_DEFAULT) + if (cmdmode == MODE_LONG || + (!ls_options || (ls_options & LS_RECURSIVE) + || (ls_options & LS_SHOW_TREES) + || (ls_options & LS_TREE_ONLY))) show_default(&data); return recurse; } +struct ls_tree_cmdmode_to_fmt { + enum ls_tree_cmdmode mode; + const char *const fmt; +}; + +static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = { + { + .mode = MODE_DEFAULT, + .fmt = "%(objectmode) %(objecttype) %(objectname)%x09%(path)", + }, + { + .mode = MODE_LONG, + .fmt = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)", + }, + { + .mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */ + .fmt = "%(path)", + }, + { + .mode = MODE_OBJECT_ONLY, + .fmt = "%(objectname)", + }, + { 0 }, +}; + int cmd_ls_tree(int argc, const char **argv, const char *prefix) { struct object_id oid; @@ -367,25 +363,23 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) if (!tree) die("not a tree object"); - parse_shown_fields(&shown_fields); - /* * The generic show_tree_fmt() is slower than show_tree(), so * take the fast path if possible. */ - if (format && (!strcmp(format, default_format))) { - fn = show_tree; - } else if (format && (!strcmp(format, long_format))) { - shown_fields = shown_fields | FIELD_SIZE; - fn = show_tree; - } else if (format && (!strcmp(format, name_only_format))) { - shown_fields = FIELD_PATH_NAME; - fn = show_tree; - } else if (format && (!strcmp(format, object_only_format))) { - shown_fields = FIELD_OBJECT_NAME; - fn = show_tree; - } else if (format) + if (format) { + struct ls_tree_cmdmode_to_fmt *m2f; + fn = show_tree_fmt; + for (m2f = ls_tree_cmdmode_format; m2f->fmt; m2f++) { + if (strcmp(format, m2f->fmt)) + continue; + + cmdmode = m2f->mode; + fn = show_tree; + break; + } + } return !!read_tree(the_repository, tree, &pathspec, fn, &shown_fields); } From patchwork Mon Mar 21 07:33:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12786949 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 C7619C433F5 for ; Mon, 21 Mar 2022 07:34:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241506AbiCUHgK (ORCPT ); Mon, 21 Mar 2022 03:36:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344963AbiCUHfu (ORCPT ); Mon, 21 Mar 2022 03:35:50 -0400 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 2635517A8B for ; Mon, 21 Mar 2022 00:34:26 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id o6-20020a17090a9f8600b001c6562049d9so13649826pjp.3 for ; Mon, 21 Mar 2022 00:34:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vNUGGInsboEmmZD6stgtnlSrPjoKjE8Hw9/tjipXSeA=; b=ICGEP1fRI9dZY+yv+HrEeqXJczo84nZiiz/LqKnjdXhc/ITMbqyJnQX3U879zi4xED QoqnXDmwF3RkSzQQBKV4biCI2Dv5UnGA8crcCn9El1xVlGUyf+pnW8SDntsEghcPC1Km TZuZ5EBFyEFeUtbcasKoK6UmFXgx6YoLK+3ZDYP3e+X8weRWnu8A8ZwBZ4YeOv13zI0S +ESiLRrvbmfL5I9iH9kDSXuplTZlDFKJfkBYTfwQwAwLfvywvs0z1JVZTizEsRKRgr6Q fTXBJhoccKOHFDeDF1jbXsIXpFjlM0Vo2YiRIZGLhoXq1Lz72uivCpXyB0Kk/7SyE6j9 cAzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vNUGGInsboEmmZD6stgtnlSrPjoKjE8Hw9/tjipXSeA=; b=TkalnU/XoGcFZhCiybEaHOlRJUJZkHSMgOwpUHRb+nPNiSWVB3x2Ouu+mWtVQ7rbN9 f6jqxO5wqn+5c6NAtRZ+gSipeRtRxPnGH51elz8TOgBrwPtlkTODi2bag3Lz1nzJxXK4 EWdFKDxIsUCuoI2kxSQtUkgvVEXE4sSPW/hCB+o89rFjpgCYRzhk2va+eOB2bu+Av4Iu cxEfzJlT1rfmVVp8KUKK8xUvXimBsfrz2Uw55NP/9Mj4Bsy85x6NkeS9KMQ0bd1gCheU 71IoxT2Nio3Wn8aaWvzhGvRJCCSd7dIgTq6dd7yumQ2VPN5EwhIubtAs+3432QU8NMUr dwzg== X-Gm-Message-State: AOAM531QETQMG7Cra2Dq1c0IHguDbUkW26I9nhepq3PSc0HX89dYir2q nAuWopRPLjIp90TA7xVSedidmW+4KNwsmw== X-Google-Smtp-Source: ABdhPJxULgz7IQ5ifl8gEUM3ZTAjFpHhMtH76FmcOfnzQaNehg9eUf377a40rrVX6AD/ZID5cG+FNQ== X-Received: by 2002:a17:90b:1bc8:b0:1c7:443:3ffb with SMTP id oa8-20020a17090b1bc800b001c704433ffbmr5682375pjb.84.1647848065577; Mon, 21 Mar 2022 00:34:25 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.46]) by smtp.gmail.com with ESMTPSA id o6-20020a17090ad20600b001b8d01566ccsm18914422pju.8.2022.03.21.00.34.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2022 00:34:25 -0700 (PDT) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v13 16/16] ls-tree: split up "fast path" callbacks Date: Mon, 21 Mar 2022 15:33:30 +0800 Message-Id: <010e3c0eceac0a936a447a6df7ba8c9abb7c77b2.1647846935.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.406.g2e0e55130e In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Ævar Arnfjörð Bjarmason Make the various if/else in the callbacks for the "fast path" a lot easier to read by just using common functions for the parts that are common, and have per-format callbacks for those parts that are different. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 198 +++++++++++++++++++++++++++++----------------- 1 file changed, 124 insertions(+), 74 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 3e756b5eee..52ae71efaa 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -173,108 +173,157 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, return recurse; } -static int show_default(struct show_tree_data *data) +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) { - size_t baselen = data->base->len; - - if (cmdmode == MODE_LONG) { - char size_text[24]; - if (data->type == OBJ_BLOB) { - unsigned long size; - if (oid_object_info(the_repository, data->oid, &size) == OBJ_BAD) - xsnprintf(size_text, sizeof(size_text), "BAD"); - else - xsnprintf(size_text, sizeof(size_text), - "%" PRIuMAX, (uintmax_t)size); - } else { - 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); - } else { - printf("%06o %s %s\t", data->mode, type_name(data->type), - find_unique_abbrev(data->oid, abbrev)); - } - baselen = 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(data->base, baselen); - return 1; -} - -static int show_tree(const struct object_id *oid, struct strbuf *base, - const char *pathname, unsigned mode, void *context) -{ - int recurse = 0; - size_t baselen; enum object_type type = object_type(mode); - struct show_tree_data data = { - .mode = mode, - .type = type, - .oid = oid, - .pathname = pathname, - .base = base, - }; + 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) - return 0; + ret = 0; } else if (type == OBJ_TREE && show_recursive(base->buf, base->len, pathname)) { - recurse = READ_TREE_RECURSIVE; + *recurse = READ_TREE_RECURSIVE; if (!(ls_options & LS_SHOW_TREES)) - return recurse; + ret = *recurse; } - if (cmdmode == MODE_OBJECT_ONLY) { - printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination); - return recurse; - } + return ret; +} - if (cmdmode == MODE_NAME_ONLY) { - baselen = base->len; - strbuf_addstr(base, pathname); - write_name_quoted_relative(base->buf, - chomp_prefix ? ls_tree_prefix : NULL, - stdout, line_termination); - strbuf_setlen(base, baselen); - return recurse; +static void show_tree_common_default_long(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); + strbuf_setlen(base, baselen); +} + +static int show_tree_default(const struct object_id *oid, struct strbuf *base, + const char *pathname, unsigned mode, + void *context) +{ + int early; + int recurse; + struct show_tree_data data = { 0 }; + + early = show_tree_common(&data, &recurse, oid, base, pathname, mode); + 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); + return recurse; +} + +static int show_tree_long(const struct object_id *oid, struct strbuf *base, + const char *pathname, unsigned mode, void *context) +{ + int early; + int recurse; + struct show_tree_data data = { 0 }; + char size_text[24]; + + early = show_tree_common(&data, &recurse, oid, base, pathname, mode); + if (early >= 0) + return early; + + if (data.type == OBJ_BLOB) { + unsigned long size; + if (oid_object_info(the_repository, data.oid, &size) == OBJ_BAD) + xsnprintf(size_text, sizeof(size_text), "BAD"); + else + xsnprintf(size_text, sizeof(size_text), + "%" PRIuMAX, (uintmax_t)size); + } else { + xsnprintf(size_text, sizeof(size_text), "-"); } - if (cmdmode == MODE_LONG || - (!ls_options || (ls_options & LS_RECURSIVE) - || (ls_options & LS_SHOW_TREES) - || (ls_options & LS_TREE_ONLY))) - show_default(&data); + 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); + return 1; +} +static int show_tree_name_only(const struct object_id *oid, struct strbuf *base, + const char *pathname, unsigned mode, void *context) +{ + int early; + int recurse; + const size_t baselen = base->len; + struct show_tree_data data = { 0 }; + + early = show_tree_common(&data, &recurse, oid, base, pathname, mode); + if (early >= 0) + return early; + + strbuf_addstr(base, pathname); + write_name_quoted_relative(base->buf, + chomp_prefix ? ls_tree_prefix : NULL, + stdout, 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) +{ + int early; + int recurse; + struct show_tree_data data = { 0 }; + + 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); return recurse; } struct ls_tree_cmdmode_to_fmt { enum ls_tree_cmdmode mode; const char *const fmt; + read_tree_fn_t fn; }; static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = { { .mode = MODE_DEFAULT, .fmt = "%(objectmode) %(objecttype) %(objectname)%x09%(path)", + .fn = show_tree_default, }, { .mode = MODE_LONG, .fmt = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)", + .fn = show_tree_long, }, { .mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */ .fmt = "%(path)", + .fn = show_tree_name_only, }, { .mode = MODE_OBJECT_ONLY, .fmt = "%(objectname)", + .fn = show_tree_object + }, + { + /* fallback */ + .fn = show_tree_default, }, - { 0 }, }; int cmd_ls_tree(int argc, const char **argv, const char *prefix) @@ -283,7 +332,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) struct tree *tree; int i, full_tree = 0; unsigned int shown_fields = 0; - read_tree_fn_t fn = show_tree; + read_tree_fn_t fn = NULL; const struct option ls_tree_options[] = { OPT_BIT('d', NULL, &ls_options, N_("only show trees"), LS_TREE_ONLY), @@ -312,6 +361,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) OPT__ABBREV(&abbrev), OPT_END() }; + struct ls_tree_cmdmode_to_fmt *m2f = ls_tree_cmdmode_format; git_config(git_default_config, NULL); ls_tree_prefix = prefix; @@ -367,18 +417,18 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) * The generic show_tree_fmt() is slower than show_tree(), so * take the fast path if possible. */ - if (format) { - struct ls_tree_cmdmode_to_fmt *m2f; - - fn = show_tree_fmt; - for (m2f = ls_tree_cmdmode_format; m2f->fmt; m2f++) { - if (strcmp(format, m2f->fmt)) - continue; - + while (m2f++) { + if (!m2f->fmt) { + fn = format ? show_tree_fmt : show_tree_default; + } else if (format && !strcmp(format, m2f->fmt)) { cmdmode = m2f->mode; - fn = show_tree; - break; + fn = m2f->fn; + } else if (!format && cmdmode == m2f->mode) { + fn = m2f->fn; + } else { + continue; } + break; } return !!read_tree(the_repository, tree, &pathspec, fn, &shown_fields);