From patchwork Tue Apr 9 23:07:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyle Meyer X-Patchwork-Id: 10892841 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 77A4117E0 for ; Tue, 9 Apr 2019 23:08:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6174624B44 for ; Tue, 9 Apr 2019 23:08:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5560C2892C; Tue, 9 Apr 2019 23:08:09 +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 736B6288E6 for ; Tue, 9 Apr 2019 23:08:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726755AbfDIXIH (ORCPT ); Tue, 9 Apr 2019 19:08:07 -0400 Received: from pb-smtp21.pobox.com ([173.228.157.53]:50362 "EHLO pb-smtp21.pobox.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726592AbfDIXIH (ORCPT ); Tue, 9 Apr 2019 19:08:07 -0400 Received: from pb-smtp21.pobox.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 1F39B52A01; Tue, 9 Apr 2019 19:07:55 -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:mime-version:content-transfer-encoding; s=sasl; bh=CHaPkeTwr30ehKDXedRU7HMOI2s=; b=LcnsyBUlaXaSig81AfIn K5ZmtNImYh/GmJkkdOhYbGnvyIgFaDBFBVKH88fcQZgfZU7n5h/0/n8xDVWZju0A 0aomuv5Csz4gtEb2jOPfR3qepkLDLMQlpnUr5cxuuIEfLWIFAOT9OtqIO65mIJJo FLwytFGe6nrtG7mRRhWVYQM= Received: from pb-smtp21.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 1734552A00; Tue, 9 Apr 2019 19:07:55 -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:mime-version:content-transfer-encoding; s=mesmtp; bh=FWMjmk9ppppbrr0wFYCggyJXkx0zwcW50vIABTciqso=; b=ObR4x0NNRTIle0Eyr8klfR+xwr/ozFwGdKJU4X/PXGYkheXs2wD0cXY+60V2Y/41q7g2rPuwUb9L2WYAv8yE57TC5v7YCKFm8l/e6KN0ZaSQt77K6pJ8meOVEisYZv9lJcFbv6cKsv3ZHtbeLgAKyNanJ1iXhgCj8BFlw+fUT0k= 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 1B618529FA; Tue, 9 Apr 2019 19:07:52 -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 0/3] dir: treat a repository without commits as a repository Date: Tue, 9 Apr 2019 19:07:34 -0400 Message-Id: <20190409230737.26809-1-kyle@kyleam.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 X-Pobox-Relay-ID: 4D392114-5B1C-11E9-8A42-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 This is a reroll of <20190402183505.31512-1-kyle@kyleam.com>. Thanks, Junio, for your comments on v2. The series improves the handling of sub-repositories that don't have a commit checked out. In particular, it addresses the following issues: * Calling 'git add repo' is meant to register the repository as a submodule, but, for a sub-repository that doesn't have a commit checked out, it is at best a no-op. If the sub-repository has any untracked files, they are added as blobs in the top-level repository. Instead error in this situation. * 'git submodule add' calls 'git add' on a sub-repository, so it has the same issues as above. Plus it modifies .gitmodules even though there is no sub-project OID to register. Instead error in this situation. * Commands like 'ls-files -o' and 'status -uall' usually stop traversing at sub-repository boundaries, but they don't if the sub-repository doesn't have a commit checked out. Instead stop traversing at a sub-repository even if it doesn't have a commit checked out. Main changes from v2: * Leave be the "non-submodule .git" test from t3000-ls-files-others.sh rather than confusingly using it as the basis for the new t3009-ls-files-others-nonsubmodule.sh (<87d0m2bui0.fsf@kyleam.com>). * Drop unneeded setup function in test (<87d0m2bui0.fsf@kyleam.com>). * Provide appropriate output for the 'git add repo-no-commits' failure (<87bm1mbua4.fsf@kyleam.com>). Other changes from v2: * Commit message tweaks. These include replacing wording like "no commits" with something like "no commit checked out" because, technically, this series also concerns repositories that have commit objects but are on an unborn branch. * Change the "git submodule add" error message to match the wording of the new error message added in add_to_index(). * Consistently use "expect" rather than "expected" as the output file name for expected test output. Kyle Meyer (3): submodule: refuse to add repository with no commits dir: do not traverse repositories with no commits add: error appropriately on repository with no commits builtin/add.c | 3 +- dir.c | 6 ++- git-submodule.sh | 7 ++++ read-cache.c | 3 ++ t/t3009-ls-files-others-nonsubmodule.sh | 50 +++++++++++++++++++++++++ t/t3700-add.sh | 12 ++++++ t/t7400-submodule-basic.sh | 11 +++++- 7 files changed, 88 insertions(+), 4 deletions(-) create mode 100755 t/t3009-ls-files-others-nonsubmodule.sh Range-diff against v2: 1: b080e2c557 ! 1: ad1b0b44e8 submodule: refuse to add repository with no commits @@ -4,14 +4,16 @@ 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 any commits, we don't get a useful - result: there is no subproject OID to track, and any untracked files - in the sub-repository are added to the current repository. + 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. - Detect if the path is a repository with no commits and abort to avoid - getting into this state. Note that this check must come before the - 'git add --dry-run' check because an upcoming commit will make 'git - add' fail in this situation. + 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 @@ -26,7 +28,7 @@ + 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 any commits")" ++ die "$(eval_gettext "'\$sm_path' does not have a commit checked out")" + fi + if test -z "$force" && @@ -41,12 +43,12 @@ ' +test_expect_success 'add aborts on repository with no commits' ' -+ cat >expected <<-\EOF && -+ '"'repo-no-commits'"' does not have any 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 expected actual ++ test_i18ncmp expect actual +' + test_expect_success 'setup - repository in init subdirectory' ' 2: c027701842 < -: ---------- t3000: move non-submodule repo test to separate file 3: 97f53e30c0 < -: ---------- t3009: test that ls-files -o traverses bogus repo 4: a926b87102 ! 2: 1bf762bbd3 dir: do not traverse repositories with no commits @@ -9,12 +9,15 @@ will show only the directory, even when there are untracked files within the directory. - For the unusual case where a repository doesn't have any commits, - 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 + 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. + 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, @@ -22,23 +25,12 @@ 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 with an empty - repository. + downstream when resolve_gitlink_ref() is called. - As the required update to t3700-add shows, being looser with the check - means that we're stricter when adding empty repositories to the index: - - % 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 - - That error message isn't particularly helpful in this situation, but - it seems preferable to the old behavior of adding the repository's - untracked files. And if the caller really wants the previous - behavior, they can get it by adding a trailing slash. + 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 @@ -61,12 +53,20 @@ } diff --git a/t/t3009-ls-files-others-nonsubmodule.sh b/t/t3009-ls-files-others-nonsubmodule.sh - --- a/t/t3009-ls-files-others-nonsubmodule.sh + new file mode 100755 + --- /dev/null +++ b/t/t3009-ls-files-others-nonsubmodule.sh @@ - directory with no files aside from a bogus .git file - repo-bogus-untracked-file/ - directory with a bogus .git file and another untracked file ++#!/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/ @@ -75,26 +75,14 @@ + 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 -@@ - expected - output - repo-bogus-untracked-file/untracked -+ repo-no-commit-no-files/ -+ repo-no-commit-untracked-file/ -+ repo-with-commit-no-files/ -+ repo-with-commit-untracked-file/ - EOF - ' - -@@ - echo foo >repo-bogus-no-files/.git && - mkdir repo-bogus-untracked-file && - echo foo >repo-bogus-untracked-file/.git && -- : >repo-bogus-untracked-file/untracked -+ : >repo-bogus-untracked-file/untracked && ++' ++ ++. ./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 && @@ -103,9 +91,22 @@ + 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 non-submodule .git' ' ++' ++ ++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 --- a/t/t3700-add.sh -: ---------- > 3: a2f9af5448 add: error appropriately on repository with no commits