From patchwork Wed Mar 23 09:13:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789588 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 AD42BC433EF for ; Wed, 23 Mar 2022 09:13:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240961AbiCWJPE (ORCPT ); Wed, 23 Mar 2022 05:15:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238816AbiCWJO6 (ORCPT ); Wed, 23 Mar 2022 05:14:58 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8AAD75631 for ; Wed, 23 Mar 2022 02:13:25 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id h19so43571pfv.1 for ; Wed, 23 Mar 2022 02:13:25 -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=kU1r40q4bbAX0HhIyOD0tXPK6t8gwvZYzi4SgPP1uGs=; b=XDCow0Ci8z4mx9LMoL2ZxVneiztivmOpqQAOo8yuZ28a1AXQvZmBEIqnlWaq6HjcBW gaE+rOSdeBS3/SyWiRe+efk61luKT1nSTJR5qcn3SZIcVVscoTuu4nh4f46CgNtJLXK5 cA/7MH3C32ColvhkC2AllCe212iLV+utncNXdC7obn1jCvmiycT9MhLE+fAQb2B2X1Vs gBoesqX+fS9NBsx/zqnFW9X3JEYJlo99H4G+AcdTiuz2wc59GxAWd4tCLwu0uDaZ8wC7 +ps4zjwcOnHhrEWZR0Lsh0dBlht01P5m2gVWTh5dg8n6sW0yUVlPV11XkvLCLc+D6qv2 GLpw== 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=kU1r40q4bbAX0HhIyOD0tXPK6t8gwvZYzi4SgPP1uGs=; b=lvhBXLM382eQfhz7pqI18EIPvgk/yrrTEsgJDv3ma+cjI6yx9omtaBOKzkgxEm2C9P Urk6YIISzTXlA9Y/cN/DQMihYD1JP3fOWeXhA/GRE5AcdKcZKN7iaH7m/szVfF7BX7SM lA2s8pg73+eUV4s4e+RdKZ4BACLl+CE3nNfgm/Cs9IVRUW6g5crwS7Quj+ECP3KxDXuK b8pfVcWfiweLGJxfhLkBXWx1quvXdvtSCkt3gHrRDAbRlyFbQjCVjyKx1s20CcRMWgPE 971BXuwCFu+9LutryunJ03RZU6P/VcYcnzMlDW0iVA+c181X4j23DDXJH2LXtLi7NHhD tkLQ== X-Gm-Message-State: AOAM530sZpWDUPjmXjnk7KeA+RboRCYW289GKo8JX6uaUW2WtNuIA2gP nkIdUEgCrBY2rQ1ffK2G/n7QC7jMjjsMzA== X-Google-Smtp-Source: ABdhPJzmVLwbyKeTcQTwLrFwlK8Rn1qW7ZW/HUlTK6QInq4I7iq247MY2/HmYWJkKoiR0W4eO1D2VQ== X-Received: by 2002:a65:6390:0:b0:376:7f43:e9a1 with SMTP id h16-20020a656390000000b003767f43e9a1mr24972933pgv.480.1648026804497; Wed, 23 Mar 2022 02:13:24 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:24 -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 v14 01/15] ls-tree tests: add tests for --name-status Date: Wed, 23 Mar 2022 17:13:01 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 Wed Mar 23 09:13:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789589 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 572BCC433FE for ; Wed, 23 Mar 2022 09:13:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238816AbiCWJPF (ORCPT ); Wed, 23 Mar 2022 05:15:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239780AbiCWJO6 (ORCPT ); Wed, 23 Mar 2022 05:14:58 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C72B75C02 for ; Wed, 23 Mar 2022 02:13:27 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d19so1026426pfv.7 for ; Wed, 23 Mar 2022 02:13:27 -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=ooJNt+bsWMSp7XCYJM+O8W0comcFHRBzqpGSoo+IqNM=; b=WFkE6Nn+G+vwnBGuf0w0vtlDCtidaRZT/s1t3VhU4bAMLUdIzhtd0fs29VcVghlz5V T9YHMj1ELZothD7niCE6irJeOk3aWJbyaVJsIW9o3hjp/RSQ+W/zYZ+mEjLY5j5BOWJm GvQ3JUwKgk6nL1aRQJojdN1dgyPiCgEGwEBtyNxPk+z6KDqGmAVUhIek0xvLg+6BmO4J EjDksX/24qgwS2Zqmzi0B1SA5KRMSEL2iuwilOGUR8esl36kDw/mpgMgU2jYvWr2PZe+ PK0hcKBpUcGYoCMkaD+660zQgqrD+jmqZcxnYIYnAexwBEFryTU2EizqcKQXB4pR7dVK /lkg== 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=ooJNt+bsWMSp7XCYJM+O8W0comcFHRBzqpGSoo+IqNM=; b=Jbu+c2yncVvnEMgg2radiZzO/0e0qJnoL9AnOzqoai5RpsLh8KP6w+wJ7peTsr4cSM Hh2wmDBQilPFXYdtIZnRD1goJSGdoz85HoG+SwzbuwfwmHpsMvSN5IgovsX7sT9O5dgC anJPLVUEKPFOM1+S9kELm8BRTdapsMYaEQvNVxdA5+wwp/MRqU7taNJFypGbyvk7bBCo XzdtQNF6bMYyeK0LpJJyV8XnbxGrEVbREXAJrmVtt8WMhxtQEc/J0ZHD/HF9mfb8ZYYg 0N7QN4xd4pOx8eVwVLyAJIq3RpAGiuVKCmiDqOZ7/18laSwqSqqq291fE1XS08N4grCM YUAQ== X-Gm-Message-State: AOAM531J771jcvWsX7IKJOjF08Vepw9v5gSZPsnbecPzXTwXwLYNG6d+ lFlkb5Wgj0OB4x4X7LwN6RE= X-Google-Smtp-Source: ABdhPJzB915lrm9asPFuK/4Sv2FCgkDomhEveJ3GO77hLWakZdoCm05alk3Anma6MVjUk6oE20t7gA== X-Received: by 2002:a63:4d5a:0:b0:381:eef7:33f8 with SMTP id n26-20020a634d5a000000b00381eef733f8mr25464419pgl.193.1648026807147; Wed, 23 Mar 2022 02:13:27 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:26 -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 v14 02/15] ls-tree: remove commented-out code Date: Wed, 23 Mar 2022 17:13:02 +0800 Message-Id: <02779bfd5d0e9aa216b48ff5f738c75d2882fbd3.1648026472.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 Wed Mar 23 09:13:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789590 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 41AACC433EF for ; Wed, 23 Mar 2022 09:13:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242509AbiCWJPH (ORCPT ); Wed, 23 Mar 2022 05:15:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234230AbiCWJPD (ORCPT ); Wed, 23 Mar 2022 05:15:03 -0400 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 93CD55D657 for ; Wed, 23 Mar 2022 02:13:30 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id c11so613503pgu.11 for ; Wed, 23 Mar 2022 02:13:30 -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=6eg2+PfrGKe9kMDDOq5YTVuEaSpdzCrMNdfKaE5/kYw=; b=T7R4z93MAO+LyZUkA5g1ZwGO35/MJ7juJz3PWmWHFDOz4DfL+xV+yd5pD6tuFxKcfN hutt2ESDNXLBQphk3x2sWj/+CjDm+1sJglrom1no+MSKOyxtQmwSQHNVyX1VmK5a8wiL jejMNBQCjJtURapxlRVvoVG01iaR/1/5YYvdSmItzHGELuZUsSiaJrXYQzbh1UepTK6J d1Nd6dtmGUmBIoFLMjzibd8chty5/yypsbBSmi+/vJtLaeHgrxNRlq4yynqYWgvK4uIT ES5ps8dh3AuuGUabpyObvjeY2IlIHDaWIg91sEJLQmsTy8YmKBFAcEm5E7b2EZDAhqmQ 5dag== 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=6eg2+PfrGKe9kMDDOq5YTVuEaSpdzCrMNdfKaE5/kYw=; b=1bnUnh5hhNVoxbOs640i1uWPw7gXnhblE/G5+AMChg4AHjIgmSQu2U8vEWWuUxVB+z QxQFkn6rz5rqJxLWo+i+v3n10HPs3dCR02IfW2WgakhpF178LSLpv4DXu31KwMVRRrTo A9eEqwPWb/0TW8YhWxpw9DDuU5ixYwxDK7Y3rIzOm7Lh3wNI1tFCHi/osGdIBcl0A8R8 I+SlXRfaEqPcgiLLOooNHKxiQ0B1nAjA0BXHrkGFQsThWQgU1EAnra6c86o4zFnD58ee fA5CsR/WrttKDE7nrLkevUW74rMoOLP/6Zd5OoYV3B3XsRy82W8Uoi4hE8ZziQCsmE1Q MwuA== X-Gm-Message-State: AOAM530oNKbJ60nmSSNnTDsZrCwlZk1x6xhkejg2rEGmAlwm8BJiAScV yc1K/OydqBaraPKjEeVw76Q= X-Google-Smtp-Source: ABdhPJzsWWpJXy36yOvLeezLgF36psfEu/IA8yLBQ2uW4QTQf/Np79MpnoVqvGsKantLDkswDKFCLA== X-Received: by 2002:a05:6a00:140a:b0:4e0:54d5:d01 with SMTP id l10-20020a056a00140a00b004e054d50d01mr32818107pfu.20.1648026810038; Wed, 23 Mar 2022 02:13:30 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:29 -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 v14 03/15] ls-tree: add missing braces to "else" arms Date: Wed, 23 Mar 2022 17:13:03 +0800 Message-Id: <93484b102d5ac22408bc76f7feecc26406b9e0a4.1648026472.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 Wed Mar 23 09:13:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789591 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 E796BC433EF for ; Wed, 23 Mar 2022 09:13:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243061AbiCWJPJ (ORCPT ); Wed, 23 Mar 2022 05:15:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243065AbiCWJPD (ORCPT ); Wed, 23 Mar 2022 05:15:03 -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 2450275C24 for ; Wed, 23 Mar 2022 02:13:33 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id o8so617218pgf.9 for ; Wed, 23 Mar 2022 02:13:33 -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=7FH2A4tRQ1zxvREu2whUFYnsHPfNFPPnefDubuKWt2s=; b=maw+mFuVe0MXZ3Sf9Y7WA8WnxDTDr/vlHdMZuPA42bKnahbPTjJlcSil8woGWVMZLb rPiGZWjEGh6FM1z6DfZ2hSJOi3JcWlb+s0HRQQVeVFmq/z22lezylj128myB9mglbvkJ PqyOn5iybfu203hWzTj2m2iBG13W7Wj9lRNDcTitJLHPYySvFI3m0y7KGeSAn2V+7LUp 9lMebD0qpSEXRpOthzk64YAdhykRn0MOgxdzZI+no0+iPAvUpSmia6mh8gKW8hcCSs24 qCMQM07En87+sQGYJy4g0zj3vHAUi5Zc0JTIqWtBkpyPgZIFsogQLk1HN2U0v4auqbmA cujA== 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=7FH2A4tRQ1zxvREu2whUFYnsHPfNFPPnefDubuKWt2s=; b=AH6h+kS5ckOv5xKkAl2OuiMRAr916/o/qf9XoxP+bHMR6WkNjB6yNS1U1FN+Y+FCr4 W9UigwSZpbKe9oYtzaVm5yZGpj7VXJNFTvKzbZdD5eOhOy1iNHMvRuutUm3MuEMvmH1v 4xz//f3FeHAbcx48MVIuVDjGbh8gWGk2fj03PmmWpmWAqrVhaXWv4IGejvzoVcbZcjnw 2JB7EYamRcdjwbhgfVs6c3/QrJXqZipiFrs7snsFHv1x+y2L3H9L1fvBMvtpr+37jPtc IWVtOGt5FHxL5j/2FB5WV0+TEj7HRPA4WeqIVshYT6jHjIbMEMsPUkrafMuhBl8lVqEE jq1g== X-Gm-Message-State: AOAM530fkrf5sheP9A/b+5IgSdeQJX2klafe3wSoIAdcKWmBHCmMWS3W aC8asK6lCXImKs79gJPNnxU= X-Google-Smtp-Source: ABdhPJwXV/ykdHzUxPHQmsXYhZxcXGp0/sx+D+FPTQwWipPaHcNRVtFuu9VrPLmcFC8al/4WRyCS4Q== X-Received: by 2002:aa7:8104:0:b0:4fa:2091:d200 with SMTP id b4-20020aa78104000000b004fa2091d200mr33494392pfi.17.1648026812583; Wed, 23 Mar 2022 02:13:32 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:32 -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 v14 04/15] ls-tree: use "enum object_type", not {blob,tree,commit}_type Date: Wed, 23 Mar 2022 17:13:04 +0800 Message-Id: <2b746624ece8525000c6abe3f600696ee8bd0685.1648026472.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 Wed Mar 23 09:13:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789592 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 BDEBBC433F5 for ; Wed, 23 Mar 2022 09:13:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243069AbiCWJPL (ORCPT ); Wed, 23 Mar 2022 05:15:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240803AbiCWJPG (ORCPT ); Wed, 23 Mar 2022 05:15:06 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D22F5D657 for ; Wed, 23 Mar 2022 02:13:36 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id n18so946226plg.5 for ; Wed, 23 Mar 2022 02:13:36 -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=flo5WnekFKg/jYhkQac5ddpxHfhUuvBa6zBhq+XDeZs=; b=X8PcjOQzoJ4h18YGIK0nGUndwvLQdAgPRExOeVZkYfktiR1Ikwa/MpQpSIqII6ypuk uLzx1IkG0Uc6UhpjWZq58Vvh11WBY7ffQva1/jA6kuQLTUZDH7CnvzDMjg4QPE4SF0mI 7LvjW9hRvDTGc4IZmdSVGuAlFUXeQyP/WShBWxjZE83C5t9KXDrxYoY/SjFIxWjHOrn8 qAuFmInnZLIPJ2/DdsKk6TYtK6ieE/78+UDUEJZksp/WMXfEHMnNFcSD8nBY106KjgKt Ql3F51lBkxTpZ6Br/5+McmYjHuIJVPlpWy3B/uQdr+oirpkHa/DIQwbM4IcEXHq5FKyA TAkQ== 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=flo5WnekFKg/jYhkQac5ddpxHfhUuvBa6zBhq+XDeZs=; b=HiYIaDiC/UAWuXeTGSx5oUuqB2kNwSZzRYa7AE/pA3YutbtNIeIDvkAB5K8SLHpELC xigpl/lLiI5dPMoFpLWcK9l6RIhSlzXtKYemG5KWfxleDGN2aArhVO35JDL2A5hC7F+h xcwVm+i5mUbkccMav6sWV2L+RcQZpSoel1frajVlBNPDE4sKhoaKqvmfcoddH3I0EStF urE4rWGLLbNqoJx5XNrhNyhxX0MG1D4C8KsC4rBzyXjHtrTk/Rt5wyNQHyuRCIsBf7lK HHdPj/KN/i7Wvjp2X472DK3MeAU0KSK2mP7djAkBYwwY+zL05sn0OAyZho2tfac+WAVr rU0Q== X-Gm-Message-State: AOAM533ptY6Y3DRvyZDqlRq8o9FKcXgPdBYZiO0Nt758bD3Z+BBRKLFE BJnFIl9NTnVKbyZdG0UN8gs= X-Google-Smtp-Source: ABdhPJwT8jemUskZs9weLSC2Rv8wYP1nu1h5H7ICIrzWU98DeDLnH/si+8R2nXaKDVcc46jXZW/Vyg== X-Received: by 2002:a17:903:1cc:b0:154:31c6:4e00 with SMTP id e12-20020a17090301cc00b0015431c64e00mr20575221plh.114.1648026815573; Wed, 23 Mar 2022 02:13:35 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:34 -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 v14 05/15] ls-tree: use "size_t", not "int" for "struct strbuf"'s "len" Date: Wed, 23 Mar 2022 17:13:05 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 Wed Mar 23 09:13:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789593 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 B1652C433EF for ; Wed, 23 Mar 2022 09:13:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233749AbiCWJPN (ORCPT ); Wed, 23 Mar 2022 05:15:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242497AbiCWJPH (ORCPT ); Wed, 23 Mar 2022 05:15:07 -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 A005D75622 for ; Wed, 23 Mar 2022 02:13:38 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id o6-20020a17090a9f8600b001c6562049d9so1145086pjp.3 for ; Wed, 23 Mar 2022 02:13:38 -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=zbGShpW8GYjRDhu/9DoIl/0h4lyz3H3tjcdUp1qvQUM=; b=WG4v4CuQQQYFEK7/8/JUSIF/FLDlupxteSJGeW8zars4wn25rP80oJhtWwDb3zaCjZ 5RcgWZFiGX4WxDZ6OJQQPnH+9KmKLWZ3g0+a1hTJs8N2SjBb/Ct92m9qYqBRicIdMZCv hM8AP2rwJ2hYqwb0sHrBQm2cQM+21Y2odrAWtSkhvRT+uGmFaYh4Pzedc0mRgnEAxFRb iUIQoTKEyWIYOwuXW7JTwY6TfX2vrqzrD76QMJ7ilaOVpz0PbWRqKSu5oGECh2q7y5ni iBRpj/rBk+hkupYjtJVrxA5ISlQ8/2JRMmFa1kXqm9gT11ckQvwQB+MbNHUmDkbmhOdT CnBg== 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=zbGShpW8GYjRDhu/9DoIl/0h4lyz3H3tjcdUp1qvQUM=; b=BERgCcIcuS7v0suz3BCKirMx5PobyraipSJi+vJwVii2YAMgp/xA0WtoiSuVYzuFPi rpsp3oIQU0chi40Yo6WTJKja4chczk4LEkgs9LWUZnIpl6CBraBBm4Lyu3/fzaJOhuUH imjlH3NBSzYC+lzN3ZX5vWVeyxLQr2c6TId8kwwLcEfBpLRvUVwvLatCxtGT+GHOIJ0n Vczcov+chrvE5ly7x5vC0bY+ushbtSyqS2lp08UB/pAep+dsKq2PgTS7j6e+Ar5Tmwoh 2frsXw4c5p6pyqsNb/1Vqpa83OpWr3P7er+kzO9FoMLjBjfS3CkZU3FN3ChOZD4U50Rp BCZQ== X-Gm-Message-State: AOAM5303nnvwVYx4Td6sj0cyApAHr3b46OAF1u3/dUbZjwR9qr+ZIIPC Fbd7XqrzzsXCS80m4OGA62E= X-Google-Smtp-Source: ABdhPJyaHIf+uHYKG8PHEez7CjTTXUck4/bk+HW1fLBQF/tf6gx2tVThDf7RLehBvF5QmLKDmvjIKA== X-Received: by 2002:a17:902:c105:b0:154:81e0:529d with SMTP id 5-20020a170902c10500b0015481e0529dmr8328953pli.1.1648026818135; Wed, 23 Mar 2022 02:13:38 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:37 -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 v14 06/15] ls-tree: rename "retval" to "recurse" in "show_tree()" Date: Wed, 23 Mar 2022 17:13:06 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 Wed Mar 23 09:13:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789594 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 608ECC433F5 for ; Wed, 23 Mar 2022 09:13:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243112AbiCWJPZ (ORCPT ); Wed, 23 Mar 2022 05:15:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239897AbiCWJPW (ORCPT ); Wed, 23 Mar 2022 05:15:22 -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 59EBB5D657 for ; Wed, 23 Mar 2022 02:13:41 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id kx5-20020a17090b228500b001c6ed9db871so5808804pjb.1 for ; Wed, 23 Mar 2022 02:13:41 -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=LSCgYerIJoEYaQDkukdioFVpfUA46EUlOFZymoT0//k=; b=POTwnpL1GvjMUQLasfMYimKZpHxYFSOerSx5FLHglyXOfAR0EkL9bJPUlCDx9fsr3+ F7BtDJDdVcfhrr0cdYO6WPgSU0AOWAPLbPOZHOVF2PlvI3zeJnBFlSCLZPpJuURGexNX 8opXdT9HWUjpIzB1f4Yl4p+79O/sKTgUUB3SFVePI/BvxUZ7KjECGWt6Hxv/4xBfd8P2 5wGbQ2kjkxdAJhvjn1xpy4a1oddZSDjAHn3Mo5s0mTwkMq2w7hOswpWkvAWjk/VODAMr Wq224aWpYxjGWwM7Lp1KN3XBqXtzX2CO6r1IQ29GsycS2iyuQgg7J5QDrklixzsHXfQK KpnA== 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=LSCgYerIJoEYaQDkukdioFVpfUA46EUlOFZymoT0//k=; b=IgIcUeSbaHy3LGCRU8/0jdd8aN8okl09iaZRKEaMEd3XfCG6LjSfsFlhTEZDH8OUKZ tA4HI4Lb7PIjclrylFAW+FM2SZbDD2hbCUGyCc0YjNeV/bg3L9xYh+51V5QjUH0f+2an wiKOiRkVRwiTBL3v5Gzo8W3VWl3pJpDk1p0ytKpQqkQC24N2gL/dhS53oV4tVkrRfk8+ xBFbZdgSqVS/vTgmu6tiZgs7ukPFEcnAM5p7ebe8DGa/EGXhGhD55rxVP218UxmUB9fX 0k2xK825WLAnxFY7w5zwzed0ZjrpKL34Lic07TKsLbaJq7AM9nOJdhnD1rBx5C3Az22K +vzw== X-Gm-Message-State: AOAM531GLev7bZA0bptmMTv2Wo1QFxHLsMhNu2nYQvoLs/i0pFHf16Vs W4fe9B6T5T3kTyEMWXHI0Lw= X-Google-Smtp-Source: ABdhPJyd2JNN6yk0W3Tm4BK81Yxtp/ZKjXJtkDNirbuj1a0/Q5cqmsExDq3YZaVb0GQ57YjmSRZbTw== X-Received: by 2002:a17:90b:1d82:b0:1c7:1d3:f4 with SMTP id pf2-20020a17090b1d8200b001c701d300f4mr10364020pjb.223.1648026820829; Wed, 23 Mar 2022 02:13:40 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:40 -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 v14 07/15] ls-tree: simplify nesting if/else logic in "show_tree()" Date: Wed, 23 Mar 2022 17:13:07 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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. Helped-by: Ævar Arnfjörð Bjarmason 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 Wed Mar 23 09:13:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789595 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 92E76C433FE for ; Wed, 23 Mar 2022 09:14:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243095AbiCWJP2 (ORCPT ); Wed, 23 Mar 2022 05:15:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243086AbiCWJPX (ORCPT ); Wed, 23 Mar 2022 05:15:23 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39FBE75C3D for ; Wed, 23 Mar 2022 02:13:44 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id p8so1021285pfh.8 for ; Wed, 23 Mar 2022 02:13:44 -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=u1MMu1Eg9zHoUexbH/JvOn9zvm6XnzHJnDc3qWW1ZHY=; b=VWzR2CjzXro9gtKrOdTZedgbUMbShPFjsAeHvsV+JbQUC4DFjZm8Hu55zEGMvALSbv RrJtLGEqPza8WqY2ZMCW2GicPjBTOex4DAIOAqRwo7+f45RKz9zF5DGa3eAlLiffQMvZ wBsptsetWiskzfZsNXmR1LGdGajdBmls4XTfoE6C3ZOsI8uOf3BwXCeKEeqwug1mkwne Y4aRr/fsU5Ge1XCiE13DFXwAwlZhQKSmpvtZivWj/eU+7kisoBXzIbF+QNO2HZ+xfO8O sEPChRZxF8r5Ae86sTtZTUxo3T0GBQyl0bmuW9eBv4e6T1t43revKyvAcpIDrr5ak1DW XXhA== 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=u1MMu1Eg9zHoUexbH/JvOn9zvm6XnzHJnDc3qWW1ZHY=; b=0tcOn4sOiVMegb/Vr0UTSQIQPqOQgSJm7lD6CRurpYmJbquPRuCHb/yyVx06Ef+3AE IxHN51yDEVMzo93DFYIIeYBr6zzkck/KjMTlZXIF4shwXhtV+zpeDbnu/KHmbpALwuNh UiViUGvVQVgvXAgQfU3+MFCznpEjM6rDRsJ69CWbnrij+KIicrlcGDafaDyEwT1swa79 bKvY4HvUkJARApZgvWJ1DT25CQqabULuaA5GRUm2jYjBJ7Z+RpfKk7fv+ijjLuafvDlO UFBACAkY793n5fTSULGh7pZqWuHF/uMtHmzqma8fQCpZTuxLOqexA2LDxHR/TfYJMbGo VsSw== X-Gm-Message-State: AOAM530QdGpniMnpIpWFtgdC7H7kmLWNQYSyKBy7ZlEkJLMpwDtcTeMm 0wKT+OjYhOm/NmP2zaTdmSI= X-Google-Smtp-Source: ABdhPJyIj0U8OuhK4+wU4wKRtDbX7mF24M106g1xYrSUdqQMv8Hqh5joWOkH8tp79dFUkiXqWAeqaw== X-Received: by 2002:a05:6a00:a0f:b0:4e1:309:83c0 with SMTP id p15-20020a056a000a0f00b004e1030983c0mr33746441pfh.68.1648026823618; Wed, 23 Mar 2022 02:13:43 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:43 -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 v14 08/15] ls-tree: fix "--name-only" and "--long" combined use bug Date: Wed, 23 Mar 2022 17:13:08 +0800 Message-Id: <1d1808bdf0a07b500d9242dcfdbe4831f612c67f.1648026472.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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/ Helped-by: Ævar Arnfjörð Bjarmason 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 Wed Mar 23 09:13:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789596 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 90BEBC433F5 for ; Wed, 23 Mar 2022 09:14:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243087AbiCWJP3 (ORCPT ); Wed, 23 Mar 2022 05:15:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243078AbiCWJPY (ORCPT ); Wed, 23 Mar 2022 05:15:24 -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 A925675E70 for ; Wed, 23 Mar 2022 02:13:46 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id n7-20020a17090aab8700b001c6aa871860so1160599pjq.2 for ; Wed, 23 Mar 2022 02:13: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=Zj9pkTsFlErJXEhnU3DaQ6sNjOOJhZ5dYfJBZ0HaiNw=; b=YX4vt0r5QiTPbcernH11cO3rbUCWzEKCiSeuK3OrHYXJLghqNiH05d2228+oDNLjpI KUN6HG+k+EylnMTfPV975sWRbcn+8Ohb9fKpMNbSPNf/4FVpEa7nAPkhZFPceAE/FCtH SP9xUyIL8DJA8AoC5UIlB5ONYSDSfIms/iNhEljpKdMv72en9ix4PzpK2IZ8YY3DiphS YCxDTVPgNgsYDMu2xSJnthkTJESRyo4wOeNg7m3J2mcT9sYRrc0yhviZCZGnc9Kg/vHF I3s3b4s3pqR+BR8grCcNAqdUCtNFbjG1jz9plerdQSp1eTki6hquUSP+lu4rI83RA2xz iyWQ== 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=Zj9pkTsFlErJXEhnU3DaQ6sNjOOJhZ5dYfJBZ0HaiNw=; b=d0QerILjRLtpWRecSZVfNH1BJ31C2Ng+sO9oStosdc3o2DuPRuFv7We9YhAfeq2wOd 350uswpAPSr192gYlrF7Rz6l/YGnEDwByoqvmCfFAVr6InhFaM2mfeB4W6s3UkSfMW29 45zv+kjkDSW1rTODvdPiFLIhHmOrQhJ6Vra/kM4fuwfcgzmznF4Sas4uzRuTJ3oWVwk9 9RuZ0Jt7jxVFLDzUaQVs85+22a2CTbqz6kNcTwR+GehZx/wfwrfl/p9n2aMttZVgAGfq I1JoOOp9bbD5GbrBefzL6dJd2IZnPpsSv0Y0NGpXbKwQ5qY1vSR5zNLxzclYlGTOXZvH 5Jrg== X-Gm-Message-State: AOAM533YrOjAHZKtute6HkrYlBoz3rY+9AGiWEUZpefoewXiHeMGOAcx 7UXtnSD4Sg+Gz/8gIzpa/lc= X-Google-Smtp-Source: ABdhPJyHusehLEZj1D6UAImTIh0NdREbgbmGSWiK+sR10atMLpjvuMGc9IT3t2XK5n39lk/dzO7kVg== X-Received: by 2002:a17:902:f643:b0:14d:7b8f:14b3 with SMTP id m3-20020a170902f64300b0014d7b8f14b3mr22610865plg.19.1648026826144; Wed, 23 Mar 2022 02:13:46 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13: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 v14 09/15] ls-tree: slightly refactor `show_tree()` Date: Wed, 23 Mar 2022 17:13:09 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 introduce an enum "ls_tree_cmdmode" then use it to mark which columns to output. 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 "short-circuit logic in "show_tree()"). Helped-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 98 +++++++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 37 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 7be4c13228..173a534e92 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -16,10 +16,8 @@ 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; @@ -31,6 +29,11 @@ static const char * const ls_tree_usage[] = { NULL }; +static enum ls_tree_cmdmode { + MODE_LONG = 1, + MODE_NAME_ONLY, +} cmdmode; + static int show_recursive(const char *base, size_t baselen, const char *pathname) { int i; @@ -61,6 +64,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 (cmdmode == MODE_LONG) { + 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 +114,22 @@ 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 (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; } - 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 (cmdmode == MODE_LONG || + (!ls_options || (ls_options & LS_RECURSIVE) + || (ls_options & LS_SHOW_TREES) + || (ls_options & LS_TREE_ONLY))) + show_default(oid, type, pathname, mode, base); + return recurse; } @@ -123,12 +147,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, From patchwork Wed Mar 23 09:13:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789597 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 F2720C433EF for ; Wed, 23 Mar 2022 09:14:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242497AbiCWJPa (ORCPT ); Wed, 23 Mar 2022 05:15:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243119AbiCWJPZ (ORCPT ); Wed, 23 Mar 2022 05:15:25 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1A5F7629A for ; Wed, 23 Mar 2022 02:13:49 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id w8so932158pll.10 for ; Wed, 23 Mar 2022 02:13: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=i66FOR8tkiHOUzGM260kwQJtPAurnrmyRZzWJuY22hE=; b=PGtYA3JuDXTNMYNi21abHtrQFY0bnbMJ95hfJFaffNBRLaCMmhVz/4rIbCj8dBQXy5 dub2NbKOHkBqMKFWtagGmiUmZL/9ud/IyknG3uYfp7z/6TqD3yz6TkTj9e9v9SJBi4QQ tBVELU0AzVWl+daRW4upxmTKtyyiV1csZqg+d0S1PBmBYeloYWYx2UoT3Ognx2ZwlS+r /hcPfgGt8RqPYjrvndORP1Xc4BwSQzfMfWmB+YvuX6Zj6D8Vhfv+OkuR/aEWv9PpVML6 BF+OZRdv8L8q6r+KfmtHsW9nZg+tBTneK+TSSOFGTeD8qU20aMH4KNVea6cYeOpVsU4k HzfQ== 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=i66FOR8tkiHOUzGM260kwQJtPAurnrmyRZzWJuY22hE=; b=hDRihHC97oc4YvMyFFvvUVHTjHwq0UPDAikSFjYbuTCph0u2aIO3KRG3vxTA6olkAz x5LvNYrYg3jM8sqLX6RsHo2fsNbVwzNPe1C9z2VhyvG0EV4Ox6fv58IIH+0Cu/Bij+n6 Q60R6OkSA/Gi/yZgP7ER/8hDwbUl4dehMJ0yuAD1jTR/duYc9bMo1DejI5LbXkO1nmJv 90I+T9sDRZvFCzxD9CdID0wRZy4IhIcOkSVxPk0zg3RZzP3uxKj2/W0LNT0o4GaFCbsj PAPail2PAoFwkEunlzpyMlwKhwbenl9tMFfKuOhZ7Hh/wccXtxbd9Rnh5lH7HW8C8e9D EbCg== X-Gm-Message-State: AOAM533vXsfv1lPyJiUL/4IOgqq1zHVZp7b05t9odHjp4oLFZXdJiWqL 6JKsmMT9oz0auqBf3KT+y3o= X-Google-Smtp-Source: ABdhPJy+HGoz5lJFS+llotzfr60WxM2Qp2uk62wHWz2dYzYvHz5kV3NHRJ3KYJrsfseBGccnfFe4NQ== X-Received: by 2002:a17:902:7141:b0:154:28e0:9720 with SMTP id u1-20020a170902714100b0015428e09720mr21692702plm.116.1648026828693; Wed, 23 Mar 2022 02:13:48 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13: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 v14 10/15] ls-tree: introduce struct "show_tree_data" Date: Wed, 23 Mar 2022 17:13:10 +0800 Message-Id: <5131c0dcff7407220d43e1ea507047053f65be39.1648026472.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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. Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- builtin/ls-tree.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 173a534e92..edc8bf53a6 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -23,6 +23,13 @@ static int ls_options; static struct pathspec pathspec; static int chomp_prefix; static const char *ls_tree_prefix; +struct show_tree_data { + unsigned mode; + enum object_type type; + const struct object_id *oid; + const char *pathname; + struct strbuf *base; +}; static const char * const ls_tree_usage[] = { N_("git ls-tree [] [...]"), @@ -64,17 +71,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 (cmdmode == MODE_LONG) { 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), @@ -82,18 +87,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; } @@ -103,6 +108,13 @@ 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); + struct show_tree_data data = { + .mode = mode, + .type = type, + .oid = oid, + .pathname = pathname, + .base = base, + }; if (type == OBJ_BLOB) { if (ls_options & LS_TREE_ONLY) @@ -128,7 +140,7 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, (!ls_options || (ls_options & LS_RECURSIVE) || (ls_options & LS_SHOW_TREES) || (ls_options & LS_TREE_ONLY))) - show_default(oid, type, pathname, mode, base); + show_default(&data); return recurse; } From patchwork Wed Mar 23 09:13:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789598 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 4538DC433FE for ; Wed, 23 Mar 2022 09:14:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243082AbiCWJPb (ORCPT ); Wed, 23 Mar 2022 05:15:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243090AbiCWJP0 (ORCPT ); Wed, 23 Mar 2022 05:15:26 -0400 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 EF54B76580 for ; Wed, 23 Mar 2022 02:13:51 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id q5so950312plg.3 for ; Wed, 23 Mar 2022 02:13:51 -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=+voSzOjW1RM4B04bcnSPHxziNDQXrcmPqzG5XP3QSNY=; b=FxU2qMeDSZQZauHx1v1lf44vr8NmFn6MeAm93lkmbPeybLQYeCzrU2DYDR9fts+Khr jjkccq8ZHFyh3wB7SawcnoY3f/IHMR2U1Uht25xqIOQaZyh7lewzkrt+I9mk/BtZ6GDD JTVW6v4c4WmNSaBbU9cKzd5LsMmrNQ3NUkb9VsNh2tAfl8AEO0rSzX0xzNh/3p9h3Lex sRXcR3pwAN/eE15rNpcyJzLyHP4VK84Qe3Z1jCRr99l3J46Iak4RW1UT0Iu5RzaNnYmh sYaWMIXJKjvtKMzDU7ziMb/MdJidx6Pwgt5RmVsk9EmiSXMeS3lOJ0EEsZGqVpvXbTCM ndpg== 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=+voSzOjW1RM4B04bcnSPHxziNDQXrcmPqzG5XP3QSNY=; b=W73o549k/n0XCz+ykInik9jxXY0jZECsb1G+D2VpIGn28SJqC2zAmmdliEdC0fCcaq ab0ZrQRQbau9JHqW9PTU5WrkwSOCOsmiVAHfEwF9a0GWzhjj9s58vVXzMq+5e/2po5Mh koLz06i2fLPm54GHy0NaZRN4uQ7qwi5fSiDny3q1q6R5OajBZ1Wvg+nS58bx0k3HU1Rk WVz7MVxxJ+HjTymkU4clF08hWLitTth18LI1xY28RH0l1LV08t4tt+Uhg6BeXPqJokOf O5EB6OY641PgdS3fdKfEK1VEfmFGWVkOztAgToNPoEfO88w5VUgHYdgOqrP98Jf1O3Xf L0dw== X-Gm-Message-State: AOAM53093TWz3dvYxRltOhMZz4Gf7RhfdtV1gST3WnNnKQfuefNpVXHz aBmNDGB1GvQxl3eRIm+qBKY= X-Google-Smtp-Source: ABdhPJzs+NhDphJ9cGySnc92DitUx9qNGuL8mUDFo+eXKyxOYz3R2BOVAxSalcMiZv9vfSylLdpm5w== X-Received: by 2002:a17:90a:d3d1:b0:1bb:fdc5:182 with SMTP id d17-20020a17090ad3d100b001bbfdc50182mr10426440pjw.206.1648026831406; Wed, 23 Mar 2022 02:13:51 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13: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, Johannes Schindelin Subject: [PATCH v14 11/15] cocci: allow padding with `strbuf_addf()` Date: Wed, 23 Mar 2022 17:13:11 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 Wed Mar 23 09:13:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789599 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 537AAC433F5 for ; Wed, 23 Mar 2022 09:14:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243113AbiCWJPd (ORCPT ); Wed, 23 Mar 2022 05:15:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242945AbiCWJP0 (ORCPT ); Wed, 23 Mar 2022 05:15:26 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94F0875637 for ; Wed, 23 Mar 2022 02:13:54 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id u22so1029793pfg.6 for ; Wed, 23 Mar 2022 02:13:54 -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=aHDdzOh48ZBYUsTQBACBR/bI7aX+2OW+rhzYq8lmdoA=; b=fCLXV0tjqsKfXAhEfHcLHmMHxQelSWjW7SSjulgmljxg5aNhhIdUSNSFn5tLyLtnXl risEhfl7qdsZueS3DN+unGHhjnNMERPIHtF5rfCN6Zx3rbK+zgsO0JoqMQeTtNB3AwSm /Fe8f54HblYzodpXcT+e/uDIvLPz22egQxvSAAuaxGpdLIpHfHTmef4oEKEA9gcaYfFa acJnEItcmdGE7XIFyqkJzY/2kd2xHf8qDIzfKmQ2C7UkT1mE0Gxsz0ianYeNOot3/j+E XenGEiXHrc16VVyakA5nabcy+65YSiyXPb0c2hIFMk2KH4PHTwsdWNgjYNKCDEf1/yf1 21KQ== 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=aHDdzOh48ZBYUsTQBACBR/bI7aX+2OW+rhzYq8lmdoA=; b=s3YqpZ30yl9CdFki34ItiPqCmOn1F9+FbP9IOL1S+axeQEQuqbeY9i8QYe5aDOYtM6 nS7h8uoBjw6WmdtnXrokFx8O10dqcAi+aPCGlFjwsIaJfiOM8vVz0N+UebciReepvg3n qtec8YAmP8NvoB4dy0cAXwPiBTsvaMLjpaLtsupfAWPEtHF+PRnItleYP6L82tB5bY6u Fom8XeeHLxqvsY1L60eX68O5Wi39+tP5nRNoL8uQkjE5+eASRkafzxdMQ+9ZH5sXYTnH lVoAYzeS+8Q+l5keo7NJO5jNu/nuriIalXO+KUWW2RQzG6Cif8GN9JALb9tyNFQQNSaA 6zKQ== X-Gm-Message-State: AOAM531IICrwcUK3TSOzM2mSbanMCgxyJ6FjRgvtdmh7scoNW5e3ILQW ZUQ2HyWhoc5jMI3MOfOG9/M= X-Google-Smtp-Source: ABdhPJyJ2L3+7Z4AyLpYzUSJffMMt7W/TsvAe0Ifiy2Jw4dgrXaAOU4iuHIMqvEy+roggeUvgPGs8A== X-Received: by 2002:a63:381:0:b0:385:f8e6:bea1 with SMTP id 123-20020a630381000000b00385f8e6bea1mr4439800pgd.120.1648026833987; Wed, 23 Mar 2022 02:13:53 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:53 -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 v14 12/15] ls-tree: introduce "--format" option Date: Wed, 23 Mar 2022 17:13:12 +0800 Message-Id: <8b21b204a5c4be137f47ecb3a07373d36d171019.1648026472.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 | 153 +++++++++++++++++++++++++++++++++- t/t3104-ls-tree-format.sh | 67 +++++++++++++++ 3 files changed, 272 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 edc8bf53a6..8a1bf2fa4d 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -23,6 +23,8 @@ static int ls_options; static struct pathspec pathspec; static int chomp_prefix; static const char *ls_tree_prefix; +static const char *format; + struct show_tree_data { unsigned mode; enum object_type type; @@ -37,10 +39,77 @@ static const char * const ls_tree_usage[] = { }; static enum ls_tree_cmdmode { - MODE_LONG = 1, + MODE_DEFAULT = 0, + MODE_LONG, 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 show_recursive(const char *base, size_t baselen, const char *pathname) { int i; @@ -71,6 +140,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; @@ -145,11 +246,33 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, 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)", + }, + { 0 }, +}; + int cmd_ls_tree(int argc, const char **argv, const char *prefix) { struct object_id oid; struct tree *tree; int i, full_tree = 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), @@ -170,6 +293,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() }; @@ -190,6 +316,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)) @@ -211,6 +341,23 @@ 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); + /* + * 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; + + cmdmode = m2f->mode; + fn = show_tree; + break; + } + } + + return !!read_tree(the_repository, tree, &pathspec, fn, NULL); } 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: 12789600 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 DBEECC433F5 for ; Wed, 23 Mar 2022 09:14:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243124AbiCWJPe (ORCPT ); Wed, 23 Mar 2022 05:15:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243085AbiCWJP2 (ORCPT ); Wed, 23 Mar 2022 05:15:28 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C9A975C1A for ; Wed, 23 Mar 2022 02:13:57 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id b15so1032598pfm.5 for ; Wed, 23 Mar 2022 02:13:57 -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=Ev7CrIs9nVSw5cdnSNV5+12ng7Edw3fNhGpobXgbAdQ=; b=mucBN08nJlTro9FyWCClMwQMxVc9maNcrCshWhvm1RbNYV1/daRS4cukQ5X9K/cfRy MJ/wEPdbmYGbahrjGt0h/NyI9PiknwA8oe5cQHMwRf3VzV3ak+asIaESGyZ11C1YwHnq +xf6UcHYAEIqziSdLs3Z0EgC3OVlBpf8j9Rg+5HRQmoe+utDTapnyO9ehV/SadxZpe6H bVg92J5auLi08R3iaz2NC2Bp2QfQzo0XMqtLHoeLQaI+hNeo+S2PDd01MrNf5BmxGJrQ FoqktNnmnC6SSstJJA5RlAUyJ8zpBh+qbbqDzrVq1M70doCc94b62Bz8/pSbzN9X5Q0w I7ig== 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=Ev7CrIs9nVSw5cdnSNV5+12ng7Edw3fNhGpobXgbAdQ=; b=nGnczyhgDVtC3fKZ2nsrr6eA66lonsmuffUpE+vF6+/VzINsvQjy0mvF2CqertDdEo rpjdF9pJLYteMn0KAjXH6uPBclesBI4fkLUJXypXYL4lOpe48OiTrzHz7TAYhTcWnYKk 7YPCu451Iu65kYUbpaW3yAhZJnYsQxbdnPKuwTW1tE5IugbOpegVCEI9vtgejQtL1SHC QY0lXyZAZ4LgX9u5VWCip6abM31URuBE6ZuFpjklJkxz4EjiFj922H4qX6w2Mlj+Hywb 9jjTAhDBthGigbW8xSBl7c8SmGpxwpARKa5QcUipATqbFgWnH8IpMOj1XN7oKujWofFw 9o/Q== X-Gm-Message-State: AOAM533NfP4MnavLs2NqLGBZ+HQZK7tnzfyiAKixrMUdpzCowKSmSspC ljhkxDHNd1ibrRNJx9Sqf7g= X-Google-Smtp-Source: ABdhPJzq0XqIQqmznbsGEbxEYS7RMqs3bAULiwtOCvPrbvItzbHyXJVQjWC1g1NYyaKv96YSkSkczQ== X-Received: by 2002:a63:3688:0:b0:382:8bf0:aad7 with SMTP id d130-20020a633688000000b003828bf0aad7mr10314974pga.602.1648026836957; Wed, 23 Mar 2022 02:13:56 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:56 -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 v14 13/15] ls-tree: support --object-only option for "git-ls-tree" Date: Wed, 23 Mar 2022 17:13:13 +0800 Message-Id: <27fd1d9e2c2fad8159b4a38709929e0b66d5f93f.1648026472.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 Helped-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- Documentation/git-ls-tree.txt | 11 ++++++++++- builtin/ls-tree.c | 13 ++++++++++++- t/t3103-ls-tree-misc.sh | 7 +++++-- t/t3104-ls-tree-format.sh | 9 +++++++++ 4 files changed, 36 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 8a1bf2fa4d..3474f8c3d6 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -24,7 +24,6 @@ static struct pathspec pathspec; static int chomp_prefix; static const char *ls_tree_prefix; static const char *format; - struct show_tree_data { unsigned mode; enum object_type type; @@ -42,6 +41,7 @@ static enum ls_tree_cmdmode { MODE_DEFAULT = 0, MODE_LONG, MODE_NAME_ONLY, + MODE_OBJECT_ONLY, } cmdmode; static void expand_objectsize(struct strbuf *line, const struct object_id *oid, @@ -227,6 +227,11 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, return recurse; } + if (cmdmode == MODE_OBJECT_ONLY) { + printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination); + return recurse; + } + if (cmdmode == MODE_NAME_ONLY) { baselen = base->len; strbuf_addstr(base, pathname); @@ -264,6 +269,10 @@ static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = { .mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */ .fmt = "%(path)", }, + { + .mode = MODE_OBJECT_ONLY, + .fmt = "%(objectname)", + }, { 0 }, }; @@ -288,6 +297,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, 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 Wed Mar 23 09:13:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12789610 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 8BDB7C433EF for ; Wed, 23 Mar 2022 09:14:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234667AbiCWJPz (ORCPT ); Wed, 23 Mar 2022 05:15:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243088AbiCWJP3 (ORCPT ); Wed, 23 Mar 2022 05:15:29 -0400 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 3012575C30 for ; Wed, 23 Mar 2022 02:13:59 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id o6-20020a17090a9f8600b001c6562049d9so1145836pjp.3 for ; Wed, 23 Mar 2022 02:13: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=vBdODXO6ATaNZ4WZ5hNMH/6g/FPAMQvNC5QptkE8s8s=; b=Sx9uSK6qOe/mwLn/XI5N8yu7zxDDpTcehvOAE1nzDDCACrtCUiPu7ewp7f8rQEKGOL kEsOiNTeLXAxImdIey6yQO151pRH6AI50oYsBm783RqP/gc2f+QokyKjFaIA+HxRuDTN iXM8omNYud0jT9y06U5626n8vC1+CUdFu3PC7XhtZP6i4gMJOnwWomK7vXTQryxxxwYj IOBnnRgbIyniowsJVqCG8BEzmnQkjZwUvYqwnJOtktz9i4mYc6nIUQ2R7/TP/AAU+9MI wVFAv5iEbNCesWggXnnOJTSe3dMb8qi/28H2L2Xj0rHW7ddNK9heFWjZEbz11/ZwEZ4a u3jg== 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=vBdODXO6ATaNZ4WZ5hNMH/6g/FPAMQvNC5QptkE8s8s=; b=7xKAkGj8IPhI/o3nx96rz6ZZ3aLMYMsjwU8Ci/PJZW/iN3sPW842J/DQy3oBCfGg1d gFO7Poxw70RhepSEIcuceoqM7Ty50+orfgKUnBQiQKeThB3AtFSq9azkgwQdfQRwZGbZ 1Sq/4b1+cJ31xQm6Xkrkcr+QgREBSZApoWw6JIcC9pCRQLAUuQip2yP23609Pbd78qAW kJn2e5KjwlGD8wEwTBcecZ3Ww/uvWJzlKWVBaKh06YQGKgX15MiCClwKO8/SA7FkASUE U4+E2YRnqY3y0aXonwiCRPqvsfh0LqrEhlJkXMdY+PFb1yvBGrDrttBxwK4KdVmHBRDX 25zw== X-Gm-Message-State: AOAM533lz255DOqGOy6f8FCpzz4WrPGdT/+2GY5Mm/el76OoIM5CJcwP pJ4F94e1sB12cuMTpTURY/8= X-Google-Smtp-Source: ABdhPJzrUxubKx+Z4nLDj/BkEsASNWBBPrDMUQSiJcniauApuqZekau8lWunGsK5SWvlXezh1G56BQ== X-Received: by 2002:a17:903:244d:b0:154:3bb0:7b8c with SMTP id l13-20020a170903244d00b001543bb07b8cmr19751068pls.115.1648026839468; Wed, 23 Mar 2022 02:13:59 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:13:59 -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 v14 14/15] ls-tree: detect and error on --name-only --name-status Date: Wed, 23 Mar 2022 17:13:14 +0800 Message-Id: <1d5d9470aba9564ef75abd550bc508cc173855f7.1648026472.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 3474f8c3d6..6550f27dfe 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -41,6 +41,7 @@ static enum ls_tree_cmdmode { MODE_DEFAULT = 0, MODE_LONG, MODE_NAME_ONLY, + MODE_NAME_STATUS, MODE_OBJECT_ONLY, } cmdmode; @@ -296,7 +297,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, @@ -322,6 +323,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 Wed Mar 23 09:13: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: 12789611 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 5930BC433F5 for ; Wed, 23 Mar 2022 09:14:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243205AbiCWJQF (ORCPT ); Wed, 23 Mar 2022 05:16:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243094AbiCWJPc (ORCPT ); Wed, 23 Mar 2022 05:15:32 -0400 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 ACB3775E56 for ; Wed, 23 Mar 2022 02:14:02 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id j13so937138plj.8 for ; Wed, 23 Mar 2022 02:14: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=q/biLvqSFiqH9pmRg3/K8Zbz+ot4nD0sdObZwmVy9hE=; b=YZ13tXng5T00ifGKVVrcU8mhY6GfmEJ2HggNTTu58DC67/YaudkRw1jTse+F9awmdY /v2QVO9yprsv0LZajMfXVqf2kjQgOlLRbFhB/nSaXRBlsmbM+95F3o78/3hkOpYY79uo F7XjJwe8byHFG4vdR9SGt7dsXcoHCLGE4WfzL982UrMgdggVUI/l1tJ/yMF9KhCx4er4 vhlbN3zbYlRqvWAhWRfgLh7Rw7lMzLB2TCZRMtERjdaxr8yihSvNM+6uqATuHSXzyF8b w0g33GNWMRRoj56wltnm/fi75Sfy9x4QrqIFW2Va3jX1LQ6GGQRi4HWl4EkabSW1PWe4 KVaQ== 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=q/biLvqSFiqH9pmRg3/K8Zbz+ot4nD0sdObZwmVy9hE=; b=qDNKHOXaAyhHAWv9j4H3eqpHBnj0/MaJL7ZSN5MtOiauF9ZK1sAhD7eP/bdYX6UEmT ARVjrUsLkbv1NdOivHiOYkdyLdHqtUBwVLs9ea8KokbR7owiHqI6/8aB8u89o6MUw0nS z0iZcBEn9Qt8xCrSjW0BmcHRnkwV/lQP737/wKuZqNioKj3QMYtCS0jMUsgVsfui9xN5 YgA/TZqKGD8xpacO3+blI9M9oETLRp5mdiblD1KtTRMDU/JzVDWndQZB6ez/34FY1ceo m7sI3FWX4b96r7+/9pyGy/LtbAMb4ZVqXh77rAEM+xlgQH8VnV9fvNfGQzlBSR446LmV ITBw== X-Gm-Message-State: AOAM531LLuF9fzWcxB+2mZ1nQsfWDVwxxIMrBHtTl+R9ZyrqFZTSfz3P K8dYRmJRuCttQZFS9Ex90aI= X-Google-Smtp-Source: ABdhPJzfclOozFltQ0uQ+E7n5JBdGwdyF7q7VZ0cE/LEN3q/NOMI/PT/5wGE3kU2RtsOvaNZVAytzQ== X-Received: by 2002:a17:902:70cc:b0:154:1cc8:9df8 with SMTP id l12-20020a17090270cc00b001541cc89df8mr22934034plt.32.1648026842093; Wed, 23 Mar 2022 02:14:02 -0700 (PDT) Received: from code-infra-dev-cbj.ea134 ([140.205.70.39]) by smtp.gmail.com with ESMTPSA id s135-20020a63778d000000b0038259e54389sm10224261pgc.19.2022.03.23.02.13.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Mar 2022 02:14: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 v14 15/15] ls-tree: split up "fast path" callbacks Date: Wed, 23 Mar 2022 17:13:15 +0800 Message-Id: <55a96e933b0949bbf3f33153b8e370af6c6299bb.1648026472.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.404.g92931a4a2c.dirty 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 | 199 +++++++++++++++++++++++++++++----------------- 1 file changed, 125 insertions(+), 74 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 6550f27dfe..44a91cf9d0 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) @@ -282,7 +331,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; - 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), @@ -311,6 +360,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; @@ -365,18 +415,19 @@ 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 { + m2f++; + continue; } + break; } return !!read_tree(the_repository, tree, &pathspec, fn, NULL);