From patchwork Thu Jun 23 11:41:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12892133 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 A919FC43334 for ; Thu, 23 Jun 2022 11:41:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231277AbiFWLlz (ORCPT ); Thu, 23 Jun 2022 07:41:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbiFWLls (ORCPT ); Thu, 23 Jun 2022 07:41:48 -0400 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 30EED4BFEF for ; Thu, 23 Jun 2022 04:41:46 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id jb13so4341292plb.9 for ; Thu, 23 Jun 2022 04:41:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NyBk/n5Xb1g9Y6Zvz48uQ/XmYg2QSFrTAGliHe5zU3k=; b=MCDGLmys9dmi+KHIFLV0OJTUnzyadPCGrreipMw2LU6Gr695Rjt+FYic8Hu7evBNT6 byTDJdWOG5FsnM0mtGAFxbkTUkQFc7JJpZ9/rdk7MUe9WDJKLUPe+DSxdl0uwEjmWR/x e25kiGkOpGTCM5HTtT1bqdxc60bCVFZ/JbheNagAvVHtIM0jEzHDQcC1xu3yhwyAE0y9 rsQEV5L6yVwEWepaVRaxCQvA0rcwlYalRSp/EgFPVnOIHf0GIdLA05Nv2nFTyPYCNYlo HEQ/fhXPHdWwbIi1BaJs11IfpTEk+v3ZeuyGDc3CH6V9Drec+zHj6PgjJx06sty+g1PP Cc9A== 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=NyBk/n5Xb1g9Y6Zvz48uQ/XmYg2QSFrTAGliHe5zU3k=; b=KeOjtl8XDCe0UGwZEzOiFAxwRr01R2lB/Myy9nygo0Sr8OmIRNqa+xEWUegY4L7dsV 3vmMkU4SiMeRTs/ChC+gQgij8AoKVKgzFTnHsV+LmPYbosfC3lgPIPkiTDBqWS4pq6q0 C89luB2N4PJUy2LSbhxiWkYqvZXGDwKHuCwKhiuJmJzGP/YoPtNykc5LBWUxHwt1aa0U gHzIXzEgxQpTKiKdXCPQFMtb5zncT9CQ2HWQRAdrur1a07XrdWE5QImlPkPcfNatfgu4 rsdEmG4g0E9ym8bb+qGaVEO7Ytuao3j3fjgLt93ffFpH8RfdfJeJ4K0k3SDE66G1As81 4+ag== X-Gm-Message-State: AJIora/Jr+oO5MmelE6X+gFpxutHFAhkCJ+LmMciT+/7tShHR17V1pla 8w3HDgWssHUyx1xxWf8yk9jXhTNkYq5H1zhT X-Google-Smtp-Source: AGRyM1vZr3zIEDql97Q4JRGk2Yxfk3yC/CNpU8fBHBjOZuqqWwCS6VBcLV3S+TJIuwiJnmGMu9RVmQ== X-Received: by 2002:a17:90b:4b48:b0:1ec:fc87:691c with SMTP id mi8-20020a17090b4b4800b001ecfc87691cmr3600011pjb.120.1655984505652; Thu, 23 Jun 2022 04:41:45 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.146.236]) by smtp.gmail.com with ESMTPSA id j7-20020a17090a7e8700b001ec4f258028sm1629299pjl.55.2022.06.23.04.41.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 04:41:45 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com Subject: [PATCH v4 1/7] t7002: add tests for moving out-of-cone file/directory Date: Thu, 23 Jun 2022 19:41:14 +0800 Message-Id: <20220623114120.12768-2-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add corresponding tests to test following situations: We do not have sufficient coverage of moving files outside of a sparse-checkout cone. Create new tests covering this behavior, keeping in mind that the user can include --sparse (or not), move a file or directory, and the destination can already exist in the index (in this case user can use --force to overwrite existing entry). Helped-by: Victoria Dye Helped-by: Derrick Stolee Signed-off-by: Shaoxuan Yuan --- t/t7002-mv-sparse-checkout.sh | 84 +++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh index f0f7cbfcdb..023e657c9e 100755 --- a/t/t7002-mv-sparse-checkout.sh +++ b/t/t7002-mv-sparse-checkout.sh @@ -4,6 +4,18 @@ test_description='git mv in sparse working trees' . ./test-lib.sh +setup_sparse_checkout () { + mkdir folder1 && + touch folder1/file1 && + git add folder1 && + git sparse-checkout set --cone sub +} + +cleanup_sparse_checkout () { + git sparse-checkout disable && + git reset --hard +} + test_expect_success 'setup' " mkdir -p sub/dir sub/dir2 && touch a b c sub/d sub/dir/e sub/dir2/e && @@ -196,6 +208,7 @@ test_expect_success 'can move files to non-sparse dir' ' ' test_expect_success 'refuse to move file to non-skip-worktree sparse path' ' + test_when_finished "cleanup_sparse_checkout" && git reset --hard && git sparse-checkout init --no-cone && git sparse-checkout set a !/x y/ !x/y/z && @@ -206,4 +219,75 @@ test_expect_success 'refuse to move file to non-skip-worktree sparse path' ' test_cmp expect stderr ' +test_expect_failure 'refuse to move out-of-cone directory without --sparse' ' + test_when_finished "cleanup_sparse_checkout" && + setup_sparse_checkout && + + test_must_fail git mv folder1 sub 2>stderr && + cat sparse_error_header >expect && + echo folder1/file1 >>expect && + cat sparse_hint >>expect && + test_cmp expect stderr +' + +test_expect_failure 'can move out-of-cone directory with --sparse' ' + test_when_finished "cleanup_sparse_checkout" && + setup_sparse_checkout && + + git mv --sparse folder1 sub 2>stderr && + test_must_be_empty stderr && + + test_path_is_dir sub/folder1 && + test_path_is_file sub/folder1/file1 +' + +test_expect_failure 'refuse to move out-of-cone file without --sparse' ' + test_when_finished "cleanup_sparse_checkout" && + setup_sparse_checkout && + + test_must_fail git mv folder1/file1 sub 2>stderr && + cat sparse_error_header >expect && + echo folder1/file1 >>expect && + cat sparse_hint >>expect && + test_cmp expect stderr +' + +test_expect_failure 'can move out-of-cone file with --sparse' ' + test_when_finished "cleanup_sparse_checkout" && + setup_sparse_checkout && + + git mv --sparse folder1/file1 sub 2>stderr && + test_must_be_empty stderr && + + test_path_is_file sub/file1 +' + +test_expect_failure 'refuse to move sparse file to existing destination' ' + test_when_finished "cleanup_sparse_checkout" && + mkdir folder1 && + touch folder1/file1 && + touch sub/file1 && + git add folder1 sub/file1 && + git sparse-checkout set --cone sub && + + test_must_fail git mv --sparse folder1/file1 sub 2>stderr && + echo "fatal: destination exists, source=folder1/file1, destination=sub/file1" >expect && + test_cmp expect stderr +' + +test_expect_failure 'move sparse file to existing destination with --force and --sparse' ' + test_when_finished "cleanup_sparse_checkout" && + mkdir folder1 && + touch folder1/file1 && + touch sub/file1 && + echo "overwrite" >folder1/file1 && + git add folder1 sub/file1 && + git sparse-checkout set --cone sub && + + git mv --sparse --force folder1/file1 sub 2>stderr && + test_must_be_empty stderr && + echo "overwrite" >expect && + test_cmp expect sub/file1 +' + test_done From patchwork Thu Jun 23 11:41:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12892134 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 C53DBC433EF for ; Thu, 23 Jun 2022 11:41:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230300AbiFWLl5 (ORCPT ); Thu, 23 Jun 2022 07:41:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230169AbiFWLls (ORCPT ); Thu, 23 Jun 2022 07:41:48 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF8914BFF3 for ; Thu, 23 Jun 2022 04:41:47 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id i64so18851757pfc.8 for ; Thu, 23 Jun 2022 04:41:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9iaetEQDKHbfqnoZNuD5o6iw6SbIV5GTRxdHSt2XRlA=; b=RMzxk2s3BJ75qeHBdduQkfX6/9X9E3PRaN3RCQwn7VM6sPv0Mjv91wgKnhg7Pb8DGL IiPO1aZEibVRjpfuL4dOzvrEd1En4PlZw1TNnLhLsr6NL0zjqmLcpffrUdANdm962dGK Sa/ZgYC21EiSeR9oe2B6tbHz3tnJd+gT08ZygyDzH8+eRPr5fnLCwPTps6ADaX/0CyM7 y8HJuDh0DniLYBiFqBg11vdamMRxpr0XKXDdDnjReQ1qlkpvbLVJpS2S6Fr1e2WhNIly Eyg7Ff9EEIfGFtJgsipDwO2MJ5L/6/iXiwvC65xbZqhU1J6WvveDLoj5SF5AKoT0Imtt eCvw== 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=9iaetEQDKHbfqnoZNuD5o6iw6SbIV5GTRxdHSt2XRlA=; b=zUXzfJwk3Tp6JLl8ISp0W6/nk5Px709s5WQ5wk+PvgJINdiOztL5SkUHdY7N2hwgoT fHCG9xOTgW3oq/J9zKlSOC+U/7Pxf5xsug6urYHXrdP9Q80/AJk0+a3rCe16h2zoduge Tn8mEtHeuvo5Qr23B4a+IC5x5Lj4ta84tgSWwPp2fI5LwRl9VW3geWsSzh9M67sdD7DY cGy4UNm07WMbUJfTWvfolkpdlO+jSJRatMnZtRZ7sxANS78VCGQ4grKxuMnh906OvePv qUGuRpYrV3882erKUcFQqKc0P0p/LPU/TMDl3w+ku9121090ZenMmOiY2fHOc7oof0o/ XqdA== X-Gm-Message-State: AJIora9TsuSs/PA2mkRvrrJntiwHGdvqkDN/S7v+lUmxkpGBiBvTOgBg kfRjB6VuYjx/7ww6vlZADdc= X-Google-Smtp-Source: AGRyM1u2qOp4AieNlE69/qQxI2S601YlCtkLYM/VOUtd4bDyW+ZLIOyjE6UEz0gf9WlS3lfhcqvIsg== X-Received: by 2002:a63:4c1d:0:b0:3fd:9833:cdd9 with SMTP id z29-20020a634c1d000000b003fd9833cdd9mr7385244pga.103.1655984507552; Thu, 23 Jun 2022 04:41:47 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.146.236]) by smtp.gmail.com with ESMTPSA id j7-20020a17090a7e8700b001ec4f258028sm1629299pjl.55.2022.06.23.04.41.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 04:41:47 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com Subject: [PATCH v4 2/7] mv: update sparsity after moving from out-of-cone to in-cone Date: Thu, 23 Jun 2022 19:41:15 +0800 Message-Id: <20220623114120.12768-3-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Originally, "git mv" a sparse file from out-of-cone to in-cone does not update the moved file's sparsity (remove its SKIP_WORKTREE bit). And the corresponding cache entry is, unexpectedly, not checked out in the working tree. Update the behavior so that: 1. Moving from out-of-cone to in-cone removes the SKIP_WORKTREE bit from corresponding cache entry. 2. The moved cache entry is checked out in the working tree to reflect the updated sparsity. Helped-by: Victoria Dye Signed-off-by: Shaoxuan Yuan --- builtin/mv.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/builtin/mv.c b/builtin/mv.c index 83a465ba83..0c0c2b4914 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -13,6 +13,7 @@ #include "string-list.h" #include "parse-options.h" #include "submodule.h" +#include "entry.h" static const char * const builtin_mv_usage[] = { N_("git mv [] ... "), @@ -304,6 +305,11 @@ int cmd_mv(int argc, const char **argv, const char *prefix) const char *src = source[i], *dst = destination[i]; enum update_mode mode = modes[i]; int pos; + struct checkout state = CHECKOUT_INIT; + state.istate = &the_index; + + if (force) + state.force = 1; if (show_only || verbose) printf(_("Renaming %s to %s\n"), src, dst); if (show_only) @@ -328,6 +334,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix) pos = cache_name_pos(src, strlen(src)); assert(pos >= 0); rename_cache_entry_at(pos, dst); + + if ((mode & SPARSE) && + (path_in_sparse_checkout(dst, &the_index))) { + int dst_pos; + + dst_pos = cache_name_pos(dst, strlen(dst)); + active_cache[dst_pos]->ce_flags &= ~CE_SKIP_WORKTREE; + + if (checkout_entry(active_cache[dst_pos], &state, NULL, NULL)) + die(_("cannot checkout %s"), ce->name); + } } if (gitmodules_modified) From patchwork Thu Jun 23 11:41:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12892135 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 6B09FC43334 for ; Thu, 23 Jun 2022 11:41:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231417AbiFWLl6 (ORCPT ); Thu, 23 Jun 2022 07:41:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230391AbiFWLlx (ORCPT ); Thu, 23 Jun 2022 07:41:53 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F087F4BFF6 for ; Thu, 23 Jun 2022 04:41:49 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id 73-20020a17090a0fcf00b001eaee69f600so2308494pjz.1 for ; Thu, 23 Jun 2022 04:41:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ftgkv7iHtsvvAMKFX/nkifg/+bFRjLAh4IzF23ubEqk=; b=MY/RrkajiXvvxdIT38bpAc8sVM8nwiZ0MaUJSvmUv+XHyCI9BqFr/2ZWbSe2JBDNbw ACVPco35eP2pP7MrLmHkNMUESeO9z/hnnkv1JAPzJ/+xCanOUTk9bE3d5JataCPGhBh9 y0eaVBdRrFbOx5yNwBGaFMB9585Pw5q9lY8lvFE8IF9hCw1VKcGozogK22r3K2eWcgYb 1yoG1Vvzd+wx+UxqinEctKquDCnw33aZSVGcW3aO2AwANeL7AU27Rj8xctMfsC9nYCN6 p0lGQ6yLf+PxvCsczFRWBIucTgutqH6iBiYAbOeEIEe0ibiTSedtVS14qgHA/Vh1Qjua bJiA== 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=Ftgkv7iHtsvvAMKFX/nkifg/+bFRjLAh4IzF23ubEqk=; b=MvHJbawZejIQUEPSvUZXentB/O1F/9KOm5l16iXmwe9N+lmI/WZSyd0x4TIb4H8J/p ruNv2d1DP1gCuk+jtdwzfhqWdcb3hxrsgyqJnF0acPUKFZPE9kKMNLpBsVK58KFMtoBs a2lzbkwvx87GNeNU79ifALyvrcuw9BtyCOI0xP6MEfknE3VIUZo6Y6x0u2nmmeZ95xXl oSF+RPrygcJ1Q8xeDF7DAFJAlnVC/jL2C9lbQuZ0IpPwoXtW+nD/QgVzBvMu48xG2cWt p6W8F6unNYQEc5mB/wiuJnyc0QVW4AjgMY0kGOg26nF7Q9/UqLbGp5PNppmNcgv3vZfk dYuw== X-Gm-Message-State: AJIora+uAKF0e7oy0FhScho5El8hZqHpDkkXN7afmLsPrnw5ki9meDzT CUGHKCYQ0LDPnTm+Tme5/Bs= X-Google-Smtp-Source: AGRyM1sH3QNCiwzT0VcpTmBRvJKc2TL51cwlgwYdetT5tNoRqSG9QjEaTQ76+FLU6vqxhyyYy0QANg== X-Received: by 2002:a17:90a:c244:b0:1ec:d65f:e1c7 with SMTP id d4-20020a17090ac24400b001ecd65fe1c7mr3636792pjx.15.1655984509437; Thu, 23 Jun 2022 04:41:49 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.146.236]) by smtp.gmail.com with ESMTPSA id j7-20020a17090a7e8700b001ec4f258028sm1629299pjl.55.2022.06.23.04.41.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 04:41:49 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com Subject: [PATCH v4 3/7] mv: decouple if/else-if checks using goto Date: Thu, 23 Jun 2022 19:41:16 +0800 Message-Id: <20220623114120.12768-4-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Previous if/else-if chain are highly nested and hard to develop/extend. Refactor to decouple this if/else-if chain by using goto to jump ahead. Suggested-by: Derrick Stolee Signed-off-by: Shaoxuan Yuan --- builtin/mv.c | 139 +++++++++++++++++++++++++++++---------------------- 1 file changed, 80 insertions(+), 59 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 0c0c2b4914..7f8d658028 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -187,53 +187,68 @@ int cmd_mv(int argc, const char **argv, const char *prefix) length = strlen(src); if (lstat(src, &st) < 0) { /* only error if existence is expected. */ - if (modes[i] != SPARSE) + if (modes[i] != SPARSE) { bad = _("bad source"); - } else if (!strncmp(src, dst, length) && - (dst[length] == 0 || dst[length] == '/')) { + goto act_on_entry; + } + } + if (!strncmp(src, dst, length) && + (dst[length] == 0 || dst[length] == '/')) { bad = _("can not move directory into itself"); - } else if ((src_is_dir = S_ISDIR(st.st_mode)) - && lstat(dst, &st) == 0) + goto act_on_entry; + } + if ((src_is_dir = S_ISDIR(st.st_mode)) + && lstat(dst, &st) == 0) { bad = _("cannot move directory over file"); - else if (src_is_dir) { + goto act_on_entry; + } + if (src_is_dir) { + int j, dst_len, n; int first = cache_name_pos(src, length), last; - if (first >= 0) + if (first >= 0) { prepare_move_submodule(src, first, submodule_gitfile + i); - else if (index_range_of_same_dir(src, length, - &first, &last) < 1) + goto act_on_entry; + } else if (index_range_of_same_dir(src, length, + &first, &last) < 1) { bad = _("source directory is empty"); - else { /* last - first >= 1 */ - int j, dst_len, n; - - modes[i] = WORKING_DIRECTORY; - n = argc + last - first; - REALLOC_ARRAY(source, n); - REALLOC_ARRAY(destination, n); - REALLOC_ARRAY(modes, n); - REALLOC_ARRAY(submodule_gitfile, n); - - dst = add_slash(dst); - dst_len = strlen(dst); - - for (j = 0; j < last - first; j++) { - const struct cache_entry *ce = active_cache[first + j]; - const char *path = ce->name; - source[argc + j] = path; - destination[argc + j] = - prefix_path(dst, dst_len, path + length + 1); - modes[argc + j] = ce_skip_worktree(ce) ? SPARSE : INDEX; - submodule_gitfile[argc + j] = NULL; - } - argc += last - first; + goto act_on_entry; } - } else if (!(ce = cache_file_exists(src, length, 0))) { + + /* last - first >= 1 */ + modes[i] = WORKING_DIRECTORY; + n = argc + last - first; + REALLOC_ARRAY(source, n); + REALLOC_ARRAY(destination, n); + REALLOC_ARRAY(modes, n); + REALLOC_ARRAY(submodule_gitfile, n); + + dst = add_slash(dst); + dst_len = strlen(dst); + + for (j = 0; j < last - first; j++) { + const struct cache_entry *ce = active_cache[first + j]; + const char *path = ce->name; + source[argc + j] = path; + destination[argc + j] = + prefix_path(dst, dst_len, path + length + 1); + modes[argc + j] = ce_skip_worktree(ce) ? SPARSE : INDEX; + submodule_gitfile[argc + j] = NULL; + } + argc += last - first; + goto act_on_entry; + } + if (!(ce = cache_file_exists(src, length, 0))) { bad = _("not under version control"); - } else if (ce_stage(ce)) { + goto act_on_entry; + } + if (ce_stage(ce)) { bad = _("conflicted"); - } else if (lstat(dst, &st) == 0 && - (!ignore_case || strcasecmp(src, dst))) { + goto act_on_entry; + } + if (lstat(dst, &st) == 0 && + (!ignore_case || strcasecmp(src, dst))) { bad = _("destination exists"); if (force) { /* @@ -247,34 +262,40 @@ int cmd_mv(int argc, const char **argv, const char *prefix) } else bad = _("Cannot overwrite"); } - } else if (string_list_has_string(&src_for_dst, dst)) + goto act_on_entry; + } + if (string_list_has_string(&src_for_dst, dst)) { bad = _("multiple sources for the same target"); - else if (is_dir_sep(dst[strlen(dst) - 1])) + goto act_on_entry; + } + if (is_dir_sep(dst[strlen(dst) - 1])) { bad = _("destination directory does not exist"); - else { - /* - * We check if the paths are in the sparse-checkout - * definition as a very final check, since that - * allows us to point the user to the --sparse - * option as a way to have a successful run. - */ - if (!ignore_sparse && - !path_in_sparse_checkout(src, &the_index)) { - string_list_append(&only_match_skip_worktree, src); - skip_sparse = 1; - } - if (!ignore_sparse && - !path_in_sparse_checkout(dst, &the_index)) { - string_list_append(&only_match_skip_worktree, dst); - skip_sparse = 1; - } - - if (skip_sparse) - goto remove_entry; + goto act_on_entry; + } - string_list_insert(&src_for_dst, dst); + /* + * We check if the paths are in the sparse-checkout + * definition as a very final check, since that + * allows us to point the user to the --sparse + * option as a way to have a successful run. + */ + if (!ignore_sparse && + !path_in_sparse_checkout(src, &the_index)) { + string_list_append(&only_match_skip_worktree, src); + skip_sparse = 1; + } + if (!ignore_sparse && + !path_in_sparse_checkout(dst, &the_index)) { + string_list_append(&only_match_skip_worktree, dst); + skip_sparse = 1; } + if (skip_sparse) + goto remove_entry; + + string_list_insert(&src_for_dst, dst); + +act_on_entry: if (!bad) continue; if (!ignore_errors) From patchwork Thu Jun 23 11:41:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12892136 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 057E2CCA47C for ; Thu, 23 Jun 2022 11:42:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231329AbiFWLmA (ORCPT ); Thu, 23 Jun 2022 07:42:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231246AbiFWLlz (ORCPT ); Thu, 23 Jun 2022 07:41:55 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FDFB4BFF9 for ; Thu, 23 Jun 2022 04:41:51 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id 73-20020a17090a0fcf00b001eaee69f600so2308494pjz.1 for ; Thu, 23 Jun 2022 04:41:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NN8OhcDXR6q1XLN9NsKGEVVgFuPfGxmyPxmclVfqsys=; b=c0QqmbsF41t42P/3bMcaj91xYiEurqrV9lV7Rt3QDOgr3Q+WXhUpRmZMxR+2yw4CY3 FX0pKHwUJhJo++eMT/O+5iA6XVJTz6VDhG2+GXOkCSwbBcuXmd6avgGKSoTrVzpDtxYr AEA0naVsU8e3ggGr0Jc5p5REsZ24JVj7+Jk8sdLCkdYzFoH4rsP2P+grv21AHxYVLtzz XZN4ObtW8gn2fzeTyR5NdpZdsTLNeggy7P9KtsQHhTJFijoc/F9ptYoPEC+kMWEQYxjf KBJEz+jz6u+GozLQjCMdjKo/5z2pfawvh9g3UgmSI6meImpPNZHonl2Cfe6NX9sgp58B vChg== 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=NN8OhcDXR6q1XLN9NsKGEVVgFuPfGxmyPxmclVfqsys=; b=JWX8DCTsByNXeCWSSsVwgz+jXHn9MlsoMPdytOk3/5V/KzdPUE+Zc2B4gEVQ2C+Y1y kf5uC5mcdcVSDuYxv1RU8AV9adSeTuaqzGIc4ie7IaYqWZtcbjrG4vgmwRLttKvKaNDM NXNSZ9jP9X4eQ6JDEPwO4U8tGT7+Ror41OifDyuYP6XOLisokyqt46dm+VehLW1lTBpW HOtKmc4eyvX1EmcczJkiNS/iXIDHK16iB8v4v5ao/TdgMqwPdu8sA7Zw8sFOjojCUGAs HQwXy4G7sHabbMi/ohR65C45spO887aXbUu8T0vCDeeEWs2tWEHUzKForURugr0CCn9n aaQQ== X-Gm-Message-State: AJIora/CZXAz+c2s+UbdwrL015q3TmsaFaiRGL7l1cCY7dZBzx5Q9RpK RlTqA+pV3vZlrG3ywEeA21cq9xag2eesiGBr X-Google-Smtp-Source: AGRyM1vtixqp8xHcoS52+HGbehvi9EhyStOiqqZhFMRYgqy5SvWM2rO40+9LIkCB/FkOCU1+gmqIHw== X-Received: by 2002:a17:90b:3907:b0:1ec:9225:6a81 with SMTP id ob7-20020a17090b390700b001ec92256a81mr3645111pjb.0.1655984511321; Thu, 23 Jun 2022 04:41:51 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.146.236]) by smtp.gmail.com with ESMTPSA id j7-20020a17090a7e8700b001ec4f258028sm1629299pjl.55.2022.06.23.04.41.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 04:41:51 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com Subject: [PATCH v4 4/7] mv: check if out-of-cone file exists in index with SKIP_WORKTREE bit Date: Thu, 23 Jun 2022 19:41:17 +0800 Message-Id: <20220623114120.12768-5-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Originally, moving a file which is not on-disk but exists in index as a SKIP_WORKTREE enabled cache entry, "giv mv" command errors out with "bad source". Change the checking logic, so that such file makes "giv mv" command warns with "advise_on_updating_sparse_paths()" instead of "bad source"; also user now can supply a "--sparse" flag so this operation can be carried out successfully. Signed-off-by: Shaoxuan Yuan --- builtin/mv.c | 21 +++++++++++++++++++-- t/t7002-mv-sparse-checkout.sh | 4 ++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 7f8d658028..d1b3229be6 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -186,11 +186,28 @@ int cmd_mv(int argc, const char **argv, const char *prefix) length = strlen(src); if (lstat(src, &st) < 0) { - /* only error if existence is expected. */ - if (modes[i] != SPARSE) { + int pos; + const struct cache_entry *ce; + + pos = cache_name_pos(src, length); + if (pos < 0) { + /* only error if existence is expected. */ + if (modes[i] != SPARSE) + bad = _("bad source"); + goto act_on_entry; + } + + ce = active_cache[pos]; + if (!ce_skip_worktree(ce)) { bad = _("bad source"); goto act_on_entry; } + + if (!ignore_sparse) + string_list_append(&only_match_skip_worktree, src); + else + modes[i] = SPARSE; + goto act_on_entry; } if (!strncmp(src, dst, length) && (dst[length] == 0 || dst[length] == '/')) { diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh index 023e657c9e..1510b5ed6a 100755 --- a/t/t7002-mv-sparse-checkout.sh +++ b/t/t7002-mv-sparse-checkout.sh @@ -241,7 +241,7 @@ test_expect_failure 'can move out-of-cone directory with --sparse' ' test_path_is_file sub/folder1/file1 ' -test_expect_failure 'refuse to move out-of-cone file without --sparse' ' +test_expect_success 'refuse to move out-of-cone file without --sparse' ' test_when_finished "cleanup_sparse_checkout" && setup_sparse_checkout && @@ -252,7 +252,7 @@ test_expect_failure 'refuse to move out-of-cone file without --sparse' ' test_cmp expect stderr ' -test_expect_failure 'can move out-of-cone file with --sparse' ' +test_expect_success 'can move out-of-cone file with --sparse' ' test_when_finished "cleanup_sparse_checkout" && setup_sparse_checkout && From patchwork Thu Jun 23 11:41:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12892137 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 6DDDFC43334 for ; Thu, 23 Jun 2022 11:42:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231477AbiFWLmG (ORCPT ); Thu, 23 Jun 2022 07:42:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231374AbiFWLl5 (ORCPT ); Thu, 23 Jun 2022 07:41:57 -0400 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 AB3474BFFD for ; Thu, 23 Jun 2022 04:41:53 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id k7so17939328plg.7 for ; Thu, 23 Jun 2022 04:41:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JkEnir3dEuowaiIAGgpmBD7eo16ymK2kzWe/eJtpUy0=; b=MO+Un+nyLoGpUMuSEimuRYHF+KhTciJaXhBjPHpo7IwoE1wWzST6cHTVjN9qelvsAL aZi0RMjncnFHlQM7EeuRv8xYErYYncoxOVKbkrZSiDsQsH2VN6lQafPSyGkvXg548cLT wion0MajawY9qlNWsGn+xRbcrcNNMJrSUlzeqUbF7DZvO6+w64do+2qf/BjCNLsG//Ug YNc2Mol/BChNs1mhNr4ASPHnRA9n3lVaIWGuBp3Ny5KxcL02aM6veevpYrrMVR76OB86 vh+ziDkW4sG8x9YrUixyLXsZacNgQu2ZHhZkJC2+bTmrrvD3IMg4VxyuepnoqZngf6kw pyxQ== 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=JkEnir3dEuowaiIAGgpmBD7eo16ymK2kzWe/eJtpUy0=; b=ut9+a13CWWWGNeOSmRnfQRiq95GeBaauUBtJYG4lgdfMr/3lZWiAId85mJjT5KXuI+ kaELrou85PRF8dGsUCqnC5t+dpkvwj7JuUTkTT1zZ2FRPiM9Ter3dXuRzJBhElmWFQRp BhTyVa/oDrxG74hP/j2qblxkA7wTyYGQWYfUoISSkLAaxDqHMEJ23gylM4OU2QqF9uDz ehyK+42TRUoYFHO5slL35Yk00bnBHrVbJwiVQ6DrrJVzDCsR5SLc+1m8kDKeUJP+CdgW tCLygCdVU3oZlpEJiEPhnNv5fXRTYRIRAS+dVTmHOd207wtCk1RAJdMnzZxqSwOSmkqC oL7A== X-Gm-Message-State: AJIora+3h2Wb/CcAumXZrIUEJp2gZhfQWKetLys2vke8orR7/sqSmIOw SJr9VtW2as/JRWmQ5vWEDckD4A3HOGkdd/qC X-Google-Smtp-Source: AGRyM1sW4OdyLUx2LJ6hbHn1GUqRY+JHd/Uq9NFBrigg0M3v6VSES7SS80d/dHRLzeZbZey1ud1DCQ== X-Received: by 2002:a17:902:e5c2:b0:16a:62c7:4190 with SMTP id u2-20020a170902e5c200b0016a62c74190mr227846plf.110.1655984513234; Thu, 23 Jun 2022 04:41:53 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.146.236]) by smtp.gmail.com with ESMTPSA id j7-20020a17090a7e8700b001ec4f258028sm1629299pjl.55.2022.06.23.04.41.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 04:41:52 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com Subject: [PATCH v4 5/7] mv: check if exists in index to handle overwriting Date: Thu, 23 Jun 2022 19:41:18 +0800 Message-Id: <20220623114120.12768-6-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Originally, moving a sparse file into cone can result in unwarned overwrite of existing entry. The expected behavior is that if the exists in the entry, user should be prompted to supply a [-f|--force] to carry out the operation, or the operation should fail. Add a check mechanism to do that. Signed-off-by: Shaoxuan Yuan --- builtin/mv.c | 15 ++++++++++++--- t/t7002-mv-sparse-checkout.sh | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index d1b3229be6..40a3a5c5ff 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -202,11 +202,20 @@ int cmd_mv(int argc, const char **argv, const char *prefix) bad = _("bad source"); goto act_on_entry; } - - if (!ignore_sparse) + if (!ignore_sparse) { string_list_append(&only_match_skip_worktree, src); - else + goto act_on_entry; + } + /* Check if dst exists in index */ + if (cache_name_pos(dst, strlen(dst)) < 0) { modes[i] = SPARSE; + goto act_on_entry; + } + if (!force) { + bad = _("destination exists"); + goto act_on_entry; + } + modes[i] = SPARSE; goto act_on_entry; } if (!strncmp(src, dst, length) && diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh index 1510b5ed6a..6d2fb4f8d2 100755 --- a/t/t7002-mv-sparse-checkout.sh +++ b/t/t7002-mv-sparse-checkout.sh @@ -262,7 +262,7 @@ test_expect_success 'can move out-of-cone file with --sparse' ' test_path_is_file sub/file1 ' -test_expect_failure 'refuse to move sparse file to existing destination' ' +test_expect_success 'refuse to move sparse file to existing destination' ' test_when_finished "cleanup_sparse_checkout" && mkdir folder1 && touch folder1/file1 && @@ -275,7 +275,7 @@ test_expect_failure 'refuse to move sparse file to existing destination' ' test_cmp expect stderr ' -test_expect_failure 'move sparse file to existing destination with --force and --sparse' ' +test_expect_success 'move sparse file to existing destination with --force and --sparse' ' test_when_finished "cleanup_sparse_checkout" && mkdir folder1 && touch folder1/file1 && From patchwork Thu Jun 23 11:41:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12892138 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 E4AABC433EF for ; Thu, 23 Jun 2022 11:42:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231493AbiFWLmJ (ORCPT ); Thu, 23 Jun 2022 07:42:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231408AbiFWLl5 (ORCPT ); Thu, 23 Jun 2022 07:41:57 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8974C4CD43 for ; Thu, 23 Jun 2022 04:41:55 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id d129so18894254pgc.9 for ; Thu, 23 Jun 2022 04:41:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KEyj55fx2hqgi3jtHrN+A4isIxXz3bquoRPbZlG4MkQ=; b=ZPQIOTsDFSQvr7vvk8zeTIAHZGqYnoIOxKJDMa9AnfVIEwW4S2zcQNixITI6I+BXqF ShOm0C2swnREh1SHMQn0ntUh1GClSglD3F/jILr7IuJR0DQ88OIqKLec5yOKo8q9nQPa w5l0+fUDvhPi5OEOmoIC98Mf+wDVvOpDxOqZmwYepkCgF36Ofe4QuPXwKx2QacqSj4xL areksngKxLkLaxwc+vZgBXIVEcmOGCSLFUed/OXEPtyJrsBCNuOmofFOqxX9+ZvJJwDt wXgzpIub6X8o9fsvarT/VfuixhGywNaE/3IugiKhkJLsIFGJ4z8P+bZ93GFY3G+vkF0H H+4g== 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=KEyj55fx2hqgi3jtHrN+A4isIxXz3bquoRPbZlG4MkQ=; b=Aap1lIzoz1rmrGP+Kdx/VqLL1zxiffesPH8okftYwjgM4uVll7SZrlQnDDDHiVGDKH 9+PcUqcETZaVKTiy0MZF+ONo1KJP073rht13FubYtdJXoDLOm1Ah2vz07ki+orUvr5c+ xlfUY9XUWiqmeYjNaKzjlSQ1KKgjynu2wtubB2a5FA5CWZgwZOSf1/ZZxg6kORvTad6E 2tJ0C8HMdLUZdnfUL/4ScKgugDnyAq/RbK9JaknDXr4ZJLdKCqI4xpn4SS0qypzZ2s9L 0B3O3qrqbovt59XG2RDA56lxxtj+M+Ua147pk4gKk00yVBsvmYDiQ02LeSUrfFIVajoA lqqg== X-Gm-Message-State: AJIora8EZEUBC07Nc7bpnb+WgTKBgyRmLXR+iZxfPB10Lb2ZVlyTOjqO uBGexsP0g/MGev88lAyU0w2GYJwO8eUheQfK X-Google-Smtp-Source: AGRyM1uTSQIpKETVAbhdB4QYw70GQZn6gjb2wM4GGsc3okAkm0zJ4OJL8GzZtfanN5k2Fr4Ltua5QQ== X-Received: by 2002:a63:6bca:0:b0:408:897c:3fb8 with SMTP id g193-20020a636bca000000b00408897c3fb8mr7169586pgc.576.1655984515094; Thu, 23 Jun 2022 04:41:55 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.146.236]) by smtp.gmail.com with ESMTPSA id j7-20020a17090a7e8700b001ec4f258028sm1629299pjl.55.2022.06.23.04.41.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 04:41:54 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com Subject: [PATCH v4 6/7] mv: use flags mode for update_mode Date: Thu, 23 Jun 2022 19:41:19 +0800 Message-Id: <20220623114120.12768-7-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org As suggested by Derrick [1], move the in-line definition of "enum update_mode" to the top of the file and make it use "flags" mode (each state is a different bit in the word). Change the flag assignments from '=' (single assignment) to '|=' (additive). Also change flag evaluation from '==' to '&', etc. [1] https://lore.kernel.org/git/22aadea2-9330-aa9e-7b6a-834585189144@github.com/ Helped-by: Victoria Dye Helped-by: Derrick Stolee Signed-off-by: Shaoxuan Yuan --- builtin/mv.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 40a3a5c5ff..aa29da4337 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -20,6 +20,13 @@ static const char * const builtin_mv_usage[] = { NULL }; +enum update_mode { + BOTH = 0, + WORKING_DIRECTORY = (1 << 1), + INDEX = (1 << 2), + SPARSE = (1 << 3), +}; + #define DUP_BASENAME 1 #define KEEP_TRAILING_SLASH 2 @@ -130,7 +137,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) OPT_END(), }; const char **source, **destination, **dest_path, **submodule_gitfile; - enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX, SPARSE } *modes; + enum update_mode *modes; struct stat st; struct string_list src_for_dst = STRING_LIST_INIT_NODUP; struct lock_file lock_file = LOCK_INIT; @@ -192,7 +199,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) pos = cache_name_pos(src, length); if (pos < 0) { /* only error if existence is expected. */ - if (modes[i] != SPARSE) + if (!(modes[i] & SPARSE)) bad = _("bad source"); goto act_on_entry; } @@ -208,14 +215,14 @@ int cmd_mv(int argc, const char **argv, const char *prefix) } /* Check if dst exists in index */ if (cache_name_pos(dst, strlen(dst)) < 0) { - modes[i] = SPARSE; + modes[i] |= SPARSE; goto act_on_entry; } if (!force) { bad = _("destination exists"); goto act_on_entry; } - modes[i] = SPARSE; + modes[i] |= SPARSE; goto act_on_entry; } if (!strncmp(src, dst, length) && @@ -243,7 +250,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) } /* last - first >= 1 */ - modes[i] = WORKING_DIRECTORY; + modes[i] |= WORKING_DIRECTORY; n = argc + last - first; REALLOC_ARRAY(source, n); REALLOC_ARRAY(destination, n); @@ -259,7 +266,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix) source[argc + j] = path; destination[argc + j] = prefix_path(dst, dst_len, path + length + 1); - modes[argc + j] = ce_skip_worktree(ce) ? SPARSE : INDEX; + memset(modes + argc + j, 0, sizeof(enum update_mode)); + modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX; submodule_gitfile[argc + j] = NULL; } argc += last - first; @@ -361,7 +369,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix) printf(_("Renaming %s to %s\n"), src, dst); if (show_only) continue; - if (mode != INDEX && mode != SPARSE && rename(src, dst) < 0) { + if (!(mode & (INDEX | SPARSE)) && + rename(src, dst) < 0) { if (ignore_errors) continue; die_errno(_("renaming '%s' failed"), src); @@ -375,7 +384,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) 1); } - if (mode == WORKING_DIRECTORY) + if (mode & (WORKING_DIRECTORY)) continue; pos = cache_name_pos(src, strlen(src)); From patchwork Thu Jun 23 11:41:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12892139 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 24F21C433EF for ; Thu, 23 Jun 2022 11:42:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231446AbiFWLmP (ORCPT ); Thu, 23 Jun 2022 07:42:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231421AbiFWLl7 (ORCPT ); Thu, 23 Jun 2022 07:41:59 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 881094BFE9 for ; Thu, 23 Jun 2022 04:41:57 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id k127so13650945pfd.10 for ; Thu, 23 Jun 2022 04:41:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cGJonfRz8QCsdAKY1pQeQi7v/qw/myKwiE3nlvGM1Fc=; b=IIg5w2eodYyOuRm/WTuJuxj3cFD8TzETXuTBrli/CEn9x73wa1QorHX7rwZsMDoe6/ dP+FPN8OuCWhdhsqEnT1YnAAmjONbJnKnfPp5ydwPRRH9OMoue73HPs0zsL9glIzjRn2 OgHiUcJw9sxKxVi3bnPiFF6oktSFWgbxhL+oaTJtDuRGShXWC4dUYROT3IkmN7JgkuU3 E25MJQuUGq4m8thuAU7AB6a2c3AjVFeJrQ2Q7zCV/oPEzWJa6CUpXe9xSpIMGo1GjPL2 +Yq/rWZ4CvOphoS6zpJY3boPDtJUQ5PGpdNH2F8LVOxZPIkHqVpDlrKu3judw40pbQh3 8DyA== 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=cGJonfRz8QCsdAKY1pQeQi7v/qw/myKwiE3nlvGM1Fc=; b=ylpi+oBuT7gpOvyTaAS7+k7emvfestrwF3recE0lkGCyePBOy7c/CNcp66Dpdiq6Q0 /kJv/GfjJsyhNnwpVeB9fsQkD+9t8MEnbiiBpLjlpswCwlSfSax3BWcPphF/ugrL0dZf GQCyLH40ZiHvEuzzxfvfauxt/m7b6V4uqO7DQnC8zwODXkW/w7+L39pO+V1rz3H0nct5 zdDLI7bs5rrz+2h5mNLI/lSfHj6o3GfE2wPihrTuqFmpxSus7RGf0YUe+KTw/0xzKxhc MB9RKPeCT8Me8D8xgZkKsWlAhew9A4MJ0ZnPv59dg/IwSKxTq/aUBoYBWjMOUKSTXEFz TN/g== X-Gm-Message-State: AJIora+XoJkf9bYSgi/baowjkq5ASobXMxYpg0uxSjchIrp4u77aGOAt yUxwvSRb1g1iTFk+DPI7gpA= X-Google-Smtp-Source: AGRyM1vpV/y7G501j/nJ6zdY5F29MU23wUIhTtZRRp4SHX+O7mYctx/kcRFEff3xtV7jRXaHkGXtfg== X-Received: by 2002:a63:7a57:0:b0:40c:e242:e947 with SMTP id j23-20020a637a57000000b0040ce242e947mr7281052pgn.511.1655984516992; Thu, 23 Jun 2022 04:41:56 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.146.236]) by smtp.gmail.com with ESMTPSA id j7-20020a17090a7e8700b001ec4f258028sm1629299pjl.55.2022.06.23.04.41.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 04:41:56 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com Subject: [PATCH v4 7/7] mv: add check_dir_in_index() and solve general dir check issue Date: Thu, 23 Jun 2022 19:41:20 +0800 Message-Id: <20220623114120.12768-8-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220623114120.12768-1-shaoxuan.yuan02@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Originally, moving a directory which is not on-disk due to its existence outside of sparse-checkout cone, "giv mv" command errors out with "bad source". Add a helper check_dir_in_index() function to see if a directory name exists in the index. Also add a SKIP_WORKTREE_DIR bit to mark such directories. Change the checking logic, so that such directory makes "giv mv" command warns with "advise_on_updating_sparse_paths()" instead of "bad source"; also user now can supply a "--sparse" flag so this operation can be carried out successfully. Helped-by: Victoria Dye Helped-by: Derrick Stolee Signed-off-by: Shaoxuan Yuan --- builtin/mv.c | 50 ++++++++++++++++++++++++++++++----- t/t7002-mv-sparse-checkout.sh | 4 +-- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index aa29da4337..b5d0d8ef4f 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -25,6 +25,7 @@ enum update_mode { WORKING_DIRECTORY = (1 << 1), INDEX = (1 << 2), SPARSE = (1 << 3), + SKIP_WORKTREE_DIR = (1 << 4), }; #define DUP_BASENAME 1 @@ -123,6 +124,36 @@ static int index_range_of_same_dir(const char *src, int length, return last - first; } +/* + * Check if an out-of-cone directory should be in the index. Imagine this case + * that all the files under a directory are marked with 'CE_SKIP_WORKTREE' bit + * and thus the directory is sparsified. + * + * Return 0 if such directory exist (i.e. with any of its contained files not + * marked with CE_SKIP_WORKTREE, the directory would be present in working tree). + * Return 1 otherwise. + */ +static int check_dir_in_index(const char *name) +{ + const char *with_slash = add_slash(name); + int length = strlen(with_slash); + + int pos = cache_name_pos(with_slash, length); + const struct cache_entry *ce; + + if (pos < 0) { + pos = -pos - 1; + if (pos >= the_index.cache_nr) + return 1; + ce = active_cache[pos]; + if (strncmp(with_slash, ce->name, length)) + return 1; + if (ce_skip_worktree(ce)) + return 0; + } + return 1; +} + int cmd_mv(int argc, const char **argv, const char *prefix) { int i, flags, gitmodules_modified = 0; @@ -184,7 +215,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) /* Checking */ for (i = 0; i < argc; i++) { const char *src = source[i], *dst = destination[i]; - int length, src_is_dir; + int length; const char *bad = NULL; int skip_sparse = 0; @@ -198,12 +229,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix) pos = cache_name_pos(src, length); if (pos < 0) { + const char *src_w_slash = add_slash(src); + if (!path_in_sparse_checkout(src_w_slash, &the_index) && + !check_dir_in_index(src)) { + modes[i] |= SKIP_WORKTREE_DIR; + goto dir_check; + } /* only error if existence is expected. */ if (!(modes[i] & SPARSE)) bad = _("bad source"); goto act_on_entry; } - ce = active_cache[pos]; if (!ce_skip_worktree(ce)) { bad = _("bad source"); @@ -230,12 +266,14 @@ int cmd_mv(int argc, const char **argv, const char *prefix) bad = _("can not move directory into itself"); goto act_on_entry; } - if ((src_is_dir = S_ISDIR(st.st_mode)) + if (S_ISDIR(st.st_mode) && lstat(dst, &st) == 0) { bad = _("cannot move directory over file"); goto act_on_entry; } - if (src_is_dir) { + +dir_check: + if (S_ISDIR(st.st_mode)) { int j, dst_len, n; int first = cache_name_pos(src, length), last; @@ -369,7 +407,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) printf(_("Renaming %s to %s\n"), src, dst); if (show_only) continue; - if (!(mode & (INDEX | SPARSE)) && + if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) && rename(src, dst) < 0) { if (ignore_errors) continue; @@ -384,7 +422,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) 1); } - if (mode & (WORKING_DIRECTORY)) + if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR)) continue; pos = cache_name_pos(src, strlen(src)); diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh index 6d2fb4f8d2..71fe29690f 100755 --- a/t/t7002-mv-sparse-checkout.sh +++ b/t/t7002-mv-sparse-checkout.sh @@ -219,7 +219,7 @@ test_expect_success 'refuse to move file to non-skip-worktree sparse path' ' test_cmp expect stderr ' -test_expect_failure 'refuse to move out-of-cone directory without --sparse' ' +test_expect_success 'refuse to move out-of-cone directory without --sparse' ' test_when_finished "cleanup_sparse_checkout" && setup_sparse_checkout && @@ -230,7 +230,7 @@ test_expect_failure 'refuse to move out-of-cone directory without --sparse' ' test_cmp expect stderr ' -test_expect_failure 'can move out-of-cone directory with --sparse' ' +test_expect_success 'can move out-of-cone directory with --sparse' ' test_when_finished "cleanup_sparse_checkout" && setup_sparse_checkout &&