From patchwork Thu Jan 6 04:31:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12705069 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 76FB6C433EF for ; Thu, 6 Jan 2022 04:31:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233182AbiAFEbu (ORCPT ); Wed, 5 Jan 2022 23:31:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232552AbiAFEbt (ORCPT ); Wed, 5 Jan 2022 23:31:49 -0500 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BB3BC061245 for ; Wed, 5 Jan 2022 20:31:49 -0800 (PST) Received: by mail-pj1-x1036.google.com with SMTP id l16-20020a17090a409000b001b2e9628c9cso1898952pjg.4 for ; Wed, 05 Jan 2022 20:31: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=l8CyCo84CERA+hWqvBNuUUTSqg6HBUea36tRvi4YNpE=; b=UX0mTgM7cH41vb0A235UjosVTONCqE4b3+ayggZg06v51p3OLtI/NWS1eUetwYu8w/ OwWIMI0swRbslnrtfYOaUi7sTwl0Rc+5phz3UC+zKuULYJaHhK99LGSYcaXlcOjl3g3G l7MfxecTLX3wbGWQZoZl3i5JFEVvUkbRE8VEAJpLHe+zbxi08XcFClLJdhnx8YNV0/VN Ufe40E/mlJQaq5F51HRY8sogymAhFmPEk9V1TnHqFkDTQnndzoFfYEVt1Mz+23iRtRqs aNqYKQBePnwqKPiuf5YmW+t0+Y4PCXpFu2ORGlwN5sH00arZ37mav5rb16Zi9OCoI4u+ I2SQ== 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=l8CyCo84CERA+hWqvBNuUUTSqg6HBUea36tRvi4YNpE=; b=EPsH9PYCWLlQ+X+FRdmkE7KDa/tfRg0EVBGZ58TrYeXiNQnOG45pIADng1moEYWk+M TqaW6M+Dz9tveiGZFiegNDsTpkyVsbXxKFsqQs/yDD6zs/7TViLNEs6tru/gT1dkqp7h BDaUi4CMc8GEI2lOruqiczP53Y+uz7IJZh/isbzB2iJ6e6GZFj4V577V74aj2dCEW3NF aBoXe9Tnfg7CWscCfw0TsOmSnpeHoQa1Q228ktAgYZ41zfSyWDhR4OTlDfDr3lcbKk/V YX9tGJf5gJn8rdrW/2G8i1O3PG5c0uhX3KaShuc2jeLskV/avzYyMBbjtSXOkljdSnL4 j7Dw== X-Gm-Message-State: AOAM533lJTllAh0t+moekub5Nij8fxxN1P/B8YwXlEByZOjX/etzQ1lF ifRDkIpmIfWDZQZ2PKdIHZw= X-Google-Smtp-Source: ABdhPJzPfZk/MrI401S31C2AFH0+WAJqjHBMgHXi1vp0pVGUjrULkZt0u3GkmzoeYcLlHhy9cqojRQ== X-Received: by 2002:a17:902:d484:b0:149:95f1:fcd8 with SMTP id c4-20020a170902d48400b0014995f1fcd8mr37764057plg.41.1641443509065; Wed, 05 Jan 2022 20:31:49 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.34]) by smtp.gmail.com with ESMTPSA id 13sm636491pfm.161.2022.01.05.20.31.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jan 2022 20:31:48 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes.Schindelin@gmx.de Subject: [PATCH v9 1/9] ls-tree: remove commented-out code Date: Thu, 6 Jan 2022 12:31:24 +0800 Message-Id: <2fcff7e0d40255e0be9496647787352dd0aeb784.1641440700.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.33.0.rc1.1794.g2ae0a9cb82 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 Thu Jan 6 04:31:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12705070 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 A8DCFC433EF for ; Thu, 6 Jan 2022 04:31:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233478AbiAFEbz (ORCPT ); Wed, 5 Jan 2022 23:31:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232552AbiAFEbz (ORCPT ); Wed, 5 Jan 2022 23:31:55 -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 2387AC061245 for ; Wed, 5 Jan 2022 20:31:55 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id 196so1481862pfw.10 for ; Wed, 05 Jan 2022 20:31:55 -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=0R5Veoz7R8vUjhE0PBh6JNtDLC9bhLmigsYwFhTYU3s=; b=qTldMbjZoHEhUCcdon+a3RhOKJfybpBUCY7cP1gfGhm3dJbL7bn0whnxkNQL65VL1V EdsWx+ojWzdUJXnp65cnwX60VtPUZYuXujlJkRBlnFTOw4d7YhPzjQ+DAbLYwtqLxLZU pW5rwOESD7RM21DQcMpyLJRGDO6904YIpgYzY9JtthUi5ycK6vgA9IutBcpJlw84eQnO Nv0nKpzVLFsygWdfMKLPBP3fVtZG3xS2ZMX/qUenZxXx8P9kxg32CCFv5ieWpQm0O7Bv z1So2a9uDBNBl/8HgJwvl1XPd9mRWyBDUkWZJJjwWe5L8lMbtyBfr+tz8e4CatGeXMqV ADlw== 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=0R5Veoz7R8vUjhE0PBh6JNtDLC9bhLmigsYwFhTYU3s=; b=VYaEo1DZiQU0qUfUrlbaamXpPT62fMLFU+aQqEtfqrzUahQx33bDi9U8qliqCWidYB EFHZtkR+uQSioNv8++ENyde4yFZoQ5mzlKuUJFPR7GIIg+JC0DlPhwSf6BA30XBexqav rUJVOWLrjQPekTQub31SOmMbmIM9L81PdeNwuGEA1xBkrKycAngfBO5SHFu01L9mp1Ao rTFaOcffhz7cDJSAIh++gJzxTmohrKpmExFStgvpcs0/eHjyDqhZta4M/XG/hvAeaxDl W0kFTFcikvp6sfKP/rRZHTaoF01Yo1T28V/jL+nKsaLRXmdDcK34mDsozLvQqvU/11qz DN+w== X-Gm-Message-State: AOAM533KOpi2ec8ENlVwrBi4aBoPMduasy8iGtq6RTggKmGO6c8YvD7L PrFOdv1sWrtDQ1nQDsT0GXM= X-Google-Smtp-Source: ABdhPJxeDakpwwcHvTCtveIW71T8CXRJu8rUwwBf+X4bfYxQzxN+ioveEN0BT7SkdEFvAryKYxoebA== X-Received: by 2002:a63:7945:: with SMTP id u66mr38359361pgc.455.1641443514681; Wed, 05 Jan 2022 20:31:54 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.34]) by smtp.gmail.com with ESMTPSA id 13sm636491pfm.161.2022.01.05.20.31.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jan 2022 20:31:54 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes.Schindelin@gmx.de Subject: [PATCH v9 2/9] ls-tree: add missing braces to "else" arms Date: Thu, 6 Jan 2022 12:31:25 +0800 Message-Id: <6fd1dd938303192d5b0a4cdc2914b2ff72cf5a3d.1641440700.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.33.0.rc1.1794.g2ae0a9cb82 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 Thu Jan 6 04:31: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: 12705071 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 59B98C433EF for ; Thu, 6 Jan 2022 04:32:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233646AbiAFEb7 (ORCPT ); Wed, 5 Jan 2022 23:31:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232552AbiAFEb6 (ORCPT ); Wed, 5 Jan 2022 23:31:58 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72EC8C061245 for ; Wed, 5 Jan 2022 20:31:58 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id w7so1629366plp.13 for ; Wed, 05 Jan 2022 20:31:58 -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=G9TfqrPPqvQ9Q5rPIxT2vhXl2paAUnRnTsrVrzzBQP0=; b=DNxefMmTnC0xGk78Zv0c6SVZ+HjdEkoxzDJDzQLVLHYsjEOI/2O30aY23Ba0gcGneo OHVgdSOkF9qROeUAUerF/8174yt6J1E2UGylrpKiwQPV0vk00E2ULbjH0UZQW85cZ7zO sgEUuHZFg+V7+nvHxK+PFo/bhgKHHbwmB6qxofwRVI5IIZoMGKiIB5EYIystwOUKKrw+ bUyZvyXmPoONcFljU/vAQowm99PeD0yA5E8TS0kza9/zYY37l2QLoJsoR6Dr0iVFEVAI FAf8nWIlS7IxiUxsy7s1yhTVvMVFZVhLaLRRoRcfylJgjLYjtuplcIm1pv+yvndrwTlm jpXw== 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=G9TfqrPPqvQ9Q5rPIxT2vhXl2paAUnRnTsrVrzzBQP0=; b=2cmxnDFj1reTaPqfjup3e2v5VqMAyKSQzqrohasEI7ZBO3A+h+Ol4j5ui9sTDd5iD6 FaBkEo6J+z3FLm580MRgsSqBlBtb+38d/IdEMFnx1jugs+uVQ4sKi9AbS3nBnYJRjc2k cQZl+cMBUnnTWo9afvMpcvB1w9vXJbA5EsqJHeCa8yKIXGXV6WZXv/Ke6Aarc50JJKr7 htXpeGKNkD5dA4e8rh3v6f1l7CWdSK2G3NyJ/YRVqbLjG2MTVQDCz5N3YopxPBQaVjcX Kh/bQnJ5a+LJJO4rZCy4JHCuh0nKxaoZVgGCmVSv2Idxm8XNLskWO/jczHj4Qi+dSJRI NYBw== X-Gm-Message-State: AOAM533hxXPJHJTSu2peiy3cJtsFhNgKgLzHptRYuRc5GzI1iQ2DkjMH ggDa66Bkfjl1ECIr79i0yGc= X-Google-Smtp-Source: ABdhPJxrJR9XdAfQq4bK9JgYBjHs5EhexZvL5UoMX8eJO4Aj2ABEQojRBkVADgAQajDDnu+T2g4Bow== X-Received: by 2002:a17:903:2307:b0:149:ba56:ff83 with SMTP id d7-20020a170903230700b00149ba56ff83mr18485374plh.7.1641443518028; Wed, 05 Jan 2022 20:31:58 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.34]) by smtp.gmail.com with ESMTPSA id 13sm636491pfm.161.2022.01.05.20.31.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jan 2022 20:31:57 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes.Schindelin@gmx.de Subject: [PATCH v9 3/9] ls-tree: use "enum object_type", not {blob,tree,commit}_type Date: Thu, 6 Jan 2022 12:31:26 +0800 Message-Id: <208654b5e2aebbe988b5a8f478869253a0166e9b.1641440700.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.33.0.rc1.1794.g2ae0a9cb82 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 Thu Jan 6 04:31: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: 12705072 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 EB14CC433EF for ; Thu, 6 Jan 2022 04:32:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232552AbiAFEcD (ORCPT ); Wed, 5 Jan 2022 23:32:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233817AbiAFEcC (ORCPT ); Wed, 5 Jan 2022 23:32:02 -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 DA99AC061201 for ; Wed, 5 Jan 2022 20:32:01 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id gp5so1465194pjb.0 for ; Wed, 05 Jan 2022 20:32: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=quPlpqJZbkVLddFnUJGmACJ58TFoAyL7YzXhu9KzNic=; b=Yhams3REDSx4U1dwQYd6lgFe1y6eTAePl14pwhNcvINAYMzaj4dgkvt03G2BfKAOEA J4VlJtE50srht3j9yGE1lGkrgrgI0s5nfjPT11sJPIa6zUoUrrmYuGatfNCyCS6/7XFC hDDVNLWL+z7H4RbYmjVWqQEMJT10dmwULlTr+Q6+wshdPRjwdLqY7/OojOMYFnNUALb2 8/1d9kHJkWh90qXzev29njd6Bd61GVtN+rGfUE0wM0jkqgqQ61w1zJJnUoXSk5x280XC fizjxDACsUwgU7nVLkdLvrH3gCe+ZKx4F0Zf/O9S31xz6ul6Qfid2naTaQO5O6Iy8XpC LSLw== 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=quPlpqJZbkVLddFnUJGmACJ58TFoAyL7YzXhu9KzNic=; b=J6moWwLSx12dU8G6MRR9VZoWOhWmDa7kkeNfgG8r/a3pjhJmapcQNmkzV2nbNLAJu0 mYThypbEIo2J3z2LKzfLeIcWTcDoQl2wqY5mBdGls8bFH7E/bKfg7mfjgYgY67iAYI62 fqIbDs5/4d6EmGnWF+e+T3PLkRtDr39SUYfA0BNC/fOfzgBSxT+4P26zKkp+ayBxAyMO QogT3RCDmgW1C9Nc9dO9b7N7ScZrHJC+V4KMEo6GiP2rKIqIE9IegvRxJ960SwDpxavs asqPViv8V214vQGebEXB9qH87o14Eoo+8Mj+U52f95QZuZH253HdQtfk/YOTxZ7Wz6i+ 92XQ== X-Gm-Message-State: AOAM533tu/ksVKbxUjmLU3d5jLw0pq3WB72ihHWWnjAUWmCLtNCdOtXY iN0/7UUVUO26Xk9kTGLIXME= X-Google-Smtp-Source: ABdhPJyd1T9piKw+QbnRKq1uLWt7Llfa+CkttP+dUD1Ct9vZ+8fcmn3EQArvITpVAyNqUSeY2GhVsg== X-Received: by 2002:a17:903:1246:b0:149:a6fc:cf80 with SMTP id u6-20020a170903124600b00149a6fccf80mr28528026plh.156.1641443521406; Wed, 05 Jan 2022 20:32:01 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.34]) by smtp.gmail.com with ESMTPSA id 13sm636491pfm.161.2022.01.05.20.31.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jan 2022 20:32:01 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes.Schindelin@gmx.de Subject: [PATCH v9 4/9] ls-tree: use "size_t", not "int" for "struct strbuf"'s "len" Date: Thu, 6 Jan 2022 12:31:27 +0800 Message-Id: <2637464fd8f6b8bae6c681f20c10c9fc76dc6d6c.1641440700.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.33.0.rc1.1794.g2ae0a9cb82 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 Thu Jan 6 04:31:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12705073 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 3E4F7C433F5 for ; Thu, 6 Jan 2022 04:32:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233736AbiAFEcH (ORCPT ); Wed, 5 Jan 2022 23:32:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233819AbiAFEcF (ORCPT ); Wed, 5 Jan 2022 23:32:05 -0500 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BAB1C06118A for ; Wed, 5 Jan 2022 20:32:05 -0800 (PST) Received: by mail-pg1-x52e.google.com with SMTP id v25so1546181pge.2 for ; Wed, 05 Jan 2022 20:32:05 -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=89Uz1mkv+gPqWm/Mc69dIJDLJ4vI4UWr7PalD/8OJQs=; b=iMI5V3485XP4l1CvUovoF/6pydXRvL6+svnGNMDh8XYwLDFCsniRtoBIoWxVUlXo7z DhXKyAR5V1PlGMCNcT8DWKj4BcpfXqf3x498od084CSkyyVRNglcS4wWZuqAhE0OqgYu rkriMaiuPe+2NYSg2IA35glo3TjBa8vgGJFvfOr4xvKhQanKxKHMn4jiXlENt4qqLOKH QdbNlZ1ed+dmjX7nFKdqf6eDPKwwsyHqFnGOmgHYSdPN5AZ9IUP0yEwaE2+Z4Lqfxhw1 AwLpYNzIOLNnBPl/bLdZyoXYd5x27IUQoKYHAWqSlJ3pPvnVZDAL0uOUQdrRWNQ0y8Iv PWgg== 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=89Uz1mkv+gPqWm/Mc69dIJDLJ4vI4UWr7PalD/8OJQs=; b=jLSLHR4A+ZuHNT6ZfDe1LJaeI0N7aiqJf65vCl7isDzjt8XosFzfYyHt+koAXaNoi1 q9OEH2QnYhWzFgH74P/FzLRP1cP1J5IN2d8U2/278JBHZOaXq9Vzrf6YTzTiYNYr7O+F fVC6I5iannJ1L4L3nrMdCPAu5E2NYDCQ0M7QAY6pa/G5Cpuj70KiQgDTdYIk8GzQ/DmK eTMU61dVtglnqFLEJw17cd4Svnxs3FRcUS3Mcpnv5BsdMWXV5JOpM5tK7hfcUazO2j4w HySYfjszQUyIikYdqBErKckcZI0BkL3ZX/acY1NVJqvHjU85uPTmk/nKwAG/ljCOylKJ OYsA== X-Gm-Message-State: AOAM531MnujhMLrWLVjh0BEWXj76ArktR/OdmQ0qChcJ42FIsatiJ0Qn 5DSGFEUnbyEb4jkiJ+IIzhY= X-Google-Smtp-Source: ABdhPJyBoNzsHb/nDSq9uaJHbTQJg5PoJ/KWfwR5YvphF9kSgEYtQy1DuOcpmsZ1UXFL4RnfcNFhaw== X-Received: by 2002:a63:44a:: with SMTP id 71mr49906381pge.453.1641443524960; Wed, 05 Jan 2022 20:32:04 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.34]) by smtp.gmail.com with ESMTPSA id 13sm636491pfm.161.2022.01.05.20.32.02 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jan 2022 20:32:04 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes.Schindelin@gmx.de, Teng Long Subject: [PATCH v9 5/9] ls-tree: optimize naming and handling of "return" in show_tree() Date: Thu, 6 Jan 2022 12:31:28 +0800 Message-Id: <75503c41a7e2f3fdbb59ce3568853049b55a2d3b.1641440700.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.33.0.rc1.1794.g2ae0a9cb82 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. This commit tries to make the variable and the related codes more clear in the context. The change is based on three steps. The first is to rename "retval" to a more meaningful name. The second is that there are different "return" cases in "show_tree", some places use "return retval;", some just directly use "return 0;", this maybe cause some confusion when reading these "returns". For this , we change all the "return" cases to the new uniform name. The last is there are some nested "if" judgments surround the "returns", this even make the codes here a little hard to understand. So we put some logic in individual methods, "init_type()" and "init_recursive()". After the steps, let us look at "show_tree()" again. It has a uniform return variable name now, and first we init the "type" by "mode", then call "init_recursive" to init the value of "recursive" which means whether to go on reading recusively into the "tree". The codes here become a little bit clearer, so we do not need to take a look at "read_tree_at()" in "tree.c" to make sure the context of the return value. Signed-off-by: Teng Long --- builtin/ls-tree.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index eecc7482d5..7383dddf8c 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -61,25 +61,35 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname return 0; } +static void init_type(unsigned mode, enum object_type *type) +{ + if (S_ISGITLINK(mode)) + *type = OBJ_COMMIT; + else if (S_ISDIR(mode)) + *type = OBJ_TREE; +} + +static void init_recursive(struct strbuf *base, const char *pathname, + int *recursive) +{ + if (show_recursive(base->buf, base->len, pathname)) + *recursive = READ_TREE_RECURSIVE; +} + static int show_tree(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, void *context) { - int retval = 0; + int recursive = 0; size_t baselen; enum object_type type = OBJ_BLOB; - if (S_ISGITLINK(mode)) { - 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 = OBJ_TREE; - } - else if (ls_options & LS_TREE_ONLY) - return 0; + init_type(mode, &type); + init_recursive(base, pathname, &recursive); + + if (type == OBJ_TREE && recursive && !(ls_options & LS_SHOW_TREES)) + return recursive; + if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) + return !READ_TREE_RECURSIVE; if (!(ls_options & LS_NAME_ONLY)) { if (ls_options & LS_SHOW_SIZE) { @@ -109,7 +119,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 recursive; } int cmd_ls_tree(int argc, const char **argv, const char *prefix) From patchwork Thu Jan 6 04:31: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: 12705074 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 5E3BCC433EF for ; Thu, 6 Jan 2022 04:32:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233463AbiAFEcN (ORCPT ); Wed, 5 Jan 2022 23:32:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233848AbiAFEcJ (ORCPT ); Wed, 5 Jan 2022 23:32:09 -0500 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04D9AC0611FD for ; Wed, 5 Jan 2022 20:32:09 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id a11-20020a17090a854b00b001b11aae38d6so1919408pjw.2 for ; Wed, 05 Jan 2022 20:32: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=FAHfOr+S+QCtnoIUvVHsx8a0brl8zjbp5m6mnA39zpQ=; b=C7chPsOsiVPawgguOmwfYklvIzq9w12EEMZYQbXQKaCGs+mdQE+FXOvTicrj9BoN0b jT5EM/vf3SgjlEz1ljiwAWV7zi3woyAL5vonrVIFS/VNr6tm92TIKtB1ngXv9auvew2y g8+ale7h4wjQj609coCv5vF0PKHXtrY94K6Xbs8jAtqPCfi1lC7+9hPsYj4LZFVuIFNA bKJJTAl4n77lrSW39zTxytscXxvQFHjWJA5Cp3o+loFygsJ9fO+UYmjDeN03w2yXe60A H+FID2mPSP0C1z0AviHPSk1kaQtR58FgEgv3Y2FVFyAoDu13IDTyhIA7bv/qz4v2vUia o8yg== 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=FAHfOr+S+QCtnoIUvVHsx8a0brl8zjbp5m6mnA39zpQ=; b=8DDFCc+fqvIL7Ew8Ozrmc2J9dgaZUaB121bnAQmX6+eCmamZ2kElrJPzS6ZQ4+yNhU sqX74qh75cxTTEusD+zjItwSaOFW4FlZn/zKxRDyIlghCsaACrqQyiogD+Mn32nKbW3g gckSplCtluKtkCS5gGbdPwx6sGrcgCj11QH7kNytIT5LukJlShiOWBLnatoQIiqZPK0d ssWjovEfXkWfR3fy/1ugmnBDJhyrcuw+GeJ5KCpt2eV0FD7t6PdhoZl5fv+zzvU9TCyo DYZVdWEirz0ieRSyfvvQOvf3CPJdwKuehDlmW0/MtO/kHvZKxAw92+nhp2+DoTDBRi3s XUSg== X-Gm-Message-State: AOAM5323BF2IAVNqMmqatbB3AkYXZquDcrgzzplomTWVsyjnMr/D59Oi W5YjhOGUUNQaKFz/UTT6xck0S2u2eudItw/lAuk= X-Google-Smtp-Source: ABdhPJxVAuvtV/J8rw4Ae45cVAfMnpf72y7Xt1iYut0bWGdO2n1SgrRrBGHx1cGlGKD/RI2FvEtmVw== X-Received: by 2002:a17:90b:1206:: with SMTP id gl6mr7997585pjb.228.1641443528493; Wed, 05 Jan 2022 20:32:08 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.34]) by smtp.gmail.com with ESMTPSA id 13sm636491pfm.161.2022.01.05.20.32.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jan 2022 20:32:08 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes.Schindelin@gmx.de Subject: [PATCH v9 6/9] ls-tree.c: support --object-only option for "git-ls-tree" Date: Thu, 6 Jan 2022 12:31:29 +0800 Message-Id: X-Mailer: git-send-email 2.33.0.rc1.1794.g2ae0a9cb82 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). A simple refactoring was done to the "show_tree" function, intead by using bitwise operations to recognize the format for printing to stdout. The reason for doing this is that we don't want to increase the readability difficulty with the addition of "-object-only", making this part of the logic easier to read and expand. In terms of performance, there is no loss comparing to the "master" (2ae0a9cb8298185a94e5998086f380a355dd8907), 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 | 141 +++++++++++++++++++++++++--------- t/t3104-ls-tree-oid.sh | 51 ++++++++++++ 3 files changed, 160 insertions(+), 39 deletions(-) create mode 100755 t/t3104-ls-tree-oid.sh diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt index db02d6d79a..729370f235 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] [--object-only] [--full-name] [--full-tree] [--abbrev[=]] [...] 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 7383dddf8c..6b5e3ab9dd 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -16,22 +16,60 @@ 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) +#define LS_OBJECT_ONLY (1 << 5) 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_FILE_NAME 1 +#define FIELD_SIZE (1 << 1) +#define FIELD_OBJECT_NAME (1 << 2) +#define FIELD_TYPE (1 << 3) +#define FIELD_MODE (1 << 4) +#define FIELD_DEFAULT 29 /* 11101 size is not shown to output by default */ +#define FIELD_LONG_DEFAULT (FIELD_DEFAULT | FIELD_SIZE) static const char * const ls_tree_usage[] = { N_("git ls-tree [] [...]"), NULL }; -static int show_recursive(const char *base, size_t baselen, const char *pathname) +enum { + MODE_UNSPECIFIED = 0, + MODE_NAME_ONLY, + MODE_OBJECT_ONLY, + MODE_LONG, +}; + +static int cmdmode = MODE_UNSPECIFIED; + +static int parse_shown_fields(void) +{ + if (cmdmode == MODE_NAME_ONLY) { + shown_fields = FIELD_FILE_NAME; + return 0; + } + if (cmdmode == MODE_OBJECT_ONLY) { + shown_fields = FIELD_OBJECT_NAME; + return 0; + } + if (!ls_options || (ls_options & LS_RECURSIVE) + || (ls_options & LS_SHOW_TREES) + || (ls_options & LS_TREE_ONLY)) + shown_fields = FIELD_DEFAULT; + if (cmdmode == MODE_LONG) + shown_fields = FIELD_LONG_DEFAULT; + return 1; +} + +static int show_recursive(const char *base, size_t baselen, + const char *pathname) { int i; @@ -61,6 +99,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 void init_type(unsigned mode, enum object_type *type) { if (S_ISGITLINK(mode)) @@ -91,34 +162,24 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) return !READ_TREE_RECURSIVE; - 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_OBJECT_NAME) { + printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination); + return recursive; } - 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_FILE_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 recursive; + } + + if (shown_fields >= FIELD_DEFAULT) + show_default(oid, type, pathname, mode, base); + return recursive; } @@ -136,12 +197,14 @@ 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", &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_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, @@ -172,6 +235,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 diff --git a/t/t3104-ls-tree-oid.sh b/t/t3104-ls-tree-oid.sh new file mode 100755 index 0000000000..6ce62bd769 --- /dev/null +++ b/t/t3104-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 From patchwork Thu Jan 6 04:31: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: 12705075 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 325D2C433FE for ; Thu, 6 Jan 2022 04:32:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233234AbiAFEcN (ORCPT ); Wed, 5 Jan 2022 23:32:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233558AbiAFEcM (ORCPT ); Wed, 5 Jan 2022 23:32:12 -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 5012CC061245 for ; Wed, 5 Jan 2022 20:32:12 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id l16-20020a17090a409000b001b2e9628c9cso1899651pjg.4 for ; Wed, 05 Jan 2022 20:32: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=JcVlap2DybYKMoXTnY18ziEJBU2JBRvgabCnv4g56Ws=; b=F6cdDH3QH5HBIirkwDG7GeplK3BXDavUf25e4DEozBYZX5Iu9ZV9VGK5QRHLbnvXCP Tg/LLYdrMfE46E6E6Q0ySAouKOJHEWmAEmRNX3VExWHXubnX8OXb7OsxixocQ31RjvkN MInk4JlYtKCxG8olJSHYiGh1ijXkQdquU8BGUkiTPFOpgKoZlruLKMReP/0Ofy7b0Kk5 WBgSVFRMfSRt3iNHe2xHXfMDrwXJS46SobKXgLrr6irlznKC9beVBmzWZ1oBKXMa6pFR 4z7QKWxDwaDr6fGigGWUDuorDXgaMCGeNSQ2YvtbvnpZlbcZrrpq9k5FENuCstGdksIz +zFQ== 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=JcVlap2DybYKMoXTnY18ziEJBU2JBRvgabCnv4g56Ws=; b=LRb8f2miX7jxU1hfYxx43+/HZ9PzpSmYj+NfpIDLAi8xGqTSKQ+cU0zKnNofFPOG8J 1XkAecVsTKg2SHfV0z8MySsyPe3ERKtGdQPBwfGaO/qGiBplBTmtl1wEOpnvE298OP+C Vb/HR7y/rkgTA3bF/IgwDRW42nJhGAEoBim8gWWyh+uEDB6N+TDy4t5bqZthEItXO6cc rR8+vLgtxy85+0GEmwRbcPcSCH8lUiF3V+I7jW69jwcmRCo4JKJ1hoNR3Bd3TMvv/YTG /lltwauYdMkfGmvygPGAHinRuiPcg0EixtfXVVPQfKJP+wYu2inOIOtuqYnJKmnWJZNX Ck9Q== X-Gm-Message-State: AOAM532CNdDY343UucPVkXH4vfmY+fH3wekhuwVIVaui+GWifskuKv/x F+kzSkTtX7X5CKP9S5Cor8g= X-Google-Smtp-Source: ABdhPJz/147/p5pRjMNcfxpuHoZI/rUjniBLf/YtPhzP7c0nCEX8kvelkqDdWrXA405mrI6fwMVbQQ== X-Received: by 2002:a17:90b:4f82:: with SMTP id qe2mr7891100pjb.128.1641443531869; Wed, 05 Jan 2022 20:32:11 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.34]) by smtp.gmail.com with ESMTPSA id 13sm636491pfm.161.2022.01.05.20.32.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jan 2022 20:32:11 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes.Schindelin@gmx.de Subject: [PATCH v9 7/9] ls-tree.c: introduce struct "show_tree_data" Date: Thu, 6 Jan 2022 12:31:30 +0800 Message-Id: <725c4d01877fe4e03bbe27f50db0b33f07158d63.1641440700.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.33.0.rc1.1794.g2ae0a9cb82 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org "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: Teng Long --- builtin/ls-tree.c | 50 +++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 6b5e3ab9dd..12beb02423 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -35,6 +35,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 @@ -99,17 +107,15 @@ static int show_recursive(const char *base, size_t baselen, 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), @@ -117,18 +123,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; } @@ -152,14 +158,20 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, { int recursive = 0; size_t baselen; - enum object_type type = OBJ_BLOB; + struct show_tree_data data = { + .mode = mode, + .type = OBJ_BLOB, + .oid = oid, + .pathname = pathname, + .base = base, + }; - init_type(mode, &type); + init_type(mode, &data.type); init_recursive(base, pathname, &recursive); - if (type == OBJ_TREE && recursive && !(ls_options & LS_SHOW_TREES)) + if (data.type == OBJ_TREE && recursive && !(ls_options & LS_SHOW_TREES)) return recursive; - if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) + if (data.type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) return !READ_TREE_RECURSIVE; if (shown_fields == FIELD_OBJECT_NAME) { @@ -178,7 +190,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 recursive; } From patchwork Thu Jan 6 04:31:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12705076 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 542C7C433F5 for ; Thu, 6 Jan 2022 04:32:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233838AbiAFEcV (ORCPT ); Wed, 5 Jan 2022 23:32:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233907AbiAFEcU (ORCPT ); Wed, 5 Jan 2022 23:32:20 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CFD7C034004 for ; Wed, 5 Jan 2022 20:32:16 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id s15so1636977plg.12 for ; Wed, 05 Jan 2022 20:32:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OEuJFjdW1Rt6Uc2F34/ejGd2s6uxeqOSYWbM3IR82QA=; b=GhWzjcCR0jY0RRFyHTn4GsgmOLZca7na3PVGB1vrzTULHVldrmDcfX62yBl30CujpA N/s3LBGZWZ4qF46lvvSg1WSdIt+TZS6tGgyZJ/lAldezhmmQMSlmA7PZFEXotj57uJGA yrLSeRXtWitzb+DMjHKbr0Rxp5yHJ9YA6CpyCZy8KDwWf5silWuNT2BkeclUwkILcuZj SQOGOUF4lq2SIAy+P1i8v7NGeAl/xDwibufx7GeAkfleXh80pSXnzcBDKofLEAw0GmYk pCqfLvJSWP+Xm5FgabtbPeg+Dk2orAa2y4N8HDVMJJrOhm0JE9jrncvxUJBujTo/pbKs xspg== 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=OEuJFjdW1Rt6Uc2F34/ejGd2s6uxeqOSYWbM3IR82QA=; b=vluUEtSa4VePdrsQGifyZvLCT5FxYg5MFzSQWL8qGH9k6HWsd9am4tr7ZtvUYZfzkn 7fenl5niRslt1954EeacRq2tQ04ii2Aq+RZ74otph0z0WvCamwmul21CoNA3Xa+TVEKZ 0b8OJvJC8/MjuWNsEGW5gVHGh14pJeE369XUSYDq4hdxhiuckxe1Bkx6ko2OaKUlR1rY ggYQUGo2qSI6mDMmtv5M+Cf7QOPBVYWrbrWvVT//6k0SQsPiyrnQ8K79xs2VesflOp3u BMKfh+KgGfPJdDAArs3aEjXltmbDMGCtCkvz2mSy9eGB5ZU64AFy7rnA7fu0E+hDWFgx Sosw== X-Gm-Message-State: AOAM531RFB5g6yw8gTXaXvbN2ZGQTaluc6wSe+GM+kdDFALpOsR9/4TB lXAChCVUwDmgAB+o7p66z0Ok90HYJzzI6uNaRfo= X-Google-Smtp-Source: ABdhPJxaELwwsgfzmMeAEHHITdl8dfSHEv0E7ML5DYvGuK1HYlBZs8fEDYcOvYOnmDlEv8jhnMBECw== X-Received: by 2002:a17:90b:1c0d:: with SMTP id oc13mr7972695pjb.65.1641443535744; Wed, 05 Jan 2022 20:32:15 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.34]) by smtp.gmail.com with ESMTPSA id 13sm636491pfm.161.2022.01.05.20.32.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jan 2022 20:32:15 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes.Schindelin@gmx.de Subject: [PATCH v9 8/9] ls-tree.c: introduce "--format" option Date: Thu, 6 Jan 2022 12:31:31 +0800 Message-Id: <7df58483a4f665f9f54b92a97a6b57704f1e7a0d.1641440700.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.33.0.rc1.1794.g2ae0a9cb82 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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: Teng Long --- Documentation/git-ls-tree.txt | 50 ++++++++++- builtin/ls-tree.c | 158 ++++++++++++++++++++++++++++++---- t/t3105-ls-tree-format.sh | 55 ++++++++++++ 3 files changed, 243 insertions(+), 20 deletions(-) create mode 100755 t/t3105-ls-tree-format.sh diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt index 729370f235..2ca8667c5b 100644 --- a/Documentation/git-ls-tree.txt +++ b/Documentation/git-ls-tree.txt @@ -10,9 +10,9 @@ SYNOPSIS -------- [verse] 'git ls-tree' [-d] [-r] [-t] [-l] [-z] - [--name-only] [--name-status] [--object-only] [--full-name] [--full-tree] [--abbrev[=]] - [...] - + [--name-only] [--name-status] [--object-only] + [--full-name] [--full-tree] [--abbrev[=]] + [--format=] [...] DESCRIPTION ----------- Lists the contents of a given tree object, like what "/bin/ls -a" does @@ -79,6 +79,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 @@ -87,6 +97,9 @@ OPTIONS Output Format ------------- + +Default format: + SP SP TAB This output format is compatible with what `--index-info --stdin` of @@ -105,6 +118,37 @@ 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's support to print customized format by `%(fieldname)` with `--format` option. +For example, if you want to only print the and fields with a +JSON style, executing with a specific "--format" like + + git ls-tree --format='{"object":"%(object)", "file":"%(file)"}' + +The output format changes to: + + {"object":"", "file":""} + +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: + +mode:: + The mode of the object. +type:: + The type of the object (`blob` or `tree`). +object:: + The name of the object. +size[:padded]:: + The size of the object ("-" if it's a tree). + It also supports a padded format of size with "%(size:padded)". +file:: + The filename of the object. + GIT --- Part of the linkgit:git[1] suite diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 12beb02423..d0ba7c4365 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -57,6 +57,12 @@ enum { static int cmdmode = MODE_UNSPECIFIED; +static const char *format; +static const char *default_format = "%(mode) %(type) %(object)%x09%(file)"; +static const char *long_format = "%(mode) %(type) %(object) %(size:padded)%x09%(file)"; +static const char *name_only_format = "%(file)"; +static const char *object_only_format = "%(object)"; + static int parse_shown_fields(void) { if (cmdmode == MODE_NAME_ONLY) { @@ -76,6 +82,72 @@ static int parse_shown_fields(void) return 1; } +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 *line, 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(line, 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, "(mode)", &p)) { + strbuf_addf(line, "%06o", data->mode); + } else if (skip_prefix(start, "(type)", &p)) { + strbuf_addstr(line, type_name(data->type)); + } else if (skip_prefix(start, "(size:padded)", &p)) { + expand_objectsize(line, data->oid, data->type, 1); + } else if (skip_prefix(start, "(size)", &p)) { + expand_objectsize(line, data->oid, data->type, 0); + } else if (skip_prefix(start, "(object)", &p)) { + strbuf_add_unique_abbrev(line, data->oid, abbrev); + } else if (skip_prefix(start, "(file)", &p)) { + const char *name = data->base->buf; + const char *prefix = chomp_prefix ? ls_tree_prefix : NULL; + struct strbuf quoted = STRBUF_INIT; + struct strbuf sb = STRBUF_INIT; + strbuf_addstr(data->base, data->pathname); + name = relative_path(data->base->buf, prefix, &sb); + quote_c_style(name, "ed, NULL, 0); + strbuf_addbuf(line, "ed); + strbuf_release(&sb); + 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) { @@ -107,6 +179,52 @@ static int show_recursive(const char *base, size_t baselen, return 0; } +static void init_recursive(struct strbuf *base, const char *pathname, + int *recursive) +{ + if (show_recursive(base->buf, base->len, pathname)) + *recursive = READ_TREE_RECURSIVE; +} + +static void init_type(unsigned mode, enum object_type *type) +{ + if (S_ISGITLINK(mode)) + *type = OBJ_COMMIT; + else if (S_ISDIR(mode)) + *type = OBJ_TREE; +} + +static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, + const char *pathname, unsigned mode, void *context) +{ + size_t baselen; + int recursive = 0; + struct strbuf line = STRBUF_INIT; + struct show_tree_data data = { + .mode = mode, + .type = OBJ_BLOB, + .oid = oid, + .pathname = pathname, + .base = base, + }; + + init_type(mode, &data.type); + init_recursive(base, pathname, &recursive); + + if (data.type == OBJ_TREE && recursive && !(ls_options & LS_SHOW_TREES)) + return recursive; + if (data.type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) + return !READ_TREE_RECURSIVE; + + baselen = base->len; + strbuf_expand(&line, format, expand_show_tree, &data); + strbuf_addch(&line, line_termination); + fwrite(line.buf, line.len, 1, stdout); + strbuf_release(&line); + strbuf_setlen(base, baselen); + return recursive; +} + static int show_default(struct show_tree_data *data) { size_t baselen = data->base->len; @@ -138,21 +256,6 @@ static int show_default(struct show_tree_data *data) return 1; } -static void init_type(unsigned mode, enum object_type *type) -{ - if (S_ISGITLINK(mode)) - *type = OBJ_COMMIT; - else if (S_ISDIR(mode)) - *type = OBJ_TREE; -} - -static void init_recursive(struct strbuf *base, const char *pathname, - int *recursive) -{ - if (show_recursive(base->buf, base->len, pathname)) - *recursive = READ_TREE_RECURSIVE; -} - static int show_tree(const struct object_id *oid, struct strbuf *base, const char *pathname, unsigned mode, void *context) { @@ -200,6 +303,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), @@ -222,6 +326,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() }; @@ -242,6 +349,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)) @@ -265,6 +376,19 @@ 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) || + !strcmp(format, long_format) || + !strcmp(format, name_only_format) || + !strcmp(format, object_only_format))) + fn = show_tree; + else if (format) + fn = show_tree_fmt; + + return !!read_tree(the_repository, tree, &pathspec, fn, NULL); } diff --git a/t/t3105-ls-tree-format.sh b/t/t3105-ls-tree-format.sh new file mode 100755 index 0000000000..92b4d240e8 --- /dev/null +++ b/t/t3105-ls-tree-format.sh @@ -0,0 +1,55 @@ +#!/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 && + test_expect_code 129 git ls-tree --format=fmt --name-only && + test_expect_code 129 git ls-tree --format=fmt --name-status && + test_expect_code 129 git ls-tree --format=fmt --object-only +' + +test_expect_success 'setup' ' + mkdir dir && + test_commit dir/sub-file && + test_commit top-file +' + +test_ls_tree_format () { + format=$1 && + opts=$2 && + 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 \ + "%(mode) %(type) %(object)%x09%(file)" \ + "" +' + +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(mode) %(type) %(object) %(size:padded)%x09%(file)" \ + "--long" +' + +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(file)" \ + "--name-only" +' + +test_expect_success 'ls-tree --format=' ' + test_ls_tree_format \ + "%(object)" \ + "--object-only" +' + +test_done From patchwork Thu Jan 6 04:31: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: 12705077 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 765A5C433EF for ; Thu, 6 Jan 2022 04:32:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233513AbiAFEcX (ORCPT ); Wed, 5 Jan 2022 23:32:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233824AbiAFEcU (ORCPT ); Wed, 5 Jan 2022 23:32:20 -0500 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97A82C0611FD for ; Wed, 5 Jan 2022 20:32:20 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id y16-20020a17090a6c9000b001b13ffaa625so7124942pjj.2 for ; Wed, 05 Jan 2022 20:32:20 -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=xcGBZE208J+nFLcDQbdOgAxCPJV3e8eVzpI4vHD3ssY=; b=UzPWwL4h0EkVhpBYeTCfsNIlK1bbcB2xxZhpOl8P1T3nFCgaroiGYIq43Gll7SdfCS B1F3VMG3oQUdszdKgUSt3s7lxpk8X3LwLDLDnTAlou0YdGPUMtwWoD7JE0kns6D80/lB flOkuTQ4C8ibtSMsxYlGvCJC8/3T+1NI7oem2BsqlegmlzhkWvHBOOGoO2eizoKJvuT5 0+Kqo7pClp0hCfAAdAFDwKyhcIoNziJguUNynCdchCYnM+uTy/hmHDvSa8E8TK1BZ+P7 s7qI6PsGbP8MoWM55m4RTRPjna2YJUBhCyWFb+0myBQn991lUIxq6fASxP6b4KnMRn2p T/nQ== 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=xcGBZE208J+nFLcDQbdOgAxCPJV3e8eVzpI4vHD3ssY=; b=7f8k5gzFmuKu0e8uqth/dDWpQXhyCo/bga/dc97ScMsBhYhkBpeXiEVOWHNv//2rrI Z9Egz/zB4YC7OdfN2hO+3NsnCl7gfoQNDbISXb2R+G27ngaa7vAABzIHRtG229Hvk3Vg 1LHwZ4tPla1I9AO5193Toy97+9s7YFi+Z3eaSwdy2xXdu+3TLgRuDLPbCr5X7B5ao8st b7iTGhBUIgBGupr17o61DF9Owj5EODcyB0RO9mF2aegeIN37znWxB8Lr64DckrGemOMY J2t1BmRkPyHzegQ22ZJc7ZRIGrkB9l1oTae/z/i6eCmfq1dexIUea5fdBT/DtNoh7gVp XKJg== X-Gm-Message-State: AOAM5320NiKNvxYsQbqidj4fRv9NF1AjSLWgXnUvi89xzNQwNIH2g4M+ pUsvc/q3NyvthEK33+hkH1c= X-Google-Smtp-Source: ABdhPJwrFyfjhNHvvW74kVyyc042GTJfwscpId7yT9rtgnvbUbX9i5UXgmmRNMV3qyze3Mg+PulYsw== X-Received: by 2002:a17:902:d484:b0:149:95f1:fcd8 with SMTP id c4-20020a170902d48400b0014995f1fcd8mr37765541plg.41.1641443539971; Wed, 05 Jan 2022 20:32:19 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.34]) by smtp.gmail.com with ESMTPSA id 13sm636491pfm.161.2022.01.05.20.32.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jan 2022 20:32:19 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, Johannes.Schindelin@gmx.de, Johannes Schindelin Subject: [PATCH v9 9/9] cocci: allow padding with `strbuf_addf()` Date: Thu, 6 Jan 2022 12:31:32 +0800 Message-Id: <8dafb2b3779715aa277eb825a94af87b72f3e093.1641440700.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.33.0.rc1.1794.g2ae0a9cb82 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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: https://github.com/git/git/commit/28c23cd4c3902449aff72cb9a4a703220be0d6ac Signed-off-by: Johannes Schindelin Signed-off-by: Teng Long --- contrib/coccinelle/strbuf.cocci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci index d9ada69b43..2d6e0f58fc 100644 --- a/contrib/coccinelle/strbuf.cocci +++ b/contrib/coccinelle/strbuf.cocci @@ -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);