From patchwork Mon Oct 9 23:34:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugo Sales X-Patchwork-Id: 13414688 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 46236E95A9C for ; Mon, 9 Oct 2023 23:35:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379061AbjJIXfR (ORCPT ); Mon, 9 Oct 2023 19:35:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379038AbjJIXfQ (ORCPT ); Mon, 9 Oct 2023 19:35:16 -0400 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [IPv6:2001:67c:2050:0:465::201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86D6A9D for ; Mon, 9 Oct 2023 16:35:13 -0700 (PDT) Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4S4Fkg60Bnz9snY; Tue, 10 Oct 2023 01:35:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hsal.es; s=MBO0001; t=1696894507; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ezzO0xe3JSClqveT5b0ioxz8R3VpCy3UM8ZEnoWM1Fc=; b=wE/6Tx+ggwPI8W8XrsWc4/6s/M1jjIdLYSuBRq3ZirKrwpnyj9PEGAHeRnBu7fGgr0VKpC CD7WIipJuYs7kD0TZf/pJPBKAf8jx+0NER04SKVKElNPk+BqSyxaada+PVlCY0lAFxnVIJ GnXehDrg2mNB86gGaeuMvYYKXXjtAC9T0tzDZU03RBUvf2Bfz/sxdcYT1VM6yAXMtxOz21 ZnFZI881Smwk174tUc/jFD9jopLiqScnwOZghgk7/0jUSnM9a+iRAaDbqA8CLJj7kugexr K4JlQHWC6B0cy4TmEqo2MrUJUZULF/Fn1cH1oWyb8NGxmvrGbgkNk0VvZ13pvQ== From: Hugo Sales To: git@vger.kernel.org Cc: Hugo Sales Subject: [PATCH 1/3] mv: Add -p option to create parent directories Date: Tue, 10 Oct 2023 00:34:56 +0100 Message-ID: <20231009233458.1371351-2-hugo@hsal.es> In-Reply-To: <20231009233458.1371351-1-hugo@hsal.es> References: <20231009233458.1371351-1-hugo@hsal.es> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4S4Fkg60Bnz9snY Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Inspired by "mkdir -p", this patch allows specifying a "-p" or "--parents" flag which will create all non-existent directories in the destination path before renaming the file. This allows the user to not have to run two commands to move files to a new directory. Signed-off-by: Hugo Sales --- builtin/mv.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index c596515ad0..5d64d86179 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -168,7 +168,7 @@ static int empty_dir_has_sparse_contents(const char *name) int cmd_mv(int argc, const char **argv, const char *prefix) { int i, flags, gitmodules_modified = 0; - int verbose = 0, show_only = 0, force = 0, ignore_errors = 0, ignore_sparse = 0; + int verbose = 0, show_only = 0, force = 0, ignore_errors = 0, ignore_sparse = 0, create_parents = 0; struct option builtin_mv_options[] = { OPT__VERBOSE(&verbose, N_("be verbose")), OPT__DRY_RUN(&show_only, N_("dry run")), @@ -176,6 +176,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) PARSE_OPT_NOCOMPLETE), OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")), OPT_BOOL(0, "sparse", &ignore_sparse, N_("allow updating entries outside of the sparse-checkout cone")), + OPT_BOOL('p', "parents", &create_parents, N_("create missing parent directories")), OPT_END(), }; const char **source, **destination, **dest_path, **submodule_gitfile; @@ -220,8 +221,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix) if (dest_path[0][0] == '\0') /* special case: "." was normalized to "" */ destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME); - else if (!lstat(dest_path[0], &st) && - S_ISDIR(st.st_mode)) { + else if (create_parents || + (!lstat(dest_path[0], &st) && S_ISDIR(st.st_mode))) { destination = internal_prefix_pathspec(dst_w_slash, argv, argc, DUP_BASENAME); } else { if (!path_in_sparse_checkout(dst_w_slash, &the_index) && @@ -381,7 +382,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix) bad = _("multiple sources for the same target"); goto act_on_entry; } - if (is_dir_sep(dst[strlen(dst) - 1])) { + + if (!create_parents && is_dir_sep(dst[strlen(dst) - 1])) { bad = _("destination directory does not exist"); goto act_on_entry; } @@ -459,11 +461,18 @@ int cmd_mv(int argc, const char **argv, const char *prefix) if (show_only) continue; if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) && - !(dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) && - rename(src, dst) < 0) { - if (ignore_errors) - continue; - die_errno(_("renaming '%s' failed"), src); + !(dst_mode & (SKIP_WORKTREE_DIR | SPARSE))) { + if (create_parents && safe_create_leading_directories_const(dst) < 0) { + if (ignore_errors) + continue; + die_errno(_("creating parent directories for '%s' failed"), dst); + } + + if (rename(src, dst) < 0) { + if (ignore_errors) + continue; + die_errno(_("renaming '%s' failed"), src); + } } if (submodule_gitfile[i]) { if (!update_path_in_gitmodules(src, dst)) From patchwork Mon Oct 9 23:34:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugo Sales X-Patchwork-Id: 13414686 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 300E8E9371F for ; Mon, 9 Oct 2023 23:35:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378998AbjJIXfP (ORCPT ); Mon, 9 Oct 2023 19:35:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234611AbjJIXfN (ORCPT ); Mon, 9 Oct 2023 19:35:13 -0400 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27EE4A6 for ; Mon, 9 Oct 2023 16:35:12 -0700 (PDT) Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4S4Fkh30tFz9sny; Tue, 10 Oct 2023 01:35:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hsal.es; s=MBO0001; t=1696894508; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=26KheS83dEowkinWRd7ozsP5i/oCz2R04s7j1n5R3CE=; b=cWMtat/ntnGh5CSubNQIPptwSysPKxOiVoQmpYj1EWZyPPjbp22o3zDQGiWdsYetmGE72a P2DDoHCyRdmQGnPHgq/pzcKDTCSRlReo2oVbGfTRf1LY7PbbMsr3+8Ov+B/AfGIbr2nD/J 1wdnaePa0z9ascvv3wrCXnsefzjCyeoD2zH1XgPa0cRNnFJ5sf9vK9f0gEBJaec7nrT/KP n0MnfR1MfaBEU/t9tn6Z2fJev4nUhEd7xuibkRyxbnYt5OP2g2VkmyQiMooH6vbAa0MO8U jbMpouVwEfcTWBNfiUJCtz5VNEZILcGhk2PatbvQvdBm6B3LGS0YpoGc2nha2g== From: Hugo Sales To: git@vger.kernel.org Cc: Hugo Sales Subject: [PATCH 2/3] mv: Add tests for new -p flag Date: Tue, 10 Oct 2023 00:34:57 +0100 Message-ID: <20231009233458.1371351-3-hugo@hsal.es> In-Reply-To: <20231009233458.1371351-1-hugo@hsal.es> References: <20231009233458.1371351-1-hugo@hsal.es> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4S4Fkh30tFz9sny Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Signed-off-by: Hugo Sales --- t/t7009-mv-parents.sh | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 t/t7009-mv-parents.sh diff --git a/t/t7009-mv-parents.sh b/t/t7009-mv-parents.sh new file mode 100755 index 0000000000..cc1d9dae08 --- /dev/null +++ b/t/t7009-mv-parents.sh @@ -0,0 +1,79 @@ +#!/bin/sh + +test_description='git mv -p' +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME + +. ./test-lib.sh + +test_expect_success 'mv fails to move a file if the target directory does not exist' ' + echo test >test1 && + git add test1 && + test_must_fail git mv test1 foo/ +' + +test_expect_success 'mv fails to move multiple files if the target directory does not exist' ' + echo test >test2-1 && + echo test >test2-2 && + git add test2-1 test2-2 && + test_must_fail git mv test2-1 test2-2 foo/ +' + +test_expect_success 'mv fails to move a file if the target refers to a file in a directory that does not exist' ' + echo test >test3 && + git add test3 && + test_must_fail git mv test3 foo/test3.txt +' + +test_expect_success 'mv succeeds to move a file even if the target directory does not exist' ' + echo test >test4 && + git add test4 && + git commit -m test4-commit1 && + git mv -p test4 dir4/ && + git commit -m test4-commit2 && + git diff-tree -r -M --name-status HEAD^ HEAD >test4-actual && + grep "^R100..*test4..*dir4/test4" test4-actual +' + +test_expect_success 'mv succeeds to move multiple files even if the target directory does not exist' ' + echo test >test5-1 && + echo test >test5-2 && + git add test5-1 test5-2 && + git commit -m test5-commit1 && + git mv -p test5-1 test5-2 dir5/ && + git commit -m test5-commit2 && + git diff-tree -r -M --name-status HEAD^ HEAD >test5-actual && + grep -e "^R100..*test5-1..*dir5/test5-1" -e "^R100..*test5-2..*dir5/test5-2" test5-actual +' + +test_expect_success 'mv succeeds to move a file even if the target refers to a file in a directory that does not exist' ' + echo test >test6 && + git add test6 && + git commit -m test6-commmit-1 && + git mv -p test6 dir6/test6.txt && + git commit -m test6-commit2 && + git diff-tree -r -M --name-status HEAD^ HEAD >test6-actual && + grep "^R100..*test6..*dir6/test6.txt" test6-actual +' + +test_expect_success 'mv succeeds to move a file even if the target refers to a file in a directory inside a directory that does not exist' ' + echo test >test7 && + git add test7 && + git commit -m test7-commit1 && + git mv -p test7 dir7/dir7/test7.txt && + git commit -m test7-commit2 && + git diff-tree -r -M --name-status HEAD^ HEAD >test7-actual && + grep "^R100..*test7..*dir7/dir7/test7.txt" test7-actual +' + +test_expect_success 'mv succeeds to move a file even if the target refers to a file in a directory inside a directory inside a directory that does not exist' ' + echo test >test8 && + git add test8 && + git commit -m test8-commit1 && + git mv -p test8 dir8/dir8/dir8/test8.txt && + git commit -m test8-commit2 && + git diff-tree -r -M --name-status HEAD^ HEAD >test8-actual && + grep "^R100..*test8..*dir8/dir8/dir8/test8.txt" test8-actual +' + +test_done From patchwork Mon Oct 9 23:34:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugo Sales X-Patchwork-Id: 13414687 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 9711EE95A97 for ; Mon, 9 Oct 2023 23:35:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379049AbjJIXfQ (ORCPT ); Mon, 9 Oct 2023 19:35:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234602AbjJIXfO (ORCPT ); Mon, 9 Oct 2023 19:35:14 -0400 Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3425BA7 for ; Mon, 9 Oct 2023 16:35:12 -0700 (PDT) Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4S4Fkh74g6z9sSS; Tue, 10 Oct 2023 01:35:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hsal.es; s=MBO0001; t=1696894509; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YD9ROmTwt/AoEiVVzoWDQP138HBIDuStSHGs1J8J0pk=; b=0z4fXnLysPSrTGG5juW7fV+0d9QlMh03oJMoLlGo8gm+Z/0A4LSe1ViEkxexW7eAunlKgI r+DcJd8JLAQSEPdg9nOuD+qOUYuw1NgxWrh5ZfxS7LYs34eY0gevzZpEbGm0372v2l5CTB situRUhICbGhKb7tbuqOLN+1WBoA7dk7iHaIApl/APJ9r7rwUqk/64QXZRb195prMfiyUZ Ip+fpfMeMOCUFIshXx/YepoIi9T5h9kaFvgJoxKpHgkLzVQ6nNUFqXs+tjmSDE0YwDfxwm BD/FKHln6xYuk4SejFrEWjMYQ2ebQhhSJijXJw8HTUmrgGMZZLVLGAmkfnd33Q== From: Hugo Sales To: git@vger.kernel.org Cc: Hugo Sales Subject: [PATCH 3/3] mv: Add documentation for new `-p' flag Date: Tue, 10 Oct 2023 00:34:58 +0100 Message-ID: <20231009233458.1371351-4-hugo@hsal.es> In-Reply-To: <20231009233458.1371351-1-hugo@hsal.es> References: <20231009233458.1371351-1-hugo@hsal.es> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4S4Fkh74g6z9sSS Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Signed-off-by: Hugo Sales --- Documentation/git-mv.txt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Documentation/git-mv.txt b/Documentation/git-mv.txt index fb0220fd18..70c1e9572c 100644 --- a/Documentation/git-mv.txt +++ b/Documentation/git-mv.txt @@ -15,13 +15,14 @@ DESCRIPTION ----------- Move or rename a file, directory or symlink. - git mv [-v] [-f] [-n] [-k] - git mv [-v] [-f] [-n] [-k] ... + git mv [-v] [-f] [-n] [-k] [-p] + git mv [-v] [-f] [-n] [-k] [-p] ... In the first form, it renames , which must exist and be either a file, symlink or directory, to . -In the second form, the last argument has to be an existing -directory; the given sources will be moved into this directory. +In the second form, the last argument refers to a directory, which has +to exist unless -p is specified, in which case it gets created; the +given sources will be moved into this directory. The index is updated after successful completion, but the change must still be committed. @@ -39,6 +40,9 @@ OPTIONS -n:: --dry-run:: Do nothing; only show what would happen +-p:: +--parents:: + Create missing parent directories in the or path. Similar to `mkdir -p', all missing leading directories are created. -v:: --verbose::