From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS53758 23.128.96.0/24 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by dcvr.yhbt.net (Postfix) with ESMTP id 623751F8C7 for ; Wed, 14 Jul 2021 13:14:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239484AbhGNNPv (ORCPT ); Wed, 14 Jul 2021 09:15:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239418AbhGNNPr (ORCPT ); Wed, 14 Jul 2021 09:15:47 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 187B0C061762 for ; Wed, 14 Jul 2021 06:12:53 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id b14-20020a1c1b0e0000b02901fc3a62af78so3872503wmb.3 for ; Wed, 14 Jul 2021 06:12:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=ety81XM5lUpz7BNNk0XX/4/Cau8dRM1r2U9XdOAMldo=; b=K3qHvab85FD0Aik7ha3TGD+tkEfvHEFISeLY8WSVdsU3GWpNROfmtD94koys7QxWRE Ub88ail4iqxtvNDRQ7MTa533KyeXF8DAj9ri3UR5ciZId9O6sirSLUv4AkBf54wj8A7s qBcyB6cxPc6F38FEhQsDN++0VmO0WNaRms46uqCSYttoM6nvPa/kU2Xq7fPkJyE6uwPb y/9/SXjL5rJCEgk9ugYZDf+AANep2tQV3EIxmHiNO5LIhOpvykIWmzijvRjkPfj0EWRl rEYFpnnTmyIwx7P4YHIXcnlynk8L95Va6xruWuRW0Ggjz4q0eCPFXGrYIunhwe5wYQ/i IP+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=ety81XM5lUpz7BNNk0XX/4/Cau8dRM1r2U9XdOAMldo=; b=MMgKknvx8XyBwkp+AhTqnSdAwJLbXa7kTG4LLRPRqAl2YIdZyOM0cJv6//27H/E/I2 30rBa0B6MqpbUo83rCIqLXEQPkoV59RBsANszcBm/ZdjTdAb6ecIYAMooKQR/+LT4tg2 XdDCy67nmEYqitQl3BInJj75S7zE+7/cCJfVRHsV4n/wfVCHlZqm+6aleA/iPyOuHlhb p7kN0ZCmAzJKXQEsLnIkgP8sjt61gf2/xJW6wfFFpqHwFAd6MxpXi2lbmcR4nE3XZj0d jMJLZM0o/1JXwyPg+teDEUCO2eRVuN7TrwlEynntKxOv6D0zI9H83/jkH9JNn1W5B9T7 MGIA== X-Gm-Message-State: AOAM531Ruh7rGWV3tqK9K9lwAgevKypICn4lIzTnbgwvjWPVANjQRBwH gOqC0pHnnzjqWCTTa9pblnLesnoI37E= X-Google-Smtp-Source: ABdhPJwe2/KSLVqYT7wbXXZhDmgxnpvbDvQ4dLW4QYfKT1kLKC8SFg400ojv3rkchNlP3dcpjrakAQ== X-Received: by 2002:a7b:cb13:: with SMTP id u19mr10945948wmj.122.1626268371664; Wed, 14 Jul 2021 06:12:51 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f12sm5013440wmg.16.2021.07.14.06.12.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 06:12:51 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Derrick Stolee via GitGitGadget" Date: Wed, 14 Jul 2021 13:12:34 +0000 Subject: [PATCH v9 10/16] dir.c: accept a directory as part of cone-mode patterns Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MIME-Version: 1.0 To: git@vger.kernel.org Cc: gitster@pobox.com, newren@gmail.com, Matheus Tavares Bernardino , Derrick Stolee , git@jeffhostetler.com, johannes.schindelin@gmx.de, Bagas Sanjaya , Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee When we have sparse directory entries in the index, we want to compare that directory against sparse-checkout patterns. Those pattern matching algorithms are built expecting a file path, not a directory path. This is especially important in the "cone mode" patterns which will match files that exist within the "parent directories" as well as the recursive directory matches. If path_matches_pattern_list() is given a directory, we can add a fake filename ("-") to the directory and get the same results as before, assuming we are in cone mode. Since sparse index requires cone mode patterns, this is an acceptable assumption. Reviewed-by: Elijah Newren Signed-off-by: Derrick Stolee --- dir.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/dir.c b/dir.c index ebe5ec046e0..0c5264b3b20 100644 --- a/dir.c +++ b/dir.c @@ -1376,7 +1376,7 @@ enum pattern_match_result path_matches_pattern_list( struct path_pattern *pattern; struct strbuf parent_pathname = STRBUF_INIT; int result = NOT_MATCHED; - const char *slash_pos; + size_t slash_pos; if (!pl->use_cone_patterns) { pattern = last_matching_pattern_from_list(pathname, pathlen, basename, @@ -1397,21 +1397,35 @@ enum pattern_match_result path_matches_pattern_list( strbuf_addch(&parent_pathname, '/'); strbuf_add(&parent_pathname, pathname, pathlen); + /* + * Directory entries are matched if and only if a file + * contained immediately within them is matched. For the + * case of a directory entry, modify the path to create + * a fake filename within this directory, allowing us to + * use the file-base matching logic in an equivalent way. + */ + if (parent_pathname.len > 0 && + parent_pathname.buf[parent_pathname.len - 1] == '/') { + slash_pos = parent_pathname.len - 1; + strbuf_add(&parent_pathname, "-", 1); + } else { + const char *slash_ptr = strrchr(parent_pathname.buf, '/'); + slash_pos = slash_ptr ? slash_ptr - parent_pathname.buf : 0; + } + if (hashmap_contains_path(&pl->recursive_hashmap, &parent_pathname)) { result = MATCHED_RECURSIVE; goto done; } - slash_pos = strrchr(parent_pathname.buf, '/'); - - if (slash_pos == parent_pathname.buf) { + if (!slash_pos) { /* include every file in root */ result = MATCHED; goto done; } - strbuf_setlen(&parent_pathname, slash_pos - parent_pathname.buf); + strbuf_setlen(&parent_pathname, slash_pos); if (hashmap_contains_path(&pl->parent_hashmap, &parent_pathname)) { result = MATCHED; -- gitgitgadget