From patchwork Tue Apr 9 23:07:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Meyer X-Patchwork-Id: 10892839 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2F85417E0 for ; Tue, 9 Apr 2019 23:08:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A754288E6 for ; Tue, 9 Apr 2019 23:08:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E56628957; Tue, 9 Apr 2019 23:08:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D0123288E6 for ; Tue, 9 Apr 2019 23:08:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726723AbfDIXIB (ORCPT ); Tue, 9 Apr 2019 19:08:01 -0400 Received: from pb-smtp21.pobox.com ([173.228.157.53]:64001 "EHLO pb-smtp21.pobox.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726671AbfDIXIB (ORCPT ); Tue, 9 Apr 2019 19:08:01 -0400 Received: from pb-smtp21.pobox.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id A3F9F52A08; Tue, 9 Apr 2019 19:07:59 -0400 (EDT) (envelope-from kyle@kyleam.com) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=pobox.com; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=aI4ZLy0bcLjTR8+ia+GSZi7JI R4=; b=v+EsZvMP3LUl+Q9zEPg/KT9MKEdFHqPw5JNsxIPCuH7iVAJwiHRnQnEaG Ur7h+vC61QrJ7kbIPLhhja+UDGr4YsmbBh6tpUO+K8W4K5F3A+8x4SL7fCCA+uxt 1PC42XJx4WgE9ZV9zCuxKpyFxh709otuIKObtXDqvt09DyOJdY= Received: from pb-smtp21.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 9DB0A52A04; Tue, 9 Apr 2019 19:07:59 -0400 (EDT) (envelope-from kyle@kyleam.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=kyleam.com; h=from:to:cc:subject:date:message-id:in-reply-to:references:mime-version:content-transfer-encoding; s=mesmtp; bh=1steWBb7d9OVqYfLekljoKJG923KpOPJWIKAzGbEan8=; b=ytxytWtLH28UeBQMLsWnmj6UKcaK4ebmqtNdH74uoYLPIuIDYgw8utonih6JIv010k3ruHe5lMt6xjXI1TNW/KZqIOloYOmZs7Wz7cklURRZLkC4vKPTmVx0gbdDCaxlLusRfeDgt35NCf44hCFEwfW7tZXTOKnUp+L9Xrsj2p0= Received: from hylob.dartmouth.edu (unknown [129.170.31.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pb-smtp21.pobox.com (Postfix) with ESMTPSA id 53EA3529FF; Tue, 9 Apr 2019 19:07:54 -0400 (EDT) (envelope-from kyle@kyleam.com) From: Kyle Meyer To: git@vger.kernel.org Cc: Junio C Hamano , debian@onerussian.com, Kyle Meyer Subject: [PATCH v3 1/3] submodule: refuse to add repository with no commits Date: Tue, 9 Apr 2019 19:07:35 -0400 Message-Id: <20190409230737.26809-2-kyle@kyleam.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190409230737.26809-1-kyle@kyleam.com> References: <20190409230737.26809-1-kyle@kyleam.com> MIME-Version: 1.0 X-Pobox-Relay-ID: 4F25EBC4-5B1C-11E9-85C9-EE24A11ADF13-24757444!pb-smtp21.pobox.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When the path given to 'git submodule add' is an existing repository that is not in the index, the repository is passed to 'git add'. If this repository doesn't have a commit checked out, we don't get a useful result: there is no subproject OID to track, and any untracked files in the sub-repository are added as blobs in the top-level repository. To avoid getting into this state, abort if the path is a repository that doesn't have a commit checked out. Note that this check must come before the 'git add --dry-run' check because the next commit will make 'git add' fail when given a repository that doesn't have a commit checked out. Signed-off-by: Kyle Meyer --- git-submodule.sh | 7 +++++++ t/t7400-submodule-basic.sh | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/git-submodule.sh b/git-submodule.sh index 2c0fb6d723..c88a05086e 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -231,6 +231,13 @@ cmd_add() die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")" fi + if test -d "$sm_path" && + test -z $(git -C "$sm_path" rev-parse --show-cdup 2>/dev/null) + then + git -C "$sm_path" rev-parse --verify -q HEAD >/dev/null || + die "$(eval_gettext "'\$sm_path' does not have a commit checked out")" + fi + if test -z "$force" && ! git add --dry-run --ignore-missing --no-warn-embedded-repo "$sm_path" > /dev/null 2>&1 then diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh index aba2d4d6ee..a208cb26e1 100755 --- a/t/t7400-submodule-basic.sh +++ b/t/t7400-submodule-basic.sh @@ -46,6 +46,15 @@ test_expect_success 'submodule update aborts on missing gitmodules url' ' test_must_fail git submodule init ' +test_expect_success 'add aborts on repository with no commits' ' + cat >expect <<-\EOF && + '"'repo-no-commits'"' does not have a commit checked out + EOF + git init repo-no-commits && + test_must_fail git submodule add ../a ./repo-no-commits 2>actual && + test_i18ncmp expect actual +' + test_expect_success 'setup - repository in init subdirectory' ' mkdir init && ( @@ -809,7 +818,7 @@ test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.gi cp pristine-.git-config .git/config && cp pristine-.gitmodules .gitmodules && mkdir -p a/b/c && - (cd a/b/c && git init) && + (cd a/b/c && git init && test_commit msg) && git config remote.origin.url ../foo/bar.git && git submodule add ../bar/a/b/c ./a/b/c && git submodule init && From patchwork Tue Apr 9 23:07:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Meyer X-Patchwork-Id: 10892845 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 184F817E0 for ; Tue, 9 Apr 2019 23:08:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0197D24B44 for ; Tue, 9 Apr 2019 23:08:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E984B2892C; Tue, 9 Apr 2019 23:08:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 66E2B24B44 for ; Tue, 9 Apr 2019 23:08:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726774AbfDIXIJ (ORCPT ); Tue, 9 Apr 2019 19:08:09 -0400 Received: from pb-smtp21.pobox.com ([173.228.157.53]:59877 "EHLO pb-smtp21.pobox.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726685AbfDIXIJ (ORCPT ); Tue, 9 Apr 2019 19:08:09 -0400 Received: from pb-smtp21.pobox.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 7E44E52A10; Tue, 9 Apr 2019 19:08:01 -0400 (EDT) (envelope-from kyle@kyleam.com) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=pobox.com; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=B444AfZYNjWiWWu3SXcFZOvbT Wg=; b=sQOFAfckcL8aPW+1+OGxma3HHJnIsob5fbQBhUKZLhOPGp420jTZ7MlvR R/cHzXNG+3VRelSY0XYHcUZVW9FgkYKdwdNmfEKTv54HhGnkX6N5l5eEOWdKgy7d JsvZ+b975ulMwgJQ+z4mSsH2c5ugNqZLRfp6Q7cLEYOaOCxAnE= Received: from pb-smtp21.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 7782C52A0F; Tue, 9 Apr 2019 19:08:01 -0400 (EDT) (envelope-from kyle@kyleam.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=kyleam.com; h=from:to:cc:subject:date:message-id:in-reply-to:references:mime-version:content-transfer-encoding; s=mesmtp; bh=L2Cl4ihWgtsRKFb0Np7XaJqmCZUBwT7qT3nrHa1Yr1Y=; b=nZ+IGVOqL72lPQretIN/RjMxwM29ta95gnbpFTItMVDu75udHuney7X/l0q6HrNMalodV4608ES874TirMOxWAqnHyGRpHfXG0dsrYpxdXcqsc27Ennlh1ufs9r4b17xnF7XABk6pG3/uYLXsF3uodhxqiMCQZSYLHI2EbaFcVs= Received: from hylob.dartmouth.edu (unknown [129.170.31.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pb-smtp21.pobox.com (Postfix) with ESMTPSA id 88A6152A03; Tue, 9 Apr 2019 19:07:57 -0400 (EDT) (envelope-from kyle@kyleam.com) From: Kyle Meyer To: git@vger.kernel.org Cc: Junio C Hamano , debian@onerussian.com, Kyle Meyer Subject: [PATCH v3 2/3] dir: do not traverse repositories with no commits Date: Tue, 9 Apr 2019 19:07:36 -0400 Message-Id: <20190409230737.26809-3-kyle@kyleam.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190409230737.26809-1-kyle@kyleam.com> References: <20190409230737.26809-1-kyle@kyleam.com> MIME-Version: 1.0 X-Pobox-Relay-ID: 51107A26-5B1C-11E9-A248-EE24A11ADF13-24757444!pb-smtp21.pobox.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When treat_directory() encounters a directory that is not in the index and DIR_NO_GITLINKS is unset, it calls resolve_gitlink_ref() to decide if a directory looks like a repository, in which case the directory won't be traversed. As a result, 'status -uall' and 'ls-files -o' will show only the directory, even when there are untracked files within the directory. For the unusual case where a repository doesn't have a commit checked out, resolve_gitlink_ref() returns -1 because HEAD cannot be resolved, and the directory is treated as a normal directory (i.e. traversal does not stop at the repository boundary). The status and ls-files commands above list untracked files within the repository rather than showing only the top-level directory. And if 'git add' is called on a repository with no commit checked out, any untracked files under the repository are added as blobs in the top-level project, a behavior that is unlikely to be what the caller intended. The above case is a corner case in an already unusual situation of the working tree containing a repository that is not a tracked submodule, but we might as well treat anything that looks like a repository consistently. Loosen the "looks like a repository" criteria in treat_directory() by replacing resolve_gitlink_ref() with is_nonbare_repository_dir(), one of the checks that is performed downstream when resolve_gitlink_ref() is called. As the required update to t3700-add shows, calling 'git add' on a repository with no commit checked out will now raise an error. While this is the desired behavior, note that the output isn't yet appropriate. The next commit will improve this output. Signed-off-by: Kyle Meyer --- dir.c | 6 ++- t/t3009-ls-files-others-nonsubmodule.sh | 50 +++++++++++++++++++++++++ t/t3700-add.sh | 1 + 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100755 t/t3009-ls-files-others-nonsubmodule.sh diff --git a/dir.c b/dir.c index b2cabadf25..a4e59eb351 100644 --- a/dir.c +++ b/dir.c @@ -1467,9 +1467,11 @@ static enum path_treatment treat_directory(struct dir_struct *dir, return path_none; } if (!(dir->flags & DIR_NO_GITLINKS)) { - struct object_id oid; - if (resolve_gitlink_ref(dirname, "HEAD", &oid) == 0) + struct strbuf sb = STRBUF_INIT; + strbuf_addstr(&sb, dirname); + if (is_nonbare_repository_dir(&sb)) return exclude ? path_excluded : path_untracked; + strbuf_release(&sb); } return path_recurse; } diff --git a/t/t3009-ls-files-others-nonsubmodule.sh b/t/t3009-ls-files-others-nonsubmodule.sh new file mode 100755 index 0000000000..963f3462b7 --- /dev/null +++ b/t/t3009-ls-files-others-nonsubmodule.sh @@ -0,0 +1,50 @@ +#!/bin/sh + +test_description='test git ls-files --others with non-submodule repositories + +This test runs git ls-files --others with the following working tree: + + nonrepo-no-files/ + plain directory with no files + nonrepo-untracked-file/ + plain directory with an untracked file + repo-no-commit-no-files/ + git repository without a commit or a file + repo-no-commit-untracked-file/ + git repository without a commit but with an untracked file + repo-with-commit-no-files/ + git repository with a commit and no untracked files + repo-with-commit-untracked-file/ + git repository with a commit and an untracked file +' + +. ./test-lib.sh + +test_expect_success 'setup: directories' ' + mkdir nonrepo-no-files/ && + mkdir nonrepo-untracked-file && + : >nonrepo-untracked-file/untracked && + git init repo-no-commit-no-files && + git init repo-no-commit-untracked-file && + : >repo-no-commit-untracked-file/untracked && + git init repo-with-commit-no-files && + git -C repo-with-commit-no-files commit --allow-empty -mmsg && + git init repo-with-commit-untracked-file && + test_commit -C repo-with-commit-untracked-file msg && + : >repo-with-commit-untracked-file/untracked +' + +test_expect_success 'ls-files --others handles untracked git repositories' ' + git ls-files -o >output && + cat >expect <<-EOF && + nonrepo-untracked-file/untracked + output + repo-no-commit-no-files/ + repo-no-commit-untracked-file/ + repo-with-commit-no-files/ + repo-with-commit-untracked-file/ + EOF + test_cmp expect output +' + +test_done diff --git a/t/t3700-add.sh b/t/t3700-add.sh index be582a513b..5a8425962b 100755 --- a/t/t3700-add.sh +++ b/t/t3700-add.sh @@ -396,6 +396,7 @@ test_expect_success 'no file status change if no pathspec is given in subdir' ' ' test_expect_success 'all statuses changed in folder if . is given' ' + rm -fr empty && git add --chmod=+x . && test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 && git add --chmod=-x . && From patchwork Tue Apr 9 23:07:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Meyer X-Patchwork-Id: 10892843 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9A0E41669 for ; Tue, 9 Apr 2019 23:08:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8496524B44 for ; Tue, 9 Apr 2019 23:08:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 78ECD2892C; Tue, 9 Apr 2019 23:08:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0F78424B44 for ; Tue, 9 Apr 2019 23:08:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726766AbfDIXII (ORCPT ); Tue, 9 Apr 2019 19:08:08 -0400 Received: from pb-smtp21.pobox.com ([173.228.157.53]:55418 "EHLO pb-smtp21.pobox.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726671AbfDIXII (ORCPT ); Tue, 9 Apr 2019 19:08:08 -0400 Received: from pb-smtp21.pobox.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id A2C2352A17; Tue, 9 Apr 2019 19:08:04 -0400 (EDT) (envelope-from kyle@kyleam.com) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=pobox.com; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=+0lhzme+vNDLQTaizt7PIc8gZ 9Y=; b=h+Gi+AXsemKFMLFbBHTdoocT884tHJW5ukdvYUO5r/+oM1iQYdKZLdE2t /ONMoB5tkijTJwcBI4kksUAw7195PFz1dahmGUFr958JVmWzhPnelugIlIhwVEoV IBJucTKESRmNdsu0G1svlGDd2U9W4edlg9Oiusxtzy3A/R/wBQ= Received: from pb-smtp21.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 9B4F652A15; Tue, 9 Apr 2019 19:08:04 -0400 (EDT) (envelope-from kyle@kyleam.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=kyleam.com; h=from:to:cc:subject:date:message-id:in-reply-to:references:mime-version:content-transfer-encoding; s=mesmtp; bh=yHYCQgWWcgWi0LFyVdcLYncOjvn7HnV2AgAP1EadCf0=; b=elxvc2qidHFXkZijWFf1bQ36A+lxOKT+yKpRAV82CbiVEtw15v0BTXW7aBOadPEcSRfh9Sd/4Vf3YcMmFu6esvLzqgFD63QmSxmO9VRy+ZdXDt77fUwyAia78APuD+dFSk9DJ0fF16Ja9+I2vOd/xoryS6btuNKMmDQ3pPmyF0Y= Received: from hylob.dartmouth.edu (unknown [129.170.31.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pb-smtp21.pobox.com (Postfix) with ESMTPSA id BE8E952A0A; Tue, 9 Apr 2019 19:08:00 -0400 (EDT) (envelope-from kyle@kyleam.com) From: Kyle Meyer To: git@vger.kernel.org Cc: Junio C Hamano , debian@onerussian.com, Kyle Meyer Subject: [PATCH v3 3/3] add: error appropriately on repository with no commits Date: Tue, 9 Apr 2019 19:07:37 -0400 Message-Id: <20190409230737.26809-4-kyle@kyleam.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190409230737.26809-1-kyle@kyleam.com> References: <20190409230737.26809-1-kyle@kyleam.com> MIME-Version: 1.0 X-Pobox-Relay-ID: 52FBBE4A-5B1C-11E9-B7DE-EE24A11ADF13-24757444!pb-smtp21.pobox.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The previous commit made 'git add' abort when given a repository that doesn't have a commit checked out. However, the output upon failure isn't appropriate: % git add repo warning: adding embedded git repository: repo hint: You've added another git repository inside your current repository. hint: [...] error: unable to index file 'repo/' fatal: adding files failed The hint doesn't apply in this case, and the error message doesn't tell the user why 'repo' couldn't be added to the index. Provide better output by teaching add_to_index() to error when given a git directory where HEAD can't be resolved. To avoid the embedded repository warning and hint, call check_embedded_repo() only after add_file_to_index() succeeds because, in general, its output doesn't make sense if adding to the index fails. Signed-off-by: Kyle Meyer --- builtin/add.c | 3 ++- read-cache.c | 3 +++ t/t3700-add.sh | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/builtin/add.c b/builtin/add.c index db2dfa4350..dd18e5c9b6 100644 --- a/builtin/add.c +++ b/builtin/add.c @@ -374,11 +374,12 @@ static int add_files(struct dir_struct *dir, int flags) } for (i = 0; i < dir->nr; i++) { - check_embedded_repo(dir->entries[i]->name); if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) { if (!ignore_add_errors) die(_("adding files failed")); exit_status = 1; + } else { + check_embedded_repo(dir->entries[i]->name); } } return exit_status; diff --git a/read-cache.c b/read-cache.c index 4dc6de1b55..b4b68d6ec8 100644 --- a/read-cache.c +++ b/read-cache.c @@ -708,6 +708,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st, int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| (intent_only ? ADD_CACHE_NEW_ONLY : 0)); int hash_flags = HASH_WRITE_OBJECT; + struct object_id oid; if (flags & ADD_CACHE_RENORMALIZE) hash_flags |= HASH_RENORMALIZE; @@ -717,6 +718,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st, namelen = strlen(path); if (S_ISDIR(st_mode)) { + if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) + return error(_("'%s' does not have a commit checked out"), path); while (namelen && path[namelen-1] == '/') namelen--; } diff --git a/t/t3700-add.sh b/t/t3700-add.sh index 5a8425962b..c325167b90 100755 --- a/t/t3700-add.sh +++ b/t/t3700-add.sh @@ -296,6 +296,17 @@ test_expect_success '"git add ." in empty repo' ' ) ' +test_expect_success 'error on a repository with no commits' ' + rm -fr empty && + git init empty && + test_must_fail git add empty >actual 2>&1 && + cat >expect <<-EOF && + error: '"'empty/'"' does not have a commit checked out + fatal: adding files failed + EOF + test_i18ncmp expect actual +' + test_expect_success 'git add --dry-run of existing changed file' " echo new >>track-this && git add --dry-run track-this >actual 2>&1 &&