From patchwork Wed Jul 1 09:36:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Wijen X-Patchwork-Id: 11635937 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 61EA813B4 for ; Wed, 1 Jul 2020 09:46:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 48BA12077D for ; Wed, 1 Jul 2020 09:46:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726343AbgGAJqE (ORCPT ); Wed, 1 Jul 2020 05:46:04 -0400 Received: from smtp01.domein-it.com ([92.48.232.134]:50407 "EHLO smtp01.domein-it.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729125AbgGAJqD (ORCPT ); Wed, 1 Jul 2020 05:46:03 -0400 X-Greylist: delayed 531 seconds by postgrey-1.27 at vger.kernel.org; Wed, 01 Jul 2020 05:46:03 EDT Received: by smtp01.domein-it.com (Postfix, from userid 1000) id 20D018086D57; Wed, 1 Jul 2020 11:37:21 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on smtp01.domein-it.com X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=AWL,BAYES_00,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.0 Received: from ferret.domein-it.nl (ferret.domein-it.nl [84.244.139.72]) by smtp01.domein-it.com (Postfix) with ESMTP id 0B81A8086D5B for ; Wed, 1 Jul 2020 11:37:07 +0200 (CEST) Received: from 80-112-22-40.cable.dynamic.v4.ziggo.nl ([80.112.22.40]:48484 helo=ben.dynamic.ziggo.nl) by ferret.domein-it.nl with esmtpa (Exim 4.93) (envelope-from ) id 1jqZAs-00066q-Uv; Wed, 01 Jul 2020 11:37:03 +0200 From: Ben Wijen To: git@vger.kernel.org Cc: Ben Wijen Subject: [PATCH 1/2] git clone: check for non-empty directory Date: Wed, 1 Jul 2020 11:36:52 +0200 Message-Id: <20200701093653.3706-2-ben@wijen.net> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200701093653.3706-1-ben@wijen.net> References: <20200701093653.3706-1-ben@wijen.net> MIME-Version: 1.0 X-Domein-IT-MailScanner-Information: Please contact the ISP for more information X-Domein-IT-MailScanner-ID: 1jqZAs-00066q-Uv X-Domein-IT-MailScanner: Not scanned: please contact your Internet E-Mail Service Provider for details X-Domein-IT-MailScanner-SpamCheck: X-Domein-IT-MailScanner-From: ben@wijen.net Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When using git clone with --separate-git-dir realgitdir and realgitdir already exists, it's content is destroyed. Extend test to make sure content is left intact Signed-off-by: Ben Wijen --- t/t5601-clone.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh index 84ea2a3eb7..03901c55f2 100755 --- a/t/t5601-clone.sh +++ b/t/t5601-clone.sh @@ -271,7 +271,8 @@ test_expect_success 'fetch from gitfile parent' ' test_expect_success 'clone separate gitdir where target already exists' ' rm -rf dst && - test_must_fail git clone --separate-git-dir realgitdir src dst + test_must_fail git clone --separate-git-dir realgitdir src dst && + test -f realgitdir/config ' test_expect_success 'clone --reference from original' ' From patchwork Wed Jul 1 09:36:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Wijen X-Patchwork-Id: 11635941 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DFFE813B6 for ; Wed, 1 Jul 2020 09:46:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CA66E2073E for ; Wed, 1 Jul 2020 09:46:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729718AbgGAJqY (ORCPT ); Wed, 1 Jul 2020 05:46:24 -0400 Received: from smtp01.domein-it.com ([92.48.232.134]:59184 "EHLO smtp01.domein-it.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729236AbgGAJqX (ORCPT ); Wed, 1 Jul 2020 05:46:23 -0400 Received: by smtp01.domein-it.com (Postfix, from userid 1000) id 1038D8086D5B; Wed, 1 Jul 2020 11:37:35 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on smtp01.domein-it.com X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=AWL,BAYES_00,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.0 Received: from ferret.domein-it.nl (unknown [92.48.232.148]) by smtp01.domein-it.com (Postfix) with ESMTP id 26AE48086D5E for ; Wed, 1 Jul 2020 11:37:07 +0200 (CEST) Received: from 80-112-22-40.cable.dynamic.v4.ziggo.nl ([80.112.22.40]:48484 helo=ben.dynamic.ziggo.nl) by ferret.domein-it.nl with esmtpa (Exim 4.93) (envelope-from ) id 1jqZAt-00066q-2U; Wed, 01 Jul 2020 11:37:03 +0200 From: Ben Wijen To: git@vger.kernel.org Cc: Ben Wijen Subject: [PATCH 2/2] git clone: don't clone into non-empty directory Date: Wed, 1 Jul 2020 11:36:53 +0200 Message-Id: <20200701093653.3706-3-ben@wijen.net> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200701093653.3706-1-ben@wijen.net> References: <20200701093653.3706-1-ben@wijen.net> MIME-Version: 1.0 X-Domein-IT-MailScanner-Information: Please contact the ISP for more information X-Domein-IT-MailScanner-ID: 1jqZAt-00066q-2U X-Domein-IT-MailScanner: Not scanned: please contact your Internet E-Mail Service Provider for details X-Domein-IT-MailScanner-SpamCheck: X-Domein-IT-MailScanner-From: ben@wijen.net Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When using git clone with --separate-git-dir realgitdir and realgitdir already exists, it's content is destroyed. Make sure we don't clone into an existing non-empty directory. Signed-off-by: Ben Wijen --- builtin/clone.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/builtin/clone.c b/builtin/clone.c index bef70745c0..54a91acf06 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -946,7 +946,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) int is_bundle = 0, is_local; const char *repo_name, *repo, *work_tree, *git_dir; char *path, *dir, *display_repo = NULL; - int dest_exists; + int dest_exists, real_dest_exists = 0; const struct ref *refs, *remote_head; const struct ref *remote_head_points_at; const struct ref *our_head_points_at; @@ -1021,6 +1021,14 @@ int cmd_clone(int argc, const char **argv, const char *prefix) die(_("destination path '%s' already exists and is not " "an empty directory."), dir); + if (real_git_dir) { + real_dest_exists = path_exists(real_git_dir); + if (real_dest_exists && !is_empty_dir(real_git_dir)) + die(_("destination path '%s' already exists and is not " + "an empty directory."), real_git_dir); + } + + strbuf_addf(&reflog_msg, "clone: from %s", display_repo ? display_repo : repo); free(display_repo); @@ -1057,7 +1065,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) } if (real_git_dir) { - if (path_exists(real_git_dir)) + if (real_dest_exists) junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL; junk_git_dir = real_git_dir; } else {