From patchwork Fri Mar 4 10:42: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: 12768946 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 39812C433FE for ; Fri, 4 Mar 2022 10:43:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237703AbiCDKnz (ORCPT ); Fri, 4 Mar 2022 05:43:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237719AbiCDKnr (ORCPT ); Fri, 4 Mar 2022 05:43:47 -0500 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 9966C1AAFF8 for ; Fri, 4 Mar 2022 02:42:59 -0800 (PST) Received: by mail-pg1-x52b.google.com with SMTP id 195so7192398pgc.6 for ; Fri, 04 Mar 2022 02:42:59 -0800 (PST) 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=DkkPlODKXHWQMMMa3BFGHFvscvruy/TjX1sgmxzPenc=; b=oxL80QJoosSH6V/0ZSoVDWKi3yrXMO7Kh7Wp+Ovu5WtraJAgBCgGNJBJ30vj3qbx03 A59akjwO5E7ruGWLPN9kLy8Dd0U85OJOkVb9srtfiXdbJS7nA/Rn2Qx+uEtpPZNT3Be3 B3OXMhwGTgSoCID31iTXfhCCigvubcWCOeYsVWnM+Zlr31phMGyEugxYco9GTLCljZn1 Ybk+PNqoBZri5Y8ekh/9YeA+EyrfLnK0JZZmK4xzZz1ep1VPcN4APbY9hfT46NTvJ3sm 0ul1snlZBIvGKbLx2N+99+L7yMDadUADq0CFDWsavGflmGdNWNx8EcEcGf6Pf77T6cU8 FomA== 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=DkkPlODKXHWQMMMa3BFGHFvscvruy/TjX1sgmxzPenc=; b=y0B6fqYP097aXjR4gLD5JJal/VPSDDsfbJ0x3bvUb+sg2G6QvKLVpfoAMaqBNArxdF +uD/VR7BrrOybzmKs1uBqVpMzIpLGJiDaaQMGy6XRCcZFunFIKm+QeDBrndM1n/B7hFk 6BQIg0ScmY3/pdD1IqoYfoMQWuTn/j9jJFzMgO2bZA4R3HztkdmN20NSue0UzwhHuiUS u4DXSJ6ZOLQiEb4eKrRgUmd825hOdK5zvfh6NP6zC9Cv2M8xiRK+beaYGLlIXUeDb6n0 z8HRkcZcOgt3q/tkAiL5rEWabPmy0ZuTivDxJ56BTQXC5Jnu4jimr6okuoASSwKM7B2r Iniw== X-Gm-Message-State: AOAM5317BALUUoMGLuI0CAikMeaWyVIWCwkXvbN2wD+PhwcdtagtIECA SKC5bloDvSSfTNX0usUbe8o= X-Google-Smtp-Source: ABdhPJzVQ8Pi01KdspTmU7DWbOWo+OpIV5cEWp0N0Qa1zRYwyt3eI6GBxQIPjJJeRn2VSUsc2VrHNQ== X-Received: by 2002:a05:6a00:1ad3:b0:4e1:5623:fdd with SMTP id f19-20020a056a001ad300b004e156230fddmr42880990pfv.26.1646390579097; Fri, 04 Mar 2022 02:42:59 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.42.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:42:58 -0800 (PST) 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 v12 01/12] ls-tree: remove commented-out code Date: Fri, 4 Mar 2022 18:42:27 +0800 Message-Id: <2fcff7e0d40255e0be9496647787352dd0aeb784.1646390152.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 Fri Mar 4 10:42: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: 12768947 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 905AAC433F5 for ; Fri, 4 Mar 2022 10:43:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237734AbiCDKnz (ORCPT ); Fri, 4 Mar 2022 05:43:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237758AbiCDKnx (ORCPT ); Fri, 4 Mar 2022 05:43:53 -0500 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06E941AC299 for ; Fri, 4 Mar 2022 02:43:04 -0800 (PST) Received: by mail-pg1-x52c.google.com with SMTP id 27so7169172pgk.10 for ; Fri, 04 Mar 2022 02:43:04 -0800 (PST) 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=rS9RLmJeaLN/NF1gn+rcBH3G3sV9jboIhHegjMfhSjo=; b=O6R6Iwxk+XZUs7FqdygpB0FuLfumLPil730X2KI/OovX06I86fTt3kl+b5V+8qzh4c KOsZP2a+qVnu47+E5UGAnh0FN5jmSj0WGb/Cbc1QTUUorX03hTKp9rZxkzt21oV5YWAU ZIR/CRRf0SH16C5ZuKxqrUPB6jHrRDG3KCMR4xBkiXXAQji+FX6uz433QNieMoo8H0MM zymwODS6i8ANvWSa6L9SbHTRPnlo7MS4O4aKQCRVM8hRP7m0sEd4Yn2KaNNK/NS10/Cy O16alCRuCB/xwHohsQzO1re4DYTAIn6TisgBoJgxwklAc9drxgC5sJExFQ4rFdhXfc4d zmyA== 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=rS9RLmJeaLN/NF1gn+rcBH3G3sV9jboIhHegjMfhSjo=; b=muRP+NECeRKKhHMALPjUTCRluMfzDfZY/V2myzqMQE9RmwJ6No+XhtYaXiSOGtswC5 R7Et8tbcNaceYMhhDD02tLDD3yFODgltQLh97saErW6TbjTVGnPZ+JcWUybsyc3SOQUl 44wDfvjghGHqR6dudJY4l9AyYLDr1Z4z9rydZVUxAh/EinL19u4A56Dc1KeznMxG8KRl 4yYuSKI0r8b1Lr9k0PzAQLw74N/ccoE7uyest6oAX/Z/JlMvPRkwVMcxvqwOYMUuEAFU YPQXSJ8qzZQVXiNCFDwZcxXkJ0vCylKcPccunkKSxzm+KhhZpZeny3PzewwPLvk4DIiM w0Lg== X-Gm-Message-State: AOAM5338y5GqJnk+AR+IT3xrDoWVetT1C0nDWdNPuF40q8Sb7KtRMOqa S98CAH5HkBCNC0M0ieO7pW4= X-Google-Smtp-Source: ABdhPJy3a5d9THKnZ66TVXb3CllYDTpjMi67u+Xz6/1+PYMAlnpW4jpHFSCayY/feDefqtTR/mvGJA== X-Received: by 2002:a05:6a00:23d4:b0:4c9:f1b6:8e97 with SMTP id g20-20020a056a0023d400b004c9f1b68e97mr42473947pfc.27.1646390583553; Fri, 04 Mar 2022 02:43:03 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.42.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:03 -0800 (PST) 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 v12 02/12] ls-tree: add missing braces to "else" arms Date: Fri, 4 Mar 2022 18:42:28 +0800 Message-Id: <6fd1dd938303192d5b0a4cdc2914b2ff72cf5a3d.1646390152.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 Fri Mar 4 10:42: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: 12768948 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 5F25EC433F5 for ; Fri, 4 Mar 2022 10:43:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237757AbiCDKn5 (ORCPT ); Fri, 4 Mar 2022 05:43:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237719AbiCDKnz (ORCPT ); Fri, 4 Mar 2022 05:43:55 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D2301AAFD7 for ; Fri, 4 Mar 2022 02:43:08 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id gj15-20020a17090b108f00b001bef86c67c1so7489092pjb.3 for ; Fri, 04 Mar 2022 02:43:08 -0800 (PST) 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=u4mT1w8IjJ/aU+qFaSkCiTjcZvoTAWMzGcwVXfgZrOA=; b=E3O88OU6wpU9+YpfkyK5PPKs+7icj0DaQW8ZOIxtZJAhOXBnMb8/gI/R/DQ2vAgVPS 6R1xsWONzLCXJhcX/DbOnONzCcd/r8Jiav56B+j4GlJG4mT2cS4r7pm6tl17EfecIyKY bU1tzdel0eBKi+W8GaIE4XnY1sXZpiX0JxgT4LtMBwpJQlmMPUw4x38mY9jhtl3P2gQY UNEf+oaIO8+in7vXzMAL79RAoQ5knMfjEwZUCBfwRbeLj6HX4YOWUkxEL78GCbDwZ+kU Jr8sSxRnHiQBgSY0XkeVMqyBUaqTv69atOTDBHk1JSOnJrOw7hgKsWRmAhr9ghmfQ3h7 GzUw== 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=u4mT1w8IjJ/aU+qFaSkCiTjcZvoTAWMzGcwVXfgZrOA=; b=EmIRp+kvagsnB/YIV8iB0Gu/SBp/pOiJ5IRq58zZvbqnzJVMpwISGlJXbeZFfZg7XF KcSUJBIpwScoYYGxjFMZP+eUF6YOULyyNeF9ML52KujRzdtgbMqkV4dpRgBx2jODCBg2 KJhfRcVqDEKNFj1yta263ri8Csf9uLlq5S9dIU4auhvZ2D8x4UKkQdc/PyNoelSOrGfl IxTItu78om51CeG8+6Ij9U0u1ML7VBno78ISie8ktz4/xBQkyO7qetbFD+WFC77j4nQZ IOnmG34xR4juQ7QHScRLp8qJPB5DY1MTnrrAAFY5wmV0ys3HDGeAPsJccqXFnQvWazVP dMEQ== X-Gm-Message-State: AOAM532y+GWbq2oaeyKvxT6vB1stEkEwuSD4SAsj08M/JPqCWcnBLeke 5nkBEWIUFlwB8jl5SjmYzrbAyh3PNECMG0D+ X-Google-Smtp-Source: ABdhPJwBpg8uSPid7EpMtSh5NsqTrTxbmBvnB8BkGN6SRo7HVrdhQIBgYZx4JOhR9yV1ocHPGMveog== X-Received: by 2002:a17:90a:ccb:b0:1bc:e520:2284 with SMTP id 11-20020a17090a0ccb00b001bce5202284mr10119804pjt.66.1646390587891; Fri, 04 Mar 2022 02:43:07 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:07 -0800 (PST) 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 v12 03/12] ls-tree: use "enum object_type", not {blob,tree,commit}_type Date: Fri, 4 Mar 2022 18:42:29 +0800 Message-Id: <208654b5e2aebbe988b5a8f478869253a0166e9b.1646390152.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 Fri Mar 4 10:42: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: 12768949 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 4FF6BC433F5 for ; Fri, 4 Mar 2022 10:43:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237627AbiCDKoC (ORCPT ); Fri, 4 Mar 2022 05:44:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237766AbiCDKoB (ORCPT ); Fri, 4 Mar 2022 05:44:01 -0500 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB05A1AC296 for ; Fri, 4 Mar 2022 02:43:12 -0800 (PST) Received: by mail-pl1-x62a.google.com with SMTP id e2so7370945pls.10 for ; Fri, 04 Mar 2022 02:43:12 -0800 (PST) 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=3KuLP5qP/VqlP1E8IR9y0kSFcaMZIji7j23ou89GJxw=; b=V8f7KoNZCTGQ4LT6/Y4Z6kTBaulbSpKgnTcnMUgkWvIn/25DOvkm63RcglcN04TElP UvLgh4ZVCbQUDcWSGZrXKTlGlueAYtqg4b71eMgNpiL2/NtrCrFT+LOkjVvnPMV7Icbe RH5zjnnWPZuCnvYmd9vQJTyZ3Yfpan0xVpSLKz61v+B79UOGUNYErfbbuXtvMM5H5ztt TeWn/OMylGpToQ5LTcQEiBp1BQPEQNZ/rgr5A6QSV6bK+0BSoxnhwfN+0SUo2NfQfbew vU+kJOYS427EJMrXKbSSuhxfeDjUoZRqTdpOmPDpk8S9TEL18yC/hN7aJWtgcB1QNtj/ WUpA== 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=3KuLP5qP/VqlP1E8IR9y0kSFcaMZIji7j23ou89GJxw=; b=SWb6dY4CHZ3yeZtepIY8HGwuTJXAL5FJIZJf5maONgOQkhkv4w7nSzXlTC86UUp3lH 0vKTtXAknM/6OzhfL05oIHUi0xUR0Jn8NX/oE6S+fGyny8HIccSPU8oiXvugE9ck8jy3 rdGAYSVidrpeOTRnaVLwxl2eCpHywu/UK8t3wn9LB7QUPyJOgaNqp9Lw3n+vf61uqdQt U0p/TJkF0gzJaDxpzQlaA8Nb8DHu6NCl0wiDqXqA8HXxbG528rfyKlZXEGfL/uvYlBzc U8Ju0Tnt/WB82geqKVrlZu2L/Nh1zGgrJSr6I8fOfRTlcyFNtsuVpRJb0wy4t5SL4CNA RmGw== X-Gm-Message-State: AOAM531yu3PYP7jNVTBeF+8i3kigMaMfP5Ze44yfxvdP55lHNfCRl2XV wqLIcNszYuTYxKKRmhFd7K+pbwl5ZC8ogVLJ X-Google-Smtp-Source: ABdhPJyUjhSmILgiJInNbzmeJ7SytsXNEWABbLZ7Qwm0Fe01+yNX7PugKbTeNVgSvQzLInKY2r35EA== X-Received: by 2002:a17:90a:5505:b0:1b8:ebd4:d602 with SMTP id b5-20020a17090a550500b001b8ebd4d602mr9879559pji.147.1646390592161; Fri, 04 Mar 2022 02:43:12 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:11 -0800 (PST) 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 v12 04/12] ls-tree: use "size_t", not "int" for "struct strbuf"'s "len" Date: Fri, 4 Mar 2022 18:42:30 +0800 Message-Id: <2637464fd8f6b8bae6c681f20c10c9fc76dc6d6c.1646390152.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 Fri Mar 4 10:42:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12768950 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 64D14C433EF for ; Fri, 4 Mar 2022 10:43:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235496AbiCDKoL (ORCPT ); Fri, 4 Mar 2022 05:44:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237731AbiCDKoE (ORCPT ); Fri, 4 Mar 2022 05:44:04 -0500 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D63F51AC297 for ; Fri, 4 Mar 2022 02:43:16 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id cx5so7044320pjb.1 for ; Fri, 04 Mar 2022 02:43:16 -0800 (PST) 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=0tlU96CYeTuyTtdpREYGgx8VcGfsmQiSu7UvBT/Rsxs=; b=FrocB5pPqVzMdDue8168keI7ZY73reAjN+be+XtXeNKctkBap+hY/ICaj1GxpqWeEz Mp8zU0Zied+Tbh/IQ6+VuQimI0uLL6IRW7RNDLR92Ayv9EDPqf5vKf/ac5IFFHEdAcmn yWQs8VCWYzkTgxA8FRlhfsJmU3V2jq3jI/ufGTyOzG432l8hdl/O9wm3yrgWSxO6YD/P KdaLaqlSvsLvZSoeefOY6bnirRVkkoP0KqELAvnilNlx8p0abXggB699Gq6uzAEuWKm1 3E8jJD3YQ815uREZi5JbF1J6RF1o50b78Lt4Yz7P84Cx7bMT2l6BlqNSmZ9yDzEa2ESO J9wg== 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=0tlU96CYeTuyTtdpREYGgx8VcGfsmQiSu7UvBT/Rsxs=; b=CO6MrPeHPC4z3u5QTLYrUHIzBpc8V8rET6rawe89vhnxZqO3e6tjHvQiHG5nPzDGlx DW/ns8cMRiJ+KUTslo2SRIMQFsvYHAlymM2E8AQiLkwrOrBWFrAKcqdu2RQVY4GR5//R gsUZCAeQqL7Onp+bgMFtxP4J+V2V5VAzhNGja0eL9TrLygT0ltDjd6R2PiTXnbhG19iY AoZaK6/oB9nadO1fH4kza5bT6Cy8NHwVQON5R4kaBYklxHCkmDp+6nY/TWlo8A5mJ/OV wES8uH1xRik0d5IXYrWa7c19mICeMWpxnFT7Q6lH2eTRnpdT2BFL1M9i1bcWxuefE6xS JVQw== X-Gm-Message-State: AOAM531/BvbKwEgdV5XGyGcyuT43DzKzkbPqfsGD4CQEtAN5Y4vhS+EJ dc2jLUhyphwQaiRg8BFzjvI= X-Google-Smtp-Source: ABdhPJz+eAHLDLICF3FCVzsB+mm+Ol/ht/H6XxcBBgqZT9m4yAyEjLXwr9+guOIfl9NHt3QfW+ZEvA== X-Received: by 2002:a17:90b:1c8c:b0:1b8:5adb:fd84 with SMTP id oo12-20020a17090b1c8c00b001b85adbfd84mr10037156pjb.238.1646390596361; Fri, 04 Mar 2022 02:43:16 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:16 -0800 (PST) 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 v12 05/12] ls-tree: rename "retval" to "recurse" in "show_tree()" Date: Fri, 4 Mar 2022 18:42:31 +0800 Message-Id: <99e6d47108308b0bbaf406b439170de04e375c56.1646390152.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 Fri Mar 4 10:42:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12768951 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 D5338C433EF for ; Fri, 4 Mar 2022 10:43:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237831AbiCDKoN (ORCPT ); Fri, 4 Mar 2022 05:44:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237851AbiCDKoJ (ORCPT ); Fri, 4 Mar 2022 05:44:09 -0500 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 6DAC01AC28E for ; Fri, 4 Mar 2022 02:43:21 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id gb21so7010997pjb.5 for ; Fri, 04 Mar 2022 02:43:21 -0800 (PST) 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=5pWjgLNiHsMJ9655JgBu+NQx7QZXdZDxYUYMYDeDCkk=; b=U67USCirV3wnvfFnAKI8xo1EFVnXrXkYo6Chhp+qxlXbyWaIR0DYlcAFJSRG2aLkxJ CFmQrxY14gOLI+aVYk8wqAWfsmZkjwgC6CepIrBzu/ygzK1C8y93a1rXjw4xCvpdiDY4 ELcUzM9Y2/r6dDohZp/LSd5PISDwgHitKQZJxZ1oSZITzA1JYfyWdkDVjwfsxqH5p6Yu knr6+jxDNQpSlwW6lLN330ruyhO+IOzkc3YTABjtaTKE9WXlQjY8WpTZFvZYmUq8QgsL mVPw3byATFaGlPNWXTAueEEW8NCPfFuI0RPPPwKaiCgYtHnVygfqBX7scrOUT3JPcH75 h/qw== 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=5pWjgLNiHsMJ9655JgBu+NQx7QZXdZDxYUYMYDeDCkk=; b=zIrEmcCLygNIfQSz/f2lvHbahYS9w7RD950lwzHd2HX8NUP2FkT8UDi4eEN4DLYcAd fsiZwT8BBD2YqrQ15w6/1661ClsxRHlSVwvYo+7jxo5Yy9aDKV78IOaTDkd39EukrPuD 74wlWyzcC6amKPW/eKdVwZ0XgthUMb7+182LUaKnhuddaIHenT29K8Ae6dhYR8LLlhbx oTa1oBmOjsqkEInMtJCh+vhphNgq2Ers0mkZ/aRb+HdRM/jGsSzzcblK9z8L+Jhtg/sM vBTnqnN7q+g+8PQEyuA+5kJgAbFeXiRt3Tl5QMHPiGZ5SNXfzUu+t9zfyG4qjaPMi4ih 4yRg== X-Gm-Message-State: AOAM5301bK9fPyihP1XEuzEhFcTGXuKFi1eMPhqI85au/CWN6vI1e0FB msJzAkydriRDgpWV3WyHllG2vX6mHqDO0At1 X-Google-Smtp-Source: ABdhPJwdLbHVHjG6m0tv2m+TkwsHvYn5RSsTYNnA3hxXli7w0vR5H3FSf92f0VOjtNOTaGCi+3vaCg== X-Received: by 2002:a17:902:f602:b0:14f:53a8:64f7 with SMTP id n2-20020a170902f60200b0014f53a864f7mr40520671plg.151.1646390600901; Fri, 04 Mar 2022 02:43:20 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:20 -0800 (PST) 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 v12 06/12] ls-tree: simplify nesting if/else logic in "show_tree()" Date: Fri, 4 Mar 2022 18:42:32 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 Fri Mar 4 10:42:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12768952 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 5D1BBC433EF for ; Fri, 4 Mar 2022 10:43:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237864AbiCDKoP (ORCPT ); Fri, 4 Mar 2022 05:44:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237811AbiCDKoN (ORCPT ); Fri, 4 Mar 2022 05:44:13 -0500 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CE8C1AC299 for ; Fri, 4 Mar 2022 02:43:25 -0800 (PST) Received: by mail-pg1-x533.google.com with SMTP id o23so7164311pgk.13 for ; Fri, 04 Mar 2022 02:43:25 -0800 (PST) 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=DTJ9o84c0gZUTwdV/dNn0LYgzY5QUbjGEeHsdbTEEIA=; b=NUuedFLLSovCL0uLM6BlVUE8rmvp4+vRNUhC6c4Al2vwm8P+Z7kmtO1iZSfVd4MLMY wsHt5J09IPgEEe3LqKA+EEgz30bj7r0VFcs2RQnjueTqYRctkidTTJGp5OoL3UTndIQm i7P1UlkK+1DGgJZQ+NI3OJ91BXplOfUFr54T6CZQLNfAUowja3GAizJIrmRjp/oxkY3i lh0NH5z6q/my/fLvJdcxRArUXP2UczOl+eodOifMTwF42fuCS5cYed5DoUI/WScIA5uV d/cg8CIudQXXHGKsj7l8XWu0lIwZH2Guhj+Mi0AvGcQQBad3bv6qaeIk0RTGL/EnUWdZ 15Og== 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=DTJ9o84c0gZUTwdV/dNn0LYgzY5QUbjGEeHsdbTEEIA=; b=PvlnyGsGtqv8YtWzgjWSDfexfHj2ltOt3zP5FPmzeqIMgH8jL7d9mgT9qmow0od/Pg KmRKEj9AP4KKYEy/zM0f0O3YrQEmuh/uBl4GQxxMnkuFm8wTqSURdTsYSEq96ClJyzHS PyuOCu9y2MKWsx8nRC4QASOGGXzMUbAME2bejt2qU0x2klSqsflVIbe/OaQq8GFf40wG pwFJqiuVqsKHNembscPWGkHGFEye0iiiVg/Y86VGIx2Ng5IhM8KfiJXZZyFFHTIV3hsj 2snP+yhjjYt3GOMDmo8bmhU9cxuk1LjRncNzyCFLKNLe7Qc5hhcih0FXJEccTEjrWyXU 44mg== X-Gm-Message-State: AOAM533HjJQI8zv5RCE1uw701KIU5wMFahZaNFjVHL/xbwi/wktSZ89/ j9ZhHiCnKOJlwoinUkz7cGCG9T/AxMLI7GNy X-Google-Smtp-Source: ABdhPJyWUFboaq3WMZfrQ2mXHQyyqVwEyMxDX+oq1YKlVrcqxRxxq2YbhKxQgd9WnNXrWko2XcfacQ== X-Received: by 2002:a63:8843:0:b0:37c:926d:3fc7 with SMTP id l64-20020a638843000000b0037c926d3fc7mr2818121pgd.112.1646390605077; Fri, 04 Mar 2022 02:43:25 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:24 -0800 (PST) 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 v12 07/12] ls-tree: fix "--name-only" and "--long" combined use bug Date: Fri, 4 Mar 2022 18:42:33 +0800 Message-Id: <25a07e048f25e0efdd2f8690f2e6967352a20e9b.1646390152.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 Fri Mar 4 10:42:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12768953 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 14158C433EF for ; Fri, 4 Mar 2022 10:43:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237918AbiCDKoW (ORCPT ); Fri, 4 Mar 2022 05:44:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237851AbiCDKoV (ORCPT ); Fri, 4 Mar 2022 05:44:21 -0500 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C112A1AC2B2 for ; Fri, 4 Mar 2022 02:43:29 -0800 (PST) Received: by mail-pg1-x535.google.com with SMTP id 6so2648394pgg.0 for ; Fri, 04 Mar 2022 02:43:29 -0800 (PST) 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=WgyDVYeY7QQW1zjlibjeDYEwPgOgxdg0ZBsRXJBKTgM=; b=bjJBSnpIOTOkIxisxYQ/lVDyf4zCiYlUEzM81OMt8/FveVAB6WuBOxSQ3z/xMiFb7N VGHAc9jX3vIqyfdhIIwBBFV98RKglwdng7AUKBmqa6y2zbIZcREeQMwonK3D4474Dgkb yfr4I1CDB4yOTR/kEZXKQKt5vUchSLWENLvTxLifqL2BcReVd+t5pHrjsvL2nvhQdXOF dOmBxs4N0yKwJjhqac1xe3qDe6clhBRLBRGVmRH8Bb0gBhnGKXyNvVXksNeNzVXcWITj 8EIg3mYLMjjdcpcwOoHoNmuTbtvPUPA4TvOR0AjdhfzXICBLidA+XqrCG0zu0hhGtzuq HBlw== 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=WgyDVYeY7QQW1zjlibjeDYEwPgOgxdg0ZBsRXJBKTgM=; b=o1qv81EvUsyExUAjOaUeRpXB7UR+qUYMly7XOCndnYAnff/JOZKgltcEw/sihxGTzN 5llYPx4C2rNbsjcvx89q5K8zi1w4ntfx/pe7ZODXss9qdWgvkdH6hWskRy7iDci3seYu mqdxZg4d9uY3O6JRZOQs1s0cPAElFei5bVU/BK2biTwD1FurXXSTgcdanoRWm17HPko7 5E0uolHQAtQ6JFjw7vr2SHKYvNRU37Jr9g1W2dJUBnp3wUBpmgCjvpPnfH2QPf+9jYlA +C6kETsW7Ky1wSaO57JefDgr/MAV8z9WF9oyqiKb03xoRSfI5wlvw6AZXcDHoYXyBpEf iOhQ== X-Gm-Message-State: AOAM532iIFJsHqU0Ed9jPRkbYok93540UVsMNrono5snPt3P2NOLM/8U mubERprhiNzYlccofenMCw4U8w3uNL3nkTt7 X-Google-Smtp-Source: ABdhPJyS8YWptDRs1bcC2mTKOwAOyoieIMEp2IVbNe3SXvluyL/UOzSEuSLBxJAIpUiEzT64eM8SzA== X-Received: by 2002:a63:d642:0:b0:378:a4c2:7b94 with SMTP id d2-20020a63d642000000b00378a4c27b94mr20759388pgj.218.1646390609243; Fri, 04 Mar 2022 02:43:29 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:28 -0800 (PST) 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 v12 08/12] ls-tree: slightly refactor `show_tree()` Date: Fri, 4 Mar 2022 18:42:34 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 | 124 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 37 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 7be4c13228..c70e31ac3d 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -16,21 +16,51 @@ 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) +#define LS_NAME_ONLY (1 << 3) +#define LS_SHOW_SIZE (1 << 4) 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 mutx_option { + MODE_UNSPECIFIED = 0, + MODE_NAME_ONLY, + MODE_LONG, +} 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 +91,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 +141,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 +171,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 +207,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 Fri Mar 4 10:42:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12768954 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 AFCCDC433EF for ; Fri, 4 Mar 2022 10:43:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237906AbiCDKo1 (ORCPT ); Fri, 4 Mar 2022 05:44:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237925AbiCDKoW (ORCPT ); Fri, 4 Mar 2022 05:44:22 -0500 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 26D8A1ACA3C for ; Fri, 4 Mar 2022 02:43:34 -0800 (PST) Received: by mail-pf1-x430.google.com with SMTP id j1so3311394pfj.5 for ; Fri, 04 Mar 2022 02:43:34 -0800 (PST) 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=mX4JMQhn1M6nXvnDyTKezObtL3dqTWuWcelZGd199Jk=; b=CzSbgxw6IIIPf7VmVsPPSIdn9Xp51QNQm5QLJDWEev77zdwH1vYEWPYkPUYxgeCfBX CXaBZ8DgrPIVySQCMJJ1DIdvaDZev3odqSiZXPBt75a6rFPw9j7UY+GPZKSxO3rE56qi jtZFxLajlqZ9xYJZRd2/O+HN0LIaHTMAIyiHQRFllI930H1HTXm5NrB4lRHc8wGT+i4O Erx6vVFz659tId/qVwaxBCUF34mre+VHh9Fy2gSU8eN2ZJg17IUJThlznCm7jwtkeDK1 Cqv5uz3X/6+4btH1D3e5nuGfaXBG0YW6yR+CJmHs+rA1fRlcJjCA7xxzv2US/0VdYzkG P2Bg== 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=mX4JMQhn1M6nXvnDyTKezObtL3dqTWuWcelZGd199Jk=; b=V166H3XN/ukOnvi8JqqoXiZWZovtVgXpeOAItXdmhVV23B7gAGC5i5OCUzSLnw025G 6fMEjv4TPOH0N3nQtflmsjxcb9V7BG+o/69Fi2RTlM92F02Y1ePs+Aw1eypK4MD6HdYU bGIRdqN1jI4fpGedPoeAKkxxY7iixeAhuqA15tp4ZNLxx0uBdHcy1Cfol6rwSMuXsiax lpjy9ByWu0kTftSuodWXlzZbCsGViH3eOAxD2AOK6q7/sKhR/3IVBe3Ke3GRKdY4ZTDH DJ2yHMeBJjQrYlMuVf53kqoLZzF/2HLo3pksyszlWSK0/HtXZpiYoyhAdcDRpMHpC1Ni JUdQ== X-Gm-Message-State: AOAM5311JPVDcbWx/YRmOoMeEE02vD52MDwmeaknivZWNKl0uEN7Y0TH omq+vwEnNJjFO9Mr9ZJVWj8= X-Google-Smtp-Source: ABdhPJyfic+bkrykPqQfnmL/xQ5SHn5xuXNsFrZE0gZJeuf6O8RGtoqgSPm+SXoA1Xp2MLWv06dEtQ== X-Received: by 2002:a62:7990:0:b0:4e0:5ae8:6c11 with SMTP id u138-20020a627990000000b004e05ae86c11mr43092145pfc.34.1646390613461; Fri, 04 Mar 2022 02:43:33 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:33 -0800 (PST) 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 v12 09/12] ls-tree: introduce struct "show_tree_data" Date: Fri, 4 Mar 2022 18:42:35 +0800 Message-Id: <80311adc7c3630cadac275dca70b6168516e6d21.1646390152.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 c70e31ac3d..04aefc6106 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -25,7 +25,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) @@ -34,6 +33,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 @@ -45,19 +53,19 @@ static enum mutx_option { MODE_LONG, } 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; } @@ -91,17 +99,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), @@ -109,18 +115,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; } @@ -130,6 +136,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) @@ -151,8 +166,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; } @@ -162,6 +177,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), @@ -207,8 +223,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 @@ -225,6 +239,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 Fri Mar 4 10:42:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12768955 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 0372DC433EF for ; Fri, 4 Mar 2022 10:43:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237844AbiCDKoh (ORCPT ); Fri, 4 Mar 2022 05:44:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237758AbiCDKo0 (ORCPT ); Fri, 4 Mar 2022 05:44:26 -0500 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94DFF1AE644 for ; Fri, 4 Mar 2022 02:43:38 -0800 (PST) Received: by mail-pg1-x533.google.com with SMTP id o26so7177614pgb.8 for ; Fri, 04 Mar 2022 02:43:38 -0800 (PST) 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=Zat6ZRTSvvw2CIXm+7n76MZX8/mahV4fB4VisSulkj4=; b=Cn6QWXeICV7BQsLQbYy0A5MOvp0qlYhPi30/1ieArCS1RfqSwL/y4Nek1uzBQXEqpI EvdjYe22zyiegCaWDbzS7OYZrv18XaZAmBYCMPXS8EsFAh7M8zNRBfStX357DLl0Wewn PUmHirlYlFA+z17p702pb70DA+5fAOdDaxUqWK0jLVrWRSOH1F6TVG1J1L/LPKSld+lS k8xqhPLZ9/0ynQB/DGm8i54TNjQjFDNzGy/JVTrTFa1yJpz5umWbr5MHoh3xvkTTxGRs ieCnHIJSNLERr+Q65SV8EIVgw2lp2J3haeM9jqaEMrYISsFDnkVtRoUmGi3wKnOZrL1W VhXg== 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=Zat6ZRTSvvw2CIXm+7n76MZX8/mahV4fB4VisSulkj4=; b=u7GxbkwUl8bT0WPCPfrOYx8YL7KK0O8kYca9+OyIlYOKxKlY5nEBpa0M4zTWWPXRtE euTH9MGgIPidFGRnwWxOCc8UkkN5C1eDJZ50nz2uhPujRaN8q4LmpqfaC1U3qk5+2DqC ubaXcBwVe10HPaiHTOG+AE828NI02s5DkL5c3cTBuePPMlO485B/KjeBw7ibh9+QtBt9 TxYlar86pO2VgzNlfdbTGaqKqYKW8Jj4Y4rBhwvC2QDuAARHD1IfYQ2++l8UW4XDJlq0 xwSDlyFsyX89HY3fvvx5qS9kafIh6Th1BcnExpyAum5b/p7Cqd0eyB5f0wby+YM9bj1E NaOQ== X-Gm-Message-State: AOAM5300Nx9f+alVHC6TSuQvuB+nVXcniJqNc5koZqLLn1lng7TYPtMt jHNyONNPaB1pXs8pfni00Nc= X-Google-Smtp-Source: ABdhPJzhOnOFqchZe9aLgaiaqUp08/JCaUNrRNc2qxqB/7NUJMAtkmnUrwsIApeK4jTSMHVsZR1F5g== X-Received: by 2002:a63:8548:0:b0:37c:945e:2603 with SMTP id u69-20020a638548000000b0037c945e2603mr2489298pgd.124.1646390618092; Fri, 04 Mar 2022 02:43:38 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.33 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:37 -0800 (PST) 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 v12 10/12] cocci: allow padding with `strbuf_addf()` Date: Fri, 4 Mar 2022 18:42:36 +0800 Message-Id: <459080f549e040d5e2396533abff76276b60c274.1646390152.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 Fri Mar 4 10:42:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12768956 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 138F9C433FE for ; Fri, 4 Mar 2022 10:43:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237944AbiCDKom (ORCPT ); Fri, 4 Mar 2022 05:44:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238003AbiCDKoe (ORCPT ); Fri, 4 Mar 2022 05:44:34 -0500 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26A071ACA3B for ; Fri, 4 Mar 2022 02:43:43 -0800 (PST) Received: by mail-pg1-x52c.google.com with SMTP id 6so2648872pgg.0 for ; Fri, 04 Mar 2022 02:43:43 -0800 (PST) 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=C70ic36NHxPV6e5bc72Q1nkZOd3DdFa0TW/RQvHo3P0=; b=Zt169LOT7SIgwGjrc8mPTxsr+ADfmfw6XCozT/rr+Id2lo+dbwKMspRSfRKdwOgy9d wG0XAXsXJI9UAOlUbt8XD/bw2mjRhbP4LXYpmNCLdbLbM8IE5dbiVeo8MhCUBqRycyVA FcX5vqQuNvoYbC9JBIOJiUcwJqFet9IeCBgzw2yCT0zV+jozby+IuE9wyAmfJhYSvhsz /8ZB5EfKOhxdhwsFvDGlwUln0csqUF+wDTeso9g7PyzTx1osxB3PmBQK3FcCkJpGC9Ym UbwH93jI5d7r3UqC1a8tt3IeUzbiao/kueF1SHs6972KvaoIlf6bl1z5d+y3sGEh8C0v B2yg== 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=C70ic36NHxPV6e5bc72Q1nkZOd3DdFa0TW/RQvHo3P0=; b=iEyajsIp64c/PWu8pgyhD8+VhQ69PYIPs/DuJ2PSvTSNMdB4+lUY0N2Q1N++Lcivnw pGIxhPGk5EKNNE9HNi8a36pUJBs5YRODSh8SSTD6gUGWEURZdhSe9Ptz6Q7ydURaTWiF Cx8xdxuNq5+ehDclhviLPe7KtU4GvxeJvcrbBqeT/YW/9mLEyuAjUN0YVkG8d0HebHdK /Y6kkj8Aoqt0fxCkubuxFO1YixsgwI70DytgwIVx9FLVVY8n7DyC5cYI9Of72oFOAiMO eSyfUNg/RQMY82VmWe/yALWWbgmmHmY95sxI/5cEUG9NGF1cLQa7IUnNmH4s5x7RHHE7 a/xA== X-Gm-Message-State: AOAM533Dfprq4g/vxHUpzA8IzYMaQNHGvVSpX1gIYZFwONm/b8XkB/CJ IixzwGRdR2FbFF7roLwOgEY= X-Google-Smtp-Source: ABdhPJxTYV2+pHUfYxJY/p08Egnn2mGuoOgyhL38EU+PoCpYW/ER2Y/2OhMkBRqQVehUQOJ9d5i95Q== X-Received: by 2002:a05:6a00:889:b0:4e0:dcc3:5e06 with SMTP id q9-20020a056a00088900b004e0dcc35e06mr42517553pfj.29.1646390622495; Fri, 04 Mar 2022 02:43:42 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:42 -0800 (PST) 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 v12 11/12] ls-tree: introduce "--format" option Date: Fri, 4 Mar 2022 18:42:37 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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]. 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/ 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 | 81 +++++++++++++++++++++ 3 files changed, 262 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 04aefc6106..a46fa73bb3 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -32,7 +32,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; @@ -53,6 +56,72 @@ static enum mutx_option { MODE_LONG, } 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) { @@ -99,6 +168,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; @@ -178,6 +279,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), @@ -198,6 +300,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() }; @@ -218,6 +323,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)) @@ -242,6 +351,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..e08c83dc47 --- /dev/null +++ b/t/t3104-ls-tree-format.sh @@ -0,0 +1,81 @@ +#!/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 && + git ls-tree $opts -r HEAD >expect.raw && + sed "s/^/> /" >expect actual && + test_cmp expect actual +} + +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ + "" +' + +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)" \ + "--long" +' + +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(path)" \ + "--name-only" +' + +test_expect_success 'ls-tree combine --format= and -t' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ + "-t" \ + "-t" +' + +test_expect_success 'ls-tree combine --format= and --full-name' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ + "--full-name" \ + "--full-name" +' + +test_expect_success 'ls-tree combine --format= and --full-tree' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ + "--full-tree" \ + "--full-tree" +' + +test_expect_success 'ls-tree hit fast-path with --format=' ' + git ls-tree -r HEAD >expect && + git ls-tree --format="%(objectmode) %(objecttype) %(objectname)%x09%(path)" -r HEAD >actual && + test_cmp expect actual +' + +test_expect_success 'ls-tree hit fast-path with --format=' ' + git ls-tree -r --name-only HEAD >expect && + git ls-tree --format="%(path)" -r HEAD >actual && + test_cmp expect actual +' +test_done From patchwork Fri Mar 4 10:42:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12768957 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 75435C433F5 for ; Fri, 4 Mar 2022 10:43:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235508AbiCDKoo (ORCPT ); Fri, 4 Mar 2022 05:44:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238048AbiCDKof (ORCPT ); Fri, 4 Mar 2022 05:44:35 -0500 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 54AC21AC2BF for ; Fri, 4 Mar 2022 02:43:47 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id bx5so7020304pjb.3 for ; Fri, 04 Mar 2022 02:43:47 -0800 (PST) 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=GUTDDOVeBFl29d4A6IroFS8RhiM/GAeygpocBZmMUpI=; b=UINoaW1jje5YTxQtuLgvP3tCg4kH4WBZbUSqvkV8IgbyTSfmbKui1dDqbWvd88AXaS 0LXwyF4LI0cpJMeA8XE5J/tWuYcP8fDo0AeYlzfN1s1V4xXGGBP3IOj6Qfdee+IW3A50 QVF6jP7TtgsFJmgKVS0otcR2xIRyDFkc3IKednP+JfcRySVlUKQAEz7RV82RkhSuunIy Kjw4a5KIZsh1+m9tQ+hQgJovKd/+R4FCrmLlKd7+jGv7xCF21BNFIY3JcXwl5MGyojmS hmpe7DfWmslXVJoUCOrdpYbLC9WJrc7jc33PPaiPhfM8a0yuMiTzj4O0R2r2vlQmszJD EVIg== 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=GUTDDOVeBFl29d4A6IroFS8RhiM/GAeygpocBZmMUpI=; b=oSbdHKjkQD3mOyD14pGQ6SYPZArfXxgJv0ouXywPCPMAZBpV0gICZoPya6XVwSerRU Ac31+crCXvwQsbqgDrkYmomhJY1M90/r366piniindwF2Bo2+4j1myeZvF2cE6tJERvk 19zCTofWBwjS8BDip6sGMLeB2cqnyuIzQBtDiqJzSWcLBZMkXI9fKB8S36VeALBe/260 Z3fhmHUP+UVWLhJiJ7WJo9lqazVRQzMKLUh84Hk7VIMvnrlf3TS/769a6KrTGznqbh3t OJMBlqiDkaiF/Ox9QgBpMOkv9JyBGFJm68xOPNynHuO1L771F+zFKpxiXyKY0TX8hqaE K+sA== X-Gm-Message-State: AOAM530gGL+G+UaR2oo/3ytMhS8i+/v9auOPMgV1PtWJ9NUhfO/wl3nj HCE6oUKeZQCN9TyNkc98hvI= X-Google-Smtp-Source: ABdhPJws8DIGOjmMMxeqftouYYA9cDIUA/mbeYSBuM0HUBaAUPwriZkSmsmVA1z7+hSDSd+v1GJFmg== X-Received: by 2002:a17:90b:17c2:b0:1bf:2d51:4e6f with SMTP id me2-20020a17090b17c200b001bf2d514e6fmr1170596pjb.172.1646390626720; Fri, 04 Mar 2022 02:43:46 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.53]) by smtp.gmail.com with ESMTPSA id k7-20020a6555c7000000b0034e101ca75csm4236253pgs.6.2022.03.04.02.43.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2022 02:43:46 -0800 (PST) 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 v12 12/12] ls-tree: support --object-only option for "git-ls-tree" Date: Fri, 4 Mar 2022 18:42:38 +0800 Message-Id: <55b1c4379d51c8254ac71a7b897d81bddd863143.1646390152.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.402.gef0f5bd184 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 | 18 +++++++++++++++++- t/t3103-ls-tree-misc.sh | 7 +++++-- t/t3104-ls-tree-format.sh | 19 +++++++++++++++++++ 4 files changed, 51 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 a46fa73bb3..7da21d9963 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -20,6 +20,7 @@ static int line_termination = '\n'; #define LS_SHOW_TREES (1 << 2) #define LS_NAME_ONLY (1 << 3) #define LS_SHOW_SIZE (1 << 4) +#define LS_OBJECT_ONLY (1 << 5) static int abbrev; static int ls_options; static struct pathspec pathspec; @@ -36,6 +37,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; @@ -53,6 +55,7 @@ static const char * const ls_tree_usage[] = { static enum mutx_option { MODE_UNSPECIFIED = 0, MODE_NAME_ONLY, + MODE_OBJECT_ONLY, MODE_LONG, } cmdmode; @@ -128,7 +131,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)) @@ -257,6 +263,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); @@ -295,6 +306,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, @@ -363,6 +376,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 e08c83dc47..b86941c202 100755 --- a/t/t3104-ls-tree-format.sh +++ b/t/t3104-ls-tree-format.sh @@ -46,6 +46,19 @@ test_expect_success 'ls-tree --format=' ' "--name-only" ' +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(objectname)" \ + "--object-only" +' + +test_expect_success 'ls-tree --format= --abbrev' ' + test_ls_tree_format \ + "%(objectname)" \ + "--object-only --abbrev" \ + "--abbrev" +' + test_expect_success 'ls-tree combine --format= and -t' ' test_ls_tree_format \ "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ @@ -78,4 +91,10 @@ test_expect_success 'ls-tree hit fast-path with --format=' ' git ls-tree --format="%(path)" -r HEAD >actual && test_cmp expect actual ' + +test_expect_success 'ls-tree hit fast-path with --format=' ' + git ls-tree -r --object-only HEAD >expect && + git ls-tree --format="%(objectname)" -r HEAD >actual && + test_cmp expect actual +' test_done