Skip to content

Commit ec19e44

Browse files
fix(vcs): fix cloning local dirty template repo when core.fsmonitor=true (#2151)
Signed-off-by: Kamontat Chantrachirathumrong <14089557+kamontat@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 1e16b65 commit ec19e44

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

copier/_vcs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ def clone(url: str, ref: str | None = None) -> str:
212212
)
213213

214214
with local.cwd(location):
215-
git("checkout", "-f", ref or "HEAD")
215+
## The `git checkout -f <ref>` command doesn't works when repo is local, dirty and core.fsmonitor is enabled
216+
## ref: https://github.com/copier-org/copier/issues/1887
217+
git("-c", "core.fsmonitor=false", "checkout", "-f", ref or "HEAD")
216218
git("submodule", "update", "--checkout", "--init", "--recursive", "--force")
217219

218220
return location

tests/test_vcs.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
import pytest
88
from packaging.version import Version
99
from plumbum import local
10+
from pytest_gitconfig.plugin import GitConfig
1011

1112
from copier import run_copy, run_update
1213
from copier._main import Worker
1314
from copier._user_data import load_answersfile_data
1415
from copier._vcs import checkout_latest_tag, clone, get_git_version, get_repo
15-
from copier.errors import ShallowCloneWarning
16+
from copier.errors import DirtyLocalWarning, ShallowCloneWarning
1617

17-
from .helpers import git
18+
from .helpers import build_file_tree, git, git_save
1819

1920

2021
def test_get_repo() -> None:
@@ -88,6 +89,33 @@ def test_local_clone() -> None:
8889
shutil.rmtree(local_tmp, ignore_errors=True)
8990

9091

92+
def test_local_dirty_clone(
93+
tmp_path_factory: pytest.TempPathFactory, gitconfig: GitConfig
94+
) -> None:
95+
"""
96+
When core.fsmonitor is enabled, normal `git checkout` command won't works.
97+
"""
98+
99+
gitconfig.set({"core.fsmonitor": "true"})
100+
src = tmp_path_factory.mktemp("src")
101+
print(src)
102+
103+
build_file_tree({src / "version.txt": "0.1.0"})
104+
git_save(src)
105+
106+
build_file_tree({src / "version.txt": "0.2.0", src / "README.md": "hello world"})
107+
108+
with pytest.warns(DirtyLocalWarning):
109+
local_tmp = clone(str(src))
110+
111+
assert local_tmp
112+
assert Path(local_tmp, "version.txt").exists()
113+
assert Path(local_tmp, "version.txt").read_text() == "0.2.0"
114+
assert Path(local_tmp, "README.md").exists()
115+
assert Path(local_tmp, "README.md").read_text() == "hello world"
116+
shutil.rmtree(local_tmp, ignore_errors=True)
117+
118+
91119
@pytest.mark.impure
92120
def test_shallow_clone(tmp_path: Path, recwarn: pytest.WarningsRecorder) -> None:
93121
# This test should always work but should be much slower if `is_git_shallow_repo()` is not

0 commit comments

Comments
 (0)