From patchwork Tue Feb 8 12:14:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738691 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 EAE93C433F5 for ; Tue, 8 Feb 2022 13:14:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359587AbiBHNOq (ORCPT ); Tue, 8 Feb 2022 08:14:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350205AbiBHMOu (ORCPT ); Tue, 8 Feb 2022 07:14:50 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1053DC03FEC0 for ; Tue, 8 Feb 2022 04:14:49 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id x4so6090477plb.4 for ; Tue, 08 Feb 2022 04:14:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8ffNNihJnqCbLjL8fklpTgd0y3rLXxhaHEiiAvRIvao=; b=MRCDwcCsdNjwvXNSBafn94z76+LGbn95sCDizB55n8lAkKBW85MljQxISZ/7fcRc6S 95yQ2TUEQgW8CSukI8isTvM+TqZ+CGvcNOnzJouYKPTcbPhzJYg/kXWTbx3UaQKIXkom vg0TmNFHf6YpSAFdYA6Z4I+DFZcb0viDVGRO+htxPA7pjn3l5LEI2CnQZ2nQaNir+tjj 3RQNlPQ07ImULDU5tkaioj16Jk7x2yBRDnf1ncaX8z0cioAyQMy9XnBUi1KmAA3L2wjO E34VjpNH61TogETKbZhX/OZgq8bq5EICNeZkuVyDhFGpV+4dUtQx15foLZHNL34+pqur M1aQ== 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=8ffNNihJnqCbLjL8fklpTgd0y3rLXxhaHEiiAvRIvao=; b=UrhInmy/jlTWtMGk5E4I95Oww1cUoz2T66FU3ZnQ6qufk7n3eP7PiqwtHBcSdPkwYg DABslQzcZIfeMYEJlBZ1j+Y8KGtT2DeYPU7COtX+3kYEZOa4PXap+8WUNhuJ6ecKA4HE 4rqDcQYb29d0DfuZlA5TtODhQn3Xqrj/5wdGp4NCWF/r9YIXBcU4xkh66fPWXaVPDolw ioSr/+sFF3DUvFiad49rCDEPncZmO1deiJbH977nyPw02COzLmks3oQ6tB6Zmhm56i3h kXVBijMp32tf6Gb24erBRfGRb4L6ascYNYKT1VArprMo56+6WFJvm2Rx185WAhqdtidK RFvw== X-Gm-Message-State: AOAM530iYu4ln1tYfv8mRAq+efUMhEPLcckZOh/+9E2a3Cy/tPxSXiUa Gc2qUiqbGHOpeGOqpY3X0sw= X-Google-Smtp-Source: ABdhPJxPIfFjev9loyK5Js06o8Pte2o1DpzBUuotM0pJ/Qk3yUaW0aM7l+YETGJBrg2lvgkXK/TFIw== X-Received: by 2002:a17:90b:3808:: with SMTP id mq8mr1033929pjb.203.1644322488576; Tue, 08 Feb 2022 04:14:48 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.14.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:14:48 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 01/13] ls-tree: remove commented-out code Date: Tue, 8 Feb 2022 20:14:26 +0800 Message-Id: <2fcff7e0d40255e0be9496647787352dd0aeb784.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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 Tue Feb 8 12:14:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738697 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 1794FC4321E for ; Tue, 8 Feb 2022 13:15:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350660AbiBHNOr (ORCPT ); Tue, 8 Feb 2022 08:14:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350221AbiBHMOw (ORCPT ); Tue, 8 Feb 2022 07:14:52 -0500 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 A36ABC03FEC0 for ; Tue, 8 Feb 2022 04:14:51 -0800 (PST) Received: by mail-pf1-x434.google.com with SMTP id z13so898657pfa.3 for ; Tue, 08 Feb 2022 04:14:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Whv3pYMu8nPr6t8743zsp3QXUodPLiauUUIFsR6ts2Q=; b=KM4M245jwJ2NZv/YbROfRuWXU+kNggDrRGUNXNXfS/czgXYheMsr4Ec4FlaQBvlYv+ tC3+uu3fjovA9ul6UuNLMFOQ6jtSZ+6A+KFnw51EiXQW8b9lLO2+S3PulX1Us9hzj7VJ D8zDMUCh1TcYsrBL1N4DuCUG8Jr30b/01kA8TFDAc10jvyWWLhhOKhLwExLETtncHUvG IH4y30u/XP5PUsVf5CK2fvMFq/IbVIC72yLqovT0iReN0FrPBACROFPNdoNaYd+csk43 OSomPIbUh80CM3xyQNBNFCzFU735xmUeS2PqKuxpn6Wfvh0sOpHusLvrq1I444IiT0Ra HRzw== 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=Whv3pYMu8nPr6t8743zsp3QXUodPLiauUUIFsR6ts2Q=; b=i+5ZCDXf+broaivqA2PYNgTq0vZWrpU2kTptrsyeVZjC/H6lAAQmqa+8kfkmG+VT/B Q2cFboFn6EuhBHfrPMixsd8vAoJZI1/02zpri2GDxDKI9sZfHK75Np9vIymznbCDnkDi xuehtMkjbgZGZmq/k6mu8oFTLSbQFzgWiD+NaC1inWGg51yLtqMRjY+UuelAW9NM1U6I NUzvgNOZV9oOsYb84tAttgENfAtZjB1oa0X7fjItPgqZDxh2RG2pR+YkS88dqfl+fuy7 BjrrtwakBF2Tk/GfuAjVAQu4bfNzZ5BJRljM77jNk3Yg9j00H3RH3pGvvQcoxh6bXh7i utYA== X-Gm-Message-State: AOAM533rQIOYKsoieY2hkSu3aWWKDF0MGVXghDJv5vgr9x51j2c2blJY vk5u4e3hsZ6/2hgFAOemx21nOhSnQrkkyg== X-Google-Smtp-Source: ABdhPJymthpbfUCvs9zjrH30VQ5INzWS7pP+yUZaa6j68SM7SJJgggbcyLILYrzRneSO2l7g7/Xksg== X-Received: by 2002:a63:a102:: with SMTP id b2mr3260198pgf.459.1644322491109; Tue, 08 Feb 2022 04:14:51 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.14.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:14:50 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 02/13] ls-tree: add missing braces to "else" arms Date: Tue, 8 Feb 2022 20:14:27 +0800 Message-Id: <6fd1dd938303192d5b0a4cdc2914b2ff72cf5a3d.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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 Tue Feb 8 12:14:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738693 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 9CD1AC43217 for ; Tue, 8 Feb 2022 13:15:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238228AbiBHNOr (ORCPT ); Tue, 8 Feb 2022 08:14:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350223AbiBHMOy (ORCPT ); Tue, 8 Feb 2022 07:14:54 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35F11C03FEC0 for ; Tue, 8 Feb 2022 04:14:54 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id y15-20020a17090a474f00b001b88562650aso1496386pjg.0 for ; Tue, 08 Feb 2022 04:14:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ptcAFncxGBKtBgWsUxBBg90Humwutx+K7Mif6gkRRN8=; b=F3QTs3R/m4TSOtEwD2FBDSWNoOw3uW80SWvouC5BkOrllRud/ZwmmtsTPyOTaK2A6t RT/8q8MiUJ/II3iw5JswgjYJFN4y3B7xOhNq1XKgm1uFHVnWHvFdIrtq9jSImUxbktYU Xj1V1gViWYa3HZ697qv6pdtw9bQXt3zx4lZOkcF9Z27dADsqEvcNvQTJClD/Dds9kYV/ /PjQyNzhZjHedVQ3QdKsAAoQryQlTuqghe/AWgSF1zl0inJ4CZlh5tj8w3aBSaIOQQ07 QwWK0EcuUC+mB1eKdMTPZJRYhSl5fD3wxXpU3EfmLIok0twoVcjI63um2WgshnmcsCbq Jktw== 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=ptcAFncxGBKtBgWsUxBBg90Humwutx+K7Mif6gkRRN8=; b=rZaIDLsBKka38SO89+RY7bNZ6fOhfXkKQ6QvZSGfv2zKTUhocK15ekExGm9WZS2hCh iphMLicU6HzL7qS3KiD9ADRqNgbKo7LtHtczlI7EQp4WPSdiLADYNaarj4ErwiUozpdP 895mXRX5B7zMGYy0YCYQAvGJoJ7mQn/rhs+NbMqy4nib4F3lue7t4J8cHweUJUu1xxwo VI/zbD0w1x7vKkuCdy9BVmpBLaK1cL08zWPiXaYCefZmJiawHR49HCPHUDErDn9K2ykl dW48PJS6dspuhqEx1THlPgVqsqLULwFyMCiGqHfJc9rZmjZVp9X7gvOA8vtVEiy+uPRH PDPg== X-Gm-Message-State: AOAM533ij1WrzEQl1AgVbylz5QV//JINz1HOyMvRRN9Qoi5NIHRkQbyd kCP0Yo+P98NuJt6ukWo6uog= X-Google-Smtp-Source: ABdhPJzcHRAQ892lWN0UhzXD1eNeeBpWD2BGsevZZ0ykKvn9zYrRJ+3AEvHpTbHwiWPManHczXynBg== X-Received: by 2002:a17:902:7b89:: with SMTP id w9mr4270374pll.174.1644322493703; Tue, 08 Feb 2022 04:14:53 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.14.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:14:53 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 03/13] ls-tree: use "enum object_type", not {blob,tree,commit}_type Date: Tue, 8 Feb 2022 20:14:28 +0800 Message-Id: <208654b5e2aebbe988b5a8f478869253a0166e9b.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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 Tue Feb 8 12:14:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738694 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 282D5C43219 for ; Tue, 8 Feb 2022 13:15:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376894AbiBHNOr (ORCPT ); Tue, 8 Feb 2022 08:14:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350228AbiBHMO5 (ORCPT ); Tue, 8 Feb 2022 07:14:57 -0500 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B55C0C03FEC0 for ; Tue, 8 Feb 2022 04:14:56 -0800 (PST) Received: by mail-pf1-x42d.google.com with SMTP id b3so43643pfg.7 for ; Tue, 08 Feb 2022 04:14:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DsQ4/8Eeg48radubstj3W86Jg99jiR+oV9E1n8D4GNQ=; b=qPyw+p+swd+URgM+jIgRT/RDG8y99L9t5ZaOrwo5Q7NSX31DLag2FTi5Vpc7CXx0EO d6GJz93ZWeMOllUoASLAN2NzyskHlTr+JtHGgnYPKpay55Hz0do4HUy1plwqvYRzO1b0 4q1HRg3RYz0MtxvvGxnU24Vfkp3VEb3FFpPtAxmQNICI8dKYoMTth6X3hZm/CQyT3fgw 2VTgRBPpNQf+1mODbaMDOTbP2EdZniemteyLffTzekV8tCSNAf9lphIjOcSRyL+RQcIc ML7NYShaTJgPOtojGL0CVxC5oQY9WN/L9nPgnTn3HVw//Dh59prlh8pcyd+XaEx/SB3Q kelA== 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=DsQ4/8Eeg48radubstj3W86Jg99jiR+oV9E1n8D4GNQ=; b=HY09Uz4iAp5XOF1cKHJRxqvUkrJXYaLXBnOvpHQLveQDeAE/oMQyYOYMnpdhdPvTWE vMePWzVfUrry+iM0mlTXletmsXrlkHpxUX21JV4b0DuE+kDYTzJ2nqTq/bpi6+q3CmXZ OFvMJmw2MyuY+gB3LXrKjD5ZEONm7c8CIEp39csHU3rvMFvpWPUjmcXu+5hOsp5MM7RZ 020bFqHCv+5f2LEUBldkXNidOiUgAOM4dRfpqQ8o3rc46/bbRgdU1BudpkWIOcKeqxyQ IYbmxT7VFrfFcIEaoAT56RQ5uCgW9ZLLePd1t0juEC6XZmXqRWwZooaan9FvkAahOTlW fcfw== X-Gm-Message-State: AOAM532uCQOdU3TQx28VHhf9MFRH65YHj22cekLo71A4DvKm7eMRQn5L pkur1AkHKBC3+WfwAUkclU8= X-Google-Smtp-Source: ABdhPJyvYEaiFvMa30DsJkDeRyCvrpgPSfriaU6oJJf5Mu7lThAlj2xWNI1UgI2KAqsyRAHH76aVAA== X-Received: by 2002:a05:6a00:8c5:: with SMTP id s5mr4140612pfu.18.1644322496229; Tue, 08 Feb 2022 04:14:56 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.14.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:14:55 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 04/13] ls-tree: use "size_t", not "int" for "struct strbuf"'s "len" Date: Tue, 8 Feb 2022 20:14:29 +0800 Message-Id: <2637464fd8f6b8bae6c681f20c10c9fc76dc6d6c.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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 Tue Feb 8 12:14:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738699 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 3701BC4167D for ; Tue, 8 Feb 2022 13:15:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376916AbiBHNOs (ORCPT ); Tue, 8 Feb 2022 08:14:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350242AbiBHMO7 (ORCPT ); Tue, 8 Feb 2022 07:14:59 -0500 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 2EE50C03FEC0 for ; Tue, 8 Feb 2022 04:14:59 -0800 (PST) Received: by mail-pj1-x102a.google.com with SMTP id om7so2864222pjb.5 for ; Tue, 08 Feb 2022 04:14:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hKtDBI+EUQhE6MFFnvjFiseJ/0q3yxKWPgE7iZBevkM=; b=jvaRI64tbINqK3oRjzxs2lq323sRsl2+QvVUU+/0heuY44fZiukkBLPOZT18+rmj+s pM3/0wEt9oO89Zd/7zZ4DqSE1wMbj+zdxkFbtGTQwNBvsLkD3h9SrskrZ0z6bVV0Nf4V RBhr+mJnTxiG0CSA5raH3ZcV2YrDRVKmoo9HaP9RjVD0xDhTvM2JM8sEcetYPX6KHvBa YNLqbr/IEHxAhIxMAVfhF9gYTOHhbKZSAIRdwy6F0ZpGrRwrnge88e7SNZsP4tt0zjnG DYscCjNuSAFO+6icd1rM4XdjZsfba8WdUAzy6A122U5qkhcGF02/4WTnw4lWgoQgr79I f8pw== 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=hKtDBI+EUQhE6MFFnvjFiseJ/0q3yxKWPgE7iZBevkM=; b=3LPS02dhRlGdq/elvl3mHGOHqU98Jv0qPCKqFCnLHyRWpATt0o4kKRzxHx9XfIaFCc seIEvMzoOxQqhS33HcT1lri7Xwvdez5AHS59IQcm2yi4N1w94s8pgNFFaN/J2oLiGSNG dWRrIj624jv4o6wNvLdTEB3Xl7nPCbZiPb3Jjr2YAsIlXA3PnJWYHDxUNdcLxPYdOtJh Mvvr86S4kJTo4cTmzmlOQ9LBry7YaRipo0wzfw/OYGfWFBy0cT9BIY3RabPUlPO20ts0 tFmhbwSlaV+CJHdKZR7o+2rq4T6TOdWex701ZP4Ohft/yLrcxmwDKZUJya333itnGr3s qUTg== X-Gm-Message-State: AOAM531Yx8cdhgMKc8z2xRzKaag1O11R3obzLJSx39RjlpJSAuqGvYnN qk1yI6qJsBhC/qD3YNhyQaE= X-Google-Smtp-Source: ABdhPJyn6kywfLpD++4Mro+bkz4yBWDbJzJ3KLfb/0zKX2ZeoOBx75VkWgCWqPqQnGucT/TPf5Vnow== X-Received: by 2002:a17:90a:2e0a:: with SMTP id q10mr1062568pjd.130.1644322498709; Tue, 08 Feb 2022 04:14:58 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.14.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:14:58 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 05/13] ls-tree: rename "retval" to "recurse" in "show_tree()" Date: Tue, 8 Feb 2022 20:14:30 +0800 Message-Id: <99e6d47108308b0bbaf406b439170de04e375c56.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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 Tue Feb 8 12:14:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738698 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 9BCEFC4167B for ; Tue, 8 Feb 2022 13:15:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350485AbiBHNOt (ORCPT ); Tue, 8 Feb 2022 08:14:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350265AbiBHMPC (ORCPT ); Tue, 8 Feb 2022 07:15:02 -0500 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE3BFC03FEC0 for ; Tue, 8 Feb 2022 04:15:01 -0800 (PST) Received: by mail-pj1-x1030.google.com with SMTP id h14-20020a17090a130e00b001b88991a305so1883363pja.3 for ; Tue, 08 Feb 2022 04:15:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6vFjCb4DmwVr6H1e3h9iSurKdr0K1TaRcLgkxFEYDV8=; b=BzCxCzUYYa2qVYmsMxNUBT6Sbm3t+MT4AIrTIY0Sal3+28E+0yTL3dgzsIf3XDC94D FEBW7s6n/uY9Rvl2i6QQ4hdW42ULy71Uh1JOK0o1VVuxpy38ME6VbtKLFNFv8sUU7SZR sHC46ZgrGNOU1Zn5BRvY1UbOoP0jLl/whWp52zgPTBeGkca9KotvS9mcigYFyuVhvQRx 5r5I37RQsxitnamnUAeikAUWHEVMOP8cNghHiDTE9L5Tv/8vDB7SyoxX10znXUbka1O2 QQHcOqkjxNhZX5UtiTjoT2pzyj7sFryYeMiGeAJ066h2DEFmQ3NO1KSPwEhouyxz1+xn XdHA== 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=6vFjCb4DmwVr6H1e3h9iSurKdr0K1TaRcLgkxFEYDV8=; b=Tl4JNij5vZY5KaF36Gc/Cmi4CmXjzKavC04B8SDhTgAtbyfXTnvv6YfSy4nv2iZ8kw r3gnA4lPq+fzvpaurLnv9kcVssTKWs5uf8PQl48zCZV0JJc0s8ayGba3vH4oMp64K0wl cBDe/8QMidKPo55asc2fQh9m7PotonVNcufXHnC+pC+m0/fB43foJsrAGQ2u6BJS3z89 l/Fty4YkFuVMw1n8J2Mnm5O8a+8q1BqoEfV8z5tqJZryDmmBmpoD4WLqFgI6yE6qnrWA TyGDXIx0+LOvzYjLSvTLH8WBKxjsgGgw+gjxnjgHhU0Yr+YGmalCxv8fJdFlmz6vEK0y y2Jg== X-Gm-Message-State: AOAM5323TD51M6i5khzixmP5f29URJsrDpym9ADXFjaUNsNYd2FwQn4r m2UamInwGR9zL+mBZsmaiic= X-Google-Smtp-Source: ABdhPJybNJa/XTeGgTQpCOzbvwGn0SYRF1/AbvTpiBGl9HHKj6uNqmOcZQC/jJFsiiN3mAB0510a1w== X-Received: by 2002:a17:90a:5206:: with SMTP id v6mr1042146pjh.220.1644322501412; Tue, 08 Feb 2022 04:15:01 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.14.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:15:01 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Teng Long Subject: [PATCH v11 06/13] ls-tree: simplify nesting if/else logic in "show_tree()" Date: Tue, 8 Feb 2022 20:14:31 +0800 Message-Id: <3816a65fe62b1039c396eb38d27bf2c22e8d84de.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.dirty In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This commit use "object_type()" to get the type, then remove some of the nested if to let the codes here become more cleaner. Signed-off-by: Teng Long --- builtin/ls-tree.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index ef8c414f61..9c57a36c8c 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -66,19 +66,13 @@ 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; - } - else if (ls_options & LS_TREE_ONLY) + 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; if (!(ls_options & LS_NAME_ONLY)) { From patchwork Tue Feb 8 12:14:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738700 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 ACEA7C41535 for ; Tue, 8 Feb 2022 13:15:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350382AbiBHNOu (ORCPT ); Tue, 8 Feb 2022 08:14:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350274AbiBHMPF (ORCPT ); Tue, 8 Feb 2022 07:15:05 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71F5DC03FEC0 for ; Tue, 8 Feb 2022 04:15:04 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id h14-20020a17090a130e00b001b88991a305so1883446pja.3 for ; Tue, 08 Feb 2022 04:15:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=l0j9MvNlEHYd7VY03t/yyjO2xfuAHL72IRIFUia1XG0=; b=aH6vVi6bhw0+NwaOVJDCgiNWJikervLORjq/+p28TZsrrXEyqT6VV6T8gGdz5iz3gL JXsiUkpQmRPZAJPD74R4vBneV2dR/U/1D3sJ0uHMNNNc0D0zy6qiHINzzIPOG2Az6qqa YwM07DsM5CovHQN5+1SA5s35vxQhZtHcc/pIo57/JSgHXvj0jXHIwbZ7TWcH4HpSSyy4 EcFHrbq9FsDvyTXB8T70FPFBAgFysKK+uqaLHA5YzxggtX4lJjsNzawiHPxhOgx6BxBT S7OMI2J7jEVbJy4wWFVPcvEaDgMOqd2lNetpiep2GAVaTKCTqJQou832/sZ+WRhAlya2 HsXA== 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=l0j9MvNlEHYd7VY03t/yyjO2xfuAHL72IRIFUia1XG0=; b=DzIj6P4R4xF4FHNrZzM22Kx3gm2Qz8yiEAKoi3xsNp4I6WyiGU6Wr/YZc6YrQ8lyRY kpSzDF/AFYIyyl985RGb1oxx/5ob5wOcl0GW6fDPEOAvxtOviZNOOMycwUc2XAvQCPm3 TE0MgBuVgYsKKZD+tb5rTLOgKENWeALIt+vIHVvqHVjnZEYcs38LUKe5t2l4nLVLEtte J2PugjuuSE+3qmLlXzMPHe7jSflIC8bD2Ibd86Q4pXVVaf10PBKdHtnaVTPKpW952cHB +gfKBiOeIcUpgMnXVJBpecL6JOGmdngTmFSS+rosFdwAbwR4uTMnprLZdEaiVZUpsCf6 J82A== X-Gm-Message-State: AOAM530MDI097lPTqrbjUP1vtXqkxVuWrrdaocv4Kt7YLcJotZJKvnSF 44CEDuJ+fsvhP6PNA/ovSsY= X-Google-Smtp-Source: ABdhPJzyYP5RrXYdXD7KpJIF57NiQRsbhNnjeyOrhXz54fdwxIuQrZ4fvOKhc2ROjSt1galc8pWh0w== X-Received: by 2002:a17:90b:1d88:: with SMTP id pf8mr1032629pjb.162.1644322503968; Tue, 08 Feb 2022 04:15:03 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.15.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:15:03 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 07/13] ls-tree: fix "--name-only" and "--long" combined use bug Date: Tue, 8 Feb 2022 20:14:32 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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/ Signed-off-by: Teng Long --- 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 9c57a36c8c..32147e75e6 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -120,12 +120,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, From patchwork Tue Feb 8 12:14:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738701 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 DCAE5C4167E for ; Tue, 8 Feb 2022 13:15:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376968AbiBHNOw (ORCPT ); Tue, 8 Feb 2022 08:14:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350290AbiBHMPI (ORCPT ); Tue, 8 Feb 2022 07:15:08 -0500 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3107C03FEC0 for ; Tue, 8 Feb 2022 04:15:06 -0800 (PST) Received: by mail-pl1-x62f.google.com with SMTP id j4so1391336plj.8 for ; Tue, 08 Feb 2022 04:15:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IC6fX02N8mHHRgpZPEjR8TynJIG+AZT0/xAKQJgQdXg=; b=RbVuP/8j2xWy9P0/xR/5EE1VArVO3wupfoh2uXe1mGNxF67/W8wuHQTj12/zdDzJ/o fcCfxIyez5fcEATkhKlm25hs1v7zhYuj7P6Gqvd3WBhAMwWBAPvvTc9T7rVKcCm5H8r/ Dq1u+102kg01ZetNFd/NysrAi1/E2zH5dxbUXBtZn7Nw6lo/7nl0vnexoV1ameFuzjHg UUAlagCPEeSM1LcttjJeYCVA0ciXmJPKavmoC4jnIJzmKv1COYXFccjFD4GAD3+QXNyQ 8fz2Wg717T3xVwedoDw/dAY9QKDOeP3+qoRAALVeiD1eFMvimMmsjb7XaqlVFauIAtN9 jqtQ== 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=IC6fX02N8mHHRgpZPEjR8TynJIG+AZT0/xAKQJgQdXg=; b=Ml9kguebRJpAHkQHE6bGymr/q0g6A902AhkSnYDgq9ktnNNPpxNf94Gp4ytQ4+zH4L sBxH1d01ysskV4DWgHMb7KDBF1pwPZl6asdpepuP3i9lofjJXPeKM90CFrj4m5+RUX+J 0BqnITzqlh7n8/Xl53bwiAg+8IhG9TnwhYSFzPt+7enWgzmsZJDXhKLF5csge8OamYdD TKkBZoCWYKFP1l5hKy/34mVvNYbFu/ViS9tqCyV+ZiH9JIJS1FaqumnYC9klaOdoZ/0Q D66he9zx2YsStmWYVXh3w0OAFfdiiWV8wldZj6ve/lRke/Liobxrk9isJCWGauPMThnB AXXQ== X-Gm-Message-State: AOAM533F8lG2JsnPgYgpLtCuH2Pd0WnQZeq4zwpdYEdMSNop1MRIAsxg 8RCqbQ2m25FPIsgSRfvaXSQ= X-Google-Smtp-Source: ABdhPJypQsONJMLWLngX/wVdTpNwfYdNpAAgsxVdEekdDhR5ENxA8XHVfg55uvrnDXUMWm13oijqUw== X-Received: by 2002:a17:902:7c84:: with SMTP id y4mr3948291pll.149.1644322506430; Tue, 08 Feb 2022 04:15:06 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.15.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:15:06 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 08/13] ls-tree: slightly refactor `show_tree()` Date: Tue, 8 Feb 2022 20:14:33 +0800 Message-Id: <41e8ed50476a5afd6009db1a69c7a39b04e038b0.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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 use a new int "shown_fields" to mark which columns to output, and `parse_shown_fields()` to calculate the value of "shown_fields". This has the advantage of making the show_tree logic simpler and more readable, as well as making it easier to extend new options (for example, if we want to add a "--object-only" option, we just need to add a similar "if (shown_fields == FIELD_OBJECT_NAME)" short-circuit logic in "show_tree()"). Signed-off-by: Teng Long --- builtin/ls-tree.c | 126 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 89 insertions(+), 37 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 32147e75e6..8baab7c83e 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -16,21 +16,53 @@ static int line_termination = '\n'; #define LS_RECURSIVE 1 -#define LS_TREE_ONLY 2 -#define LS_SHOW_TREES 4 -#define LS_NAME_ONLY 8 -#define LS_SHOW_SIZE 16 +#define LS_TREE_ONLY (1 << 1) +#define LS_SHOW_TREES (1 << 2) +#define LS_NAME_ONLY (1 << 3) +#define LS_SHOW_SIZE (1 << 4) static int abbrev; static int ls_options; static struct pathspec pathspec; static int chomp_prefix; static const char *ls_tree_prefix; +static unsigned int shown_fields; +#define FIELD_PATH_NAME 1 +#define FIELD_SIZE (1 << 1) +#define FIELD_OBJECT_NAME (1 << 2) +#define FIELD_TYPE (1 << 3) +#define FIELD_MODE (1 << 4) +#define FIELD_DEFAULT 29 /* 11101 size is not shown to output by default */ +#define FIELD_LONG_DEFAULT (FIELD_DEFAULT | FIELD_SIZE) static const char * const ls_tree_usage[] = { N_("git ls-tree [] [...]"), NULL }; +enum { + MODE_UNSPECIFIED = 0, + MODE_NAME_ONLY, + MODE_LONG, +}; + +static int cmdmode = MODE_UNSPECIFIED; + +static int parse_shown_fields(void) +{ + if (cmdmode == MODE_NAME_ONLY) { + shown_fields = FIELD_PATH_NAME; + return 0; + } + + if (!ls_options || (ls_options & LS_RECURSIVE) + || (ls_options & LS_SHOW_TREES) + || (ls_options & LS_TREE_ONLY)) + shown_fields = FIELD_DEFAULT; + if (cmdmode == MODE_LONG) + shown_fields = FIELD_LONG_DEFAULT; + return 1; +} + static int show_recursive(const char *base, size_t baselen, const char *pathname) { int i; @@ -61,6 +93,39 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname return 0; } +static int show_default(const struct object_id *oid, enum object_type type, + const char *pathname, unsigned mode, + struct strbuf *base) +{ + size_t baselen = base->len; + + if (shown_fields & FIELD_SIZE) { + char size_text[24]; + if (type == OBJ_BLOB) { + unsigned long size; + if (oid_object_info(the_repository, oid, &size) == OBJ_BAD) + xsnprintf(size_text, sizeof(size_text), "BAD"); + else + xsnprintf(size_text, sizeof(size_text), + "%" PRIuMAX, (uintmax_t)size); + } else { + xsnprintf(size_text, sizeof(size_text), "-"); + } + printf("%06o %s %s %7s\t", mode, type_name(type), + find_unique_abbrev(oid, abbrev), size_text); + } else { + printf("%06o %s %s\t", mode, type_name(type), + find_unique_abbrev(oid, abbrev)); + } + baselen = base->len; + strbuf_addstr(base, pathname); + write_name_quoted_relative(base->buf, + chomp_prefix ? ls_tree_prefix : NULL, stdout, + line_termination); + strbuf_setlen(base, baselen); + return 1; +} + static int show_tree(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, void *context) { @@ -75,34 +140,19 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) return 0; - if (!(ls_options & LS_NAME_ONLY)) { - if (ls_options & LS_SHOW_SIZE) { - char size_text[24]; - if (type == OBJ_BLOB) { - unsigned long size; - if (oid_object_info(the_repository, oid, &size) == OBJ_BAD) - xsnprintf(size_text, sizeof(size_text), - "BAD"); - else - xsnprintf(size_text, sizeof(size_text), - "%"PRIuMAX, (uintmax_t)size); - } else { - xsnprintf(size_text, sizeof(size_text), "-"); - } - printf("%06o %s %s %7s\t", mode, type_name(type), - find_unique_abbrev(oid, abbrev), - size_text); - } else { - printf("%06o %s %s\t", mode, type_name(type), - find_unique_abbrev(oid, abbrev)); - } + if (shown_fields == FIELD_PATH_NAME) { + baselen = base->len; + strbuf_addstr(base, pathname); + write_name_quoted_relative(base->buf, + chomp_prefix ? ls_tree_prefix : NULL, + stdout, line_termination); + strbuf_setlen(base, baselen); + return recurse; } - baselen = base->len; - strbuf_addstr(base, pathname); - write_name_quoted_relative(base->buf, - chomp_prefix ? ls_tree_prefix : NULL, - stdout, line_termination); - strbuf_setlen(base, baselen); + + if (shown_fields >= FIELD_DEFAULT) + show_default(oid, type, pathname, mode, base); + return recurse; } @@ -120,12 +170,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, @@ -156,6 +206,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) if (get_oid(argv[0], &oid)) die("Not a valid object name %s", argv[0]); + parse_shown_fields(); + /* * show_recursive() rolls its own matching code and is * generally ignorant of 'struct pathspec'. The magic mask From patchwork Tue Feb 8 12:14:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738703 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 07B69C46467 for ; Tue, 8 Feb 2022 13:15:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350549AbiBHNOx (ORCPT ); Tue, 8 Feb 2022 08:14:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350292AbiBHMPJ (ORCPT ); Tue, 8 Feb 2022 07:15:09 -0500 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D1C6C03FEC0 for ; Tue, 8 Feb 2022 04:15:09 -0800 (PST) Received: by mail-pf1-x42c.google.com with SMTP id x65so4023213pfx.12 for ; Tue, 08 Feb 2022 04:15:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qoJ3meIlcealGFNuBKdJlsA7z7DtO3lQHLko6cPEYgw=; b=aaEtO/WQTCbLar4PTACn6dtI0SqPr0qosuiiGarSHbfNUIK1s9mp3pQgzEYGzyXxK3 7QLEq+jTErwM1bm6HTsE+bgHQcvTrm+25Y7VGPqVVKFPPhPpQM2n0u7nM4G5ELl0Gidv mPzlYkKXbkOEiyZOcbI7K6ybPLtbfdl/3zHz6ODqF3c3wn3fUDYiaC7dR9NK2Fop8Zw8 FEoO8uQYT35nuvF7pVrD/cNMAy8T9VbsARTsPmkYUGF5BD6gQPkFtenLnbZ5Ey/cQ2QU 5zGdRjjabkjBz7EUAHw4sIjyHjGujCVGrdBilcFAjaF076CKx02+fBtI9dzJghTRChkI T8Mw== 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=qoJ3meIlcealGFNuBKdJlsA7z7DtO3lQHLko6cPEYgw=; b=P1s8EcprIYU5zpL5zOxek0L+QQvSMIDKYEpobhkeKfT5+E/nyR5T2Cb0CdalKGLdPq qCSDjgHT91C9KgAE+7mDWhyW0XtfQ1mjSYONbgwGbjGc+9+rG8MltWdSdEhPUFS2uxzL Dyu0PQpV9q0ECZJ72y6sB4xxLfxmnTwarsTOcT+mn+Acg+dc9zyDr96sau/N2QSCDDwz gBpwToXoJJlpDHN78rfCeVln/Ghsyxw/f51DOYt8vsnXFXAvUU/2CczaDZ/atcGd7c7L 1NcqS+7en+WeR/lY5T2ctmLKVAv1ccZS0X3hpE8xEptTqQ00Dm9/4uEg/wi9G1KLeVYx DIwA== X-Gm-Message-State: AOAM531oOQaBegPYB0cvaDg1yISaoPTz8/CN1erdlWU+eTbMuzUODeSx 9eTgAb89BfXrTRaKzPu/CnE= X-Google-Smtp-Source: ABdhPJyHpGqTNPlJuEEqt76vac5Rr8KaqiWW4Z5MfJMc4HpMT046f9E3KhMcGibwHA/lKRu6V0ddhw== X-Received: by 2002:a63:69c1:: with SMTP id e184mr3237479pgc.495.1644322508921; Tue, 08 Feb 2022 04:15:08 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.15.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:15:08 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 09/13] ls-tree: introduce struct "show_tree_data" Date: Tue, 8 Feb 2022 20:14:34 +0800 Message-Id: <46e10a5392ace1b1caef1eb8f7dfc4b60779ff55.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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 | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 8baab7c83e..293b8f9dfb 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -34,6 +34,14 @@ static unsigned int shown_fields; #define FIELD_DEFAULT 29 /* 11101 size is not shown to output by default */ #define FIELD_LONG_DEFAULT (FIELD_DEFAULT | FIELD_SIZE) +struct show_tree_data { + unsigned mode; + enum object_type type; + const struct object_id *oid; + const char *pathname; + struct strbuf *base; +}; + static const char * const ls_tree_usage[] = { N_("git ls-tree [] [...]"), NULL @@ -93,17 +101,15 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname return 0; } -static int show_default(const struct object_id *oid, enum object_type type, - const char *pathname, unsigned mode, - struct strbuf *base) +static int show_default(struct show_tree_data *data) { - size_t baselen = base->len; + size_t baselen = data->base->len; if (shown_fields & FIELD_SIZE) { 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), @@ -111,18 +117,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; } @@ -132,6 +138,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_TREE && show_recursive(base->buf, base->len, pathname)) recurse = READ_TREE_RECURSIVE; @@ -151,7 +164,7 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, } if (shown_fields >= FIELD_DEFAULT) - show_default(oid, type, pathname, mode, base); + show_default(&data); return recurse; } From patchwork Tue Feb 8 12:14:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738702 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 18B04C3525C for ; Tue, 8 Feb 2022 13:15:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377014AbiBHNOx (ORCPT ); Tue, 8 Feb 2022 08:14:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350296AbiBHMPM (ORCPT ); Tue, 8 Feb 2022 07:15:12 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43264C03FEC0 for ; Tue, 8 Feb 2022 04:15:12 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id h14-20020a17090a130e00b001b88991a305so1883686pja.3 for ; Tue, 08 Feb 2022 04:15:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=k61gPCyYa++t62hfIn5SzAt1sRF/aZFfvQC8DVdwMaY=; b=JDR2JUGoYm2gk4M2hoarMeAVJEYp9XiulIUOZTBG4++sM+RAoq5XlKTnpwQfiR+q8y zQjDclP4FyWO+mjQh6oA8ZX9iSHJRoedbtN5s3JqiK3Ka7mgzYJhTb74fM261tMJkL9D GxdpaNQrlaBNkaBQBCr5rCgFclCgOvBS9H4DxJBzys2xLnaOyAJ3nNzL8tuzXhRc86r9 ZJnRF387tTyyGx0Q1x+Wk5vB4k7X5Cmc6gUBV1w/Qdv5D9+Io0C5eD2xi30QlYMaGqyj WdKnWuxKTKXl/P+vWOtoo92e+WjtD2CHnq4Xr5MQCoz7vNhPhLskgZRIxs5/30ofFkgw ApIg== 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=k61gPCyYa++t62hfIn5SzAt1sRF/aZFfvQC8DVdwMaY=; b=IrIJj+MdPDC1s/23XJR0ZTsqDllQ7Zai1JKMtv4hOkSfs01qJMrh9j7D+jDYyLXCeq 8aQkI22Qmy7dupCyCxM5RnnvKjXCIi7ZEhwgPJA8I1EPS6/lYn7s5qAYDVIQu7EHajnb B/scgnYZwn0hRmDsfiPUQPt22DL6UmJuWwRx4Fq34Fs9zY+99HGLHDxRMI9wP0oG0MUL F6AQ407yGYBR9C5mXiGJ5XYxmBRgzsgurpv3hMfD8MHdg0vTGMbEd2hNn1R3Jo24Y9sk ISSHo8og7Tg7vlh0K/BN8uHUJLj8Qlu1FqRtnGVE2TF+znfDHGMg6AOFnA0NGcAeNB4/ pN/A== X-Gm-Message-State: AOAM533BYEGsyXmeX3CXlmtL/S8rDm03Ccbtndui0gcgIJT+hzgR1MWL 5vIhMbZ7V3xztHJVkRsZ0hk= X-Google-Smtp-Source: ABdhPJzzg0KsF9pshmB8UKhgovK37RLMMPSiMGwHe2VyrqIngFu/F5gPvic9AjYjdtMMkFfRCHpgsA== X-Received: by 2002:a17:902:cacb:: with SMTP id y11mr4333032pld.96.1644322511665; Tue, 08 Feb 2022 04:15:11 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.15.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:15:11 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes Schindelin Subject: [PATCH v11 10/13] cocci: allow padding with `strbuf_addf()` Date: Tue, 8 Feb 2022 20:14:35 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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 Tue Feb 8 12:14:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738704 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 29E61C4707A for ; Tue, 8 Feb 2022 13:15:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350578AbiBHNOy (ORCPT ); Tue, 8 Feb 2022 08:14:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350309AbiBHMPQ (ORCPT ); Tue, 8 Feb 2022 07:15:16 -0500 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 CDC9BC03FEC0 for ; Tue, 8 Feb 2022 04:15:14 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id b3so45003pfg.7 for ; Tue, 08 Feb 2022 04:15:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=H5zHk9dk8E6C5fSYiDHkj/hWgwjz7j0RBjHbuYJuqy0=; b=f2nwIbeaGzwvAA1u61WZSF6BzwXqAj3DnF/DuCBRVJoD+JjAMmkDYlqiMQ2WP6wcFE wotO6fY9PLoJbjowQi1a/UlFUn0kbwzDFfIx8AEXUm1V6hJ/ibUOke9uZzwnP7wuUqo1 GIYMz/8G8NS5/Dx2bJLWbmcNzJTqqeheGpqxkXvLo5ytokzQsDCO0BxD4iPw7Enn8GrM u33UuEnkRxd+xP0K4DZxS5lDIWRrml3tEpwLWNeIbyhhJ2LApgVuuKqwtlRKhXIwZS7B Xs8xlVgifBTWXG3HnsgRDSR5kutlygD7GF6W43e+Pz5kSkV9TtnrKn9WxyU4JKxW6Fnr fCUQ== 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=H5zHk9dk8E6C5fSYiDHkj/hWgwjz7j0RBjHbuYJuqy0=; b=GGgesVUkyzPjuEQSwfOjbnp1CIm+jUoZVqmTSSHwYDBXXK2Jc7wwyn3NET/oWPpebB 1UACvSlbQYehx1xlmxnrgTiMYyzCYnDVdQAyR3iLN/JFAH36DBwTiDE6/3lsb0yhHUtq S+5zchTvWr3Fn22Z3jCfr5Bi770HdHv8O3sQxU0/qsLwQ1Rww6DrFszo3IqLkmcNsQhP l7Ps1cz7ILyShw/5CCjs9HZzwLSleQGAHxdwoCS9JmnD7Tf9OQju6tT4kndrDhmkjG1C KFAAomjFKgMtfLf3ztUilah6pCPspMUnFmW0yJE2m2wNVtaeeHMbQwEkfLMcQS+rlbnv i90A== X-Gm-Message-State: AOAM531Jnm04Z8i+4RUuUVUfApcDzGsYjLAIcxAfIZiSpTqALhkbl68s ZGfc4Lj126ntZVysIZMajjPOXP2PY70gfg== X-Google-Smtp-Source: ABdhPJzpSlkC7YwikhflyIkRuCLTNNv1lKFlhAIrvYyjwIdt6B7GGp1/nItomqb6++1HQONeJ9i3NA== X-Received: by 2002:a05:6a00:1342:: with SMTP id k2mr4178716pfu.20.1644322514265; Tue, 08 Feb 2022 04:15:14 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.15.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:15:13 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 11/13] ls-tree.c: introduce "--format" option Date: Tue, 8 Feb 2022 20:14:36 +0800 Message-Id: <5936004f1386388740ded0208ebeb8c5d0c88ef2.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.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]. Here is the statistics about performance tests: 1. Default format (hitten the builtin formats): "git ls-tree " vs "--format='%(mode) %(type) %(object)%x09%(file)'" $hyperfine --warmup=10 "/opt/git/master/bin/git ls-tree -r HEAD" Benchmark 1: /opt/git/master/bin/git ls-tree -r HEAD Time (mean ± σ): 105.2 ms ± 3.3 ms [User: 84.3 ms, System: 20.8 ms] Range (min … max): 99.2 ms … 113.2 ms 28 runs $hyperfine --warmup=10 "/opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='%(mode) %(type) %(object)%x09%(file)' HEAD" Benchmark 1: /opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='%(mode) %(type) %(object)%x09%(file)' HEAD Time (mean ± σ): 106.4 ms ± 2.7 ms [User: 86.1 ms, System: 20.2 ms] Range (min … max): 100.2 ms … 110.5 ms 29 runs 2. Default format includes object size (hitten the builtin formats): "git ls-tree -l " vs "--format='%(mode) %(type) %(object) %(size:padded)%x09%(file)'" $hyperfine --warmup=10 "/opt/git/master/bin/git ls-tree -r -l HEAD" Benchmark 1: /opt/git/master/bin/git ls-tree -r -l HEAD Time (mean ± σ): 335.1 ms ± 6.5 ms [User: 304.6 ms, System: 30.4 ms] Range (min … max): 327.5 ms … 348.4 ms 10 runs $hyperfine --warmup=10 "/opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='%(mode) %(type) %(object) %(size:padded)%x09%(file)' HEAD" Benchmark 1: /opt/git/ls-tree-oid-only/bin/git ls-tree -r --format='%(mode) %(type) %(object) %(size:padded)%x09%(file)' HEAD Time (mean ± σ): 337.2 ms ± 8.2 ms [User: 309.2 ms, System: 27.9 ms] Range (min … max): 328.8 ms … 349.4 ms 10 runs Links: [1] https://public-inbox.org/git/RFC-patch-6.7-eac299f06ff-20211217T131635Z-avarab@gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Teng Long --- Documentation/git-ls-tree.txt | 59 ++++++++++++++-- builtin/ls-tree.c | 129 +++++++++++++++++++++++++++++++++- t/t3104-ls-tree-format.sh | 81 +++++++++++++++++++++ 3 files changed, 262 insertions(+), 7 deletions(-) create mode 100755 t/t3104-ls-tree-format.sh diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt index db02d6d79a..db29a9efb5 100644 --- a/Documentation/git-ls-tree.txt +++ b/Documentation/git-ls-tree.txt @@ -10,7 +10,7 @@ SYNOPSIS -------- [verse] 'git ls-tree' [-d] [-r] [-t] [-l] [-z] - [--name-only] [--name-status] [--full-name] [--full-tree] [--abbrev[=]] + [--name-only] [--name-status] [--full-name] [--full-tree] [--abbrev[=]] [--format=] [...] DESCRIPTION @@ -74,6 +74,16 @@ OPTIONS Do not limit the listing to the current working directory. Implies --full-name. +--format=:: + A string that interpolates `%(fieldname)` from the result + being shown. It also interpolates `%%` to `%`, and + `%xx` where `xx` are hex digits interpolates to character + with hex code `xx`; for example `%00` interpolates to + `\0` (NUL), `%09` to `\t` (TAB) and `%0a` to `\n` (LF). + When specified, `--format` cannot be combined with other + format-altering options, including `--long`, `--name-only` + and `--object-only`. + [...]:: When paths are given, show them (note that this isn't really raw pathnames, but rather a list of patterns to match). Otherwise @@ -82,16 +92,29 @@ OPTIONS Output Format ------------- - SP SP TAB + +The output format of `ls-tree` is determined by either the `--format` +option, or other format-altering options such as `--name-only` etc. +(see `--format` above). + +The use of certain `--format` directives is equivalent to using those +options, but invoking the full formatting machinery can be slower than +using an appropriate formatting option. + +In cases where the `--format` would exactly map to an existing option +`ls-tree` will use the appropriate faster path. Thus the default format +is equivalent to: + + %(objectmode) %(objecttype) %(objectname)%x09%(path) This output format is compatible with what `--index-info --stdin` of 'git update-index' expects. When the `-l` option is used, format changes to - SP SP SP TAB + %(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path) -Object size identified by is given in bytes, and right-justified +Object size identified by is given in bytes, and right-justified with minimum width of 7 characters. Object size is given only for blobs (file) entries; for other entries `-` character is used in place of size. @@ -100,6 +123,34 @@ quoted as explained for the configuration variable `core.quotePath` (see linkgit:git-config[1]). Using `-z` the filename is output verbatim and the line is terminated by a NUL byte. +Customized format: + +It is possible to print in a custom format by using the `--format` option, +which is able to interpolate different fields using a `%(fieldname)` notation. +For example, if you only care about the "objectname" and "path" fields, you +can execute with a specific "--format" like + + git ls-tree --format='%(objectname) %(path)' + +FIELD NAMES +----------- + +Various values from structured fields can be used to interpolate +into the resulting output. For each outputing line, the following +names can be used: + +objectmode:: + The mode of the object. +objecttype:: + The type of the object (`blob` or `tree`). +objectname:: + The name of the object. +objectsize[:padded]:: + The size of the object ("-" if it's a tree). + It also supports a padded format of size with "%(size:padded)". +path:: + The pathname of the object. + GIT --- Part of the linkgit:git[1] suite diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 293b8f9dfb..1c71e5d543 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -33,7 +33,10 @@ static unsigned int shown_fields; #define FIELD_MODE (1 << 4) #define FIELD_DEFAULT 29 /* 11101 size is not shown to output by default */ #define FIELD_LONG_DEFAULT (FIELD_DEFAULT | FIELD_SIZE) - +static const char *format; +static const char *default_format = "%(objectmode) %(objecttype) %(objectname)%x09%(path)"; +static const char *long_format = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)"; +static const char *name_only_format = "%(path)"; struct show_tree_data { unsigned mode; enum object_type type; @@ -55,6 +58,72 @@ enum { static int cmdmode = MODE_UNSPECIFIED; +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: as '%s'"), start); + + end = strchr(start + 1, ')'); + if (!end) + die(_("bad ls-tree format: element '%s' does not end in ')'"), start); + + len = end - start + 1; + if (skip_prefix(start, "(objectmode)", &p)) { + strbuf_addf(sb, "%06o", data->mode); + } else if (skip_prefix(start, "(objecttype)", &p)) { + strbuf_addstr(sb, type_name(data->type)); + } else if (skip_prefix(start, "(objectsize:padded)", &p)) { + expand_objectsize(sb, data->oid, data->type, 1); + } else if (skip_prefix(start, "(objectsize)", &p)) { + expand_objectsize(sb, data->oid, data->type, 0); + } else if (skip_prefix(start, "(objectname)", &p)) { + strbuf_add_unique_abbrev(sb, data->oid, abbrev); + } else if (skip_prefix(start, "(path)", &p)) { + const char *name = data->base->buf; + const char *prefix = chomp_prefix ? ls_tree_prefix : NULL; + struct strbuf quoted = STRBUF_INIT; + struct strbuf sbuf = STRBUF_INIT; + strbuf_addstr(data->base, data->pathname); + name = relative_path(data->base->buf, prefix, &sbuf); + quote_c_style(name, "ed, NULL, 0); + strbuf_addbuf(sb, "ed); + strbuf_release(&sbuf); + strbuf_release("ed); + } else { + errlen = (unsigned long)len; + die(_("bad ls-tree format: %%%.*s"), errlen, start); + } + return len; +} + static int parse_shown_fields(void) { if (cmdmode == MODE_NAME_ONLY) { @@ -101,6 +170,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; @@ -174,6 +275,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; const struct option ls_tree_options[] = { OPT_BIT('d', NULL, &ls_options, N_("only show trees"), LS_TREE_ONLY), @@ -194,6 +296,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() }; @@ -214,6 +319,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)) @@ -237,6 +346,20 @@ 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 && (!strcmp(format, default_format))) { + fn = show_tree; + } else if (format && (!strcmp(format, long_format))) { + shown_fields = shown_fields | FIELD_SIZE; + fn = show_tree; + } else if (format && (!strcmp(format, name_only_format))) { + shown_fields = FIELD_PATH_NAME; + fn = show_tree; + } else if (format) + fn = show_tree_fmt; + + return !!read_tree(the_repository, tree, &pathspec, fn, NULL); } diff --git a/t/t3104-ls-tree-format.sh b/t/t3104-ls-tree-format.sh new file mode 100755 index 0000000000..e08c83dc47 --- /dev/null +++ b/t/t3104-ls-tree-format.sh @@ -0,0 +1,81 @@ +#!/bin/sh + +test_description='ls-tree --format' + +TEST_PASSES_SANITIZE_LEAK=true +. ./test-lib.sh + +test_expect_success 'ls-tree --format usage' ' + test_expect_code 129 git ls-tree --format=fmt -l HEAD && + test_expect_code 129 git ls-tree --format=fmt --name-only HEAD && + test_expect_code 129 git ls-tree --format=fmt --name-status HEAD +' + +test_expect_success 'setup' ' + mkdir dir && + test_commit dir/sub-file && + test_commit top-file +' + +test_ls_tree_format () { + format=$1 && + opts=$2 && + fmtopts=$3 && + shift 2 && + git ls-tree $opts -r HEAD >expect.raw && + sed "s/^/> /" >expect actual && + test_cmp expect actual +} + +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ + "" +' + +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)" \ + "--long" +' + +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(path)" \ + "--name-only" +' + +test_expect_success 'ls-tree combine --format= and -t' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ + "-t" \ + "-t" +' + +test_expect_success 'ls-tree combine --format= and --full-name' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ + "--full-name" \ + "--full-name" +' + +test_expect_success 'ls-tree combine --format= and --full-tree' ' + test_ls_tree_format \ + "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ + "--full-tree" \ + "--full-tree" +' + +test_expect_success 'ls-tree hit fast-path with --format=' ' + git ls-tree -r HEAD >expect && + git ls-tree --format="%(objectmode) %(objecttype) %(objectname)%x09%(path)" -r HEAD >actual && + test_cmp expect actual +' + +test_expect_success 'ls-tree hit fast-path with --format=' ' + git ls-tree -r --name-only HEAD >expect && + git ls-tree --format="%(path)" -r HEAD >actual && + test_cmp expect actual +' +test_done From patchwork Tue Feb 8 12:14:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738695 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 7A6EAC4332F for ; Tue, 8 Feb 2022 13:15:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350640AbiBHNOz (ORCPT ); Tue, 8 Feb 2022 08:14:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350317AbiBHMPR (ORCPT ); Tue, 8 Feb 2022 07:15:17 -0500 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CB23C03FEC0 for ; Tue, 8 Feb 2022 04:15:17 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id h14-20020a17090a130e00b001b88991a305so1883873pja.3 for ; Tue, 08 Feb 2022 04:15:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KWdTw3XgNxjSBuQwBII3Ji6fiHgnFV/CEDoNMYoeG+Y=; b=YHQA0MMA6e2nY5Dm70QWQbowwHm64D3F0/5RhKO7QM/9XQubk5pxz4Acnx3fyvYNzq UmfuGRI0wrfYyPnEBxyeXVOdRzRlJPpDXOJhZUaOesDdkLsgGgVQJOq/2gy0TeJpBUou t5rZMPoq/yBTO4s50ztRG7r1csTMM5VImTamBF5BjUr44KiuD5LR55ucPqDjukltKjOF aC6cCW1bANvMKtNoxyqRgSw+ECv3sYhh83Zjd33A57tGnCOuHX+/+E/Sk3yXktOVk8AA TN/jN1mTN5dNHvmFApJS4saS4xp6v5iX6VM5nGVMDEgkjVtXeUEkL3v6XtnrbVXO79Nb 6oGQ== 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=KWdTw3XgNxjSBuQwBII3Ji6fiHgnFV/CEDoNMYoeG+Y=; b=2LCnnVkfGV0VcyZeH2wEhoepcS2lVc32Xpr1WUmqVQJaYOVviVDcAUSXkiJY7mry9z m9LDfR2tvxhjdUoPbG8fXzInjOt/r/Jdd1bnvNjlLt/wjNSwOONvfw6gdup1VisSlPjU Tv8bevqLfJ/qGdgnisDUaBSp0aqwkb55uvBtX6AoThx23+xjxvQwQnoPtMcQi1exh9qi IZOg2F0YJJ0c83e5rhBIz8qhq4PFQIHNKTNGp3JDAKgMrMXNvlnJPnfyOGSh9AUMdqzG ZSmgSbJ3omZuqACPhm9Jnnj8ssz1RUWqh+T6dU5f3CwE8tadmu7SbV5Ae7wdw0LuxBRn jazg== X-Gm-Message-State: AOAM533zY9Bm9cYQtkz7DnWlh53Tl6/fl5dsVn5QhQ4wSBeedbhf3W36 cwkzpcOXGfpdiGXKU7WfTJA= X-Google-Smtp-Source: ABdhPJy6QgugzAqI2iUCyyNYstUCLy2UI2rmoc2gsYwYyuwdjcAmBB56MVQoVsSZqceqV3vHqf1jkQ== X-Received: by 2002:a17:902:f64e:: with SMTP id m14mr4223612plg.158.1644322516891; Tue, 08 Feb 2022 04:15:16 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.15.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:15:16 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 12/13] ls-tree: introduce function "fast_path()" Date: Tue, 8 Feb 2022 20:14:37 +0800 Message-Id: <6d264977491b3645292bba5f91479ab1623e8b80.1644319434.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.dirty In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The generic "show_tree_fmt()" is slower than "show_tree()", so we want to take the fast path if possible. when "--format=" is passed, "fast_path()" will determine whether to use "show_tree()" or insist on using "show_tree_fmt()" by a try of finding out if the built-int format is hit. This commit take out the related codes from "cmd_ls_tree()" and package them into a new funtion "fast_path()". Explain it a little bit further, whether fast_path is hit or not, the final correctness should not break. Abstracting a separate method helps improve the readability of "cmd_ls_tree()" and the cohesiveness and extensibility of fast path logic. Signed-off-by: Teng Long --- builtin/ls-tree.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 1c71e5d543..ba96bcf602 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -58,6 +58,19 @@ enum { static int cmdmode = MODE_UNSPECIFIED; +static int fast_path(void){ + if (!strcmp(format, default_format)) { + return 1; + } else if (!strcmp(format, long_format)) { + shown_fields = shown_fields | FIELD_SIZE; + return 1; + } else if (!strcmp(format, name_only_format)) { + shown_fields = FIELD_PATH_NAME; + return 1; + } + return 0; +} + static void expand_objectsize(struct strbuf *line, const struct object_id *oid, const enum object_type type, unsigned int padded) { @@ -350,15 +363,7 @@ 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 && (!strcmp(format, default_format))) { - fn = show_tree; - } else if (format && (!strcmp(format, long_format))) { - shown_fields = shown_fields | FIELD_SIZE; - fn = show_tree; - } else if (format && (!strcmp(format, name_only_format))) { - shown_fields = FIELD_PATH_NAME; - fn = show_tree; - } else if (format) + if (format && !fast_path()) fn = show_tree_fmt; return !!read_tree(the_repository, tree, &pathspec, fn, NULL); From patchwork Tue Feb 8 12:14:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12738696 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 8C416C433FE for ; Tue, 8 Feb 2022 13:15:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377036AbiBHNO4 (ORCPT ); Tue, 8 Feb 2022 08:14:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350320AbiBHMPU (ORCPT ); Tue, 8 Feb 2022 07:15:20 -0500 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E19D0C03FEC0 for ; Tue, 8 Feb 2022 04:15:19 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id p22-20020a17090adf9600b001b8783b2647so1878277pjv.5 for ; Tue, 08 Feb 2022 04:15:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=36p6/x068UVwb2pNta2ncpwZbX9rD54FEVAELqChjt8=; b=BN5hriAn1o0MnUnZLDWoTBiwY3wPTFLdEPulwN+JkbNy8K9Bb74wS9X7PCl/gqyTSk YWiXXN+5kGfvdrjNetTtVjE+m0ePpE+NA/Zah3/wt2uKzsbF4DPQyNyTwhsP2iBAvYzG iMmY3eYScb0iJX26RYmNlq2YtVqXTFBOjSyvkiHimFB8ACzmECmOQhWegvXkAuZPuYfx FM4GEa7mR6bS2aDhloGKBJ2S2ZWUHRAqbky6zIdprU61jEDO2GRruBd3YSeaId3lfmFu UPIITKOtZiIqEsyPsXnXV0Qv+Aqz3MkAD8eX+j/jVqvjFduJOXVUlCgzd3XaWnS5ZAxy WnyA== 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=36p6/x068UVwb2pNta2ncpwZbX9rD54FEVAELqChjt8=; b=s3qi/TOoZD4nMrA8zxYkk429Hkw48Vkhq74ZLHIiKL9U6gKGDDlEqetvmEtjT1hjwo II4usOsmMPR5oUCVD/kqxRMZLI1P6Q8nQvG4Oh67k0czDyfWtHlGbg4xKr+BOH7ZFwVf 3DKStVVx2XRJTVnwS90hvF5TbJAfTGFWnJgVPH1oU0eNTv+dpT3bB38DgAB6bQWgQHpg OWt5IeOt2m67XlizuyUT2iEoXCutikqlg9cbkI9m5Hr4yms13OjHHg7iUUshlrK+26IW zzCiL5+ZeuYPbCzaK6myI7N5av//kTsImB34UVnUrDr1pv82jmBY+I0kca6Zho7fycaY oIYw== X-Gm-Message-State: AOAM531ie6Iy/d62Kqkx2YrF+1ZvE3GXNwaq6mWr+PHrReZGfU5h0aI7 nvIVXIpEiTdNk4qPcHbK2q4= X-Google-Smtp-Source: ABdhPJwzP+Ou4KkjzSt/Ju3NJwsHKc1onAphWsOfWyIUICU2pOYsqZPmbzRaAz7ee2Lr6mWb4N2G6A== X-Received: by 2002:a17:902:b414:: with SMTP id x20mr4289323plr.14.1644322519377; Tue, 08 Feb 2022 04:15:19 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.44]) by smtp.gmail.com with ESMTPSA id t3sm16973344pfg.28.2022.02.08.04.15.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 04:15:19 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, martin.agren@gmail.com, peff@peff.net, tenglong.tl@alibaba-inc.com Subject: [PATCH v11 13/13] ls-tree.c: support --object-only option for "git-ls-tree" Date: Tue, 8 Feb 2022 20:14:38 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.403.gb35f2687cf.dirty In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We usually pipe the output from `git ls-trees` to tools like `sed` or `cut` when we only want to extract some fields. When we want only the pathname component, we can pass `--name-only` option to omit such a pipeline, but there are no options for extracting other fields. Teach the "--object-only" option to the command to only show the object name. This option cannot be used together with "--name-only" or "--long" , they are mutually exclusive (actually "--name-only" and "--long" can be combined together before, this commit by the way fix this bug). In terms of performance, there is no loss comparing to the "master" (2ae0a9c), here are the results of the performance tests in my environment based on linux repository: $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.8 ms ± 2.7 ms [User: 85.7 ms, System: 20.0 ms] Range (min … max): 101.5 ms … 111.3 ms 28 runs $hyperfine --warmup=10 "/opt/git/ls-tree-oid-only/bin/git ls-tree -r HEAD" Benchmark 1: /opt/git/ls-tree-oid-only/bin/git ls-tree -r HEAD Time (mean ± σ): 105.0 ms ± 3.0 ms [User: 83.7 ms, System: 21.2 ms] Range (min … max): 99.3 ms … 109.5 ms 27 runs $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 ± σ): 337.4 ms ± 10.9 ms [User: 308.3 ms, System: 29.0 ms] Range (min … max): 323.0 ms … 355.0 ms 10 runs $hyperfine --warmup=10 "/opt/git/ls-tree-oid-only/bin/git ls-tree -r -l HEAD" Benchmark 1: /opt/git/ls-tree-oid-only/bin/git ls-tree -r -l HEAD Time (mean ± σ): 337.6 ms ± 6.2 ms [User: 309.4 ms, System: 28.1 ms] Range (min … max): 330.4 ms … 349.9 ms 10 runs Signed-off-by: Teng Long --- Documentation/git-ls-tree.txt | 7 ++++- builtin/ls-tree.c | 16 ++++++++++- t/t3104-ls-tree-format.sh | 12 +++++++++ t/t3105-ls-tree-oid.sh | 51 +++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100755 t/t3105-ls-tree-oid.sh diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt index db29a9efb5..21045dd163 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,11 @@ 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`. --abbrev[=]:: Instead of showing the full 40-byte hexadecimal object diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index ba96bcf602..9819a24186 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -20,6 +20,7 @@ static int line_termination = '\n'; #define LS_SHOW_TREES (1 << 2) #define LS_NAME_ONLY (1 << 3) #define LS_SHOW_SIZE (1 << 4) +#define LS_OBJECT_ONLY (1 << 5) static int abbrev; static int ls_options; static struct pathspec pathspec; @@ -37,6 +38,7 @@ static const char *format; static const char *default_format = "%(objectmode) %(objecttype) %(objectname)%x09%(path)"; static const char *long_format = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)"; static const char *name_only_format = "%(path)"; +static const char *object_only_format = "%(objectname)"; struct show_tree_data { unsigned mode; enum object_type type; @@ -53,6 +55,7 @@ static const char * const ls_tree_usage[] = { enum { MODE_UNSPECIFIED = 0, MODE_NAME_ONLY, + MODE_OBJECT_ONLY, MODE_LONG, }; @@ -67,6 +70,8 @@ static int fast_path(void){ } else if (!strcmp(format, name_only_format)) { shown_fields = FIELD_PATH_NAME; return 1; + } else if (!strcmp(format, object_only_format)) { + shown_fields = FIELD_OBJECT_NAME; } return 0; } @@ -143,7 +148,10 @@ static int parse_shown_fields(void) shown_fields = FIELD_PATH_NAME; return 0; } - + if (cmdmode == MODE_OBJECT_ONLY) { + shown_fields = FIELD_OBJECT_NAME; + return 0; + } if (!ls_options || (ls_options & LS_RECURSIVE) || (ls_options & LS_SHOW_TREES) || (ls_options & LS_TREE_ONLY)) @@ -267,6 +275,10 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) return 0; + if (shown_fields == FIELD_OBJECT_NAME) { + printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination); + return recurse; + } if (shown_fields == FIELD_PATH_NAME) { baselen = base->len; strbuf_addstr(base, pathname); @@ -304,6 +316,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/t3104-ls-tree-format.sh b/t/t3104-ls-tree-format.sh index e08c83dc47..c0ffc8e1c3 100755 --- a/t/t3104-ls-tree-format.sh +++ b/t/t3104-ls-tree-format.sh @@ -46,6 +46,12 @@ test_expect_success 'ls-tree --format=' ' "--name-only" ' +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(objectname)" \ + "--object-only" +' + test_expect_success 'ls-tree combine --format= and -t' ' test_ls_tree_format \ "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \ @@ -78,4 +84,10 @@ test_expect_success 'ls-tree hit fast-path with --format=' ' git ls-tree --format="%(path)" -r HEAD >actual && test_cmp expect actual ' + +test_expect_success 'ls-tree hit fast-path with --format=' ' + git ls-tree -r --object-only HEAD >expect && + git ls-tree --format="%(objectname)" -r HEAD >actual && + test_cmp expect actual +' test_done diff --git a/t/t3105-ls-tree-oid.sh b/t/t3105-ls-tree-oid.sh new file mode 100755 index 0000000000..992bb26bfa --- /dev/null +++ b/t/t3105-ls-tree-oid.sh @@ -0,0 +1,51 @@ +#!/bin/sh + +test_description='git ls-tree objects handling.' + +. ./test-lib.sh + +test_expect_success 'setup' ' +test_commit A && +test_commit B && +mkdir -p C && +test_commit C/D.txt && +find *.txt path* \( -type f -o -type l \) -print | +xargs git update-index --add && +tree=$(git write-tree) && +echo $tree +' + +test_expect_success 'usage: --object-only' ' +git ls-tree --object-only $tree >current && +git ls-tree $tree >result && +cut -f1 result | cut -d " " -f3 >expected && +test_cmp current expected +' + +test_expect_success 'usage: --object-only with -r' ' +git ls-tree --object-only -r $tree >current && +git ls-tree -r $tree >result && +cut -f1 result | cut -d " " -f3 >expected && +test_cmp current expected +' + +test_expect_success 'usage: --object-only with --abbrev' ' +git ls-tree --object-only --abbrev=6 $tree >current && +git ls-tree --abbrev=6 $tree >result && +cut -f1 result | cut -d " " -f3 >expected && +test_cmp current expected +' + +test_expect_success 'usage: incompatible options: --name-only with --object-only' ' +test_expect_code 129 git ls-tree --object-only --name-only $tree +' + +test_expect_success 'usage: incompatible options: --name-status with --object-only' ' +test_expect_code 129 git ls-tree --object-only --name-status $tree +' + +test_expect_success 'usage: incompatible options: --long with --object-only' ' +test_expect_code 129 git ls-tree --object-only --long $tree +' + +test_done