repo: open: handle bare git repos

If we are asked to open a potentially unitialized repo, we need to make sure
it is not a bare git repo, so that we don't accidentally start putting dvc
files inside git control dir.

Discovered while working on #8789
This commit is contained in:
Ruslan Kuprieiev
2023-03-16 02:03:31 +02:00
parent 33a2449c91
commit 990e834f1d
2 changed files with 28 additions and 0 deletions
+2
View File
@@ -125,6 +125,8 @@ class Repo:
if not scm:
try:
scm = SCM(root_dir or os.curdir)
if scm.dulwich.repo.bare:
raise NotDvcRepoError(f"{scm.root_dir} is a bare git repo")
except SCMError:
scm = SCM(os.curdir, no_scm=True)
+26
View File
@@ -1,6 +1,32 @@
import os
from dvc.cachemgr import CacheManager
from dvc.dvcfile import LOCK_FILE, PROJECT_FILE
from dvc.fs import system
from dvc.repo import Repo
from dvc.scm import Git
def test_open_bare(tmp_dir, scm, dvc, tmp_path_factory):
tmp_dir.dvc_gen(
{
"dir123": {"foo": "foo content"},
"dirextra": {"extrafoo": "extra foo content"},
},
commit="initial",
)
url = os.fspath(tmp_path_factory.mktemp("bare"))
Git.init(url, bare=True).close()
scm.gitpython.repo.create_remote("origin", url)
scm.gitpython.repo.remote("origin").push("master")
with Repo.open(url) as repo:
assert repo.scm.root_dir != url
with Repo.open(url, uninitialized=True) as repo:
assert repo.scm.root_dir != url
def test_destroy(tmp_dir, dvc, run_copy):