From patchwork Sun Nov 21 00:46:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12630659 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 51614C433FE for ; Sun, 21 Nov 2021 00:47:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237409AbhKUAuP (ORCPT ); Sat, 20 Nov 2021 19:50:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232469AbhKUAuI (ORCPT ); Sat, 20 Nov 2021 19:50:08 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF70DC061574 for ; Sat, 20 Nov 2021 16:47:03 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id r8so25262764wra.7 for ; Sat, 20 Nov 2021 16:47:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=BOYpUluqwNatwvKtNga45xDXh7nrcYdcitiqQBHzpx8=; b=kc0Rpr54quYgXSxJvhjMdtlkCNs9Ec+d3D6TPKucIxrsL+qyXfmyoq1K5QMCtUVWJP Au6VYqtnngbn/j6JZFfEh59sNYl9DkyJ8f/PZQMq9V+lthQsqJlakwri3YPltuAa40Dl o2XgjEImUmdRK9uIqc26u23m0TvoOG8HZhkMP/pXmueMQLzUG0o7nON3inOOT7h+nBDk lotDbhC5eE4ayGzE0Vj686aTCuo67tykXC1+UwylJHqEBL3JK1FT3z1GHTGNDDMYWkS1 Xvy2apxzU3szTbYJcA4Bgp3jTc3qBz+FiZtXoG44oHEprwdwAwt8QcMgL6qBYT5olipw CtpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=BOYpUluqwNatwvKtNga45xDXh7nrcYdcitiqQBHzpx8=; b=AKzSoIh81QJ7eskHIO73ITAg4JIx4yc0GNtN+oxWlvm4w/Muk13O0soW4EA+qcUlGu svtXV0o2OcVZEopG2rHV6jO08rv+QHktjLlviCkWUsRyd6Aou0386aWyKdOegxJfG15v 1C96J4kGaO7gnN8ZADphL9B1JqGeyQ07JcZuvaw1t9l3YT/gz5iIQN1qMuKEmo1n8dTe REO/x6OHZvSsy13J+M74rH/71AYKRAIeCMy6tORj+E/I6kvcbb/+yVQCBtqZzzGthWVl ZIVq0W5EEpVCx9x3jg2WqxAfV4TEpis9RsbdDbjmgzXvSOEMIUyscxe9kcAwQU1gWleH x63Q== X-Gm-Message-State: AOAM532UEGbYmJqb+uNcxFUCiwJ4+Emp/9v4Nhx9WswjUg79qgx7PRXb gi9KKztoM+0DQNdc/ZC8ZKbDxkEzbbA= X-Google-Smtp-Source: ABdhPJyFOW/eUny8jk7Ehx4J6Tnqu13nTdYtFEntWi1GjWlrfLY6H8Yp2aHHoy9iQQpDHuSlyS62LQ== X-Received: by 2002:adf:fe4b:: with SMTP id m11mr22284005wrs.136.1637455622122; Sat, 20 Nov 2021 16:47:02 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f81sm18587674wmf.22.2021.11.20.16.47.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 16:47:01 -0800 (PST) Message-Id: <0b71996a3b462d4147fb792b20057544b9ef1710.1637455620.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sun, 21 Nov 2021 00:46:53 +0000 Subject: [PATCH 1/8] t2501: add various tests for removing the current working directory Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren Numerous commands will remove empty working directories, especially if they are in the way of placing needed files. That is normally fine, but removing the current working directory can cause confusion for the user when they run subsequent commands. Add some tests checking for such problems. Signed-off-by: Elijah Newren --- t/t2501-cwd-empty.sh | 255 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100755 t/t2501-cwd-empty.sh diff --git a/t/t2501-cwd-empty.sh b/t/t2501-cwd-empty.sh new file mode 100755 index 00000000000..5dfb456a691 --- /dev/null +++ b/t/t2501-cwd-empty.sh @@ -0,0 +1,255 @@ +#!/bin/sh + +test_description='Test handling of the current working directory becoming empty' + +. ./test-lib.sh + +test_expect_success setup ' + test_commit init && + mkdir subdir && + test_commit subdir/file && + + git branch fd_conflict && + + mkdir -p foo/bar && + test_commit foo/bar/baz && + + git revert HEAD && + git tag reverted && + + git checkout fd_conflict && + git rm subdir/file.t && + echo not-a-directory >dirORfile && + git add dirORfile && + git commit -m dirORfile +' + +test_expect_failure 'checkout does not clean cwd incidentally' ' + git checkout foo/bar/baz && + test_path_is_dir foo/bar && + + ( + cd foo && + git checkout init && + cd .. + ) && + test_path_is_missing foo/bar/baz && + test_path_is_missing foo/bar && + test_path_is_dir foo +' + +test_expect_failure 'checkout fails if cwd needs to be removed' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + mkdir dirORfile && + ( + cd dirORfile && + + test_must_fail git checkout fd_conflict 2>../error && + grep "Refusing to remove the current working directory" ../error + ) && + + test_path_is_dir dirORfile +' + +test_expect_failure 'reset --hard does not clean cwd incidentally' ' + git checkout foo/bar/baz && + test_path_is_dir foo/bar && + + ( + cd foo && + git reset --hard init && + cd .. + ) && + test_path_is_missing foo/bar/baz && + test_path_is_missing foo/bar && + test_path_is_dir foo +' + +test_expect_failure 'reset --hard fails if cwd needs to be removed' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + mkdir dirORfile && + ( + cd dirORfile && + + test_must_fail git reset --hard fd_conflict 2>../error && + grep "Refusing to remove.*the current working directory" ../error + ) && + + test_path_is_dir dirORfile +' + +test_expect_failure 'merge does not remove cwd incidentally' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + ( + cd subdir && + git merge fd_conflict + ) && + + test_path_is_missing subdir/file.t && + test_path_is_dir subdir +' + +test_expect_failure 'merge fails if cwd needs to be removed' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + mkdir dirORfile && + ( + cd dirORfile && + test_must_fail git merge fd_conflict 2>../error && + grep "Refusing to remove the current working directory" ../error + ) && + + test_path_is_dir dirORfile +' + +test_expect_failure 'cherry-pick does not remove cwd incidentally' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + ( + cd subdir && + git cherry-pick fd_conflict + ) && + + test_path_is_missing subdir/file.t && + test_path_is_dir subdir +' + +test_expect_failure 'cherry-pick fails if cwd needs to be removed' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + mkdir dirORfile && + ( + cd dirORfile && + test_must_fail git cherry-pick fd_conflict 2>../error && + grep "Refusing to remove the current working directory" ../error + ) && + + test_path_is_dir dirORfile +' + +test_expect_failure 'rebase does not remove cwd incidentally' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + ( + cd subdir && + git rebase foo/bar/baz fd_conflict + ) && + + test_path_is_missing subdir/file.t && + test_path_is_dir subdir +' + +test_expect_failure 'rebase fails if cwd needs to be removed' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + mkdir dirORfile && + ( + cd dirORfile && + test_must_fail git rebase foo/bar/baz fd_conflict 2>../error && + grep "Refusing to remove the current working directory" ../error + ) && + + test_path_is_dir dirORfile +' + +test_expect_failure 'revert does not remove cwd incidentally' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + ( + cd subdir && + git revert subdir/file + ) && + + test_path_is_missing subdir/file.t && + test_path_is_dir subdir +' + +test_expect_failure 'revert fails if cwd needs to be removed' ' + git checkout fd_conflict && + git revert HEAD && + test_when_finished "git clean -fdx" && + + mkdir dirORfile && + ( + cd dirORfile && + test_must_fail git revert HEAD 2>../error && + grep "Refusing to remove the current working directory" ../error + ) && + + test_path_is_dir dirORfile +' + +test_expect_failure 'rm does not remove cwd incidentally' ' + test_when_finished "git reset --hard" && + git checkout foo/bar/baz && + + ( + cd foo && + git rm bar/baz.t + ) && + + test_path_is_missing foo/bar/baz && + test_path_is_missing foo/bar && + test_path_is_dir foo +' + +test_expect_failure 'apply does not remove cwd incidentally' ' + test_when_finished "git reset --hard" && + git checkout foo/bar/baz && + + ( + cd subdir && + git diff subdir/file init | git apply + ) && + + test_path_is_missing subdir/file.t && + test_path_is_dir subdir +' + +test_expect_failure 'clean does not remove cwd incidentally' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + mkdir empty && + mkdir untracked && + >untracked/random && + ( + cd untracked && + git clean -fd -e warnings :/ >../warnings && + grep "Refusing to remove current working directory" ../warnings + ) && + + test_path_is_missing empty && + test_path_is_missing untracked/random && + test_path_is_dir untracked +' + +test_expect_failure 'stash does not remove cwd incidentally' ' + git checkout foo/bar/baz && + test_when_finished "git clean -fdx" && + + mkdir untracked && + >untracked/random && + ( + cd untracked && + git stash --include-untracked && + git status + ) && + + test_path_is_missing untracked/random && + test_path_is_dir untracked +' + +test_done From patchwork Sun Nov 21 00:46:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12630657 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 B2EDBC433EF for ; Sun, 21 Nov 2021 00:47:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237312AbhKUAuL (ORCPT ); Sat, 20 Nov 2021 19:50:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237210AbhKUAuI (ORCPT ); Sat, 20 Nov 2021 19:50:08 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8166AC06173E for ; Sat, 20 Nov 2021 16:47:04 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id a9so25325594wrr.8 for ; Sat, 20 Nov 2021 16:47:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=vLrjPUiGJjgMCpzwqn9JDmb83ofz3HcEdCbxB0pFgAs=; b=MbBctB8u0H9TsudPU4fTvMrZsnDv4zBIbf0JoPlaYbUTEmj+5g02o3SdgS3uscYT9Y rYd3/jnz7fN4L3epKD0FdkAYHYE9keWAKja4mhTGeTxyoixznWjzqHOb4Ath3V6idDjp m8EyFTRYHYPntXZ8+u8wxBEH0qism58dghQBAao3x5arEtm5fIap/x/FQiD3NUH/BGJK SWOemEgJUgwdvoR4f/8U5pK78oXxKColY1labqpNwr1kCNEug7OJ6OzW2omK6STaGUHB 9cwwi2KqiWkYA9cJeI+uDZKQ/i1g85a1LdX+y7GSp4S2NGXFmEpKog8L4l6HsgTBe3S6 0GVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=vLrjPUiGJjgMCpzwqn9JDmb83ofz3HcEdCbxB0pFgAs=; b=erptyFy3v1Y87j0YpSTeg+dhx36SUKHtsd+E+4BS7zKfS7O5iDxbD+qqKcm0c1s9Hr 9Uetq5Qtw2MF+wQsmfNFtTL6Fvj0sD9IPoJSngPtOX366nPADxQviXKBFklwdgO7f7ai EgRi87l8/BkbdjUgSkuvZXS98+fToDK82bBHfbY+lEzNCqKq2FxZUqTx/SuypFEU8VNR f6mjxIhRhoFLhKy+GM1qnU1FwbMmIhVIoy341OIbAFtqLjKPrx94PTyl6P8qAlm4gHex r62qI6IBsRV2nkpb10jqSxTs8/mZiKBM3wXkhFuWexzCfs6qSez0kza1lxqAhHC1Iknu n4Jg== X-Gm-Message-State: AOAM530oc/PilM80rHBUGSFI10qJ5e/+kFSHt+EfqSajJekX7I8z0E2s LCOwjIJ+UxjgpAJXZic1w+pRWPFA3vs= X-Google-Smtp-Source: ABdhPJwBq1FontOzGQG/R1c+XVRSAxe+JaycAv4DR1tKVBaIpYTZY0+JMcdyV81L3f6V709b/DzqQg== X-Received: by 2002:a5d:69c5:: with SMTP id s5mr22313182wrw.283.1637455623035; Sat, 20 Nov 2021 16:47:03 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n32sm4586325wms.42.2021.11.20.16.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 16:47:02 -0800 (PST) Message-Id: <7b0c665fb75d3d73d9d8d03b629a09a0ec4244e6.1637455620.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sun, 21 Nov 2021 00:46:54 +0000 Subject: [PATCH 2/8] repository, setup: introduce the_cwd Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren Removing the current working directory causes all subsequent git commands (and likely a number of non-git commands) run from that directory to get confused and fail with a message about being unable to read the current working directory. That confuses end users, particularly since the command they get the error from is not the one that caused the problem; the problem came from the side-effect of some previous command. We would like to avoid removing the current working directory; towards this end, introduce a new the_cwd variable that tracks the current working directory. Subsequent commits will make use of this new variable. Signed-off-by: Elijah Newren --- repository.c | 1 + repository.h | 1 + setup.c | 2 ++ 3 files changed, 4 insertions(+) diff --git a/repository.c b/repository.c index c5b90ba93ea..69a106c553c 100644 --- a/repository.c +++ b/repository.c @@ -17,6 +17,7 @@ static struct repository the_repo; struct repository *the_repository; struct index_state the_index; +char *the_cwd; void initialize_the_repository(void) { diff --git a/repository.h b/repository.h index a057653981c..45de85d18ef 100644 --- a/repository.h +++ b/repository.h @@ -147,6 +147,7 @@ struct repository { }; extern struct repository *the_repository; +extern char *the_cwd; /* * Define a custom repository layout. Any field can be NULL, which diff --git a/setup.c b/setup.c index 347d7181ae9..4466fa55af3 100644 --- a/setup.c +++ b/setup.c @@ -887,6 +887,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv, set_git_dir(gitdirenv, 1); if (chdir(worktree)) die_errno(_("cannot chdir to '%s'"), worktree); + the_cwd = xstrdup(cwd->buf + offset); strbuf_addch(cwd, '/'); free(gitfile); return cwd->buf + offset; @@ -940,6 +941,7 @@ static const char *setup_discovered_git_dir(const char *gitdir, /* Make "offset" point past the '/' (already the case for root dirs) */ if (offset != offset_1st_component(cwd->buf)) offset++; + the_cwd = xstrdup(cwd->buf + offset); /* Add a '/' at the end */ strbuf_addch(cwd, '/'); return cwd->buf + offset; From patchwork Sun Nov 21 00:46:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12630663 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 8FEA8C433EF for ; Sun, 21 Nov 2021 00:47:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232469AbhKUAuR (ORCPT ); Sat, 20 Nov 2021 19:50:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237218AbhKUAuJ (ORCPT ); Sat, 20 Nov 2021 19:50:09 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20AC1C061574 for ; Sat, 20 Nov 2021 16:47:05 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id w29so25282037wra.12 for ; Sat, 20 Nov 2021 16:47:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=7uwKibAxpKQ7iPujXJcM6AnuiMkYSv9Z39P/Lfd4iUY=; b=QEIkcCJO42H+54IntS9gFfTh2tU5zmBman4EVuxtZPv2UXzz/Is1MHIPCMWoKBXoc5 esDKhsjBR75dKgnbDUDlIQwXx00oLwRv9d4V6fDpG9oCaXCM8Yp7xKnHwb//30YrgB1x EScwusqDaUieDQUYuYHfSnclqgYy0EkIcTPpiUZJu2zP6iZtTdzBmDcAjxVMGg12TkMs D1tlsFAyRfafoIrM4qDvQAvPZwxHmF1YueHqRfTd6KFYS/Sunl7MHfumWI0oI6qQCiIo DWThvzMSvdnIQA8q/OrQJQrPs8JfjMI33uieynsw9VmGhjaSuEm2BboUuccp0+fvM3cL gXcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=7uwKibAxpKQ7iPujXJcM6AnuiMkYSv9Z39P/Lfd4iUY=; b=8P7UZULa63oAabTwqbNCLEDlyCPrTr62TRDnylfLGhHywpuUoPIyaW/QrskXZe6pRn nny3W6/CM28nuJsdjbVw4k0teIVy6aWcnR0KjCjIsiSqla9UZivlJG3m+owPr0Xfz3ao 3Bif8GN+imjYH1iWyCvrAGRXIxqE2wr62qr3RmOzkSeRmgIDoC9iKjrszd+yk3grpl1m Vmjb+/T2mhBO84qxdX/te7FbmVWxLg6CS1slhfSa//4bqJHPkd160C2E7ExgpsRcO5Ve EpbXD0um9Jz+AUki8/3sYTSAaxn4O0ZC6Vx65i5suYp3nUqctBHpGaBA1FwOVrOeFUi0 2BjA== X-Gm-Message-State: AOAM532b8jDtkQiDnVW0swGXPETiuL358mjSYhX/yhztJuXwMKB+jj7l LVCdKocLQC7vl8W5IFf9EhINlFsJEGI= X-Google-Smtp-Source: ABdhPJwoy7gn/I8cGsAwjfe8Vmvd7O2ziODWaSzplDX1+lpFyMlFbE8vw22jLTfakjRXtOjiwyoERw== X-Received: by 2002:a5d:47a1:: with SMTP id 1mr22002203wrb.436.1637455623594; Sat, 20 Nov 2021 16:47:03 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id bg12sm2489473wmb.5.2021.11.20.16.47.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 16:47:03 -0800 (PST) Message-Id: <94bf468be0972ce3d6037e6688749cab01269b14.1637455620.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sun, 21 Nov 2021 00:46:55 +0000 Subject: [PATCH 3/8] unpack-trees: refuse to remove the current working directory Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren In the past, when a directory needs to be removed to make room for a file, we have always errored out when that directory contains any untracked (but not ignored) files. Add an extra condition on that: also error out if the directory is the current working directory. Signed-off-by: Elijah Newren --- t/t2501-cwd-empty.sh | 10 +++++----- unpack-trees.c | 16 ++++++++++++---- unpack-trees.h | 1 + 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/t/t2501-cwd-empty.sh b/t/t2501-cwd-empty.sh index 5dfb456a691..212676d71c3 100755 --- a/t/t2501-cwd-empty.sh +++ b/t/t2501-cwd-empty.sh @@ -38,7 +38,7 @@ test_expect_failure 'checkout does not clean cwd incidentally' ' test_path_is_dir foo ' -test_expect_failure 'checkout fails if cwd needs to be removed' ' +test_expect_success 'checkout fails if cwd needs to be removed' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && @@ -95,7 +95,7 @@ test_expect_failure 'merge does not remove cwd incidentally' ' test_path_is_dir subdir ' -test_expect_failure 'merge fails if cwd needs to be removed' ' +test_expect_success 'merge fails if cwd needs to be removed' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && @@ -122,7 +122,7 @@ test_expect_failure 'cherry-pick does not remove cwd incidentally' ' test_path_is_dir subdir ' -test_expect_failure 'cherry-pick fails if cwd needs to be removed' ' +test_expect_success 'cherry-pick fails if cwd needs to be removed' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && @@ -149,7 +149,7 @@ test_expect_failure 'rebase does not remove cwd incidentally' ' test_path_is_dir subdir ' -test_expect_failure 'rebase fails if cwd needs to be removed' ' +test_expect_success 'rebase fails if cwd needs to be removed' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && @@ -176,7 +176,7 @@ test_expect_failure 'revert does not remove cwd incidentally' ' test_path_is_dir subdir ' -test_expect_failure 'revert fails if cwd needs to be removed' ' +test_expect_success 'revert fails if cwd needs to be removed' ' git checkout fd_conflict && git revert HEAD && test_when_finished "git clean -fdx" && diff --git a/unpack-trees.c b/unpack-trees.c index 89ca95ce90b..8879b8d6c8c 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -36,6 +36,9 @@ static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = { /* ERROR_NOT_UPTODATE_DIR */ "Updating '%s' would lose untracked files in it", + /* ERROR_CWD_IN_THE_WAY */ + "Refusing to remove '%s' since it is the current working directory.", + /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ "Untracked working tree file '%s' would be overwritten by merge.", @@ -131,6 +134,9 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts, msgs[ERROR_NOT_UPTODATE_DIR] = _("Updating the following directories would lose untracked files in them:\n%s"); + msgs[ERROR_CWD_IN_THE_WAY] = + _("Refusing to remove the current working directory:\n%s"); + if (!strcmp(cmd, "checkout")) msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) ? _("The following untracked working tree files would be removed by checkout:\n%%s" @@ -2146,10 +2152,7 @@ static int verify_clean_subdirectory(const struct cache_entry *ce, cnt++; } - /* - * Then we need to make sure that we do not lose a locally - * present file that is not ignored. - */ + /* Do not lose a locally present file that is not ignored. */ pathbuf = xstrfmt("%.*s/", namelen, ce->name); memset(&d, 0, sizeof(d)); @@ -2160,6 +2163,11 @@ static int verify_clean_subdirectory(const struct cache_entry *ce, free(pathbuf); if (i) return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name); + + /* Do not lose the current working directory. */ + if (the_cwd && !strcmp(the_cwd, ce->name)) + return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name); + return cnt; } diff --git a/unpack-trees.h b/unpack-trees.h index 71ffb7eeb0c..efb9edfbb27 100644 --- a/unpack-trees.h +++ b/unpack-trees.h @@ -19,6 +19,7 @@ enum unpack_trees_error_types { ERROR_WOULD_OVERWRITE = 0, ERROR_NOT_UPTODATE_FILE, ERROR_NOT_UPTODATE_DIR, + ERROR_CWD_IN_THE_WAY, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, ERROR_BIND_OVERLAP, From patchwork Sun Nov 21 00:46:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12630661 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 A6B0EC433FE for ; Sun, 21 Nov 2021 00:47:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237331AbhKUAuS (ORCPT ); Sat, 20 Nov 2021 19:50:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237219AbhKUAuJ (ORCPT ); Sat, 20 Nov 2021 19:50:09 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1778C06173E for ; Sat, 20 Nov 2021 16:47:05 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id d27so25347383wrb.6 for ; Sat, 20 Nov 2021 16:47:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=WUJYX8Jwxtngb9Y6+X6CK5tzG2PUQkACmxWU1cVCOcQ=; b=o5Yi4PtK5fNY3aDVGtEI2fMZppS/OJLN17unydySRB8ycWzSoHi6yhqPhgOsYGmjWI d33lTBRsxy7jxxkHRTX4WziQaWlBvdsOzzWnhA5s3/fp43sPxyfzUCNUnWPj5DKUGFL4 o+Q/KMG4WaEC9tcRm+YBjunwQBLyqq3WZ5Lr+BcWJA62hqjaGSg5FJoDULKiEQdpKkmj HUcidkghvlATpeEZZkfLrDzH454XqStblC2zQW0nWzrkETytAGUBRBw30t5W1O9o6i3t F1lG83QXLC8/A4hcDmBOh+wF3CYmMubR8cJSl8/8IGv7ZUH/6a1gSi4otA/j01uVv1+4 FBFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=WUJYX8Jwxtngb9Y6+X6CK5tzG2PUQkACmxWU1cVCOcQ=; b=UwOKjVkQ1BEFNS9+4ds2+jzpLnRvWl4XCinBbvtAy1PbQ1dEcO55WsOu44HSj51O2u VNHPjT76DBHWW+PFzzqjNq5HBwDqHbuITWld0vh8mGgq4UwLQT45k7FjPpvlDVTNoDFm PzBXL8Q5ShuFgABH870a5DKjzL5X1owtMSggq+ViG9I2ovVpwSY2Ib/75NQKNkmQOQHD PXassOO7oJXfhWulbS1e+4qpqogDzkVARPf1Ma56PBjPsKDcqCzAVZ3xmN3KfcS6pVPC /L5T0NLCSo2AdVaQXAvR2D/8h7swob6XECW0HvwXRO9C01+b0rZkFgftYapqrt6bd71C GsFg== X-Gm-Message-State: AOAM531Fi59nUIABh/FtW4ShA5IcwJBckmKgCWNhEV8060dprMeS5Z21 dqzb7t5aAQcEtd37o4ckHX3TWx0ZMxs= X-Google-Smtp-Source: ABdhPJzd4rXxJq3PVHM1t1S3XFWMGMNSuU0pjEJEne4qJMEIvlFInTEyU22iby9xTm57rYT8D3Wayw== X-Received: by 2002:adf:fe8e:: with SMTP id l14mr22832048wrr.177.1637455624246; Sat, 20 Nov 2021 16:47:04 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id x21sm15851573wmc.14.2021.11.20.16.47.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 16:47:03 -0800 (PST) Message-Id: <39830fffd45e103b608e2ac7869a509fcd5dc7db.1637455620.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sun, 21 Nov 2021 00:46:56 +0000 Subject: [PATCH 4/8] unpack-trees: add special cwd handling Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren When running commands such as `git reset --hard` from a subdirectory, if that subdirectory is in the way of adding needed files, bail with an error message. Note that this change looks kind of like it duplicates the new lines of code from the previous commit in verify_clean_subdirectory(). However, when we are preserving untracked files, we would rather any error messages about untracked files being in the way take precedence over error messages about a subdirectory that happens to be the current working directory being in the way. But in the UNPACK_RESET_OVERWRITE_UNTRACKED case, there is no untracked checking to be done, so we simply add a special case near the top of verify_absent_1. Signed-off-by: Elijah Newren --- t/t2501-cwd-empty.sh | 2 +- unpack-trees.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/t/t2501-cwd-empty.sh b/t/t2501-cwd-empty.sh index 212676d71c3..526d8ec2ee3 100755 --- a/t/t2501-cwd-empty.sh +++ b/t/t2501-cwd-empty.sh @@ -67,7 +67,7 @@ test_expect_failure 'reset --hard does not clean cwd incidentally' ' test_path_is_dir foo ' -test_expect_failure 'reset --hard fails if cwd needs to be removed' ' +test_expect_success 'reset --hard fails if cwd needs to be removed' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && diff --git a/unpack-trees.c b/unpack-trees.c index 8879b8d6c8c..d1ce4f4a35b 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -2260,10 +2260,18 @@ static int verify_absent_1(const struct cache_entry *ce, int len; struct stat st; - if (o->index_only || !o->update || - o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) + if (o->index_only || !o->update) return 0; + if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) { + /* Avoid nuking cwd... */ + if (the_cwd && !strcmp(the_cwd, ce->name)) + return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, + ce->name); + /* ...but nuke anything else. */ + return 0; + } + len = check_leading_path(ce->name, ce_namelen(ce), 0); if (!len) return 0; From patchwork Sun Nov 21 00:46:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12630669 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 E432AC433EF for ; Sun, 21 Nov 2021 00:47:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237548AbhKUAuY (ORCPT ); Sat, 20 Nov 2021 19:50:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237276AbhKUAuK (ORCPT ); Sat, 20 Nov 2021 19:50:10 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C15AC06174A for ; Sat, 20 Nov 2021 16:47:06 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id f7-20020a1c1f07000000b0032ee11917ceso10575293wmf.0 for ; Sat, 20 Nov 2021 16:47:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=qvnZz8QiNul7WHe0ZnCPt0rBbpsoLenQCIBGu6vkc6A=; b=D5z36zJKHbD3px3mPhwNpmKDbxPpTElrNeeoZkXclOMIc0kiQ58YGufhycOg7WdbrK Gg/wdOmywqoAPYFJPp6sU4dkSA5x0PF3dZydhFWdAfiFtvZFQRAIYhj/IEuLhJ5skvCx x9BUDIhP7L7HiNqaHxaCtiqM3/W/BM+HS4qHo7Ovg3UN16IA50P/fVZ1Jl4ls4AEeq6Q sgZxCY+srS6lHaGeRG7cKJy+8wK26Re9ed/2Onj8B5zKnJHU9D8BQ4p5cXxh+t0LfHki YS4FZeHYHwueScUcRT7tYte/Epui8Hf0PJVpyBUkgR/qGvYY3ycP/tGJ6dYU5JRjUF+i ITrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=qvnZz8QiNul7WHe0ZnCPt0rBbpsoLenQCIBGu6vkc6A=; b=U77dPRyUYYVyrB/ZaYsIpXazXLNctc8HOcCilnHVzjtVDH1j9Fs/W7y3zw6WVOJb37 xsqF3oVUEjPzmEz90hhMc5Id2L5p6W++CVw+ZGmRigYfyykyFrFexQTiW6UF0+pk2mgC pii3Ny6mEGa29aVEQfvAb8V9vsZzizyfVHeAwyeT8YrQ8+2HFkjplZ2k1x0q3MdvJZUx yakkCfJChgmA3ZhyK03cgIQ2ARMy/tB2egM5dKk3VC7HmyJgIUb/Hfg3rhdC163Imove R+t3sJlxEE5QCFjCoGuc84wL9SmgJ7Op6fPgENGI0ZZ6I3Y8L9/LY+H1BKGbne43NUrH Lkxw== X-Gm-Message-State: AOAM5318v0Jk2Z3AcspjbY/ktYddmgPykukKRrw5WDcg+TDAkbAcYy2L C0sCwCt9/I13XaAmBN6kz5hmth77c3g= X-Google-Smtp-Source: ABdhPJwsKUI//TxdSxeSc/yK/KKmzPSRo5EVUHAl2/DzC7C1HtHBHZa+egn58+RW04ellvfdKUuEiA== X-Received: by 2002:a05:600c:24c:: with SMTP id 12mr14809098wmj.124.1637455624820; Sat, 20 Nov 2021 16:47:04 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id k15sm4139988wrx.53.2021.11.20.16.47.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 16:47:04 -0800 (PST) Message-Id: <8a69d2878c99e1b9321e57073d266cf797dc5630.1637455620.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sun, 21 Nov 2021 00:46:57 +0000 Subject: [PATCH 5/8] symlinks: do not include current working directory in dir removal Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren symlinks has a pair of schedule_dir_for_removal() and remove_scheduled_dirs() functions that ensure that directories made empty by removing other files also themselves get removed. However, we want to exclude the current working directory and leave it around so that subsequent git commands (and non-git commands) that the user runs afterwards don't cause the user to get confused. Signed-off-by: Elijah Newren --- symlinks.c | 12 +++++++++++- t/t2501-cwd-empty.sh | 12 ++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/symlinks.c b/symlinks.c index 5232d02020c..84622bedcde 100644 --- a/symlinks.c +++ b/symlinks.c @@ -275,11 +275,18 @@ static int threaded_has_dirs_only_path(struct cache_def *cache, const char *name static struct strbuf removal = STRBUF_INIT; +static int cant_remove(char *dirname) +{ + if (the_cwd && !strcmp(dirname, the_cwd)) + return 1; + return rmdir(dirname); +} + static void do_remove_scheduled_dirs(int new_len) { while (removal.len > new_len) { removal.buf[removal.len] = '\0'; - if (rmdir(removal.buf)) + if (cant_remove(removal.buf)) break; do { removal.len--; @@ -293,6 +300,9 @@ void schedule_dir_for_removal(const char *name, int len) { int match_len, last_slash, i, previous_slash; + if (the_cwd && !strcmp(name, the_cwd)) + return; /* Do not remove the current working directory */ + match_len = last_slash = i = longest_path_match(name, len, removal.buf, removal.len, &previous_slash); diff --git a/t/t2501-cwd-empty.sh b/t/t2501-cwd-empty.sh index 526d8ec2ee3..b92e1a9bb16 100755 --- a/t/t2501-cwd-empty.sh +++ b/t/t2501-cwd-empty.sh @@ -24,7 +24,7 @@ test_expect_success setup ' git commit -m dirORfile ' -test_expect_failure 'checkout does not clean cwd incidentally' ' +test_expect_success 'checkout does not clean cwd incidentally' ' git checkout foo/bar/baz && test_path_is_dir foo/bar && @@ -53,7 +53,7 @@ test_expect_success 'checkout fails if cwd needs to be removed' ' test_path_is_dir dirORfile ' -test_expect_failure 'reset --hard does not clean cwd incidentally' ' +test_expect_success 'reset --hard does not clean cwd incidentally' ' git checkout foo/bar/baz && test_path_is_dir foo/bar && @@ -82,7 +82,7 @@ test_expect_success 'reset --hard fails if cwd needs to be removed' ' test_path_is_dir dirORfile ' -test_expect_failure 'merge does not remove cwd incidentally' ' +test_expect_success 'merge does not remove cwd incidentally' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && @@ -109,7 +109,7 @@ test_expect_success 'merge fails if cwd needs to be removed' ' test_path_is_dir dirORfile ' -test_expect_failure 'cherry-pick does not remove cwd incidentally' ' +test_expect_success 'cherry-pick does not remove cwd incidentally' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && @@ -136,7 +136,7 @@ test_expect_success 'cherry-pick fails if cwd needs to be removed' ' test_path_is_dir dirORfile ' -test_expect_failure 'rebase does not remove cwd incidentally' ' +test_expect_success 'rebase does not remove cwd incidentally' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && @@ -163,7 +163,7 @@ test_expect_success 'rebase fails if cwd needs to be removed' ' test_path_is_dir dirORfile ' -test_expect_failure 'revert does not remove cwd incidentally' ' +test_expect_success 'revert does not remove cwd incidentally' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && From patchwork Sun Nov 21 00:46:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12630667 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 6478CC433FE for ; Sun, 21 Nov 2021 00:47:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237444AbhKUAuW (ORCPT ); Sat, 20 Nov 2021 19:50:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237292AbhKUAuL (ORCPT ); Sat, 20 Nov 2021 19:50:11 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DCA83C061756 for ; Sat, 20 Nov 2021 16:47:06 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id u1so25278214wru.13 for ; Sat, 20 Nov 2021 16:47:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=fCdjzoDdlkgCxhufNCKzSpaLeuUqZfQCiQ4S923ZeGA=; b=VnexvILYI+SrcGt11AZ6y0Gz1Llt1WgbV+mcRxptmeHgnOXscGeGUsn3LTwHKpInR8 hHDQRFK4JNHVmkiGqxLADWnlluzGZuO357vgHKGZ1fnI9zzYJWOtp5FzJ51zYzujnwPi fU8KKsMJH/ShSUU/Or1wsHteEiLCi9cmhZhqa505mZWammtrFnBbrqksoPvZp90jYMf7 A/mbij/PzyjCbtujn+9RC/QHDThxqXQUJFr+hGcYGf7AhP0aipsNOnaOzccE/0Ocg0h4 x+exLsX9JBbNXiI2wvywcmjMfJGnH004f0vYZfBUmU8MsCBvPhTwQS2zdJwKe8IItfTE mO7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=fCdjzoDdlkgCxhufNCKzSpaLeuUqZfQCiQ4S923ZeGA=; b=1ORjtGsgPrkNb+bhITcxJ3HH2Af4TFSxvqN0xyri8v2rY3ZUAsdQVq8XTfAlwKqzcO PNYdq8fZDnfodTc1f5ym7wSzh8f56u6RPsPAi6fquLf5sxc1Kjk2Y8pprZwY4p0e8CUL uipxXHOvccJ0eFqVJhI7bG78GZpbOyA6niXyiLjsTgXSRVs84i5ZL+UESpdCiKC9k1wc Xr0BkosUowcjph27s57rspGZHNh/yrDKUfiObyQUx93L9zVD37+MEMbyGCywxap7RG6b wfxbj/Ga64s0FbaGqnEfXKejkhlLAWZeqB4Zf+52jSdyVBb/UTaZtQo0Xpc0pCcY5l4o S9Xg== X-Gm-Message-State: AOAM5307CspyMQXwuyJo4agfdFjeXJDualJFWMshBb6VsSnyTZbOmSpm BwbcJ74E4cr9PZ21ZnYs4HqG26hFbpY= X-Google-Smtp-Source: ABdhPJwkRJgQkppvgUUoKZ40DcEakvOf4S6rrxukdxa4uncQE4PeXdAmyM6PdtHZ2TxxdqVODYz5+g== X-Received: by 2002:a05:6000:15c6:: with SMTP id y6mr22322919wry.422.1637455625406; Sat, 20 Nov 2021 16:47:05 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id r15sm16057633wmh.13.2021.11.20.16.47.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 16:47:05 -0800 (PST) Message-Id: <69bcaf0aab28fec145742f7183b1d89b12eaf0f9.1637455620.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sun, 21 Nov 2021 00:46:58 +0000 Subject: [PATCH 6/8] clean: do not attempt to remove current working directory Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren Signed-off-by: Elijah Newren --- builtin/clean.c | 29 ++++++++++++++++++++--------- t/t2501-cwd-empty.sh | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/builtin/clean.c b/builtin/clean.c index 98a2860409b..17d54be57b7 100644 --- a/builtin/clean.c +++ b/builtin/clean.c @@ -36,6 +36,8 @@ static const char *msg_skip_git_dir = N_("Skipping repository %s\n"); static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n"); static const char *msg_warn_remove_failed = N_("failed to remove %s"); static const char *msg_warn_lstat_failed = N_("could not lstat %s\n"); +static const char *msg_skip_cwd = N_("Refusing to remove current working directory\n"); +static const char *msg_would_skip_cwd = N_("Would refuse to remove current working directory\n"); enum color_clean { CLEAN_COLOR_RESET = 0, @@ -231,16 +233,25 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, strbuf_setlen(path, original_len); if (*dir_gone) { - res = dry_run ? 0 : rmdir(path->buf); - if (!res) - *dir_gone = 1; - else { - int saved_errno = errno; - quote_path(path->buf, prefix, "ed, 0); - errno = saved_errno; - warning_errno(_(msg_warn_remove_failed), quoted.buf); + int prefixlen = prefix ? strlen(prefix) : 0; + if (prefix && + path->len == prefixlen + 2 && + !strncmp(path->buf, prefix, prefixlen) && + !strcmp(path->buf + prefixlen, "./")) { + printf("%s", dry_run ? _(msg_would_skip_cwd) : _(msg_skip_cwd)); *dir_gone = 0; - ret = 1; + } else { + res = dry_run ? 0 : rmdir(path->buf); + if (!res) + *dir_gone = 1; + else { + int saved_errno = errno; + quote_path(path->buf, prefix, "ed, 0); + errno = saved_errno; + warning_errno(_(msg_warn_remove_failed), quoted.buf); + *dir_gone = 0; + ret = 1; + } } } diff --git a/t/t2501-cwd-empty.sh b/t/t2501-cwd-empty.sh index b92e1a9bb16..20e1b6adede 100755 --- a/t/t2501-cwd-empty.sh +++ b/t/t2501-cwd-empty.sh @@ -218,7 +218,7 @@ test_expect_failure 'apply does not remove cwd incidentally' ' test_path_is_dir subdir ' -test_expect_failure 'clean does not remove cwd incidentally' ' +test_expect_success 'clean does not remove cwd incidentally' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && From patchwork Sun Nov 21 00:46:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12630665 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 44233C433F5 for ; Sun, 21 Nov 2021 00:47:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237359AbhKUAuU (ORCPT ); Sat, 20 Nov 2021 19:50:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237302AbhKUAuL (ORCPT ); Sat, 20 Nov 2021 19:50:11 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A79AC061757 for ; Sat, 20 Nov 2021 16:47:07 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id b12so25320300wrh.4 for ; Sat, 20 Nov 2021 16:47:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=Zn2pluaaPf4nCJdurmQT9dENP+qQU+JMTqpsfX/z46A=; b=ARj/AcLbg+5lXzkO7ksHMjDNcrsIG34FmXQOWbcfxz3AVSubEcnCwSeHvuS25RU3TJ O0nyX+GHfXaDEl1MxzoWvOfZGxD93eWCclBri5qdiap9do0rmIomsv6nG2QZ4XU9pX+W pr+dkRiAReW8PYPV4jfHPox9W+cIeskJTq/W7cURUfTywOmLQhqtdMduL7WLkmN9XMM3 OgYiPahMmXw+y+wVWWLmnpMhhox5zoU58nIXXLAH5bDUyaXyx671VJ+uGvWZzv6r49qv lUN/B/uRnggHBbrlMNZTEu7AFIPgQkjXO9TrClnso+/T61t2CCJInN8fpnAF77A80vzi bD2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=Zn2pluaaPf4nCJdurmQT9dENP+qQU+JMTqpsfX/z46A=; b=ka8dQNNTGdwdHo4aMEEXYTNawVk20qrEEx+M/K4wlyTLVIJEo/Z8tcbDUqXh0USCn2 PxYnQ1K1T15ZayibqnTnBaR4jaf0YJ7GXkG81Qe52+Mrg+Zc9ji/RvwArpZ44w1mL3f5 bXRdBhNsONkx8uZZAxNns+AtbH6wSUJgo8sSJmeGS982BdhqkpCJ0qCwMbV/mqOLRh9V TsATbp1KNhXk3CA9t2Xux/oetFIWhP6cjLqNJR7DrRKM6HPTD37259KlXfyxTt+7T92b DlSwU9uS79U7XU9I85S0QONlG4hdn+P13T6EdNRlWlrS8isbxEKbKHo2rK4Ms0Fkt2p+ 0NKw== X-Gm-Message-State: AOAM530OBW0E5ZUacU7UQ3PW9VeY7v0b9jv5QDYPRYBl7AQdPpWnJM1R je9WQLEUE7M7YRaI1meiv2s4OtyBbt8= X-Google-Smtp-Source: ABdhPJyYzcwaetSr18YP47QLb4usqv/t35ElxmTeVABGjYcYyCNjPg603Bgc4eZvvroszFH2dcQdIw== X-Received: by 2002:adf:e109:: with SMTP id t9mr21703361wrz.387.1637455626008; Sat, 20 Nov 2021 16:47:06 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d8sm4192397wrm.76.2021.11.20.16.47.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 16:47:05 -0800 (PST) Message-Id: <93005b17c2880407872340d4927007db0d9e97b6.1637455620.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sun, 21 Nov 2021 00:46:59 +0000 Subject: [PATCH 7/8] stash: do not attempt to remove current working directory Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren Signed-off-by: Elijah Newren --- builtin/stash.c | 13 ++++++++----- t/t2501-cwd-empty.sh | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/builtin/stash.c b/builtin/stash.c index a0ccc8654df..556287cdb56 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -1404,7 +1404,8 @@ static int create_stash(int argc, const char **argv, const char *prefix) return ret; } -static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet, +static int do_push_stash(const struct pathspec *ps, const char *stash_msg, + const char *prefix, int quiet, int keep_index, int patch_mode, int include_untracked) { int ret = 0; @@ -1485,8 +1486,10 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q struct child_process cp = CHILD_PROCESS_INIT; cp.git_cmd = 1; + if (prefix) + strvec_pushl(&cp.args, "-C", prefix, NULL); strvec_pushl(&cp.args, "clean", "--force", - "--quiet", "-d", NULL); + "--quiet", "-d", ":/", NULL); if (include_untracked == INCLUDE_ALL_FILES) strvec_push(&cp.args, "-x"); if (run_command(&cp)) { @@ -1656,8 +1659,8 @@ static int push_stash(int argc, const char **argv, const char *prefix, die(_("--pathspec-file-nul requires --pathspec-from-file")); } - return do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode, - include_untracked); + return do_push_stash(&ps, stash_msg, prefix, quiet, keep_index, + patch_mode, include_untracked); } static int save_stash(int argc, const char **argv, const char *prefix) @@ -1693,7 +1696,7 @@ static int save_stash(int argc, const char **argv, const char *prefix) stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' '); memset(&ps, 0, sizeof(ps)); - ret = do_push_stash(&ps, stash_msg, quiet, keep_index, + ret = do_push_stash(&ps, stash_msg, prefix, quiet, keep_index, patch_mode, include_untracked); strbuf_release(&stash_msg_buf); diff --git a/t/t2501-cwd-empty.sh b/t/t2501-cwd-empty.sh index 20e1b6adede..ff4e7cd89fa 100755 --- a/t/t2501-cwd-empty.sh +++ b/t/t2501-cwd-empty.sh @@ -236,7 +236,7 @@ test_expect_success 'clean does not remove cwd incidentally' ' test_path_is_dir untracked ' -test_expect_failure 'stash does not remove cwd incidentally' ' +test_expect_success 'stash does not remove cwd incidentally' ' git checkout foo/bar/baz && test_when_finished "git clean -fdx" && From patchwork Sun Nov 21 00:47:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12630671 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 AEB49C4332F for ; Sun, 21 Nov 2021 00:47:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237601AbhKUAuZ (ORCPT ); Sat, 20 Nov 2021 19:50:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237326AbhKUAuM (ORCPT ); Sat, 20 Nov 2021 19:50:12 -0500 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43EFBC06173E for ; Sat, 20 Nov 2021 16:47:08 -0800 (PST) Received: by mail-wm1-x332.google.com with SMTP id c71-20020a1c9a4a000000b0032cdcc8cbafso10508752wme.3 for ; Sat, 20 Nov 2021 16:47:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=9h8b2WP43X1mVJW7SQOhSXJLeLMjNAq60C6WDGw+TiA=; b=GM5+5SoBmD3m62LR+oRFK805semoQ6+feO+sgldtm8VQSoZQzAJ58RAsgshPdoecCH WBLzhBQtKraQAPT3xYGPGCYJjzkDJb5eiOK96Ogh8TX9pIB9HVhk0arMqO6mb8qSvb0x hkt3ZzqUH6cd7XFgjmcoqcpRCD+poMmghsX+2rIG4jok3JMeGI7dF3i6uEDU6SAkCQ0K tneTeZrK+fxis+60vLNGo4VZFCjb9WiFZJYHxSKjCR4IDh6Zm+CkCy9ohlCeawIM0C5A ySHy3EB+KxCtDbMPTaHYJIW+Vj3yYWiYPO8jUDaqxeURSxkOiaaDVxeo23o4qhgqv2Gw fNhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=9h8b2WP43X1mVJW7SQOhSXJLeLMjNAq60C6WDGw+TiA=; b=FHsoCg+OmzlVW/XwJm3TLHH2rPPhYz+op3/lsibOKSFZfU88D/mgSsPQnfgAipDa7z +MdAJIbgu2Bhr8OGASPjY9tIgG0QgZZDcjnZUFLXZ5js3Ywu6xmTmCH09EyDlRwqH6Di 0cLYJ1l75co2jEoS7McPn9GuDjrW86q1GbYoF27WclG8udpyQ0Ghvy8lzsHYBhSptiBv /BGVFS8qcwj8QanB/ICjWyvG86zWVF3AnyqL0Kh1sO+Z5p3hq/ZQYIeT4uNivcldJ9Mt WBZlsZFyKN94A75fsWtPsB2YIoUiyQqZTWZhP86xmh1ewAdn9IwHulY2fA903kT3eR3d z92w== X-Gm-Message-State: AOAM533oB1xm8DyPO/u3dBk8G6H8u8M/t1EywY3/j5vod2Cuv+6AgRkm 2S2IZRYx8TslGnUAKyIIGHtYyZJJ5+s= X-Google-Smtp-Source: ABdhPJxRFOm0bqQ8N9YryfvJ0uKb5d+eutoQn/LZZQmr4KqNYkpfmkqE7PKvvFRJp98bhRdrawXXzw== X-Received: by 2002:a7b:cc8f:: with SMTP id p15mr15296601wma.129.1637455626682; Sat, 20 Nov 2021 16:47:06 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t189sm4314596wma.8.2021.11.20.16.47.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Nov 2021 16:47:06 -0800 (PST) Message-Id: In-Reply-To: References: Date: Sun, 21 Nov 2021 00:47:00 +0000 Subject: [PATCH 8/8] dir: avoid removing the current working directory Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren dir has a convenient remove_path() helper that will both remove a file in a directory and remove its containing directory if it becomes empty as a result of the removal, recursing all the way up. However, we do not want the current working directory to be removed, even if it becomes empty. dir also has a remove_dir_recursively() function which appears to mostly be used to remove metadata directories or temporary directories or submodules or worktrees. I am not sure if it needs to be protected against removing the current working directory, but did so for good measure. Signed-off-by: Elijah Newren --- dir.c | 11 ++++++++--- t/t2501-cwd-empty.sh | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/dir.c b/dir.c index 94489298f4c..15e7639a158 100644 --- a/dir.c +++ b/dir.c @@ -3259,9 +3259,12 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) closedir(dir); strbuf_setlen(path, original_len); - if (!ret && !keep_toplevel && !kept_down) - ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1; - else if (kept_up) + if (!ret && !keep_toplevel && !kept_down) { + if (the_cwd && !strcmp(the_cwd, path->buf)) + ret = -1; /* Do not remove current working directory */ + else + ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1; + } else if (kept_up) /* * report the uplevel that it is not an error that we * did not rmdir() our directory. @@ -3327,6 +3330,8 @@ int remove_path(const char *name) slash = dirs + (slash - name); do { *slash = '\0'; + if (the_cwd && !strcmp(the_cwd, dirs)) + break; } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/'))); free(dirs); } diff --git a/t/t2501-cwd-empty.sh b/t/t2501-cwd-empty.sh index ff4e7cd89fa..4362e7b15e5 100755 --- a/t/t2501-cwd-empty.sh +++ b/t/t2501-cwd-empty.sh @@ -191,7 +191,7 @@ test_expect_success 'revert fails if cwd needs to be removed' ' test_path_is_dir dirORfile ' -test_expect_failure 'rm does not remove cwd incidentally' ' +test_expect_success 'rm does not remove cwd incidentally' ' test_when_finished "git reset --hard" && git checkout foo/bar/baz && @@ -205,7 +205,7 @@ test_expect_failure 'rm does not remove cwd incidentally' ' test_path_is_dir foo ' -test_expect_failure 'apply does not remove cwd incidentally' ' +test_expect_success 'apply does not remove cwd incidentally' ' test_when_finished "git reset --hard" && git checkout foo/bar/baz &&