From patchwork Sun Jun 19 03:25:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12886529 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 A241EC433EF for ; Sun, 19 Jun 2022 03:26:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232420AbiFSD0O (ORCPT ); Sat, 18 Jun 2022 23:26:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229762AbiFSD0L (ORCPT ); Sat, 18 Jun 2022 23:26:11 -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 01266B852 for ; Sat, 18 Jun 2022 20:26:09 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id m14so6983812plg.5 for ; Sat, 18 Jun 2022 20:26:09 -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=EG5gWnDNeQ+at6Y9UAM/Z70VqqJBMK4L16z7V5lnPY0=; b=nlaeaRiopeDg6jvn35jTu9cQQniwj+nr4HTvheOfPfgCAVkkJdhqP2Sy02/fVrGGSn j7T2aijfYpglax2mDZCOVgv9YtPKSKvoyFSznZnq/6r40nVyKm8lA+JLAH6/Joos/35l KCV3mUoxsQjjFsJJbbHsR7543/T+lVQAVqIFu6ydvi+qLOPN45Thfb4gcACXbWCQOWCg W7cvugUo/R/eoY4z+5Niq/n6DQCyVo9nxP1dQ3fUBOU3s/iYPL75yp34sAiij3oyVsEO /S0Bm6o223K940fNtRfR++01EvM74HFjmPo9RyGOd9PsflmgDBKv0OmKgqGLkeINKEZL ftUA== 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=EG5gWnDNeQ+at6Y9UAM/Z70VqqJBMK4L16z7V5lnPY0=; b=QDJt0Cbj0jVsn/dXuN6iO0G0cfjQG6Q8BAfoBlSc7v06QXxjZX0+dTU0p7ObaZACg0 Pcf3Le7U8Ukabamyzg04qVJGrHaDkdKcWifCrpZ9FiNYr6yIa6ELFALevkOR9clzhMBu TeqlGiQJWBJ+E0kJXkuFvDHRNewbliB+Iu+d+b0r/we5fvFFgYQcaC/lRPHOU+R0ehdQ AzQ4jzvZdMkj/iP6tKkni2VxPkMqy4Q72eqk2W3FfktUuqq/GuMUJJ85a/K0+GdphIQD FzH+QgjoZfogYvnfr3ChXvwiUB32R8VzBzkLaoEgXtz7MUbOZ22zIMBYYa9sgXLpsSp3 dnZg== X-Gm-Message-State: AJIora9FNtpDKVZOfE83n1QRYYXxTbD7ncVuJV54fsErFLD4cztNbuRg FHUyGNrytFjGtCKZswQlqO4= X-Google-Smtp-Source: AGRyM1uMMc/ae3Fcz7TMxt/Gbsh2LxV8Lwwm11uIwFdeIVb0v/dSZI7aRhjUIR6nWO0S6FEazYoJJg== X-Received: by 2002:a17:90b:17d2:b0:1e2:c0a2:80fb with SMTP id me18-20020a17090b17d200b001e2c0a280fbmr31130023pjb.67.1655609169504; Sat, 18 Jun 2022 20:26:09 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.147.33]) by smtp.gmail.com with ESMTPSA id u1-20020a170903124100b0016188a4005asm5990650plh.122.2022.06.18.20.26.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jun 2022 20:26:09 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com, newren@gmail.com Subject: [WIP v3 1/7] t7002: add tests for moving out-of-cone file/directory Date: Sun, 19 Jun 2022 11:25:43 +0800 Message-Id: <20220619032549.156335-2-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220619032549.156335-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220619032549.156335-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 | 87 +++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh index f0f7cbfcdb..d6e7315a5a 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,78 @@ 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 1>actual 2>stderr && + test_must_be_empty stderr && + + git sparse-checkout reapply && + 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 1>actual 2>stderr && + test_must_be_empty stderr && + + git sparse-checkout reapply && + ! test_path_is_dir sub/folder1 && + 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 Sun Jun 19 03:25:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12886530 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 9D59CC433EF for ; Sun, 19 Jun 2022 03:26:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232520AbiFSD0Q (ORCPT ); Sat, 18 Jun 2022 23:26:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232301AbiFSD0N (ORCPT ); Sat, 18 Jun 2022 23:26:13 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AF93B851 for ; Sat, 18 Jun 2022 20:26:12 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id d14so3901495pjs.3 for ; Sat, 18 Jun 2022 20:26:12 -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=std8deXcUT2GcoWGie8OWc1yk48JfUU1UEQGV78jO/U=; b=mXh+6PIhcHEO12vrKBv/85O0bjo7g9QMEgpiBwJB+arNvEt+fzlsBTok16umflrvT4 RRvQhi2HzkUhh/OLNP9NaYdEa3OzOXgZPsh69me/arx3Q68ebPaCJO0JDPLvSKl6ooa3 WBn1phGXynwfe0tEEA9MxM35xaC2bQfvx79vgIqRD6JfoJkUGP4I3gxnfoekF8V+XdCB rAzNrChfaF7YZI4WhzoGSdsvplkXe6MI3WyshCmY8Emv8cl6yy+ExsX5YuLj6lwQhbZs PxE6NkGBUEbigNHlH7WHfsCSlNye+WKZSuy4WCGDlhjRnW1ssFuhXW/fMJ185RPchJ+4 75yQ== 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=std8deXcUT2GcoWGie8OWc1yk48JfUU1UEQGV78jO/U=; b=cDyqTvBThfnP0ufHxhsnwc2XzxvN2tVGfI3eXT6IbacnMxd1Ii/sFZWn9YZsXhV1ec A68aCjuRpztSCnUsS2pNDy9Zf1m5rXsgHJHgf0dhkvvWfOyEdzTZx+GCVfG54qg0sfCU s91gXC6G7jd1DqsY87R7PU91K92XYcy0KLVZav/KgC9/x/Y5NpVvcW/iN2eqw14f+eXo SzIK5Mn+BrvVAi2GYQSPmzIQv7MMqU818XCsx+rbGWCG9LoEPATyDzbT5ir3duofwqwx HHFUmIh4VHKC0/SddB3n2kyfr0RWF/qYNnM41v+ifKxMH84myrVPeOug4uotWog9IlOW 1caw== X-Gm-Message-State: AJIora+J7miLLGxREhao/7AXxtGgYFSBVaUauvTiBEAocLLRF/B2P8/d fkS/7VyROuB362GQZCpHSYs= X-Google-Smtp-Source: AGRyM1sPmblCWmOo6LrSZ8DG/a8yflv7hoRVRkGfagi9MZgmtqyiFxoJoo+aguzJAzY98n6OEdYXLQ== X-Received: by 2002:a17:902:8f87:b0:166:3cf5:335f with SMTP id z7-20020a1709028f8700b001663cf5335fmr17110691plo.119.1655609171565; Sat, 18 Jun 2022 20:26:11 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.147.33]) by smtp.gmail.com with ESMTPSA id u1-20020a170903124100b0016188a4005asm5990650plh.122.2022.06.18.20.26.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jun 2022 20:26:11 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com, newren@gmail.com Subject: [WIP v3 2/7] mv: decouple if/else-if checks using goto Date: Sun, 19 Jun 2022 11:25:44 +0800 Message-Id: <20220619032549.156335-3-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220619032549.156335-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220619032549.156335-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 83a465ba83..1ca2c21da8 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -186,53 +186,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) { /* @@ -246,34 +261,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 Sun Jun 19 03:25:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12886531 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 8D1E5C43334 for ; Sun, 19 Jun 2022 03:26:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233464AbiFSD0Y (ORCPT ); Sat, 18 Jun 2022 23:26:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232487AbiFSD0P (ORCPT ); Sat, 18 Jun 2022 23:26:15 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4936EB861 for ; Sat, 18 Jun 2022 20:26:14 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id h1so6960812plf.11 for ; Sat, 18 Jun 2022 20:26:14 -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=4VjMXTCwloixnfFye9Oqcx47NsZU71R4j1Q5TLyQ+fo=; b=jDsz3uBdffixJEDrjSL187qz5ce9ZZZqXYuixpyE2RG1wxO8JVfUasCOooeXL9UELj uGZBKVXvYXN9kB9GWaz2se5fUNwqmPrTbj74CmIMrT/IYAI36GIEwwHdCIjnUzCsvkLc HaTgUtRKIgga8nMcr0hORoEbxzIbAJBNvWlI2HeOof0UstCmvk2D4uZg7L2akQajzeaB ccoBqn1+d9eR5Q52L3pqd47JjFcbQy804FxYUa+h/npQJZ1xaChFZm/ea8YiEO9/VMQj mTCG6O13eb4ZGcz+Jq/zsS2WdcNUkPyh8LZF8NwxN5WBmxnX9dlt8PJUWB3g0XzEzFUJ NQYQ== 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=4VjMXTCwloixnfFye9Oqcx47NsZU71R4j1Q5TLyQ+fo=; b=sOeU6QrVkpPkRcRUW26yjoHu3VdRGKPQOZ1b77pa/p9wCdLl7Ns8ECEuBBdUC1kfL4 sg3hSpywi2PoawLqNimhi6CPYiYPS8DK256R/OroFat95tA4KBNmOFkoSYY/sjdbFdJ/ ariEhKzm+8TT0fNhQWP8QlbYBb2qC64e+AUJl0Q9mwtuwxzxqVF/K4olsSQjszSAhzcp BYYRu9VH/MckD+XHYyl27BXbBlzzKXh//BP2dy22fykLwe2sM620Cjqf1UAx1a27GS5c Y9M7f0HYivtR25CbaoLxKBLMKLilAxHBHQNmRSTgz9z0woi8V9VKYNPM46KJtOvoFVLg 737g== X-Gm-Message-State: AJIora8NFnoZ6aoewffhrx1RNxpblL0t670g+Ly9wL/X0ULrOCXuFOEX TjlCqc5DMOQ1BnODvx1KJxU= X-Google-Smtp-Source: AGRyM1t4zONeJMGL0qdZ270Og5B4zq7EL6/0CPp3WP3ETrpo0NpQtLYLljtSLiDvaLuE64eDipftvw== X-Received: by 2002:a17:902:ce83:b0:166:42de:29d5 with SMTP id f3-20020a170902ce8300b0016642de29d5mr17570553plg.123.1655609173690; Sat, 18 Jun 2022 20:26:13 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.147.33]) by smtp.gmail.com with ESMTPSA id u1-20020a170903124100b0016188a4005asm5990650plh.122.2022.06.18.20.26.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jun 2022 20:26:13 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com, newren@gmail.com Subject: [WIP v3 3/7] mv: check if out-of-cone file exists in index with SKIP_WORKTREE bit Date: Sun, 19 Jun 2022 11:25:45 +0800 Message-Id: <20220619032549.156335-4-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220619032549.156335-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220619032549.156335-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 1ca2c21da8..9d8494a2e4 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -185,11 +185,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 d6e7315a5a..1984cf131d 100755 --- a/t/t7002-mv-sparse-checkout.sh +++ b/t/t7002-mv-sparse-checkout.sh @@ -242,7 +242,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 && @@ -253,7 +253,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 Sun Jun 19 03:25:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12886532 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 8B91DC433EF for ; Sun, 19 Jun 2022 03:26:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232536AbiFSD00 (ORCPT ); Sat, 18 Jun 2022 23:26:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232301AbiFSD0R (ORCPT ); Sat, 18 Jun 2022 23:26:17 -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 4A617BC20 for ; Sat, 18 Jun 2022 20:26:16 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id y13-20020a17090a154d00b001eaaa3b9b8dso7354180pja.2 for ; Sat, 18 Jun 2022 20:26:16 -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=AGCBW8RoEvEiBZAX1k87U81P878nRnAoTUXBjhr/XNQ=; b=HjWTyQ2mTNyG3Byx5wMTHGMHmfWbUGK74olNPFx59CwAisuqFy90lYVwyQYVYLHzGZ tYqyDrPpKBfErTYUHp/Nf5HI36OhKR307rdLQVxvhSGazl3NmVbhP937PVgZV+f2Wb6I o/IzpkGDj0tjn4OFVBe3dH04lRXQyN1FkppHqIYfzNcqDI4z2wlXm4HptbXdGyskA1Bk sAC46hRhINqd694kdV+xDx3Ur+SJXrnt8kwEZl0mM0o240N/f9FRAgnhXfmovtVutlzx 4hAOk0dpKATvRV86iB8NRZ+U0oD4XwBENLSLbVVbD3VFkI3e13Z3N3hwJYivs7oaqrvC wx3g== 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=AGCBW8RoEvEiBZAX1k87U81P878nRnAoTUXBjhr/XNQ=; b=w8B+HkB2UPetvnF9bJsnOmDf+zzl6wmHz+xtaqDN6lWHkksLKwP6avONenu5G0ktyT PkDjE34PISdT0NK4P5vcO5xaKLhCs5L+J34Qfh7GwqOaxJcH+toSIj29+N01HjKaRi0U P/+7bTl3pa1I4sUTVObkEzEWrHYnApvDxcoibiseMmfzL0/IxDhJhc56oiMrndY3RapM a9LRQ0fnYXSuGmyi8UvkmKSoLk+j5i1sX3jzxU3R+kob4qeu4A1RMgbpiWIVtzrACbFt xP7e2VXQCiH2oCMXNuzsmH97uu6OAdbFtVQbgK3hBk7dfVr4AKoYQE/ug3ZUNiqWerb7 a+fA== X-Gm-Message-State: AJIora/B8VIlI3U8B/cgQYTwxUDyNNlM4aBP1IXZnRPCHbUmIJdJn2Dj zHX3IzIpE0OUnrFJKAVQcz4= X-Google-Smtp-Source: AGRyM1vzEDUhudsE+wJXex3YFzAu2My9z02Qb1vEnLhO5lGNdnoqnQMhQZWKTTrPxLtFfSoCp+sDFQ== X-Received: by 2002:a17:903:260d:b0:16a:20a2:374d with SMTP id jd13-20020a170903260d00b0016a20a2374dmr55161plb.127.1655609175721; Sat, 18 Jun 2022 20:26:15 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.147.33]) by smtp.gmail.com with ESMTPSA id u1-20020a170903124100b0016188a4005asm5990650plh.122.2022.06.18.20.26.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jun 2022 20:26:15 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com, newren@gmail.com Subject: [WIP v3 4/7] mv: check if exists in index to handle overwriting Date: Sun, 19 Jun 2022 11:25:46 +0800 Message-Id: <20220619032549.156335-5-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220619032549.156335-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220619032549.156335-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 | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 9d8494a2e4..abb90d3266 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -201,11 +201,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 1984cf131d..5b61fbad5f 100755 --- a/t/t7002-mv-sparse-checkout.sh +++ b/t/t7002-mv-sparse-checkout.sh @@ -265,7 +265,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 && From patchwork Sun Jun 19 03:25:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12886533 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 44F44C433EF for ; Sun, 19 Jun 2022 03:26:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232678AbiFSD01 (ORCPT ); Sat, 18 Jun 2022 23:26:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232749AbiFSD0X (ORCPT ); Sat, 18 Jun 2022 23:26:23 -0400 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 51315B86D for ; Sat, 18 Jun 2022 20:26:18 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id go6so2568352pjb.0 for ; Sat, 18 Jun 2022 20:26:18 -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=GqI1VE/xlgwyWmrJRQhBuFug8nWgFNU8Ekx6d4zYx54=; b=j79wUxG3m0vlOy4h1ygpWeaayHs4pEPc7x8e4nrh3vuXlEMmirRjrpfBEg5ibFSrvg s3CHkrsOvLf69jEYTNla+TcydlOfYOnqw8aatFpSGP/vDjUPuhdPGsslc2Cvym9Bb6NM bnqQ85e5ehkWO+jtJKIfZLKbrcSVBW0/Lj2+L3puOe6ZJeVZ+K+gXtpkr1N9QkdyEsW0 dh+ezv9A4+eZHVH8Rg/MgcY7EskRvB2aTPcQV2BeOW9yF1WZP88PD+fjtc2hrrALfCDl WNTAjzWEuYXvAGBLDlx17eYu0kydHc8jAXq9K2cuMIVZ1cbyr3Aqj9dYYighV5Gi8Sbt EzIg== 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=GqI1VE/xlgwyWmrJRQhBuFug8nWgFNU8Ekx6d4zYx54=; b=t2G1M9vJXV2c6ki+rShvBN39ZTAQntDFTA25Nw+lHXQtNJEO6k6dgS07+e8MK2Oovt uuipqk9g7IIKjVvVPF1511tcUD7wKjSHtN0QHg4d0yablkjAmnSh/taHA8THx8mGnY24 2AMGRRXZ+Rii+tjRCC1P4lfBIt+15k46ZFDZWQUIGIov673y9XkhqXqyfuS/alKCUX2L hHe45p85wIbXkU0tGssOpTp9nOB6zj2EBJ48ZfResvPDEJ2GokTLLU4Ekncy8OiXAqZd t0SRb0J7cJlnrfANjvH1zHGP/WO6wU8h6b0EefJVJpfFqdaXC6n8Oik7be5TpTlCO6AE eh8Q== X-Gm-Message-State: AJIora+49IZwSAwlmVLZwr4W0Zvk4vqAUvHm0hI7RkhAZhKSBRvVKZXf S+esTLyEDJkXkRUVzGWU1UI= X-Google-Smtp-Source: AGRyM1sN/pwptAOO2y5lef8wSdoOspy9xKOPdYeO6EDalRgxbyGeBafucG/NOVbNHDBXUYfQkdNhFw== X-Received: by 2002:a17:903:41c9:b0:164:57e:4b22 with SMTP id u9-20020a17090341c900b00164057e4b22mr17255686ple.2.1655609177708; Sat, 18 Jun 2022 20:26:17 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.147.33]) by smtp.gmail.com with ESMTPSA id u1-20020a170903124100b0016188a4005asm5990650plh.122.2022.06.18.20.26.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jun 2022 20:26:17 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com, newren@gmail.com Subject: [WIP v3 5/7] mv: use flags mode for update_mode Date: Sun, 19 Jun 2022 11:25:47 +0800 Message-Id: <20220619032549.156335-6-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220619032549.156335-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220619032549.156335-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). [1] https://lore.kernel.org/git/22aadea2-9330-aa9e-7b6a-834585189144@github.com/ Signed-off-by: Shaoxuan Yuan --- builtin/mv.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index abb90d3266..7ce7992d6c 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -19,6 +19,14 @@ static const char * const builtin_mv_usage[] = { NULL }; +enum update_mode { + BOTH = 0, + WORKING_DIRECTORY = (1 << 1), + INDEX = (1 << 2), + SPARSE = (1 << 3), + SKIP_WORKTREE_DIR = (1 << 4), +}; + #define DUP_BASENAME 1 #define KEEP_TRAILING_SLASH 2 @@ -129,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; @@ -191,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; } @@ -207,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) && @@ -242,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); @@ -258,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; @@ -355,7 +364,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 | SKIP_WORKTREE_DIR)) && + rename(src, dst) < 0) { if (ignore_errors) continue; die_errno(_("renaming '%s' failed"), src); @@ -369,7 +379,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)); From patchwork Sun Jun 19 03:25:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12886534 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 B3CC1C43334 for ; Sun, 19 Jun 2022 03:26:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233064AbiFSD0d (ORCPT ); Sat, 18 Jun 2022 23:26:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232952AbiFSD0X (ORCPT ); Sat, 18 Jun 2022 23:26:23 -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 8C26BEE36 for ; Sat, 18 Jun 2022 20:26:20 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id b10so45383plg.4 for ; Sat, 18 Jun 2022 20:26:20 -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=9lYNZ9mw0Rsmsb9RYrpFEuwwroPEAOy3nqzqzm0sONg=; b=GnkTGi5QbMMNypdiSySDiaB2BqhSFfZY3/ATWAp42u+M++guT6UZpqrC9Ndij5wFUG ZXbT13UQd57wKNCMrvld4T+OX0KB0LdYb4r95UqKwQQUj0N66KohwWZpDLX3aOVwphpw UzXyP4nnhgabsXnYpgJRJ39x/aqcLBqMLyuavkkogYqEIOQYTPRsU9lZt9AO+nmvdK8g fkqmbvXefYwZsEh6N6+pwldqiW8m5RTbpJDXEeyPb9eg9VtzFfJhvSsjPMGs7R5YKIY7 Gv7/2NVsfMzEnlNK2YfuZuDNCCf6bOgQXtFL5I1S25tfXFb3VINEwR6Y8S2gs7WT0hN9 H4Xg== 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=9lYNZ9mw0Rsmsb9RYrpFEuwwroPEAOy3nqzqzm0sONg=; b=mmuCzU4g+dOOEpqGwtp33bd5GqZzs72wHAo7pXfgesynBbv/hynoqfKD6ws09wRsl3 xshlaYZJG5mMEHcPJznbEDAY7/6O7vy8eB5tgX9p30w4a3sPxLB6XBvR6xcEebg/J5tB AnFT72aXMoWythsZ9Fg8OG0nWusLm/EZf39fbD79WosxgSo6BeAHvicrwiNiPLgeG2/r r5JAt2oMXllJf7j5CqkmaitIav27WsDR5mi17vPfpSqSpUBqKhrm/LRZc+OYR+az1asy JujGtsXYZbHDxQA+glhTHny2rUxtIWLK3VhYovOepYEjbYayYMmrDB6daPazREwFJp64 38kw== X-Gm-Message-State: AJIora8cSHLyvu7pNxR2z+uHaWe4nO0ApHE1x+xl550LS9WgevtNODbS djC4ujZva+e9zlRG2F9Iri0= X-Google-Smtp-Source: AGRyM1sDv6e5GlbuutDjcz7KAzt1JAFcPG0wzWMHrq0tLf3KS0IHURkMGVcDBmnZ5v8nVbTLkCLCuA== X-Received: by 2002:a17:902:8b8b:b0:167:4e55:2dac with SMTP id ay11-20020a1709028b8b00b001674e552dacmr17529712plb.132.1655609179815; Sat, 18 Jun 2022 20:26:19 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.147.33]) by smtp.gmail.com with ESMTPSA id u1-20020a170903124100b0016188a4005asm5990650plh.122.2022.06.18.20.26.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jun 2022 20:26:19 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com, newren@gmail.com Subject: [WIP v3 6/7] mv: add check_dir_in_index() and solve general dir check issue Date: Sun, 19 Jun 2022 11:25:48 +0800 Message-Id: <20220619032549.156335-7-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220619032549.156335-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220619032549.156335-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. Signed-off-by: Shaoxuan Yuan --- builtin/mv.c | 49 +++++++++++++++++++++++++++++++---- t/t7002-mv-sparse-checkout.sh | 4 +-- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 7ce7992d6c..cb3441c7cb 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -123,6 +123,37 @@ 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, int namelen) +{ + int ret = 1; + const char *with_slash = add_slash(name); + int length = namelen + 1; + + 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 ret; + ce = active_cache[pos]; + if (strncmp(with_slash, ce->name, length)) + return ret; + if (ce_skip_worktree(ce)) + return ret = 0; + } + return ret; +} + 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 (!check_dir_in_index(src, length) && + !path_in_sparse_checkout(src_w_slash, &the_index)) { + 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,14 +266,17 @@ 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; + int first, last; + first = cache_name_pos(src, length); if (first >= 0) { prepare_move_submodule(src, first, diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh index 5b61fbad5f..30e13b9979 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 && From patchwork Sun Jun 19 03:25:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shaoxuan Yuan X-Patchwork-Id: 12886535 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 C9CB6C433EF for ; Sun, 19 Jun 2022 03:26:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233162AbiFSD0f (ORCPT ); Sat, 18 Jun 2022 23:26:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233217AbiFSD0Y (ORCPT ); Sat, 18 Jun 2022 23:26:24 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 738A7F59D for ; Sat, 18 Jun 2022 20:26:22 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id u18so6983310plb.3 for ; Sat, 18 Jun 2022 20:26:22 -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=QvI7YUGmKvvkFdWKpmwW/k5ky80zzBvcOB9aG5Dthsk=; b=gJ8RCD9PN3wmUpOE9K+BllQ8vlEka7aRpxvRqRCW2U6UHrP0Rf2jTfPZHvnncaoxLY lCqrvz0YeQ+a/brORjOszMQTLvfjCrNso5e/n3KgzWAx6BgMCz0CGeJN0yMb6DjsnQSl NUfvDgOz9SpbdqaBJqyq2ZFhfUQ++EpnjV7jqSk7BKmj4EpwtY5J9RmlTJz2CGGCYV+B zapp2qKaYrmt4I4lwqzGO3oRH6m3n3fg0ZkToX8E7UFmfUwSdNOZJVMr7taR3swtJNjs x7fldxCBPol1NiKM9/iUeMGrPw6phiLsUbuMWo7hSXn9PbLkj7PPEbz6lRalqS9abhrM LzHA== 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=QvI7YUGmKvvkFdWKpmwW/k5ky80zzBvcOB9aG5Dthsk=; b=7kAOkyIhVpXhLmqYSP7mmrAWhC45sZB+WicTMxiHEUGetrWWHhdk30azfWIJg6P8Ud +Qly5gaMRROfAXp+8AaaLQkhWemXMdm0Xeb89/5fSkSnH1DXiwe/a/QrUMo3d7saDD0H OZ1dvLJFhX6sOejpxQojWHHqq35I6MA1W8F6oHlMHBHAqH3A0nARJIk/qU0V87514K8A UsCEA5jToVph/g0ogQbIGv846tVteNcOyxC2IzdWgD71unxPmh+Wg7J/JcfOPNQAyMnQ C47cmkmWlTwbH5K7Iw4Rfr/sBVbowOvlDLHVL1GcjSIh7s5js1iiAYoqunuAT24xOAz+ w9tQ== X-Gm-Message-State: AJIora9+D25kMZvj8rts9afMoAsqqu8Fp+e+nl4AKneLj9B2r/iaVaY/ SE5ipVPnzlbpzkNsuyEpsTVdK8hIcPvQao+6iS4= X-Google-Smtp-Source: AGRyM1tBpZ/PFefpGExD9yuLaqiWHC2sCmhBm2l5x8zMapEZ8gRMuAaYrc16vVeJ+MX/8GOLh14Mrg== X-Received: by 2002:a17:90a:e582:b0:1ec:8bd2:981e with SMTP id g2-20020a17090ae58200b001ec8bd2981emr5683729pjz.22.1655609181816; Sat, 18 Jun 2022 20:26:21 -0700 (PDT) Received: from ffyuanda.localdomain ([112.195.147.33]) by smtp.gmail.com with ESMTPSA id u1-20020a170903124100b0016188a4005asm5990650plh.122.2022.06.18.20.26.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jun 2022 20:26:21 -0700 (PDT) From: Shaoxuan Yuan To: shaoxuan.yuan02@gmail.com Cc: derrickstolee@github.com, git@vger.kernel.org, gitster@pobox.com, vdye@github.com, newren@gmail.com Subject: [WIP v3 7/7] mv: update sparsity after moving from out-of-cone to in-cone Date: Sun, 19 Jun 2022 11:25:49 +0800 Message-Id: <20220619032549.156335-8-shaoxuan.yuan02@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220619032549.156335-1-shaoxuan.yuan02@gmail.com> References: <20220331091755.385961-1-shaoxuan.yuan02@gmail.com> <20220619032549.156335-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. Signed-off-by: Shaoxuan Yuan --- builtin/mv.c | 18 ++++++++++++++++++ t/t7002-mv-sparse-checkout.sh | 4 +--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index cb3441c7cb..a8b9f55654 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 [] ... "), @@ -399,6 +400,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) @@ -424,6 +430,18 @@ 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) { + if (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) diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh index 30e13b9979..7734119197 100755 --- a/t/t7002-mv-sparse-checkout.sh +++ b/t/t7002-mv-sparse-checkout.sh @@ -237,7 +237,6 @@ test_expect_success 'can move out-of-cone directory with --sparse' ' git mv --sparse folder1 sub 1>actual 2>stderr && test_must_be_empty stderr && - git sparse-checkout reapply && test_path_is_dir sub/folder1 && test_path_is_file sub/folder1/file1 ' @@ -260,7 +259,6 @@ test_expect_success 'can move out-of-cone file with --sparse' ' git mv --sparse folder1/file1 sub 1>actual 2>stderr && test_must_be_empty stderr && - git sparse-checkout reapply && ! test_path_is_dir sub/folder1 && test_path_is_file sub/file1 ' @@ -278,7 +276,7 @@ test_expect_success '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 &&