From patchwork Thu Jan 13 03:42:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12712192 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 A43CEC433EF for ; Thu, 13 Jan 2022 03:42:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232245AbiAMDmZ (ORCPT ); Wed, 12 Jan 2022 22:42:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229636AbiAMDmW (ORCPT ); Wed, 12 Jan 2022 22:42:22 -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 1599BC06173F for ; Wed, 12 Jan 2022 19:42:22 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id u11so2436553plh.13 for ; Wed, 12 Jan 2022 19:42:22 -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=EivWN4tKjC9qJD4S7qofZ34VHlI0C7dKN2uPAx0RUo0=; b=eOrexc14PSnoEKzopww3WguV6a9pNolg5P5FBNXuJv4pDQZLSE+AfwAjtKPcFpwACb FOZ1t5NHl8747tLddhTC8ps8i1Hnf507ooOh7qJdWCOfWfPyIV+lStFoNsAS0egc8c5j Yh9nVDkj/BE4Vke8wu8EqxsY7+drsTubiqgUawLNggPyPkL/p5eTQMsGiqSxQsQk65Z/ xOI7jEY1yGHgB+bnFPHHKec0is7auo1bOhv7WuGcGByQnixKY9lEaeX/22g65MJtgVif 3RYEjDeNtge71AglUKB+hKVKUGOu8ZAcUNkxsPP9xGFwV55QF4DQBl3W1ah+tiOIk/yq ORYQ== 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=EivWN4tKjC9qJD4S7qofZ34VHlI0C7dKN2uPAx0RUo0=; b=Dy4CESrmaohIaiGzyfWOFfVmVdG4oorDL8vFsk6eRSp0Upw6pCgh8V1gDQV8nWiv4p ikxzFHhClo6ZcJ5CgAa3hNPbWUYAkZhpUIaJYlGsUDeZRqlRuodPOyxh1jddp3XMBYHY CyioqAC0ixbeNp7qFeiqQmFGlqQUoRqnE4m7c9dIyqrm3PvLWYp7zTkrqy60uYraZZ5l 2UXmyoveXuAlOetph/C4NWHpfZh5RfhEI6Ot10h2/xCKQhN9C8kEdx+RkHYIwySMAhx5 IyF56eaqtf2RDIcz1zVJIsXsG5/dWnsoRPmKyQqiB4SV/3QgpI33FzJsYOT5mvI6QQHh F2JA== X-Gm-Message-State: AOAM530clPSSd970GQlM4OBrMD2BwzpcFyEkvg3HTsT7BGk4fHqORV6U q7grT0QnSgGPkkd9wx3NCgzk+6Ke1+9EeWczsEs= X-Google-Smtp-Source: ABdhPJxHZNjZllcvLoFYC1o8mhvnaFk8/bYwezopxonsRdLQCDqZh246vRb4VJCoaQ8AxLFfMP19EA== X-Received: by 2002:a63:7c5e:: with SMTP id l30mr2409544pgn.158.1642045341630; Wed, 12 Jan 2022 19:42:21 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.48]) by smtp.gmail.com with ESMTPSA id c20sm879436pgk.75.2022.01.12.19.42.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jan 2022 19:42:21 -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, peff@peff.net, tenglong.tl@alibaba-inc.com, martin.agren@gmail.com Subject: [PATCH v10 1/9] ls-tree: remove commented-out code Date: Thu, 13 Jan 2022 11:42:04 +0800 Message-Id: <2fcff7e0d40255e0be9496647787352dd0aeb784.1641978175.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82.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 Thu Jan 13 03:42:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12712193 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 40808C433FE for ; Thu, 13 Jan 2022 03:42:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232246AbiAMDmZ (ORCPT ); Wed, 12 Jan 2022 22:42:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232237AbiAMDmY (ORCPT ); Wed, 12 Jan 2022 22:42:24 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C763CC06173F for ; Wed, 12 Jan 2022 19:42:24 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id z3so7595711plg.8 for ; Wed, 12 Jan 2022 19:42:24 -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=IriodHRimd7+PzU6tgt68KmEhLst1mOd7aJJ6mMOKVg=; b=hC9sOlm3xQyFPvtHlQ8M4Da8gGbhnlrNamHIbh0hR8ayj1TVulz7zc9MiHRbjFenE2 I2fkV55SA//YnbQg+GGWRCVu2xaNoM94kAhPXZffLWywo0hTUKgXwlKw1akV3RM1RX+4 PAB6VVCK4AlOaUs2lpuJG/uex2HBewQsv0W2aF2gSMTkfqPbbuYK93haJ8xI/bUo5fWS Tf4NTBQMaF4uDCyMsVq5zBycu5tMK4BWeLfCJDVZoU79c+R8Y42ZV40kgIiFRB421YlC 6RYZZp4utPB/QtiWeNid8tFQyh5MKBAI7jzukMqJ20nrCAg02BN70k+ongU7NFIuLqXU 2grQ== 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=IriodHRimd7+PzU6tgt68KmEhLst1mOd7aJJ6mMOKVg=; b=HxU6fM2E6F10JBS/wvMS9N9kcW1p9xiqxbR8YmsqBXlBnJfYFQxJwnipk8bPAFJ82P I8m3z1yejrA5Yjb60W/59o6hbl7isDXhaV/23s7PtFftINe0GUlD+OVVYOWoG1gvYzMS glNYyB5PLmJRxI7BKIR9yMXJ25CUbGLSyJy5aFhTZ4mKUL7SuSa4qWfSJlK5VuwklavF HG45OO8OkSIZRAKIidM2yG26+X6x7uxJjci00cTT64wmiGB3BVMUdAJBvsMOYGRKloq+ lVTwZXL59MVLUlap/t3W3TeT6LgOs9W4Cpn3G9x0+BvxpuEK9Zfk8w4klIsQBrCnab6W SgcQ== X-Gm-Message-State: AOAM532infB0/WWnZ1HRheU4vg1OrQhhB3H16UdXfCT+KkOD+toL0gZl TbMSiWNXfPwA73XHbz7bFlI= X-Google-Smtp-Source: ABdhPJzgKNa8935Z7v8L/AKEm18/NBDuZVa3ziUEjZ/3ehg/mVXXb8f3Fxgq7FUfeJQ87u5eIPgq1A== X-Received: by 2002:a17:90a:c695:: with SMTP id n21mr3115967pjt.10.1642045344118; Wed, 12 Jan 2022 19:42:24 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.48]) by smtp.gmail.com with ESMTPSA id c20sm879436pgk.75.2022.01.12.19.42.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jan 2022 19:42:23 -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, peff@peff.net, tenglong.tl@alibaba-inc.com, martin.agren@gmail.com Subject: [PATCH v10 2/9] ls-tree: add missing braces to "else" arms Date: Thu, 13 Jan 2022 11:42:05 +0800 Message-Id: <6fd1dd938303192d5b0a4cdc2914b2ff72cf5a3d.1641978175.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82.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 Thu Jan 13 03:42:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12712194 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 7DE97C433F5 for ; Thu, 13 Jan 2022 03:42:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232248AbiAMDm3 (ORCPT ); Wed, 12 Jan 2022 22:42:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232237AbiAMDm1 (ORCPT ); Wed, 12 Jan 2022 22:42:27 -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 1480AC06173F for ; Wed, 12 Jan 2022 19:42:27 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id u15so7667118ple.2 for ; Wed, 12 Jan 2022 19:42:27 -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=pmZI9YoLhRPKFtW+CoSYeM/MQUzBxsyZVt0ENVu7K1U=; b=NwjQBkKtazJeAuCU8wWuWQeV47n/ytOWNezKNQJLVi0NNDvypVQYIC6TATqwVpZSHU WmPxtSv2Tz1JM7nMT9Vphb1Drd+qRTBzDL/VTlbVKtED+9rFwTH/STTDfipf92lZgWq4 VrkJe72TAtNUQzv99+Hfa3HL6Ax/0Stc4u0Gz90reFOJMjRFMkn7CSQNYEV3REwh3/b4 IkWpTbi6SQckllNE7HfLn84a7nIyAtwEigd3QZ4ve8rXa+7bgTgvYJNL41/G4/yqwVBO XJgYL5r67nw2fAwf1g8fOJV/qZiKoGKJeWMEuvi9OXh5huOIUaH+AfV0G+SWLN95fiYz MsCg== 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=pmZI9YoLhRPKFtW+CoSYeM/MQUzBxsyZVt0ENVu7K1U=; b=JchtXe75yH6fPjfX+POws2u1rjQLepHsDh2Uf+Jg41FUNr6mgjEuymkKnvRHUzUTuZ Ed1+4Z/FzK/jYtSs4F9jSMfiHpfhCyt4MQaijREzLHkF2lQXIG2ohj780rlZPR/VRYuO Co7100fHDsk+SOZxNEDjDEh9T3Mat6f2d9zuKjMyslbO/AYRnU3VsrRM/R5ZNM2nEjNC QzIMmoacbvT7sFCG34ethdFTH8weZOFvNH2PsAmSR0IxeBF75ThfXR7oLdY/99LIY6TM /a05TTDQZoHZw7fHUbbP+mBtTkZPCY3g4hl7mCR47Snt2Bkrya7wPtn8u7FlG1wjItBF tc+g== X-Gm-Message-State: AOAM530m+9aW7YbGFgqQ0MtYGpdeRm3oI31vh9UNkq+6+XvMWQYhn4Cx 3m09dZpQLL7uozAWBl5LqKo= X-Google-Smtp-Source: ABdhPJyKEYPCBTEJov/WJoQf7WPh/CEXTly3FlK/6ImXSmE+mzlh8RpUY22sguA0X+kM273fDVlmoA== X-Received: by 2002:a17:90a:de0b:: with SMTP id m11mr12475592pjv.76.1642045346609; Wed, 12 Jan 2022 19:42:26 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.48]) by smtp.gmail.com with ESMTPSA id c20sm879436pgk.75.2022.01.12.19.42.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jan 2022 19:42:26 -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, peff@peff.net, tenglong.tl@alibaba-inc.com, martin.agren@gmail.com Subject: [PATCH v10 3/9] ls-tree: use "enum object_type", not {blob,tree,commit}_type Date: Thu, 13 Jan 2022 11:42:06 +0800 Message-Id: <208654b5e2aebbe988b5a8f478869253a0166e9b.1641978175.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82.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 Thu Jan 13 03:42:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12712195 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 5AE3BC433F5 for ; Thu, 13 Jan 2022 03:42:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232260AbiAMDmc (ORCPT ); Wed, 12 Jan 2022 22:42:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232254AbiAMDm3 (ORCPT ); Wed, 12 Jan 2022 22:42:29 -0500 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 940A2C061751 for ; Wed, 12 Jan 2022 19:42:29 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id l10-20020a17090a384a00b001b22190e075so16608007pjf.3 for ; Wed, 12 Jan 2022 19:42:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Og3NRhvCB8viY+g8beafb7FBfKqhyiS8McSgvZBORGw=; b=mcO06NuBWF5z+EN/5roIdawMON7nWGd8urRGPoywfp1p505pXWkA/T7bOsLNhhCf43 HVtOALUuj3oWd/VuA1dujHXuRq6bjAqdDj3kJ0E6lmGe0flMs/pz9pWx2KyiSI1t3KIB 7YA9xjfV1VGp+wd0jdAW0B5gX4i0KGKCIMKzTCB3U0iZUyvgFWrsxfz+JnMGp5Tpii/9 oXejogmj0EfnGvXPLxzEllvWFCGt3ZYzjhKWfSQ+udw/1zSxsrMUiGqr7TQfKVxjk5U3 C3qcjyrKwmzHs6niEhsZo9R4YQALTbzZ/3OaJrzEUxbb8xUK90EfaepWtERSgBW6/Gmj XKYQ== 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=Og3NRhvCB8viY+g8beafb7FBfKqhyiS8McSgvZBORGw=; b=OMZ2BPI9YR3DjCJy13AoMWSNrbPyk89xI+plAhov0nSlT2u9XFs/FcDGAM6pJp0XAS lfthqWPkZ+1sq+Lz60M/EPGrdD/SsMm3BRJ7SpqlAcoA7gYU6/2ZlOS7d4Npct3sW76Z aUOqqdmMxZ1ZvetaLCydfDnBgmwE4ll0ZDWKGkTzk/N2O9JbKXQVvvaUkhB5c81O6u5n Ky+hlW0NN2dTaX+7ISWvksER8OaTz/pAqVAVKe7Oihfs2BF/SkfqDyHylcovuXbvK4wO hu04k/21q8JhIbNDPzJ6hngXInxMeyuUta5rlBNB0BzCQvLZtBqlfYOYhWxsoyf8+VHm Mhjw== X-Gm-Message-State: AOAM533NPUgBKlqqXQ35shd/o2DH3a/tBqB26N4RcsbRaMuP+Gl/aOHA d7Z+oV+OVZZ6QD3s2//C8Ow= X-Google-Smtp-Source: ABdhPJzz10f5OjIXReYgSA0VoncXNB0gXoJECBheP4r20Wv5YLxBTAsvbAsk9vGLKk7NISp/X1oGig== X-Received: by 2002:a17:90b:180e:: with SMTP id lw14mr3047750pjb.179.1642045349102; Wed, 12 Jan 2022 19:42:29 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.48]) by smtp.gmail.com with ESMTPSA id c20sm879436pgk.75.2022.01.12.19.42.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jan 2022 19:42:28 -0800 (PST) From: Teng Long To: dyroneteng@gmail.com Cc: Johannes.Schindelin@gmx.de, avarab@gmail.com, congdanhqx@gmail.com, git@vger.kernel.org, gitster@pobox.com, peff@peff.net, tenglong.tl@alibaba-inc.com, martin.agren@gmail.com Subject: [PATCH v10 4/9] ls-tree: use "size_t", not "int" for "struct strbuf"'s "len" Date: Thu, 13 Jan 2022 11:42:07 +0800 Message-Id: <2637464fd8f6b8bae6c681f20c10c9fc76dc6d6c.1641978175.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82.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 Thu Jan 13 03:42:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12712196 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 7A84AC433EF for ; Thu, 13 Jan 2022 03:42:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232273AbiAMDmf (ORCPT ); Wed, 12 Jan 2022 22:42:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232261AbiAMDmc (ORCPT ); Wed, 12 Jan 2022 22:42:32 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 526CDC061756 for ; Wed, 12 Jan 2022 19:42:32 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id n30-20020a17090a5aa100b001b2b6509685so8970224pji.3 for ; Wed, 12 Jan 2022 19:42:32 -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=nnvutfgoPVy8ARQIFqCSevdqjzHGqHwG1IPa3F2O7Tc=; b=pI+mevWKSJODGz2HMAm2bZ+ra6hyp6wv8CeXz77xD4EVuIF1+ysBJAy4PdW9AVtJhf 7saEPDJjxCy/ws1vxJ1iUdLXmhe2FvoiNGEwmRnZRAARlHtZ/mEaV7pGLKPisPs0r4jM MC1idilxEifT3AaYdbIUQwtAtgiYwIlKj8cY9d9CoscVrTYixTbLMUqiWisZuMsgp1I9 aICDofttwBpMpSwYg+LtHapLzb2TrnenzbnwX+kzs8ssNSjxpOp42VF6tRzRiQFByWOj EfTkuKk9DprPKKptcQvx9QoVwm84w53QCmNLbM5/nvsMfbBHIvYm4hpg0lXDwBWa/hJp ZASw== 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=nnvutfgoPVy8ARQIFqCSevdqjzHGqHwG1IPa3F2O7Tc=; b=PHsOwb3FR92q89JWRoxvR3ths1o7YV86ixlTx3sTg49P8nwzEmb7IWtIWQdofzyUHQ KkO70CZpgCKC8314BaW3PEl0qhKtGWd9AbYS61KRevHYPksk2YB4rhGBV7GQiwYfAT8q KxmHcrNcBRDriUuOXGULqKtgK0Fqb3z937RrhoNwY8d8itoggkiHWzOS1+CS57Jq65a8 zkFEeewX8Y3jCfDmeSNobvVOTRefPuyC6afm5SLDkLPalzskbL48GRknHZpfrDkZFAUu jmmnG/+a/wMVl3NKPFj31XjF80O4Mvkspb9aoJ28kC6C93WE2x55FldBk/6jezBoAPpf w3NA== X-Gm-Message-State: AOAM530oWPN/tyEFayCJor5jeotnIQ8CofGzbjGgDzzHJIOkgtOaWoUW 1M7fh9ao653iKFJY/ko3at/FG+4ux7xXmlCxWzE= X-Google-Smtp-Source: ABdhPJw1Wh7abfQZ3Csx/pyV/O/ZUZjgkMdxOgBb1cg8HYN2qH2LeCBHzDpfq+jIGOxNbI/E9SkT6g== X-Received: by 2002:a17:902:c404:b0:14a:1594:2e69 with SMTP id k4-20020a170902c40400b0014a15942e69mr2576351plk.167.1642045351821; Wed, 12 Jan 2022 19:42:31 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.48]) by smtp.gmail.com with ESMTPSA id c20sm879436pgk.75.2022.01.12.19.42.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jan 2022 19:42:31 -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, peff@peff.net, tenglong.tl@alibaba-inc.com, martin.agren@gmail.com, Teng Long Subject: [PATCH v10 5/9] ls-tree: optimize naming and handling of "return" in show_tree() Date: Thu, 13 Jan 2022 11:42:08 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82.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. This commit tries to make the variable and the related codes more clear in the context. The commit firstly rename "retval" to "recurse" which is a more meaningful name than before. Secondly, "get_type()" is introduced to setup the "type" by "mode", this will remove some of the nested if. After this, 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 | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index eecc7482d5..9729854a3d 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -61,24 +61,27 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname return 0; } +static enum object_type get_type(unsigned int mode) +{ + return (S_ISGITLINK(mode) + ? OBJ_COMMIT + : S_ISDIR(mode) + ? OBJ_TREE + : OBJ_BLOB); +} + 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; - - 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) + enum object_type type = get_type(mode); + + 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)) { @@ -109,7 +112,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 Thu Jan 13 03:42:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12712197 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 10301C433EF for ; Thu, 13 Jan 2022 03:42:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232262AbiAMDmi (ORCPT ); Wed, 12 Jan 2022 22:42:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232267AbiAMDmf (ORCPT ); Wed, 12 Jan 2022 22:42:35 -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 DA171C061759 for ; Wed, 12 Jan 2022 19:42:34 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id pf13so8979507pjb.0 for ; Wed, 12 Jan 2022 19:42:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8JpgwWjmesDR39WnJycXEe9zdnrRfkszov0Z3qMpYRA=; b=kaKclGoVakkISwum+wvO58wdY5gWMSFdFN+6MEjbSDQlG3ddHgXE0HvLMY725Mcrj6 mwh2J3JdUAxfr6j6T6Hp4E0BP8U0lwPndLb+Oo6XJq8YA0oLTcp56qErWXwDe5J/QCJS yyOa3wVuiLOP0gaNXVFScClP7CRGplXE9uj/b4fHBLSNYw61ezlVm8dPtW6sQM1JVYoc zEXKD+yjkH4UdNhEJIbCe2nZbW/BM6DteCmqZmiq5rqNWRzeDLxUn94eUanOlMPRQFrk tkz9v+O96YjP7kNnTBc9v8ugiqEQSFr1oxFwxetc/yasRfbEIEJPhTECIPoiZAt/sfx4 b2+g== 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=8JpgwWjmesDR39WnJycXEe9zdnrRfkszov0Z3qMpYRA=; b=y2FOBHV3MDaKBLkKwRD7MYOaFm18cNX2Jrmyf8MqNkYJV8Git+bQ9JD0CDXFKttVr2 rbfTCqzJkxTE4weMoAYWAF+PkMI7meRYLUBSM9RtZPPy8fNzWqFmoqiuHVUnMxyrOM+j O19CZ3LgBBgReDji6VaQCGyuG/YrQln5bc9ob9kpgm78TMiGVaROKiwVhxlBIn3J2bvb 9KU5Lvr2ZNc+wAmV87NV8oYJuBojeKyAX/eg+uCUfVc2C4OcbnOQmjXgVjLVmbzW2E1D SJHfGDbl15SWtRkcMMBnomjbcK+rNvY9MS5LcoPuav74AlIERxSdRlJQEavsX6+yG05O FFgQ== X-Gm-Message-State: AOAM5325mCTbBsriWQVWz+CBmB49KjmCsGPWarnt8MKJvLox1KcD2V+E K3iqHm2WypYQzSNdvdrKZC0= X-Google-Smtp-Source: ABdhPJy0kYOfiaF028OMLfZzOXe9iVR80siVGTRX8ZvNpXoxaMycOWnjlAnxZMSvLEzOaOKdd1YIFQ== X-Received: by 2002:a17:902:7b96:b0:149:49fc:7de0 with SMTP id w22-20020a1709027b9600b0014949fc7de0mr2513351pll.25.1642045354343; Wed, 12 Jan 2022 19:42:34 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.48]) by smtp.gmail.com with ESMTPSA id c20sm879436pgk.75.2022.01.12.19.42.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jan 2022 19:42:34 -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, peff@peff.net, tenglong.tl@alibaba-inc.com, martin.agren@gmail.com Subject: [PATCH v10 6/9] ls-tree.c: support --object-only option for "git-ls-tree" Date: Thu, 13 Jan 2022 11:42:09 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82.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). 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 9729854a3d..e1a2f8225b 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; @@ -70,6 +108,39 @@ static enum object_type get_type(unsigned int mode) : OBJ_BLOB); } +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) { @@ -84,34 +155,24 @@ 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_OBJECT_NAME) { + printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination); + 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_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 recurse; + } + + if (shown_fields >= FIELD_DEFAULT) + show_default(oid, type, pathname, mode, base); + return recurse; } @@ -129,12 +190,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, @@ -165,6 +228,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 13 03:42:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12712198 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 B2FDCC433F5 for ; Thu, 13 Jan 2022 03:42:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232276AbiAMDmr (ORCPT ); Wed, 12 Jan 2022 22:42:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232278AbiAMDmh (ORCPT ); Wed, 12 Jan 2022 22:42:37 -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 6D91EC061759 for ; Wed, 12 Jan 2022 19:42:37 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id y16-20020a17090a6c9000b001b13ffaa625so16651277pjj.2 for ; Wed, 12 Jan 2022 19:42:37 -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=B6UWG0IOiHJhFSmbNoxoi7AFXeUUN0/apGWh9laPaqA=; b=oA1bxwiA9+j63tbtw5miwKlx48/U8obk72P//c/D3j9Af3hJqOpguoCPsAKE7bOSAk K5jY+gfhYbPJTihxOy3zkj34rE+YDAM2VrDjpIG55DKb7iE9FDonRVxKSO6cRsnIK3mA GaGP2FmRW9zgy65d2v0OmY1CLPrUX1boMWU+C5GWtXfx/lPvpu++YmH2+M7raBjp6QMY ncoA+sDEpiYfe1/xSOBeqngI6wSfbKQx3F4xlok6tbZ7IYoV2c+tGVgkayOo9x8wLztU f0zWkSKQ7r0/S6dquStjf+lbXznKR7+XdRtrMHns19kPFW69Jk76bvHFAQP/+4s0ivzq QyGw== 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=B6UWG0IOiHJhFSmbNoxoi7AFXeUUN0/apGWh9laPaqA=; b=FdEA5KvN3lxVa53Z/qri4Tkudt7gqAMPVM0TDhkNbiX3FswNPPP7u+iqaQJQZn0gSa rJcCxKXWmMZ3SuNlQnzvy0hPGo/Tg9ev2DOFL8c8eJV0UkfEt1ALpv8BReKI9L9tsLS9 heeicSVa0OzKEjspemEGESRd6W6wVUkDwDY0WMpOxeuexUzpRZ4XT9iyK+xuGi4qKYZ3 zQycYr9ppCDxUUsnCcW1qk/8UVfGHvtVicheQDFvsdwcX40wRa0ulumZBo6HmE43V3G7 DTZ22IZ8oosOuq+lj6kug5K0+EImqahPps6wD/3bAF/vyYvsmyyHPZc54iDaFhDgt8K1 ccFw== X-Gm-Message-State: AOAM5323SFsDVhHSaftxesb1Q7T3lxX+1rYGUrm83qMkVGwdKDTGWFTj 5Q1E2xYEmD3iRMXHg/ptgz0= X-Google-Smtp-Source: ABdhPJxha/ojLGxPJlVi5mIvo10X3N6L88YhEPrhGP6ORZMSs13NiUwDEcjTzyUj87nq0P/gpWD6ew== X-Received: by 2002:a63:8543:: with SMTP id u64mr154338pgd.474.1642045356948; Wed, 12 Jan 2022 19:42:36 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.48]) by smtp.gmail.com with ESMTPSA id c20sm879436pgk.75.2022.01.12.19.42.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jan 2022 19:42:36 -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, peff@peff.net, tenglong.tl@alibaba-inc.com, martin.agren@gmail.com Subject: [PATCH v10 7/9] ls-tree.c: introduce struct "show_tree_data" Date: Thu, 13 Jan 2022 11:42:10 +0800 Message-Id: <3ddffa10278b5814123a5bcd2fd3a531def3ecdc.1641978175.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82.dirty 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 | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index e1a2f8225b..56cc166adb 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 @@ -108,17 +116,15 @@ static enum object_type get_type(unsigned int mode) : OBJ_BLOB); } -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), @@ -126,18 +132,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; } @@ -148,6 +154,14 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, size_t baselen; enum object_type type = get_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)) @@ -171,7 +185,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 Thu Jan 13 03:42:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12712199 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 31C94C433EF for ; Thu, 13 Jan 2022 03:42:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232283AbiAMDms (ORCPT ); Wed, 12 Jan 2022 22:42:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232282AbiAMDmo (ORCPT ); Wed, 12 Jan 2022 22:42:44 -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 24EC1C061245 for ; Wed, 12 Jan 2022 19:42:40 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id y16-20020a17090a6c9000b001b13ffaa625so16651419pjj.2 for ; Wed, 12 Jan 2022 19:42:40 -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=WP/rVHesd0tLJ7fuvItSuZM2Y6Efr8+wRCkMN++hhKQ=; b=PQcPJbHWa9ZtDmNj+SNb/J3OydxtbAtAq6yH/rojcHa7Qfmi1cRZBvWZNzEC29z6zg QhjEf+X63m3sEuRWN59wBTB/U+kJlODQBwEo4o3Ut81bovxylxfbS/2nCVD2qleHyGaa sRizNW2ZGmTo5KrbPKBxy9cLewLjX2YtiLvXbp18FtY1f66PcWQOyjtyhbXWIuAnXHbL yjEnw4Exe5ZU+E0qOVrsZWJItVy99yyjPMlF7vGimZptLCxXXaZ812R/UPmhP0e3pfK1 tKZSB8b65LsHcKbSgdOt0hhv1wi0c3hRa3N6LLFm1GfOHWH/E+Qi3BKI9p8xcEnz5oCl awAQ== 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=WP/rVHesd0tLJ7fuvItSuZM2Y6Efr8+wRCkMN++hhKQ=; b=6iFHfVOaRP56rPgM0J+s2SVH1ZNn5keDNrYEv9tZ/QAVQPUAcGTZzzUDZa4RC6y0nb MO4crdETM1rwCXOCpM1Aj3FXAWHNzw2bNqkkmeo1mhz42MR92rzEBbg5LOCt6/d5lRPN ZGcv+GLLiV7AZPLMyfsYLVaZPdRjUQYeSojEpyjJFt3UDEMguatvzRAsvUyElvWn38+7 rxjQlulLqEENvjLhnBplzvgydrrYw78KA053bHXyub1+ucJrbuhy/OOBAqbAwbO/Sn3X JV+oLv7OgpEutrQuPw7z7JoPyKSL+CYUmh+nEJJrpY5oTel49KjRWGN9zqeisEnAu8gr Ph0A== X-Gm-Message-State: AOAM530qZRMavz6ABFIfHJNB8sUknXm/fn2x9nQXWXCqpmaL6BHwNq+E OTf8AO7axfOG3218ttK/BKhk+q7OG7PiybjTSYOzZg== X-Google-Smtp-Source: ABdhPJxoBkPDj3qbMpMWV+1h5X4SMzmMM920rFY8iX9deNwlTo2QkuQ97H6LZ8Y00USbLJvZeOZ5Hw== X-Received: by 2002:a17:902:9a0a:b0:149:7da0:af27 with SMTP id v10-20020a1709029a0a00b001497da0af27mr2792928plp.49.1642045359664; Wed, 12 Jan 2022 19:42:39 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.48]) by smtp.gmail.com with ESMTPSA id c20sm879436pgk.75.2022.01.12.19.42.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jan 2022 19:42:39 -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, peff@peff.net, tenglong.tl@alibaba-inc.com, martin.agren@gmail.com, Johannes Schindelin Subject: [PATCH v10 8/9] cocci: allow padding with `strbuf_addf()` Date: Thu, 13 Jan 2022 11:42:11 +0800 Message-Id: <4b58a707c2913da3e2c39c1ffc09b08dfd98737e.1641978175.git.dyroneteng@gmail.com> X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82.dirty 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: 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 Thu Jan 13 03:42:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Teng Long X-Patchwork-Id: 12712200 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 96947C433F5 for ; Thu, 13 Jan 2022 03:43:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232267AbiAMDmt (ORCPT ); Wed, 12 Jan 2022 22:42:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232291AbiAMDmp (ORCPT ); Wed, 12 Jan 2022 22:42:45 -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 D5E9CC06118C for ; Wed, 12 Jan 2022 19:42:42 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id hv15so8854802pjb.5 for ; Wed, 12 Jan 2022 19:42:42 -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=RK2c8uYaNoTYvZDHcz4Uhf5/ur9geWCxA6BtH6iBEkA=; b=CoAtzA5k2QBEaRx/Lp5nia/owIlIZaAi1Mqmumq65IQGWFrOtoJBdORR6xwleNUpbj xpdIg5akQPItLagPQMuZ4t1usDemthouj/NB0Mtxk+97cp75xlKrTX7L/O3rCK7ihDKP V0vmy/Vc3mKIqeHxL3P1VvChW03AjcVVua78tmO2J1Zpczc2RvwJoSlkI9S7Ig83W3Kl I5wtNaEPYb26hEQlt5S8MRdt6h5xMy/1Lo4gFziErPGDTBd1FkVT2EYkzUpUNU5YbxJj vbLFzdkrZM+lEMFz9JQFHIRO8odnmZSqbhenQm/aZfiarj1klbMfhEYDOmuBc2rK8DzC yvuw== 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=RK2c8uYaNoTYvZDHcz4Uhf5/ur9geWCxA6BtH6iBEkA=; b=5P8cSiRqyU3J4LCJFneilzMWBG3ArYSzLNWangCxny4iBDJY93O3z5n1kBLhSpDRM5 q8VlWA/ou0fg2qRbz5jwJF5FeOgdiDC8kmt+lTnoamptMwItqauh9ixe7creIRcTXfzE ohnmv2CeIEZh+V1vF4hguDG7Lq1oFNjj00th/K8kH9dygaAORenrb4tGNFD9PdTSVaYI q+zk0BHpKGW5EwTUoVZ30BF/Hztmsprkxc2XNKa6Sbu3/Ff6j7DNNUGc4NzxjAsjZbmH lmxG3bh7OHy8RTo2Y8cc0X4FcP5Mn+1UBqmxJFYUXih3pNXKz7/AUZgyTv0ot5cK1WCv w2fA== X-Gm-Message-State: AOAM533Q0u/+GgP6d4imFkqEz1jGMewnKaPEzbR4f4YqXLo5hKQATpF0 sAeU2jmee7JQ0z4P0MNhjWw= X-Google-Smtp-Source: ABdhPJzgKig8nr+Elmc/69LaQY+7aomcQPwgpnGbkYmWu80bCg+b9I2w4fXRYd8QOE3v3g+UTGWilw== X-Received: by 2002:a17:902:ced2:b0:14a:699e:3ab3 with SMTP id d18-20020a170902ced200b0014a699e3ab3mr2611412plg.0.1642045362360; Wed, 12 Jan 2022 19:42:42 -0800 (PST) Received: from code-infra-dev-cbj.ea134 ([140.205.70.48]) by smtp.gmail.com with ESMTPSA id c20sm879436pgk.75.2022.01.12.19.42.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jan 2022 19:42:41 -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, peff@peff.net, tenglong.tl@alibaba-inc.com, martin.agren@gmail.com Subject: [PATCH v10 9/9] ls-tree.c: introduce "--format" option Date: Thu, 13 Jan 2022 11:42:12 +0800 Message-Id: X-Mailer: git-send-email 2.34.1.390.g2ae0a9cb82.dirty 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 | 51 +++++++++++++- builtin/ls-tree.c | 129 +++++++++++++++++++++++++++++++++- t/t3105-ls-tree-format.sh | 55 +++++++++++++++ 3 files changed, 230 insertions(+), 5 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..ebdde6eae3 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,38 @@ 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 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 56cc166adb..e048a68ee0 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) { @@ -116,6 +188,38 @@ static enum object_type get_type(unsigned int mode) : OBJ_BLOB); } +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 line = STRBUF_INIT; + enum object_type type = get_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(&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 recurse; +} + static int show_default(struct show_tree_data *data) { size_t baselen = data->base->len; @@ -195,6 +299,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), @@ -217,6 +322,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() }; @@ -237,6 +345,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)) @@ -260,6 +372,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..ea0f51d866 --- /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 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_code 129 git ls-tree --format=fmt --object-only HEAD +' + +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