Skip to content

Commit 8808cab

Browse files
authored
fix: http-based generation (#96)
1 parent dbb61ab commit 8808cab

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

hooks/post_gen_project.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import git
1919
import yaml
20+
from cookiecutter.repository import expand_abbreviations
2021

2122
LOG_FORMAT = json.dumps(
2223
{
@@ -59,23 +60,33 @@ def get_context() -> dict:
5960

6061
try:
6162
if Path(template).is_absolute():
62-
template_path = Path(template).resolve()
63+
template_path: Path = Path(template).resolve()
6364
else:
64-
output_path = Path(output).resolve()
65-
template_path = output_path.joinpath(template)
65+
output_path: Path = Path(output).resolve()
66+
template_path: Path = output_path.joinpath(template)
6667

67-
repo = git.Repo(template_path)
68+
# IMPORTANT: If the specified template is remote (http/git/ssh) this SHOULD raise an exception. The remote logic is in the except block
69+
repo: git.Repo = git.Repo(template_path)
6870

6971
# Expect this is a local template
70-
branch = str(repo.active_branch)
71-
dirty = repo.is_dirty(untracked_files=True)
72+
branch: str = str(repo.active_branch)
73+
dirty: bool = repo.is_dirty(untracked_files=True)
7274
template_commit_hash = git.cmd.Git().ls_remote(template_path, "HEAD")[:40]
7375
except (git.exc.InvalidGitRepositoryError, git.exc.NoSuchPathError):
74-
# Expect this is a remote template
75-
template_repo = template
76+
# This exception handling occurs every time the template repo is remote
77+
78+
# From https://github.com/cookiecutter/cookiecutter/blob/1b8520e7075175db4a3deae85e71081730ca7ad1/cookiecutter/config.py#L15
79+
abbreviations: dict[str, str] = {
80+
"gh": "https://github.com/{0}.git",
81+
"gl": "https://gitlab.com/{0}.git",
82+
"bb": "https://bitbucket.org/{0}",
83+
}
84+
template_repo: str = expand_abbreviations(template, abbreviations)
85+
7686
# This currently assumes main until https://github.com/cookiecutter/cookiecutter/issues/1759 is resolved
77-
branch = "main"
78-
dirty = False
87+
branch: str = "main"
88+
dirty: bool = False
89+
7990
template_commit_hash = git.cmd.Git().ls_remote(template_repo, branch)[:40]
8091

8192
context: dict[

0 commit comments

Comments
 (0)