Skip to content

Commit

Permalink
feat: expand tilde in template source path (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Oct 2, 2022
1 parent 2f71455 commit 682f917
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
7 changes: 7 additions & 0 deletions copier/main.py
Expand Up @@ -157,6 +157,13 @@ def __enter__(self):
return self

def __exit__(self, type, value, traceback):
if value is not None:
# exception was raised from code inside context manager:
# try to clean up, ignoring any exception, then re-raise
with suppress(Exception):
self._cleanup()
raise value
# otherwise clean up and let any exception bubble up
self._cleanup()

def _cleanup(self):
Expand Down
8 changes: 6 additions & 2 deletions copier/template.py
Expand Up @@ -207,8 +207,12 @@ def _cleanup(self) -> None:

@property
def _temp_clone(self) -> Optional[Path]:
if self.local_abspath != Path(self.url).absolute():
return self.local_abspath
clone_path = self.local_abspath
original_path = Path(self.url).expanduser()
with suppress(OSError): # triggered for URLs on Windows
original_path = original_path.resolve()
if clone_path != original_path:
return clone_path
return None

@cached_property
Expand Down
30 changes: 17 additions & 13 deletions copier/vcs.py
Expand Up @@ -68,23 +68,27 @@ def get_repo(url: str) -> OptStr:
- git+https://mywebsiteisagitrepo.example.com/
- /local/path/to/git/repo
- /local/path/to/git/bundle/file.bundle
- ~/path/to/git/repo
- ~/path/to/git/repo.bundle
"""
for pattern, replacement in REPLACEMENTS:
url = re.sub(pattern, replacement, url)

if url.endswith(GIT_POSTFIX) or url.startswith(GIT_PREFIX):
if url.startswith("git+"):
url = url[4:]
elif url.startswith("https://") and not url.endswith(GIT_POSTFIX):
url = "".join((url, GIT_POSTFIX))
return url

url_path = Path(url)
if not (
url.endswith(GIT_POSTFIX)
or url.startswith(GIT_PREFIX)
or is_git_repo_root(url_path)
or is_git_bundle(url_path)
):
return None

if url.startswith("git+"):
url = url[4:]
elif url.startswith("https://") and not url.endswith(GIT_POSTFIX):
url = "".join((url, GIT_POSTFIX))
return url
if url.startswith("~"):
url_path = url_path.expanduser()

if is_git_repo_root(url_path) or is_git_bundle(url_path):
return url_path.as_posix()

return None


def checkout_latest_tag(local_repo: StrOrPath, use_prereleases: OptBool = False) -> str:
Expand Down
40 changes: 39 additions & 1 deletion tests/test_vcs.py
@@ -1,7 +1,12 @@
import os
import shutil
from os.path import exists, join
from pathlib import Path

from copier import Worker, vcs
from plumbum import local
from plumbum.cmd import git

from copier import Worker, run_copy, run_update, vcs


def test_get_repo():
Expand Down Expand Up @@ -75,3 +80,36 @@ def test_dont_remove_local_clone(tmp_path):
with Worker(src_path=src_path, dst_path=tmp_path, defaults=True) as worker:
worker.run_copy()
assert exists(src_path)


def test_update_using_local_source_path_with_tilde(tmp_path):
# first, get a local repository clone
src_path = vcs.clone("https://github.com/copier-org/autopretty.git")

# then prepare the user path to this clone (starting with ~)
if os.name == "nt":
src_path = Path(src_path)
# in GitHub CI, the user in the temporary path is not the same as the current user:
# ["C:\\", "Users", "RUNNER~X"] vs. runneradmin
user = src_path.parts[2]
user_src_path = str(Path("~", "..", user, *src_path.parts[3:]))
else:
# temporary path is in /tmp, so climb back up from ~ using ../
user_src_path = f"~/{'/'.join(['..'] * len(Path.home().parts))}{src_path}"

# generate project and assert correct path in answers
worker = run_copy(src_path=user_src_path, dst_path=tmp_path, defaults=True)
assert worker.answers.combined["_src_path"] == user_src_path

# assert project update works and correct path again
with local.cwd(tmp_path):
git("init")
git("add", "-A")
git("commit", "-m", "init")
worker = run_update(
dst_path=tmp_path,
defaults=True,
overwrite=True,
answers_file=".copier-answers.autopretty.yml",
)
assert worker.answers.combined["_src_path"] == user_src_path

0 comments on commit 682f917

Please sign in to comment.